[1918] | 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" |
---|
[2291] | 11 | #include "reduction_types.hpp" |
---|
[1918] | 12 | |
---|
| 13 | |
---|
| 14 | namespace xios |
---|
| 15 | { |
---|
| 16 | |
---|
| 17 | class CGathererConnector |
---|
| 18 | { |
---|
| 19 | private: |
---|
[2267] | 20 | shared_ptr<CDistributedView> srcView_; |
---|
| 21 | shared_ptr<CLocalView> dstView_; |
---|
[1918] | 22 | map<int, vector<int>> connector_ ; |
---|
| 23 | map<int, vector<bool>> mask_ ; // mask is on src view |
---|
| 24 | int dstSize_ ; |
---|
| 25 | map<int,int> srcSize_ ; |
---|
| 26 | |
---|
| 27 | public: |
---|
[2267] | 28 | CGathererConnector(shared_ptr<CDistributedView> srcView, shared_ptr<CLocalView> dstView) : srcView_(srcView), dstView_(dstView) {} ; |
---|
[1918] | 29 | void computeConnector(void) ; |
---|
| 30 | |
---|
| 31 | template<typename T> |
---|
[2291] | 32 | void transfer(int repeat, int sizeT, map<int, CArray<T,1>>& dataIn, CArray<T,1>& dataOut, EReduction op = EReduction::none) |
---|
[1918] | 33 | { |
---|
[1930] | 34 | // for future, make a specific transfer function for sizeT=1 to avoid multiplication (increasing performance) |
---|
[2291] | 35 | |
---|
[1984] | 36 | size_t dstSlice = dstSize_*sizeT ; |
---|
| 37 | dataOut.resize(repeat* dstSlice) ; |
---|
| 38 | |
---|
[2291] | 39 | |
---|
| 40 | if (op == EReduction::none) // tranfer without reduction |
---|
[1918] | 41 | { |
---|
[2291] | 42 | for(auto& data : dataIn) |
---|
| 43 | { |
---|
| 44 | T* output = dataOut.dataFirst() ; |
---|
| 45 | int rank=data.first ; |
---|
| 46 | auto input = data.second.dataFirst() ; |
---|
| 47 | auto& connector=connector_[rank] ; |
---|
| 48 | auto& mask=mask_[rank] ; |
---|
| 49 | int size=mask.size() ; |
---|
| 50 | size_t srcSlice = size * sizeT ; |
---|
| 51 | for(int l=0; l<repeat; l++) |
---|
| 52 | { |
---|
| 53 | for(int i=0, j=0 ;i<size;i++) |
---|
[1984] | 54 | { |
---|
[2291] | 55 | if (mask[i]) |
---|
| 56 | { |
---|
| 57 | int cj = connector[j]*sizeT ; |
---|
| 58 | int ci = i*sizeT ; |
---|
| 59 | for (int k=0;k<sizeT;k++) output[cj+k] = input[ci+k] ; |
---|
| 60 | j++ ; |
---|
| 61 | } |
---|
[1984] | 62 | } |
---|
[2291] | 63 | input+=srcSlice ; |
---|
| 64 | output+=dstSlice ; |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | else // with reduction, to be optimized, see how to vectorize |
---|
| 69 | { |
---|
| 70 | vector<int> vcount(dataOut.size(),0) ; |
---|
| 71 | int* count = vcount.data() ; |
---|
| 72 | T defaultValue = std::numeric_limits<T>::quiet_NaN(); |
---|
| 73 | for(auto& data : dataIn) |
---|
| 74 | { |
---|
| 75 | T* output = dataOut.dataFirst() ; |
---|
| 76 | int rank=data.first ; |
---|
| 77 | auto input = data.second.dataFirst() ; |
---|
| 78 | auto& connector=connector_[rank] ; |
---|
| 79 | auto& mask=mask_[rank] ; |
---|
| 80 | int size=mask.size() ; |
---|
| 81 | size_t srcSlice = size * sizeT ; |
---|
| 82 | for(int l=0; l<repeat; l++) |
---|
| 83 | { |
---|
| 84 | for(int i=0, j=0 ;i<size;i++) |
---|
| 85 | { |
---|
| 86 | if (mask[i]) |
---|
| 87 | { |
---|
| 88 | int cj = connector[j]*sizeT ; |
---|
| 89 | int ci = i*sizeT ; |
---|
| 90 | for (int k=0;k<sizeT;k++) |
---|
| 91 | { |
---|
| 92 | if (!std::isnan(input[ci+k])) // manage missing value |
---|
| 93 | { |
---|
| 94 | if (count[cj+k]==0) output[cj+k] = input[ci+k] ; |
---|
| 95 | else |
---|
| 96 | { |
---|
| 97 | switch(op) |
---|
| 98 | { |
---|
| 99 | case EReduction::sum : |
---|
| 100 | output[cj+k] += input[ci+k] ; |
---|
| 101 | break ; |
---|
| 102 | case EReduction::min : |
---|
| 103 | output[cj+k]= std::min(output[cj+k],input[ci+k]) ; |
---|
| 104 | break ; |
---|
| 105 | case EReduction::max : |
---|
| 106 | output[cj+k]= std::max(output[cj+k],input[ci+k]) ; |
---|
| 107 | break ; |
---|
| 108 | case EReduction::average : |
---|
| 109 | output[cj+k] += input[ci+k] ; |
---|
| 110 | break ; |
---|
| 111 | default : |
---|
| 112 | ERROR("CGathererConnector::transfer", |
---|
| 113 | <<"reduction operator "<<(int)op<<" is not defined for this operation") ; |
---|
| 114 | break ; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | count[cj+k]++ ; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | j++ ; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | input+=srcSlice ; |
---|
| 124 | output+=dstSlice ; |
---|
| 125 | count+=dstSlice ; |
---|
[1918] | 126 | } |
---|
| 127 | } |
---|
[2291] | 128 | |
---|
| 129 | T* output = dataOut.dataFirst() ; |
---|
| 130 | if (op==EReduction::average) |
---|
| 131 | for(int i=0; i < dataOut.size() ; i++) |
---|
| 132 | { |
---|
| 133 | if (count[i]>0) dataOut[i]/=count[i] ; |
---|
| 134 | else dataOut[i] = defaultValue ; |
---|
| 135 | } |
---|
| 136 | else for(int i=0; i < dataOut.size() ; i++) if (count[i]==0) dataOut[i] = defaultValue ; |
---|
[1918] | 137 | } |
---|
| 138 | } |
---|
[1984] | 139 | |
---|
| 140 | template<typename T> |
---|
[2291] | 141 | void transfer(int sizeT, map<int, CArray<T,1>>& dataIn, CArray<T,1>& dataOut, EReduction op = EReduction::none) |
---|
[1984] | 142 | { |
---|
[2291] | 143 | transfer(1, sizeT, dataIn, dataOut, op) ; |
---|
[1984] | 144 | } |
---|
[1918] | 145 | |
---|
[1930] | 146 | template<typename T> |
---|
[2291] | 147 | void transfer(map<int, CArray<T,1>>& dataIn, CArray<T,1>& dataOut, EReduction op = EReduction::none) |
---|
[1930] | 148 | { |
---|
[2291] | 149 | transfer(1,dataIn,dataOut, op) ; |
---|
[1930] | 150 | } |
---|
[1918] | 151 | |
---|
| 152 | template<typename T> |
---|
[2291] | 153 | void transfer(int rank, shared_ptr<CGathererConnector>* connectors, int nConnectors, const T* input, T* output, EReduction op = EReduction::none, int* count=nullptr) |
---|
[1918] | 154 | { |
---|
| 155 | auto& connector = connector_[rank] ; // probably costly, find a better way to avoid the map |
---|
| 156 | auto& mask = mask_[rank] ; |
---|
| 157 | int srcSize = mask.size() ; |
---|
| 158 | |
---|
| 159 | if (nConnectors==0) |
---|
| 160 | { |
---|
[2291] | 161 | if (op == EReduction::none) |
---|
| 162 | { |
---|
| 163 | for(int i=0, j=0; i<srcSize; i++) |
---|
| 164 | if (mask[i]) |
---|
| 165 | { |
---|
| 166 | *(output+connector[j]) = *(input + i) ; |
---|
| 167 | j++ ; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | { |
---|
| 172 | switch(op) |
---|
[1918] | 173 | { |
---|
[2291] | 174 | case EReduction::sum : |
---|
| 175 | for(int i=0, j=0; i<srcSize; i++) |
---|
| 176 | if (mask[i]) |
---|
| 177 | { |
---|
| 178 | if (!std::isnan(*(input + i))) |
---|
| 179 | { |
---|
| 180 | if (*(count+connector[j])==0) *(output+connector[j]) = *(input + i) ; |
---|
| 181 | else *(output+connector[j]) += *(input + i) ; |
---|
| 182 | (*(count+connector[j]))++ ; |
---|
| 183 | } |
---|
| 184 | j++ ; |
---|
| 185 | } |
---|
| 186 | break ; |
---|
| 187 | case EReduction::min : |
---|
| 188 | for(int i=0, j=0; i<srcSize; i++) |
---|
| 189 | if (mask[i]) |
---|
| 190 | { |
---|
| 191 | if (!std::isnan(*(input + i))) |
---|
| 192 | { |
---|
| 193 | if (*(count+connector[j])==0) *(output+connector[j]) = *(input + i) ; |
---|
| 194 | else *(output+connector[j]) = std::min(*(output+connector[j]),*(input + i)) ; |
---|
| 195 | (*(count+connector[j]))++ ; |
---|
| 196 | } |
---|
| 197 | j++ ; |
---|
| 198 | } |
---|
| 199 | break ; |
---|
| 200 | case EReduction::max : |
---|
| 201 | for(int i=0, j=0; i<srcSize; i++) |
---|
| 202 | if (mask[i]) |
---|
| 203 | { |
---|
| 204 | if (!std::isnan(*(input + i))) |
---|
| 205 | { |
---|
| 206 | if (*(count+connector[j])==0) *(output+connector[j]) = *(input + i) ; |
---|
| 207 | else *(output+connector[j]) = std::max(*(output+connector[j]),*(input + i)) ; |
---|
| 208 | (*(count+connector[j]))++ ; |
---|
| 209 | } |
---|
| 210 | j++ ; |
---|
| 211 | } |
---|
| 212 | break ; |
---|
| 213 | case EReduction::average : |
---|
| 214 | for(int i=0, j=0; i<srcSize; i++) |
---|
| 215 | if (mask[i]) |
---|
| 216 | { |
---|
| 217 | if (!std::isnan(*(input + i))) |
---|
| 218 | { |
---|
| 219 | if (*(count+connector[j])==0) *(output+connector[j]) = *(input + i) ; |
---|
[2294] | 220 | else *(output+connector[j]) = *(output+connector[j])* (*(count+connector[j])) + *(input + i) ; |
---|
[2291] | 221 | (*(count+connector[j]))++ ; |
---|
| 222 | } |
---|
| 223 | j++ ; |
---|
| 224 | } |
---|
[2294] | 225 | for(int i=0, j=0; i<srcSize; i++) |
---|
| 226 | if (mask[i]) |
---|
| 227 | { |
---|
| 228 | if (!std::isnan(*(input + i))) |
---|
| 229 | *(output+connector[j]) /= (*(count+connector[j])); |
---|
| 230 | j++ ; |
---|
| 231 | } |
---|
[2291] | 232 | break ; |
---|
| 233 | default : |
---|
| 234 | ERROR("CGathererConnector::transfer", |
---|
| 235 | <<"reduction operator "<<(int)op<<" is not defined for this operation") ; |
---|
| 236 | break ; |
---|
[1918] | 237 | } |
---|
[2291] | 238 | } |
---|
[1918] | 239 | |
---|
| 240 | } |
---|
| 241 | else |
---|
| 242 | { |
---|
| 243 | int srcSliceSize = (*(connectors-1))->getSrcSliceSize(rank, connectors-1, nConnectors-1) ; |
---|
| 244 | int dstSliceSize = (*(connectors-1))->getDstSliceSize(connectors-1, nConnectors-1) ; |
---|
| 245 | |
---|
| 246 | const T* in = input ; |
---|
| 247 | for(int i=0,j=0;i<srcSize;i++) |
---|
| 248 | { |
---|
| 249 | if (mask[i]) |
---|
| 250 | { |
---|
[2291] | 251 | (*(connectors-1))->transfer(rank, connectors-1, nConnectors-1, in, output+connector[j]*dstSliceSize, op, count+connector[j]*dstSliceSize) ; // the multiplication must be avoid in further optimization |
---|
[1918] | 252 | j++ ; |
---|
| 253 | } |
---|
| 254 | in += srcSliceSize ; |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | } |
---|
| 259 | |
---|
[2011] | 260 | // hook for transfering mask in grid connector, maybe find an other way to doing that... |
---|
[2267] | 261 | void transfer_or(int rank, shared_ptr<CGathererConnector>* connectors, int nConnectors, const bool* input, bool* output) |
---|
[2011] | 262 | { |
---|
| 263 | auto& connector = connector_[rank] ; // probably costly, find a better way to avoid the map |
---|
| 264 | auto& mask = mask_[rank] ; |
---|
| 265 | int srcSize = mask.size() ; |
---|
| 266 | |
---|
| 267 | if (nConnectors==0) |
---|
| 268 | { |
---|
| 269 | for(int i=0, j=0; i<srcSize; i++) |
---|
| 270 | if (mask[i]) |
---|
| 271 | { |
---|
| 272 | *(output+connector[j]) |= *(input + i) ; |
---|
| 273 | j++ ; |
---|
| 274 | } |
---|
[1918] | 275 | |
---|
[2011] | 276 | } |
---|
| 277 | else |
---|
| 278 | { |
---|
| 279 | int srcSliceSize = (*(connectors-1))->getSrcSliceSize(rank, connectors-1, nConnectors-1) ; |
---|
| 280 | int dstSliceSize = (*(connectors-1))->getDstSliceSize(connectors-1, nConnectors-1) ; |
---|
| 281 | |
---|
| 282 | const bool* in = input ; |
---|
| 283 | for(int i=0,j=0;i<srcSize;i++) |
---|
| 284 | { |
---|
| 285 | if (mask[i]) |
---|
| 286 | { |
---|
| 287 | (*(connectors-1))->transfer_or(rank, connectors-1, nConnectors-1, in, output+connector[j]*dstSliceSize) ; // the multiplication must be avoid in further optimization |
---|
| 288 | j++ ; |
---|
| 289 | } |
---|
| 290 | in += srcSliceSize ; |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | |
---|
| 297 | |
---|
[1918] | 298 | template<typename T> |
---|
[2291] | 299 | void transfer(map<int, CArray<T,1>>& dataIn, CArray<T,1>& dataOut, T missingValue, EReduction op = EReduction::none) |
---|
[1918] | 300 | { |
---|
[2291] | 301 | transfer(1, 1, dataIn, dataOut, missingValue, op); |
---|
[1930] | 302 | } |
---|
| 303 | |
---|
| 304 | template<typename T> |
---|
[2291] | 305 | void transfer(int sizeT, map<int, CArray<T,1>>& dataIn, CArray<T,1>& dataOut, T missingValue, EReduction op = EReduction::none) |
---|
[1930] | 306 | { |
---|
[2291] | 307 | transfer(1, sizeT, dataIn, dataOut, missingValue, op) ; |
---|
[1984] | 308 | } |
---|
| 309 | |
---|
| 310 | template<typename T> |
---|
[2291] | 311 | void transfer(int repeat , int sizeT, map<int, CArray<T,1>>& dataIn, CArray<T,1>& dataOut, T missingValue, EReduction op = EReduction::none) |
---|
[1984] | 312 | { |
---|
| 313 | dataOut.resize(repeat*dstSize_*sizeT) ; |
---|
[1918] | 314 | dataOut=missingValue ; |
---|
[2291] | 315 | transfer(repeat, sizeT, dataIn, dataOut, op) ; |
---|
[1918] | 316 | } |
---|
[1930] | 317 | |
---|
| 318 | template<typename T> |
---|
[2291] | 319 | void transfer(CEventServer& event, int sizeT, CArray<T,1>& dataOut, EReduction op = EReduction::none) |
---|
[1930] | 320 | { |
---|
| 321 | map<int, CArray<T,1>> dataIn ; |
---|
| 322 | for (auto& subEvent : event.subEvents) |
---|
| 323 | { |
---|
| 324 | auto& data = dataIn[subEvent.rank]; |
---|
| 325 | (*subEvent.buffer) >> data ; |
---|
| 326 | } |
---|
[2291] | 327 | transfer(1, sizeT, dataIn, dataOut, op) ; |
---|
[1930] | 328 | } |
---|
[1918] | 329 | |
---|
| 330 | template<typename T> |
---|
[2291] | 331 | void transfer(CEventServer& event, CArray<T,1>& dataOut, EReduction op = EReduction::none) |
---|
[1918] | 332 | { |
---|
[2291] | 333 | transfer(event, 1, dataOut, op) ; |
---|
[1930] | 334 | } |
---|
| 335 | |
---|
| 336 | template<typename T> |
---|
[2291] | 337 | void transfer(CEventServer& event, int sizeT, CArray<T,1>& dataOut, T missingValue, EReduction op = EReduction::none) |
---|
[1930] | 338 | { |
---|
[1918] | 339 | map<int, CArray<T,1>> dataIn ; |
---|
| 340 | for (auto& subEvent : event.subEvents) |
---|
| 341 | { |
---|
| 342 | auto& data = dataIn[subEvent.rank]; |
---|
| 343 | (*subEvent.buffer) >> data ; |
---|
| 344 | } |
---|
[2291] | 345 | transfer(1, sizeT, dataIn, dataOut, missingValue, op) ; |
---|
[1918] | 346 | } |
---|
| 347 | |
---|
| 348 | template<typename T> |
---|
[2291] | 349 | void transfer(CEventServer& event, CArray<T,1>& dataOut, T missingValue, EReduction op = EReduction::none) |
---|
[1918] | 350 | { |
---|
| 351 | map<int, CArray<T,1>> dataIn ; |
---|
| 352 | for (auto& subEvent : event.subEvents) |
---|
| 353 | { |
---|
| 354 | auto& data = dataIn[subEvent.rank]; |
---|
| 355 | (*subEvent.buffer) >> data ; |
---|
| 356 | } |
---|
[2291] | 357 | transfer(1, 1, dataIn, dataOut, missingValue, op) ; |
---|
[1918] | 358 | } |
---|
| 359 | |
---|
[2267] | 360 | int getSrcSliceSize(int rank, shared_ptr<CGathererConnector>* connectors, int nConnectors) |
---|
[1918] | 361 | { if (nConnectors==0) return srcSize_[rank] ; else return srcSize_[rank] * (*(connectors-1))->getSrcSliceSize(rank, connectors-1,nConnectors-1) ; } |
---|
| 362 | |
---|
[2267] | 363 | int getDstSliceSize(shared_ptr<CGathererConnector>* connectors, int nConnectors) |
---|
[1918] | 364 | { if (nConnectors==0) return dstSize_ ; else return dstSize_ * (*(connectors-1))->getDstSliceSize(connectors-1,nConnectors-1) ; } |
---|
| 365 | |
---|
| 366 | int getDstSize(void) {return dstSize_ ;} |
---|
| 367 | } ; |
---|
| 368 | |
---|
| 369 | } |
---|
| 370 | |
---|
[2118] | 371 | #endif |
---|