https://mooseframework.inl.gov
Loading...
Searching...
No Matches
KokkosResidualObject.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 "KokkosDispatcher.h"
13#include "KokkosFESystem.h"
14#include "KokkosVariableValue.h"
16
17#include "MooseVariableBase.h"
18#include "ResidualObject.h"
19
20namespace Moose::Kokkos
21{
22
27 public MeshHolder,
28 public AssemblyHolder,
29 public FESystemHolder
30{
31public:
33
35 static constexpr bool uses_precompute_hooks = false;
36
38 static constexpr bool supports_scalar_jacobian = false;
39
46 Moose::VarFieldType field_type,
47 bool nodal = false);
52
58 {
59 };
61 {
62 };
64 {
65 };
67 {
68 };
70
71 virtual const MooseVariableBase & variable() const override { return _var; }
72
73 virtual void computeOffDiagJacobian(unsigned int) override final
74 {
75 mooseError("computeOffDiagJacobian() is not used for Kokkos residual objects.");
76 }
77 virtual void computeResidualAndJacobian() override
78 {
81 }
82
83protected:
100 std::unique_ptr<DispatcherBase> _residual_dispatcher;
101 std::unique_ptr<DispatcherBase> _jacobian_dispatcher;
102 std::unique_ptr<DispatcherBase> _offdiag_jacobian_dispatcher;
103 std::unique_ptr<DispatcherBase> _offdiag_jacobian_scalar_dispatcher;
105
109 const unsigned int _dimension;
110
115
136
144 KOKKOS_FUNCTION void accumulateTaggedElementalResidual(const Real local_re,
145 const ContiguousElementID elem,
146 const unsigned int i,
147 const unsigned int comp = 0) const;
155 KOKKOS_FUNCTION void accumulateTaggedNodalResidual(const bool add,
156 const Real local_re,
157 const ContiguousNodeID node,
158 const unsigned int comp = 0) const;
166 KOKKOS_FUNCTION void accumulateTaggedVectorNodalResidual(const bool add,
167 const Real local_re,
168 const ContiguousNodeID node,
169 const unsigned int i) const;
179 KOKKOS_FUNCTION void accumulateTaggedElementalMatrix(const Real local_ke,
180 const ContiguousElementID elem,
181 const unsigned int i,
182 const unsigned int j,
183 const unsigned int jvar,
184 const unsigned int comp = 0) const;
193 KOKKOS_FUNCTION void accumulateTaggedElementalMatrix(const DNDerivativeType & local_ke,
194 const AssemblyDatum & datum,
195 const unsigned int i,
196 const unsigned int comp = 0) const;
205 KOKKOS_FUNCTION void accumulateTaggedNodalMatrix(const bool add,
206 const Real local_ke,
207 const ContiguousNodeID node,
208 const unsigned int jvar,
209 const unsigned int comp = 0) const;
219 KOKKOS_FUNCTION void accumulateTaggedVectorNodalMatrix(const bool add,
220 const Real local_ke,
221 const ContiguousNodeID node,
222 const unsigned int i,
223 const unsigned int j,
224 const unsigned int jvar) const;
233 KOKKOS_FUNCTION void accumulateTaggedNodalMatrix(const bool add,
234 const DNDerivativeType & local_ke,
235 const ContiguousNodeID node,
236 const unsigned int comp = 0) const;
237
243 template <typename function>
244 KOKKOS_FUNCTION void computeResidualInternal(AssemblyDatum & datum, function body) const;
250 template <typename function>
251 KOKKOS_FUNCTION void computeJacobianInternal(AssemblyDatum & datum, function body) const;
252
253private:
261};
262
263KOKKOS_FUNCTION inline void
265 const ContiguousElementID elem,
266 const unsigned int i,
267 const unsigned int comp) const
268{
269 if (!local_re)
270 return;
271
272 auto & sys = kokkosSystem(_kokkos_var.sys(comp));
273 auto dof = sys.getElemLocalDofIndex(elem, i, _kokkos_var.var(comp));
274
275 for (unsigned int t = 0; t < _vector_tags.size(); ++t)
276 {
277 auto tag = _vector_tags[t];
278
279 if (sys.isResidualTagActive(tag))
280 ::Kokkos::atomic_add(&sys.getVectorDofValue(dof, tag), local_re);
281 }
282}
283
284KOKKOS_FUNCTION inline void
286 const Real local_re,
287 const ContiguousNodeID node,
288 const unsigned int comp) const
289{
290 if (!local_re && add)
291 return;
292
293 auto & sys = kokkosSystem(_kokkos_var.sys(comp));
294 auto dof = sys.getNodeLocalDofIndex(node, 0, _kokkos_var.var(comp));
295
296 for (unsigned int t = 0; t < _vector_tags.size(); ++t)
297 {
298 auto tag = _vector_tags[t];
299
300 if (sys.isResidualTagActive(tag))
301 {
302 if (add)
303 sys.getVectorDofValue(dof, tag) += local_re;
304 else
305 sys.getVectorDofValue(dof, tag) = local_re;
306 }
307 }
308}
309
310KOKKOS_FUNCTION inline void
312 const Real local_re,
313 const ContiguousNodeID node,
314 const unsigned int i) const
315{
316 if (!local_re && add)
317 return;
318
319 auto & sys = kokkosSystem(_kokkos_var.sys());
320 auto dof = sys.getNodeLocalDofIndex(node, i, _kokkos_var.var());
321
322 for (unsigned int t = 0; t < _vector_tags.size(); ++t)
323 {
324 auto tag = _vector_tags[t];
325
326 if (sys.isResidualTagActive(tag))
327 {
328 if (add)
329 sys.getVectorDofValue(dof, tag) += local_re;
330 else
331 sys.getVectorDofValue(dof, tag) = local_re;
332 }
333 }
334}
335
336KOKKOS_FUNCTION inline void
338 const ContiguousElementID elem,
339 const unsigned int i,
340 const unsigned int j,
341 const unsigned int jvar,
342 const unsigned int comp) const
343{
344 if (!local_ke)
345 return;
346
347 auto & sys = kokkosSystem(_kokkos_var.sys(comp));
348 auto row = sys.getElemLocalDofIndex(elem, i, _kokkos_var.var(comp));
349 auto col = sys.isScalarVariable(jvar) ? sys.getScalarGlobalDofIndex(j, jvar)
350 : sys.getElemGlobalDofIndex(elem, j, jvar);
351
352 for (unsigned int t = 0; t < _matrix_tags.size(); ++t)
353 {
354 auto tag = _matrix_tags[t];
355
356 if (sys.isMatrixTagActive(tag) && !sys.hasNodalBCMatrixTag(row, tag))
357 ::Kokkos::atomic_add(&sys.getMatrixValue(row, col, tag), local_ke);
358 }
359}
360
361KOKKOS_FUNCTION inline void
363 const AssemblyDatum & datum,
364 const unsigned int i,
365 const unsigned int comp) const
366{
367 auto & sys = kokkosSystem(_kokkos_var.sys(comp));
368 auto row = sys.getElemLocalDofIndex(datum.elem().id, i, _kokkos_var.var(comp));
369
370 for (unsigned int t = 0; t < _matrix_tags.size(); ++t)
371 {
372 auto tag = _matrix_tags[t];
373
374 if (sys.isMatrixTagActive(tag) && !sys.hasNodalBCMatrixTag(row, tag))
375 for (unsigned int j = 0; j < local_ke.size(); ++j)
376 {
377 auto col = local_ke.raw_index(j);
378
379 ::Kokkos::atomic_add(&sys.getMatrixValue(row, col, tag), local_ke.raw_at(j));
380 }
381 }
382}
383
384KOKKOS_FUNCTION inline void
386 const Real local_ke,
387 const ContiguousNodeID node,
388 const unsigned int jvar,
389 const unsigned int comp) const
390{
391 if (!local_ke && add)
392 return;
393
394 auto & sys = kokkosSystem(_kokkos_var.sys(comp));
395 auto row = sys.getNodeLocalDofIndex(node, 0, _kokkos_var.var(comp));
396 auto col = sys.getNodeGlobalDofIndex(node, 0, jvar);
397
398 for (unsigned int t = 0; t < _matrix_tags.size(); ++t)
399 {
400 auto tag = _matrix_tags[t];
401
402 if (sys.isMatrixTagActive(tag))
403 {
404 auto & matrix = sys.getMatrix(tag);
405
406 if (add)
407 matrix(row, col) += local_ke;
408 else
409 {
410 matrix.zero(row);
411 matrix(row, col) = local_ke;
412 }
413 }
414 }
415}
416
417KOKKOS_FUNCTION inline void
419 const Real local_ke,
420 const ContiguousNodeID node,
421 const unsigned int i,
422 const unsigned int j,
423 const unsigned int jvar) const
424{
425 if (!local_ke && add)
426 return;
427
428 auto & sys = kokkosSystem(_kokkos_var.sys());
429 auto row = sys.getNodeLocalDofIndex(node, i, _kokkos_var.var());
430 auto col = sys.getNodeGlobalDofIndex(node, j, jvar);
431
432 for (unsigned int t = 0; t < _matrix_tags.size(); ++t)
433 {
434 auto tag = _matrix_tags[t];
435
436 if (sys.isMatrixTagActive(tag))
437 {
438 auto & matrix = sys.getMatrix(tag);
439
440 if (add)
441 matrix(row, col) += local_ke;
442 else
443 {
444 matrix.zero(row);
445 matrix(row, col) = local_ke;
446 }
447 }
448 }
449}
450
451KOKKOS_FUNCTION inline void
453 const DNDerivativeType & local_ke,
454 const ContiguousNodeID node,
455 const unsigned int comp) const
456{
457 auto & sys = kokkosSystem(_kokkos_var.sys(comp));
458 auto row = sys.getNodeLocalDofIndex(node, 0, _kokkos_var.var(comp));
459
460 for (unsigned int t = 0; t < _matrix_tags.size(); ++t)
461 {
462 auto tag = _matrix_tags[t];
463 auto & matrix = sys.getMatrix(tag);
464
465 if (sys.isMatrixTagActive(tag))
466 {
467 if (!add)
468 matrix.zero(row);
469
470 for (unsigned int j = 0; j < local_ke.size(); ++j)
471 {
472 auto col = local_ke.raw_index(j);
473
474 if (add)
475 matrix(row, col) += local_ke.raw_at(j);
476 else
477 matrix(row, col) = local_ke.raw_at(j);
478 }
479 }
480 }
481}
482
483template <typename function>
484KOKKOS_FUNCTION void
486{
487 Real local_re[MAX_CACHED_DOF];
488
489 unsigned int stride = MAX_CACHED_DOF * datum.num_local_threads();
490 unsigned int num_batches = datum.n_dofs() / stride;
491
492 if (datum.n_dofs() % stride)
493 ++num_batches;
494
495 for (unsigned int batch = 0; batch < num_batches; ++batch)
496 {
497 unsigned int ib = batch * stride;
498 unsigned int ie = ::Kokkos::min(ib + stride, datum.n_dofs());
499
500 const unsigned int n = ie - ib;
501 const unsigned int d = n / datum.num_local_threads();
502 const unsigned int m = n % datum.num_local_threads();
503 const unsigned int t = datum.local_thread_id();
504
505 ib += t * d + (t < m ? t : m);
506 ie = ib + d + (t < m ? 1 : 0);
507
508 for (unsigned int i = ib; i < ie; ++i)
509 local_re[i - ib] = 0;
510
511 body(local_re - ib, ib, ie);
512
513 for (unsigned int i = ib; i < ie; ++i)
514 accumulateTaggedElementalResidual(local_re[i - ib], datum.elem().id, i);
515 }
516}
517
518template <typename function>
519KOKKOS_FUNCTION void
521{
522 Real local_ke[MAX_CACHED_DOF];
523
524 for (unsigned int j = datum.local_thread_id(); j < datum.n_jdofs();
525 j += datum.num_local_threads())
526 {
527 unsigned int num_batches = datum.n_idofs() / MAX_CACHED_DOF;
528
529 if (datum.n_idofs() % MAX_CACHED_DOF)
530 ++num_batches;
531
532 for (unsigned int batch = 0; batch < num_batches; ++batch)
533 {
534 unsigned int ib = batch * MAX_CACHED_DOF;
535 unsigned int ie = ::Kokkos::min(ib + MAX_CACHED_DOF, datum.n_idofs());
536
537 for (unsigned int i = ib; i < ie; ++i)
538 local_ke[i - ib] = 0;
539
540 body(local_ke - ib, ib, ie, j);
541
542 for (unsigned int i = ib; i < ie; ++i)
543 accumulateTaggedElementalMatrix(local_ke[i - ib], datum.elem().id, i, j, datum.jvar());
544 }
545 }
546}
547
548} // namespace Moose::Kokkos
dof_id_type ContiguousElementID
Definition KokkosMesh.h:20
dof_id_type ContiguousNodeID
Definition KokkosMesh.h:21
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
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition MooseBase.h:271
Base variable class.
This class provides an interface for common operations on field variables of both FE and FV types wit...
KOKKOS_FUNCTION index_type size() const
Get the total array size.
The Kokkos array class.
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 unsigned int jvar() const
Get the coupled variable number.
KOKKOS_FUNCTION unsigned int n_idofs() const
Get the number of local DOFs.
KOKKOS_FUNCTION unsigned int n_jdofs() const
Get the number of local DOFs for the coupled variable.
The Kokkos interface that holds the host reference of the Kokkos assembly and copies it to device dur...
KOKKOS_FUNCTION unsigned int num_local_threads() const
Get the number of local threads.
KOKKOS_FUNCTION unsigned int local_thread_id() const
Get the current local thread ID.
KOKKOS_FUNCTION const ElementInfo & elem() const
Get the element information object.
Definition KokkosDatum.h:46
The Kokkos interface that holds the host reference of the Kokkos mesh and copies it to device during ...
Definition KokkosMesh.h:630
The base class for Kokkos residual objects.
virtual void computeResidualAndJacobian() override
Compute this object's contribution to the residual and Jacobian simultaneously.
Scalar< const Real > _t_old
Old time.
ResidualObject(const InputParameters &parameters, Moose::VarFieldType field_type, bool nodal=false)
Constructor.
std::unique_ptr< DispatcherBase > _residual_dispatcher
Kokkos functor dispatchers.
Scalar< Real > _t
TODO: Move to TransientInterface.
Scalar< Real > _dt_old
Size of the old time step.
KOKKOS_FUNCTION void accumulateTaggedVectorNodalMatrix(const bool add, const Real local_ke, const ContiguousNodeID node, const unsigned int i, const unsigned int j, const unsigned int jvar) const
Accumulate or set local nodal Jacobian contribution to tagged matrices for vector FE variables.
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.
ResidualObject(const ResidualObject &object)
Copy constructor for parallel dispatch.
Variable _kokkos_var
Kokkos variable.
Thread _thread
Kokkos thread object.
KOKKOS_FUNCTION void accumulateTaggedNodalMatrix(const bool add, const Real local_ke, const ContiguousNodeID node, const unsigned int jvar, const unsigned int comp=0) const
Accumulate or set local nodal Jacobian contribution to tagged matrices.
const unsigned int _dimension
Mesh dimension.
static InputParameters validParams()
std::unique_ptr< DispatcherBase > _offdiag_jacobian_scalar_dispatcher
static constexpr bool uses_precompute_hooks
Whether this object's hooks factor out the test function.
KOKKOS_FUNCTION void accumulateTaggedNodalResidual(const bool add, const Real local_re, const ContiguousNodeID node, const unsigned int comp=0) const
Accumulate or set local nodal residual contribution to tagged vectors.
Array< TagID > _vector_tags
Tags this object operates on.
KOKKOS_FUNCTION void accumulateTaggedVectorNodalResidual(const bool add, const Real local_re, const ContiguousNodeID node, const unsigned int i) const
Accumulate or set local nodal residual contribution to tagged vectors for vector FE variables.
MooseVariableFieldBase & _var
Reference of the MOOSE variable.
virtual void computeOffDiagJacobian(unsigned int) override final
Computes this object's contribution to off-diagonal blocks of the system Jacobian matrix.
static constexpr bool supports_scalar_jacobian
Whether this object has computeQpOffDiagJacobianScalar() hook.
KOKKOS_FUNCTION void computeJacobianInternal(AssemblyDatum &datum, function body) const
The common loop structure template for computing elemental Jacobian.
Scalar< Real > _dt
Time step size.
std::unique_ptr< DispatcherBase > _offdiag_jacobian_dispatcher
virtual const MooseVariableBase & variable() const override
Returns the variable that this object operates on.
std::unique_ptr< DispatcherBase > _jacobian_dispatcher
Scalar< int > _t_step
The number of the time step.
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 void computeResidualInternal(AssemblyDatum &datum, function body) const
The common loop structure template for computing elemental residual.
The Kokkos wrapper class that can hold the reference of an arithmetic scalar variable.
The Kokkos thread object that aids in converting the one-dimensional thread index into multi-dimensio...
The Kokkos variable object that carries the coupled variable and tag information.
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.
This is the common base class for objects that give residual contributions.
virtual void computeResidual()=0
Compute this object's contribution to the residual.
virtual void computeJacobian()=0
Compute this object's contribution to the diagonal Jacobian entries.
KokkosSemiDynamicSparseNumberArray< Real, libMesh::dof_id_type, NWrapper< MOOSE_AD_MAX_DOFS_PER_ELEM > > DNDerivativeType
constexpr unsigned int MAX_CACHED_DOF
Maximum number of DOFs to cache during residual and Jacobian computation.
ContiguousElementID id
Contiguous element ID.
Definition KokkosMesh.h:42