[1985] | 1 | /*! |
---|
| 2 | \file scalar_algorithm_reduce_scalar.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 23 June 2016 |
---|
| 5 | \date 23 June 2016 |
---|
| 6 | |
---|
| 7 | \brief Algorithm for reduce an axis to a scalar |
---|
| 8 | */ |
---|
| 9 | #include "scalar_algorithm_reduce_axis.hpp" |
---|
| 10 | #include "axis.hpp" |
---|
| 11 | #include "scalar.hpp" |
---|
| 12 | #include "reduce_axis_to_scalar.hpp" |
---|
| 13 | #include "grid.hpp" |
---|
| 14 | #include "grid_transformation_factory_impl.hpp" |
---|
| 15 | #include "reduction.hpp" |
---|
| 16 | |
---|
| 17 | #include "reduction.hpp" |
---|
| 18 | |
---|
| 19 | namespace xios { |
---|
[2270] | 20 | shared_ptr<CGenericAlgorithmTransformation> CScalarAlgorithmReduceAxis::create(bool isSource, CGrid* gridDst, CGrid* gridSrc, |
---|
[1985] | 21 | CTransformation<CScalar>* transformation, |
---|
| 22 | int elementPositionInGrid, |
---|
| 23 | std::map<int, int>& elementPositionInGridSrc2ScalarPosition, |
---|
| 24 | std::map<int, int>& elementPositionInGridSrc2AxisPosition, |
---|
| 25 | std::map<int, int>& elementPositionInGridSrc2DomainPosition, |
---|
| 26 | std::map<int, int>& elementPositionInGridDst2ScalarPosition, |
---|
| 27 | std::map<int, int>& elementPositionInGridDst2AxisPosition, |
---|
| 28 | std::map<int, int>& elementPositionInGridDst2DomainPosition) |
---|
| 29 | TRY |
---|
| 30 | { |
---|
| 31 | std::vector<CScalar*> scalarListDestP = gridDst->getScalars(); |
---|
| 32 | std::vector<CAxis*> axisListSrcP = gridSrc->getAxis(); |
---|
| 33 | |
---|
| 34 | CReduceAxisToScalar* reduceAxis = dynamic_cast<CReduceAxisToScalar*> (transformation); |
---|
| 35 | int scalarDstIndex = elementPositionInGridDst2ScalarPosition[elementPositionInGrid]; |
---|
| 36 | int axisSrcIndex = elementPositionInGridSrc2AxisPosition[elementPositionInGrid]; |
---|
| 37 | |
---|
[2270] | 38 | return make_shared<CScalarAlgorithmReduceAxis>(isSource, scalarListDestP[scalarDstIndex], axisListSrcP[axisSrcIndex], reduceAxis); |
---|
[1985] | 39 | } |
---|
| 40 | CATCH |
---|
| 41 | |
---|
| 42 | bool CScalarAlgorithmReduceAxis::dummyRegistered_ = CScalarAlgorithmReduceAxis::registerTrans(); |
---|
| 43 | bool CScalarAlgorithmReduceAxis::registerTrans() |
---|
| 44 | TRY |
---|
| 45 | { |
---|
| 46 | return CGridTransformationFactory<CScalar>::registerTransformation(TRANS_REDUCE_AXIS_TO_SCALAR, create); |
---|
| 47 | } |
---|
| 48 | CATCH |
---|
| 49 | |
---|
[1988] | 50 | CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(bool isSource, CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo) |
---|
[1999] | 51 | : CAlgorithmTransformationReduce(isSource) |
---|
[1985] | 52 | TRY |
---|
| 53 | { |
---|
| 54 | if (algo->operation.isEmpty()) |
---|
| 55 | ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CAxis* axisDestination, CAxis* axisSource, CReduceAxisToScalar* algo)", |
---|
| 56 | << "Operation must be defined." |
---|
| 57 | << "Axis source " <<axisSource->getId() << std::endl |
---|
| 58 | << "Scalar destination " << scalarDestination->getId()); |
---|
| 59 | StdString op; |
---|
| 60 | switch (algo->operation) |
---|
| 61 | { |
---|
| 62 | case CReduceAxisToScalar::operation_attr::sum: |
---|
[1999] | 63 | operator_ = EReduction::sum; |
---|
[1985] | 64 | break; |
---|
| 65 | case CReduceAxisToScalar::operation_attr::min: |
---|
[1999] | 66 | operator_ = EReduction::min; |
---|
[1985] | 67 | break; |
---|
| 68 | case CReduceAxisToScalar::operation_attr::max: |
---|
[1999] | 69 | operator_ = EReduction::max; |
---|
[1985] | 70 | break; |
---|
| 71 | case CReduceAxisToScalar::operation_attr::average: |
---|
[1999] | 72 | operator_ = EReduction::average; |
---|
[1985] | 73 | break; |
---|
| 74 | default: |
---|
| 75 | ERROR("CScalarAlgorithmReduceAxis::CScalarAlgorithmReduceAxis(CScalar* scalarDestination, CAxis* axisSource, CReduceAxisToScalar* algo)", |
---|
| 76 | << "Operation is wrongly defined. Supported operations: sum, min, max, average." << std::endl |
---|
| 77 | << "Domain source " <<axisSource->getId() << std::endl |
---|
| 78 | << "Scalar destination " << scalarDestination->getId()); |
---|
| 79 | |
---|
[1999] | 80 | } |
---|
[2291] | 81 | |
---|
| 82 | bool local=false ; |
---|
| 83 | if (!algo->local.isEmpty()) local=algo->local ; |
---|
[1985] | 84 | |
---|
[2291] | 85 | auto& transMap = this->transformationMapping_; |
---|
| 86 | |
---|
| 87 | if (local) |
---|
| 88 | { |
---|
| 89 | scalarDestination->n=1 ; // no mask |
---|
| 90 | scalarDestination->mask.reset() ; |
---|
| 91 | |
---|
| 92 | CArray<size_t,1> srcGlobalIndex ; |
---|
| 93 | axisSource->getLocalView(CElementView::WORKFLOW)->getGlobalIndexView(srcGlobalIndex) ; |
---|
| 94 | size_t nbIdx = srcGlobalIndex.numElements(); |
---|
| 95 | if (nbIdx==0) scalarDestination->n=0 ; |
---|
| 96 | scalarDestination->checkAttributes() ; |
---|
[1985] | 97 | |
---|
[2291] | 98 | for (size_t idx = 0; idx < nbIdx; ++idx) |
---|
| 99 | { |
---|
| 100 | size_t globalIdx = srcGlobalIndex(idx); |
---|
| 101 | transformationMapping_[0].push_back(globalIdx); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | } |
---|
| 105 | else |
---|
| 106 | { |
---|
| 107 | scalarDestination->checkAttributes() ; |
---|
| 108 | |
---|
| 109 | int globalIndexSize = axisSource->getLocalView(CElementView::WORKFLOW)->getGlobalSize(); |
---|
| 110 | CArray<size_t,1> dstGlobalIndex ; |
---|
| 111 | scalarDestination->getLocalView(CElementView::WORKFLOW)->getGlobalIndexView(dstGlobalIndex) ; |
---|
| 112 | if (dstGlobalIndex.numElements()!=0) |
---|
| 113 | for (int idx = 0; idx < globalIndexSize; ++idx) transformationMapping_[0].push_back(idx); |
---|
| 114 | } |
---|
| 115 | |
---|
[1999] | 116 | this->computeAlgorithm(axisSource->getLocalView(CElementView::WORKFLOW), scalarDestination->getLocalView(CElementView::WORKFLOW)) ; |
---|
| 117 | |
---|
[1985] | 118 | } |
---|
| 119 | CATCH |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | CScalarAlgorithmReduceAxis::~CScalarAlgorithmReduceAxis() |
---|
| 123 | TRY |
---|
| 124 | { |
---|
| 125 | } |
---|
| 126 | CATCH |
---|
| 127 | |
---|
| 128 | } |
---|