https://mooseframework.inl.gov
KokkosVectorKernel.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 
20 class VectorKernel : public KernelBase
21 {
22 public:
24 
29 
33  virtual void computeResidual() override;
37  virtual void computeJacobian() override;
38 
43 
53  template <typename Derived>
54  KOKKOS_FUNCTION Real computeQpJacobian(const unsigned int /* i */,
55  const unsigned int /* j */,
56  const unsigned int /* qp */,
57  AssemblyDatum & /* datum */) const
58  {
59  ::Kokkos::abort("Default computeQpJacobian() should never be called. Make sure you properly "
60  "redefined this method in your class without typos.");
61 
62  return 0;
63  }
74  template <typename Derived>
75  KOKKOS_FUNCTION Real computeQpOffDiagJacobian(const unsigned int /* i */,
76  const unsigned int /* j */,
77  const unsigned int /* jvar */,
78  const unsigned int /* qp */,
79  AssemblyDatum & /* datum */) const
80  {
81  ::Kokkos::abort(
82  "Default computeQpOffDiagJacobian() should never be called. Make sure you properly "
83  "redefined this method in your class without typos.");
84 
85  return 0;
86  }
88 
94  template <typename Derived>
96  static auto defaultJacobian()
97  {
98  return &VectorKernel::computeQpJacobian<Derived>;
99  }
100  template <typename Derived>
102  {
103  return &VectorKernel::computeQpOffDiagJacobian<Derived>;
104  }
106 
110  template <typename Derived>
112  KOKKOS_FUNCTION void operator()(ResidualLoop, const ThreadID tid, const Derived & kernel) const;
113  template <typename Derived>
114  KOKKOS_FUNCTION void operator()(JacobianLoop, const ThreadID tid, const Derived & kernel) const;
115  template <typename Derived>
116  KOKKOS_FUNCTION void
117  operator()(OffDiagJacobianLoop, const ThreadID tid, const Derived & kernel) const;
119 
125 
131  template <typename Derived>
132  KOKKOS_FUNCTION void computeResidualInternal(const Derived & kernel, AssemblyDatum & datum) const;
138  template <typename Derived>
139  KOKKOS_FUNCTION void computeJacobianInternal(const Derived & kernel, AssemblyDatum & datum) const;
145  template <typename Derived>
146  KOKKOS_FUNCTION void computeOffDiagJacobianInternal(const Derived & kernel,
147  AssemblyDatum & datum) const;
149 
150 protected:
175 };
176 
177 template <typename Derived>
178 KOKKOS_FUNCTION void
179 VectorKernel::operator()(ResidualLoop, const ThreadID tid, const Derived & kernel) const
180 {
181  auto elem = kokkosBlockElementID(_thread(tid, 1));
182 
183  AssemblyDatum datum(elem,
185  kokkosAssembly(),
186  kokkosSystems(),
187  _kokkos_var,
188  _kokkos_var.var());
189 
190  datum.set_local_parallel(_thread(tid, 0), _thread.size(0));
191 
192  kernel.computeResidualInternal(kernel, datum);
193 }
194 
195 template <typename Derived>
196 KOKKOS_FUNCTION void
197 VectorKernel::operator()(JacobianLoop, const ThreadID tid, const Derived & kernel) const
198 {
199  auto elem = kokkosBlockElementID(_thread(tid, 1));
200 
201  AssemblyDatum datum(elem,
203  kokkosAssembly(),
204  kokkosSystems(),
205  _kokkos_var,
206  _kokkos_var.var());
207 
208  datum.set_local_parallel(_thread(tid, 0), _thread.size(0));
209 
210  kernel.computeJacobianInternal(kernel, datum);
211 }
212 
213 template <typename Derived>
214 KOKKOS_FUNCTION void
215 VectorKernel::operator()(OffDiagJacobianLoop, const ThreadID tid, const Derived & kernel) const
216 {
217  auto elem = kokkosBlockElementID(_thread(tid, 2));
218 
219  auto & sys = kokkosSystem(_kokkos_var.sys());
220  auto jvar = sys.getCoupling(_kokkos_var.var())[_thread(tid, 1)];
221 
222  if (!sys.isVariableActive(jvar, kokkosMesh().getElementInfo(elem).subdomain))
223  return;
224 
225  AssemblyDatum datum(
226  elem, libMesh::invalid_uint, kokkosAssembly(), kokkosSystems(), _kokkos_var, jvar);
227 
228  datum.set_local_parallel(_thread(tid, 0), _thread.size(0));
229 
230  kernel.computeOffDiagJacobianInternal(kernel, datum);
231 }
232 
233 template <typename Derived>
234 KOKKOS_FUNCTION void
235 VectorKernel::computeResidualInternal(const Derived & kernel, AssemblyDatum & datum) const
236 {
238  datum,
239  [&](Real * local_re, const unsigned int ib, const unsigned int ie)
240  {
241  for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
242  for (unsigned int i = ib; i < ie; ++i)
243  local_re[i] += datum.JxW(qp) * kernel.template computeQpResidual<Derived>(i, qp, datum);
244  });
245 }
246 
247 template <typename Derived>
248 KOKKOS_FUNCTION void
249 VectorKernel::computeJacobianInternal(const Derived & kernel, AssemblyDatum & datum) const
250 {
252  datum,
253  [&](Real * local_ke, const unsigned int ib, const unsigned int ie, const unsigned int j)
254  {
255  for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
256  for (unsigned int i = ib; i < ie; ++i)
257  local_ke[i] +=
258  datum.JxW(qp) * kernel.template computeQpJacobian<Derived>(i, j, qp, datum);
259  });
260 }
261 
262 template <typename Derived>
263 KOKKOS_FUNCTION void
264 VectorKernel::computeOffDiagJacobianInternal(const Derived & kernel, AssemblyDatum & datum) const
265 {
267  datum,
268  [&](Real * local_ke, const unsigned int ib, const unsigned int ie, const unsigned int j)
269  {
270  for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
271  for (unsigned int i = ib; i < ie; ++i)
272  local_ke[i] += datum.JxW(qp) * kernel.template computeQpOffDiagJacobian<Derived>(
273  i, j, datum.jvar(), qp, datum);
274  });
275 }
276 
277 } // namespace Moose::Kokkos
KOKKOS_FUNCTION unsigned int sys(unsigned int comp=0) const
Get the system number of a component.
KOKKOS_FUNCTION void operator()(ResidualLoop, const ThreadID tid, const Derived &kernel) const
The parallel computation entry functions called by Kokkos.
KOKKOS_FUNCTION void computeJacobianInternal(AssemblyDatum &datum, function body) const
The common loop structure template for computing elemental Jacobian.
virtual void computeJacobian() override
Dispatch diagonal and off-diagonal Jacobian calculation.
const VectorVariablePhiGradient _grad_phi
Gradient of the current vector shape function.
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:369
const unsigned int invalid_uint
KOKKOS_FUNCTION const Assembly & kokkosAssembly() const
Get the const reference of the Kokkos assembly.
static auto defaultJacobian()
Functions used to check if users have overriden the hook methods, whose calculations can be skipped w...
The base class for a user to derive their own Kokkos vector kernels on vector variables.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
virtual void computeResidual() override
Dispatch residual calculation.
const VectorVariableValue _u
Current vector solution at quadrature points.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
Thread _thread
Kokkos thread object.
KOKKOS_FUNCTION unsigned int jvar() const
Get the coupled variable number.
Definition: KokkosDatum.h:651
const VectorVariablePhiValue _phi
Current vector shape function.
const VectorVariableGradient _grad_u
Gradient of the current vector solution at quadrature points.
static InputParameters validParams()
MOOSE_KOKKOS_INDEX_TYPE ThreadID
Definition: KokkosThread.h:22
KOKKOS_FUNCTION unsigned int n_qps() const
Get the number of local quadrature points.
Definition: KokkosDatum.h:303
The base class for Kokkos kernels.
KOKKOS_FUNCTION void computeResidualInternal(const Derived &kernel, AssemblyDatum &datum) const
The parallel computation bodies that can be customized in the derived class by defining them in the d...
KOKKOS_FUNCTION Real computeQpOffDiagJacobian(const unsigned int, const unsigned int, const unsigned int, const unsigned int, AssemblyDatum &) const
Compute off-diagonal Jacobian contribution on a quadrature point.
const VectorVariableTestValue _test
Current vector test function.
KOKKOS_FUNCTION ContiguousElementID kokkosBlockElementID(Moose::Kokkos::ThreadID tid) const
Get the contiguous element ID this Kokkos thread is operating on.
KOKKOS_FUNCTION const Mesh & kokkosMesh() const
Get the const reference of the Kokkos mesh.
Definition: KokkosMesh.h:651
KOKKOS_FUNCTION void computeJacobianInternal(const Derived &kernel, AssemblyDatum &datum) const
Compute diagonal Jacobian.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
The Kokkos object that holds thread-private data in the parallel operations of Kokkos kernels...
Definition: KokkosDatum.h:559
KOKKOS_FUNCTION Real JxW(const unsigned int qp)
Get the transformed Jacobian weight.
Definition: KokkosDatum.h:506
Variable _kokkos_var
Kokkos variable.
KOKKOS_FUNCTION unsigned int var(unsigned int comp=0) const
Get the variable number of a component.
const VectorVariableTestGradient _grad_test
Gradient of the current vector test function.
KOKKOS_FUNCTION thread_id_type size() const
Get the total thread pool size.
Definition: KokkosThread.h:50
KOKKOS_FUNCTION void computeResidualInternal(AssemblyDatum &datum, function body) const
The common loop structure template for computing elemental residual.
KOKKOS_FUNCTION void computeOffDiagJacobianInternal(const Derived &kernel, AssemblyDatum &datum) const
Compute off-diagonal Jacobian.
VectorKernel(const InputParameters &parameters)
Constructor.
KOKKOS_FUNCTION Real computeQpJacobian(const unsigned int, const unsigned int, const unsigned int, AssemblyDatum &) const
Default methods to prevent compile errors even when these methods were not defined in the derived cla...