1 | #ifndef __GATHERER_CONNECTOR_HPP__ |
---|
2 | #define __GATHERER_CONNECTOR_HPP__ |
---|
3 | |
---|
4 | #include "xios_spl.hpp" |
---|
5 | #include "array_new.hpp" |
---|
6 | #include "distributed_view.hpp" |
---|
7 | #include "mpi.hpp" |
---|
8 | #include "local_view.hpp" |
---|
9 | #include "distributed_view.hpp" |
---|
10 | #include "context_client.hpp" |
---|
11 | |
---|
12 | |
---|
13 | namespace xios |
---|
14 | { |
---|
15 | |
---|
16 | class CGathererConnector |
---|
17 | { |
---|
18 | private: |
---|
19 | CDistributedView* srcView_; |
---|
20 | CLocalView* dstView_; |
---|
21 | map<int, vector<int>> connector_ ; |
---|
22 | map<int, vector<bool>> mask_ ; // mask is on src view |
---|
23 | int dstSize_ ; |
---|
24 | map<int,int> srcSize_ ; |
---|
25 | |
---|
26 | public: |
---|
27 | CGathererConnector(CDistributedView* srcView, CLocalView* dstView) : srcView_(srcView), dstView_(dstView) {} ; |
---|
28 | void computeConnector(void) ; |
---|
29 | |
---|
30 | template<typename T> |
---|
31 | void transfer(int sizeT, map<int, CArray<T,1>>& dataIn, CArray<T,1>& dataOut) |
---|
32 | { |
---|
33 | // for future, make a specific transfer function for sizeT=1 to avoid multiplication (increasing performance) |
---|
34 | dataOut.resize(dstSize_*sizeT) ; |
---|
35 | T* output = dataOut.dataFirst() ; |
---|
36 | for(auto& data : dataIn) |
---|
37 | { |
---|
38 | int rank=data.first ; |
---|
39 | auto input = data.second.dataFirst() ; |
---|
40 | auto& connector=connector_[rank] ; |
---|
41 | auto& mask=mask_[rank] ; |
---|
42 | int size=mask.size() ; |
---|
43 | |
---|
44 | for(int i=0, j=0 ;i<size;i++) |
---|
45 | { |
---|
46 | if (mask[i]) |
---|
47 | { |
---|
48 | int cj = connector[j]*sizeT ; |
---|
49 | int ci = i*sizeT ; |
---|
50 | for (int k=0;k<sizeT;k++) output[cj+k] = input[ci+k] ; |
---|
51 | j++ ; |
---|
52 | } |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | template<typename T> |
---|
58 | void transfer(map<int, CArray<T,1>>& dataIn, CArray<T,1>& dataOut) |
---|
59 | { |
---|
60 | transfer(1,dataIn,dataOut) ; |
---|
61 | } |
---|
62 | |
---|
63 | template<typename T> |
---|
64 | void transfer(int rank, CGathererConnector** connectors, int nConnectors, const T* input, T* output) |
---|
65 | { |
---|
66 | auto& connector = connector_[rank] ; // probably costly, find a better way to avoid the map |
---|
67 | auto& mask = mask_[rank] ; |
---|
68 | int srcSize = mask.size() ; |
---|
69 | |
---|
70 | if (nConnectors==0) |
---|
71 | { |
---|
72 | for(int i=0, j=0; i<srcSize; i++) |
---|
73 | if (mask[i]) |
---|
74 | { |
---|
75 | *(output+connector[j]) = *(input + i) ; |
---|
76 | j++ ; |
---|
77 | } |
---|
78 | |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | int srcSliceSize = (*(connectors-1))->getSrcSliceSize(rank, connectors-1, nConnectors-1) ; |
---|
83 | int dstSliceSize = (*(connectors-1))->getDstSliceSize(connectors-1, nConnectors-1) ; |
---|
84 | |
---|
85 | const T* in = input ; |
---|
86 | for(int i=0,j=0;i<srcSize;i++) |
---|
87 | { |
---|
88 | if (mask[i]) |
---|
89 | { |
---|
90 | (*(connectors-1))->transfer(rank, connectors-1, nConnectors-1, in, output+connector[j]*dstSliceSize) ; // the multiplication must be avoid in further optimization |
---|
91 | j++ ; |
---|
92 | } |
---|
93 | in += srcSliceSize ; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | template<typename T> |
---|
101 | void transfer(map<int, CArray<T,1>>& dataIn, CArray<T,1>& dataOut, T missingValue) |
---|
102 | { |
---|
103 | transfer(1, dataIn, dataout, missingValue) |
---|
104 | } |
---|
105 | |
---|
106 | template<typename T> |
---|
107 | void transfer(int sizeT, map<int, CArray<T,1>>& dataIn, CArray<T,1>& dataOut, T missingValue) |
---|
108 | { |
---|
109 | dataOut.resize(dstSize_*sizeT) ; |
---|
110 | dataOut=missingValue ; |
---|
111 | transfer(sizeT, dataIn, dataOut) ; |
---|
112 | } |
---|
113 | |
---|
114 | template<typename T> |
---|
115 | void transfer(CEventServer& event, int sizeT, CArray<T,1>& dataOut) |
---|
116 | { |
---|
117 | map<int, CArray<T,1>> dataIn ; |
---|
118 | for (auto& subEvent : event.subEvents) |
---|
119 | { |
---|
120 | auto& data = dataIn[subEvent.rank]; |
---|
121 | (*subEvent.buffer) >> data ; |
---|
122 | } |
---|
123 | transfer(sizeT, dataIn, dataOut) ; |
---|
124 | } |
---|
125 | |
---|
126 | template<typename T> |
---|
127 | void transfer(CEventServer& event, CArray<T,1>& dataOut) |
---|
128 | { |
---|
129 | transfer(event, 1, dataOut) ; |
---|
130 | } |
---|
131 | |
---|
132 | template<typename T> |
---|
133 | void transfer(CEventServer& event, int sizeT, CArray<T,1>& dataOut, T missingValue) |
---|
134 | { |
---|
135 | map<int, CArray<T,1>> dataIn ; |
---|
136 | for (auto& subEvent : event.subEvents) |
---|
137 | { |
---|
138 | auto& data = dataIn[subEvent.rank]; |
---|
139 | (*subEvent.buffer) >> data ; |
---|
140 | } |
---|
141 | transfer(sizeT, dataIn, dataOut, missingValue) ; |
---|
142 | } |
---|
143 | |
---|
144 | template<typename T> |
---|
145 | void transfer(CEventServer& event, CArray<T,1>& dataOut, T missingValue) |
---|
146 | { |
---|
147 | map<int, CArray<T,1>> dataIn ; |
---|
148 | for (auto& subEvent : event.subEvents) |
---|
149 | { |
---|
150 | auto& data = dataIn[subEvent.rank]; |
---|
151 | (*subEvent.buffer) >> data ; |
---|
152 | } |
---|
153 | transfer(1, dataIn, dataOut, missingValue) ; |
---|
154 | } |
---|
155 | |
---|
156 | int getSrcSliceSize(int rank, CGathererConnector** connectors, int nConnectors) |
---|
157 | { if (nConnectors==0) return srcSize_[rank] ; else return srcSize_[rank] * (*(connectors-1))->getSrcSliceSize(rank, connectors-1,nConnectors-1) ; } |
---|
158 | |
---|
159 | int getDstSliceSize(CGathererConnector** connectors, int nConnectors) |
---|
160 | { if (nConnectors==0) return dstSize_ ; else return dstSize_ * (*(connectors-1))->getDstSliceSize(connectors-1,nConnectors-1) ; } |
---|
161 | |
---|
162 | int getDstSize(void) {return dstSize_ ;} |
---|
163 | } ; |
---|
164 | |
---|
165 | } |
---|
166 | |
---|
167 | #endif |
---|