https://mooseframework.inl.gov
Loading...
Searching...
No Matches
KokkosSystem.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 "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
20class MooseMesh;
21class SystemBase;
22
23namespace Moose::Kokkos
24{
25
30class System : public PerfGraphInterface, public MeshHolder
31{
32public:
37 System(SystemBase & system);
38
43 System(const System & src) = default;
44
53
54#ifdef MOOSE_KOKKOS_SCOPE
59 void sync(const MemcpyType dir);
66 void sync(const std::set<TagID> & tags, const MemcpyType dir);
67 void sync(const std::vector<TagID> & tags, const MemcpyType dir);
68 void sync(const TagID tag, const MemcpyType dir);
70
75 void setActiveVariables(const std::set<MooseVariableFieldBase *> & vars);
76
81 void setActiveSolutionTags(const std::set<TagID> & tags);
82
87 void setActiveResidualTags(const std::set<TagID> & tags);
88
93 void setActiveMatrixTags(const std::set<TagID> & tags);
94
99
104
113
122
129 const SystemBase & getSystem() const { return _system; }
131
136 const libMesh::DofMap & getDofMap() const { return _dof_map; }
137
142 const Parallel::Communicator & getComm() const { return _comm; }
143
149
155
160 const Sparsity & getSparsity() const { return _sparsity; }
161
172 void buildCooIndices(std::vector<PetscInt> & coo_i, std::vector<PetscInt> & coo_j) const;
173
179 KOKKOS_FUNCTION bool isScalarVariable(unsigned int var) const { return _var_is_scalar[var]; }
180
186 KOKKOS_FUNCTION Real getVariableScalingFactor(unsigned int var) const
187 {
188 return _var_scaling_factor[var];
189 }
190
197 KOKKOS_FUNCTION bool isVariableActive(unsigned int var, ContiguousSubdomainID subdomain) const
198 {
199 return _var_subdomain_active(var, subdomain);
200 }
201
207 KOKKOS_FUNCTION bool isResidualTagActive(TagID tag) const { return _residual_tag_active[tag]; }
208
214 KOKKOS_FUNCTION bool isMatrixTagActive(TagID tag) const { return _matrix_tag_active[tag]; }
215
220 KOKKOS_FUNCTION dof_id_type getNumLocalDofs() const { return _num_local_dofs; }
221
226 KOKKOS_FUNCTION dof_id_type getNumGhostDofs() const { return _num_ghost_dofs; }
227
233 KOKKOS_FUNCTION unsigned int getNumScalarDofs(unsigned int var) const
234 {
235 return _scalar_dof_index[var].size();
236 }
237
245 KOKKOS_FUNCTION dof_id_type getElemLocalDofIndex(ContiguousElementID elem,
246 unsigned int i,
247 unsigned int var) const
248 {
249 return _local_elem_dof_index[var](i, elem);
250 }
251
258 KOKKOS_FUNCTION dof_id_type getScalarLocalDofIndex(unsigned int i, unsigned int var) const
259 {
260 return _scalar_dof_index[var][i];
261 }
262
270 KOKKOS_FUNCTION dof_id_type getElemGlobalDofIndex(ContiguousElementID elem,
271 unsigned int i,
272 unsigned int var) const
273 {
275 }
276
283 KOKKOS_FUNCTION dof_id_type getScalarGlobalDofIndex(unsigned int i, unsigned int var) const
284 {
286 }
287
293 KOKKOS_FUNCTION dof_id_type localToGlobalDofIndex(dof_id_type dof) const
294 {
295 return _local_to_global_dof_index[dof];
296 }
297
303 KOKKOS_FUNCTION Vector & getVector(TagID tag) const { return _vectors[tag]; }
304
310 KOKKOS_FUNCTION Matrix & getMatrix(TagID tag) const { return _matrices[tag]; }
311
318 KOKKOS_FUNCTION Real & getVectorDofValue(const dof_id_type dof, const TagID tag) const
319 {
320 return _vectors[tag][dof];
321 }
322
330 KOKKOS_FUNCTION Real & getMatrixValue(dof_id_type row, dof_id_type col, TagID tag) const
331 {
332 return _matrices[tag](row, col);
333 }
334
335#endif
336
337protected:
342
347
352
357
361 const unsigned int _num_vars;
362
366 const dof_id_type _num_local_dofs;
367
371 const dof_id_type _num_ghost_dofs;
372
380
385
390
395
400
405
410
415
420
429
437
445
446private:
451
456
460 void setupDofs();
461
466
471};
472
473#ifdef MOOSE_KOKKOS_SCOPE
474#define MakeSystemHolderMethods(SystemTypeName) \
475 KOKKOS_FUNCTION const Array<SystemTypeName> & kokkosSystems() const \
476 { \
477 KOKKOS_IF_ON_HOST(return _systems_host;) \
478 return _systems_device; \
479 } \
480 Array<SystemTypeName> & kokkosSystems() { return _systems_host; } \
481 KOKKOS_FUNCTION const SystemTypeName & kokkosSystem(unsigned int sys) const \
482 { \
483 KOKKOS_IF_ON_HOST(return _systems_host[sys];) \
484 return _systems_device[sys]; \
485 } \
486 SystemTypeName & kokkosSystem(unsigned int sys) { return _systems_host[sys]; }
487#else
488#define MakeSystemHolderMethods(SystemTypeName)
489#endif
490
497// clang-format off
498#define MakeSystemHolder(SystemTypeName) \
499 class SystemTypeName##Holder \
500 { \
501 public: \
502 SystemTypeName##Holder(Array<SystemTypeName> & systems) \
503 : _systems_host(systems), _systems_device(systems) \
504 { \
505 } \
506 SystemTypeName##Holder(const SystemTypeName##Holder & holder) \
507 : _systems_host(holder._systems_host), _systems_device(holder._systems_host) \
508 { \
509 } \
510 MakeSystemHolderMethods(SystemTypeName) \
511 private: \
512 Array<SystemTypeName> & _systems_host; \
513 const Array<SystemTypeName> _systems_device; \
514 }
515// clang-format on
516
518} // namespace Moose::Kokkos
dof_id_type ContiguousElementID
Definition KokkosMesh.h:20
SubdomainID ContiguousSubdomainID
Definition KokkosMesh.h:18
unsigned int TagID
Definition MooseTypes.h:238
char ** vars
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition MooseMesh.h:95
void destroy()
Free all data and reset.
The Kokkos array class.
The Kokkos wrapper class for PETSc matrix.
The Kokkos interface that holds the host reference of the Kokkos mesh and copies it to device during ...
Definition KokkosMesh.h:669
The Kokkos base system class.
void clearActiveSolutionTags()
Clear the cached active solution tags.
const unsigned int _num_vars
Number of variables.
void setActiveMatrixTags(const std::set< TagID > &tags)
Set the active matrix tags.
Array< TagID > _active_matrix_tags
void sync(const TagID tag, const MemcpyType dir)
const Array< Array< dof_id_type > > & getLocalCommList() const
Get the list of local DOF indices to communicate.
void buildCooIndices(std::vector< PetscInt > &coo_i, std::vector< PetscInt > &coo_j) const
Build the COO index arrays describing the sparsity pattern.
KOKKOS_FUNCTION bool isResidualTagActive(TagID tag) const
Check whether a residual tag is active.
Array< unsigned int > _max_dofs_per_elem
Maximum number of DOFs per element for each variable.
Sparsity _sparsity
Matrix sparsity pattern data.
void sync(const MemcpyType dir)
Synchronize the active tagged vectors and matrices between host and device.
SystemBase & _system
Reference of the MOOSE system.
void clearActiveResidualTags()
Clear the cached active residual tags.
void sync(const std::vector< TagID > &tags, const MemcpyType dir)
KOKKOS_FUNCTION unsigned int getNumScalarDofs(unsigned int var) const
Get the number of DOFs of a scalar variable.
void updateVariableScalingFactors()
Update variable scaling factors on device.
Array< Array< dof_id_type > > _local_comm_list
List of DOFs to send and receive.
const SystemBase & getSystem() const
const Array< Array< dof_id_type > > & getGhostCommList() const
Get the list of ghost DOF indices to communicate.
void setActiveResidualTags(const std::set< TagID > &tags)
Set the active residual tags.
void clearActiveMatrixTags()
Clear the cached active matrix tags.
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.
Array< unsigned int > _active_variables
List of active variable numbers.
const Parallel::Communicator & getComm() const
Get the libMesh communicator.
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.
KOKKOS_FUNCTION bool isVariableActive(unsigned int var, ContiguousSubdomainID subdomain) const
Check whether a variable is active on a subdomain.
const libMesh::DofMap & getDofMap() const
Get the libMesh DOF map.
KOKKOS_FUNCTION Real & getVectorDofValue(const dof_id_type dof, const TagID tag) const
Get the DOF value of a tagged vector.
Array< TagID > _active_solution_tags
List of active tags.
KOKKOS_FUNCTION Real & getMatrixValue(dof_id_type row, dof_id_type col, TagID tag) const
Get an entry from a tagged matrix.
KOKKOS_FUNCTION Vector & getVector(TagID tag) const
Get a tagged Kokkos vector.
SystemBase & getSystem()
Get the MOOSE system.
Array2D< bool > _var_subdomain_active
Whether each variable is active on subdomains.
System(SystemBase &system)
Constructor.
Array< dof_id_type > _local_to_global_dof_index
Map from local DOF index to global DOF index.
void setupVariables()
Setup variable data.
System(const System &src)=default
Defaulted copy constructor Used by FESystem in mixed FE+FV simulations.
KOKKOS_FUNCTION Matrix & getMatrix(TagID tag) const
Get a tagged Kokkos matrix.
KOKKOS_FUNCTION dof_id_type getScalarGlobalDofIndex(unsigned int i, unsigned int var) const
Get the global DOF index of a scalar variable.
Array< Array2D< dof_id_type > > _local_elem_dof_index
Local element DOF indices of each variable.
KOKKOS_FUNCTION dof_id_type getScalarLocalDofIndex(unsigned int i, unsigned int var) const
Get the local DOF index of a scalar variable.
void setActiveVariables(const std::set< MooseVariableFieldBase * > &vars)
Set the active variables.
const dof_id_type _num_ghost_dofs
Number of ghost DOFs.
const MooseMesh & _mesh
Reference of the MOOSE mesh.
Array< Array< dof_id_type > > _scalar_dof_index
DOF indices of each scalar variable.
KOKKOS_FUNCTION bool isScalarVariable(unsigned int var) const
Check whether a variable is scalar.
KOKKOS_FUNCTION bool isMatrixTagActive(TagID tag) const
Check whether a matrix tag is active.
const dof_id_type _num_local_dofs
Number of local DOFs.
Array< TagID > _active_residual_tags
Array< Vector > _vectors
Kokkos vectors and matrices on device.
Array< Real > _var_scaling_factor
Equation scaling factor of each variable.
void setupDofs()
Setup DOF data.
Array< bool > _matrix_tag_active
void setActiveSolutionTags(const std::set< TagID > &tags)
Set the active solution tags.
Array< Array< dof_id_type > > _ghost_comm_list
const libMesh::DofMap & _dof_map
Reference of the libMesh DOF map.
void sync(const std::set< TagID > &tags, const MemcpyType dir)
Synchronize the specified tagged vectors between host and device.
KOKKOS_FUNCTION Real getVariableScalingFactor(unsigned int var) const
Get the equation scaling factor of a variable.
Array< bool > _var_is_scalar
Whether each variable is scalar.
KOKKOS_FUNCTION dof_id_type localToGlobalDofIndex(dof_id_type dof) const
Get the global DOF index of a local DOF index.
Array< bool > _residual_tag_active
Flag whether each tag is active.
KOKKOS_FUNCTION dof_id_type getNumLocalDofs() const
Get the number of local DOFs.
Array< Matrix > _matrices
const Sparsity & getSparsity() const
Get the sparisty pattern data.
void clearActiveVariables()
Clear the cached active variables.
KOKKOS_FUNCTION dof_id_type getNumGhostDofs() const
Get the number of ghost DOFs.
const Parallel::Communicator & _comm
Reference of the libMesh communicator.
void setupSparsity()
Setup sparsity data.
The Kokkos wrapper class for PETSc vector.
Interface for objects interacting with the PerfGraph.
Base class for a system (of equations)
Definition SystemBase.h:87
MakeSystemHolder(FESystem)
MemcpyType
The enumerator that dictates the memory copy direction.
Definition KokkosArray.h:42
CSR format sparsity data.