[888] | 1 | /*! |
---|
| 2 | \file sum.cpp |
---|
| 3 | \author Ha NGUYEN |
---|
| 4 | \since 27 June 2016 |
---|
[1018] | 5 | \date 9 Jan 2017 |
---|
[888] | 6 | |
---|
| 7 | \brief sum reduction |
---|
| 8 | */ |
---|
[979] | 9 | #include "sum_reduction.hpp" |
---|
[1018] | 10 | #include "utils.hpp" |
---|
[888] | 11 | |
---|
| 12 | namespace xios { |
---|
| 13 | |
---|
| 14 | CSumReductionAlgorithm::CSumReductionAlgorithm() |
---|
| 15 | : CReductionAlgorithm() |
---|
| 16 | { |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | CReductionAlgorithm* CSumReductionAlgorithm::create() |
---|
| 20 | { |
---|
| 21 | return (new CSumReductionAlgorithm()); |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | bool CSumReductionAlgorithm::registerTrans() |
---|
| 25 | { |
---|
| 26 | return registerOperation(TRANS_REDUCE_SUM, CSumReductionAlgorithm::create); |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | void CSumReductionAlgorithm::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
| 30 | const double* dataInput, |
---|
| 31 | CArray<double,1>& dataOut, |
---|
[1018] | 32 | std::vector<bool>& flagInitial, |
---|
[1076] | 33 | bool ignoreMissingValue) |
---|
[888] | 34 | { |
---|
[1076] | 35 | if (ignoreMissingValue) |
---|
[888] | 36 | { |
---|
[1018] | 37 | int nbLocalIndex = localIndex.size(); |
---|
[1205] | 38 | int currentlocalIndex = 0; |
---|
[1043] | 39 | |
---|
[1018] | 40 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
[888] | 41 | { |
---|
[1018] | 42 | currentlocalIndex = localIndex[idx].first; |
---|
| 43 | if (!NumTraits<double>::isnan(*(dataInput + idx))) |
---|
| 44 | { |
---|
| 45 | if (flagInitial[currentlocalIndex]) |
---|
| 46 | { |
---|
| 47 | dataOut(currentlocalIndex) = *(dataInput + idx); |
---|
| 48 | flagInitial[currentlocalIndex] = false; |
---|
| 49 | } |
---|
| 50 | else |
---|
| 51 | { |
---|
| 52 | dataOut(currentlocalIndex) += *(dataInput + idx); |
---|
| 53 | } |
---|
| 54 | } |
---|
[1205] | 55 | else |
---|
| 56 | { |
---|
| 57 | if (flagInitial[currentlocalIndex]) |
---|
| 58 | dataOut(currentlocalIndex) = std::numeric_limits<double>::quiet_NaN(); |
---|
| 59 | } |
---|
[1018] | 60 | } |
---|
| 61 | } |
---|
| 62 | else |
---|
| 63 | { |
---|
| 64 | int nbLocalIndex = localIndex.size(); |
---|
| 65 | int currentlocalIndex = 0; |
---|
| 66 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
[888] | 67 | { |
---|
[1018] | 68 | currentlocalIndex = localIndex[idx].first; |
---|
| 69 | if (flagInitial[currentlocalIndex]) |
---|
| 70 | { |
---|
| 71 | dataOut(currentlocalIndex) = *(dataInput + idx); |
---|
| 72 | flagInitial[currentlocalIndex] = false; |
---|
| 73 | } |
---|
| 74 | else |
---|
| 75 | { |
---|
| 76 | dataOut(currentlocalIndex) += *(dataInput + idx); |
---|
| 77 | } |
---|
[888] | 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | } |
---|