https://mooseframework.inl.gov
KokkosNodalKernel.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 "KokkosNodalKernelBase.h"
13 
14 namespace Moose
15 {
16 namespace Kokkos
17 {
18 
48 template <typename Derived>
50 {
51 public:
53 
58 
62  virtual void computeResidual() override;
66  virtual void computeJacobian() override;
67 
72 
78  KOKKOS_FUNCTION Real computeQpJacobian(const ContiguousNodeID /* node */) const { return 0; }
85  KOKKOS_FUNCTION Real computeQpOffDiagJacobian(const unsigned int /* jvar */,
86  const ContiguousNodeID /* node */) const
87  {
88  return 0;
89  }
91 
95  KOKKOS_FUNCTION void operator()(ResidualLoop, const ThreadID tid) const;
97  KOKKOS_FUNCTION void operator()(JacobianLoop, const ThreadID tid) const;
98  KOKKOS_FUNCTION void operator()(OffDiagJacobianLoop, const ThreadID tid) const;
100 
101 protected:
106 
107 private:
115  const bool _default_diag;
119  const bool _default_offdiag;
120 };
121 
122 template <typename Derived>
125 {
127  return params;
128 }
129 
130 template <typename Derived>
133  _u(kokkosSystems(), _var),
134  _boundary_restricted(boundaryRestricted()),
135  _default_diag(&Derived::computeQpJacobian == &NodalKernel::computeQpJacobian),
136  _default_offdiag(&Derived::computeQpOffDiagJacobian == &NodalKernel::computeQpOffDiagJacobian)
137 {
138 }
139 
140 template <typename Derived>
141 void
143 {
144  ::Kokkos::RangePolicy<ResidualLoop, ExecSpace, ::Kokkos::IndexType<ThreadID>> policy(
145  0, _boundary_restricted ? numKokkosBoundaryNodes() : numKokkosBlockNodes());
146  ::Kokkos::parallel_for(policy, *static_cast<Derived *>(this));
147  ::Kokkos::fence();
148 }
149 
150 template <typename Derived>
151 void
153 {
154  if (!_default_diag)
155  {
156  ::Kokkos::RangePolicy<JacobianLoop, ExecSpace, ::Kokkos::IndexType<ThreadID>> policy(
157  0, _boundary_restricted ? numKokkosBoundaryNodes() : numKokkosBlockNodes());
158  ::Kokkos::parallel_for(policy, *static_cast<Derived *>(this));
159  ::Kokkos::fence();
160  }
161 
162  if (!_default_offdiag)
163  {
164  auto & sys = kokkosSystem(_kokkos_var.sys());
165 
166  _thread.resize({sys.getCoupling(_kokkos_var.var()).size(),
167  _boundary_restricted ? numKokkosBoundaryNodes() : numKokkosBlockNodes()});
168 
169  ::Kokkos::RangePolicy<OffDiagJacobianLoop, ExecSpace, ::Kokkos::IndexType<ThreadID>> policy(
170  0, _thread.size());
171  ::Kokkos::parallel_for(policy, *static_cast<Derived *>(this));
172  ::Kokkos::fence();
173  }
174 }
175 
176 template <typename Derived>
177 KOKKOS_FUNCTION void
179 {
180  auto kernel = static_cast<const Derived *>(this);
181  auto node = _boundary_restricted ? kokkosBoundaryNodeID(tid) : kokkosBlockNodeID(tid);
182  auto & sys = kokkosSystem(_kokkos_var.sys());
183 
184  if (!sys.isNodalDefined(node, _kokkos_var.var()))
185  return;
186 
187  Real local_re = kernel->computeQpResidual(node);
188 
189  accumulateTaggedNodalResidual(true, local_re, node);
190 }
191 
192 template <typename Derived>
193 KOKKOS_FUNCTION void
195 {
196  auto kernel = static_cast<const Derived *>(this);
197  auto node = _boundary_restricted ? kokkosBoundaryNodeID(tid) : kokkosBlockNodeID(tid);
198  auto & sys = kokkosSystem(_kokkos_var.sys());
199 
200  if (!sys.isNodalDefined(node, _kokkos_var.var()))
201  return;
202 
203  Real local_ke = kernel->computeQpJacobian(node);
204 
205  accumulateTaggedNodalMatrix(true, local_ke, node, _kokkos_var.var());
206 }
207 
208 template <typename Derived>
209 KOKKOS_FUNCTION void
211 {
212  auto kernel = static_cast<const Derived *>(this);
213  auto node = _boundary_restricted ? kokkosBoundaryNodeID(_thread(tid, 1))
214  : kokkosBlockNodeID(_thread(tid, 1));
215  auto & sys = kokkosSystem(_kokkos_var.sys());
216  auto jvar = sys.getCoupling(_kokkos_var.var())[_thread(tid, 0)];
217 
218  if (!sys.isNodalDefined(node, _kokkos_var.var()))
219  return;
220 
221  Real local_ke = kernel->computeQpOffDiagJacobian(jvar, node);
222 
223  accumulateTaggedNodalMatrix(true, local_ke, node, jvar);
224 }
225 
226 } // namespace Kokkos
227 } // namespace Moose
228 
229 #define usingKokkosNodalKernelMembers(T) \
230  usingKokkosNodalKernelBaseMembers; \
231  \
232 protected: \
233  using Moose::Kokkos::NodalKernel<T>::_u; \
234  \
235 public: \
236  using Moose::Kokkos::NodalKernel<T>::operator()
const bool _default_offdiag
Flag whether computeQpOffDiagJacobian() was not defined in the derived class.
static InputParameters validParams()
VarFieldType
Definition: MooseTypes.h:722
dof_id_type ThreadID
Definition: KokkosThread.h:18
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...
The base class for a user to derive their own Kokkos nodal kernels.
virtual void computeJacobian() override
Compute the Jacobian at one node.
Definition: NodalKernel.C:88
The base class for Kokkos nodal kernels.
virtual void computeResidual() override
Dispatch residual calculation.
KOKKOS_FUNCTION Real computeQpOffDiagJacobian(const unsigned int, const ContiguousNodeID) const
Compute off-diagonal Jacobian contribution on a node.
dof_id_type ContiguousNodeID
Definition: KokkosMesh.h:21
virtual void computeResidual() override
Compute the residual at the current node.
Definition: NodalKernel.C:69
KOKKOS_FUNCTION Real computeQpJacobian(const ContiguousNodeID) const
Default methods to prevent compile errors even when these methods were not defined in the derived cla...
static InputParameters validParams()
virtual void computeJacobian() override
Dispatch diagonal and off-diagonal Jacobian calculation.
const VariableNodalValue _u
Current solution at nodes.
const bool _boundary_restricted
Flag whether this kernel is boundary-restricted.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
NodalKernel(const InputParameters &parameters)
Constructor.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
bool boundaryRestricted(const std::set< BoundaryID > &boundary_ids)
const bool _default_diag
Flag whether computeJacobian() was not defined in the derived class.
KOKKOS_FUNCTION void operator()(ResidualLoop, const ThreadID tid) const
The parallel computation entry functions called by Kokkos.