https://mooseframework.inl.gov
Loading...
Searching...
No Matches
KokkosAuxKernel.h
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#pragma once
11
12#include "KokkosDispatcher.h"
13#include "KokkosVariableValue.h"
15#include "KokkosAssembly.h"
16#include "KokkosFESystem.h"
17
18#include "AuxKernelBase.h"
19
20namespace Moose::Kokkos
21{
22
39 public MeshHolder,
40 public AssemblyHolder,
41 public FESystemHolder
42{
43public:
45
50
54 AuxKernel(const AuxKernel & object);
55
56 // Unused for Kokkos auxiliary kernels because all elements are computed in parallel
57 virtual void subdomainSetup() override final {}
58
62 virtual void compute() override;
63
68 KOKKOS_FUNCTION bool isNodal() const { return _nodal; }
69
75 {
76 };
77 struct NodeLoop
78 {
79 };
81
86 template <typename Derived>
87 KOKKOS_FUNCTION void operator()(ElementLoop, const ThreadID tid, const Derived & auxkernel) const;
88 template <typename Derived>
89 KOKKOS_FUNCTION void operator()(NodeLoop, const ThreadID tid, const Derived & auxkernel) const;
91
98
103 template <typename Derived>
104 KOKKOS_FUNCTION void computeElementInternal(const Derived & auxkernel,
105 AssemblyDatum & datum) const;
111 template <typename Derived>
112 KOKKOS_FUNCTION void computeNodeInternal(const Derived & auxkernel, AssemblyDatum & datum) const;
114
115protected:
126
133 KOKKOS_FUNCTION void setElementSolution(const Real * const values,
134 const AssemblyDatum & datum,
135 const unsigned int comp = 0) const;
142 KOKKOS_FUNCTION void
143 setNodeSolution(const Real value, const AssemblyDatum & datum, const unsigned int comp = 0) const;
144
148 const bool _nodal;
149
158 std::unique_ptr<DispatcherBase> _element_dispatcher;
159 std::unique_ptr<DispatcherBase> _node_dispatcher;
161
170
175
196
197private:
202 void getKokkosMaterialPropertyHook(const std::string & prop_name_in,
203 const unsigned int state) override final;
204};
205
206template <typename Derived>
207KOKKOS_FUNCTION void
208AuxKernel::operator()(ElementLoop, const ThreadID tid, const Derived & auxkernel) const
209{
210 auto elem = kokkosBlockElementID(tid);
211
212 AssemblyDatum datum(elem,
215 kokkosSystems(),
217 _kokkos_var.var());
218
219 auxkernel.computeElementInternal(auxkernel, datum);
220}
221
222template <typename Derived>
223KOKKOS_FUNCTION void
224AuxKernel::operator()(NodeLoop, const ThreadID tid, const Derived & auxkernel) const
225{
226 auto node = _bnd ? kokkosBoundaryNodeID(tid) : kokkosBlockNodeID(tid);
227 auto & sys = kokkosSystem(_kokkos_var.sys());
228
229 if (!sys.isNodalDefined(node, _kokkos_var.var()))
230 return;
231
232 AssemblyDatum datum(node, kokkosAssembly(), kokkosSystems(), _kokkos_var, _kokkos_var.var());
233
234 auxkernel.computeNodeInternal(auxkernel, datum);
235}
236
237template <typename Derived>
238KOKKOS_FUNCTION void
239AuxKernel::computeElementInternal(const Derived & auxkernel, AssemblyDatum & datum) const
240{
241 Real x[MAX_CACHED_DOF];
242 Real b[MAX_CACHED_DOF];
244
245 for (unsigned int i = 0; i < datum.n_dofs(); ++i)
246 {
247 x[i] = 0;
248 b[i] = 0;
249
250 for (unsigned int j = 0; j < datum.n_dofs(); ++j)
251 A[j + datum.n_dofs() * i] = 0;
252 }
253
254 for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
255 {
256 const auto value = auxkernel.template computeValue<Derived>(qp, datum);
257
258 for (unsigned int i = 0; i < datum.n_dofs(); ++i)
259 {
260 const auto t = datum.JxW(qp) * _test(datum, i, qp);
261
262 b[i] += t * value;
263
264 for (unsigned int j = 0; j < datum.n_dofs(); ++j)
265 A[j + datum.n_dofs() * i] += t * _test(datum, j, qp);
266 }
267 }
268
269 if (datum.n_dofs() == 1)
270 x[0] = b[0] / A[0];
271 else
272 Utils::choleskySolve(A, x, b, datum.n_dofs());
273
274 setElementSolution(x, datum);
275}
276
277template <typename Derived>
278KOKKOS_FUNCTION void
279AuxKernel::computeNodeInternal(const Derived & auxkernel, AssemblyDatum & datum) const
280{
281 auto value = auxkernel.template computeValue<Derived>(0, datum);
282
283 setNodeSolution(value, datum);
284}
285
286KOKKOS_FUNCTION inline void
287AuxKernel::setElementSolution(const Real * const values,
288 const AssemblyDatum & datum,
289 const unsigned int comp) const
290{
291 auto & sys = kokkosSystem(_kokkos_var.sys(comp));
292 auto var_num = _kokkos_var.var(comp);
293 auto tag = _kokkos_var.tag();
294 auto elem = datum.elem().id;
295
296 for (unsigned int i = 0; i < datum.n_dofs(); ++i)
297 sys.getVectorDofValue(sys.getElemLocalDofIndex(elem, i, var_num), tag) = values[i];
298}
299
300KOKKOS_FUNCTION inline void
301AuxKernel::setNodeSolution(const Real value,
302 const AssemblyDatum & datum,
303 const unsigned int comp) const
304{
305 auto & sys = kokkosSystem(_kokkos_var.sys(comp));
306 auto var_num = _kokkos_var.var(comp);
307 auto tag = _kokkos_var.tag();
308 auto node = datum.node();
309
310 sys.getVectorDofValue(sys.getNodeLocalDofIndex(node, 0, var_num), tag) = value;
311}
312
313} // namespace Moose::Kokkos
std::array< Real, 2 > values
Definition MortarUtils.C:52
Base class for auxiliary kernels.
const bool _bnd
true if the kernel is boundary kernel, false if it is interior kernels
KOKKOS_FUNCTION ContiguousElementID kokkosBlockElementID(Moose::Kokkos::ThreadID tid) const
Get the contiguous element ID this Kokkos thread is operating on.
KOKKOS_FUNCTION ContiguousElementID kokkosBlockNodeID(Moose::Kokkos::ThreadID tid) const
Get the contiguous node index this Kokkos thread is operating on.
KOKKOS_FUNCTION ContiguousNodeID kokkosBoundaryNodeID(Moose::Kokkos::ThreadID tid) const
Get the contiguous node ID this Kokkos thread is operating on.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
const InputParameters & parameters() const
Get the parameters of the object.
Definition MooseBase.h:131
The Kokkos object that holds thread-private data in the parallel operations of Kokkos kernels.
KOKKOS_FUNCTION unsigned int n_dofs() const
Get the number of local DOFs.
The Kokkos interface that holds the host reference of the Kokkos assembly and copies it to device dur...
KOKKOS_FUNCTION const Assembly & kokkosAssembly() const
Get the const reference of the Kokkos assembly.
The base class for a user to derive their own Kokkos auxiliary kernels.
std::unique_ptr< DispatcherBase > _node_dispatcher
const VariableTestValue _test
Current test function.
KOKKOS_FUNCTION void operator()(ElementLoop, const ThreadID tid, const Derived &auxkernel) const
The parallel computation entry functions called by Kokkos.
Scalar< Real > _dt_old
Size of the old time step.
const VariableValue _u
Current solution.
KOKKOS_FUNCTION void setNodeSolution(const Real value, const AssemblyDatum &datum, const unsigned int comp=0) const
Set node value to the auxiliary solution vector.
Scalar< int > _t_step
The number of the time step.
VariableValue uOld() const
Retrieve the old value of the variable that this kernel operates on.
void getKokkosMaterialPropertyHook(const std::string &prop_name_in, const unsigned int state) override final
Override of the MaterialPropertyInterface function to throw on material property request for nodal ke...
const bool _nodal
Flag whether this kernel is nodal.
Scalar< const Real > _t_old
Old time.
Variable _kokkos_var
Kokkos variable.
static InputParameters validParams()
KOKKOS_FUNCTION void computeNodeInternal(const Derived &auxkernel, AssemblyDatum &datum) const
Compute a node.
KOKKOS_FUNCTION void setElementSolution(const Real *const values, const AssemblyDatum &datum, const unsigned int comp=0) const
Set element values to the auxiliary solution vector.
virtual void compute() override
Dispatch calculation.
AuxKernel(const AuxKernel &object)
Copy constructor for parallel dispatch.
std::unique_ptr< DispatcherBase > _element_dispatcher
Kokkos functor dispatchers.
KOKKOS_FUNCTION bool isNodal() const
Get whether this auxiliary kernel is nodal.
VariableValue uOlder() const
Retrieve the older value of the variable that this kernel operates on.
AuxKernel(const InputParameters &parameters)
Constructor.
Scalar< Real > _t
TODO: Move to TransientInterface.
Scalar< Real > _dt
Time step size.
virtual void subdomainSetup() override final
Gets called when the subdomain changes (i.e.
KOKKOS_FUNCTION void computeElementInternal(const Derived &auxkernel, AssemblyDatum &datum) const
The parallel computation bodies that can be customized in the derived class by defining them in the d...
KOKKOS_FUNCTION Real JxW(const unsigned int qp)
Get the transformed Jacobian weight.
KOKKOS_FUNCTION ContiguousNodeID node() const
Get the contiguous node ID.
KOKKOS_FUNCTION unsigned int n_qps() const
Get the number of local quadrature points.
KOKKOS_FUNCTION const ElementInfo & elem() const
Get the element information object.
Definition KokkosDatum.h:46
The Kokkos interface that holds the host reference of the Kokkos mesh and copies it to device during ...
Definition KokkosMesh.h:630
The Kokkos wrapper class that can hold the reference of an arithmetic scalar variable.
The Kokkos wrapper classes for MOOSE-like shape function access.
The Kokkos wrapper classes for MOOSE-like variable value access.
The Kokkos variable object that carries the coupled variable and tag information.
KOKKOS_FUNCTION unsigned int var(unsigned int comp=0) const
Get the variable number of a component.
KOKKOS_FUNCTION TagID tag() const
Get the vector tag ID.
KOKKOS_FUNCTION unsigned int sys(unsigned int comp=0) const
Get the system number of a component.
KOKKOS_INLINE_FUNCTION void choleskySolve(Real *const A, Real *const x, Real *const b, const unsigned int n)
Perform an in-place linear solve using Cholesky decomposition Matrix and right-hand-side vector are m...
Definition KokkosUtils.h:72
constexpr unsigned int MAX_CACHED_DOF
Maximum number of DOFs to cache during residual and Jacobian computation.
MOOSE_KOKKOS_INDEX_TYPE ThreadID
const unsigned int invalid_uint
ContiguousElementID id
Contiguous element ID.
Definition KokkosMesh.h:42