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