https://mooseframework.inl.gov
KokkosADKernel.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "KokkosKernelBase.h"
13 
14 namespace Moose::Kokkos
15 {
16 
38 class ADKernel : public KernelBase
39 {
40 public:
42 
47 
48  virtual void computeResidual() override;
49  virtual void computeJacobian() override;
50  virtual void computeResidualAndJacobian() override;
51 
55  template <typename Derived>
56  KOKKOS_FUNCTION void operator()(ResidualLoop, const ThreadID tid, const Derived & kernel) const;
57 
63  template <typename Derived>
64  KOKKOS_FUNCTION void computeResidualInternal(const Derived & kernel, AssemblyDatum & datum) const;
65 
66 protected:
70  virtual void dispatch();
71 
99  bool _computing_residual = false;
103  bool _computing_jacobian = false;
104 };
105 
106 template <typename Derived>
107 KOKKOS_FUNCTION void
108 ADKernel::operator()(ResidualLoop, const ThreadID tid, const Derived & kernel) const
109 {
110  auto elem = kokkosBlockElementID(_thread(tid, 1));
111 
112  AssemblyDatum datum(elem,
114  kokkosAssembly(),
115  kokkosSystems(),
116  _kokkos_var,
117  _kokkos_var.var());
118 
119  datum.set_local_parallel(_thread(tid, 0), _thread.size(0));
120 
121  datum.do_derivatives(_computing_jacobian);
122 
123  kernel.computeResidualInternal(kernel, datum);
124 }
125 
126 template <typename Derived>
127 KOKKOS_FUNCTION void
128 ADKernel::computeResidualInternal(const Derived & kernel, AssemblyDatum & datum) const
129 {
130  for (unsigned int i = datum.local_thread_id(); i < datum.n_dofs(); i += datum.num_local_threads())
131  {
132  ADReal local_re = 0;
133 
134  for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
135  local_re += datum.JxW(qp) * kernel.template computeQpResidual<Derived>(i, qp, datum);
136 
138  accumulateTaggedElementalResidual(local_re.value(), datum.elem().id, i);
140  accumulateTaggedElementalMatrix(local_re.derivatives(), datum, i);
141  }
142 }
143 
144 } // namespace Moose::Kokkos
virtual void computeJacobian() override
Compute this object&#39;s contribution to the diagonal Jacobian entries.
virtual void dispatch()
Dispatch parallel calculation.
KOKKOS_FUNCTION void set_local_parallel(const unsigned int local_thread_id, const unsigned int num_local_threads)
Set local parallelization option.
Definition: KokkosDatum.h:187
const unsigned int invalid_uint
KOKKOS_FUNCTION const Assembly & kokkosAssembly() const
Get the const reference of the Kokkos assembly.
KOKKOS_FUNCTION unsigned int num_local_threads() const
Get the number of local threads.
Definition: KokkosDatum.h:202
KOKKOS_FUNCTION void computeResidualInternal(const Derived &kernel, AssemblyDatum &datum) const
Compute residual.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Thread _thread
Kokkos thread object.
The Kokkos wrapper classes for MOOSE-like shape function access.
DualNumber< Real, DNDerivativeType, false > ADReal
Definition: KokkosADReal.h:28
MOOSE_KOKKOS_INDEX_TYPE ThreadID
Definition: KokkosThread.h:22
const ADVariablePhiValue _phi
Current shape function.
bool _computing_jacobian
Whether computing Jacobian.
KOKKOS_FUNCTION void accumulateTaggedElementalMatrix(const Real local_ke, const ContiguousElementID elem, const unsigned int i, const unsigned int j, const unsigned int jvar, const unsigned int comp=0) const
Accumulate local elemental Jacobian contribution to tagged matrices.
KOKKOS_FUNCTION unsigned int n_qps() const
Get the number of local quadrature points.
Definition: KokkosDatum.h:118
KOKKOS_FUNCTION unsigned int local_thread_id() const
Get the current local thread ID.
Definition: KokkosDatum.h:197
The base class for Kokkos kernels.
bool _computing_residual
Whether computing residual.
The Kokkos wrapper classes for MOOSE-like variable value access.
const ADVariablePhiGradient _grad_phi
Gradient of the current shape function.
ADKernel(const InputParameters &parameters)
Constructor.
KOKKOS_FUNCTION void operator()(ResidualLoop, const ThreadID tid, const Derived &kernel) const
The parallel computation entry function called by Kokkos.
const ADVariableTestValue _test
Current test function.
KOKKOS_FUNCTION ContiguousElementID kokkosBlockElementID(Moose::Kokkos::ThreadID tid) const
Get the contiguous element ID this Kokkos thread is operating on.
static InputParameters validParams()
const ADVariableGradient _grad_u
Gradient of the current solution at quadrature points.
const ADVariableTestGradient _grad_test
Gradient of the current test function.
KOKKOS_FUNCTION void accumulateTaggedElementalResidual(const Real local_re, const ContiguousElementID elem, const unsigned int i, const unsigned int comp=0) const
Accumulate local elemental residual contribution to tagged vectors.
KOKKOS_FUNCTION const ElementInfo & elem() const
Get the element information object.
Definition: KokkosDatum.h:84
The base class for a user to derive their own Kokkos kernels using automatic differentiation (AD)...
virtual void computeResidual() override
Compute this object&#39;s contribution to the residual.
The Kokkos object that holds thread-private data in the parallel operations of Kokkos kernels...
Definition: KokkosDatum.h:364
virtual void computeResidualAndJacobian() override
Compute this object&#39;s contribution to the residual and Jacobian simultaneously.
KOKKOS_FUNCTION Real JxW(const unsigned int qp)
Get the transformed Jacobian weight.
Definition: KokkosDatum.h:311
Variable _kokkos_var
Kokkos variable.
KOKKOS_FUNCTION unsigned int var(unsigned int comp=0) const
Get the variable number of a component.
KOKKOS_FUNCTION unsigned int n_dofs() const
Get the number of local DOFs.
Definition: KokkosDatum.h:426
KOKKOS_FUNCTION thread_id_type size() const
Get the total thread pool size.
Definition: KokkosThread.h:50
const ADVariableValue _u
Current solution at quadrature points.
ContiguousElementID id
Contiguous element ID.
Definition: KokkosMesh.h:42
KOKKOS_FUNCTION const Array< System > & kokkosSystems() const
Get the const reference of the Kokkos systems.
Definition: KokkosSystem.h:775