1 | /*! |
---|

2 | \file sum.cpp |
---|

3 | \author Ha NGUYEN |
---|

4 | \since 27 June 2016 |
---|

5 | \date 9 Jan 2017 |
---|

6 | |
---|

7 | \brief sum reduction |
---|

8 | */ |
---|

9 | #include "sum_reduction.hpp" |
---|

10 | #include "utils.hpp" |
---|

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, |
---|

32 | std::vector<bool>& flagInitial, |
---|

33 | const double& defaultValue) |
---|

34 | { |
---|

35 | bool hasMissingValue = NumTraits<double>::isnan(defaultValue); |
---|

36 | |
---|

37 | if (hasMissingValue) |
---|

38 | { |
---|

39 | int nbLocalIndex = localIndex.size(); |
---|

40 | int currentlocalIndex = 0; |
---|

41 | |
---|

42 | dataOut=std::numeric_limits<double>::quiet_NaN(); |
---|

43 | |
---|

44 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|

45 | { |
---|

46 | currentlocalIndex = localIndex[idx].first; |
---|

47 | if (!NumTraits<double>::isnan(*(dataInput + idx))) |
---|

48 | { |
---|

49 | if (flagInitial[currentlocalIndex]) |
---|

50 | { |
---|

51 | dataOut(currentlocalIndex) = *(dataInput + idx); |
---|

52 | flagInitial[currentlocalIndex] = false; |
---|

53 | } |
---|

54 | else |
---|

55 | { |
---|

56 | dataOut(currentlocalIndex) += *(dataInput + idx); |
---|

57 | } |
---|

58 | } |
---|

59 | } |
---|

60 | } |
---|

61 | else |
---|

62 | { |
---|

63 | int nbLocalIndex = localIndex.size(); |
---|

64 | int currentlocalIndex = 0; |
---|

65 | for (int idx = 0; idx < nbLocalIndex; ++idx) |
---|

66 | { |
---|

67 | currentlocalIndex = localIndex[idx].first; |
---|

68 | if (flagInitial[currentlocalIndex]) |
---|

69 | { |
---|

70 | dataOut(currentlocalIndex) = *(dataInput + idx); |
---|

71 | flagInitial[currentlocalIndex] = false; |
---|

72 | } |
---|

73 | else |
---|

74 | { |
---|

75 | dataOut(currentlocalIndex) += *(dataInput + idx); |
---|

76 | } |
---|

77 | } |
---|

78 | } |
---|

79 | } |
---|

80 | |
---|

81 | } |
---|