[624] | 1 | /*! |
---|
| 2 | \file generic_algorithm_transformation.hpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 14 May 2015 |
---|
[829] | 5 | \date 21 Mars 2016 |
---|
[624] | 6 | |
---|
| 7 | \brief Interface for all transformation algorithms. |
---|
| 8 | */ |
---|
[620] | 9 | #include "generic_algorithm_transformation.hpp" |
---|
[862] | 10 | #include "context.hpp" |
---|
| 11 | #include "context_client.hpp" |
---|
| 12 | #include "client_client_dht_template.hpp" |
---|
[1158] | 13 | #include "utils.hpp" |
---|
[1216] | 14 | #include "timer.hpp" |
---|
[1399] | 15 | #include "mpi.hpp" |
---|
[620] | 16 | |
---|
| 17 | namespace xios { |
---|
| 18 | |
---|
| 19 | CGenericAlgorithmTransformation::CGenericAlgorithmTransformation() |
---|
[933] | 20 | : transformationMapping_(), transformationWeight_(), transformationPosition_(), |
---|
[1216] | 21 | idAuxInputs_(), type_(ELEMENT_NO_MODIFICATION_WITH_DATA), indexElementSrc_(), |
---|
[1399] | 22 | computedProcSrcNonTransformedElement_(false), eliminateRedondantSrc_(true), isDistributedComputed_(false) |
---|
[620] | 23 | { |
---|
| 24 | } |
---|
| 25 | |
---|
[979] | 26 | void CGenericAlgorithmTransformation::updateData(CArray<double,1>& dataOut) |
---|
| 27 | { |
---|
| 28 | |
---|
| 29 | } |
---|
| 30 | |
---|
[888] | 31 | void CGenericAlgorithmTransformation::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
| 32 | const double* dataInput, |
---|
| 33 | CArray<double,1>& dataOut, |
---|
[918] | 34 | std::vector<bool>& flagInitial, |
---|
[1260] | 35 | bool ignoreMissingValue, bool firstPass ) |
---|
[1646] | 36 | TRY |
---|
[888] | 37 | { |
---|
[1677] | 38 | |
---|
[1158] | 39 | int nbLocalIndex = localIndex.size(); |
---|
| 40 | double defaultValue = std::numeric_limits<double>::quiet_NaN(); |
---|
[1601] | 41 | |
---|
[1158] | 42 | if (ignoreMissingValue) |
---|
[888] | 43 | { |
---|
[1480] | 44 | if (firstPass) dataOut=defaultValue ; |
---|
[1601] | 45 | |
---|
[918] | 46 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
| 47 | { |
---|
[1480] | 48 | if (! NumTraits<double>::isNan(*(dataInput + idx))) |
---|
[918] | 49 | { |
---|
[1480] | 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 |
---|
[918] | 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
[888] | 56 | } |
---|
[918] | 57 | else |
---|
| 58 | { |
---|
| 59 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
| 60 | { |
---|
| 61 | dataOut(localIndex[idx].first) += *(dataInput + idx) * localIndex[idx].second; |
---|
| 62 | } |
---|
| 63 | } |
---|
[888] | 64 | } |
---|
[1646] | 65 | CATCH |
---|
[888] | 66 | |
---|
| 67 | void CGenericAlgorithmTransformation::computePositionElements(CGrid* dst, CGrid* src) |
---|
[1646] | 68 | TRY |
---|
[888] | 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 | } |
---|
[1646] | 114 | CATCH |
---|
[888] | 115 | |
---|
[1399] | 116 | bool CGenericAlgorithmTransformation::isDistributedTransformation(int elementPositionInGrid, CGrid* gridSrc, CGrid* gridDst) |
---|
[1646] | 117 | TRY |
---|
[1399] | 118 | { |
---|
| 119 | |
---|
| 120 | if (!isDistributedComputed_) |
---|
| 121 | { |
---|
[1400] | 122 | isDistributedComputed_=true ; |
---|
| 123 | if (!eliminateRedondantSrc_) isDistributed_=true ; |
---|
| 124 | else |
---|
| 125 | { |
---|
| 126 | CContext* context = CContext::getCurrent(); |
---|
| 127 | CContextClient* client = context->client; |
---|
[1399] | 128 | |
---|
[1400] | 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 ; |
---|
[1399] | 134 | |
---|
[1400] | 135 | CArray<int,1> axisDomainSrcOrder = gridSrc->axis_domain_order; |
---|
| 136 | if (2 == axisDomainSrcOrder(elementPositionInGrid)) // It's domain |
---|
| 137 | { |
---|
| 138 | distributed=domainListSrcP[elementPositionInGridSrc2DomainPosition_[elementPositionInGrid]]->isDistributed() ; |
---|
[1661] | 139 | MPI_Allreduce(&distributed,&distributed_glo, 1, MPI_INT, MPI_LOR, client->intraComm) ; |
---|
[1399] | 140 | |
---|
[1400] | 141 | } |
---|
| 142 | else if (1 == axisDomainSrcOrder(elementPositionInGrid))//it's an axis |
---|
| 143 | { |
---|
| 144 | distributed=axisListSrcP[elementPositionInGridSrc2AxisPosition_[elementPositionInGrid]]->isDistributed() ; |
---|
[1661] | 145 | MPI_Allreduce(&distributed,&distributed_glo, 1, MPI_INT, MPI_LOR, client->intraComm) ; |
---|
[1400] | 146 | } |
---|
| 147 | else //it's a scalar |
---|
| 148 | { |
---|
| 149 | distributed_glo=false ; |
---|
| 150 | } |
---|
| 151 | isDistributed_=distributed_glo ; |
---|
[1399] | 152 | } |
---|
| 153 | } |
---|
| 154 | return isDistributed_ ; |
---|
[1646] | 155 | } |
---|
| 156 | CATCH |
---|
| 157 | |
---|
[620] | 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), |
---|
[862] | 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 |
---|
[867] | 164 | \param[in\out] globaIndexWeightFromSrcToDst mapping of each global index source and weight to index destination |
---|
[862] | 165 | */ |
---|
[620] | 166 | void CGenericAlgorithmTransformation::computeGlobalSourceIndex(int elementPositionInGrid, |
---|
[862] | 167 | CGrid* gridSrc, |
---|
| 168 | CGrid* gridDst, |
---|
| 169 | SourceDestinationIndexMap& globaIndexWeightFromSrcToDst) |
---|
[1646] | 170 | TRY |
---|
[862] | 171 | { |
---|
| 172 | CContext* context = CContext::getCurrent(); |
---|
| 173 | CContextClient* client = context->client; |
---|
| 174 | int nbClient = client->clientSize; |
---|
| 175 | |
---|
[1542] | 176 | typedef std::unordered_map<int, std::vector<std::pair<int,double> > > SrcToDstMap; |
---|
[1216] | 177 | int idx; |
---|
[866] | 178 | |
---|
[1216] | 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 | |
---|
[866] | 190 | bool isTransPosEmpty = transformationPosition_.empty(); |
---|
| 191 | CArray<size_t,1> transPos; |
---|
| 192 | if (!isTransPosEmpty) transPos.resize(transformationMapping_.size()); |
---|
[1221] | 193 | std::set<size_t> allIndexSrc; // All index of source, which can be scattered among processes, need for doing transformation |
---|
[1216] | 194 | |
---|
[866] | 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 | |
---|
[862] | 201 | // Build mapping between global source element index and global destination element index. |
---|
| 202 | itTransWeight = itbTransWeight; |
---|
[827] | 203 | for (itTransMap = itbTransMap; itTransMap != iteTransMap; ++itTransMap, ++itTransWeight) |
---|
| 204 | { |
---|
[862] | 205 | const std::vector<int>& srcIndex = itTransMap->second; |
---|
[1216] | 206 | for (idx = 0; idx < srcIndex.size(); ++idx) |
---|
| 207 | allIndexSrc.insert(srcIndex[idx]); |
---|
[862] | 208 | } |
---|
| 209 | |
---|
| 210 | if (!isTransPosEmpty) |
---|
| 211 | { |
---|
[866] | 212 | TransformationPositionMap::const_iterator itPosMap = transformationPosition_[idxTrans].begin(); |
---|
| 213 | transPos(idxTrans) = itPosMap->second[0]; |
---|
[862] | 214 | } |
---|
[866] | 215 | } |
---|
| 216 | |
---|
[1216] | 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; |
---|
[866] | 221 | |
---|
[1216] | 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 |
---|
[866] | 227 | { |
---|
[1216] | 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 | } |
---|
[866] | 235 | |
---|
[1216] | 236 | if (computeGlobalIndexOnProc) |
---|
| 237 | indexElementSrc_.swap(allIndexSrc); |
---|
| 238 | |
---|
| 239 | int sendValue = (computeGlobalIndexOnProc) ? 1 : 0; |
---|
| 240 | int recvValue = 0; |
---|
[1661] | 241 | MPI_Allreduce(&sendValue, &recvValue, 1, MPI_INT, MPI_SUM, client->intraComm); |
---|
[1217] | 242 | computeGlobalIndexOnProc = (0 < recvValue); |
---|
[1216] | 243 | |
---|
[1400] | 244 | // CClientClientDHTInt::Index2VectorInfoTypeMap globalIndexOfTransformedElementOnProc; |
---|
[1399] | 245 | |
---|
[1221] | 246 | if (computeGlobalIndexOnProc || !computedProcSrcNonTransformedElement_) |
---|
[1216] | 247 | { |
---|
[1400] | 248 | { |
---|
| 249 | CClientClientDHTInt::Index2VectorInfoTypeMap tmp ; |
---|
| 250 | globalIndexOfTransformedElementOnProc_.swap(tmp) ; |
---|
| 251 | } |
---|
[1216] | 252 | // Find out global index source of transformed element on corresponding process. |
---|
| 253 | if (globalElementIndexOnProc_.empty()) |
---|
| 254 | globalElementIndexOnProc_.resize(axisDomainDstOrder.numElements()); |
---|
[1298] | 255 | |
---|
[1216] | 256 | for (idx = 0; idx < axisDomainDstOrder.numElements(); ++idx) |
---|
[866] | 257 | { |
---|
[1400] | 258 | |
---|
[1216] | 259 | if (idx == elementPositionInGrid) |
---|
[1400] | 260 | computeExchangeGlobalIndex(indexSrc, axisDomainSrcOrder(idx), globalIndexOfTransformedElementOnProc_); //globalElementIndexOnProc[idx]); |
---|
[1216] | 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]); |
---|
[888] | 270 | |
---|
[1216] | 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 | } |
---|
[888] | 290 | } |
---|
[866] | 291 | |
---|
[1216] | 292 | if (!isTransPosEmpty && !computedProcSrcNonTransformedElement_) |
---|
[866] | 293 | { |
---|
[1216] | 294 | for (idx = 0; idx < globalElementIndexOnProc_.size(); ++idx) |
---|
[827] | 295 | { |
---|
[1216] | 296 | if (idx != elementPositionInGrid) |
---|
| 297 | { |
---|
[1542] | 298 | std::unordered_map<int,std::vector<size_t> >::iterator itb = globalElementIndexOnProc_[idx].begin(), it, |
---|
[1216] | 299 | ite = globalElementIndexOnProc_[idx].end(); |
---|
| 300 | for (it = itb; it != ite; ++it) it->second.resize(1); |
---|
| 301 | } |
---|
[866] | 302 | } |
---|
| 303 | } |
---|
[1389] | 304 | |
---|
| 305 | /* |
---|
[1216] | 306 | if (!computedProcSrcNonTransformedElement_) |
---|
| 307 | { |
---|
| 308 | for (idx = 0; idx < globalElementIndexOnProc_.size(); ++idx) |
---|
| 309 | { |
---|
| 310 | if (idx != elementPositionInGrid) |
---|
| 311 | { |
---|
[1542] | 312 | std::unordered_map<int,std::vector<size_t> >::iterator itb = globalElementIndexOnProc_[idx].begin(), it, |
---|
[1216] | 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); |
---|
[1389] | 343 | */ |
---|
[1399] | 344 | |
---|
[1389] | 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) ; |
---|
[1400] | 354 | CClientClientDHTInt::Index2VectorInfoTypeMap::iterator itIdxb = globalIndexOfTransformedElementOnProc_.begin(), |
---|
| 355 | itIdxe = globalIndexOfTransformedElementOnProc_.end(), itIdx; |
---|
[1389] | 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) ; |
---|
[1542] | 370 | std::unordered_map<int,std::vector<size_t> >::iterator itb = globalElementIndexOnProc_[idx].begin(), it, |
---|
[1389] | 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 | } |
---|
[1216] | 379 | |
---|
[1426] | 380 | if (idx==0) commonProc_= procList ; |
---|
[1389] | 381 | else |
---|
| 382 | { |
---|
[1399] | 383 | for (std::set<int>::iterator it = commonProc_.begin(); it != commonProc_.end(); ++it) |
---|
[1389] | 384 | if (procList.count(*it)==1) commonTmp.insert(*it) ; |
---|
[1399] | 385 | commonProc_.swap(commonTmp) ; |
---|
[1389] | 386 | } |
---|
| 387 | } |
---|
[1399] | 388 | std::vector<int> procContainSrcElementIdx(commonProc_.size()); |
---|
[1389] | 389 | int count = 0; |
---|
[1399] | 390 | for (std::set<int>::iterator it = commonProc_.begin(); it != commonProc_.end(); ++it) procContainSrcElementIdx[count++] = *it; |
---|
[1389] | 391 | procContainSrcElementIdx_.swap(procContainSrcElementIdx); |
---|
| 392 | |
---|
[1216] | 393 | // For the first time, surely we calculate proc containing non transformed elements |
---|
[1389] | 394 | if (!computedProcSrcNonTransformedElement_) computedProcSrcNonTransformedElement_ = true; |
---|
[866] | 395 | } |
---|
[1216] | 396 | |
---|
[866] | 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. |
---|
[1542] | 406 | std::unordered_map<int,std::vector<size_t> >().swap(globalElementIndexOnProc_[elementPositionInGrid]); |
---|
[1216] | 407 | std::set<int> tmpCounter; |
---|
[866] | 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; |
---|
[1216] | 413 | for (idx = 0; idx < srcIndex.size(); ++idx) |
---|
[866] | 414 | { |
---|
| 415 | src2DstMap[srcIndex[idx]].push_back(make_pair(itTransMap->first, weight[idx])); |
---|
[1216] | 416 | if (0 == tmpCounter.count(srcIndex[idx])) |
---|
| 417 | { |
---|
| 418 | tmpCounter.insert(srcIndex[idx]); |
---|
[1389] | 419 | |
---|
[1400] | 420 | vector<int>& rankSrc = globalIndexOfTransformedElementOnProc_[srcIndex[idx]] ; |
---|
[1399] | 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]); |
---|
[866] | 427 | } |
---|
[827] | 428 | } |
---|
[866] | 429 | } |
---|
[1216] | 430 | |
---|
[866] | 431 | if (!isTransPosEmpty) |
---|
| 432 | { |
---|
[1216] | 433 | for (idx = 0; idx < globalElementIndexOnProc_.size(); ++idx) |
---|
[862] | 434 | { |
---|
| 435 | if (idx != elementPositionInGrid) |
---|
[866] | 436 | { |
---|
[1542] | 437 | std::unordered_map<int,std::vector<size_t> >::iterator itb = globalElementIndexOnProc_[idx].begin(), it, |
---|
[1216] | 438 | ite = globalElementIndexOnProc_[idx].end(); |
---|
[866] | 439 | for (it = itb; it != ite; ++it) it->second[0] = transPos(idxTrans); |
---|
| 440 | } |
---|
[862] | 441 | } |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | // Ok, now compute global index of grid source and ones of grid destination |
---|
| 445 | computeGlobalGridIndexMapping(elementPositionInGrid, |
---|
[1216] | 446 | procContainSrcElementIdx_, //srcRank, |
---|
[862] | 447 | src2DstMap, |
---|
[871] | 448 | gridSrc, |
---|
[862] | 449 | gridDst, |
---|
[1216] | 450 | globalElementIndexOnProc_, |
---|
[862] | 451 | globaIndexWeightFromSrcToDst); |
---|
[1216] | 452 | } |
---|
[862] | 453 | } |
---|
[1646] | 454 | CATCH |
---|
[862] | 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 |
---|
[1274] | 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 |
---|
[862] | 465 | */ |
---|
| 466 | void CGenericAlgorithmTransformation::computeGlobalGridIndexMapping(int elementPositionInGrid, |
---|
| 467 | const std::vector<int>& srcRank, |
---|
[1542] | 468 | std::unordered_map<int, std::vector<std::pair<int,double> > >& src2DstMap, |
---|
[862] | 469 | CGrid* gridSrc, |
---|
| 470 | CGrid* gridDst, |
---|
[1542] | 471 | std::vector<std::unordered_map<int,std::vector<size_t> > >& globalElementIndexOnProc, |
---|
[862] | 472 | SourceDestinationIndexMap& globaIndexWeightFromSrcToDst) |
---|
[1646] | 473 | TRY |
---|
[862] | 474 | { |
---|
[1274] | 475 | SourceDestinationIndexMap globaIndexWeightFromSrcToDst_tmp ; |
---|
| 476 | |
---|
| 477 | CContext* context = CContext::getCurrent(); |
---|
| 478 | CContextClient* client=context->client; |
---|
| 479 | int clientRank = client->clientRank; |
---|
| 480 | |
---|
[887] | 481 | std::vector<CDomain*> domainListSrcP = gridSrc->getDomains(); |
---|
[862] | 482 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
[887] | 483 | std::vector<CScalar*> scalarListSrcP = gridSrc->getScalars(); |
---|
| 484 | CArray<int,1> axisDomainSrcOrder = gridSrc->axis_domain_order; |
---|
| 485 | |
---|
[862] | 486 | size_t nbElement = axisDomainSrcOrder.numElements(); |
---|
[887] | 487 | std::vector<size_t> nGlobSrc(nbElement); |
---|
| 488 | size_t globalSrcSize = 1; |
---|
| 489 | int domainIndex = 0, axisIndex = 0, scalarIndex = 0; |
---|
[862] | 490 | for (int idx = 0; idx < nbElement; ++idx) |
---|
| 491 | { |
---|
| 492 | nGlobSrc[idx] = globalSrcSize; |
---|
[887] | 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; |
---|
[1274] | 521 | set<size_t> globalSrcStoreIndex ; |
---|
| 522 | |
---|
[887] | 523 | for (int idx = 0; idx < nbElement; ++idx) |
---|
| 524 | { |
---|
[862] | 525 | nGlobDst[idx] = globalDstSize; |
---|
[888] | 526 | int elementDimension = axisDomainDstOrder(idx); |
---|
[862] | 527 | |
---|
| 528 | // If this is a domain |
---|
[887] | 529 | if (2 == elementDimension) |
---|
[862] | 530 | { |
---|
| 531 | globalDstSize *= domainListDestP[domainIndex]->nj_glo.getValue() * domainListDestP[domainIndex]->ni_glo.getValue(); |
---|
| 532 | ++domainIndex; |
---|
| 533 | } |
---|
[887] | 534 | else if (1 == elementDimension) // So it's an axis |
---|
[862] | 535 | { |
---|
| 536 | globalDstSize *= axisListDestP[axisIndex]->n_glo.getValue(); |
---|
| 537 | ++axisIndex; |
---|
| 538 | } |
---|
[887] | 539 | else |
---|
| 540 | { |
---|
| 541 | globalDstSize *= 1; |
---|
| 542 | ++scalarIndex; |
---|
| 543 | } |
---|
[862] | 544 | } |
---|
| 545 | |
---|
[1274] | 546 | std::map< std::pair<size_t,size_t>, int > rankMap ; |
---|
| 547 | std::map< std::pair<size_t,size_t>, int >:: iterator rankMapIt ; |
---|
| 548 | |
---|
[862] | 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) |
---|
[827] | 566 | { |
---|
[862] | 567 | if (idxLoop[ind] == (globalElementIndexOnProc[ind])[rankSrc].size()) |
---|
[827] | 568 | { |
---|
[862] | 569 | idxLoop[ind] = 0; |
---|
| 570 | ++idxLoop[ind+1]; |
---|
[827] | 571 | } |
---|
[862] | 572 | |
---|
| 573 | currentIndexDst[ind] = currentIndexSrc[ind] = (globalElementIndexOnProc[ind])[rankSrc][idxLoop[ind]]; |
---|
[827] | 574 | } |
---|
[862] | 575 | |
---|
| 576 | for (int ind = 0; ind < innnerLoopSize; ++ind) |
---|
| 577 | { |
---|
[868] | 578 | currentIndexDst[0] = currentIndexSrc[0] = (globalElementIndexOnProc[0])[rankSrc][ind]; |
---|
[862] | 579 | int globalElementDstIndexSize = 0; |
---|
| 580 | if (1 == src2DstMap.count(currentIndexSrc[elementPositionInGrid])) |
---|
| 581 | { |
---|
| 582 | globalElementDstIndexSize = src2DstMap[currentIndexSrc[elementPositionInGrid]].size(); |
---|
| 583 | } |
---|
[868] | 584 | |
---|
[862] | 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 | { |
---|
[1274] | 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 ; |
---|
[862] | 613 | } |
---|
| 614 | ++idxLoop[0]; |
---|
| 615 | } |
---|
| 616 | idx += innnerLoopSize; |
---|
[620] | 617 | } |
---|
| 618 | } |
---|
[1274] | 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 |
---|
[1298] | 622 | int rankSrc ; |
---|
| 623 | size_t globalSrcIndex ; |
---|
| 624 | size_t globalDstIndex ; |
---|
| 625 | double weight ; |
---|
[1274] | 626 | |
---|
[1298] | 627 | SourceDestinationIndexMap::iterator rankIt,rankIte ; |
---|
[1542] | 628 | std::unordered_map<size_t, std::vector<std::pair<size_t,double> > >::iterator globalSrcIndexIt, globalSrcIndexIte ; |
---|
[1298] | 629 | std::vector<std::pair<size_t,double> >::iterator vectIt,vectIte ; |
---|
[1274] | 630 | |
---|
[1298] | 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 | } |
---|
[1274] | 651 | } |
---|
| 652 | } |
---|
[620] | 653 | } |
---|
[1646] | 654 | CATCH |
---|
[620] | 655 | |
---|
[862] | 656 | /*! |
---|
| 657 | Find out proc and global index of axis source which axis destination is on demande |
---|
[888] | 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, |
---|
[1542] | 666 | std::unordered_map<int,std::vector<size_t> >& globalScalarIndexOnProc) |
---|
[1646] | 667 | TRY |
---|
[888] | 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 | } |
---|
[1646] | 680 | CATCH |
---|
[888] | 681 | |
---|
| 682 | /*! |
---|
| 683 | Find out proc and global index of axis source which axis destination is on demande |
---|
[862] | 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, |
---|
[1542] | 692 | std::unordered_map<int,std::vector<size_t> >& globalAxisIndexOnProc) |
---|
[1646] | 693 | TRY |
---|
[862] | 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 | { |
---|
[1402] | 706 | if (axisSrc->mask(idx)) |
---|
| 707 | { |
---|
| 708 | globalIndex = axisSrc->index(idx); |
---|
| 709 | globalIndex2ProcRank[globalIndex].push_back(clientRank); |
---|
| 710 | } |
---|
[862] | 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 | } |
---|
[1646] | 751 | CATCH |
---|
[862] | 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, |
---|
[1542] | 763 | std::unordered_map<int,std::vector<size_t> >& globalDomainIndexOnProc) |
---|
[1646] | 764 | TRY |
---|
[862] | 765 | { |
---|
| 766 | CContext* context = CContext::getCurrent(); |
---|
| 767 | CContextClient* client=context->client; |
---|
| 768 | int clientRank = client->clientRank; |
---|
| 769 | int clientSize = client->clientSize; |
---|
| 770 | |
---|
[866] | 771 | int niGlobSrc = domainSrc->ni_glo.getValue(); |
---|
[862] | 772 | size_t globalIndex; |
---|
[866] | 773 | int i_ind, j_ind; |
---|
| 774 | int nIndexSize = (destGlobalIndexPositionInGrid.isEmpty()) ? domainSrc->i_index.numElements() |
---|
| 775 | : destGlobalIndexPositionInGrid.numElements(); |
---|
[862] | 776 | CClientClientDHTInt::Index2VectorInfoTypeMap globalIndex2ProcRank; |
---|
| 777 | globalIndex2ProcRank.rehash(std::ceil(nIndexSize/globalIndex2ProcRank.max_load_factor())); |
---|
[1402] | 778 | |
---|
[866] | 779 | if (destGlobalIndexPositionInGrid.isEmpty()) |
---|
[862] | 780 | { |
---|
[866] | 781 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
| 782 | { |
---|
| 783 | i_ind=domainSrc->i_index(idx) ; |
---|
| 784 | j_ind=domainSrc->j_index(idx) ; |
---|
[862] | 785 | |
---|
[1402] | 786 | if (domainSrc->localMask(idx)) |
---|
| 787 | { |
---|
| 788 | globalIndex = i_ind + j_ind * niGlobSrc; |
---|
| 789 | globalIndex2ProcRank[globalIndex].resize(1); |
---|
| 790 | globalIndex2ProcRank[globalIndex][0] = clientRank; |
---|
| 791 | } |
---|
[866] | 792 | } |
---|
[862] | 793 | } |
---|
[866] | 794 | else |
---|
| 795 | { |
---|
| 796 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
| 797 | { |
---|
[1402] | 798 | // if (domainSrc->localMask(idx)) -> not necessairy, mask seem to be included in destGlobalIndexPositionInGrid(idx) (ym) |
---|
| 799 | globalIndex2ProcRank[destGlobalIndexPositionInGrid(idx)].push_back(clientRank); |
---|
[866] | 800 | } |
---|
| 801 | } |
---|
[862] | 802 | |
---|
| 803 | CArray<size_t,1> globalDomainIndex; |
---|
| 804 | if (destGlobalIndexPositionInGrid.isEmpty()) |
---|
| 805 | { |
---|
[866] | 806 | int niGlobDst = domainDst->ni_glo.getValue(); |
---|
[862] | 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 | |
---|
[866] | 815 | globalDomainIndex(idx) = i_ind + j_ind * niGlobDst; |
---|
[862] | 816 | } |
---|
| 817 | } |
---|
| 818 | else |
---|
| 819 | { |
---|
[866] | 820 | globalDomainIndex.reference(destGlobalIndexPositionInGrid); |
---|
[862] | 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 | } |
---|
[1646] | 856 | CATCH |
---|
[862] | 857 | |
---|
[1399] | 858 | void CGenericAlgorithmTransformation::computeTransformationMappingNonDistributed(int elementPositionInGrid, CGrid* gridSrc, CGrid* gridDst, |
---|
[1646] | 859 | vector<int>& localSrc, vector<int>& localDst, vector<double>& weight, |
---|
| 860 | int& nlocalIndexDest) |
---|
| 861 | TRY |
---|
[1399] | 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) ; |
---|
[1404] | 883 | vector< CArray<bool,1>* > maskSrc(nElement) ; |
---|
| 884 | vector< CArray<bool,1>* > maskDst(nElement) ; |
---|
| 885 | |
---|
[1400] | 886 | int nlocalIndexSrc=1 ; |
---|
[1646] | 887 | // int nlocalIndexDest=1 ; |
---|
| 888 | nlocalIndexDest=1 ; |
---|
[1404] | 889 | CArray<bool,1> maskScalar(1) ; |
---|
| 890 | maskScalar = true ; |
---|
[1399] | 891 | |
---|
[1404] | 892 | |
---|
[1399] | 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() ; |
---|
[1404] | 900 | maskSrc[i]=&domain->localMask ; |
---|
[1399] | 901 | } |
---|
| 902 | else if (1 == dimElement) //axis |
---|
| 903 | { |
---|
[1438] | 904 | CAxis* axis=axisListSrcP[elementPositionInGridSrc2AxisPosition_[i]] ; |
---|
[1399] | 905 | nIndexSrc[i] = axis->index.numElements() ; |
---|
[1404] | 906 | maskSrc[i]=&axis->mask ; |
---|
[1399] | 907 | } |
---|
| 908 | else //scalar |
---|
| 909 | { |
---|
| 910 | nIndexSrc[i]=1 ; |
---|
[1404] | 911 | maskSrc[i]=&maskScalar ; |
---|
[1399] | 912 | } |
---|
[1400] | 913 | nlocalIndexSrc=nlocalIndexSrc*nIndexSrc[i] ; |
---|
[1399] | 914 | } |
---|
| 915 | |
---|
[1404] | 916 | |
---|
| 917 | |
---|
[1399] | 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]] ; |
---|
[1404] | 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 ; |
---|
[1399] | 931 | } |
---|
| 932 | else if (1 == dimElement) //axis |
---|
| 933 | { |
---|
[1438] | 934 | CAxis* axis = axisListDstP[elementPositionInGridDst2AxisPosition_[i]] ; |
---|
[1404] | 935 | int nIndex=axis->index.numElements() ; |
---|
| 936 | CArray<bool,1>& localMask=axis->mask ; |
---|
| 937 | int nbInd=0 ; |
---|
[1407] | 938 | for(int j=0;j<nIndex;j++) if (localMask(j)) nbInd++ ; |
---|
[1404] | 939 | nIndexDst[i] = nbInd ; |
---|
| 940 | maskDst[i]=&axis->mask ; |
---|
[1399] | 941 | } |
---|
| 942 | else //scalar |
---|
| 943 | { |
---|
| 944 | nIndexDst[i]=1 ; |
---|
[1404] | 945 | maskDst[i]=&maskScalar ; |
---|
[1399] | 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 | { |
---|
[1408] | 955 | CDomain* domain = domainListDstP[elementPositionInGridDst2DomainPosition_[elementPositionInGrid]] ; |
---|
[1399] | 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() ; |
---|
[1404] | 961 | CArray<bool,1>& localMask=domain->localMask ; |
---|
| 962 | int unmaskedInd=0 ; |
---|
| 963 | int globIndex ; |
---|
[1399] | 964 | for(int i=0;i<nIndex;i++) |
---|
| 965 | { |
---|
[1404] | 966 | if (localMask(i)) |
---|
| 967 | { |
---|
| 968 | globIndex=domain->j_index(i)*ni_glo+domain->i_index(i) ; |
---|
| 969 | dstLocalInd[globIndex]=unmaskedInd ; |
---|
| 970 | unmaskedInd++ ; |
---|
| 971 | } |
---|
[1399] | 972 | } |
---|
| 973 | } |
---|
| 974 | else if (1 == dimElement) //axis |
---|
| 975 | { |
---|
[1408] | 976 | CAxis* axis = axisListDstP[elementPositionInGridDst2AxisPosition_[elementPositionInGrid]] ; |
---|
[1399] | 977 | int nindex_glo=axis->n_glo ; |
---|
| 978 | dstLocalInd.resize(nindex_glo,-1) ; |
---|
| 979 | int nIndex=axis->index.numElements() ; |
---|
[1404] | 980 | CArray<bool,1>& localMask=axis->mask ; // axis mask must include later data_index |
---|
| 981 | int unmaskedInd=0 ; |
---|
[1399] | 982 | for(int i=0;i<nIndex;i++) |
---|
| 983 | { |
---|
[1404] | 984 | if (localMask(i)) |
---|
| 985 | { |
---|
| 986 | dstLocalInd[axis->index(i)]=unmaskedInd ; |
---|
| 987 | unmaskedInd++ ; |
---|
| 988 | } |
---|
[1399] | 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 ; |
---|
[1420] | 1020 | int srcIndCompressed=0 ; |
---|
[1399] | 1021 | |
---|
[1420] | 1022 | nonDistributedrecursiveFunct(nElement-1,true,elementPositionInGrid,maskSrc,maskDst, srcInd, srcIndCompressed, nIndexSrc, t, dstIndWeight, |
---|
[1646] | 1023 | currentInd,localSrc,localDst,weight); |
---|
[1399] | 1024 | |
---|
| 1025 | } |
---|
[1646] | 1026 | CATCH |
---|
[1399] | 1027 | |
---|
| 1028 | |
---|
[1646] | 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 |
---|
[1399] | 1035 | { |
---|
[1425] | 1036 | int masked_ ; |
---|
[1399] | 1037 | if (currentPos!=elementPositionInGrid) |
---|
| 1038 | { |
---|
| 1039 | if (currentPos!=0) |
---|
| 1040 | { |
---|
[1404] | 1041 | CArray<bool,1>& mask = *maskSrc[currentPos] ; |
---|
| 1042 | |
---|
[1399] | 1043 | for(int i=0;i<nIndexSrc[currentPos];i++) |
---|
| 1044 | { |
---|
[1425] | 1045 | masked_=masked ; |
---|
| 1046 | if (!mask(i)) masked_=false ; |
---|
[1646] | 1047 | nonDistributedrecursiveFunct(currentPos-1, masked_, elementPositionInGrid, maskSrc, maskDst, srcInd, srcIndCompressed, nIndexSrc, t, |
---|
| 1048 | dstIndWeight, currentInd, localSrc, localDst, weight); |
---|
[1399] | 1049 | } |
---|
| 1050 | } |
---|
| 1051 | else |
---|
| 1052 | { |
---|
[1404] | 1053 | CArray<bool,1>& mask = *maskSrc[currentPos] ; |
---|
[1399] | 1054 | for(int i=0;i<nIndexSrc[currentPos];i++) |
---|
| 1055 | { |
---|
[1420] | 1056 | if (masked && mask(i)) |
---|
[1399] | 1057 | { |
---|
[1404] | 1058 | if (dstIndWeight[t][currentInd].size()>0) |
---|
[1399] | 1059 | { |
---|
[1404] | 1060 | for(vector<pair<int,double> >::iterator it = dstIndWeight[t][currentInd].begin(); it!=dstIndWeight[t][currentInd].end(); ++it) |
---|
[1399] | 1061 | { |
---|
[1646] | 1062 | localSrc.push_back(srcIndCompressed) ; |
---|
| 1063 | localDst.push_back(it->first) ; |
---|
| 1064 | weight.push_back(it->second) ; |
---|
[1404] | 1065 | (it->first)++ ; |
---|
[1399] | 1066 | } |
---|
| 1067 | } |
---|
[1404] | 1068 | if (t < dstIndWeight.size()-1) t++ ; |
---|
[1646] | 1069 | srcIndCompressed ++ ; |
---|
[1399] | 1070 | } |
---|
[1420] | 1071 | srcInd++ ; |
---|
[1399] | 1072 | } |
---|
| 1073 | } |
---|
| 1074 | } |
---|
| 1075 | else |
---|
| 1076 | { |
---|
| 1077 | |
---|
| 1078 | if (currentPos!=0) |
---|
| 1079 | { |
---|
| 1080 | |
---|
[1404] | 1081 | CArray<bool,1>& mask = *maskSrc[currentPos] ; |
---|
[1399] | 1082 | for(int i=0;i<nIndexSrc[currentPos];i++) |
---|
| 1083 | { |
---|
| 1084 | t=0 ; |
---|
[1425] | 1085 | masked_=masked ; |
---|
| 1086 | if (!mask(i)) masked_=false ; |
---|
[1646] | 1087 | nonDistributedrecursiveFunct(currentPos-1, masked_, elementPositionInGrid, maskSrc, maskDst, srcInd, |
---|
| 1088 | srcIndCompressed, nIndexSrc, t, dstIndWeight , i, localSrc, localDst, weight); |
---|
[1399] | 1089 | } |
---|
| 1090 | } |
---|
| 1091 | else |
---|
| 1092 | { |
---|
| 1093 | for(int i=0;i<nIndexSrc[currentPos];i++) |
---|
| 1094 | { |
---|
[1420] | 1095 | if (masked) |
---|
[1399] | 1096 | { |
---|
[1420] | 1097 | t=0 ; |
---|
| 1098 | if (dstIndWeight[t][i].size()>0) |
---|
[1399] | 1099 | { |
---|
[1420] | 1100 | for(vector<pair<int,double> >::iterator it = dstIndWeight[t][i].begin(); it!=dstIndWeight[t][i].end(); ++it) |
---|
[1399] | 1101 | { |
---|
[1646] | 1102 | localSrc.push_back(srcIndCompressed) ; |
---|
| 1103 | localDst.push_back(it->first) ; |
---|
| 1104 | weight.push_back(it->second) ; |
---|
[1420] | 1105 | (it->first)++ ; |
---|
[1399] | 1106 | } |
---|
[1425] | 1107 | } |
---|
[1420] | 1108 | if (t < dstIndWeight.size()-1) t++ ; |
---|
[1646] | 1109 | srcIndCompressed ++ ; |
---|
[1399] | 1110 | } |
---|
| 1111 | srcInd++ ; |
---|
| 1112 | } |
---|
| 1113 | } |
---|
| 1114 | } |
---|
| 1115 | |
---|
| 1116 | } |
---|
[1646] | 1117 | CATCH |
---|
[1399] | 1118 | |
---|
[862] | 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 | */ |
---|
[827] | 1124 | void CGenericAlgorithmTransformation::computeIndexSourceMapping(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
[1646] | 1125 | TRY |
---|
[827] | 1126 | { |
---|
| 1127 | computeIndexSourceMapping_(dataAuxInputs); |
---|
[620] | 1128 | } |
---|
[1646] | 1129 | CATCH |
---|
[827] | 1130 | |
---|
| 1131 | std::vector<StdString> CGenericAlgorithmTransformation::getIdAuxInputs() |
---|
[1646] | 1132 | TRY |
---|
[827] | 1133 | { |
---|
| 1134 | return idAuxInputs_; |
---|
| 1135 | } |
---|
[1646] | 1136 | CATCH |
---|
[827] | 1137 | |
---|
[933] | 1138 | CGenericAlgorithmTransformation::AlgoTransType CGenericAlgorithmTransformation::type() |
---|
[1646] | 1139 | TRY |
---|
[933] | 1140 | { |
---|
| 1141 | return type_; |
---|
[827] | 1142 | } |
---|
[1646] | 1143 | CATCH |
---|
[933] | 1144 | |
---|
| 1145 | } |
---|