https://mooseframework.inl.gov
DistributedData.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "DistributedData.h"
11 #include "MooseUtils.h"
12 #include "MooseError.h"
13 #include "libmesh/dense_vector.h"
14 
15 namespace StochasticTools
16 {
17 
18 template <typename T>
20  : libMesh::ParallelObject(comm_in), _closed(false), _n_local_entries(0)
21 {
22 }
23 
24 template <typename T>
25 void
26 DistributedData<T>::initializeContainer(unsigned int n_global_entries)
27 {
28  // This function can be used when a linear partitioning is required and the
29  // number of global samples is known in advance. We must temporarily
30  // use dof_id_type for the last three args (pass by reference),
31  // but will cast back to unsigned int later
32  dof_id_type local_entry_begin;
33  dof_id_type local_entry_end;
34  dof_id_type n_local_entries;
35 
36  MooseUtils::linearPartitionItems(n_global_entries,
37  n_processors(),
38  processor_id(),
39  n_local_entries,
40  local_entry_begin,
41  local_entry_end);
42  _n_local_entries = n_local_entries;
43  _local_entries.resize(_n_local_entries);
44  _local_entry_ids.resize(_n_local_entries);
45 
46  // Filling the sample ID vector, leaving the elements of the sample vector
47  // with the default constructor.
48  for (unsigned int entry_i = local_entry_begin; entry_i < (unsigned int)local_entry_end; ++entry_i)
49  {
50  _local_entry_ids[entry_i] = entry_i;
51  }
52 }
53 
54 template <typename T>
55 void
56 DistributedData<T>::addNewEntry(unsigned int glob_i, const T & entry)
57 {
58  auto it = std::find(_local_entry_ids.begin(), _local_entry_ids.end(), glob_i);
59  if (it != _local_entry_ids.end())
60  ::mooseError("Local object ID (", glob_i, ") already exists!");
61  if (_closed)
62  ::mooseError("DistributeData has already been closed, cannot add new elements!");
63 
64  _local_entries.push_back(entry);
65  _local_entry_ids.push_back(glob_i);
66  _n_local_entries += 1;
67 }
68 
69 template <typename T>
70 void
71 DistributedData<T>::changeEntry(unsigned int glob_i, const T & entry)
72 {
73  auto it = std::find(_local_entry_ids.begin(), _local_entry_ids.end(), glob_i);
74  if (it == _local_entry_ids.end())
75  ::mooseError("Local object ID (", glob_i, ") does not exists!");
76  if (_closed)
77  ::mooseError("DistributeData has already been closed, cannot change elements!");
78 
79  _local_entries[std::distance(_local_entry_ids.begin(), it)] = entry;
80 }
81 
82 template <typename T>
83 const T &
84 DistributedData<T>::getGlobalEntry(unsigned int glob_i) const
85 {
86  auto it = std::find(_local_entry_ids.begin(), _local_entry_ids.end(), glob_i);
87  if (it == _local_entry_ids.end())
88  ::mooseError("Local object ID (", glob_i, ") does not exists!");
89 
90  return _local_entries[std::distance(_local_entry_ids.begin(), it)];
91 }
92 
93 template <typename T>
94 const T &
95 DistributedData<T>::getLocalEntry(unsigned int loc_i) const
96 {
97  if (loc_i > _n_local_entries - 1)
98  ::mooseError("The requested local index (",
99  loc_i,
100  ") is greater than the size (",
101  _n_local_entries,
102  ") of the locally stored vector!");
103 
104  return _local_entries[loc_i];
105 }
106 
107 template <typename T>
108 unsigned int
110 {
111  unsigned int val = _n_local_entries;
112  _communicator.sum(val);
113  return val;
114 }
115 
116 template <typename T>
117 bool
118 DistributedData<T>::hasGlobalEntry(unsigned int glob_i) const
119 {
120  const auto it = std::find(_local_entry_ids.begin(), _local_entry_ids.end(), glob_i);
121  if (it != _local_entry_ids.end())
122  return true;
123 
124  return false;
125 }
126 
127 template <typename T>
128 unsigned int
129 DistributedData<T>::getLocalIndex(unsigned int glob_i) const
130 {
131  const auto it = std::find(_local_entry_ids.begin(), _local_entry_ids.end(), glob_i);
132  if (it == _local_entry_ids.end())
133  ::mooseError("Local object ID (", glob_i, ") does not exists!");
134 
135  return std::distance(_local_entry_ids.begin(), it);
136 }
137 
138 template <typename T>
139 unsigned int
140 DistributedData<T>::getGlobalIndex(unsigned int loc_i) const
141 {
142  if (loc_i > _n_local_entries - 1)
143  ::mooseError("The requested local index (",
144  loc_i,
145  ") is greater than the size (",
146  _n_local_entries,
147  ") of the locally stored vector!");
148 
149  return _local_entry_ids[loc_i];
150 }
151 
152 // Explicit instantiation of types that are necessary.
153 template class DistributedData<DenseVector<Real>>;
155 template class DistributedData<std::vector<Real>>;
156 
157 } // StochasticTools namespace
void mooseError(Args &&... args)
DistributedData(const libMesh::Parallel::Communicator &comm_in)
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
void linearPartitionItems(dof_id_type num_items, dof_id_type num_chunks, dof_id_type chunk_id, dof_id_type &num_local_items, dof_id_type &local_items_begin, dof_id_type &local_items_end)
unsigned int getLocalIndex(unsigned int glob_i) const
Getting the local index of a global sample if locally owned.
unsigned int getNumberOfGlobalEntries() const
Getting the number of global samples.
const T & getGlobalEntry(unsigned int glob_i) const
Getting a sample using its global index.
Enum for batch type in stochastic tools MultiApp.
bool hasGlobalEntry(unsigned int glob_i) const
Checking of sample with global ID is locally owned ot not.
void changeEntry(unsigned int glob_i, const T &sample)
Changing a sample with a global index if it is owned locally.
const T & getLocalEntry(unsigned int loc_i) const
Getting a sample using its local index.
void initializeContainer(unsigned int num_global_entries)
Initialize the container with a given number of samples.
unsigned int getGlobalIndex(unsigned int loc_i) const
Getting the global index of a locally owned sample.
Templated class that specifies a distributed storage of a vector of given objects.
void ErrorVector unsigned int
uint8_t dof_id_type
void addNewEntry(unsigned int glob_i, const T &sample)
Adding a new sample locally with a global index.