https://mooseframework.inl.gov
KokkosSystem.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 "KokkosTypes.h"
13 #include "KokkosVector.h"
14 #include "KokkosMatrix.h"
15 #include "PerfGraphInterface.h"
16 #include "KokkosMesh.h"
17 
18 #include "libmesh/communicator.h"
19 
20 class MooseMesh;
21 class SystemBase;
22 
23 namespace Moose::Kokkos
24 {
25 
30 class System : public PerfGraphInterface, public MeshHolder
31 {
32 public:
37  System(SystemBase & system);
38 
43  System(const System & src) = default;
44 
48  struct Sparsity
49  {
53  };
54 
55 #ifdef MOOSE_KOKKOS_SCOPE
56 
60  void sync(const MemcpyType dir);
66  void sync(const std::set<TagID> & tags, const MemcpyType dir);
68  void sync(const std::vector<TagID> & tags, const MemcpyType dir);
69  void sync(const TagID tag, const MemcpyType dir);
71 
76  void setActiveVariables(const std::set<MooseVariableFieldBase *> & vars);
77 
82  void setActiveSolutionTags(const std::set<TagID> & tags);
83 
88  void setActiveResidualTags(const std::set<TagID> & tags);
89 
94  void setActiveMatrixTags(const std::set<TagID> & tags);
95 
100 
105 
110  {
112  _residual_tag_active = false;
113  }
114 
119  {
121  _matrix_tag_active = false;
122  }
123 
128  SystemBase & getSystem() { return _system; }
130  const SystemBase & getSystem() const { return _system; }
132 
137  const libMesh::DofMap & getDofMap() const { return _dof_map; }
138 
143  const Parallel::Communicator & getComm() const { return _comm; }
144 
150 
156 
161  const Sparsity & getSparsity() const { return _sparsity; }
162 
169  KOKKOS_FUNCTION bool isVariableActive(unsigned int var, ContiguousSubdomainID subdomain) const
170  {
171  return _var_subdomain_active(var, subdomain);
172  }
173 
179  KOKKOS_FUNCTION bool isResidualTagActive(TagID tag) const { return _residual_tag_active[tag]; }
180 
186  KOKKOS_FUNCTION bool isMatrixTagActive(TagID tag) const { return _matrix_tag_active[tag]; }
187 
192  KOKKOS_FUNCTION dof_id_type getNumLocalDofs() const { return _num_local_dofs; }
193 
198  KOKKOS_FUNCTION dof_id_type getNumGhostDofs() const { return _num_ghost_dofs; }
199 
208  unsigned int i,
209  unsigned int var) const
210  {
211  return _local_elem_dof_index[var](i, elem);
212  }
213 
222  unsigned int i,
223  unsigned int var) const
224  {
226  }
227 
233  KOKKOS_FUNCTION dof_id_type localToGlobalDofIndex(dof_id_type dof) const
234  {
235  return _local_to_global_dof_index[dof];
236  }
237 
243  KOKKOS_FUNCTION Vector & getVector(TagID tag) const { return _vectors[tag]; }
244 
250  KOKKOS_FUNCTION Matrix & getMatrix(TagID tag) const { return _matrices[tag]; }
251 
258  KOKKOS_FUNCTION Real & getVectorDofValue(const dof_id_type dof, const TagID tag) const
259  {
260  return _vectors[tag][dof];
261  }
262 
270  KOKKOS_FUNCTION Real & getMatrixValue(dof_id_type row, dof_id_type col, TagID tag) const
271  {
272  return _matrices[tag](row, col);
273  }
274 
275 #endif
276 
277 protected:
282 
286  const MooseMesh & _mesh;
287 
292 
297 
301  const unsigned int _num_vars;
302 
307 
312 
320 
325 
330 
335 
340 
345 
354 
362 
370 
371 private:
375  void setupVariables();
376 
380  void setupDofs();
381 
385  void setupSparsity();
386 
391 };
392 
393 #ifdef MOOSE_KOKKOS_SCOPE
394 #define MakeSystemHolderMethods(SystemTypeName) \
395  KOKKOS_FUNCTION const Array<SystemTypeName> & kokkosSystems() const \
396  { \
397  KOKKOS_IF_ON_HOST(return _systems_host;) \
398  return _systems_device; \
399  } \
400  Array<SystemTypeName> & kokkosSystems() { return _systems_host; } \
401  KOKKOS_FUNCTION const SystemTypeName & kokkosSystem(unsigned int sys) const \
402  { \
403  KOKKOS_IF_ON_HOST(return _systems_host[sys];) \
404  return _systems_device[sys]; \
405  } \
406  SystemTypeName & kokkosSystem(unsigned int sys) { return _systems_host[sys]; }
407 #else
408 #define MakeSystemHolderMethods(SystemTypeName)
409 #endif
410 
417 // clang-format off
418 #define MakeSystemHolder(SystemTypeName) \
419  class SystemTypeName##Holder \
420  { \
421  public: \
422  SystemTypeName##Holder(Array<SystemTypeName> & systems) \
423  : _systems_host(systems), _systems_device(systems) \
424  { \
425  } \
426  SystemTypeName##Holder(const SystemTypeName##Holder & holder) \
427  : _systems_host(holder._systems_host), _systems_device(holder._systems_host) \
428  { \
429  } \
430  MakeSystemHolderMethods(SystemTypeName) \
431  private: \
432  Array<SystemTypeName> & _systems_host; \
433  const Array<SystemTypeName> _systems_device; \
434  }
435 // clang-format on
436 
438 } // namespace Moose::Kokkos
KOKKOS_FUNCTION dof_id_type getElemLocalDofIndex(ContiguousElementID elem, unsigned int i, unsigned int var) const
Get the local DOF index of a variable for an element.
Definition: KokkosSystem.h:207
MemcpyType
The enumerator that dictates the memory copy direction.
Definition: KokkosArray.h:41
void sync(const MemcpyType dir)
Synchronize the active tagged vectors and matrices between host and device.
KOKKOS_FUNCTION Real & getMatrixValue(dof_id_type row, dof_id_type col, TagID tag) const
Get an entry from a tagged matrix.
Definition: KokkosSystem.h:270
void clearActiveResidualTags()
Clear the cached active residual tags.
Definition: KokkosSystem.h:109
Array< dof_id_type > _local_to_global_dof_index
Map from local DOF index to global DOF index.
Definition: KokkosSystem.h:329
Array< bool > _residual_tag_active
Flag whether each tag is active.
Definition: KokkosSystem.h:359
void setActiveSolutionTags(const std::set< TagID > &tags)
Set the active solution tags.
unsigned int TagID
Definition: MooseTypes.h:238
dof_id_type ContiguousElementID
Definition: KokkosMesh.h:20
KOKKOS_FUNCTION dof_id_type getNumGhostDofs() const
Get the number of ghost DOFs.
Definition: KokkosSystem.h:198
KOKKOS_FUNCTION dof_id_type getNumLocalDofs() const
Get the number of local DOFs.
Definition: KokkosSystem.h:192
char ** vars
const MooseMesh & _mesh
Reference of the MOOSE mesh.
Definition: KokkosSystem.h:286
SystemBase & getSystem()
Get the MOOSE system.
Definition: KokkosSystem.h:129
void setupSparsity()
Setup sparsity data.
Base class for a system (of equations)
Definition: SystemBase.h:85
The Kokkos base system class.
Definition: KokkosSystem.h:30
The Kokkos interface that holds the host reference of the Kokkos mesh and copies it to device during ...
Definition: KokkosMesh.h:629
The Kokkos wrapper class for PETSc matrix.
Definition: KokkosMatrix.h:28
KOKKOS_FUNCTION Real & getVectorDofValue(const dof_id_type dof, const TagID tag) const
Get the DOF value of a tagged vector.
Definition: KokkosSystem.h:258
Array< TagID > _active_matrix_tags
Definition: KokkosSystem.h:352
const Array< Array< dof_id_type > > & getLocalCommList() const
Get the list of local DOF indices to communicate.
Definition: KokkosSystem.h:149
SystemBase & _system
Reference of the MOOSE system.
Definition: KokkosSystem.h:281
const Array< Array< dof_id_type > > & getGhostCommList() const
Get the list of ghost DOF indices to communicate.
Definition: KokkosSystem.h:155
void clearActiveMatrixTags()
Clear the cached active matrix tags.
Definition: KokkosSystem.h:118
void setupVariables()
Setup variable data.
Array< Vector > _vectors
Kokkos vectors and matrices on device.
Definition: KokkosSystem.h:317
KOKKOS_FUNCTION dof_id_type localToGlobalDofIndex(dof_id_type dof) const
Get the global DOF index of a local DOF index.
Definition: KokkosSystem.h:233
KOKKOS_FUNCTION bool isVariableActive(unsigned int var, ContiguousSubdomainID subdomain) const
Check whether a variable is active on a subdomain.
Definition: KokkosSystem.h:169
KOKKOS_FUNCTION bool isResidualTagActive(TagID tag) const
Check whether a residual tag is active.
Definition: KokkosSystem.h:179
The Kokkos wrapper class for PETSc vector.
Definition: KokkosVector.h:25
System(SystemBase &system)
Constructor.
const unsigned int _num_vars
Number of variables.
Definition: KokkosSystem.h:301
void clearActiveSolutionTags()
Clear the cached active solution tags.
Definition: KokkosSystem.h:104
void clearActiveVariables()
Clear the cached active variables.
Definition: KokkosSystem.h:99
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:94
void setupDofs()
Setup DOF data.
void setActiveVariables(const std::set< MooseVariableFieldBase *> &vars)
Set the active variables.
Array< TagID > _active_residual_tags
Definition: KokkosSystem.h:351
void setActiveResidualTags(const std::set< TagID > &tags)
Set the active residual tags.
KOKKOS_FUNCTION dof_id_type getElemGlobalDofIndex(ContiguousElementID elem, unsigned int i, unsigned int var) const
Get the global DOF index of a variable for an element.
Definition: KokkosSystem.h:221
const SystemBase & getSystem() const
Definition: KokkosSystem.h:130
const dof_id_type _num_local_dofs
Number of local DOFs.
Definition: KokkosSystem.h:306
void destroy()
Free all data and reset.
Definition: KokkosArray.h:896
KOKKOS_FUNCTION bool isMatrixTagActive(TagID tag) const
Check whether a matrix tag is active.
Definition: KokkosSystem.h:186
void setActiveMatrixTags(const std::set< TagID > &tags)
Set the active matrix tags.
Interface for objects interacting with the PerfGraph.
Array< Array< dof_id_type > > _ghost_comm_list
Definition: KokkosSystem.h:368
Array< Array< dof_id_type > > _local_comm_list
List of DOFs to send and receive.
Definition: KokkosSystem.h:367
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const libMesh::DofMap & getDofMap() const
Get the libMesh DOF map.
Definition: KokkosSystem.h:137
const Sparsity & getSparsity() const
Get the sparisty pattern data.
Definition: KokkosSystem.h:161
MakeSystemHolder(FESystem)
KOKKOS_FUNCTION Vector & getVector(TagID tag) const
Get a tagged Kokkos vector.
Definition: KokkosSystem.h:243
Array< unsigned int > _active_variables
List of active variable numbers.
Definition: KokkosSystem.h:344
const Parallel::Communicator & _comm
Reference of the libMesh communicator.
Definition: KokkosSystem.h:296
CSR format sparsity data.
Definition: KokkosSystem.h:48
Array2D< bool > _var_subdomain_active
Whether each variable is active on subdomains.
Definition: KokkosSystem.h:339
const libMesh::DofMap & _dof_map
Reference of the libMesh DOF map.
Definition: KokkosSystem.h:291
const dof_id_type _num_ghost_dofs
Number of ghost DOFs.
Definition: KokkosSystem.h:311
KOKKOS_FUNCTION Matrix & getMatrix(TagID tag) const
Get a tagged Kokkos matrix.
Definition: KokkosSystem.h:250
Array< TagID > _active_solution_tags
List of active tags.
Definition: KokkosSystem.h:350
Sparsity _sparsity
Matrix sparsity pattern data.
Definition: KokkosSystem.h:390
Array< unsigned int > _max_dofs_per_elem
Maximum number of DOFs per element for each variable.
Definition: KokkosSystem.h:334
Array< Array2D< dof_id_type > > _local_elem_dof_index
Local element DOF indices of each variable.
Definition: KokkosSystem.h:324
Array< bool > _matrix_tag_active
Definition: KokkosSystem.h:360
const Parallel::Communicator & getComm() const
Get the libMesh communicator.
Definition: KokkosSystem.h:143
uint8_t dof_id_type
Array< Matrix > _matrices
Definition: KokkosSystem.h:318