https://mooseframework.inl.gov
Loading...
Searching...
No Matches
KokkosKernel.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 "KokkosKernelBase.h"
13
14namespace Moose::Kokkos
15{
16
39class Kernel : public KernelBase
40{
41public:
43
44 // This object supports computeQpOffDiagJacobianScalar()
45 static constexpr bool supports_scalar_jacobian = true;
46
51
55 virtual void computeResidual() override;
59 virtual void computeJacobian() override;
60
66
75 template <typename Derived>
76 KOKKOS_FUNCTION Real computeQpJacobian(const unsigned int /* i */,
77 const unsigned int /* j */,
78 const unsigned int /* qp */,
79 AssemblyDatum & /* datum */) const
80 {
81 ::Kokkos::abort("Default computeQpJacobian() should never be called. Make sure you properly "
82 "redefined this method in your class without typos.");
83
84 return 0;
85 }
96 template <typename Derived>
97 KOKKOS_FUNCTION Real computeQpOffDiagJacobian(const unsigned int /* i */,
98 const unsigned int /* j */,
99 const unsigned int /* jvar */,
100 const unsigned int /* qp */,
101 AssemblyDatum & /* datum */) const
102 {
103 ::Kokkos::abort(
104 "Default computeQpOffDiagJacobian() should never be called. Make sure you properly "
105 "redefined this method in your class without typos.");
106
107 return 0;
108 }
119 template <typename Derived>
120 KOKKOS_FUNCTION Real computeQpOffDiagJacobianScalar(const unsigned int /* i */,
121 const unsigned int /* j */,
122 const unsigned int /* jvar */,
123 const unsigned int /* qp */,
124 AssemblyDatum & /* datum */) const
125 {
126 ::Kokkos::abort(
127 "Default computeQpOffDiagJacobianScalar() should never be called. Make sure you properly "
128 "redefined this method in your class without typos.");
129
130 return 0;
131 }
133
140 template <typename Derived>
141 static auto defaultJacobian()
142 {
143 return &Kernel::computeQpJacobian<Derived>;
144 }
145 template <typename Derived>
147 {
148 return &Kernel::computeQpOffDiagJacobian<Derived>;
149 }
150 template <typename Derived>
152 {
153 return &Kernel::computeQpOffDiagJacobianScalar<Derived>;
154 }
156
161 template <typename Derived>
162 KOKKOS_FUNCTION void operator()(ResidualLoop, const ThreadID tid, const Derived & kernel) const;
163 template <typename Derived>
164 KOKKOS_FUNCTION void operator()(JacobianLoop, const ThreadID tid, const Derived & kernel) const;
165 template <typename Derived>
166 KOKKOS_FUNCTION void
167 operator()(OffDiagJacobianLoop, const ThreadID tid, const Derived & kernel) const;
168 template <typename Derived>
169 KOKKOS_FUNCTION void
170 operator()(OffDiagJacobianScalarLoop, const ThreadID tid, const Derived & kernel) const;
172
179
184 template <typename Derived>
185 KOKKOS_FUNCTION void computeResidualInternal(const Derived & kernel, AssemblyDatum & datum) const;
191 template <typename Derived>
192 KOKKOS_FUNCTION void computeJacobianInternal(const Derived & kernel, AssemblyDatum & datum) const;
198 template <typename Derived>
199 KOKKOS_FUNCTION void computeOffDiagJacobianInternal(const Derived & kernel,
200 AssemblyDatum & datum) const;
206 template <typename Derived>
207 KOKKOS_FUNCTION void computeOffDiagJacobianScalarInternal(const Derived & kernel,
208 AssemblyDatum & datum) const;
210
211protected:
236};
237
238template <typename Derived>
239KOKKOS_FUNCTION void
240Kernel::operator()(ResidualLoop, const ThreadID tid, const Derived & kernel) const
241{
242 auto comp = _thread(tid, 1);
243 auto elem = kokkosBlockElementID(_thread(tid, 2));
244
245 AssemblyDatum datum(elem,
248 kokkosSystems(),
250 _kokkos_var.var(comp),
251 comp);
252
253 datum.set_local_parallel(_thread(tid, 0), _thread.size(0));
254
255 kernel.computeResidualInternal(kernel, datum);
256}
257
258template <typename Derived>
259KOKKOS_FUNCTION void
260Kernel::operator()(JacobianLoop, const ThreadID tid, const Derived & kernel) const
261{
262 auto comp = _thread(tid, 1);
263 auto elem = kokkosBlockElementID(_thread(tid, 2));
264
265 AssemblyDatum datum(elem,
268 kokkosSystems(),
270 _kokkos_var.var(comp),
271 comp);
272
273 datum.set_local_parallel(_thread(tid, 0), _thread.size(0));
274
275 kernel.computeJacobianInternal(kernel, datum);
276}
277
278template <typename Derived>
279KOKKOS_FUNCTION void
280Kernel::operator()(OffDiagJacobianLoop, const ThreadID tid, const Derived & kernel) const
281{
282 auto comp = _thread(tid, 1);
283 auto elem = kokkosBlockElementID(_thread(tid, 3));
284
285 auto & sys = kokkosSystem(_kokkos_var.sys(comp));
286 auto jvar = sys.getFieldCoupling(_kokkos_var.var(comp))[_thread(tid, 2)];
287
288 if (!sys.isVariableActive(jvar, kokkosMesh().getElementInfo(elem).subdomain))
289 return;
290
291 AssemblyDatum datum(
292 elem, libMesh::invalid_uint, kokkosAssembly(), kokkosSystems(), _kokkos_var, jvar, comp);
293
294 datum.set_local_parallel(_thread(tid, 0), _thread.size(0));
295
296 kernel.computeOffDiagJacobianInternal(kernel, datum);
297}
298
299template <typename Derived>
300KOKKOS_FUNCTION void
301Kernel::operator()(OffDiagJacobianScalarLoop, const ThreadID tid, const Derived & kernel) const
302{
303 auto comp = _thread(tid, 0);
304 auto elem = kokkosBlockElementID(_thread(tid, 2));
305
306 auto & sys = kokkosSystem(_kokkos_var.sys(comp));
307 auto jvar = sys.getScalarCoupling(_kokkos_var.var(comp))[_thread(tid, 1)];
308
309 if (!sys.isVariableActive(jvar, kokkosMesh().getElementInfo(elem).subdomain))
310 return;
311
312 AssemblyDatum datum(
313 elem, libMesh::invalid_uint, kokkosAssembly(), kokkosSystems(), _kokkos_var, jvar, comp);
314
315 kernel.computeOffDiagJacobianScalarInternal(kernel, datum);
316}
317
318template <typename Derived>
319KOKKOS_FUNCTION void
320Kernel::computeResidualInternal(const Derived & kernel, AssemblyDatum & datum) const
321{
323 datum,
324 [&](Real * local_re, const unsigned int ib, const unsigned int ie)
325 {
326 for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
327 for (unsigned int i = ib; i < ie; ++i)
328 local_re[i] += datum.JxW(qp) * kernel.template computeQpResidual<Derived>(i, qp, datum);
329 });
330}
331
332template <typename Derived>
333KOKKOS_FUNCTION void
334Kernel::computeJacobianInternal(const Derived & kernel, AssemblyDatum & datum) const
335{
337 datum,
338 [&](Real * local_ke, const unsigned int ib, const unsigned int ie, const unsigned int j)
339 {
340 for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
341 for (unsigned int i = ib; i < ie; ++i)
342 local_ke[i] +=
343 datum.JxW(qp) * kernel.template computeQpJacobian<Derived>(i, j, qp, datum);
344 });
345}
346
347template <typename Derived>
348KOKKOS_FUNCTION void
349Kernel::computeOffDiagJacobianInternal(const Derived & kernel, AssemblyDatum & datum) const
350{
352 datum,
353 [&](Real * local_ke, const unsigned int ib, const unsigned int ie, const unsigned int j)
354 {
355 for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
356 for (unsigned int i = ib; i < ie; ++i)
357 local_ke[i] += datum.JxW(qp) * kernel.template computeQpOffDiagJacobian<Derived>(
358 i, j, datum.jvar(), qp, datum);
359 });
360}
361
362template <typename Derived>
363KOKKOS_FUNCTION void
365{
367 datum,
368 [&](Real * local_ke, const unsigned int ib, const unsigned int ie, const unsigned int j)
369 {
370 for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
371 for (unsigned int i = ib; i < ie; ++i)
372 local_ke[i] += datum.JxW(qp) * kernel.template computeQpOffDiagJacobianScalar<Derived>(
373 i, j, datum.jvar(), qp, datum);
374 });
375}
376
377} // namespace Moose::Kokkos
KOKKOS_FUNCTION ContiguousElementID kokkosBlockElementID(Moose::Kokkos::ThreadID tid) const
Get the contiguous element 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 jvar() const
Get the coupled variable number.
KOKKOS_FUNCTION const Assembly & kokkosAssembly() const
Get the const reference of the Kokkos assembly.
KOKKOS_FUNCTION Real JxW(const unsigned int qp)
Get the transformed Jacobian weight.
KOKKOS_FUNCTION void set_local_parallel(const unsigned int local_thread_id, const unsigned int num_local_threads)
Set local parallelization option.
KOKKOS_FUNCTION unsigned int n_qps() const
Get the number of local quadrature points.
The base class for Kokkos kernels.
The base class for a user to derive their own Kokkos kernels.
static auto defaultOffDiagJacobian()
KOKKOS_FUNCTION void operator()(ResidualLoop, const ThreadID tid, const Derived &kernel) const
The parallel computation entry functions called by Kokkos.
const VariablePhiGradient _grad_phi
Gradient of the current shape function.
static auto defaultOffDiagJacobianScalar()
virtual void computeJacobian() override
Dispatch diagonal and off-diagonal Jacobian calculation.
KOKKOS_FUNCTION void computeJacobianInternal(const Derived &kernel, AssemblyDatum &datum) const
Compute diagonal Jacobian.
const VariableTestGradient _grad_test
Gradient of the current test function.
virtual void computeResidual() override
Dispatch residual calculation.
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...
Kernel(const InputParameters &parameters)
Constructor.
KOKKOS_FUNCTION Real computeQpOffDiagJacobianScalar(const unsigned int, const unsigned int, const unsigned int, const unsigned int, AssemblyDatum &) const
Compute scalar off-diagonal Jacobian contribution on a quadrature point.
const VariableValue _u
Current solution at quadrature points.
const VariableTestValue _test
Current test function.
KOKKOS_FUNCTION void computeOffDiagJacobianInternal(const Derived &kernel, AssemblyDatum &datum) const
Compute off-diagonal Jacobian.
static InputParameters validParams()
static constexpr bool supports_scalar_jacobian
static auto defaultJacobian()
Functions used to check if users have overriden the hook methods, whose calculations can be skipped w...
KOKKOS_FUNCTION void computeOffDiagJacobianScalarInternal(const Derived &kernel, AssemblyDatum &datum) const
Compute scalar off-diagonal Jacobian.
const VariablePhiValue _phi
Current shape function.
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 VariableGradient _grad_u
Gradient of the current solution at quadrature points.
KOKKOS_FUNCTION const Mesh & kokkosMesh() const
Get the const reference of the Kokkos mesh.
Definition KokkosMesh.h:690
Variable _kokkos_var
Kokkos variable.
Thread _thread
Kokkos thread object.
KOKKOS_FUNCTION void computeJacobianInternal(AssemblyDatum &datum, function body) const
The common loop structure template for computing elemental Jacobian.
KOKKOS_FUNCTION void computeResidualInternal(AssemblyDatum &datum, function body) const
The common loop structure template for computing elemental residual.
KOKKOS_FUNCTION thread_id_type size() const
Get the total thread pool size.
The Kokkos wrapper classes for MOOSE-like shape function access.
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 unsigned int sys(unsigned int comp=0) const
Get the system number of a component.
MOOSE_KOKKOS_INDEX_TYPE ThreadID
const unsigned int invalid_uint