https://mooseframework.inl.gov
KokkosLinearSystemContributionObject.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 "KokkosMesh.h"
14 #include "KokkosSystem.h"
15 #include "KokkosVariable.h"
16 
17 #include "MooseObject.h"
18 #include "SetupInterface.h"
19 #include "FunctionInterface.h"
20 #include "UserObjectInterface.h"
21 #include "TransientInterface.h"
22 #include "PostprocessorInterface.h"
24 #include "Restartable.h"
25 #include "MeshChangedInterface.h"
26 #include "TaggingInterface.h"
28 
29 class InputParameters;
30 class FEProblemBase;
31 
32 namespace Moose::Kokkos
33 {
34 
41  public SetupInterface,
42  public FunctionInterface,
43  public UserObjectInterface,
44  public TransientInterface,
47  public Restartable,
48  public MeshChangedInterface,
49  public TaggingInterface,
50  public MeshHolder,
51  public SystemHolder
52 {
53 public:
55 
58 
63  Variable variable() const { return _var; }
64 
67  {
68  };
70  struct MatrixLoop
71  {
72  };
73 
77  virtual void computeRightHandSide() = 0;
81  virtual void computeMatrix() = 0;
82 
83 protected:
85  enum class AccumulationMode
86  {
87  Atomic,
88  NonAtomic
89  };
90 
91  template <AccumulationMode mode = AccumulationMode::Atomic>
92  KOKKOS_FUNCTION void accumulateTaggedVector(Real value, dof_id_type row) const;
93  template <AccumulationMode mode = AccumulationMode::Atomic>
94  KOKKOS_FUNCTION void accumulateTaggedMatrix(Real value, dof_id_type row, dof_id_type col) const;
95 
98 
101 
106 
108  std::unique_ptr<DispatcherBase> _rhs_dispatcher;
110  std::unique_ptr<DispatcherBase> _matrix_dispatcher;
111 
119 };
120 
121 template <LinearSystemContributionObject::AccumulationMode mode>
122 KOKKOS_FUNCTION inline void
124  const dof_id_type row) const
125 {
126  if (!value)
127  return;
128 
129  KOKKOS_ASSERT(_var.components() == 1);
130  auto & sys = kokkosSystem(_var.sys());
131  const MOOSE_KOKKOS_INDEX_TYPE tags_size = _vector_tags.size();
132  for (MOOSE_KOKKOS_INDEX_TYPE index = 0; index < tags_size; ++index)
133  {
134  const auto tag = _vector_tags[index];
135  if (sys.isResidualTagActive(tag))
136  {
137  if constexpr (mode == AccumulationMode::Atomic)
138  ::Kokkos::atomic_add(&sys.getVectorDofValue(row, tag), value);
139  else
140  sys.getVectorDofValue(row, tag) += value;
141  }
142  }
143 }
144 
145 template <LinearSystemContributionObject::AccumulationMode mode>
146 KOKKOS_FUNCTION inline void
148  const dof_id_type row,
149  const dof_id_type col) const
150 {
151  if (!value)
152  return;
153 
154  KOKKOS_ASSERT(_var.components() == 1);
155  auto & sys = kokkosSystem(_var.sys());
156  const MOOSE_KOKKOS_INDEX_TYPE tags_size = _matrix_tags.size();
157  for (MOOSE_KOKKOS_INDEX_TYPE index = 0; index < tags_size; ++index)
158  {
159  const auto tag = _matrix_tags[index];
160  if (sys.isMatrixTagActive(tag))
161  {
162  if constexpr (mode == AccumulationMode::Atomic)
163  ::Kokkos::atomic_add(&sys.getMatrixValue(row, col, tag), value);
164  else
165  sys.getMatrixValue(row, col, tag) += value;
166  }
167  }
168 }
169 
170 } // namespace Moose::Kokkos
KOKKOS_FUNCTION unsigned int sys(unsigned int comp=0) const
Get the system number of a component.
A class for creating restricted objects.
Definition: Restartable.h:28
Array< TagID > _matrix_tags
Matrix tags this object contributes to.
KOKKOS_FUNCTION void accumulateTaggedVector(Real value, dof_id_type row) const
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...
Base class for Kokkos objects that contribute to a linear system, i.e.
The Kokkos interface that holds the host reference of the Kokkos mesh and copies it to device during ...
Definition: KokkosMesh.h:629
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
Tag dispatch type for the right-hand side computation loop.
KOKKOS_FUNCTION unsigned int components() const
Get the number of components.
Interface for objects that needs transient capabilities.
LinearSystemContributionObject(const InputParameters &parameters)
virtual void computeMatrix()=0
Compute the matrix contributions of this object.
Interface for notifications that the mesh has changed.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
Every object that can be built by the factory should be derived from this class.
Definition: MooseObject.h:28
std::unique_ptr< DispatcherBase > _rhs_dispatcher
Dispatcher for the right-hand side computation loop.
KOKKOS_FUNCTION index_type size() const
Get the total array size.
Definition: KokkosArray.h:294
Interface for objects that need to use UserObjects.
KOKKOS_FUNCTION void accumulateTaggedMatrix(Real value, dof_id_type row, dof_id_type col) const
Variable variable() const
Get the Kokkos variable this object contributes to.
AccumulationMode
Whether tagged vector/matrix accumulation must use atomics.
virtual void computeRightHandSide()=0
Compute the right-hand side contributions of this object.
The Kokkos variable object that carries the coupled variable and tag information. ...
FEProblemBase & _fe_problem
Reference to the finite element problem.
Scalar< Real > _t
TODO: Move to TransientInterface.
Interface for objects that need to use functions.
std::unique_ptr< DispatcherBase > _matrix_dispatcher
Dispatcher for the matrix computation loop.
Interface class for classes which interact with Postprocessors.
uint8_t dof_id_type
Array< TagID > _vector_tags
Vector (residual) tags this object contributes to.