1 | /*! |
---|
2 | \file average.cpp |
---|
3 | \author Ha NGUYEN |
---|
4 | \since 8 Sep 2016 |
---|
5 | \date 8 Sep 2016 |
---|
6 | |
---|
7 | \brief average reduction |
---|
8 | */ |
---|
9 | #include "average_reduction.hpp" |
---|
10 | |
---|
11 | namespace xios { |
---|
12 | |
---|
13 | CAverageReductionAlgorithm::CAverageReductionAlgorithm() |
---|
14 | : CReductionAlgorithm(), resetWeight_(true) |
---|
15 | { |
---|
16 | } |
---|
17 | |
---|
18 | CReductionAlgorithm* CAverageReductionAlgorithm::create() |
---|
19 | { |
---|
20 | return (new CAverageReductionAlgorithm()); |
---|
21 | } |
---|
22 | |
---|
23 | bool CAverageReductionAlgorithm::registerTrans() |
---|
24 | { |
---|
25 | return registerOperation(TRANS_REDUCE_AVERAGE, CAverageReductionAlgorithm::create); |
---|
26 | } |
---|
27 | |
---|
28 | void CAverageReductionAlgorithm::apply(const std::vector<std::pair<int,double> >& localIndex, |
---|
29 | const double* dataInput, |
---|
30 | CArray<double,1>& dataOut, |
---|
31 | std::vector<bool>& flagInitial) |
---|
32 | { |
---|
33 | if (resetWeight_) { weights_.resize(flagInitial.size()); weights_ = 1.0; resetWeight_ = false; } |
---|
34 | |
---|
35 | int nbLocalIndex = localIndex.size(); |
---|
36 | int currentlocalIndex = 0; |
---|
37 | double currentWeight = 0.0; |
---|
38 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|
39 | { |
---|
40 | currentlocalIndex = localIndex[idx].first; |
---|
41 | currentWeight = localIndex[idx].second; |
---|
42 | |
---|
43 | if (flagInitial[currentlocalIndex]) |
---|
44 | { |
---|
45 | dataOut(currentlocalIndex) = *(dataInput + idx); |
---|
46 | flagInitial[currentlocalIndex] = false; |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | dataOut(currentlocalIndex) += *(dataInput + idx); |
---|
51 | weights_(currentlocalIndex) += 1.0; |
---|
52 | } |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | void CAverageReductionAlgorithm::updateData(CArray<double,1>& dataOut) |
---|
57 | { |
---|
58 | dataOut /= weights_; |
---|
59 | resetWeight_ = true; |
---|
60 | } |
---|
61 | |
---|
62 | } |
---|