[623] | 1 | /*! |
---|
| 2 | \file grid_transformation.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 14 May 2015 |
---|
[630] | 5 | \date 02 Jul 2015 |
---|
[623] | 6 | |
---|
| 7 | \brief Interface for all transformations. |
---|
| 8 | */ |
---|
[620] | 9 | #include "grid_transformation.hpp" |
---|
[933] | 10 | #include "grid_transformation_factory_impl.hpp" |
---|
[895] | 11 | #include "algo_types.hpp" |
---|
[622] | 12 | #include "context.hpp" |
---|
| 13 | #include "context_client.hpp" |
---|
[660] | 14 | #include "distribution_client.hpp" |
---|
[821] | 15 | #include "mpi_tag.hpp" |
---|
[867] | 16 | #include "grid.hpp" |
---|
[1542] | 17 | #include <unordered_map> |
---|
[1399] | 18 | #include "timer.hpp" |
---|
[620] | 19 | |
---|
| 20 | namespace xios { |
---|
| 21 | CGridTransformation::CGridTransformation(CGrid* destination, CGrid* source) |
---|
[887] | 22 | : CGridTransformationSelector(destination, source), |
---|
| 23 | tmpGridDestination_(destination), originalGridSource_(source), |
---|
| 24 | tempGridSrcs_(), tempGridDests_(), |
---|
| 25 | dynamicalTransformation_(false), timeStamp_() |
---|
[620] | 26 | { |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | CGridTransformation::~CGridTransformation() |
---|
| 30 | { |
---|
| 31 | } |
---|
| 32 | |
---|
[624] | 33 | /*! |
---|
[933] | 34 | Select algorithm of a scalar corresponding to its transformation type and its position in each element |
---|
[887] | 35 | \param [in] elementPositionInGrid position of element in grid. e.g: a grid has 1 domain and 1 axis, then position of domain is 0 and position of axis is 1 |
---|
| 36 | \param [in] transType transformation type, for now we have |
---|
| 37 | \param [in] transformationOrder position of the transformation in an element (an element can have several transformation) |
---|
[624] | 38 | */ |
---|
[887] | 39 | void CGridTransformation::selectScalarAlgo(int elementPositionInGrid, ETranformationType transType, int transformationOrder) |
---|
[1622] | 40 | TRY |
---|
[620] | 41 | { |
---|
[888] | 42 | std::vector<CScalar*> scaListDestP = gridDestination_->getScalars(); |
---|
| 43 | int scalarDstIndex = elementPositionInGridDst2ScalarPosition_[elementPositionInGrid]; |
---|
| 44 | CScalar::TransMapTypes trans = scaListDestP[scalarDstIndex]->getAllTransformations(); |
---|
| 45 | CScalar::TransMapTypes::const_iterator it = trans.begin(); |
---|
| 46 | |
---|
| 47 | for (int i = 0; i < transformationOrder; ++i, ++it) {} // Find the correct transformation |
---|
| 48 | CGenericAlgorithmTransformation* algo = 0; |
---|
[933] | 49 | algo = CGridTransformationFactory<CScalar>::createTransformation(transType, |
---|
| 50 | gridDestination_, |
---|
| 51 | gridSource_, |
---|
| 52 | it->second, |
---|
| 53 | elementPositionInGrid, |
---|
| 54 | elementPositionInGridSrc2ScalarPosition_, |
---|
| 55 | elementPositionInGridSrc2AxisPosition_, |
---|
| 56 | elementPositionInGridSrc2DomainPosition_, |
---|
| 57 | elementPositionInGridDst2ScalarPosition_, |
---|
| 58 | elementPositionInGridDst2AxisPosition_, |
---|
| 59 | elementPositionInGridDst2DomainPosition_); |
---|
[888] | 60 | algoTransformation_.push_back(algo); |
---|
[620] | 61 | } |
---|
[1622] | 62 | CATCH |
---|
[620] | 63 | |
---|
| 64 | /*! |
---|
[933] | 65 | Select algorithm of an axis corresponding to its transformation type and its position in each element |
---|
[887] | 66 | \param [in] elementPositionInGrid position of element in grid. e.g: a grid has 1 domain and 1 axis, then position of domain is 0 and position of axis is 1 |
---|
| 67 | \param [in] transType transformation type, for now we have zoom_axis, inverse_axis, interpolate_axis |
---|
[624] | 68 | \param [in] transformationOrder position of the transformation in an element (an element can have several transformation) |
---|
| 69 | */ |
---|
[622] | 70 | void CGridTransformation::selectAxisAlgo(int elementPositionInGrid, ETranformationType transType, int transformationOrder) |
---|
[1622] | 71 | TRY |
---|
[622] | 72 | { |
---|
| 73 | std::vector<CAxis*> axisListDestP = gridDestination_->getAxis(); |
---|
[888] | 74 | int axisDstIndex = elementPositionInGridDst2AxisPosition_[elementPositionInGrid]; |
---|
| 75 | CAxis::TransMapTypes trans = axisListDestP[axisDstIndex]->getAllTransformations(); |
---|
[622] | 76 | CAxis::TransMapTypes::const_iterator it = trans.begin(); |
---|
| 77 | for (int i = 0; i < transformationOrder; ++i, ++it) {} // Find the correct transformation |
---|
| 78 | |
---|
| 79 | CGenericAlgorithmTransformation* algo = 0; |
---|
[933] | 80 | algo = CGridTransformationFactory<CAxis>::createTransformation(transType, |
---|
| 81 | gridDestination_, |
---|
| 82 | gridSource_, |
---|
| 83 | it->second, |
---|
| 84 | elementPositionInGrid, |
---|
| 85 | elementPositionInGridSrc2ScalarPosition_, |
---|
| 86 | elementPositionInGridSrc2AxisPosition_, |
---|
| 87 | elementPositionInGridSrc2DomainPosition_, |
---|
| 88 | elementPositionInGridDst2ScalarPosition_, |
---|
| 89 | elementPositionInGridDst2AxisPosition_, |
---|
| 90 | elementPositionInGridDst2DomainPosition_); |
---|
[622] | 91 | algoTransformation_.push_back(algo); |
---|
| 92 | } |
---|
[1622] | 93 | CATCH |
---|
[622] | 94 | |
---|
[624] | 95 | /*! |
---|
[933] | 96 | Select algorithm of a domain corresponding to its transformation type and its position in each element |
---|
[887] | 97 | \param [in] elementPositionInGrid position of element in grid. e.g: a grid has 1 domain and 1 axis, then position of domain is 0 and position of axis is 1 |
---|
| 98 | \param [in] transType transformation type, for now we have zoom_domain, interpolate_domain |
---|
[624] | 99 | \param [in] transformationOrder position of the transformation in an element (an element can have several transformation) |
---|
| 100 | */ |
---|
[622] | 101 | void CGridTransformation::selectDomainAlgo(int elementPositionInGrid, ETranformationType transType, int transformationOrder) |
---|
[1622] | 102 | TRY |
---|
[622] | 103 | { |
---|
[631] | 104 | std::vector<CDomain*> domainListDestP = gridDestination_->getDomains(); |
---|
[887] | 105 | int domainIndex = elementPositionInGridDst2DomainPosition_[elementPositionInGrid]; |
---|
[631] | 106 | CDomain::TransMapTypes trans = domainListDestP[domainIndex]->getAllTransformations(); |
---|
| 107 | CDomain::TransMapTypes::const_iterator it = trans.begin(); |
---|
[978] | 108 | for (int i = 0; i < transformationOrder; ++i, ++it) {} // Find the correct transformation |
---|
[631] | 109 | |
---|
| 110 | CGenericAlgorithmTransformation* algo = 0; |
---|
[933] | 111 | algo = CGridTransformationFactory<CDomain>::createTransformation(transType, |
---|
| 112 | gridDestination_, |
---|
| 113 | gridSource_, |
---|
| 114 | it->second, |
---|
| 115 | elementPositionInGrid, |
---|
| 116 | elementPositionInGridSrc2ScalarPosition_, |
---|
| 117 | elementPositionInGridSrc2AxisPosition_, |
---|
| 118 | elementPositionInGridSrc2DomainPosition_, |
---|
| 119 | elementPositionInGridDst2ScalarPosition_, |
---|
| 120 | elementPositionInGridDst2AxisPosition_, |
---|
| 121 | elementPositionInGridDst2DomainPosition_); |
---|
[631] | 122 | algoTransformation_.push_back(algo); |
---|
[622] | 123 | } |
---|
[1622] | 124 | CATCH |
---|
[622] | 125 | |
---|
[624] | 126 | /*! |
---|
[978] | 127 | Find position of element in a grid as well as its type (domain, axis, scalar) and position in its own element list |
---|
| 128 | \return element position: map<int,<int,int> > corresponds to <element position in grid, <element type, element position in element list> > |
---|
| 129 | */ |
---|
| 130 | std::map<int,std::pair<int,int> > CGridTransformation::getElementPosition(CGrid* grid) |
---|
[1622] | 131 | TRY |
---|
[978] | 132 | { |
---|
| 133 | std::vector<CScalar*> scalarListP = grid->getScalars(); |
---|
| 134 | std::vector<CAxis*> axisListP = grid->getAxis(); |
---|
| 135 | std::vector<CDomain*> domListP = grid->getDomains(); |
---|
| 136 | CArray<int,1> axisDomainOrder = grid->axis_domain_order; |
---|
| 137 | int scalarIndex = 0, axisIndex = 0, domainIndex = 0; |
---|
| 138 | int nbElement = axisDomainOrder.numElements(), elementDim; |
---|
| 139 | std::map<int,std::pair<int,int> > elementPosition; |
---|
| 140 | for (int idx = 0; idx < nbElement; ++idx) |
---|
| 141 | { |
---|
| 142 | elementDim = axisDomainOrder(idx); |
---|
| 143 | switch (elementDim) |
---|
| 144 | { |
---|
| 145 | case 2: |
---|
| 146 | elementPosition[idx] = std::make_pair(elementDim, domainIndex); |
---|
| 147 | ++domainIndex; |
---|
| 148 | break; |
---|
| 149 | case 1: |
---|
| 150 | elementPosition[idx] = std::make_pair(elementDim, axisIndex); |
---|
| 151 | ++axisIndex; |
---|
| 152 | break; |
---|
| 153 | case 0: |
---|
| 154 | elementPosition[idx] = std::make_pair(elementDim, scalarIndex); |
---|
| 155 | ++scalarIndex; |
---|
| 156 | break; |
---|
| 157 | default: |
---|
| 158 | break; |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | return elementPosition; |
---|
| 163 | } |
---|
[1622] | 164 | CATCH |
---|
[978] | 165 | |
---|
| 166 | /*! |
---|
[871] | 167 | If there are more than one transformation, a new temporary grid will be created and it will play the role of grid destination. |
---|
| 168 | This new created one keeps a pointer to the real transformed element of grid destination and generate new copies of other elements from grid source. |
---|
| 169 | \param [in] elementPositionInGrid position of element in grid |
---|
| 170 | \param [in] transType transformation type |
---|
| 171 | */ |
---|
[978] | 172 | void CGridTransformation::setUpGridDestination(int elementPositionInGrid, ETranformationType transType) |
---|
[1622] | 173 | TRY |
---|
[871] | 174 | { |
---|
| 175 | if (isSpecialTransformation(transType)) return; |
---|
| 176 | |
---|
| 177 | if (!tempGridDests_.empty() && (getNbAlgo() == tempGridDests_.size())) |
---|
| 178 | { |
---|
| 179 | tempGridDests_.resize(0); |
---|
| 180 | } |
---|
[887] | 181 | |
---|
[978] | 182 | if (1 == getNbAlgo()) |
---|
| 183 | { |
---|
| 184 | tmpGridDestination_ = gridDestination_; |
---|
| 185 | return; |
---|
| 186 | } |
---|
| 187 | |
---|
[887] | 188 | std::vector<CScalar*> scalarListDestP = gridDestination_->getScalars(); |
---|
| 189 | std::vector<CScalar*> scalarListSrcP = gridSource_->getScalars(), scalarDst; |
---|
| 190 | |
---|
[871] | 191 | std::vector<CAxis*> axisListDestP = gridDestination_->getAxis(); |
---|
| 192 | std::vector<CAxis*> axisListSrcP = gridSource_->getAxis(), axisDst; |
---|
| 193 | |
---|
| 194 | std::vector<CDomain*> domListDestP = gridDestination_->getDomains(); |
---|
| 195 | std::vector<CDomain*> domListSrcP = gridSource_->getDomains(), domainDst; |
---|
| 196 | |
---|
[887] | 197 | CArray<int,1> axisDomainOrderSrc = gridSource_->axis_domain_order; |
---|
| 198 | CArray<int,1> axisDomainOrderDst = gridDestination_->axis_domain_order; |
---|
| 199 | |
---|
[978] | 200 | std::map<int,std::pair<int,int> > elementPositionSrc = getElementPosition(gridSource_); |
---|
| 201 | std::map<int,std::pair<int,int> > elementPositionDst = getElementPosition(gridDestination_); |
---|
[871] | 202 | |
---|
[978] | 203 | CArray<int,1> elementOrder(axisDomainOrderDst.numElements()); |
---|
[887] | 204 | for (int idx = 0; idx < axisDomainOrderDst.numElements(); ++idx) |
---|
[871] | 205 | { |
---|
[978] | 206 | if (elementPositionInGrid == idx) |
---|
[887] | 207 | { |
---|
[978] | 208 | int dimElementDst = elementPositionDst[idx].first; |
---|
| 209 | int elementIndex = elementPositionDst[idx].second; |
---|
| 210 | switch (dimElementDst) |
---|
| 211 | { |
---|
| 212 | case 2: |
---|
| 213 | domainDst.push_back(domListDestP[elementIndex]); |
---|
| 214 | break; |
---|
| 215 | case 1: |
---|
| 216 | axisDst.push_back(axisListDestP[elementIndex]); |
---|
| 217 | break; |
---|
| 218 | case 0: |
---|
| 219 | scalarDst.push_back(scalarListDestP[elementIndex]); |
---|
| 220 | break; |
---|
| 221 | default: |
---|
| 222 | break; |
---|
| 223 | } |
---|
| 224 | elementOrder(idx) = dimElementDst; |
---|
[887] | 225 | } |
---|
| 226 | else |
---|
[978] | 227 | { |
---|
| 228 | int dimElementSrc = elementPositionSrc[idx].first; |
---|
| 229 | int elementIndex = elementPositionSrc[idx].second; |
---|
| 230 | switch (dimElementSrc) |
---|
| 231 | { |
---|
| 232 | case 2: |
---|
| 233 | domainDst.push_back(domListSrcP[elementIndex]); |
---|
| 234 | break; |
---|
| 235 | case 1: |
---|
| 236 | axisDst.push_back(axisListSrcP[elementIndex]); |
---|
| 237 | break; |
---|
| 238 | case 0: |
---|
| 239 | scalarDst.push_back(scalarListSrcP[elementIndex]); |
---|
| 240 | break; |
---|
| 241 | default: |
---|
| 242 | break; |
---|
| 243 | } |
---|
| 244 | elementOrder(idx) = dimElementSrc; |
---|
[887] | 245 | } |
---|
[871] | 246 | } |
---|
| 247 | |
---|
[1158] | 248 | tmpGridDestination_ = CGrid::createGrid(domainDst, axisDst, scalarDst, elementOrder); |
---|
[871] | 249 | tempGridDests_.push_back(tmpGridDestination_); |
---|
| 250 | } |
---|
[1622] | 251 | CATCH |
---|
[871] | 252 | |
---|
| 253 | /*! |
---|
[624] | 254 | Assign the current grid destination to the grid source in the new transformation. |
---|
| 255 | The current grid destination plays the role of grid source in next transformation (if any). |
---|
| 256 | Only element on which the transformation is performed is modified |
---|
| 257 | \param [in] elementPositionInGrid position of element in grid |
---|
| 258 | \param [in] transType transformation type |
---|
| 259 | */ |
---|
[978] | 260 | void CGridTransformation::setUpGridSource(int elementPositionInGrid) |
---|
[1622] | 261 | TRY |
---|
[622] | 262 | { |
---|
[871] | 263 | if (!tempGridSrcs_.empty() && (getNbAlgo()-1) == tempGridSrcs_.size()) |
---|
[827] | 264 | { |
---|
[871] | 265 | tempGridSrcs_.resize(0); |
---|
[827] | 266 | } |
---|
| 267 | |
---|
[887] | 268 | std::vector<CScalar*> scalarListDestP = tmpGridDestination_->getScalars(); |
---|
| 269 | std::vector<CScalar*> scalarListSrcP = gridSource_->getScalars(), scalarSrc; |
---|
| 270 | |
---|
[871] | 271 | std::vector<CAxis*> axisListDestP = tmpGridDestination_->getAxis(); |
---|
[827] | 272 | std::vector<CAxis*> axisListSrcP = gridSource_->getAxis(), axisSrc; |
---|
[622] | 273 | |
---|
[871] | 274 | std::vector<CDomain*> domListDestP = tmpGridDestination_->getDomains(); |
---|
[827] | 275 | std::vector<CDomain*> domListSrcP = gridSource_->getDomains(), domainSrc; |
---|
[631] | 276 | |
---|
[978] | 277 | CArray<int,1> axisDomainOrderSrc = gridSource_->axis_domain_order; |
---|
| 278 | CArray<int,1> axisDomainOrderDst = tmpGridDestination_->axis_domain_order; |
---|
[887] | 279 | |
---|
[978] | 280 | std::map<int,std::pair<int,int> > elementPositionSrc = getElementPosition(gridSource_); |
---|
| 281 | std::map<int,std::pair<int,int> > elementPositionDst = getElementPosition(tmpGridDestination_); |
---|
[827] | 282 | |
---|
[887] | 283 | for (int idx = 0; idx < axisDomainOrderDst.numElements(); ++idx) |
---|
[978] | 284 | { |
---|
| 285 | if (elementPositionInGrid == idx) |
---|
[871] | 286 | { |
---|
[978] | 287 | int dimElementDst = elementPositionDst[idx].first; |
---|
| 288 | int elementIndex = elementPositionDst[idx].second; |
---|
| 289 | if (2 == dimElementDst) |
---|
[887] | 290 | { |
---|
| 291 | CDomain* domain = CDomain::createDomain(); |
---|
[978] | 292 | domain->domain_ref.setValue(domListDestP[elementIndex]->getId()); |
---|
[887] | 293 | domain->solveRefInheritance(true); |
---|
| 294 | domain->checkAttributesOnClient(); |
---|
| 295 | domainSrc.push_back(domain); |
---|
| 296 | } |
---|
[978] | 297 | else if (1 == dimElementDst) |
---|
[887] | 298 | { |
---|
| 299 | CAxis* axis = CAxis::createAxis(); |
---|
[978] | 300 | axis->axis_ref.setValue(axisListDestP[elementIndex]->getId()); |
---|
[887] | 301 | axis->solveRefInheritance(true); |
---|
| 302 | axis->checkAttributesOnClient(); |
---|
[978] | 303 | axisSrc.push_back(axis); |
---|
[887] | 304 | } |
---|
| 305 | else |
---|
| 306 | { |
---|
| 307 | CScalar* scalar = CScalar::createScalar(); |
---|
[978] | 308 | scalar->scalar_ref.setValue(scalarListDestP[elementIndex]->getId()); |
---|
[887] | 309 | scalar->solveRefInheritance(true); |
---|
| 310 | scalar->checkAttributesOnClient(); |
---|
| 311 | scalarSrc.push_back(scalar); |
---|
| 312 | } |
---|
[871] | 313 | } |
---|
[978] | 314 | else |
---|
| 315 | { |
---|
| 316 | int dimElementDst = elementPositionDst[idx].first; |
---|
| 317 | int elementIndex = elementPositionDst[idx].second; |
---|
| 318 | switch (dimElementDst) |
---|
| 319 | { |
---|
| 320 | case 2: |
---|
| 321 | domainSrc.push_back(domListDestP[elementIndex]); |
---|
| 322 | break; |
---|
| 323 | case 1: |
---|
| 324 | axisSrc.push_back(axisListDestP[elementIndex]); |
---|
| 325 | break; |
---|
| 326 | case 0: |
---|
| 327 | scalarSrc.push_back(scalarListDestP[elementIndex]); |
---|
| 328 | break; |
---|
| 329 | default: |
---|
| 330 | break; |
---|
| 331 | } |
---|
| 332 | } |
---|
[827] | 333 | } |
---|
| 334 | |
---|
[1158] | 335 | gridSource_ = CGrid::createGrid(domainSrc, axisSrc, scalarSrc, tmpGridDestination_->axis_domain_order); |
---|
[871] | 336 | |
---|
| 337 | tempGridSrcs_.push_back(gridSource_); |
---|
[622] | 338 | } |
---|
[1622] | 339 | CATCH |
---|
[622] | 340 | |
---|
[624] | 341 | /*! |
---|
| 342 | Perform all transformations |
---|
| 343 | For each transformation, there are some things to do: |
---|
| 344 | -) Chose the correct algorithm by transformation type and position of element |
---|
| 345 | -) Calculate the mapping of global index between the current grid source and grid destination |
---|
[867] | 346 | -) Calculate the mapping of global index between current grid DESTINATION and grid SOURCE |
---|
[624] | 347 | -) Make current grid destination become grid source in the next transformation |
---|
| 348 | */ |
---|
[832] | 349 | void CGridTransformation::computeAll(const std::vector<CArray<double,1>* >& dataAuxInputs, Time timeStamp) |
---|
[1622] | 350 | TRY |
---|
[622] | 351 | { |
---|
[887] | 352 | if (nbNormalAlgos_ < 1) return; |
---|
[827] | 353 | if (!auxInputs_.empty() && !dynamicalTransformation_) { dynamicalTransformation_ = true; return; } |
---|
[832] | 354 | if (dynamicalTransformation_) |
---|
| 355 | { |
---|
[867] | 356 | if (timeStamp_.insert(timeStamp).second) //Reset map |
---|
[858] | 357 | { |
---|
| 358 | std::list<SendingIndexGridSourceMap>().swap(localIndexToSendFromGridSource_); |
---|
| 359 | std::list<RecvIndexGridDestinationMap>().swap(localIndexToReceiveOnGridDest_); |
---|
[867] | 360 | std::list<size_t>().swap(nbLocalIndexOnGridDest_); |
---|
| 361 | } |
---|
[832] | 362 | else |
---|
| 363 | return; |
---|
| 364 | } |
---|
[824] | 365 | |
---|
[622] | 366 | CContext* context = CContext::getCurrent(); |
---|
[653] | 367 | CContextClient* client = context->client; |
---|
[622] | 368 | |
---|
| 369 | ListAlgoType::const_iterator itb = listAlgos_.begin(), |
---|
| 370 | ite = listAlgos_.end(), it; |
---|
[821] | 371 | |
---|
[622] | 372 | CGenericAlgorithmTransformation* algo = 0; |
---|
[687] | 373 | int nbAgloTransformation = 0; // Only count for executed transformation. Generate domain is a special one, not executed in the list |
---|
[622] | 374 | for (it = itb; it != ite; ++it) |
---|
| 375 | { |
---|
| 376 | int elementPositionInGrid = it->first; |
---|
| 377 | ETranformationType transType = (it->second).first; |
---|
[978] | 378 | int transformationOrder = (it->second).second.first; |
---|
| 379 | int algoType = ((it->second).second.second); //algoTypes_[std::distance(itb, it)]; |
---|
[862] | 380 | SourceDestinationIndexMap globaIndexWeightFromSrcToDst; |
---|
[978] | 381 | |
---|
[622] | 382 | |
---|
[873] | 383 | // Create a temporary grid destination which contains transformed element of grid destination and |
---|
[933] | 384 | // non-transformed elements to grid source |
---|
[978] | 385 | setUpGridDestination(elementPositionInGrid, transType); |
---|
[871] | 386 | |
---|
[622] | 387 | // First of all, select an algorithm |
---|
[827] | 388 | if (!dynamicalTransformation_ || (algoTransformation_.size() < listAlgos_.size())) |
---|
| 389 | { |
---|
[933] | 390 | selectAlgo(elementPositionInGrid, transType, transformationOrder, algoType); |
---|
[827] | 391 | algo = algoTransformation_.back(); |
---|
| 392 | } |
---|
| 393 | else |
---|
| 394 | algo = algoTransformation_[std::distance(itb, it)]; |
---|
[622] | 395 | |
---|
[941] | 396 | if ((0 != algo) && |
---|
| 397 | ((CGenericAlgorithmTransformation::ELEMENT_NO_MODIFICATION_WITH_DATA == algo->type()) || |
---|
| 398 | (CGenericAlgorithmTransformation::ELEMENT_MODIFICATION_WITH_DATA == algo->type()))) // Only registered transformation can be executed |
---|
[687] | 399 | { |
---|
[1412] | 400 | CTimer::get("computeIndexSourceMapping").resume() ; |
---|
[827] | 401 | algo->computeIndexSourceMapping(dataAuxInputs); |
---|
[1412] | 402 | CTimer::get("computeIndexSourceMapping").suspend() ; |
---|
| 403 | |
---|
[687] | 404 | // ComputeTransformation of global index of each element |
---|
| 405 | int elementPosition = it->first; |
---|
[1399] | 406 | bool nonDistributedActivated = CXios::getin<bool>("activate_non_distributed_transformation",false); |
---|
| 407 | |
---|
| 408 | if (nonDistributedActivated && !algo->isDistributedTransformation(elementPositionInGrid, gridSource_, tmpGridDestination_) ) |
---|
| 409 | { |
---|
| 410 | vector<int> localSrc ; |
---|
| 411 | vector<int> localDst ; |
---|
| 412 | vector<double> weight ; |
---|
[1637] | 413 | int nbLocalIndexOnGridDest; |
---|
[1399] | 414 | CTimer::get("computeTransformationMappingNonDistributed").resume(); |
---|
| 415 | algo->computeTransformationMappingNonDistributed(elementPosition, gridSource_, tmpGridDestination_, |
---|
[1637] | 416 | localSrc, localDst, weight, nbLocalIndexOnGridDest) ; |
---|
[1399] | 417 | CTimer::get("computeTransformationMappingNonDistributed").suspend(); |
---|
[622] | 418 | |
---|
[1399] | 419 | CTimer::get("computeTransformationMappingConvert").resume(); |
---|
[1637] | 420 | nbLocalIndexOnGridDest_.push_back(nbLocalIndexOnGridDest) ; |
---|
[1399] | 421 | int clientRank=client->clientRank ; |
---|
| 422 | { |
---|
| 423 | SendingIndexGridSourceMap tmp; |
---|
| 424 | localIndexToSendFromGridSource_.push_back(tmp) ; |
---|
| 425 | SendingIndexGridSourceMap& src=localIndexToSendFromGridSource_.back() ; |
---|
| 426 | CArray<int,1> arrayTmp ; |
---|
| 427 | src.insert( pair<int,CArray<int,1> >(clientRank,arrayTmp)) ; |
---|
| 428 | CArray<int,1>& array=src[clientRank] ; |
---|
| 429 | array.resize(localSrc.size()) ; |
---|
| 430 | for(int i=0;i< localSrc.size();++i) array(i)=localSrc[i] ; |
---|
| 431 | } |
---|
| 432 | { |
---|
| 433 | RecvIndexGridDestinationMap tmp; |
---|
| 434 | localIndexToReceiveOnGridDest_.push_back(tmp) ; |
---|
| 435 | RecvIndexGridDestinationMap& dst=localIndexToReceiveOnGridDest_.back() ; |
---|
| 436 | vector<pair<int,double> > vectTmp ; |
---|
| 437 | dst.insert( pair<int,vector<pair<int,double> > >(clientRank,vectTmp)) ; |
---|
| 438 | vector<pair<int,double> >& vect=dst[clientRank] ; |
---|
| 439 | vect.resize(localDst.size()) ; |
---|
| 440 | for(int i=0;i< localDst.size();++i) vect[i]=pair<int,double>(localDst[i],weight[i]) ; |
---|
| 441 | } |
---|
| 442 | CTimer::get("computeTransformationMappingConvert").suspend(); |
---|
| 443 | } |
---|
| 444 | else |
---|
| 445 | { |
---|
| 446 | CTimer::get("computeGlobalSourceIndex").resume(); |
---|
| 447 | algo->computeGlobalSourceIndex(elementPosition, |
---|
| 448 | gridSource_, |
---|
| 449 | tmpGridDestination_, |
---|
| 450 | globaIndexWeightFromSrcToDst); |
---|
| 451 | |
---|
| 452 | CTimer::get("computeGlobalSourceIndex").suspend(); |
---|
| 453 | CTimer::get("computeTransformationMapping").resume(); |
---|
| 454 | // Compute transformation of global indexes among grids |
---|
| 455 | computeTransformationMapping(globaIndexWeightFromSrcToDst); |
---|
| 456 | CTimer::get("computeTransformationMapping").suspend(); |
---|
| 457 | } |
---|
[887] | 458 | if (1 < nbNormalAlgos_) |
---|
[821] | 459 | { |
---|
| 460 | // Now grid destination becomes grid source in a new transformation |
---|
[978] | 461 | if (nbAgloTransformation != (nbNormalAlgos_-1)) setUpGridSource(elementPositionInGrid); |
---|
[821] | 462 | } |
---|
[687] | 463 | ++nbAgloTransformation; |
---|
| 464 | } |
---|
[622] | 465 | } |
---|
| 466 | } |
---|
[1622] | 467 | CATCH |
---|
[622] | 468 | |
---|
[621] | 469 | /*! |
---|
[841] | 470 | Compute exchange index between grid source and grid destination |
---|
| 471 | \param [in] globalIndexWeightFromDestToSource global index mapping between grid destination and grid source |
---|
[621] | 472 | */ |
---|
[862] | 473 | void CGridTransformation::computeTransformationMapping(const SourceDestinationIndexMap& globaIndexWeightFromSrcToDst) |
---|
[1622] | 474 | TRY |
---|
[620] | 475 | { |
---|
[623] | 476 | CContext* context = CContext::getCurrent(); |
---|
[653] | 477 | CContextClient* client = context->client; |
---|
[862] | 478 | int nbClient = client->clientSize; |
---|
| 479 | int clientRank = client->clientRank; |
---|
[623] | 480 | |
---|
[862] | 481 | // Recalculate the distribution of grid destination |
---|
[871] | 482 | CDistributionClient distributionClientDest(client->clientRank, tmpGridDestination_); |
---|
[862] | 483 | CDistributionClient::GlobalLocalDataMap& globalLocalIndexGridDestSendToServer = distributionClientDest.getGlobalLocalDataSendToServer(); |
---|
[871] | 484 | |
---|
[862] | 485 | // Update number of local index on each transformation |
---|
[871] | 486 | size_t nbLocalIndex = globalLocalIndexGridDestSendToServer.size(); |
---|
| 487 | nbLocalIndexOnGridDest_.push_back(nbLocalIndex); |
---|
[1637] | 488 | // localMaskOnGridDest_.push_back(std::vector<bool>()); |
---|
| 489 | // std::vector<bool>& tmpMask = localMaskOnGridDest_.back(); |
---|
| 490 | // tmpMask.resize(nbLocalIndex,false); |
---|
[622] | 491 | |
---|
[862] | 492 | // Find out number of index sent from grid source and number of index received on grid destination |
---|
| 493 | SourceDestinationIndexMap::const_iterator itbIndex = globaIndexWeightFromSrcToDst.begin(), |
---|
| 494 | iteIndex = globaIndexWeightFromSrcToDst.end(), itIndex; |
---|
[1542] | 495 | typedef std::unordered_map<size_t, std::vector<std::pair<size_t,double> > > SendIndexMap; |
---|
[862] | 496 | std::map<int,int> sendRankSizeMap,recvRankSizeMap; |
---|
| 497 | int connectedClient = globaIndexWeightFromSrcToDst.size(); |
---|
| 498 | int* recvCount=new int[nbClient]; |
---|
| 499 | int* displ=new int[nbClient]; |
---|
| 500 | int* sendRankBuff=new int[connectedClient]; |
---|
| 501 | int* sendSizeBuff=new int[connectedClient]; |
---|
| 502 | int n = 0; |
---|
| 503 | for (itIndex = itbIndex; itIndex != iteIndex; ++itIndex, ++n) |
---|
| 504 | { |
---|
| 505 | sendRankBuff[n] = itIndex->first; |
---|
| 506 | const SendIndexMap& sendIndexMap = itIndex->second; |
---|
| 507 | SendIndexMap::const_iterator itbSend = sendIndexMap.begin(), iteSend = sendIndexMap.end(), itSend; |
---|
| 508 | int sendSize = 0; |
---|
| 509 | for (itSend = itbSend; itSend != iteSend; ++itSend) |
---|
| 510 | { |
---|
| 511 | sendSize += itSend->second.size(); |
---|
| 512 | } |
---|
| 513 | sendSizeBuff[n] = sendSize; |
---|
| 514 | sendRankSizeMap[itIndex->first] = sendSize; |
---|
| 515 | } |
---|
[1639] | 516 | MPI_Allgather(&connectedClient,1,MPI_INT,recvCount,1,MPI_INT,client->intraComm); |
---|
[622] | 517 | |
---|
[862] | 518 | displ[0]=0 ; |
---|
| 519 | for(int n=1;n<nbClient;n++) displ[n]=displ[n-1]+recvCount[n-1]; |
---|
| 520 | int recvSize=displ[nbClient-1]+recvCount[nbClient-1]; |
---|
| 521 | int* recvRankBuff=new int[recvSize]; |
---|
| 522 | int* recvSizeBuff=new int[recvSize]; |
---|
[1639] | 523 | MPI_Allgatherv(sendRankBuff,connectedClient,MPI_INT,recvRankBuff,recvCount,displ,MPI_INT,client->intraComm); |
---|
| 524 | MPI_Allgatherv(sendSizeBuff,connectedClient,MPI_INT,recvSizeBuff,recvCount,displ,MPI_INT,client->intraComm); |
---|
[862] | 525 | for (int i = 0; i < nbClient; ++i) |
---|
[620] | 526 | { |
---|
[862] | 527 | int currentPos = displ[i]; |
---|
| 528 | for (int j = 0; j < recvCount[i]; ++j) |
---|
| 529 | if (recvRankBuff[currentPos+j] == clientRank) |
---|
| 530 | { |
---|
| 531 | recvRankSizeMap[i] = recvSizeBuff[currentPos+j]; |
---|
| 532 | } |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | // Sending global index of grid source to corresponding process as well as the corresponding mask |
---|
[1639] | 536 | std::vector<MPI_Request> requests; |
---|
| 537 | std::vector<MPI_Status> status; |
---|
[1542] | 538 | std::unordered_map<int, unsigned char* > recvMaskDst; |
---|
| 539 | std::unordered_map<int, unsigned long* > recvGlobalIndexSrc; |
---|
[862] | 540 | for (std::map<int,int>::const_iterator itRecv = recvRankSizeMap.begin(); itRecv != recvRankSizeMap.end(); ++itRecv) |
---|
| 541 | { |
---|
| 542 | int recvRank = itRecv->first; |
---|
| 543 | int recvSize = itRecv->second; |
---|
| 544 | recvMaskDst[recvRank] = new unsigned char [recvSize]; |
---|
| 545 | recvGlobalIndexSrc[recvRank] = new unsigned long [recvSize]; |
---|
| 546 | |
---|
[1639] | 547 | requests.push_back(MPI_Request()); |
---|
| 548 | MPI_Irecv(recvGlobalIndexSrc[recvRank], recvSize, MPI_UNSIGNED_LONG, recvRank, 46, client->intraComm, &requests.back()); |
---|
| 549 | requests.push_back(MPI_Request()); |
---|
| 550 | MPI_Irecv(recvMaskDst[recvRank], recvSize, MPI_UNSIGNED_CHAR, recvRank, 47, client->intraComm, &requests.back()); |
---|
[862] | 551 | } |
---|
| 552 | |
---|
[1542] | 553 | std::unordered_map<int, CArray<size_t,1> > globalIndexDst; |
---|
| 554 | std::unordered_map<int, CArray<double,1> > weightDst; |
---|
| 555 | std::unordered_map<int, unsigned char* > sendMaskDst; |
---|
| 556 | std::unordered_map<int, unsigned long* > sendGlobalIndexSrc; |
---|
[862] | 557 | for (itIndex = itbIndex; itIndex != iteIndex; ++itIndex) |
---|
| 558 | { |
---|
| 559 | int sendRank = itIndex->first; |
---|
| 560 | int sendSize = sendRankSizeMap[sendRank]; |
---|
| 561 | const SendIndexMap& sendIndexMap = itIndex->second; |
---|
| 562 | SendIndexMap::const_iterator itbSend = sendIndexMap.begin(), iteSend = sendIndexMap.end(), itSend; |
---|
| 563 | globalIndexDst[sendRank].resize(sendSize); |
---|
| 564 | weightDst[sendRank].resize(sendSize); |
---|
| 565 | sendMaskDst[sendRank] = new unsigned char [sendSize]; |
---|
| 566 | sendGlobalIndexSrc[sendRank] = new unsigned long [sendSize]; |
---|
| 567 | int countIndex = 0; |
---|
| 568 | for (itSend = itbSend; itSend != iteSend; ++itSend) |
---|
[620] | 569 | { |
---|
[862] | 570 | const std::vector<std::pair<size_t,double> >& dstWeight = itSend->second; |
---|
| 571 | for (int idx = 0; idx < dstWeight.size(); ++idx) |
---|
| 572 | { |
---|
| 573 | globalIndexDst[sendRank](countIndex) = dstWeight[idx].first; |
---|
| 574 | weightDst[sendRank](countIndex) = dstWeight[idx].second; |
---|
| 575 | if (0 < globalLocalIndexGridDestSendToServer.count(dstWeight[idx].first)) |
---|
| 576 | sendMaskDst[sendRank][countIndex] = 1; |
---|
| 577 | else |
---|
| 578 | sendMaskDst[sendRank][countIndex] = 0; |
---|
| 579 | sendGlobalIndexSrc[sendRank][countIndex] = itSend->first; |
---|
| 580 | ++countIndex; |
---|
| 581 | } |
---|
[620] | 582 | } |
---|
[862] | 583 | |
---|
| 584 | // Send global index source and mask |
---|
[1639] | 585 | requests.push_back(MPI_Request()); |
---|
| 586 | MPI_Isend(sendGlobalIndexSrc[sendRank], sendSize, MPI_UNSIGNED_LONG, sendRank, 46, client->intraComm, &requests.back()); |
---|
| 587 | requests.push_back(MPI_Request()); |
---|
| 588 | MPI_Isend(sendMaskDst[sendRank], sendSize, MPI_UNSIGNED_CHAR, sendRank, 47, client->intraComm, &requests.back()); |
---|
[620] | 589 | } |
---|
| 590 | |
---|
[862] | 591 | status.resize(requests.size()); |
---|
[1639] | 592 | MPI_Waitall(requests.size(), &requests[0], &status[0]); |
---|
[862] | 593 | |
---|
| 594 | // Okie, now use the mask to identify which index source we need to send, then also signal the destination which masked index we will return |
---|
[1639] | 595 | std::vector<MPI_Request>().swap(requests); |
---|
| 596 | std::vector<MPI_Status>().swap(status); |
---|
[862] | 597 | // Okie, on destination side, we will wait for information of masked index of source |
---|
| 598 | for (std::map<int,int>::const_iterator itSend = sendRankSizeMap.begin(); itSend != sendRankSizeMap.end(); ++itSend) |
---|
| 599 | { |
---|
| 600 | int recvRank = itSend->first; |
---|
| 601 | int recvSize = itSend->second; |
---|
| 602 | |
---|
[1639] | 603 | requests.push_back(MPI_Request()); |
---|
| 604 | MPI_Irecv(sendMaskDst[recvRank], recvSize, MPI_UNSIGNED_CHAR, recvRank, 48, client->intraComm, &requests.back()); |
---|
[862] | 605 | } |
---|
| 606 | |
---|
| 607 | // Ok, now we fill in local index of grid source (we even count for masked index) |
---|
| 608 | CDistributionClient distributionClientSrc(client->clientRank, gridSource_); |
---|
| 609 | CDistributionClient::GlobalLocalDataMap& globalLocalIndexGridSrcSendToServer = distributionClientSrc.getGlobalLocalDataSendToServer(); |
---|
[841] | 610 | localIndexToSendFromGridSource_.push_back(SendingIndexGridSourceMap()); |
---|
| 611 | SendingIndexGridSourceMap& tmpSend = localIndexToSendFromGridSource_.back(); |
---|
[862] | 612 | for (std::map<int,int>::const_iterator itRecv = recvRankSizeMap.begin(); itRecv != recvRankSizeMap.end(); ++itRecv) |
---|
[620] | 613 | { |
---|
[862] | 614 | int recvRank = itRecv->first; |
---|
| 615 | int recvSize = itRecv->second; |
---|
| 616 | unsigned char* recvMask = recvMaskDst[recvRank]; |
---|
| 617 | unsigned long* recvIndexSrc = recvGlobalIndexSrc[recvRank]; |
---|
| 618 | int realSendSize = 0; |
---|
| 619 | for (int idx = 0; idx < recvSize; ++idx) |
---|
[620] | 620 | { |
---|
[862] | 621 | if (0 != (*(recvMask+idx))) // OKie, now we have a demand from non-masked index destination |
---|
| 622 | if (0 < globalLocalIndexGridSrcSendToServer.count(*(recvIndexSrc+idx))) // check whether index source is masked |
---|
| 623 | ++realSendSize; |
---|
| 624 | else // inform the destination that this index is masked |
---|
| 625 | *(recvMask+idx) = 0; |
---|
[620] | 626 | } |
---|
[862] | 627 | |
---|
| 628 | tmpSend[recvRank].resize(realSendSize); |
---|
| 629 | realSendSize = 0; |
---|
| 630 | for (int idx = 0; idx < recvSize; ++idx) |
---|
| 631 | { |
---|
| 632 | if (0 != (*(recvMask+idx))) // OKie, now we have a demand from non-masked index destination |
---|
| 633 | { |
---|
| 634 | tmpSend[recvRank](realSendSize) = globalLocalIndexGridSrcSendToServer[*(recvIndexSrc+idx)]; |
---|
| 635 | ++realSendSize; |
---|
| 636 | } |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | // Okie, now inform the destination which source index are masked |
---|
[1639] | 640 | requests.push_back(MPI_Request()); |
---|
| 641 | MPI_Isend(recvMaskDst[recvRank], recvSize, MPI_UNSIGNED_CHAR, recvRank, 48, client->intraComm, &requests.back()); |
---|
[620] | 642 | } |
---|
[862] | 643 | status.resize(requests.size()); |
---|
[1639] | 644 | MPI_Waitall(requests.size(), &requests[0], &status[0]); |
---|
[862] | 645 | |
---|
| 646 | // Cool, now we can fill in local index of grid destination (counted for masked index) |
---|
| 647 | localIndexToReceiveOnGridDest_.push_back(RecvIndexGridDestinationMap()); |
---|
| 648 | RecvIndexGridDestinationMap& recvTmp = localIndexToReceiveOnGridDest_.back(); |
---|
| 649 | for (std::map<int,int>::const_iterator itSend = sendRankSizeMap.begin(); itSend != sendRankSizeMap.end(); ++itSend) |
---|
| 650 | { |
---|
| 651 | int recvRank = itSend->first; |
---|
| 652 | int recvSize = itSend->second; |
---|
| 653 | unsigned char* recvMask = sendMaskDst[recvRank]; |
---|
| 654 | |
---|
| 655 | CArray<size_t,1>& recvIndexDst = globalIndexDst[recvRank]; |
---|
| 656 | CArray<double,1>& recvWeightDst = weightDst[recvRank]; |
---|
| 657 | int realRecvSize = 0; |
---|
| 658 | for (int idx = 0; idx < recvSize; ++idx) |
---|
| 659 | { |
---|
| 660 | if (0 != *(recvMask+idx)) // OKie, now we have a non-masked index destination |
---|
| 661 | ++realRecvSize; |
---|
| 662 | } |
---|
| 663 | |
---|
| 664 | int localIndexDst; |
---|
| 665 | recvTmp[recvRank].resize(realRecvSize); |
---|
| 666 | realRecvSize = 0; |
---|
| 667 | for (int idx = 0; idx < recvSize; ++idx) |
---|
| 668 | { |
---|
| 669 | if (0 != *(recvMask+idx)) // OKie, now we have a demand from non-masked index destination |
---|
| 670 | { |
---|
| 671 | recvTmp[recvRank][realRecvSize].first = globalLocalIndexGridDestSendToServer[recvIndexDst(idx)]; |
---|
| 672 | recvTmp[recvRank][realRecvSize].second = recvWeightDst(idx); |
---|
| 673 | ++realRecvSize; |
---|
| 674 | } |
---|
| 675 | } |
---|
| 676 | } |
---|
| 677 | |
---|
| 678 | delete [] recvCount; |
---|
| 679 | delete [] displ; |
---|
| 680 | delete [] sendRankBuff; |
---|
| 681 | delete [] recvRankBuff; |
---|
| 682 | delete [] sendSizeBuff; |
---|
| 683 | delete [] recvSizeBuff; |
---|
| 684 | |
---|
[1542] | 685 | std::unordered_map<int, unsigned char* >::const_iterator itChar; |
---|
[862] | 686 | for (itChar = sendMaskDst.begin(); itChar != sendMaskDst.end(); ++itChar) |
---|
| 687 | delete [] itChar->second; |
---|
| 688 | for (itChar = recvMaskDst.begin(); itChar != recvMaskDst.end(); ++itChar) |
---|
| 689 | delete [] itChar->second; |
---|
[1542] | 690 | std::unordered_map<int, unsigned long* >::const_iterator itLong; |
---|
[862] | 691 | for (itLong = sendGlobalIndexSrc.begin(); itLong != sendGlobalIndexSrc.end(); ++itLong) |
---|
| 692 | delete [] itLong->second; |
---|
| 693 | for (itLong = recvGlobalIndexSrc.begin(); itLong != recvGlobalIndexSrc.end(); ++itLong) |
---|
| 694 | delete [] itLong->second; |
---|
| 695 | |
---|
[620] | 696 | } |
---|
[1622] | 697 | CATCH |
---|
[620] | 698 | |
---|
[621] | 699 | /*! |
---|
| 700 | Local index of data which need sending from the grid source |
---|
| 701 | \return local index of data |
---|
| 702 | */ |
---|
[841] | 703 | const std::list<CGridTransformation::SendingIndexGridSourceMap>& CGridTransformation::getLocalIndexToSendFromGridSource() const |
---|
[1622] | 704 | TRY |
---|
[620] | 705 | { |
---|
| 706 | return localIndexToSendFromGridSource_; |
---|
| 707 | } |
---|
[1622] | 708 | CATCH |
---|
[620] | 709 | |
---|
[621] | 710 | /*! |
---|
| 711 | Local index of data which will be received on the grid destination |
---|
| 712 | \return local index of data |
---|
| 713 | */ |
---|
[841] | 714 | const std::list<CGridTransformation::RecvIndexGridDestinationMap>& CGridTransformation::getLocalIndexToReceiveOnGridDest() const |
---|
[1622] | 715 | TRY |
---|
[620] | 716 | { |
---|
| 717 | return localIndexToReceiveOnGridDest_; |
---|
| 718 | } |
---|
[1622] | 719 | CATCH |
---|
[620] | 720 | |
---|
[871] | 721 | /*! |
---|
| 722 | Number of index will be received on the grid destination |
---|
| 723 | \return number of index of data |
---|
| 724 | */ |
---|
[841] | 725 | const std::list<size_t>& CGridTransformation::getNbLocalIndexToReceiveOnGridDest() const |
---|
[1622] | 726 | TRY |
---|
[841] | 727 | { |
---|
| 728 | return nbLocalIndexOnGridDest_; |
---|
[620] | 729 | } |
---|
[1622] | 730 | CATCH |
---|
[841] | 731 | |
---|
| 732 | } |
---|