https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
14namespace Moose::Kokkos
15{
16
38class ADKernel : public KernelBase
39{
40public:
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
66protected:
70 virtual void dispatch();
71
99 bool _computing_residual = false;
104};
105
106template <typename Derived>
107KOKKOS_FUNCTION void
108ADKernel::operator()(ResidualLoop, const ThreadID tid, const Derived & kernel) const
109{
110 auto elem = kokkosBlockElementID(_thread(tid, 1));
111
112 AssemblyDatum datum(elem,
115 kokkosSystems(),
117 _kokkos_var.var());
118
119 datum.set_local_parallel(_thread(tid, 0), _thread.size(0));
120
122
123 kernel.computeResidualInternal(kernel, datum);
124}
125
126template <typename Derived>
127KOKKOS_FUNCTION void
128ADKernel::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
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 base class for a user to derive their own Kokkos kernels using automatic differentiation (AD).
const ADVariableTestValue _test
Current test function.
const ADVariableTestGradient _grad_test
Gradient of the current test function.
virtual void computeResidualAndJacobian() override
Compute this object's contribution to the residual and Jacobian simultaneously.
ADKernel(const InputParameters &parameters)
Constructor.
const ADVariablePhiGradient _grad_phi
Gradient of the current shape function.
KOKKOS_FUNCTION void computeResidualInternal(const Derived &kernel, AssemblyDatum &datum) const
Compute residual.
virtual void dispatch()
Dispatch parallel calculation.
bool _computing_jacobian
Whether computing Jacobian.
virtual void computeJacobian() override
Compute this object's contribution to the diagonal Jacobian entries.
const ADVariableGradient _grad_u
Gradient of the current solution at quadrature points.
KOKKOS_FUNCTION void operator()(ResidualLoop, const ThreadID tid, const Derived &kernel) const
The parallel computation entry function called by Kokkos.
virtual void computeResidual() override
Compute this object's contribution to the residual.
bool _computing_residual
Whether computing residual.
static InputParameters validParams()
const ADVariableValue _u
Current solution at quadrature points.
const ADVariablePhiValue _phi
Current shape function.
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.
KOKKOS_FUNCTION void do_derivatives(const bool flag)
Set whether to compute derivatives for automatic differentiation (AD)
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.
KOKKOS_FUNCTION Real JxW(const unsigned int qp)
Get the transformed Jacobian weight.
KOKKOS_FUNCTION unsigned int local_thread_id() const
Get the current local thread ID.
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.
KOKKOS_FUNCTION const ElementInfo & elem() const
Get the element information object.
Definition KokkosDatum.h:46
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.
Variable _kokkos_var
Kokkos variable.
Thread _thread
Kokkos thread object.
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 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.
MOOSE_KOKKOS_INDEX_TYPE ThreadID
DualNumber< Real, DNDerivativeType, false > ADReal
const unsigned int invalid_uint
ContiguousElementID id
Contiguous element ID.
Definition KokkosMesh.h:42