Nektar++
Loading...
Searching...
No Matches
AssmbScatrOp.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: AssmbScatrOp.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35/**
36 * @file AssmbScatrOp.hpp
37 * @brief Public interface for the assemble-scatter operator, which makes
38 * element-wise coefficient storage globally consistent, and for its
39 * Dirichlet-zeroing and sign-free variants.
40 *
41 * @details
42 * A coefficient-space Field stores each element's coefficients
43 * separately, so a global degree of freedom shared by several elements
44 * (or, in parallel, by several ranks) exists as multiple local copies.
45 * Solvers for continuous (C0) discretisations repeatedly need those
46 * copies summed and made consistent, which is done in two phases:
47 *
48 * - **Assemble**: sum the local contributions to every global degree
49 * of freedom, each weighted by its orientation sign;
50 * - **Scatter**: write the summed value, weighted by each copy's
51 * orientation sign, back to every local copy.
52 *
53 * AssmbScatrOp is the abstract, execution-space-agnostic interface to
54 * this operation. Concrete implementations are provided by
55 * detail::AssmbScatrOpImpl (AssmbScatrOpImpl.hpp), built on the kernels
56 * in AssmbScatrDeviceKernels.hpp and AssmbScatrSerialAVXKernels.hpp,
57 * and are registered with the operator factory by translation units
58 * that CMake generates from AssmbScatrFactoryDec.cpp.in. When run in
59 * parallel, the implementation additionally exchanges partition-
60 * boundary contributions between ranks, overlapping that communication
61 * with the assembly of the purely interior degrees of freedom.
62 *
63 * AssmbScatrZeroDirOp and AssmbScatrNoSignOp, declared below, are the
64 * factory-facing classes of the variants whose result has the global
65 * Dirichlet boundary degrees of freedom zeroed, and which sum without the
66 * orientation signs; their implementations are registered from the same
67 * generated translation units.
68 */
69
70#pragma once
71
73
75
77{
78
79/**
80 * @brief Base class for the assemble-scatter operator: sums the local
81 * contributions to every global degree of freedom and writes the sum
82 * back to each contributing local coefficient.
83 *
84 * The operator acts on local (element-wise) coefficient storage. After
85 * Apply(), every local copy of a shared degree of freedom holds the
86 * fully assembled value in its own sign convention. In the
87 * factory-registered implementations each contribution and each
88 * write-back is weighted by the orientation sign defined in the
89 * local-to-global assembly map; for a ContField the behaviour is then
90 * equivalent to AssemblyMap::Assemble() followed by
91 * AssemblyMap::GlobalToLocal(). Overrides may adopt a different sign
92 * convention: AssmbScatrNoSignOp sums with every orientation sign
93 * replaced by its absolute value.
94 *
95 * As with the other operator base classes in this library, this class
96 * exists to fix the Apply() parameter types: both overloads accept
97 * only coefficient-space fields (FieldState::Coeff), enforced at
98 * compile time. The per-execution-space implementations
99 * (detail::AssmbScatrOpImpl) override v_Apply() and are obtained
100 * through Create(). The derived AssmbScatrZeroDirOp performs the same
101 * operation but with the global Dirichlet boundary degrees of freedom
102 * zeroed.
103 *
104 * @tparam TData Floating-point type of the field data.
105 */
106template <typename TData> class AssmbScatrOp : public Operator<TData>
107{
108public:
109 /**
110 * @brief Create an assemble-scatter operator through the operator
111 * factory.
112 *
113 * The factory key is #name followed by the execution-space string,
114 * so this returns whichever implementation was registered for that
115 * execution space (e.g. "Serial", "AVX", "Device").
116 *
117 * @param expansionList Expansion list the operator acts on. In
118 * parallel runs this must be a
119 * MultiRegions::ContField, since the
120 * implementation derives its inter-rank
121 * assembly communication from the field's
122 * assembly map.
123 * @param components Names of the field components (solution
124 * variables); used to key the assembly-map
125 * data cached in the data warehouse.
126 * @param execStr Execution-space part of the factory key.
127 * If empty, the session's "opExecSpace"
128 * command-line argument is used instead;
129 * without it the operator takes the space
130 * the build provides and warns.
131 *
132 * @return The newly created operator. Creation raises a fatal
133 * error (throws ErrorUtil::NekError) if no implementation is
134 * registered under the requested key.
135 */
136 static std::shared_ptr<AssmbScatrOp<TData>> Create(
137 const MultiRegions::ExpListSharedPtr &expansionList,
138 const std::vector<std::string> &components,
139 const std::string &execStr = "")
140 {
141 return Operator<TData>::template Create<AssmbScatrOp>(
142 expansionList, components, execStr);
143 }
144
145 /// Operator base name; Create() appends the execution space to it
146 /// to form the factory key.
147 static inline const std::string name = "AssmbScatr";
148
149 /**
150 * @brief Assemble and scatter @p inout in place.
151 *
152 * On return every local coefficient of @p inout holds the assembled
153 * sum of all local contributions to its global degree of freedom,
154 * weighted by its own orientation sign under the implementation's
155 * sign convention (see the class description).
156 *
157 * @param inout Coefficient-space field, overwritten with its
158 * assembled values.
159 */
161 {
162 v_Apply(inout);
163 }
164
165 /**
166 * @brief Assemble and scatter @p in, writing the result to @p out.
167 *
168 * The two fields must have the same block sizes and number of
169 * components. They may refer to the same object, in which case the
170 * operation is performed in place.
171 *
172 * @param in Coefficient-space input field; left unmodified
173 * unless it is the same object as @p out.
174 * @param out Coefficient-space output field receiving the
175 * assembled values.
176 */
182
183 /// @brief Call operator; equivalent to the in-place Apply().
188
189 /// @brief Call operator; equivalent to the two-field Apply().
195
196protected:
197 /**
198 * @brief Construct the interface part of a concrete implementation;
199 * called by the factory-registered creator functions.
200 *
201 * @param expansionList Expansion list the operator acts on.
202 * @param components Component names the operator is set up
203 * for.
204 */
206 const std::vector<std::string> &components)
207 : Operator<TData>(expansionList, components)
208 {
209 }
210
211 ~AssmbScatrOp() override = default;
212
213 /**
214 * @brief Implementation hook for the in-place Apply().
215 *
216 * Overrides must replace every local coefficient of @p inout, for
217 * every component, with the sum of all local contributions to its
218 * global degree of freedom, weighted by that coefficient's own
219 * orientation sign under whichever sign convention the override
220 * implements (see the class description), and must restore the
221 * field's interleaved storage layout if they change it internally.
222 *
223 * @param inout Coefficient-space field to assemble and scatter
224 * in place.
225 */
226 virtual void v_Apply(
228
229 /**
230 * @brief Implementation hook for the two-field Apply().
231 *
232 * Overrides must write the assembled and scattered form of @p in
233 * to @p out, accept @p in and @p out referring to the same field,
234 * and leave @p in unmodified when the two are distinct.
235 *
236 * @param in Coefficient-space input field.
237 * @param out Coefficient-space output field.
238 */
239 virtual void v_Apply(
242};
243
244/**
245 * @brief Assemble-scatter operator that also zeroes the global Dirichlet
246 * boundary degrees of freedom.
247 *
248 * Implementations perform the same in-place assemble-scatter as
249 * AssmbScatrOp, whose Apply() overloads this class inherits: for every
250 * global degree of freedom (DOF), all local coefficients mapping to it
251 * are summed with the +-1 sign factors held by the assembly map, and the
252 * sum is written back to each of them, weighted by each copy's
253 * orientation sign. When running on more than one rank,
254 * partition-boundary contributions are exchanged as well.
255 *
256 * The difference lies in the global Dirichlet boundary DOFs (per
257 * component, the leading AssemblyMapCG::GetNumGlobalDirBndCoeffs()
258 * entries of the global numbering): their assembly-table entries carry a
259 * sign of zero, so their contributions are discarded and every local
260 * coefficient mapping to such a DOF is overwritten with zero. The result
261 * is equivalent to AssemblyMap::Assemble(), zeroing the Dirichlet block
262 * of the global vector, then AssemblyMap::GlobalToLocal().
263 *
264 * A plain assemble-scatter is the identity on DOFs with a single local
265 * contribution, so AssmbScatrOp's tables omit them; this variant's tables
266 * additionally include single-contribution Dirichlet DOFs so that they
267 * are zeroed too. The ZERODIR flag in the data-warehouse keys selects
268 * these enlarged tables (see detail::AssmbScatrOpImpl::SetUpMaps).
269 *
270 * The class itself carries no implementation: it fixes the factory name
271 * under which detail::AssmbScatrZeroDirOpImpl (AssmbScatrOpImpl with
272 * ZERODIR = true) is registered from AssmbScatrFactoryDec.cpp.in. The
273 * iterative solvers in GlobalLinSysOps obtain it through Create() and
274 * apply it to their residuals and other Krylov work vectors.
275 *
276 * @note detail::AssmbScatrZeroDirOpImpl derives from AssmbScatrOp, not
277 * from this class, while Operator::Create() hands the factory's product
278 * back through a static_pointer_cast to this class. The cast is not
279 * backed by the object's actual type; it works because this class adds
280 * no data members or virtual functions to AssmbScatrOp, so the inherited
281 * Apply() overloads still dispatch to the implementation's v_Apply().
282 *
283 * @tparam TData Floating-point type of the coefficient data.
284 *
285 * @see AssmbScatrOp for the variant that assembles Dirichlet DOFs like
286 * any other DOF (no zeroing).
287 */
288template <typename TData> class AssmbScatrZeroDirOp : public AssmbScatrOp<TData>
289{
290public:
291 /**
292 * @brief Create an implementation of this operator through the
293 * operator factory.
294 *
295 * Looks up the key formed from #name plus the execution-space suffix
296 * and instantiates whatever implementation was registered under it
297 * (see AssmbScatrFactoryDec.cpp.in).
298 *
299 * @param expansionList Expansion list the operator acts on. In
300 * parallel runs this must be a
301 * MultiRegions::ContField, since the
302 * implementation derives its inter-rank
303 * assembly communication from the field's
304 * assembly map.
305 * @param components Session variable names the assembly maps
306 * are built for, one per field component.
307 * @param execStr Execution-space suffix, e.g. "Serial",
308 * "AVX" or "Device". If empty, it is taken
309 * from the "opExecSpace" command-line
310 * argument; without it the operator takes
311 * the space the build provides and warns.
312 *
313 * @return The operator implementation for the requested execution
314 * space.
315 */
316 static std::shared_ptr<AssmbScatrZeroDirOp<TData>> Create(
317 const MultiRegions::ExpListSharedPtr &expansionList,
318 const std::vector<std::string> &components,
319 const std::string &execStr = "")
320 {
321 return Operator<TData>::template Create<AssmbScatrZeroDirOp>(
322 expansionList, components, execStr);
323 }
324
325 /// Factory base name; implementations register under this name with an
326 /// execution-space suffix appended (e.g. "AssmbScatrZeroDirSerial").
327 static inline const std::string name = "AssmbScatrZeroDir";
328
329protected:
330 /// @brief Protected constructor; forwards the expansion list and
331 /// component names to AssmbScatrOp.
333 const std::vector<std::string> &components)
334 : AssmbScatrOp<TData>(expansionList, components)
335 {
336 }
337
338 ~AssmbScatrZeroDirOp() override = default;
339};
340
341/**
342 * @brief Assemble-scatter operator that ignores the orientation signs.
343 *
344 * Implementations perform the same in-place assemble-scatter as
345 * AssmbScatrOp, but with every sign factor of the assembly map replaced
346 * by its absolute value, so the local contributions are summed without
347 * sign reconciliation. The diagonal preconditioner uses it to assemble
348 * the operator diagonal.
349 *
350 * The class itself carries no implementation: it fixes the factory name
351 * under which detail::AssmbScatrNoSignOpImpl (AssmbScatrOpImpl with
352 * SIGNCHANGE = false) is registered from AssmbScatrFactoryDec.cpp.in.
353 *
354 * @note As for AssmbScatrZeroDirOp, the implementation derives from
355 * AssmbScatrOp, not from this class; the static_pointer_cast in
356 * Operator::Create() relies on this class adding no data members or
357 * virtual functions to AssmbScatrOp.
358 *
359 * @tparam TData Floating-point type of the coefficient data.
360 */
361template <typename TData> class AssmbScatrNoSignOp : public AssmbScatrOp<TData>
362{
363public:
364 /**
365 * @brief Create an implementation of this operator through the
366 * operator factory.
367 *
368 * @param expansionList Expansion list the operator acts on. In
369 * parallel runs this must be a
370 * MultiRegions::ContField.
371 * @param components Session variable names the assembly maps
372 * are built for, one per field component.
373 * @param execStr Execution-space suffix, e.g. "Serial",
374 * "AVX" or "Device". If empty, it is taken
375 * from the "opExecSpace" command-line
376 * argument.
377 *
378 * @return The operator implementation for the requested execution
379 * space.
380 */
381 static std::shared_ptr<AssmbScatrNoSignOp<TData>> Create(
382 const MultiRegions::ExpListSharedPtr &expansionList,
383 const std::vector<std::string> &components,
384 const std::string &execStr = "")
385 {
386 return Operator<TData>::template Create<AssmbScatrNoSignOp>(
387 expansionList, components, execStr);
388 }
389
390 /// Factory base name; implementations register under this name with an
391 /// execution-space suffix appended (e.g. "AssmbScatrNoSignSerial").
392 static inline const std::string name = "AssmbScatrNoSign";
393
394protected:
395 /// @brief Protected constructor; forwards the expansion list and
396 /// component names to AssmbScatrOp.
398 const std::vector<std::string> &components)
399 : AssmbScatrOp<TData>(expansionList, components)
400 {
401 }
402
403 ~AssmbScatrNoSignOp() override = default;
404};
405
406} // namespace Nektar::MultiRegions
Whole-field operator base class and factory machinery shared by every operator family in the MultiReg...
A Field represents expansion data to be operated on.
Assemble-scatter operator that ignores the orientation signs.
static const std::string name
Factory base name; implementations register under this name with an execution-space suffix appended (...
AssmbScatrNoSignOp(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components)
Protected constructor; forwards the expansion list and component names to AssmbScatrOp.
static std::shared_ptr< AssmbScatrNoSignOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="")
Create an implementation of this operator through the operator factory.
Base class for the assemble-scatter operator: sums the local contributions to every global degree of ...
static std::shared_ptr< AssmbScatrOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="")
Create an assemble-scatter operator through the operator factory.
void Apply(LibUtilities::Field< TData, FieldState::Coeff > &in, LibUtilities::Field< TData, FieldState::Coeff > &out)
Assemble and scatter in, writing the result to out.
void operator()(LibUtilities::Field< TData, FieldState::Coeff > &inout)
Call operator; equivalent to the in-place Apply().
static const std::string name
Operator base name; Create() appends the execution space to it to form the factory key.
virtual void v_Apply(LibUtilities::Field< TData, FieldState::Coeff > &inout)=0
Implementation hook for the in-place Apply().
void Apply(LibUtilities::Field< TData, FieldState::Coeff > &inout)
Assemble and scatter inout in place.
void operator()(LibUtilities::Field< TData, FieldState::Coeff > &in, LibUtilities::Field< TData, FieldState::Coeff > &out)
Call operator; equivalent to the two-field Apply().
AssmbScatrOp(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components)
Construct the interface part of a concrete implementation; called by the factory-registered creator f...
virtual void v_Apply(LibUtilities::Field< TData, FieldState::Coeff > &in, LibUtilities::Field< TData, FieldState::Coeff > &out)=0
Implementation hook for the two-field Apply().
Assemble-scatter operator that also zeroes the global Dirichlet boundary degrees of freedom.
static const std::string name
Factory base name; implementations register under this name with an execution-space suffix appended (...
static std::shared_ptr< AssmbScatrZeroDirOp< TData > > Create(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components, const std::string &execStr="")
Create an implementation of this operator through the operator factory.
AssmbScatrZeroDirOp(const MultiRegions::ExpListSharedPtr &expansionList, const std::vector< std::string > &components)
Protected constructor; forwards the expansion list and component names to AssmbScatrOp.
Common base class of the operator family interfaces: holds the expansion list, component names and da...
Definition Operator.hpp:100
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.