UFO
ParallelObsDistribution.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2020 Met Office UK
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 
9 #include "oops/mpi/mpi.h"
10 
11 namespace ufo {
12 
13 ParallelObsDistribution::ParallelObsDistribution(const ioda::ObsSpace &obsspace)
14  : globalObsCount_(obsspace.gnlocs())
15 {
16  const size_t numProcs = obsspace.comm().size();
17 
18  localObsCounts_.resize(numProcs);
19  obsspace.comm().allGather(static_cast<int>(obsspace.nlocs()),
20  localObsCounts_.begin(), localObsCounts_.end());
21 
22  localObsIdDisplacements_.resize(numProcs);
24  for (size_t i = 1; i < numProcs; ++i)
26 }
27 
28 // Generic implementation
29 template <typename T>
30 std::vector<T> getGlobalVariableValues(const ioda::ObsSpace &obsspace,
31  const ParallelObsDistribution &obsDistribution,
32  const std::string &variable,
33  const std::string &group) {
34  std::vector<T> localValues(obsspace.nlocs());
35  obsspace.get_db(group, variable, localValues);
36 
37  std::vector<T> globalValues(obsDistribution.globalObsCount());
38  obsspace.comm().allGatherv(localValues.begin(), localValues.end(),
39  globalValues.begin(),
40  obsDistribution.localObsCounts().data(),
41  obsDistribution.localObsIdDisplacements().data());
42  return globalValues;
43 }
44 
45 // Specialisation for date/time variables.
46 template <>
47 std::vector<util::DateTime> getGlobalVariableValues(const ioda::ObsSpace &obsspace,
48  const ParallelObsDistribution &obsDistribution,
49  const std::string &variable,
50  const std::string &group) {
51  std::vector<util::DateTime> localValues(obsspace.nlocs());
52  obsspace.get_db(group, variable, localValues);
53 
54  std::vector<util::DateTime> globalValues(obsDistribution.globalObsCount());
55  oops::mpi::allGathervUsingSerialize(obsspace.comm(),
56  localValues.begin(), localValues.end(), globalValues.begin());
57  return globalValues;
58 }
59 
60 // Explicit instantiations for the variable types supported by ioda::ObsSpace.
61 #define INSTANTIATE_GET_GLOBAL_VARIABLE_VALUES(TYPE) \
62  template std::vector<TYPE> getGlobalVariableValues(const ioda::ObsSpace &, \
63  const ParallelObsDistribution &, \
64  const std::string &, \
65  const std::string &)
69 // It's unnecessary to instantiate the template function for T = util::DateTime, since a
70 // specialization exists for that value of T.
71 
72 } // namespace ufo
ufo::ParallelObsDistribution::ParallelObsDistribution
ParallelObsDistribution(const ioda::ObsSpace &obsspace)
Construct an object describing the distribution of observations in obsspace across MPI processes.
Definition: ParallelObsDistribution.cc:13
ufo::ParallelObsDistribution
Describes how observations in an ObsSpace are distributed across MPI processes.
Definition: src/ufo/utils/ParallelObsDistribution.h:19
ufo::INSTANTIATE_GET_GLOBAL_VARIABLE_VALUES
INSTANTIATE_GET_GLOBAL_VARIABLE_VALUES(int)
ufo::ParallelObsDistribution::globalObsCount
size_t globalObsCount() const
Return the total number of observations held by all MPI processes.
Definition: src/ufo/utils/ParallelObsDistribution.h:26
ufo::getGlobalVariableValues
std::vector< T > getGlobalVariableValues(const ioda::ObsSpace &obsspace, const ParallelObsDistribution &obsDistribution, const std::string &variable, const std::string &group)
Definition: ParallelObsDistribution.cc:30
ufo::ParallelObsDistribution::localObsIdDisplacements_
std::vector< int > localObsIdDisplacements_
Definition: src/ufo/utils/ParallelObsDistribution.h:45
ufo
Definition: RunCRTM.h:27
ufo::ParallelObsDistribution::localObsCounts
const std::vector< int > & localObsCounts() const
Return a vector whose ith element is the number of observations held by the MPI process with rank i.
Definition: src/ufo/utils/ParallelObsDistribution.h:33
ufo::ParallelObsDistribution::localObsIdDisplacements
const std::vector< int > & localObsIdDisplacements() const
Return a vector whose ith element is the total number of observations held by the MPI processes with ...
Definition: src/ufo/utils/ParallelObsDistribution.h:40
ParallelObsDistribution.h
ufo::ParallelObsDistribution::localObsCounts_
std::vector< int > localObsCounts_
Definition: src/ufo/utils/ParallelObsDistribution.h:44