https://mooseframework.inl.gov
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 "KokkosSystem.h"
17 
18 #include "AuxKernelBase.h"
19 
20 namespace Moose
21 {
22 namespace Kokkos
23 {
24 
38 class AuxKernel : public ::AuxKernelBase,
39  public MeshHolder,
40  public AssemblyHolder,
41  public SystemHolder
42 {
43 public:
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 
73  struct ElementLoop
75  {
76  };
77  struct NodeLoop
78  {
79  };
81 
85  template <typename Derived>
86  KOKKOS_FUNCTION Real computeValueShim(const Derived & auxkernel,
87  const unsigned int qp,
88  AssemblyDatum & datum) const
89  {
90  return auxkernel.computeValue(qp, datum);
91  }
92 
96  template <typename Derived>
98  KOKKOS_FUNCTION void operator()(ElementLoop, const ThreadID tid, const Derived & auxkernel) const;
99  template <typename Derived>
100  KOKKOS_FUNCTION void operator()(NodeLoop, const ThreadID tid, const Derived & auxkernel) const;
102 
108 
114  template <typename Derived>
115  KOKKOS_FUNCTION void computeElementInternal(const Derived & auxkernel,
116  AssemblyDatum & datum) const;
122  template <typename Derived>
123  KOKKOS_FUNCTION void computeNodeInternal(const Derived & auxkernel, AssemblyDatum & datum) const;
125 
126 protected:
131  VariableValue uOld() const;
136  VariableValue uOlder() const;
137 
144  KOKKOS_FUNCTION void setElementSolution(const Real * const values,
145  const AssemblyDatum & datum,
146  const unsigned int comp = 0) const;
153  KOKKOS_FUNCTION void
154  setNodeSolution(const Real value, const AssemblyDatum & datum, const unsigned int comp = 0) const;
155 
159  const bool _nodal;
160 
168  std::unique_ptr<DispatcherBase> _element_dispatcher;
170  std::unique_ptr<DispatcherBase> _node_dispatcher;
172 
181 
185 
207 
208 private:
213  void getKokkosMaterialPropertyHook(const std::string & prop_name_in,
214  const unsigned int state) override final;
215 };
216 
217 template <typename Derived>
218 KOKKOS_FUNCTION void
219 AuxKernel::operator()(ElementLoop, const ThreadID tid, const Derived & auxkernel) const
220 {
221  auto elem = kokkosBlockElementID(tid);
222 
223  AssemblyDatum datum(elem,
225  kokkosAssembly(),
226  kokkosSystems(),
227  _kokkos_var,
228  _kokkos_var.var());
229 
230  auxkernel.computeElementInternal(auxkernel, datum);
231 }
232 
233 template <typename Derived>
234 KOKKOS_FUNCTION void
235 AuxKernel::operator()(NodeLoop, const ThreadID tid, const Derived & auxkernel) const
236 {
237  auto node = _bnd ? kokkosBoundaryNodeID(tid) : kokkosBlockNodeID(tid);
238  auto & sys = kokkosSystem(_kokkos_var.sys());
239 
240  if (!sys.isNodalDefined(node, _kokkos_var.var()))
241  return;
242 
244 
245  auxkernel.computeNodeInternal(auxkernel, datum);
246 }
247 
248 template <typename Derived>
249 KOKKOS_FUNCTION void
250 AuxKernel::computeElementInternal(const Derived & auxkernel, AssemblyDatum & datum) const
251 {
252  Real x[MAX_CACHED_DOF];
253  Real b[MAX_CACHED_DOF];
255 
256  for (unsigned int i = 0; i < datum.n_dofs(); ++i)
257  {
258  x[i] = 0;
259  b[i] = 0;
260 
261  for (unsigned int j = 0; j < datum.n_dofs(); ++j)
262  A[j + datum.n_dofs() * i] = 0;
263  }
264 
265  for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
266  {
267  const auto value = auxkernel.computeValueShim(auxkernel, qp, datum);
268 
269  datum.reinit();
270 
271  for (unsigned int i = 0; i < datum.n_dofs(); ++i)
272  {
273  const auto t = datum.JxW(qp) * _test(datum, i, qp);
274 
275  b[i] += t * value;
276 
277  for (unsigned int j = 0; j < datum.n_dofs(); ++j)
278  A[j + datum.n_dofs() * i] += t * _test(datum, j, qp);
279  }
280  }
281 
282  if (datum.n_dofs() == 1)
283  x[0] = b[0] / A[0];
284  else
285  Utils::choleskySolve(A, x, b, datum.n_dofs());
286 
287  setElementSolution(x, datum);
288 }
289 
290 template <typename Derived>
291 KOKKOS_FUNCTION void
292 AuxKernel::computeNodeInternal(const Derived & auxkernel, AssemblyDatum & datum) const
293 {
294  auto value = auxkernel.computeValueShim(auxkernel, 0, datum);
295 
296  setNodeSolution(value, datum);
297 }
298 
299 KOKKOS_FUNCTION inline void
300 AuxKernel::setElementSolution(const Real * const values,
301  const AssemblyDatum & datum,
302  const unsigned int comp) const
303 {
304  auto & sys = kokkosSystem(_kokkos_var.sys(comp));
305  auto var_num = _kokkos_var.var(comp);
306  auto tag = _kokkos_var.tag();
307  auto elem = datum.elem().id;
308 
309  for (unsigned int i = 0; i < datum.n_dofs(); ++i)
310  sys.getVectorDofValue(sys.getElemLocalDofIndex(elem, i, var_num), tag) = values[i];
311 }
312 
313 KOKKOS_FUNCTION inline void
314 AuxKernel::setNodeSolution(const Real value,
315  const AssemblyDatum & datum,
316  const unsigned int comp) const
317 {
318  auto & sys = kokkosSystem(_kokkos_var.sys(comp));
319  auto var_num = _kokkos_var.var(comp);
320  auto tag = _kokkos_var.tag();
321  auto node = datum.node();
322 
323  sys.getVectorDofValue(sys.getNodeLocalDofIndex(node, 0, var_num), tag) = value;
324 }
325 
326 } // namespace Kokkos
327 } // namespace Moose
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_FUNCTION bool isNodal() const
Get whether this auxiliary kernel is nodal.
Base class for auxiliary kernels.
Definition: AuxKernelBase.h:42
KOKKOS_FUNCTION ContiguousNodeID kokkosBoundaryNodeID(ThreadID tid) const
Get the contiguous node ID this Kokkos thread is operating on.
const unsigned int invalid_uint
KOKKOS_FUNCTION const Assembly & kokkosAssembly() const
Get the const reference of the Kokkos assembly.
KOKKOS_FUNCTION ContiguousElementID kokkosBlockNodeID(ThreadID tid) const
Get the contiguous node index this Kokkos thread is operating on.
The base class for a user to derive their own Kokkos auxiliary kernels.
dof_id_type ThreadID
Definition: KokkosThread.h:18
KOKKOS_FUNCTION void setNodeSolution(const Real value, const AssemblyDatum &datum, const unsigned int comp=0) const
Set node value to the auxiliary solution vector.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
virtual void compute() override
Dispatch calculation.
const bool _nodal
Flag whether this kernel is nodal.
static InputParameters validParams()
std::unique_ptr< DispatcherBase > _element_dispatcher
Kokkos functor dispatchers.
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:74
std::unique_ptr< DispatcherBase > _node_dispatcher
Variable _kokkos_var
Kokkos variable.
KOKKOS_FUNCTION Real computeValueShim(const Derived &auxkernel, const unsigned int qp, AssemblyDatum &datum) const
Shim for hook method that can be leveraged to implement static polymorphism.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
The Kokkos interface that holds the host reference of the Kokkos systems and copies it to device duri...
Definition: KokkosSystem.h:586
The Kokkos interface that holds the host reference of the Kokkos mesh and copies it to device during ...
Definition: KokkosMesh.h:338
const bool _bnd
true if the kernel is boundary kernel, false if it is interior kernels
Definition: AuxKernelBase.h:88
KOKKOS_FUNCTION void operator()(ElementLoop, const ThreadID tid, const Derived &auxkernel) const
The parallel computation entry functions called by Kokkos.
Scalar< Real > _t
TODO: Move to TransientInterface.
virtual void subdomainSetup() override final
Gets called when the subdomain changes (i.e.
KOKKOS_FUNCTION void computeNodeInternal(const Derived &auxkernel, AssemblyDatum &datum) const
Compute a node.
KOKKOS_FUNCTION ContiguousElementID kokkosBlockElementID(ThreadID tid) const
Get the contiguous element ID this Kokkos thread is operating on.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
KOKKOS_FUNCTION unsigned int n_qps() const
Get the number of local quadrature points.
Definition: KokkosDatum.h:100
KOKKOS_FUNCTION const System & kokkosSystem(unsigned int sys) const
Get the const reference of a Kokkos system.
Definition: KokkosSystem.h:625
The Kokkos interface that holds the host reference of the Kokkos assembly and copies it to device dur...
Scalar< int > _t_step
The number of the time step.
Scalar< Real > _dt_old
Size of the old time step.
VariableValue uOlder() const
Retrieve the older value of the variable that this kernel operates on.
constexpr unsigned int MAX_CACHED_DOF
Maximum number of DOFs to cache during residual and Jacobian computation.
Definition: KokkosHeader.h:79
const VariableTestValue _test
Current test function.
KOKKOS_FUNCTION ContiguousNodeID node() const
Get the contiguous node ID.
Definition: KokkosDatum.h:95
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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.
The Kokkos variable object that carries the coupled variable and tag information. ...
const VariableValue _u
Current solution.
KOKKOS_FUNCTION const ElementInfo & elem() const
Get the element information object.
Definition: KokkosDatum.h:80
The Kokkos object that holds thread-private data in the parallel operations of Kokkos kernels...
Definition: KokkosDatum.h:244
VariableValue uOld() const
Retrieve the old value of the variable that this kernel operates on.
KOKKOS_FUNCTION Real JxW(const unsigned int qp)
Get the transformed Jacobian weight.
Definition: KokkosDatum.h:139
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
The Kokkos wrapper classes for MOOSE-like variable value access.
KOKKOS_FUNCTION unsigned int var(unsigned int comp=0) const
Get the variable number of a component.
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...
Scalar< Real > _dt
Time step size.
KOKKOS_FUNCTION unsigned int n_dofs() const
Get the number of local DOFs.
Definition: KokkosDatum.h:304
AuxKernel(const InputParameters &parameters)
Constructor.
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...
KOKKOS_FUNCTION void reinit()
Reset the reinit flag.
Definition: KokkosDatum.h:166
Scalar< const Real > _t_old
Old time.
ContiguousElementID id
Contiguous element ID.
Definition: KokkosMesh.h:44
KOKKOS_FUNCTION const Array< System > & kokkosSystems() const
Get the const reference of the Kokkos systems.
Definition: KokkosSystem.h:608