[2460] | 1 | /*! |
---|
| 2 | \file scalar_algorithm_redistribute.cpp |
---|
| 3 | \brief Algorithm for redistribute a scalar. |
---|
| 4 | */ |
---|
| 5 | #include "scalar_algorithm_redistribute.hpp" |
---|
| 6 | #include "redistribute_scalar.hpp" |
---|
| 7 | #include "scalar.hpp" |
---|
| 8 | #include "grid.hpp" |
---|
| 9 | #include "grid_transformation_factory_impl.hpp" |
---|
[2482] | 10 | #include "context.hpp" |
---|
[2460] | 11 | |
---|
| 12 | namespace xios |
---|
| 13 | { |
---|
| 14 | |
---|
| 15 | shared_ptr<CGenericAlgorithmTransformation> CScalarAlgorithmRedistribute::create(bool isSource, CGrid* gridDst, CGrid* gridSrc, |
---|
| 16 | CTransformation<CScalar>* transformation, |
---|
| 17 | int elementPositionInGrid, |
---|
| 18 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
| 19 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
| 20 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
| 21 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
| 22 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
| 23 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
| 24 | TRY |
---|
| 25 | { |
---|
| 26 | std::vector<CScalar*> scalarListDestP = gridDst->getScalars(); |
---|
| 27 | std::vector<CScalar*> scalarListSrcP = gridSrc->getScalars(); |
---|
| 28 | |
---|
| 29 | CRedistributeScalar* redistributeScalar = dynamic_cast<CRedistributeScalar*> (transformation); |
---|
| 30 | int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid]; |
---|
| 31 | int scalarSrcIndex = elementPositionInGridSrc2ScalarPosition[elementPositionInGrid]; |
---|
| 32 | |
---|
| 33 | return make_shared<CScalarAlgorithmRedistribute>(isSource, scalarListDestP[scalarDstIndex], scalarListSrcP[scalarSrcIndex], redistributeScalar); |
---|
| 34 | } |
---|
| 35 | CATCH |
---|
| 36 | |
---|
| 37 | bool CScalarAlgorithmRedistribute::dummyRegistered_ = CScalarAlgorithmRedistribute::registerTrans(); |
---|
| 38 | bool CScalarAlgorithmRedistribute::registerTrans() |
---|
| 39 | TRY |
---|
| 40 | { |
---|
| 41 | return CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDISTRIBUTE_SCALAR, create); |
---|
| 42 | } |
---|
| 43 | CATCH |
---|
| 44 | |
---|
| 45 | CScalarAlgorithmRedistribute::CScalarAlgorithmRedistribute(bool isSource, CScalar* scalarDestination, CScalar* scalarSource, CRedistributeScalar* redistributeScalar) |
---|
| 46 | : CAlgorithmTransformationTransfer(isSource) |
---|
| 47 | TRY |
---|
| 48 | { |
---|
| 49 | |
---|
[2482] | 50 | CContext* context = CContext::getCurrent(); |
---|
[2460] | 51 | |
---|
[2482] | 52 | scalarDestination->n.reset(); |
---|
| 53 | scalarDestination->mask.reset(); |
---|
| 54 | scalarDestination->value.reset(); |
---|
| 55 | scalarDestination->bounds.reset(); |
---|
| 56 | |
---|
| 57 | redistributeScalar->checkValid(scalarSource); |
---|
| 58 | |
---|
| 59 | // define the global index of the new domain |
---|
| 60 | |
---|
| 61 | CArray<size_t,1> globalIndex ; |
---|
| 62 | |
---|
| 63 | if ( redistributeScalar->type == CRedistributeScalar::type_attr::bands |
---|
| 64 | || redistributeScalar->type == CRedistributeScalar::type_attr::column |
---|
| 65 | || redistributeScalar->type == CRedistributeScalar::type_attr::root ) |
---|
| 66 | { |
---|
| 67 | if (context->getIntraCommRank()==0) |
---|
| 68 | { |
---|
| 69 | scalarDestination->n = 1; |
---|
| 70 | globalIndex.resize(1) ; |
---|
| 71 | globalIndex(0)=0 ; |
---|
| 72 | } |
---|
| 73 | else |
---|
| 74 | { |
---|
| 75 | scalarDestination->n = 0; |
---|
| 76 | globalIndex.resize(0) ; |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | else if (redistributeScalar->type == CRedistributeScalar::type_attr::full) |
---|
| 80 | { |
---|
| 81 | scalarDestination->n = 1; |
---|
| 82 | globalIndex.resize(1) ; |
---|
| 83 | globalIndex(0)=0 ; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | auto& transMap = this->transformationMapping_; |
---|
| 87 | for(int i=0; i<globalIndex.numElements(); i++) transMap[globalIndex(i)]=globalIndex(i) ; |
---|
| 88 | |
---|
| 89 | auto elementDst=shared_ptr<CLocalElement>(new CLocalElement(context->getIntraCommRank(), 1, globalIndex)) ; |
---|
| 90 | elementDst->addFullView() ; |
---|
| 91 | |
---|
| 92 | auto transformConnector = make_shared<CTransformConnector>(scalarSource->getLocalView(CElementView::FULL), elementDst->getView(CElementView::FULL),context->getIntraComm()) ; |
---|
| 93 | transformConnector->computeConnector() ; |
---|
| 94 | |
---|
| 95 | CArray<double,1> valSrc, valDst ; |
---|
| 96 | valSrc.resize(scalarSource->getLocalView(CElementView::FULL)->getSize()) ; |
---|
| 97 | valDst.resize(scalarDestination->getLocalView(CElementView::FULL)->getSize()) ; |
---|
| 98 | |
---|
| 99 | if (scalarSource->hasValue()) |
---|
| 100 | { |
---|
| 101 | if (valSrc.numElements()>0) valSrc(0)=scalarSource->value ; |
---|
| 102 | transformConnector->transfer(valSrc, valDst) ; |
---|
| 103 | if (valDst.numElements()>0) scalarDestination->value = valDst(0) ; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | if (scalarSource->hasBounds()) |
---|
| 107 | { |
---|
| 108 | transformConnector->transfer(2, scalarSource->bounds, scalarDestination->bounds) ; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | if (scalarSource->hasLabel()) |
---|
| 112 | { |
---|
| 113 | //ym transferTransformConnector.transfer(axisSrc->label, axisDestination->label) ; -> probably not supported now, see later |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | // transfer the mask |
---|
| 117 | auto transformMask = make_shared<CTransformConnector>(scalarSource->getLocalView(CElementView::WORKFLOW), elementDst->getView(CElementView::FULL),context->getIntraComm()) ; |
---|
| 118 | transformMask->computeConnector() ; |
---|
| 119 | |
---|
| 120 | CArray<bool,1> workflow(scalarSource->getLocalView(CElementView::WORKFLOW)->getSize()) ; |
---|
| 121 | CArray<bool,1> mask ; |
---|
| 122 | mask.resize(scalarSource->getLocalView(CElementView::FULL)->getSize()) ; |
---|
| 123 | workflow=true ; |
---|
| 124 | |
---|
| 125 | transformMask->transfer(workflow, mask, false) ; |
---|
| 126 | scalarDestination->mask = mask(0) ; |
---|
| 127 | |
---|
[2460] | 128 | scalarDestination->checkAttributes() ; |
---|
| 129 | } |
---|
| 130 | CATCH |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | } |
---|