source: XIOS/dev/dev_trunk_omp/src/transformation/Functions/average_reduction.cpp @ 1677

Last change on this file since 1677 was 1677, checked in by yushan, 5 years ago

MARK: Dynamic workflow graph developement. Branch up to date with trunk @1663.

File size: 2.5 KB
Line 
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
12namespace xios {
13
14CAverageReductionAlgorithm::CAverageReductionAlgorithm()
15  : CReductionAlgorithm(), resetWeight_(true)
16{
17}
18
19CReductionAlgorithm* CAverageReductionAlgorithm::create()
20{
21  return (new CAverageReductionAlgorithm());
22}
23
24bool CAverageReductionAlgorithm::registerTrans()
25{
26  return registerOperation(TRANS_REDUCE_AVERAGE, CAverageReductionAlgorithm::create);
27}
28
29void 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  std::cout<<"================================ CAverageReductionAlgorithm::apply"<<std::endl;
36  if (resetWeight_) { weights_.resize(flagInitial.size()); weights_ = 1.0; resetWeight_ = false; } 
37
38  if (ignoreMissingValue)
39  {
40    int nbLocalIndex = localIndex.size();
41    int currentlocalIndex = 0;
42    double currentWeight  = 0.0;
43
44    if (firstPass) dataOut=std::numeric_limits<double>::quiet_NaN();
45
46    for (int idx = 0; idx < nbLocalIndex; ++idx)
47    {
48      currentlocalIndex = localIndex[idx].first;
49      currentWeight     = localIndex[idx].second;
50      if (!NumTraits<double>::isNan(*(dataInput + idx)))
51      {
52        if (flagInitial[currentlocalIndex])
53        {
54          dataOut(currentlocalIndex) = *(dataInput + idx);
55          flagInitial[currentlocalIndex] = false;
56        }
57        else
58        {
59          dataOut(currentlocalIndex)  += *(dataInput + idx);
60          weights_(currentlocalIndex) += 1.0;
61        }
62      }
63    }
64  }
65  else
66  {
67    int nbLocalIndex = localIndex.size();
68    int currentlocalIndex = 0;
69    double currentWeight  = 0.0;
70    for (int idx = 0; idx < nbLocalIndex; ++idx)
71    {
72      currentlocalIndex = localIndex[idx].first;
73      currentWeight     = localIndex[idx].second;
74
75      if (flagInitial[currentlocalIndex])
76      {
77        dataOut(currentlocalIndex) = *(dataInput + idx);
78        flagInitial[currentlocalIndex] = false;
79      }
80      else
81      {
82        dataOut(currentlocalIndex)  += *(dataInput + idx);
83        weights_(currentlocalIndex) += 1.0;
84      }
85    }
86  }
87}
88
89void CAverageReductionAlgorithm::updateData(CArray<double,1>& dataOut)
90{
91  dataOut /= weights_;
92  resetWeight_ = true;
93}
94
95}
Note: See TracBrowser for help on using the repository browser.