[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" |
---|
[620] | 14 | |
---|
| 15 | namespace xios { |
---|
| 16 | |
---|
| 17 | CGenericAlgorithmTransformation::CGenericAlgorithmTransformation() |
---|
[933] | 18 | : transformationMapping_(), transformationWeight_(), transformationPosition_(), |
---|
| 19 | idAuxInputs_(), type_(ELEMENT_NO_MODIFICATION_WITH_DATA) |
---|
[620] | 20 | { |
---|
| 21 | } |
---|
| 22 | |
---|
[979] | 23 | void CGenericAlgorithmTransformation::updateData(CArray<double,1>& dataOut) |
---|
| 24 | { |
---|
| 25 | |
---|
| 26 | } |
---|
| 27 | |
---|
[888] | 28 | void CGenericAlgorithmTransformation::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
| 29 | const double* dataInput, |
---|
| 30 | CArray<double,1>& dataOut, |
---|
[918] | 31 | std::vector<bool>& flagInitial, |
---|
[1158] | 32 | bool ignoreMissingValue) |
---|
[888] | 33 | { |
---|
[1158] | 34 | int nbLocalIndex = localIndex.size(); |
---|
| 35 | double defaultValue = std::numeric_limits<double>::quiet_NaN(); |
---|
| 36 | if (ignoreMissingValue) |
---|
[888] | 37 | { |
---|
[918] | 38 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
| 39 | { |
---|
[1158] | 40 | if (NumTraits<double>::isnan(*(dataInput + idx))) |
---|
[918] | 41 | { |
---|
| 42 | flagInitial[localIndex[idx].first] = false; |
---|
| 43 | } |
---|
| 44 | else |
---|
| 45 | { |
---|
| 46 | dataOut(localIndex[idx].first) += *(dataInput + idx) * localIndex[idx].second; |
---|
[1173] | 47 | flagInitial[localIndex[idx].first] = true; // Reset flag to indicate not all data source are nan |
---|
[918] | 48 | } |
---|
| 49 | } |
---|
| 50 | |
---|
[1173] | 51 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
| 52 | { |
---|
| 53 | if (!flagInitial[localIndex[idx].first]) |
---|
| 54 | dataOut(localIndex[idx].first) = defaultValue; |
---|
| 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 | } |
---|
| 65 | |
---|
| 66 | void CGenericAlgorithmTransformation::computePositionElements(CGrid* dst, CGrid* src) |
---|
| 67 | { |
---|
| 68 | int idxScalar = 0, idxAxis = 0, idxDomain = 0; |
---|
| 69 | CArray<int,1> axisDomainOrderDst = dst->axis_domain_order; |
---|
| 70 | for (int i = 0; i < axisDomainOrderDst.numElements(); ++i) |
---|
| 71 | { |
---|
| 72 | int dimElement = axisDomainOrderDst(i); |
---|
| 73 | if (2 == dimElement) |
---|
| 74 | { |
---|
| 75 | elementPositionInGridDst2DomainPosition_[i] = idxDomain; |
---|
| 76 | ++idxDomain; |
---|
| 77 | } |
---|
| 78 | else if (1 == dimElement) |
---|
| 79 | { |
---|
| 80 | elementPositionInGridDst2AxisPosition_[i] = idxAxis; |
---|
| 81 | ++idxAxis; |
---|
| 82 | } |
---|
| 83 | else |
---|
| 84 | { |
---|
| 85 | elementPositionInGridDst2ScalarPosition_[i] = idxScalar; |
---|
| 86 | ++idxScalar; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | idxScalar = idxAxis = idxDomain = 0; |
---|
| 91 | CArray<int,1> axisDomainOrderSrc = src->axis_domain_order; |
---|
| 92 | for (int i = 0; i < axisDomainOrderSrc.numElements(); ++i) |
---|
| 93 | { |
---|
| 94 | int dimElement = axisDomainOrderSrc(i); |
---|
| 95 | if (2 == dimElement) |
---|
| 96 | { |
---|
| 97 | elementPositionInGridSrc2DomainPosition_[i] = idxDomain; |
---|
| 98 | ++idxDomain; |
---|
| 99 | } |
---|
| 100 | else if (1 == dimElement) |
---|
| 101 | { |
---|
| 102 | elementPositionInGridSrc2AxisPosition_[i] = idxAxis; |
---|
| 103 | ++idxAxis; |
---|
| 104 | } |
---|
| 105 | else |
---|
| 106 | { |
---|
| 107 | elementPositionInGridSrc2ScalarPosition_[i] = idxScalar; |
---|
| 108 | ++idxScalar; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
[620] | 113 | /*! |
---|
| 114 | This function computes the global indexes of grid source, which the grid destination is in demand. |
---|
| 115 | \param[in] elementPositionInGrid position of an element in a grid .E.g: if grid is composed of domain and axis (in order), |
---|
[862] | 116 | then position of axis in grid is 1 and domain is positioned at 0. |
---|
| 117 | \param[in] gridSrc Grid source |
---|
| 118 | \param[in] gridDst Grid destination |
---|
[867] | 119 | \param[in\out] globaIndexWeightFromSrcToDst mapping of each global index source and weight to index destination |
---|
[862] | 120 | */ |
---|
[620] | 121 | void CGenericAlgorithmTransformation::computeGlobalSourceIndex(int elementPositionInGrid, |
---|
[862] | 122 | CGrid* gridSrc, |
---|
| 123 | CGrid* gridDst, |
---|
| 124 | SourceDestinationIndexMap& globaIndexWeightFromSrcToDst) |
---|
| 125 | { |
---|
| 126 | CContext* context = CContext::getCurrent(); |
---|
| 127 | CContextClient* client = context->client; |
---|
| 128 | int nbClient = client->clientSize; |
---|
| 129 | |
---|
| 130 | typedef boost::unordered_map<int, std::vector<std::pair<int,double> > > SrcToDstMap; |
---|
[866] | 131 | |
---|
| 132 | size_t indexSrcSize = 0; |
---|
[827] | 133 | for (size_t idxTrans = 0; idxTrans < transformationMapping_.size(); ++idxTrans) |
---|
[620] | 134 | { |
---|
[833] | 135 | TransformationIndexMap::const_iterator itbTransMap = transformationMapping_[idxTrans].begin(), itTransMap, |
---|
[862] | 136 | iteTransMap = transformationMapping_[idxTrans].end(); |
---|
| 137 | TransformationWeightMap::const_iterator itbTransWeight = transformationWeight_[idxTrans].begin(), itTransWeight; |
---|
[827] | 138 | |
---|
[862] | 139 | itTransWeight = itbTransWeight; |
---|
| 140 | for (itTransMap = itbTransMap; itTransMap != iteTransMap; ++itTransMap, ++itTransWeight) |
---|
[620] | 141 | { |
---|
[862] | 142 | indexSrcSize += (itTransMap->second).size(); |
---|
[827] | 143 | } |
---|
[866] | 144 | } |
---|
[827] | 145 | |
---|
[866] | 146 | bool isTransPosEmpty = transformationPosition_.empty(); |
---|
| 147 | CArray<size_t,1> transPos; |
---|
| 148 | if (!isTransPosEmpty) transPos.resize(transformationMapping_.size()); |
---|
| 149 | CArray<size_t,1> indexSrc(indexSrcSize); |
---|
| 150 | indexSrcSize = 0; |
---|
| 151 | for (size_t idxTrans = 0; idxTrans < transformationMapping_.size(); ++idxTrans) |
---|
| 152 | { |
---|
| 153 | TransformationIndexMap::const_iterator itbTransMap = transformationMapping_[idxTrans].begin(), itTransMap, |
---|
| 154 | iteTransMap = transformationMapping_[idxTrans].end(); |
---|
| 155 | TransformationWeightMap::const_iterator itbTransWeight = transformationWeight_[idxTrans].begin(), itTransWeight; |
---|
| 156 | |
---|
[862] | 157 | // Build mapping between global source element index and global destination element index. |
---|
| 158 | itTransWeight = itbTransWeight; |
---|
[827] | 159 | for (itTransMap = itbTransMap; itTransMap != iteTransMap; ++itTransMap, ++itTransWeight) |
---|
| 160 | { |
---|
[862] | 161 | const std::vector<int>& srcIndex = itTransMap->second; |
---|
| 162 | for (int idx = 0; idx < srcIndex.size(); ++idx) |
---|
[620] | 163 | { |
---|
[866] | 164 | indexSrc(indexSrcSize) = srcIndex[idx]; |
---|
| 165 | ++indexSrcSize; |
---|
[620] | 166 | } |
---|
[862] | 167 | } |
---|
| 168 | |
---|
| 169 | if (!isTransPosEmpty) |
---|
| 170 | { |
---|
[866] | 171 | TransformationPositionMap::const_iterator itPosMap = transformationPosition_[idxTrans].begin(); |
---|
| 172 | transPos(idxTrans) = itPosMap->second[0]; |
---|
[862] | 173 | } |
---|
[866] | 174 | } |
---|
| 175 | |
---|
[888] | 176 | // compute position of elements on grids |
---|
| 177 | computePositionElements(gridDst, gridSrc); |
---|
| 178 | std::vector<CScalar*> scalarListDestP = gridDst->getScalars(); |
---|
[866] | 179 | std::vector<CAxis*> axisListDestP = gridDst->getAxis(); |
---|
| 180 | std::vector<CDomain*> domainListDestP = gridDst->getDomains(); |
---|
[887] | 181 | CArray<int,1> axisDomainDstOrder = gridDst->axis_domain_order; |
---|
[888] | 182 | std::vector<CScalar*> scalarListSrcP = gridSrc->getScalars(); |
---|
[866] | 183 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
| 184 | std::vector<CDomain*> domainListSrcP = gridSrc->getDomains(); |
---|
[888] | 185 | CArray<int,1> axisDomainSrcOrder = gridSrc->axis_domain_order; |
---|
[866] | 186 | |
---|
| 187 | // Find out global index source of transformed element on corresponding process. |
---|
| 188 | std::vector<boost::unordered_map<int,std::vector<size_t> > > globalElementIndexOnProc(axisDomainDstOrder.numElements()); |
---|
[977] | 189 | CClientClientDHTInt::Index2VectorInfoTypeMap globalIndexOfTransformedElementOnProc; |
---|
[866] | 190 | for (int idx = 0; idx < axisDomainDstOrder.numElements(); ++idx) |
---|
| 191 | { |
---|
| 192 | if (idx == elementPositionInGrid) |
---|
[888] | 193 | computeExchangeGlobalIndex(indexSrc, axisDomainSrcOrder(idx), globalIndexOfTransformedElementOnProc); //globalElementIndexOnProc[idx]); |
---|
[887] | 194 | if (2 == axisDomainDstOrder(idx)) // It's domain |
---|
[862] | 195 | { |
---|
[866] | 196 | if (idx != elementPositionInGrid) |
---|
[888] | 197 | computeExchangeDomainIndex(domainListDestP[elementPositionInGridDst2DomainPosition_[idx]], |
---|
| 198 | domainListSrcP[elementPositionInGridSrc2DomainPosition_[idx]], |
---|
[866] | 199 | transPos, |
---|
[977] | 200 | globalElementIndexOnProc[idx]); |
---|
[866] | 201 | |
---|
| 202 | } |
---|
[887] | 203 | else if (1 == axisDomainDstOrder(idx))//it's an axis |
---|
[866] | 204 | { |
---|
| 205 | if (idx != elementPositionInGrid) |
---|
[888] | 206 | computeExchangeAxisIndex(axisListDestP[elementPositionInGridDst2AxisPosition_[idx]], |
---|
| 207 | axisListSrcP[elementPositionInGridSrc2AxisPosition_[idx]], |
---|
[866] | 208 | transPos, |
---|
| 209 | globalElementIndexOnProc[idx]); |
---|
| 210 | } |
---|
[888] | 211 | else //it's a scalar |
---|
| 212 | { |
---|
| 213 | if (idx != elementPositionInGrid) |
---|
| 214 | computeExchangeScalarIndex(scalarListDestP[elementPositionInGridDst2ScalarPosition_[idx]], |
---|
| 215 | scalarListSrcP[elementPositionInGridSrc2ScalarPosition_[idx]], |
---|
| 216 | transPos, |
---|
| 217 | globalElementIndexOnProc[idx]); |
---|
| 218 | |
---|
| 219 | } |
---|
[866] | 220 | } |
---|
| 221 | |
---|
| 222 | if (!isTransPosEmpty) |
---|
| 223 | { |
---|
| 224 | for (int idx = 0; idx < globalElementIndexOnProc.size(); ++idx) |
---|
| 225 | { |
---|
| 226 | if (idx != elementPositionInGrid) |
---|
[827] | 227 | { |
---|
[866] | 228 | boost::unordered_map<int,std::vector<size_t> >::iterator itb = globalElementIndexOnProc[idx].begin(), it, |
---|
| 229 | ite = globalElementIndexOnProc[idx].end(); |
---|
| 230 | for (it = itb; it != ite; ++it) it->second.resize(1); |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | } |
---|
[862] | 234 | |
---|
[866] | 235 | for (size_t idxTrans = 0; idxTrans < transformationMapping_.size(); ++idxTrans) |
---|
| 236 | { |
---|
| 237 | TransformationIndexMap::const_iterator itbTransMap = transformationMapping_[idxTrans].begin(), itTransMap, |
---|
| 238 | iteTransMap = transformationMapping_[idxTrans].end(); |
---|
| 239 | TransformationWeightMap::const_iterator itbTransWeight = transformationWeight_[idxTrans].begin(), itTransWeight; |
---|
| 240 | SrcToDstMap src2DstMap; |
---|
| 241 | src2DstMap.rehash(std::ceil(transformationMapping_[idxTrans].size()/src2DstMap.max_load_factor())); |
---|
| 242 | |
---|
| 243 | // Build mapping between global source element index and global destination element index. |
---|
| 244 | boost::unordered_map<int,std::vector<size_t> >().swap(globalElementIndexOnProc[elementPositionInGrid]); |
---|
| 245 | boost::unordered_map<int,int> tmpCounter; |
---|
| 246 | itTransWeight = itbTransWeight; |
---|
| 247 | for (itTransMap = itbTransMap; itTransMap != iteTransMap; ++itTransMap, ++itTransWeight) |
---|
| 248 | { |
---|
| 249 | const std::vector<int>& srcIndex = itTransMap->second; |
---|
| 250 | const std::vector<double>& weight = itTransWeight->second; |
---|
| 251 | for (int idx = 0; idx < srcIndex.size(); ++idx) |
---|
| 252 | { |
---|
| 253 | src2DstMap[srcIndex[idx]].push_back(make_pair(itTransMap->first, weight[idx])); |
---|
| 254 | if (1 == globalIndexOfTransformedElementOnProc.count(srcIndex[idx]) && (0 == tmpCounter.count(srcIndex[idx]))) |
---|
| 255 | { |
---|
| 256 | tmpCounter[srcIndex[idx]] = 1; |
---|
| 257 | std::vector<int>& srcProc = globalIndexOfTransformedElementOnProc[srcIndex[idx]]; |
---|
| 258 | for (int j = 0; j < srcProc.size(); ++j) |
---|
| 259 | globalElementIndexOnProc[elementPositionInGrid][srcProc[j]].push_back(srcIndex[idx]); |
---|
| 260 | } |
---|
[827] | 261 | } |
---|
[866] | 262 | } |
---|
| 263 | |
---|
| 264 | if (!isTransPosEmpty) |
---|
| 265 | { |
---|
| 266 | for (int idx = 0; idx < globalElementIndexOnProc.size(); ++idx) |
---|
[862] | 267 | { |
---|
| 268 | if (idx != elementPositionInGrid) |
---|
[866] | 269 | { |
---|
| 270 | boost::unordered_map<int,std::vector<size_t> >::iterator itb = globalElementIndexOnProc[idx].begin(), it, |
---|
| 271 | ite = globalElementIndexOnProc[idx].end(); |
---|
| 272 | for (it = itb; it != ite; ++it) it->second[0] = transPos(idxTrans); |
---|
| 273 | } |
---|
[862] | 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | std::vector<std::vector<bool> > elementOnProc(axisDomainDstOrder.numElements(), std::vector<bool>(nbClient, false)); |
---|
| 278 | boost::unordered_map<int,std::vector<size_t> >::const_iterator it, itb, ite; |
---|
| 279 | for (int idx = 0; idx < globalElementIndexOnProc.size(); ++idx) |
---|
| 280 | { |
---|
| 281 | itb = globalElementIndexOnProc[idx].begin(); |
---|
| 282 | ite = globalElementIndexOnProc[idx].end(); |
---|
| 283 | for (it = itb; it != ite; ++it) elementOnProc[idx][it->first] = true; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | // Determine procs which contain global source index |
---|
| 287 | std::vector<bool> intersectedProc(nbClient, true); |
---|
| 288 | for (int idx = 0; idx < axisDomainDstOrder.numElements(); ++idx) |
---|
| 289 | { |
---|
| 290 | std::transform(elementOnProc[idx].begin(), elementOnProc[idx].end(), |
---|
| 291 | intersectedProc.begin(), intersectedProc.begin(), |
---|
| 292 | std::logical_and<bool>()); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | std::vector<int> srcRank; |
---|
| 296 | for (int idx = 0; idx < nbClient; ++idx) |
---|
| 297 | { |
---|
| 298 | if (intersectedProc[idx]) srcRank.push_back(idx); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | // Ok, now compute global index of grid source and ones of grid destination |
---|
| 302 | computeGlobalGridIndexMapping(elementPositionInGrid, |
---|
| 303 | srcRank, |
---|
| 304 | src2DstMap, |
---|
[871] | 305 | gridSrc, |
---|
[862] | 306 | gridDst, |
---|
| 307 | globalElementIndexOnProc, |
---|
| 308 | globaIndexWeightFromSrcToDst); |
---|
| 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | /*! |
---|
| 313 | Compute mapping of global index of grid source and grid destination |
---|
| 314 | \param [in] elementPositionInGrid position of element in grid. E.x: grid composed of domain and axis, domain has position 0 and axis 1. |
---|
| 315 | \param [in] srcRank rank of client from which we demand global index of element source |
---|
| 316 | \param [in] src2DstMap mapping of global index of element source and global index of element destination |
---|
| 317 | \param[in] gridSrc Grid source |
---|
| 318 | \param[in] gridDst Grid destination |
---|
| 319 | \param[in] globalElementIndexOnProc Global index of element source on different client rank |
---|
| 320 | \param[out] globaIndexWeightFromSrcToDst Mapping of global index of grid source and grid destination |
---|
| 321 | */ |
---|
| 322 | void CGenericAlgorithmTransformation::computeGlobalGridIndexMapping(int elementPositionInGrid, |
---|
| 323 | const std::vector<int>& srcRank, |
---|
| 324 | boost::unordered_map<int, std::vector<std::pair<int,double> > >& src2DstMap, |
---|
| 325 | CGrid* gridSrc, |
---|
| 326 | CGrid* gridDst, |
---|
| 327 | std::vector<boost::unordered_map<int,std::vector<size_t> > >& globalElementIndexOnProc, |
---|
| 328 | SourceDestinationIndexMap& globaIndexWeightFromSrcToDst) |
---|
| 329 | { |
---|
[887] | 330 | std::vector<CDomain*> domainListSrcP = gridSrc->getDomains(); |
---|
[862] | 331 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
[887] | 332 | std::vector<CScalar*> scalarListSrcP = gridSrc->getScalars(); |
---|
| 333 | CArray<int,1> axisDomainSrcOrder = gridSrc->axis_domain_order; |
---|
| 334 | |
---|
[862] | 335 | size_t nbElement = axisDomainSrcOrder.numElements(); |
---|
[887] | 336 | std::vector<size_t> nGlobSrc(nbElement); |
---|
| 337 | size_t globalSrcSize = 1; |
---|
| 338 | int domainIndex = 0, axisIndex = 0, scalarIndex = 0; |
---|
[862] | 339 | for (int idx = 0; idx < nbElement; ++idx) |
---|
| 340 | { |
---|
| 341 | nGlobSrc[idx] = globalSrcSize; |
---|
[887] | 342 | int elementDimension = axisDomainSrcOrder(idx); |
---|
| 343 | |
---|
| 344 | // If this is a domain |
---|
| 345 | if (2 == elementDimension) |
---|
| 346 | { |
---|
| 347 | globalSrcSize *= domainListSrcP[domainIndex]->nj_glo.getValue() * domainListSrcP[domainIndex]->ni_glo.getValue(); |
---|
| 348 | ++domainIndex; |
---|
| 349 | } |
---|
| 350 | else if (1 == elementDimension) // So it's an axis |
---|
| 351 | { |
---|
| 352 | globalSrcSize *= axisListSrcP[axisIndex]->n_glo.getValue(); |
---|
| 353 | ++axisIndex; |
---|
| 354 | } |
---|
| 355 | else |
---|
| 356 | { |
---|
| 357 | globalSrcSize *= 1; |
---|
| 358 | ++scalarIndex; |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | std::vector<CDomain*> domainListDestP = gridDst->getDomains(); |
---|
| 363 | std::vector<CAxis*> axisListDestP = gridDst->getAxis(); |
---|
| 364 | std::vector<CScalar*> scalarListDestP = gridDst->getScalars(); |
---|
| 365 | CArray<int,1> axisDomainDstOrder = gridDst->axis_domain_order; |
---|
| 366 | |
---|
| 367 | std::vector<size_t> nGlobDst(nbElement); |
---|
| 368 | size_t globalDstSize = 1; |
---|
| 369 | domainIndex = axisIndex = scalarIndex = 0; |
---|
| 370 | for (int idx = 0; idx < nbElement; ++idx) |
---|
| 371 | { |
---|
[862] | 372 | nGlobDst[idx] = globalDstSize; |
---|
[888] | 373 | int elementDimension = axisDomainDstOrder(idx); |
---|
[862] | 374 | |
---|
| 375 | // If this is a domain |
---|
[887] | 376 | if (2 == elementDimension) |
---|
[862] | 377 | { |
---|
| 378 | globalDstSize *= domainListDestP[domainIndex]->nj_glo.getValue() * domainListDestP[domainIndex]->ni_glo.getValue(); |
---|
| 379 | ++domainIndex; |
---|
| 380 | } |
---|
[887] | 381 | else if (1 == elementDimension) // So it's an axis |
---|
[862] | 382 | { |
---|
| 383 | globalDstSize *= axisListDestP[axisIndex]->n_glo.getValue(); |
---|
| 384 | ++axisIndex; |
---|
| 385 | } |
---|
[887] | 386 | else |
---|
| 387 | { |
---|
| 388 | globalDstSize *= 1; |
---|
| 389 | ++scalarIndex; |
---|
| 390 | } |
---|
[862] | 391 | } |
---|
| 392 | |
---|
| 393 | for (int i = 0; i < srcRank.size(); ++i) |
---|
| 394 | { |
---|
| 395 | size_t ssize = 1; |
---|
| 396 | int rankSrc = srcRank[i]; |
---|
| 397 | for (int idx = 0; idx < nbElement; ++idx) |
---|
| 398 | { |
---|
| 399 | ssize *= (globalElementIndexOnProc[idx][rankSrc]).size(); |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | std::vector<int> idxLoop(nbElement,0); |
---|
| 403 | std::vector<int> currentIndexSrc(nbElement, 0); |
---|
| 404 | std::vector<int> currentIndexDst(nbElement, 0); |
---|
| 405 | int innnerLoopSize = (globalElementIndexOnProc[0])[rankSrc].size(); |
---|
| 406 | size_t idx = 0; |
---|
| 407 | while (idx < ssize) |
---|
| 408 | { |
---|
| 409 | for (int ind = 0; ind < nbElement; ++ind) |
---|
[827] | 410 | { |
---|
[862] | 411 | if (idxLoop[ind] == (globalElementIndexOnProc[ind])[rankSrc].size()) |
---|
[827] | 412 | { |
---|
[862] | 413 | idxLoop[ind] = 0; |
---|
| 414 | ++idxLoop[ind+1]; |
---|
[827] | 415 | } |
---|
[862] | 416 | |
---|
| 417 | currentIndexDst[ind] = currentIndexSrc[ind] = (globalElementIndexOnProc[ind])[rankSrc][idxLoop[ind]]; |
---|
[827] | 418 | } |
---|
[862] | 419 | |
---|
| 420 | for (int ind = 0; ind < innnerLoopSize; ++ind) |
---|
| 421 | { |
---|
[868] | 422 | currentIndexDst[0] = currentIndexSrc[0] = (globalElementIndexOnProc[0])[rankSrc][ind]; |
---|
[862] | 423 | int globalElementDstIndexSize = 0; |
---|
| 424 | if (1 == src2DstMap.count(currentIndexSrc[elementPositionInGrid])) |
---|
| 425 | { |
---|
| 426 | globalElementDstIndexSize = src2DstMap[currentIndexSrc[elementPositionInGrid]].size(); |
---|
| 427 | } |
---|
[868] | 428 | |
---|
[862] | 429 | std::vector<size_t> globalDstVecIndex(globalElementDstIndexSize,0); |
---|
| 430 | size_t globalSrcIndex = 0; |
---|
| 431 | for (int idxElement = 0; idxElement < nbElement; ++idxElement) |
---|
| 432 | { |
---|
| 433 | if (idxElement == elementPositionInGrid) |
---|
| 434 | { |
---|
| 435 | for (int k = 0; k < globalElementDstIndexSize; ++k) |
---|
| 436 | { |
---|
| 437 | globalDstVecIndex[k] += src2DstMap[currentIndexSrc[elementPositionInGrid]][k].first * nGlobDst[idxElement]; |
---|
| 438 | } |
---|
| 439 | } |
---|
| 440 | else |
---|
| 441 | { |
---|
| 442 | for (int k = 0; k < globalElementDstIndexSize; ++k) |
---|
| 443 | { |
---|
| 444 | globalDstVecIndex[k] += currentIndexDst[idxElement] * nGlobDst[idxElement]; |
---|
| 445 | } |
---|
| 446 | } |
---|
| 447 | globalSrcIndex += currentIndexSrc[idxElement] * nGlobSrc[idxElement]; |
---|
| 448 | } |
---|
| 449 | |
---|
| 450 | for (int k = 0; k < globalElementDstIndexSize; ++k) |
---|
| 451 | { |
---|
| 452 | globaIndexWeightFromSrcToDst[rankSrc][globalSrcIndex].push_back(make_pair(globalDstVecIndex[k],src2DstMap[currentIndexSrc[elementPositionInGrid]][k].second )); |
---|
| 453 | } |
---|
| 454 | ++idxLoop[0]; |
---|
| 455 | } |
---|
| 456 | idx += innnerLoopSize; |
---|
[620] | 457 | } |
---|
| 458 | } |
---|
| 459 | } |
---|
| 460 | |
---|
[862] | 461 | /*! |
---|
| 462 | Find out proc and global index of axis source which axis destination is on demande |
---|
[888] | 463 | \param[in] scalar Scalar destination |
---|
| 464 | \param[in] scalar Scalar source |
---|
| 465 | \param[in] destGlobalIndexPositionInGrid Relative position of axis corresponds to other element of grid. |
---|
| 466 | \param[out] globalScalarIndexOnProc Global index of axis source on different procs |
---|
| 467 | */ |
---|
| 468 | void CGenericAlgorithmTransformation::computeExchangeScalarIndex(CScalar* scalarDst, |
---|
| 469 | CScalar* scalarSrc, |
---|
| 470 | CArray<size_t,1>& destGlobalIndexPositionInGrid, |
---|
| 471 | boost::unordered_map<int,std::vector<size_t> >& globalScalarIndexOnProc) |
---|
| 472 | { |
---|
| 473 | CContext* context = CContext::getCurrent(); |
---|
| 474 | CContextClient* client=context->client; |
---|
| 475 | int clientRank = client->clientRank; |
---|
| 476 | int clientSize = client->clientSize; |
---|
| 477 | |
---|
| 478 | globalScalarIndexOnProc.rehash(std::ceil(clientSize/globalScalarIndexOnProc.max_load_factor())); |
---|
| 479 | for (int idx = 0; idx < clientSize; ++idx) |
---|
| 480 | { |
---|
| 481 | globalScalarIndexOnProc[idx].push_back(0); |
---|
| 482 | } |
---|
| 483 | } |
---|
| 484 | |
---|
| 485 | /*! |
---|
| 486 | Find out proc and global index of axis source which axis destination is on demande |
---|
[862] | 487 | \param[in] axisDst Axis destination |
---|
| 488 | \param[in] axisSrc Axis source |
---|
| 489 | \param[in] destGlobalIndexPositionInGrid Relative position of axis corresponds to other element of grid. |
---|
| 490 | \param[out] globalAxisIndexOnProc Global index of axis source on different procs |
---|
| 491 | */ |
---|
| 492 | void CGenericAlgorithmTransformation::computeExchangeAxisIndex(CAxis* axisDst, |
---|
| 493 | CAxis* axisSrc, |
---|
| 494 | CArray<size_t,1>& destGlobalIndexPositionInGrid, |
---|
| 495 | boost::unordered_map<int,std::vector<size_t> >& globalAxisIndexOnProc) |
---|
| 496 | { |
---|
| 497 | CContext* context = CContext::getCurrent(); |
---|
| 498 | CContextClient* client=context->client; |
---|
| 499 | int clientRank = client->clientRank; |
---|
| 500 | int clientSize = client->clientSize; |
---|
| 501 | |
---|
| 502 | size_t globalIndex; |
---|
| 503 | int nIndexSize = axisSrc->index.numElements(); |
---|
| 504 | CClientClientDHTInt::Index2VectorInfoTypeMap globalIndex2ProcRank; |
---|
| 505 | globalIndex2ProcRank.rehash(std::ceil(nIndexSize/globalIndex2ProcRank.max_load_factor())); |
---|
| 506 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
| 507 | { |
---|
| 508 | globalIndex = axisSrc->index(idx); |
---|
| 509 | globalIndex2ProcRank[globalIndex].push_back(clientRank); |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | CClientClientDHTInt dhtIndexProcRank(globalIndex2ProcRank, client->intraComm); |
---|
| 513 | CArray<size_t,1> globalAxisIndex(axisDst->index.numElements()); |
---|
| 514 | for (int idx = 0; idx < globalAxisIndex.numElements(); ++idx) |
---|
| 515 | { |
---|
| 516 | globalAxisIndex(idx) = axisDst->index(idx); |
---|
| 517 | } |
---|
| 518 | dhtIndexProcRank.computeIndexInfoMapping(globalAxisIndex); |
---|
| 519 | |
---|
| 520 | std::vector<int> countIndex(clientSize,0); |
---|
| 521 | const CClientClientDHTInt::Index2VectorInfoTypeMap& computedGlobalIndexOnProc = dhtIndexProcRank.getInfoIndexMap(); |
---|
| 522 | CClientClientDHTInt::Index2VectorInfoTypeMap::const_iterator itb = computedGlobalIndexOnProc.begin(), it, |
---|
| 523 | ite = computedGlobalIndexOnProc.end(); |
---|
| 524 | for (it = itb; it != ite; ++it) |
---|
| 525 | { |
---|
| 526 | const std::vector<int>& procList = it->second; |
---|
| 527 | for (int idx = 0; idx < procList.size(); ++idx) ++countIndex[procList[idx]]; |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | globalAxisIndexOnProc.rehash(std::ceil(clientSize/globalAxisIndexOnProc.max_load_factor())); |
---|
| 531 | for (int idx = 0; idx < clientSize; ++idx) |
---|
| 532 | { |
---|
| 533 | if (0 != countIndex[idx]) |
---|
| 534 | { |
---|
| 535 | globalAxisIndexOnProc[idx].resize(countIndex[idx]); |
---|
| 536 | countIndex[idx] = 0; |
---|
| 537 | } |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | for (it = itb; it != ite; ++it) |
---|
| 541 | { |
---|
| 542 | const std::vector<int>& procList = it->second; |
---|
| 543 | for (int idx = 0; idx < procList.size(); ++idx) |
---|
| 544 | { |
---|
| 545 | globalAxisIndexOnProc[procList[idx]][countIndex[procList[idx]]] = it->first; |
---|
| 546 | ++countIndex[procList[idx]]; |
---|
| 547 | } |
---|
| 548 | } |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | /*! |
---|
| 552 | Find out proc and global index of domain source which domain destination is on demande |
---|
| 553 | \param[in] domainDst Domain destination |
---|
| 554 | \param[in] domainSrc Domain source |
---|
| 555 | \param[in] destGlobalIndexPositionInGrid Relative position of domain corresponds to other element of grid. |
---|
| 556 | \param[out] globalDomainIndexOnProc Global index of domain source on different procs |
---|
| 557 | */ |
---|
| 558 | void CGenericAlgorithmTransformation::computeExchangeDomainIndex(CDomain* domainDst, |
---|
| 559 | CDomain* domainSrc, |
---|
| 560 | CArray<size_t,1>& destGlobalIndexPositionInGrid, |
---|
| 561 | boost::unordered_map<int,std::vector<size_t> >& globalDomainIndexOnProc) |
---|
| 562 | { |
---|
| 563 | CContext* context = CContext::getCurrent(); |
---|
| 564 | CContextClient* client=context->client; |
---|
| 565 | int clientRank = client->clientRank; |
---|
| 566 | int clientSize = client->clientSize; |
---|
| 567 | |
---|
[866] | 568 | int niGlobSrc = domainSrc->ni_glo.getValue(); |
---|
[862] | 569 | size_t globalIndex; |
---|
[866] | 570 | int i_ind, j_ind; |
---|
| 571 | int nIndexSize = (destGlobalIndexPositionInGrid.isEmpty()) ? domainSrc->i_index.numElements() |
---|
| 572 | : destGlobalIndexPositionInGrid.numElements(); |
---|
[862] | 573 | CClientClientDHTInt::Index2VectorInfoTypeMap globalIndex2ProcRank; |
---|
| 574 | globalIndex2ProcRank.rehash(std::ceil(nIndexSize/globalIndex2ProcRank.max_load_factor())); |
---|
[866] | 575 | if (destGlobalIndexPositionInGrid.isEmpty()) |
---|
[862] | 576 | { |
---|
[866] | 577 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
| 578 | { |
---|
| 579 | i_ind=domainSrc->i_index(idx) ; |
---|
| 580 | j_ind=domainSrc->j_index(idx) ; |
---|
[862] | 581 | |
---|
[866] | 582 | globalIndex = i_ind + j_ind * niGlobSrc; |
---|
| 583 | globalIndex2ProcRank[globalIndex].resize(1); |
---|
| 584 | globalIndex2ProcRank[globalIndex][0] = clientRank; |
---|
| 585 | } |
---|
[862] | 586 | } |
---|
[866] | 587 | else |
---|
| 588 | { |
---|
| 589 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
| 590 | { |
---|
| 591 | globalIndex2ProcRank[destGlobalIndexPositionInGrid(idx)].push_back(clientRank); |
---|
| 592 | } |
---|
| 593 | } |
---|
[862] | 594 | |
---|
| 595 | CArray<size_t,1> globalDomainIndex; |
---|
| 596 | if (destGlobalIndexPositionInGrid.isEmpty()) |
---|
| 597 | { |
---|
[866] | 598 | int niGlobDst = domainDst->ni_glo.getValue(); |
---|
[862] | 599 | globalDomainIndex.resize(domainDst->i_index.numElements()); |
---|
| 600 | nIndexSize = domainDst->i_index.numElements(); |
---|
| 601 | |
---|
| 602 | for (int idx = 0; idx < nIndexSize; ++idx) |
---|
| 603 | { |
---|
| 604 | i_ind=domainDst->i_index(idx) ; |
---|
| 605 | j_ind=domainDst->j_index(idx) ; |
---|
| 606 | |
---|
[866] | 607 | globalDomainIndex(idx) = i_ind + j_ind * niGlobDst; |
---|
[862] | 608 | } |
---|
| 609 | } |
---|
| 610 | else |
---|
| 611 | { |
---|
[866] | 612 | globalDomainIndex.reference(destGlobalIndexPositionInGrid); |
---|
[862] | 613 | } |
---|
| 614 | |
---|
| 615 | CClientClientDHTInt dhtIndexProcRank(globalIndex2ProcRank, client->intraComm); |
---|
| 616 | dhtIndexProcRank.computeIndexInfoMapping(globalDomainIndex); |
---|
| 617 | |
---|
| 618 | std::vector<int> countIndex(clientSize,0); |
---|
| 619 | const CClientClientDHTInt::Index2VectorInfoTypeMap& computedGlobalIndexOnProc = dhtIndexProcRank.getInfoIndexMap(); |
---|
| 620 | CClientClientDHTInt::Index2VectorInfoTypeMap::const_iterator itb = computedGlobalIndexOnProc.begin(), it, |
---|
| 621 | ite = computedGlobalIndexOnProc.end(); |
---|
| 622 | for (it = itb; it != ite; ++it) |
---|
| 623 | { |
---|
| 624 | const std::vector<int>& procList = it->second; |
---|
| 625 | for (int idx = 0; idx < procList.size(); ++idx) ++countIndex[procList[idx]]; |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | globalDomainIndexOnProc.rehash(std::ceil(clientSize/globalDomainIndexOnProc.max_load_factor())); |
---|
| 629 | for (int idx = 0; idx < clientSize; ++idx) |
---|
| 630 | { |
---|
| 631 | if (0 != countIndex[idx]) |
---|
| 632 | { |
---|
| 633 | globalDomainIndexOnProc[idx].resize(countIndex[idx]); |
---|
| 634 | countIndex[idx] = 0; |
---|
| 635 | } |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | for (it = itb; it != ite; ++it) |
---|
| 639 | { |
---|
| 640 | const std::vector<int>& procList = it->second; |
---|
| 641 | for (int idx = 0; idx < procList.size(); ++idx) |
---|
| 642 | { |
---|
| 643 | globalDomainIndexOnProc[procList[idx]][countIndex[procList[idx]]] = it->first; |
---|
| 644 | ++countIndex[procList[idx]]; |
---|
| 645 | } |
---|
| 646 | } |
---|
| 647 | } |
---|
| 648 | |
---|
| 649 | /*! |
---|
| 650 | Compute index mapping between element source and element destination with an auxiliary inputs which determine |
---|
| 651 | position of each mapped index in global index of grid destination. |
---|
| 652 | \param [in] dataAuxInputs auxiliary inputs |
---|
| 653 | */ |
---|
[827] | 654 | void CGenericAlgorithmTransformation::computeIndexSourceMapping(const std::vector<CArray<double,1>* >& dataAuxInputs) |
---|
| 655 | { |
---|
| 656 | computeIndexSourceMapping_(dataAuxInputs); |
---|
[620] | 657 | } |
---|
[827] | 658 | |
---|
| 659 | std::vector<StdString> CGenericAlgorithmTransformation::getIdAuxInputs() |
---|
| 660 | { |
---|
| 661 | return idAuxInputs_; |
---|
| 662 | } |
---|
| 663 | |
---|
[933] | 664 | CGenericAlgorithmTransformation::AlgoTransType CGenericAlgorithmTransformation::type() |
---|
| 665 | { |
---|
| 666 | return type_; |
---|
[827] | 667 | } |
---|
[933] | 668 | |
---|
| 669 | } |
---|