IODA
PairOfDistributionsAccumulator.h
Go to the documentation of this file.
1 /*
2  * (C) Crown copyright 2021, Met Office
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #ifndef DISTRIBUTION_PAIROFDISTRIBUTIONSACCUMULATOR_H_
9 #define DISTRIBUTION_PAIROFDISTRIBUTIONSACCUMULATOR_H_
10 
11 #include <memory>
12 #include <vector>
13 
14 #include "ioda/distribution/Accumulator.h"
15 
16 namespace ioda {
17 
18 /// \brief Implementation of the Accumulator interface suitable for the
19 /// PairOfDistributions.
20 template <typename T>
22  public:
23  PairOfDistributionsAccumulator(std::unique_ptr<Accumulator<T>> firstAccumulator,
24  std::unique_ptr<Accumulator<T>> secondAccumulator,
25  std::size_t firstNumLocs)
26  : firstAccumulator_(std::move(firstAccumulator)),
27  secondAccumulator_(std::move(secondAccumulator)),
28  firstNumLocs_(firstNumLocs)
29  {}
30 
31  void addTerm(std::size_t loc, const T &term) override {
32  if (loc < firstNumLocs_)
33  firstAccumulator_->addTerm(loc, term);
34  else
35  secondAccumulator_->addTerm(loc - firstNumLocs_, term);
36  }
37 
38  T computeResult() const override {
39  return firstAccumulator_->computeResult() + secondAccumulator_->computeResult();
40  }
41 
42  private:
43  std::unique_ptr<Accumulator<T>> firstAccumulator_;
44  std::unique_ptr<Accumulator<T>> secondAccumulator_;
45  std::size_t firstNumLocs_;
46 };
47 
48 template <typename T>
49 class PairOfDistributionsAccumulator<std::vector<T>> : public Accumulator<std::vector<T>> {
50  public:
52  std::unique_ptr<Accumulator<std::vector<T>>> firstAccumulator,
53  std::unique_ptr<Accumulator<std::vector<T>>> secondAccumulator,
54  std::size_t firstNumLocs)
55  : firstAccumulator_(std::move(firstAccumulator)),
56  secondAccumulator_(std::move(secondAccumulator)),
57  firstNumLocs_(firstNumLocs)
58  {}
59 
60  void addTerm(std::size_t loc, const std::vector<T> &term) override {
61  if (loc < firstNumLocs_)
62  firstAccumulator_->addTerm(loc, term);
63  else
64  secondAccumulator_->addTerm(loc - firstNumLocs_, term);
65  }
66 
67  void addTerm(std::size_t loc, std::size_t item, const T &term) override {
68  if (loc < firstNumLocs_)
69  firstAccumulator_->addTerm(loc, item, term);
70  else
71  secondAccumulator_->addTerm(loc - firstNumLocs_, item, term);
72  }
73 
74  std::vector<T> computeResult() const override {
75  std::vector<T> result = firstAccumulator_->computeResult();
76  std::vector<T> secondResult = secondAccumulator_->computeResult();
77  for (std::size_t i = 0, n = result.size(); i < n; ++i)
78  result[i] += secondResult[i];
79  return result;
80  }
81 
82  private:
83  std::unique_ptr<Accumulator<std::vector<T>>> firstAccumulator_;
84  std::unique_ptr<Accumulator<std::vector<T>>> secondAccumulator_;
85  std::size_t firstNumLocs_;
86 };
87 
88 } // namespace ioda
89 
90 #endif // DISTRIBUTION_PAIROFDISTRIBUTIONSACCUMULATOR_H_
Calculates the sum of a location-dependent quantity of type T over locations held on all PEs,...
Definition: Accumulator.h:27
std::unique_ptr< Accumulator< std::vector< T > > > firstAccumulator_
std::vector< T > computeResult() const override
Return the sums of contributions associated with locations held on all PEs (each taken into account o...
void addTerm(std::size_t loc, std::size_t item, const T &term) override
Increment the ith sum with the contribution term of location loc held on the current PE.
void addTerm(std::size_t loc, const std::vector< T > &term) override
Increment each sum with the contribution of location loc (held on the current PE) taken from the corr...
PairOfDistributionsAccumulator(std::unique_ptr< Accumulator< std::vector< T >>> firstAccumulator, std::unique_ptr< Accumulator< std::vector< T >>> secondAccumulator, std::size_t firstNumLocs)
std::unique_ptr< Accumulator< std::vector< T > > > secondAccumulator_
Implementation of the Accumulator interface suitable for the PairOfDistributions.
T computeResult() const override
Return the sum of contributions associated with locations held on all PEs (each taken into account on...
PairOfDistributionsAccumulator(std::unique_ptr< Accumulator< T >> firstAccumulator, std::unique_ptr< Accumulator< T >> secondAccumulator, std::size_t firstNumLocs)
void addTerm(std::size_t loc, const T &term) override
Increment the sum with the contribution term of location loc held on the current PE.
std::unique_ptr< Accumulator< T > > firstAccumulator_
std::unique_ptr< Accumulator< T > > secondAccumulator_