OGS
ParallelVectorMatrixAssembler.cpp
Go to the documentation of this file.
1
12
13#include <cstdlib>
14#include <fstream>
15#include <range/v3/view/iota.hpp>
16#include <vector>
17
18#include "BaseLib/StringTools.h"
25
26namespace
27{
29 const std::size_t mesh_item_id,
31 const NumLib::LocalToGlobalIndexMap& dof_table, const double t,
32 const double dt, const GlobalVector& x, const GlobalVector& x_prev,
33 std::vector<double>& local_b_data, std::vector<double>& local_Jac_data,
34 ProcessLib::AbstractJacobianAssembler& jacobian_assembler,
36{
37 std::vector<GlobalIndexType> const& indices =
38 NumLib::getIndices(mesh_item_id, dof_table);
39
40 local_b_data.clear();
41 local_Jac_data.clear();
42
43 auto const local_x = x.get(indices);
44 auto const local_x_prev = x_prev.get(indices);
45 jacobian_assembler.assembleWithJacobian(local_assembler, t, dt, local_x,
46 local_x_prev, local_b_data,
47 local_Jac_data);
48
49 if (local_Jac_data.empty())
50 {
52 "No Jacobian has been assembled! This might be due to "
53 "programming errors in the local assembler of the "
54 "current process.");
55 }
56
57 cache.add(local_b_data, local_Jac_data, indices);
58}
59
61 const std::size_t mesh_item_id,
63 std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_tables,
64 const double t, const double dt, std::vector<GlobalVector*> const& x,
65 std::vector<GlobalVector*> const& x_prev, int const process_id,
66 std::vector<double>& local_b_data, std::vector<double>& local_Jac_data,
67 ProcessLib::AbstractJacobianAssembler& jacobian_assembler,
69{
70 std::vector<std::vector<GlobalIndexType>> indices_of_processes;
71 indices_of_processes.reserve(dof_tables.size());
72 transform(cbegin(dof_tables), cend(dof_tables),
73 back_inserter(indices_of_processes),
74 [&](auto const* dof_table)
75 { return NumLib::getIndices(mesh_item_id, *dof_table); });
76
77 auto local_coupled_xs =
78 ProcessLib::getCoupledLocalSolutions(x, indices_of_processes);
79 auto const local_x = MathLib::toVector(local_coupled_xs);
80
81 auto local_coupled_x_prevs =
82 ProcessLib::getCoupledLocalSolutions(x_prev, indices_of_processes);
83 auto const local_x_prev = MathLib::toVector(local_coupled_x_prevs);
84
85 std::vector<GlobalIndexType> const& indices =
86 indices_of_processes[process_id];
87
88 local_b_data.clear();
89 local_Jac_data.clear();
90
92 local_assembler, t, dt, local_x, local_x_prev, process_id, local_b_data,
93 local_Jac_data);
94
95 if (local_Jac_data.empty())
96 {
98 "No Jacobian has been assembled! This might be due to "
99 "programming errors in the local assembler of the "
100 "current process.");
101 }
102
103 cache.add(local_b_data, local_Jac_data, indices);
104}
105
107std::vector<
108 std::tuple<std::ptrdiff_t,
109 std::reference_wrapper<ProcessLib::LocalAssemblerInterface>>>
112 ProcessLib::LocalAssemblerInterface> const& local_assemblers,
113 std::vector<std::size_t> const& active_elements)
114{
115 auto create_ids_asm_pairs = [&](auto const& element_ids)
116 {
117 std::vector<std::tuple<
118 std::ptrdiff_t,
119 std::reference_wrapper<ProcessLib::LocalAssemblerInterface>>>
120 result;
121 result.reserve(static_cast<std::size_t>(element_ids.size()));
122 for (auto const id : element_ids)
123 {
124 result.push_back({id, local_assemblers[id]});
125 }
126 return result;
127 };
128
129 if (active_elements.empty())
130 {
131 return create_ids_asm_pairs(ranges::views::iota(
132 static_cast<std::size_t>(0), local_assemblers.size()));
133 }
134 return create_ids_asm_pairs(active_elements);
135}
136
138 std::vector<std::tuple<
139 std::ptrdiff_t,
140 std::reference_wrapper<ProcessLib::LocalAssemblerInterface>>> const&
141 ids_local_assemblers,
142 ThreadException& exception,
143 auto local_matrix_output,
144 auto assemble)
145{
146 std::ptrdiff_t n_elements =
147 static_cast<std::ptrdiff_t>(ids_local_assemblers.size());
148
149#pragma omp for nowait
150 for (std::ptrdiff_t i = 0; i < n_elements; ++i)
151 {
152 if (exception)
153 {
154 continue;
155 }
156 auto [element_id, loc_asm] = ids_local_assemblers[i];
157
158 try
159 {
160 assemble(element_id, loc_asm);
161 }
162 catch (...)
163 {
164 exception.capture();
165 continue;
166 }
167
168 local_matrix_output(element_id);
169 }
170}
171
173{
174 char const* const num_threads_env = std::getenv("OGS_ASM_THREADS");
175
176 if (!num_threads_env)
177 {
178 return 1;
179 }
180
181 if (std::strlen(num_threads_env) == 0)
182 {
183 OGS_FATAL("The environment variable OGS_ASM_THREADS is set but empty.");
184 }
185
186 std::string num_threads_str{num_threads_env};
187 BaseLib::trim(num_threads_str);
188
189 std::istringstream num_threads_iss{num_threads_str};
190 int num_threads = -1;
191
192 num_threads_iss >> num_threads;
193
194 if (!num_threads_iss)
195 {
196 OGS_FATAL("Error parsing OGS_ASM_THREADS (= \"{}\").", num_threads_env);
197 }
198
199 if (!num_threads_iss.eof())
200 {
201 OGS_FATAL(
202 "Error parsing OGS_ASM_THREADS (= \"{}\"): not read entirely, the "
203 "remainder is \"{}\"",
204 num_threads_env,
205 num_threads_iss.str().substr(num_threads_iss.tellg()));
206 }
207
208 if (num_threads < 1)
209 {
210 OGS_FATAL(
211 "You asked (via OGS_ASM_THREADS) to assemble with {} < 1 thread.",
212 num_threads);
213 }
214
215 return num_threads;
216}
217} // namespace
218
219namespace ProcessLib::Assembly
220{
222 AbstractJacobianAssembler& jacobian_assembler)
223 : jacobian_assembler_{jacobian_assembler},
224 num_threads_(getNumberOfThreads())
225{
226}
227
230 LocalAssemblerInterface> const& local_assemblers,
231 std::vector<std::size_t> const& active_elements,
232 std::vector<NumLib::LocalToGlobalIndexMap const*> const& dof_tables,
233 const double t, double const dt, std::vector<GlobalVector*> const& xs,
234 std::vector<GlobalVector*> const& x_prevs, int const process_id,
235 GlobalVector& b, GlobalMatrix& Jac)
236{
237 // checks //////////////////////////////////////////////////////////////////
238 if (dof_tables.size() != xs.size())
239 {
240 OGS_FATAL("Different number of DOF tables and solution vectors.");
241 }
242
243 std::size_t const number_of_processes = xs.size();
244 // algorithm ///////////////////////////////////////////////////////////////
245
247 ConcurrentMatrixView b_view(b);
248 ConcurrentMatrixView Jac_view(Jac);
249
250 ThreadException exception;
251#pragma omp parallel num_threads(num_threads_)
252 {
253#ifdef _OPENMP
254#pragma omp single nowait
255 {
256 INFO("Number of threads: {}", omp_get_num_threads());
257 }
258#endif
259
260 // temporary data only stored here in order to avoid frequent memory
261 // reallocations.
262 std::vector<double> local_b_data;
263 std::vector<double> local_Jac_data;
264
265 // copy to avoid concurrent access
266 auto const jac_asm = jacobian_assembler_.copy();
267 auto stats_this_thread = stats->clone();
268
269 MultiMatrixElementCache cache{b_view, Jac_view,
270 stats_this_thread->data};
271
272 auto local_matrix_output = [&](std::ptrdiff_t element_id)
273 {
274 local_matrix_output_(t, process_id, element_id, local_b_data,
275 local_Jac_data);
276 };
277
278 // TODO corner case: what if all elements on a submesh are deactivated?
279
280 // Monolithic scheme
281 if (number_of_processes == 1)
282 {
283 assert(process_id == 0);
284 auto const& dof_table = *dof_tables[0];
285 auto const& x = *xs[0];
286 auto const& x_prev = *x_prevs[0];
287
288 runAssembly(
289 collectActiveLocalAssemblers(local_assemblers, active_elements),
290 exception, local_matrix_output,
291 [&](auto element_id, auto& loc_asm)
292 {
293 assembleWithJacobianOneElement(
294 element_id, loc_asm, dof_table, t, dt, x, x_prev,
295 local_b_data, local_Jac_data, *jac_asm, cache);
296 });
297 }
298 else // Staggered scheme
299 {
300 runAssembly(
301 collectActiveLocalAssemblers(local_assemblers, active_elements),
302 exception, local_matrix_output,
303 [&](auto element_id, auto& loc_asm)
304 {
305 assembleWithJacobianForStaggeredSchemeOneElement(
306 element_id, loc_asm, dof_tables, t, dt, xs, x_prevs,
307 process_id, local_b_data, local_Jac_data, *jac_asm,
308 cache);
309 });
310 }
311 }
312
313 stats->print();
314
315 global_matrix_output_(t, process_id, b, Jac);
316 exception.rethrow();
317}
318} // namespace ProcessLib::Assembly
#define OGS_FATAL(...)
Definition Error.h:26
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
Definition of string helper functions.
Global vector based on Eigen vector.
Definition EigenVector.h:25
double get(IndexType rowId) const
get entry
Definition EigenVector.h:58
Base class for Jacobian assemblers.
virtual void assembleWithJacobian(LocalAssemblerInterface &local_assembler, double const t, double const dt, std::vector< double > const &local_x, std::vector< double > const &local_x_prev, std::vector< double > &local_b_data, std::vector< double > &local_Jac_data)=0
virtual std::unique_ptr< AbstractJacobianAssembler > copy() const =0
virtual void assembleWithJacobianForStaggeredScheme(LocalAssemblerInterface &, double const, double const, Eigen::VectorXd const &, Eigen::VectorXd const &, int const, std::vector< double > &, std::vector< double > &)
static std::shared_ptr< CumulativeStats< Data > > create()
void add(std::vector< double > const &local_b_data, std::vector< double > const &local_Jac_data, std::vector< GlobalIndexType > const &indices)
ParallelVectorMatrixAssembler(AbstractJacobianAssembler &jacobian_assembler)
void assembleWithJacobian(BaseLib::PolymorphicRandomAccessContainerView< LocalAssemblerInterface > const &local_assemblers, std::vector< std::size_t > const &active_elements, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_tables, const double t, double const dt, std::vector< GlobalVector * > const &xs, std::vector< GlobalVector * > const &x_prevs, int const process_id, GlobalVector &b, GlobalMatrix &Jac)
void trim(std::string &str, char ch)
Eigen::Map< const Vector > toVector(std::vector< double > const &data, Eigen::VectorXd::Index size)
Creates an Eigen mapped vector from the given data vector.
std::vector< GlobalIndexType > getIndices(std::size_t const mesh_item_id, NumLib::LocalToGlobalIndexMap const &dof_table)
std::vector< double > getCoupledLocalSolutions(std::vector< GlobalVector * > const &global_solutions, std::vector< std::vector< GlobalIndexType > > const &indices)
std::vector< std::tuple< std::ptrdiff_t, std::reference_wrapper< ProcessLib::LocalAssemblerInterface > > > collectActiveLocalAssemblers(BaseLib::PolymorphicRandomAccessContainerView< ProcessLib::LocalAssemblerInterface > const &local_assemblers, std::vector< std::size_t > const &active_elements)
Returns a vector of active element ids with corresponding local assemblers.
void assembleWithJacobianOneElement(const std::size_t mesh_item_id, ProcessLib::LocalAssemblerInterface &local_assembler, const NumLib::LocalToGlobalIndexMap &dof_table, const double t, const double dt, const GlobalVector &x, const GlobalVector &x_prev, std::vector< double > &local_b_data, std::vector< double > &local_Jac_data, ProcessLib::AbstractJacobianAssembler &jacobian_assembler, ProcessLib::Assembly::MultiMatrixElementCache &cache)
void assembleWithJacobianForStaggeredSchemeOneElement(const std::size_t mesh_item_id, ProcessLib::LocalAssemblerInterface &local_assembler, std::vector< NumLib::LocalToGlobalIndexMap const * > const &dof_tables, const double t, const double dt, std::vector< GlobalVector * > const &x, std::vector< GlobalVector * > const &x_prev, int const process_id, std::vector< double > &local_b_data, std::vector< double > &local_Jac_data, ProcessLib::AbstractJacobianAssembler &jacobian_assembler, ProcessLib::Assembly::MultiMatrixElementCache &cache)
void runAssembly(std::vector< std::tuple< std::ptrdiff_t, std::reference_wrapper< ProcessLib::LocalAssemblerInterface > > > const &ids_local_assemblers, ThreadException &exception, auto local_matrix_output, auto assemble)