Line data Source code
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>
19 88 : DistributedData<T>::DistributedData(const libMesh::Parallel::Communicator & comm_in)
20 88 : : libMesh::ParallelObject(comm_in), _closed(false), _n_local_entries(0)
21 : {
22 88 : }
23 :
24 : template <typename T>
25 : void
26 0 : 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 0 : MooseUtils::linearPartitionItems(n_global_entries,
37 : n_processors(),
38 : processor_id(),
39 : n_local_entries,
40 : local_entry_begin,
41 : local_entry_end);
42 0 : _n_local_entries = n_local_entries;
43 0 : _local_entries.resize(_n_local_entries);
44 0 : _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 0 : for (unsigned int entry_i = local_entry_begin; entry_i < (unsigned int)local_entry_end; ++entry_i)
49 : {
50 0 : _local_entry_ids[entry_i] = entry_i;
51 : }
52 0 : }
53 :
54 : template <typename T>
55 : void
56 172 : DistributedData<T>::addNewEntry(unsigned int glob_i, const T & entry)
57 : {
58 172 : auto it = std::find(_local_entry_ids.begin(), _local_entry_ids.end(), glob_i);
59 172 : if (it != _local_entry_ids.end())
60 0 : ::mooseError("Local object ID (", glob_i, ") already exists!");
61 172 : if (_closed)
62 0 : ::mooseError("DistributeData has already been closed, cannot add new elements!");
63 :
64 172 : _local_entries.push_back(entry);
65 172 : _local_entry_ids.push_back(glob_i);
66 172 : _n_local_entries += 1;
67 172 : }
68 :
69 : template <typename T>
70 : void
71 0 : DistributedData<T>::changeEntry(unsigned int glob_i, const T & entry)
72 : {
73 0 : auto it = std::find(_local_entry_ids.begin(), _local_entry_ids.end(), glob_i);
74 0 : if (it == _local_entry_ids.end())
75 0 : ::mooseError("Local object ID (", glob_i, ") does not exists!");
76 0 : if (_closed)
77 0 : ::mooseError("DistributeData has already been closed, cannot change elements!");
78 :
79 0 : _local_entries[std::distance(_local_entry_ids.begin(), it)] = entry;
80 0 : }
81 :
82 : template <typename T>
83 : const T &
84 66 : DistributedData<T>::getGlobalEntry(unsigned int glob_i) const
85 : {
86 66 : auto it = std::find(_local_entry_ids.begin(), _local_entry_ids.end(), glob_i);
87 66 : if (it == _local_entry_ids.end())
88 0 : ::mooseError("Local object ID (", glob_i, ") does not exists!");
89 :
90 66 : return _local_entries[std::distance(_local_entry_ids.begin(), it)];
91 : }
92 :
93 : template <typename T>
94 : const T &
95 976 : DistributedData<T>::getLocalEntry(unsigned int loc_i) const
96 : {
97 976 : if (loc_i > _n_local_entries - 1)
98 0 : ::mooseError("The requested local index (",
99 : loc_i,
100 : ") is greater than the size (",
101 0 : _n_local_entries,
102 : ") of the locally stored vector!");
103 :
104 976 : return _local_entries[loc_i];
105 : }
106 :
107 : template <typename T>
108 : unsigned int
109 80 : DistributedData<T>::getNumberOfGlobalEntries() const
110 : {
111 80 : unsigned int val = _n_local_entries;
112 80 : _communicator.sum(val);
113 80 : return val;
114 : }
115 :
116 : template <typename T>
117 : bool
118 360 : DistributedData<T>::hasGlobalEntry(unsigned int glob_i) const
119 : {
120 360 : const auto it = std::find(_local_entry_ids.begin(), _local_entry_ids.end(), glob_i);
121 360 : if (it != _local_entry_ids.end())
122 96 : return true;
123 :
124 : return false;
125 : }
126 :
127 : template <typename T>
128 : unsigned int
129 0 : DistributedData<T>::getLocalIndex(unsigned int glob_i) const
130 : {
131 0 : const auto it = std::find(_local_entry_ids.begin(), _local_entry_ids.end(), glob_i);
132 0 : if (it == _local_entry_ids.end())
133 0 : ::mooseError("Local object ID (", glob_i, ") does not exists!");
134 :
135 0 : return std::distance(_local_entry_ids.begin(), it);
136 : }
137 :
138 : template <typename T>
139 : unsigned int
140 720 : DistributedData<T>::getGlobalIndex(unsigned int loc_i) const
141 : {
142 720 : if (loc_i > _n_local_entries - 1)
143 0 : ::mooseError("The requested local index (",
144 : loc_i,
145 : ") is greater than the size (",
146 0 : _n_local_entries,
147 : ") of the locally stored vector!");
148 :
149 720 : return _local_entry_ids[loc_i];
150 : }
151 :
152 : // Explicit instantiation of types that are necessary.
153 : template class DistributedData<DenseVector<Real>>;
154 : template class DistributedData<std::shared_ptr<DenseVector<Real>>>;
155 : template class DistributedData<std::vector<Real>>;
156 :
157 : } // StochasticTools namespace
|