1 | /*! |
---|
2 | \file average.cpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 8 Sep 2016 |
---|
5 | \date 9 Jan 2017 |
---|
6 | |
---|
7 | \brief average reduction |
---|
8 | */ |
---|
9 | #include "average_reduction.hpp" |
---|
10 | #include "utils.hpp" |
---|
11 | |
---|
12 | namespace xios { |
---|
13 | |
---|
14 | CAverageReductionAlgorithm::CAverageReductionAlgorithm() |
---|
15 | : CReductionAlgorithm(), resetWeight_(true) |
---|
16 | { |
---|
17 | } |
---|
18 | |
---|
19 | CReductionAlgorithm* CAverageReductionAlgorithm::create() |
---|
20 | { |
---|
21 | return (new CAverageReductionAlgorithm()); |
---|
22 | } |
---|
23 | |
---|
24 | bool CAverageReductionAlgorithm::registerTrans() |
---|
25 | { |
---|
26 | return registerOperation(TRANS_REDUCE_AVERAGE, CAverageReductionAlgorithm::create); |
---|
27 | } |
---|
28 | |
---|
29 | void CAverageReductionAlgorithm::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
30 | const double* dataInput, |
---|
31 | CArray<double,1>& dataOut, |
---|
32 | std::vector<bool>& flagInitial, |
---|
33 | bool ignoreMissingValue, bool firstPass) |
---|
34 | { |
---|
35 | if (resetWeight_) { weights_.resize(flagInitial.size()); weights_ = 1.0; resetWeight_ = false; } |
---|
36 | |
---|
37 | if (ignoreMissingValue) |
---|
38 | { |
---|
39 | int nbLocalIndex = localIndex.size(); |
---|
40 | int currentlocalIndex = 0; |
---|
41 | double currentWeight = 0.0; |
---|
42 | |
---|
43 | if (firstPass) dataOut=std::numeric_limits<double>::quiet_NaN(); |
---|
44 | |
---|
45 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
46 | { |
---|
47 | currentlocalIndex = localIndex[idx].first; |
---|
48 | currentWeight = localIndex[idx].second; |
---|
49 | if (!NumTraits<double>::isNan(*(dataInput + idx))) |
---|
50 | { |
---|
51 | if (flagInitial[currentlocalIndex]) |
---|
52 | { |
---|
53 | dataOut(currentlocalIndex) = *(dataInput + idx); |
---|
54 | flagInitial[currentlocalIndex] = false; |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | dataOut(currentlocalIndex) += *(dataInput + idx); |
---|
59 | weights_(currentlocalIndex) += 1.0; |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | int nbLocalIndex = localIndex.size(); |
---|
67 | int currentlocalIndex = 0; |
---|
68 | double currentWeight = 0.0; |
---|
69 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
70 | { |
---|
71 | currentlocalIndex = localIndex[idx].first; |
---|
72 | currentWeight = localIndex[idx].second; |
---|
73 | |
---|
74 | if (flagInitial[currentlocalIndex]) |
---|
75 | { |
---|
76 | dataOut(currentlocalIndex) = *(dataInput + idx); |
---|
77 | flagInitial[currentlocalIndex] = false; |
---|
78 | } |
---|
79 | else |
---|
80 | { |
---|
81 | dataOut(currentlocalIndex) += *(dataInput + idx); |
---|
82 | weights_(currentlocalIndex) += 1.0; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | void CAverageReductionAlgorithm::updateData(CArray<double,1>& dataOut) |
---|
89 | { |
---|
90 | dataOut /= weights_; |
---|
91 | resetWeight_ = true; |
---|
92 | } |
---|
93 | |
---|
94 | } |
---|