1 | /*! |
---|
2 | \file generic_algorithm_transformation.hpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 14 May 2015 |
---|
5 | \date 21 Mars 2016 |
---|
6 | |
---|
7 | \brief Interface for all transformation algorithms. |
---|
8 | */ |
---|
9 | #include "generic_algorithm_transformation.hpp" |
---|
10 | #include "context.hpp" |
---|
11 | #include "context_client.hpp" |
---|
12 | #include "client_client_dht_template.hpp" |
---|
13 | #include "utils.hpp" |
---|
14 | #include "timer.hpp" |
---|
15 | #include "mpi.hpp" |
---|
16 | |
---|
17 | namespace xios { |
---|
18 | |
---|
19 | CGenericAlgorithmTransformation::CGenericAlgorithmTransformation() |
---|
20 | : transformationMapping_(), transformationWeight_(), transformationPosition_(), |
---|
21 | idAuxInputs_(), type_(ELEMENT_NO_MODIFICATION_WITH_DATA), indexElementSrc_(), |
---|
22 | computedProcSrcNonTransformedElement_(false), eliminateRedondantSrc_(true), isDistributedComputed_(false) |
---|
23 | { |
---|
24 | } |
---|
25 | |
---|
26 | void CGenericAlgorithmTransformation::updateData(CArray<double,1>& dataOut) |
---|
27 | { |
---|
28 | |
---|
29 | } |
---|
30 | |
---|
31 | void CGenericAlgorithmTransformation::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
32 | const double* dataInput, |
---|
33 | CArray<double,1>& dataOut, |
---|
34 | std::vector<bool>& flagInitial, |
---|
35 | bool ignoreMissingValue, bool firstPass ) |
---|
36 | TRY |
---|
37 | { |
---|
38 | |
---|
39 | int nbLocalIndex = localIndex.size(); |
---|
40 | double defaultValue = std::numeric_limits<double>::quiet_NaN(); |
---|
41 | |
---|
42 | if (ignoreMissingValue) |
---|
43 | { |
---|
44 | if (firstPass) dataOut=defaultValue ; |
---|
45 | |
---|
46 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
47 | { |
---|
48 | if (! NumTraits<double>::isNan(*(dataInput + idx))) |
---|
49 | { |
---|
50 | if (flagInitial[localIndex[idx].first]) dataOut(localIndex[idx].first) = *(dataInput + idx) * localIndex[idx].second; |
---|
51 | else dataOut(localIndex[idx].first) += *(dataInput + idx) * localIndex[idx].second; |
---|
52 | flagInitial[localIndex[idx].first] = false; // Reset flag to indicate not all data source are nan |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | } |
---|
57 | else |
---|
58 | { |
---|
59 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
60 | { |
---|
61 | dataOut(localIndex[idx].first) += *(dataInput + idx) * localIndex[idx].second; |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | CATCH |
---|
66 | |
---|
67 | void CGenericAlgorithmTransformation::computePositionElements(CGrid* dst, CGrid* src) |
---|
68 | TRY |
---|
69 | { |
---|
70 | int idxScalar = 0, idxAxis = 0, idxDomain = 0; |
---|
71 | CArray<int,1> axisDomainOrderDst = dst->axis_domain_order; |
---|
72 | for (int i = 0; i < axisDomainOrderDst.numElements(); ++i) |
---|
73 | { |
---|
74 | int dimElement = axisDomainOrderDst(i); |
---|
75 | if (2 == dimElement) |
---|
76 | { |
---|
77 | elementPositionInGridDst2DomainPosition_[i] = idxDomain; |
---|
78 | ++idxDomain; |
---|
79 | } |
---|
80 | else if (1 == dimElement) |
---|
81 | { |
---|
82 | elementPositionInGridDst2AxisPosition_[i] = idxAxis; |
---|
83 | ++idxAxis; |
---|
84 | } |
---|
85 | else |
---|
86 | { |
---|
87 | elementPositionInGridDst2ScalarPosition_[i] = idxScalar; |
---|
88 | ++idxScalar; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | idxScalar = idxAxis = idxDomain = 0; |
---|
93 | CArray<int,1> axisDomainOrderSrc = src->axis_domain_order; |
---|
94 | for (int i = 0; i < axisDomainOrderSrc.numElements(); ++i) |
---|
95 | { |
---|
96 | int dimElement = axisDomainOrderSrc(i); |
---|
97 | if (2 == dimElement) |
---|
98 | { |
---|
99 | elementPositionInGridSrc2DomainPosition_[i] = idxDomain; |
---|
100 | ++idxDomain; |
---|
101 | } |
---|
102 | else if (1 == dimElement) |
---|
103 | { |
---|
104 | elementPositionInGridSrc2AxisPosition_[i] = idxAxis; |
---|
105 | ++idxAxis; |
---|
106 | } |
---|
107 | else |
---|
108 | { |
---|
109 | elementPositionInGridSrc2ScalarPosition_[i] = idxScalar; |
---|
110 | ++idxScalar; |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | CATCH |
---|
115 | |
---|
116 | bool CGenericAlgorithmTransformation::isDistributedTransformation(int elementPositionInGrid, CGrid* gridSrc, CGrid* gridDst) |
---|
117 | TRY |
---|
118 | { |
---|
119 | |
---|
120 | if (!isDistributedComputed_) |
---|
121 | { |
---|
122 | isDistributedComputed_=true ; |
---|
123 | if (!eliminateRedondantSrc_) isDistributed_=true ; |
---|
124 | else |
---|
125 | { |
---|
126 | CContext* context = CContext::getCurrent(); |
---|
127 | CContextClient* client = context->client; |
---|
128 | |
---|
129 | computePositionElements(gridSrc, gridDst); |
---|
130 | std::vector<CScalar*> scalarListSrcP = gridSrc->getScalars(); |
---|
131 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
132 | std::vector<CDomain*> domainListSrcP = gridSrc->getDomains(); |
---|
133 | int distributed, distributed_glo ; |
---|
134 | |
---|
135 | CArray<int,1> axisDomainSrcOrder = gridSrc->axis_domain_order; |
---|
136 | if (2 == axisDomainSrcOrder(elementPositionInGrid)) // It's domain |
---|
137 | { |
---|
138 | distributed=domainListSrcP[elementPositionInGridSrc2DomainPosition_[elementPositionInGrid]]->isDistributed() ; |
---|
139 | MPI_Allreduce(&distributed,&distributed_glo, 1, MPI_INT, MPI_LOR, client->intraComm) ; |
---|
140 | |
---|
141 | } |
---|
142 | else if (1 == axisDomainSrcOrder(elementPositionInGrid))//it's an axis |
---|
143 | { |
---|
144 | distributed=axisListSrcP[elementPositionInGridSrc2AxisPosition_[elementPositionInGrid]]->isDistributed() ; |
---|
145 | MPI_Allreduce(&distributed,&distributed_glo, 1, MPI_INT, MPI_LOR, client->intraComm) ; |
---|
146 | } |
---|
147 | else //it's a scalar |
---|
148 | { |
---|
149 | distributed_glo=false ; |
---|
150 | } |
---|
151 | isDistributed_=distributed_glo ; |
---|
152 | } |
---|
153 | } |
---|
154 | return isDistributed_ ; |
---|
155 | } |
---|
156 | CATCH |
---|
157 | |
---|
158 | /*! |
---|
159 | This function computes the global indexes of grid source, which the grid destination is in demand. |
---|
160 | \param[in] elementPositionInGrid position of an element in a grid .E.g: if grid is composed of domain and axis (in order), |
---|
161 | then position of axis in grid is 1 and domain is positioned at 0. |
---|
162 | \param[in] gridSrc Grid source |
---|
163 | \param[in] gridDst Grid destination |
---|
164 | \param[in\out] globaIndexWeightFromSrcToDst mapping of each global index source and weight to index destination |
---|
165 | */ |
---|
166 | void CGenericAlgorithmTransformation::computeGlobalSourceIndex(int elementPositionInGrid, |
---|
167 | CGrid* gridSrc, |
---|
168 | CGrid* gridDst, |
---|
169 | SourceDestinationIndexMap& globaIndexWeightFromSrcToDst) |
---|
170 | TRY |
---|
171 | { |
---|
172 | CContext* context = CContext::getCurrent(); |
---|
173 | CContextClient* client = context->client; |
---|
174 | int nbClient = client->clientSize; |
---|
175 | |
---|
176 | typedef std::unordered_map<int, std::vector<std::pair<int,double> > > SrcToDstMap; |
---|
177 | int idx; |
---|
178 | |
---|
179 | // compute position of elements on grids |
---|
180 | computePositionElements(gridDst, gridSrc); |
---|
181 | std::vector<CScalar*> scalarListDestP = gridDst->getScalars(); |
---|
182 | std::vector<CAxis*> axisListDestP = gridDst->getAxis(); |
---|
183 | std::vector<CDomain*> domainListDestP = gridDst->getDomains(); |
---|
184 | CArray<int,1> axisDomainDstOrder = gridDst->axis_domain_order; |
---|
185 | std::vector<CScalar*> scalarListSrcP = gridSrc->getScalars(); |
---|
186 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
187 | std::vector<CDomain*> domainListSrcP = gridSrc->getDomains(); |
---|
188 | CArray<int,1> axisDomainSrcOrder = gridSrc->axis_domain_order; |
---|
189 | |
---|
190 | bool isTransPosEmpty = transformationPosition_.empty(); |
---|
191 | CArray<size_t,1> transPos; |
---|
192 | if (!isTransPosEmpty) transPos.resize(transformationMapping_.size()); |
---|
193 | std::set<size_t> allIndexSrc; // All index of source, which can be scattered among processes, need for doing transformation |
---|
194 | |
---|
195 | for (size_t idxTrans = 0; idxTrans < transformationMapping_.size(); ++idxTrans) |
---|
196 | { |
---|
197 | TransformationIndexMap::const_iterator itbTransMap = transformationMapping_[idxTrans].begin(), itTransMap, |
---|
198 | iteTransMap = transformationMapping_[idxTrans].end(); |
---|
199 | TransformationWeightMap::const_iterator itbTransWeight = transformationWeight_[idxTrans].begin(), itTransWeight; |
---|
200 | |
---|
201 | // Build mapping between global source element index and global destination element index. |
---|
202 | itTransWeight = itbTransWeight; |
---|
203 | for (itTransMap = itbTransMap; itTransMap != iteTransMap; ++itTransMap, ++itTransWeight) |
---|
204 | { |
---|
205 | const std::vector<int>& srcIndex = itTransMap->second; |
---|
206 | for (idx = 0; idx < srcIndex.size(); ++idx) |
---|
207 | allIndexSrc.insert(srcIndex[idx]); |
---|
208 | } |
---|
209 | |
---|
210 | if (!isTransPosEmpty) |
---|
211 | { |
---|
212 | TransformationPositionMap::const_iterator itPosMap = transformationPosition_[idxTrans].begin(); |
---|
213 | transPos(idxTrans) = itPosMap->second[0]; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | size_t indexSrcSize = 0; |
---|
218 | CArray<size_t,1> indexSrc(allIndexSrc.size()); |
---|
219 | for (std::set<size_t>::iterator it = allIndexSrc.begin(); it != allIndexSrc.end(); ++it, ++indexSrcSize) |
---|
220 | indexSrc(indexSrcSize) = *it; |
---|
221 | |
---|
222 | // Flag to indicate whether we will recompute distribution of source global index on processes |
---|
223 | bool computeGlobalIndexOnProc = false; |
---|
224 | if (indexElementSrc_.size() != allIndexSrc.size()) |
---|
225 | computeGlobalIndexOnProc = true; |
---|
226 | else |
---|
227 | { |
---|
228 | for (std::set<size_t>::iterator it = allIndexSrc.begin(); it != allIndexSrc.end(); ++it) |
---|
229 | if (0 == indexElementSrc_.count(*it)) |
---|
230 | { |
---|
231 | computeGlobalIndexOnProc = true; |
---|
232 | break; |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | if (computeGlobalIndexOnProc) |
---|
237 | indexElementSrc_.swap(allIndexSrc); |
---|
238 | |
---|
239 | int sendValue = (computeGlobalIndexOnProc) ? 1 : 0; |
---|
240 | int recvValue = 0; |
---|
241 | MPI_Allreduce(&sendValue, &recvValue, 1, MPI_INT, MPI_SUM, client->intraComm); |
---|
242 | computeGlobalIndexOnProc = (0 < recvValue); |
---|
243 | |
---|
244 | // CClientClientDHTInt::Index2VectorInfoTypeMap globalIndexOfTransformedElementOnProc; |
---|
245 | |
---|
246 | if (computeGlobalIndexOnProc || !computedProcSrcNonTransformedElement_) |
---|
247 | { |
---|
248 | { |
---|
249 | CClientClientDHTInt::Index2VectorInfoTypeMap tmp ; |
---|
250 | globalIndexOfTransformedElementOnProc_.swap(tmp) ; |
---|
251 | } |
---|
252 | // Find out global index source of transformed element on corresponding process. |
---|
253 | if (globalElementIndexOnProc_.empty()) |
---|
254 | globalElementIndexOnProc_.resize(axisDomainDstOrder.numElements()); |
---|
255 | |
---|
256 | for (idx = 0; idx < axisDomainDstOrder.numElements(); ++idx) |
---|
257 | { |
---|
258 | |
---|
259 | if (idx == elementPositionInGrid) |
---|
260 | computeExchangeGlobalIndex(indexSrc, axisDomainSrcOrder(idx), globalIndexOfTransformedElementOnProc_); //globalElementIndexOnProc[idx]); |
---|
261 | if (!computedProcSrcNonTransformedElement_) |
---|
262 | { |
---|
263 | if (2 == axisDomainDstOrder(idx)) // It's domain |
---|
264 | { |
---|
265 | if (idx != elementPositionInGrid) |
---|
266 | computeExchangeDomainIndex(domainListDestP[elementPositionInGridDst2DomainPosition_[idx]], |
---|
267 | domainListSrcP[elementPositionInGridSrc2DomainPosition_[idx]], |
---|
268 | transPos, |
---|
269 | globalElementIndexOnProc_[idx]); |
---|
270 | |
---|
271 | } |
---|
272 | else if (1 == axisDomainDstOrder(idx))//it's an axis |
---|
273 | { |
---|
274 | if (idx != elementPositionInGrid) |
---|
275 | computeExchangeAxisIndex(axisListDestP[elementPositionInGridDst2AxisPosition_[idx]], |
---|
276 | axisListSrcP[elementPositionInGridSrc2AxisPosition_[idx]], |
---|
277 | transPos, |
---|
278 | globalElementIndexOnProc_[idx]); |
---|
279 | } |
---|
280 | else //it's a scalar |
---|
281 | { |
---|
282 | if (idx != elementPositionInGrid) |
---|
283 | computeExchangeScalarIndex(scalarListDestP[elementPositionInGridDst2ScalarPosition_[idx]], |
---|
284 | scalarListSrcP[elementPositionInGridSrc2ScalarPosition_[idx]], |
---|
285 | transPos, |
---|
286 | globalElementIndexOnProc_[idx]); |
---|
287 | |
---|
288 | } |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | if (!isTransPosEmpty && !computedProcSrcNonTransformedElement_) |
---|
293 | { |
---|
294 | for (idx = 0; idx < globalElementIndexOnProc_.size(); ++idx) |
---|
295 | { |
---|
296 | if (idx != elementPositionInGrid) |
---|
297 | { |
---|
298 | std::unordered_map<int,std::vector<size_t> >::iterator itb = globalElementIndexOnProc_[idx].begin(), it, |
---|
299 | ite = globalElementIndexOnProc_[idx].end(); |
---|
300 | for (it = itb; it != ite; ++it) it->second.resize(1); |
---|
301 | } |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | /* |
---|
306 | if (!computedProcSrcNonTransformedElement_) |
---|
307 | { |
---|
308 | for (idx = 0; idx < globalElementIndexOnProc_.size(); ++idx) |
---|
309 | { |
---|
310 | if (idx != elementPositionInGrid) |
---|
311 | { |
---|
312 | std::unordered_map<int,std::vector<size_t> >::iterator itb = globalElementIndexOnProc_[idx].begin(), it, |
---|
313 | ite = globalElementIndexOnProc_[idx].end(); |
---|
314 | for (it = itb; it != ite; ++it) procOfNonTransformedElements_.insert(it->first); |
---|
315 | if (procOfNonTransformedElements_.size() == nbClient) |
---|
316 | break; |
---|
317 | } |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | // Processes contain the source index of transformed element |
---|
322 | std::set<int> procOfTransformedElement; |
---|
323 | CClientClientDHTInt::Index2VectorInfoTypeMap::iterator itIdxb = globalIndexOfTransformedElementOnProc.begin(), |
---|
324 | itIdxe = globalIndexOfTransformedElementOnProc.end(), itIdx; |
---|
325 | for (itIdx = itIdxb; itIdx != itIdxe; ++itIdx) |
---|
326 | { |
---|
327 | std::vector<int>& tmp = itIdx->second; |
---|
328 | for (int i = 0; i < tmp.size(); ++i) |
---|
329 | procOfTransformedElement.insert(tmp[i]); |
---|
330 | if (tmp.size() == nbClient) |
---|
331 | break; |
---|
332 | } |
---|
333 | |
---|
334 | std::set<int>& commonProc = (procOfTransformedElement.size() < procOfNonTransformedElements_.size()) ? procOfTransformedElement |
---|
335 | : (!procOfNonTransformedElements_.empty() ? procOfNonTransformedElements_ : procOfTransformedElement); |
---|
336 | |
---|
337 | std::vector<int> procContainSrcElementIdx(commonProc.size()); |
---|
338 | int count = 0; |
---|
339 | for (std::set<int>::iterator it = commonProc.begin(); it != commonProc.end(); ++it) |
---|
340 | procContainSrcElementIdx[count++] = *it; |
---|
341 | |
---|
342 | procContainSrcElementIdx_.swap(procContainSrcElementIdx); |
---|
343 | */ |
---|
344 | |
---|
345 | if (procElementList_.empty()) procElementList_.resize(axisDomainDstOrder.numElements()) ; |
---|
346 | for (idx = 0; idx < axisDomainDstOrder.numElements(); ++idx) |
---|
347 | { |
---|
348 | std::set<int>& procList=procElementList_[idx] ; |
---|
349 | std::set<int> commonTmp ; |
---|
350 | if (idx == elementPositionInGrid) |
---|
351 | { |
---|
352 | set<int> tmpSet ; |
---|
353 | procList.swap(tmpSet) ; |
---|
354 | CClientClientDHTInt::Index2VectorInfoTypeMap::iterator itIdxb = globalIndexOfTransformedElementOnProc_.begin(), |
---|
355 | itIdxe = globalIndexOfTransformedElementOnProc_.end(), itIdx; |
---|
356 | for (itIdx = itIdxb; itIdx != itIdxe; ++itIdx) |
---|
357 | { |
---|
358 | std::vector<int>& tmp = itIdx->second; |
---|
359 | for (int i = 0; i < tmp.size(); ++i) procList.insert(tmp[i]); |
---|
360 | if (tmp.size() == nbClient) |
---|
361 | break; |
---|
362 | } |
---|
363 | } |
---|
364 | else |
---|
365 | { |
---|
366 | if (!computedProcSrcNonTransformedElement_) |
---|
367 | { |
---|
368 | set<int> tmpSet ; |
---|
369 | procList.swap(tmpSet) ; |
---|
370 | std::unordered_map<int,std::vector<size_t> >::iterator itb = globalElementIndexOnProc_[idx].begin(), it, |
---|
371 | ite = globalElementIndexOnProc_[idx].end(); |
---|
372 | for (it = itb; it != ite; ++it) |
---|
373 | { |
---|
374 | procList.insert(it->first); |
---|
375 | if (procList.size() == nbClient) break; |
---|
376 | } |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | if (idx==0) commonProc_= procList ; |
---|
381 | else |
---|
382 | { |
---|
383 | for (std::set<int>::iterator it = commonProc_.begin(); it != commonProc_.end(); ++it) |
---|
384 | if (procList.count(*it)==1) commonTmp.insert(*it) ; |
---|
385 | commonProc_.swap(commonTmp) ; |
---|
386 | } |
---|
387 | } |
---|
388 | std::vector<int> procContainSrcElementIdx(commonProc_.size()); |
---|
389 | int count = 0; |
---|
390 | for (std::set<int>::iterator it = commonProc_.begin(); it != commonProc_.end(); ++it) procContainSrcElementIdx[count++] = *it; |
---|
391 | procContainSrcElementIdx_.swap(procContainSrcElementIdx); |
---|
392 | |
---|
393 | // For the first time, surely we calculate proc containing non transformed elements |
---|
394 | if (!computedProcSrcNonTransformedElement_) computedProcSrcNonTransformedElement_ = true; |
---|
395 | } |
---|
396 | |
---|
397 | for (size_t idxTrans = 0; idxTrans < transformationMapping_.size(); ++idxTrans) |
---|
398 | { |
---|
399 | TransformationIndexMap::const_iterator itbTransMap = transformationMapping_[idxTrans].begin(), itTransMap, |
---|
400 | iteTransMap = transformationMapping_[idxTrans].end(); |
---|
401 | TransformationWeightMap::const_iterator itbTransWeight = transformationWeight_[idxTrans].begin(), itTransWeight; |
---|
402 | SrcToDstMap src2DstMap; |
---|
403 | src2DstMap.rehash(std::ceil(transformationMapping_[idxTrans].size()/src2DstMap.max_load_factor())); |
---|
404 | |
---|
405 | // Build mapping between global source element index and global destination element index. |
---|
406 | std::unordered_map<int,std::vector<size_t> >().swap(globalElementIndexOnProc_[elementPositionInGrid]); |
---|
407 | std::set<int> tmpCounter; |
---|
408 | itTransWeight = itbTransWeight; |
---|
409 | for (itTransMap = itbTransMap; itTransMap != iteTransMap; ++itTransMap, ++itTransWeight) |
---|
410 | { |
---|
411 | const std::vector<int>& srcIndex = itTransMap->second; |
---|
412 | const std::vector<double>& weight = itTransWeight->second; |
---|
413 | for (idx = 0; idx < srcIndex.size(); ++idx) |
---|
414 | { |
---|
415 | src2DstMap[srcIndex[idx]].push_back(make_pair(itTransMap->first, weight[idx])); |
---|
416 | if (0 == tmpCounter.count(srcIndex[idx])) |
---|
417 | { |
---|
418 | tmpCounter.insert(srcIndex[idx]); |
---|
419 | |
---|
420 | vector<int>& rankSrc = globalIndexOfTransformedElementOnProc_[srcIndex[idx]] ; |
---|
421 | for (int n=0;n<rankSrc.size();++n) |
---|
422 | { |
---|
423 | if (commonProc_.count(rankSrc[n])==1) globalElementIndexOnProc_[elementPositionInGrid][rankSrc[n]].push_back(srcIndex[idx]); |
---|
424 | } |
---|
425 | // for (int j = 0; j < procContainSrcElementIdx_.size(); ++j) |
---|
426 | // globalElementIndexOnProc_[elementPositionInGrid][procContainSrcElementIdx_[j]].push_back(srcIndex[idx]); |
---|
427 | } |
---|
428 | } |
---|
429 | } |
---|
430 | |
---|
431 | if (!isTransPosEmpty) |
---|
432 | { |
---|
433 | for (idx = 0; idx < globalElementIndexOnProc_.size(); ++idx) |
---|
434 | { |
---|
435 | if (idx != elementPositionInGrid) |
---|
436 | { |
---|
437 | std::unordered_map<int,std::vector<size_t> >::iterator itb = globalElementIndexOnProc_[idx].begin(), it, |
---|
438 | ite = globalElementIndexOnProc_[idx].end(); |
---|
439 | for (it = itb; it != ite; ++it) it->second[0] = transPos(idxTrans); |
---|
440 | } |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | // Ok, now compute global index of grid source and ones of grid destination |
---|
445 | computeGlobalGridIndexMapping(elementPositionInGrid, |
---|
446 | procContainSrcElementIdx_, //srcRank, |
---|
447 | src2DstMap, |
---|
448 | gridSrc, |
---|
449 | gridDst, |
---|
450 | globalElementIndexOnProc_, |
---|
451 | globaIndexWeightFromSrcToDst); |
---|
452 | } |
---|
453 | } |
---|
454 | CATCH |
---|
455 | |
---|
456 | /*! |
---|
457 | Compute mapping of global index of grid source and grid destination |
---|
458 | \param [in] elementPositionInGrid position of element in grid. E.x: grid composed of domain and axis, domain has position 0 and axis 1. |
---|
459 | \param [in] srcRank rank of client from which we demand global index of element source |
---|
460 | \param [in] src2DstMap mapping of global index of element source and global index of element destination |
---|
461 | \param [in] gridSrc Grid source |
---|
462 | \param [in] gridDst Grid destination |
---|
463 | \param [in] globalElementIndexOnProc Global index of element source on different client rank |
---|
464 | \param [out] globaIndexWeightFromSrcToDst Mapping of global index of grid source and grid destination |
---|
465 | */ |
---|
466 | void CGenericAlgorithmTransformation::computeGlobalGridIndexMapping(int elementPositionInGrid, |
---|
467 | const std::vector<int>& srcRank, |
---|
468 | std::unordered_map<int, std::vector<std::pair<int,double> > >& src2DstMap, |
---|
469 | CGrid* gridSrc, |
---|
470 | CGrid* gridDst, |
---|
471 | std::vector<std::unordered_map<int,std::vector<size_t> > >& globalElementIndexOnProc, |
---|
472 | SourceDestinationIndexMap& globaIndexWeightFromSrcToDst) |
---|
473 | TRY |
---|
474 | { |
---|
475 | SourceDestinationIndexMap globaIndexWeightFromSrcToDst_tmp ; |
---|
476 | |
---|
477 | CContext* context = CContext::getCurrent(); |
---|
478 | CContextClient* client=context->client; |
---|
479 | int clientRank = client->clientRank; |
---|
480 | |
---|
481 | std::vector<CDomain*> domainListSrcP = gridSrc->getDomains(); |
---|
482 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
483 | std::vector<CScalar*> scalarListSrcP = gridSrc->getScalars(); |
---|
484 | CArray<int,1> axisDomainSrcOrder = gridSrc->axis_domain_order; |
---|
485 | |
---|
486 | size_t nbElement = axisDomainSrcOrder.numElements(); |
---|
487 | std::vector<size_t> nGlobSrc(nbElement); |
---|
488 | size_t globalSrcSize = 1; |
---|
489 | int domainIndex = 0, axisIndex = 0, scalarIndex = 0; |
---|
490 | for (int idx = 0; idx < nbElement; ++idx) |
---|
491 | { |
---|
492 | nGlobSrc[idx] = globalSrcSize; |
---|
493 | int elementDimension = axisDomainSrcOrder(idx); |
---|
494 | |
---|
495 | // If this is a domain |
---|
496 | if (2 == elementDimension) |
---|
497 | { |
---|
498 | globalSrcSize *= domainListSrcP[domainIndex]->nj_glo.getValue() * domainListSrcP[domainIndex]->ni_glo.getValue(); |
---|
499 | ++domainIndex; |
---|
500 | } |
---|
501 | else if (1 == elementDimension) // So it's an axis |
---|
502 | { |
---|
503 | globalSrcSize *= axisListSrcP[axisIndex]->n_glo.getValue(); |
---|
504 | ++axisIndex; |
---|
505 | } |
---|
506 | else |
---|
507 | { |
---|
508 | globalSrcSize *= 1; |
---|
509 | ++scalarIndex; |
---|
510 | } |
---|
511 | } |
---|
512 | |
---|
513 | std::vector<CDomain*> domainListDestP = gridDst->getDomains(); |
---|
514 | std::vector<CAxis*> axisListDestP = gridDst->getAxis(); |
---|
515 | std::vector<CScalar*> scalarListDestP = gridDst->getScalars(); |
---|
516 | CArray<int,1> axisDomainDstOrder = gridDst->axis_domain_order; |
---|
517 | |
---|
518 | std::vector<size_t> nGlobDst(nbElement); |
---|
519 | size_t globalDstSize = 1; |
---|
520 | domainIndex = axisIndex = scalarIndex = 0; |
---|
521 | set<size_t> globalSrcStoreIndex ; |
---|
522 | |
---|
523 | for (int idx = 0; idx < nbElement; ++idx) |
---|
524 | { |
---|
525 | nGlobDst[idx] = globalDstSize; |
---|
526 | int elementDimension = axisDomainDstOrder(idx); |
---|
527 | |
---|
528 | // If this is a domain |
---|
529 | if (2 == elementDimension) |
---|
530 | { |
---|
531 | globalDstSize *= domainListDestP[domainIndex]->nj_glo.getValue() * domainListDestP[domainIndex]->ni_glo.getValue(); |
---|
532 | ++domainIndex; |
---|
533 | } |
---|
534 | else if (1 == elementDimension) // So it's an axis |
---|
535 | { |
---|
536 | globalDstSize *= axisListDestP[axisIndex]->n_glo.getValue(); |
---|
537 | ++axisIndex; |
---|
538 | } |
---|
539 | else |
---|
540 | { |
---|
541 | globalDstSize *= 1; |
---|
542 | ++scalarIndex; |
---|
543 | } |
---|
544 | } |
---|
545 | |
---|
546 | std::map< std::pair<size_t,size_t>, int > rankMap ; |
---|
547 | std::map< std::pair<size_t,size_t>, int >:: iterator rankMapIt ; |
---|
548 | |
---|
549 | for (int i = 0; i < srcRank.size(); ++i) |
---|
550 | { |
---|
551 | size_t ssize = 1; |
---|
552 | int rankSrc = srcRank[i]; |
---|
553 | for (int idx = 0; idx < nbElement; ++idx) |
---|
554 | { |
---|
555 | ssize *= (globalElementIndexOnProc[idx][rankSrc]).size(); |
---|
556 | } |
---|
557 | |
---|
558 | std::vector<int> idxLoop(nbElement,0); |
---|
559 | std::vector<int> currentIndexSrc(nbElement, 0); |
---|
560 | std::vector<int> currentIndexDst(nbElement, 0); |
---|
561 | int innnerLoopSize = (globalElementIndexOnProc[0])[rankSrc].size(); |
---|
562 | size_t idx = 0; |
---|
563 | while (idx < ssize) |
---|
564 | { |
---|
565 | for (int ind = 0; ind < nbElement; ++ind) |
---|
566 | { |
---|
567 | if (idxLoop[ind] == (globalElementIndexOnProc[ind])[rankSrc].size()) |
---|
568 | { |
---|
569 | idxLoop[ind] = 0; |
---|
570 | ++idxLoop[ind+1]; |
---|
571 | } |
---|
572 | |
---|
573 | currentIndexDst[ind] = currentIndexSrc[ind] = (globalElementIndexOnProc[ind])[rankSrc][idxLoop[ind]]; |
---|
574 | } |
---|
575 | |
---|
576 | for (int ind = 0; ind < innnerLoopSize; ++ind) |
---|
577 | { |
---|
578 | currentIndexDst[0] = currentIndexSrc[0] = (globalElementIndexOnProc[0])[rankSrc][ind]; |
---|
579 | int globalElementDstIndexSize = 0; |
---|
580 | if (1 == src2DstMap.count(currentIndexSrc[elementPositionInGrid])) |
---|
581 | { |
---|
582 | globalElementDstIndexSize = src2DstMap[currentIndexSrc[elementPositionInGrid]].size(); |
---|
583 | } |
---|
584 | |
---|
585 | std::vector<size_t> globalDstVecIndex(globalElementDstIndexSize,0); |
---|
586 | size_t globalSrcIndex = 0; |
---|
587 | for (int idxElement = 0; idxElement < nbElement; ++idxElement) |
---|
588 | { |
---|
589 | if (idxElement == elementPositionInGrid) |
---|
590 | { |
---|
591 | for (int k = 0; k < globalElementDstIndexSize; ++k) |
---|
592 | { |
---|
593 | globalDstVecIndex[k] += src2DstMap[currentIndexSrc[elementPositionInGrid]][k].first * nGlobDst[idxElement]; |
---|
594 | } |
---|
595 | } |
---|
596 | else |
---|
597 | { |
---|
598 | for (int k = 0; k < globalElementDstIndexSize; ++k) |
---|
599 | { |
---|
600 | globalDstVecIndex[k] += currentIndexDst[idxElement] * nGlobDst[idxElement]; |
---|
601 | } |
---|
602 | } |
---|
603 | globalSrcIndex += currentIndexSrc[idxElement] * nGlobSrc[idxElement]; |
---|
604 | } |
---|
605 | |
---|
606 | for (int k = 0; k < globalElementDstIndexSize; ++k) |
---|
607 | { |
---|
608 | |
---|
609 | globaIndexWeightFromSrcToDst_tmp[rankSrc][globalSrcIndex].push_back(make_pair(globalDstVecIndex[k],src2DstMap[currentIndexSrc[elementPositionInGrid]][k].second )); |
---|
610 | rankMapIt=rankMap.find(make_pair(globalSrcIndex,globalDstVecIndex[k])) ; |
---|
611 | if (rankMapIt==rankMap.end()) rankMap[make_pair(globalSrcIndex,globalDstVecIndex[k])] = rankSrc ; |
---|
612 | else if (rankSrc==clientRank) rankMapIt->second = rankSrc ; |
---|
613 | } |
---|
614 | ++idxLoop[0]; |
---|
615 | } |
---|
616 | idx += innnerLoopSize; |
---|
617 | } |
---|
618 | } |
---|
619 | |
---|
620 | // eliminate redondant global src point owned by differrent processes. |
---|
621 | // Avoid as possible to tranfer data from an other process if the src point is also owned by current process |
---|
622 | int rankSrc ; |
---|
623 | size_t globalSrcIndex ; |
---|
624 | size_t globalDstIndex ; |
---|
625 | double weight ; |
---|
626 | |
---|
627 | SourceDestinationIndexMap::iterator rankIt,rankIte ; |
---|
628 | std::unordered_map<size_t, std::vector<std::pair<size_t,double> > >::iterator globalSrcIndexIt, globalSrcIndexIte ; |
---|
629 | std::vector<std::pair<size_t,double> >::iterator vectIt,vectIte ; |
---|
630 | |
---|
631 | rankIt=globaIndexWeightFromSrcToDst_tmp.begin() ; rankIte=globaIndexWeightFromSrcToDst_tmp.end() ; |
---|
632 | for(;rankIt!=rankIte;++rankIt) |
---|
633 | { |
---|
634 | rankSrc = rankIt->first ; |
---|
635 | globalSrcIndexIt = rankIt->second.begin() ; globalSrcIndexIte = rankIt->second.end() ; |
---|
636 | for(;globalSrcIndexIt!=globalSrcIndexIte;++globalSrcIndexIt) |
---|
637 | { |
---|
638 | globalSrcIndex = globalSrcIndexIt->first ; |
---|
639 | vectIt = globalSrcIndexIt->second.begin() ; vectIte = globalSrcIndexIt->second.end() ; |
---|
640 | for(vectIt; vectIt!=vectIte; vectIt++) |
---|
641 | { |
---|
642 | globalDstIndex = vectIt->first ; |
---|
643 | weight = vectIt->second ; |
---|
644 | if (eliminateRedondantSrc_) |
---|
645 | { |
---|
646 | if (rankMap[make_pair(globalSrcIndex,globalDstIndex)] == rankSrc) |
---|
647 | globaIndexWeightFromSrcToDst[rankSrc][globalSrcIndex].push_back(make_pair(globalDstIndex,weight)) ; |
---|
648 | } |
---|
649 | else globaIndexWeightFromSrcToDst[rankSrc][globalSrcIndex].push_back(make_pair(globalDstIndex,weight)) ; |
---|
650 | } |
---|
651 | } |
---|
652 | } |
---|
653 | } |
---|
654 | CATCH |
---|
655 | |
---|
656 | /*! |
---|
657 | Find out proc and global index of axis source which axis destination is on demande |
---|
658 | \param[in] scalar Scalar destination |
---|
659 | \param[in] scalar Scalar source |
---|
660 | \param[in] destGlobalIndexPositionInGrid Relative position of axis corresponds to other element of grid. |
---|
661 | \param[out] globalScalarIndexOnProc Global index of axis source on different procs |
---|
662 | */ |
---|
663 | void CGenericAlgorithmTransformation::computeExchangeScalarIndex(CScalar* scalarDst, |
---|
664 | CScalar* scalarSrc, |
---|
665 | CArray<size_t,1>& destGlobalIndexPositionInGrid, |
---|
666 | std::unordered_map<int,std::vector<size_t> >& globalScalarIndexOnProc) |
---|
667 | TRY |
---|
668 | { |
---|
669 | CContext* context = CContext::getCurrent(); |
---|
670 | CContextClient* client=context->client; |
---|
671 | int clientRank = client->clientRank; |
---|
672 | int clientSize = client->clientSize; |
---|
673 | |
---|
674 | globalScalarIndexOnProc.rehash(std::ceil(clientSize/globalScalarIndexOnProc.max_load_factor())); |
---|
675 | for (int idx = 0; idx < clientSize; ++idx) |
---|
676 | { |
---|
677 | globalScalarIndexOnProc[idx].push_back(0); |
---|
678 | } |
---|
679 | } |
---|
680 | CATCH |
---|
681 | |
---|
682 | /*! |
---|
683 | Find out proc and global index of axis source which axis destination is on demande |
---|
684 | \param[in] axisDst Axis destination |
---|
685 | \param[in] axisSrc Axis source |
---|
686 | \param[in] destGlobalIndexPositionInGrid Relative position of axis corresponds to other element of grid. |
---|
687 | \param[out] globalAxisIndexOnProc Global index of axis source on different procs |
---|
688 | */ |
---|
689 | void CGenericAlgorithmTransformation::computeExchangeAxisIndex(CAxis* axisDst, |
---|
690 | CAxis* axisSrc, |
---|
691 | CArray<size_t,1>& destGlobalIndexPositionInGrid, |
---|
692 | std::unordered_map<int,std::vector<size_t> >& globalAxisIndexOnProc) |
---|
693 | TRY |
---|
694 | { |
---|
695 | CContext* context = CContext::getCurrent(); |
---|
696 | CContextClient* client=context->client; |
---|
697 | int clientRank = client->clientRank; |
---|
698 | int clientSize = client->clientSize; |
---|
699 | |
---|
700 | size_t globalIndex; |
---|
701 | int nIndexSize = axisSrc->index.numElements(); |
---|
702 | CClientClientDHTInt::Index2VectorInfoTypeMap globalIndex2ProcRank; |
---|
703 | globalIndex2ProcRank.rehash(std::ceil(nIndexSize/globalIndex2ProcRank.max_load_factor())); |
---|
704 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
705 | { |
---|
706 | if (axisSrc->mask(idx)) |
---|
707 | { |
---|
708 | globalIndex = axisSrc->index(idx); |
---|
709 | globalIndex2ProcRank[globalIndex].push_back(clientRank); |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | CClientClientDHTInt dhtIndexProcRank(globalIndex2ProcRank, client->intraComm); |
---|
714 | CArray<size_t,1> globalAxisIndex(axisDst->index.numElements()); |
---|
715 | for (int idx = 0; idx < globalAxisIndex.numElements(); ++idx) |
---|
716 | { |
---|
717 | globalAxisIndex(idx) = axisDst->index(idx); |
---|
718 | } |
---|
719 | dhtIndexProcRank.computeIndexInfoMapping(globalAxisIndex); |
---|
720 | |
---|
721 | std::vector<int> countIndex(clientSize,0); |
---|
722 | const CClientClientDHTInt::Index2VectorInfoTypeMap& computedGlobalIndexOnProc = dhtIndexProcRank.getInfoIndexMap(); |
---|
723 | CClientClientDHTInt::Index2VectorInfoTypeMap::const_iterator itb = computedGlobalIndexOnProc.begin(), it, |
---|
724 | ite = computedGlobalIndexOnProc.end(); |
---|
725 | for (it = itb; it != ite; ++it) |
---|
726 | { |
---|
727 | const std::vector<int>& procList = it->second; |
---|
728 | for (int idx = 0; idx < procList.size(); ++idx) ++countIndex[procList[idx]]; |
---|
729 | } |
---|
730 | |
---|
731 | globalAxisIndexOnProc.rehash(std::ceil(clientSize/globalAxisIndexOnProc.max_load_factor())); |
---|
732 | for (int idx = 0; idx < clientSize; ++idx) |
---|
733 | { |
---|
734 | if (0 != countIndex[idx]) |
---|
735 | { |
---|
736 | globalAxisIndexOnProc[idx].resize(countIndex[idx]); |
---|
737 | countIndex[idx] = 0; |
---|
738 | } |
---|
739 | } |
---|
740 | |
---|
741 | for (it = itb; it != ite; ++it) |
---|
742 | { |
---|
743 | const std::vector<int>& procList = it->second; |
---|
744 | for (int idx = 0; idx < procList.size(); ++idx) |
---|
745 | { |
---|
746 | globalAxisIndexOnProc[procList[idx]][countIndex[procList[idx]]] = it->first; |
---|
747 | ++countIndex[procList[idx]]; |
---|
748 | } |
---|
749 | } |
---|
750 | } |
---|
751 | CATCH |
---|
752 | |
---|
753 | /*! |
---|
754 | Find out proc and global index of domain source which domain destination is on demande |
---|
755 | \param[in] domainDst Domain destination |
---|
756 | \param[in] domainSrc Domain source |
---|
757 | \param[in] destGlobalIndexPositionInGrid Relative position of domain corresponds to other element of grid. |
---|
758 | \param[out] globalDomainIndexOnProc Global index of domain source on different procs |
---|
759 | */ |
---|
760 | void CGenericAlgorithmTransformation::computeExchangeDomainIndex(CDomain* domainDst, |
---|
761 | CDomain* domainSrc, |
---|
762 | CArray<size_t,1>& destGlobalIndexPositionInGrid, |
---|
763 | std::unordered_map<int,std::vector<size_t> >& globalDomainIndexOnProc) |
---|
764 | TRY |
---|
765 | { |
---|
766 | CContext* context = CContext::getCurrent(); |
---|
767 | CContextClient* client=context->client; |
---|
768 | int clientRank = client->clientRank; |
---|
769 | int clientSize = client->clientSize; |
---|
770 | |
---|
771 | int niGlobSrc = domainSrc->ni_glo.getValue(); |
---|
772 | size_t globalIndex; |
---|
773 | int i_ind, j_ind; |
---|
774 | int nIndexSize = (destGlobalIndexPositionInGrid.isEmpty()) ? domainSrc->i_index.numElements() |
---|
775 | : destGlobalIndexPositionInGrid.numElements(); |
---|
776 | CClientClientDHTInt::Index2VectorInfoTypeMap globalIndex2ProcRank; |
---|
777 | globalIndex2ProcRank.rehash(std::ceil(nIndexSize/globalIndex2ProcRank.max_load_factor())); |
---|
778 | |
---|
779 | if (destGlobalIndexPositionInGrid.isEmpty()) |
---|
780 | { |
---|
781 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
782 | { |
---|
783 | i_ind=domainSrc->i_index(idx) ; |
---|
784 | j_ind=domainSrc->j_index(idx) ; |
---|
785 | |
---|
786 | if (domainSrc->localMask(idx)) |
---|
787 | { |
---|
788 | globalIndex = i_ind + j_ind * niGlobSrc; |
---|
789 | globalIndex2ProcRank[globalIndex].resize(1); |
---|
790 | globalIndex2ProcRank[globalIndex][0] = clientRank; |
---|
791 | } |
---|
792 | } |
---|
793 | } |
---|
794 | else |
---|
795 | { |
---|
796 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
797 | { |
---|
798 | // if (domainSrc->localMask(idx)) -> not necessairy, mask seem to be included in destGlobalIndexPositionInGrid(idx) (ym) |
---|
799 | globalIndex2ProcRank[destGlobalIndexPositionInGrid(idx)].push_back(clientRank); |
---|
800 | } |
---|
801 | } |
---|
802 | |
---|
803 | CArray<size_t,1> globalDomainIndex; |
---|
804 | if (destGlobalIndexPositionInGrid.isEmpty()) |
---|
805 | { |
---|
806 | int niGlobDst = domainDst->ni_glo.getValue(); |
---|
807 | globalDomainIndex.resize(domainDst->i_index.numElements()); |
---|
808 | nIndexSize = domainDst->i_index.numElements(); |
---|
809 | |
---|
810 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
811 | { |
---|
812 | i_ind=domainDst->i_index(idx) ; |
---|
813 | j_ind=domainDst->j_index(idx) ; |
---|
814 | |
---|
815 | globalDomainIndex(idx) = i_ind + j_ind * niGlobDst; |
---|
816 | } |
---|
817 | } |
---|
818 | else |
---|
819 | { |
---|
820 | globalDomainIndex.reference(destGlobalIndexPositionInGrid); |
---|
821 | } |
---|
822 | |
---|
823 | CClientClientDHTInt dhtIndexProcRank(globalIndex2ProcRank, client->intraComm); |
---|
824 | dhtIndexProcRank.computeIndexInfoMapping(globalDomainIndex); |
---|
825 | |
---|
826 | std::vector<int> countIndex(clientSize,0); |
---|
827 | const CClientClientDHTInt::Index2VectorInfoTypeMap& computedGlobalIndexOnProc = dhtIndexProcRank.getInfoIndexMap(); |
---|
828 | CClientClientDHTInt::Index2VectorInfoTypeMap::const_iterator itb = computedGlobalIndexOnProc.begin(), it, |
---|
829 | ite = computedGlobalIndexOnProc.end(); |
---|
830 | for (it = itb; it != ite; ++it) |
---|
831 | { |
---|
832 | const std::vector<int>& procList = it->second; |
---|
833 | for (int idx = 0; idx < procList.size(); ++idx) ++countIndex[procList[idx]]; |
---|
834 | } |
---|
835 | |
---|
836 | globalDomainIndexOnProc.rehash(std::ceil(clientSize/globalDomainIndexOnProc.max_load_factor())); |
---|
837 | for (int idx = 0; idx < clientSize; ++idx) |
---|
838 | { |
---|
839 | if (0 != countIndex[idx]) |
---|
840 | { |
---|
841 | globalDomainIndexOnProc[idx].resize(countIndex[idx]); |
---|
842 | countIndex[idx] = 0; |
---|
843 | } |
---|
844 | } |
---|
845 | |
---|
846 | for (it = itb; it != ite; ++it) |
---|
847 | { |
---|
848 | const std::vector<int>& procList = it->second; |
---|
849 | for (int idx = 0; idx < procList.size(); ++idx) |
---|
850 | { |
---|
851 | globalDomainIndexOnProc[procList[idx]][countIndex[procList[idx]]] = it->first; |
---|
852 | ++countIndex[procList[idx]]; |
---|
853 | } |
---|
854 | } |
---|
855 | } |
---|
856 | CATCH |
---|
857 | |
---|
858 | void CGenericAlgorithmTransformation::computeTransformationMappingNonDistributed(int elementPositionInGrid, CGrid* gridSrc, CGrid* gridDst, |
---|
859 | vector<int>& localSrc, vector<int>& localDst, vector<double>& weight, |
---|
860 | int& nlocalIndexDest) |
---|
861 | TRY |
---|
862 | { |
---|
863 | |
---|
864 | CContext* context = CContext::getCurrent(); |
---|
865 | CContextClient* client = context->client; |
---|
866 | int nbClient = client->clientSize; |
---|
867 | |
---|
868 | computePositionElements(gridDst, gridSrc); |
---|
869 | std::vector<CScalar*> scalarListDstP = gridDst->getScalars(); |
---|
870 | std::vector<CAxis*> axisListDstP = gridDst->getAxis(); |
---|
871 | std::vector<CDomain*> domainListDstP = gridDst->getDomains(); |
---|
872 | CArray<int,1> axisDomainDstOrder = gridDst->axis_domain_order; |
---|
873 | std::vector<CScalar*> scalarListSrcP = gridSrc->getScalars(); |
---|
874 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
875 | std::vector<CDomain*> domainListSrcP = gridSrc->getDomains(); |
---|
876 | CArray<int,1> axisDomainSrcOrder = gridSrc->axis_domain_order; |
---|
877 | |
---|
878 | int nElement=axisDomainSrcOrder.numElements() ; |
---|
879 | int indSrc=1 ; |
---|
880 | int indDst=1 ; |
---|
881 | vector<int> nIndexSrc(nElement) ; |
---|
882 | vector<int> nIndexDst(nElement) ; |
---|
883 | vector< CArray<bool,1>* > maskSrc(nElement) ; |
---|
884 | vector< CArray<bool,1>* > maskDst(nElement) ; |
---|
885 | |
---|
886 | int nlocalIndexSrc=1 ; |
---|
887 | // int nlocalIndexDest=1 ; |
---|
888 | nlocalIndexDest=1 ; |
---|
889 | CArray<bool,1> maskScalar(1) ; |
---|
890 | maskScalar = true ; |
---|
891 | |
---|
892 | |
---|
893 | for(int i=0 ; i<nElement; i++) |
---|
894 | { |
---|
895 | int dimElement = axisDomainSrcOrder(i); |
---|
896 | if (2 == dimElement) //domain |
---|
897 | { |
---|
898 | CDomain* domain=domainListSrcP[elementPositionInGridSrc2DomainPosition_[i]] ; |
---|
899 | nIndexSrc[i] = domain->i_index.numElements() ; |
---|
900 | maskSrc[i]=&domain->localMask ; |
---|
901 | } |
---|
902 | else if (1 == dimElement) //axis |
---|
903 | { |
---|
904 | CAxis* axis=axisListSrcP[elementPositionInGridSrc2AxisPosition_[i]] ; |
---|
905 | nIndexSrc[i] = axis->index.numElements() ; |
---|
906 | maskSrc[i]=&axis->mask ; |
---|
907 | } |
---|
908 | else //scalar |
---|
909 | { |
---|
910 | nIndexSrc[i]=1 ; |
---|
911 | maskSrc[i]=&maskScalar ; |
---|
912 | } |
---|
913 | nlocalIndexSrc=nlocalIndexSrc*nIndexSrc[i] ; |
---|
914 | } |
---|
915 | |
---|
916 | |
---|
917 | |
---|
918 | int offset=1 ; |
---|
919 | for(int i=0 ; i<nElement; i++) |
---|
920 | { |
---|
921 | int dimElement = axisDomainDstOrder(i); |
---|
922 | if (2 == dimElement) //domain |
---|
923 | { |
---|
924 | CDomain* domain=domainListDstP[elementPositionInGridDst2DomainPosition_[i]] ; |
---|
925 | int nIndex=domain->i_index.numElements() ; |
---|
926 | CArray<bool,1>& localMask=domain->localMask ; |
---|
927 | int nbInd=0 ; |
---|
928 | for(int j=0;j<nIndex;j++) if (localMask(j)) nbInd++ ; |
---|
929 | nIndexDst[i] = nbInd ; |
---|
930 | maskDst[i]=&domain->localMask ; |
---|
931 | } |
---|
932 | else if (1 == dimElement) //axis |
---|
933 | { |
---|
934 | CAxis* axis = axisListDstP[elementPositionInGridDst2AxisPosition_[i]] ; |
---|
935 | int nIndex=axis->index.numElements() ; |
---|
936 | CArray<bool,1>& localMask=axis->mask ; |
---|
937 | int nbInd=0 ; |
---|
938 | for(int j=0;j<nIndex;j++) if (localMask(j)) nbInd++ ; |
---|
939 | nIndexDst[i] = nbInd ; |
---|
940 | maskDst[i]=&axis->mask ; |
---|
941 | } |
---|
942 | else //scalar |
---|
943 | { |
---|
944 | nIndexDst[i]=1 ; |
---|
945 | maskDst[i]=&maskScalar ; |
---|
946 | } |
---|
947 | if (i<elementPositionInGrid) offset=offset*nIndexDst[i] ; |
---|
948 | nlocalIndexDest=nlocalIndexDest*nIndexDst[i] ; |
---|
949 | } |
---|
950 | |
---|
951 | vector<int> dstLocalInd ; |
---|
952 | int dimElement = axisDomainDstOrder(elementPositionInGrid); |
---|
953 | if (2 == dimElement) //domain |
---|
954 | { |
---|
955 | CDomain* domain = domainListDstP[elementPositionInGridDst2DomainPosition_[elementPositionInGrid]] ; |
---|
956 | int ni_glo=domain->ni_glo ; |
---|
957 | int nj_glo=domain->nj_glo ; |
---|
958 | int nindex_glo=ni_glo*nj_glo ; |
---|
959 | dstLocalInd.resize(nindex_glo,-1) ; |
---|
960 | int nIndex=domain->i_index.numElements() ; |
---|
961 | CArray<bool,1>& localMask=domain->localMask ; |
---|
962 | int unmaskedInd=0 ; |
---|
963 | int globIndex ; |
---|
964 | for(int i=0;i<nIndex;i++) |
---|
965 | { |
---|
966 | if (localMask(i)) |
---|
967 | { |
---|
968 | globIndex=domain->j_index(i)*ni_glo+domain->i_index(i) ; |
---|
969 | dstLocalInd[globIndex]=unmaskedInd ; |
---|
970 | unmaskedInd++ ; |
---|
971 | } |
---|
972 | } |
---|
973 | } |
---|
974 | else if (1 == dimElement) //axis |
---|
975 | { |
---|
976 | CAxis* axis = axisListDstP[elementPositionInGridDst2AxisPosition_[elementPositionInGrid]] ; |
---|
977 | int nindex_glo=axis->n_glo ; |
---|
978 | dstLocalInd.resize(nindex_glo,-1) ; |
---|
979 | int nIndex=axis->index.numElements() ; |
---|
980 | CArray<bool,1>& localMask=axis->mask ; // axis mask must include later data_index |
---|
981 | int unmaskedInd=0 ; |
---|
982 | for(int i=0;i<nIndex;i++) |
---|
983 | { |
---|
984 | if (localMask(i)) |
---|
985 | { |
---|
986 | dstLocalInd[axis->index(i)]=unmaskedInd ; |
---|
987 | unmaskedInd++ ; |
---|
988 | } |
---|
989 | } |
---|
990 | } |
---|
991 | else //scalar |
---|
992 | { |
---|
993 | dstLocalInd.resize(1) ; |
---|
994 | dstLocalInd[0]=0 ; |
---|
995 | } |
---|
996 | |
---|
997 | vector<vector<vector<pair<int,double> > > > dstIndWeight(transformationMapping_.size()) ; |
---|
998 | |
---|
999 | for(int t=0;t<transformationMapping_.size();++t) |
---|
1000 | { |
---|
1001 | TransformationIndexMap::const_iterator itTransMap = transformationMapping_[t].begin(), |
---|
1002 | iteTransMap = transformationMapping_[t].end(); |
---|
1003 | TransformationWeightMap::const_iterator itTransWeight = transformationWeight_[t].begin(); |
---|
1004 | dstIndWeight[t].resize(nIndexSrc[elementPositionInGrid]) ; |
---|
1005 | |
---|
1006 | for(;itTransMap!=iteTransMap;++itTransMap,++itTransWeight) |
---|
1007 | { |
---|
1008 | int dst=dstLocalInd[itTransMap->first] ; |
---|
1009 | if (dst!=-1) |
---|
1010 | { |
---|
1011 | const vector<int>& srcs=itTransMap->second; |
---|
1012 | const vector<double>& weights=itTransWeight->second; |
---|
1013 | for(int i=0;i<srcs.size() ;i++) dstIndWeight[t][srcs[i]].push_back(pair<int,double>(dst*offset+t,weights[i])) ; |
---|
1014 | } |
---|
1015 | } |
---|
1016 | } |
---|
1017 | int srcInd=0 ; |
---|
1018 | int currentInd ; |
---|
1019 | int t=0 ; |
---|
1020 | int srcIndCompressed=0 ; |
---|
1021 | |
---|
1022 | nonDistributedrecursiveFunct(nElement-1,true,elementPositionInGrid,maskSrc,maskDst, srcInd, srcIndCompressed, nIndexSrc, t, dstIndWeight, |
---|
1023 | currentInd,localSrc,localDst,weight); |
---|
1024 | |
---|
1025 | } |
---|
1026 | CATCH |
---|
1027 | |
---|
1028 | |
---|
1029 | void CGenericAlgorithmTransformation::nonDistributedrecursiveFunct(int currentPos, bool masked, int elementPositionInGrid, |
---|
1030 | vector< CArray<bool,1>* >& maskSrc, vector< CArray<bool,1>* >& maskDst, |
---|
1031 | int& srcInd, int& srcIndCompressed, vector<int>& nIndexSrc, |
---|
1032 | int& t, vector<vector<vector<pair<int,double> > > >& dstIndWeight, int currentInd, |
---|
1033 | vector<int>& localSrc, vector<int>& localDst, vector<double>& weight) |
---|
1034 | TRY |
---|
1035 | { |
---|
1036 | int masked_ ; |
---|
1037 | if (currentPos!=elementPositionInGrid) |
---|
1038 | { |
---|
1039 | if (currentPos!=0) |
---|
1040 | { |
---|
1041 | CArray<bool,1>& mask = *maskSrc[currentPos] ; |
---|
1042 | |
---|
1043 | for(int i=0;i<nIndexSrc[currentPos];i++) |
---|
1044 | { |
---|
1045 | masked_=masked ; |
---|
1046 | if (!mask(i)) masked_=false ; |
---|
1047 | nonDistributedrecursiveFunct(currentPos-1, masked_, elementPositionInGrid, maskSrc, maskDst, srcInd, srcIndCompressed, nIndexSrc, t, |
---|
1048 | dstIndWeight, currentInd, localSrc, localDst, weight); |
---|
1049 | } |
---|
1050 | } |
---|
1051 | else |
---|
1052 | { |
---|
1053 | CArray<bool,1>& mask = *maskSrc[currentPos] ; |
---|
1054 | for(int i=0;i<nIndexSrc[currentPos];i++) |
---|
1055 | { |
---|
1056 | if (masked && mask(i)) |
---|
1057 | { |
---|
1058 | if (dstIndWeight[t][currentInd].size()>0) |
---|
1059 | { |
---|
1060 | for(vector<pair<int,double> >::iterator it = dstIndWeight[t][currentInd].begin(); it!=dstIndWeight[t][currentInd].end(); ++it) |
---|
1061 | { |
---|
1062 | localSrc.push_back(srcIndCompressed) ; |
---|
1063 | localDst.push_back(it->first) ; |
---|
1064 | weight.push_back(it->second) ; |
---|
1065 | (it->first)++ ; |
---|
1066 | } |
---|
1067 | } |
---|
1068 | if (t < dstIndWeight.size()-1) t++ ; |
---|
1069 | srcIndCompressed ++ ; |
---|
1070 | } |
---|
1071 | srcInd++ ; |
---|
1072 | } |
---|
1073 | } |
---|
1074 | } |
---|
1075 | else |
---|
1076 | { |
---|
1077 | |
---|
1078 | if (currentPos!=0) |
---|
1079 | { |
---|
1080 | |
---|
1081 | CArray<bool,1>& mask = *maskSrc[currentPos] ; |
---|
1082 | for(int i=0;i<nIndexSrc[currentPos];i++) |
---|
1083 | { |
---|
1084 | t=0 ; |
---|
1085 | masked_=masked ; |
---|
1086 | if (!mask(i)) masked_=false ; |
---|
1087 | nonDistributedrecursiveFunct(currentPos-1, masked_, elementPositionInGrid, maskSrc, maskDst, srcInd, |
---|
1088 | srcIndCompressed, nIndexSrc, t, dstIndWeight , i, localSrc, localDst, weight); |
---|
1089 | } |
---|
1090 | } |
---|
1091 | else |
---|
1092 | { |
---|
1093 | for(int i=0;i<nIndexSrc[currentPos];i++) |
---|
1094 | { |
---|
1095 | if (masked) |
---|
1096 | { |
---|
1097 | t=0 ; |
---|
1098 | if (dstIndWeight[t][i].size()>0) |
---|
1099 | { |
---|
1100 | for(vector<pair<int,double> >::iterator it = dstIndWeight[t][i].begin(); it!=dstIndWeight[t][i].end(); ++it) |
---|
1101 | { |
---|
1102 | localSrc.push_back(srcIndCompressed) ; |
---|
1103 | localDst.push_back(it->first) ; |
---|
1104 | weight.push_back(it->second) ; |
---|
1105 | (it->first)++ ; |
---|
1106 | } |
---|
1107 | } |
---|
1108 | if (t < dstIndWeight.size()-1) t++ ; |
---|
1109 | srcIndCompressed ++ ; |
---|
1110 | } |
---|
1111 | srcInd++ ; |
---|
1112 | } |
---|
1113 | } |
---|
1114 | } |
---|
1115 | |
---|
1116 | } |
---|
1117 | CATCH |
---|
1118 | |
---|
1119 | /*! |
---|
1120 | Compute index mapping between element source and element destination with an auxiliary inputs which determine |
---|
1121 | position of each mapped index in global index of grid destination. |
---|
1122 | \param [in] dataAuxInputs auxiliary inputs |
---|
1123 | */ |
---|
1124 | void CGenericAlgorithmTransformation::computeIndexSourceMapping(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
1125 | TRY |
---|
1126 | { |
---|
1127 | computeIndexSourceMapping_(dataAuxInputs); |
---|
1128 | } |
---|
1129 | CATCH |
---|
1130 | |
---|
1131 | std::vector<StdString> CGenericAlgorithmTransformation::getIdAuxInputs() |
---|
1132 | TRY |
---|
1133 | { |
---|
1134 | return idAuxInputs_; |
---|
1135 | } |
---|
1136 | CATCH |
---|
1137 | |
---|
1138 | CGenericAlgorithmTransformation::AlgoTransType CGenericAlgorithmTransformation::type() |
---|
1139 | TRY |
---|
1140 | { |
---|
1141 | return type_; |
---|
1142 | } |
---|
1143 | CATCH |
---|
1144 | |
---|
1145 | } |
---|