https://mooseframework.inl.gov
KokkosMesh.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 
14 #ifdef MOOSE_KOKKOS_SCOPE
15 #include "KokkosUtils.h"
16 #endif
17 
22 
23 class MooseMesh;
24 
25 namespace Moose::Kokkos
26 {
27 
34 {
38  unsigned int type = libMesh::invalid_uint;
47 };
48 
52 class Mesh
53 {
54 public:
59  Mesh(MooseMesh & mesh) : _mesh(mesh) {}
64  const MooseMesh & getMesh() { return _mesh; }
68  [[nodiscard]] bool initialized() const { return _initialized; }
72  void update();
73 
78  auto getNumSubdomains() const { return _maps->subdomain_id_mapping.size(); }
83  auto getNumLocalElementTypes() const { return _maps->elem_type_id_mapping.size(); }
88  const auto & getElementTypeMap() const { return _maps->elem_type_id_mapping; }
93  auto getNumLocalElements() const { return _num_local_elems; }
99  auto getNumSubdomainLocalElements(const SubdomainID subdomain) const
100  {
101  auto range = libmesh_map_find(_maps->subdomain_elem_id_ranges, subdomain);
102  return range.second - range.first;
103  }
108  auto getNumLocalNodes() const { return _num_local_nodes; }
113  const auto & getLocalNodes() const { return _maps->local_nodes; }
131  unsigned int getElementTypeID(const Elem * elem) const;
137  ContiguousElementID getContiguousElementID(const Elem * elem) const;
144  {
145  const auto & range = libmesh_map_find(_maps->subdomain_elem_id_ranges, subdomain);
146  return libMesh::make_range(range.first, range.second);
147  }
153  ContiguousNodeID getContiguousNodeID(const Node * node) const;
160  const auto & getSubdomainContiguousNodeIDs(const SubdomainID subdomain) const
161  {
162  return libmesh_map_find(_maps->subdomain_node_ids, subdomain);
163  }
170  const auto & getBoundaryContiguousNodeIDs(const BoundaryID boundary) const
171  {
172  return libmesh_map_find(_maps->boundary_node_ids, boundary);
173  }
174 #ifdef MOOSE_KOKKOS_SCOPE
175 
180  KOKKOS_FUNCTION const auto & getElementInfo(ContiguousElementID elem) const
181  {
182  return _elem_info[elem];
183  }
190  KOKKOS_FUNCTION ContiguousElementID getNeighbor(ContiguousElementID elem, unsigned int side) const
191  {
192  return _elem_neighbor(side, elem);
193  }
199  KOKKOS_FUNCTION unsigned int getNumSides(unsigned int elem_type) const
200  {
201  return _num_sides[elem_type];
202  }
208  KOKKOS_FUNCTION unsigned int getNumNodes(unsigned int elem_type) const
209  {
210  return _num_nodes[elem_type];
211  }
218  KOKKOS_FUNCTION unsigned int getNumNodes(unsigned int elem_type, unsigned int side) const
219  {
220  return _num_side_nodes[elem_type][side];
221  }
228  KOKKOS_FUNCTION dof_id_type getExtraElementID(ContiguousElementID elem, unsigned int index) const
229  {
230  KOKKOS_ASSERT(index < _extra_elem_ids.n(1));
231 
232  return _extra_elem_ids(elem, index);
233  }
240  {
241  return _starting_elem_id[subdomain];
242  }
250  unsigned int node) const
251  {
252  return _nodes(node, elem);
253  }
262  unsigned int side,
263  unsigned int node) const
264  {
265  return _nodes(_local_side_node[info.type](node, side), info.id);
266  }
272  KOKKOS_FUNCTION Real3 getNodePoint(ContiguousNodeID node) const { return _points[node]; }
279  KOKKOS_FUNCTION bool isBoundaryNode(ContiguousNodeID node, ContiguousBoundaryID boundary) const;
280 #endif
281 
282 private:
286  void initMap();
290  void initElement();
291 
299  bool _initialized = false;
300 
304  struct MeshMap
305  {
309  std::unordered_map<SubdomainID, ContiguousSubdomainID> subdomain_id_mapping;
313  std::unordered_map<BoundaryID, ContiguousBoundaryID> boundary_id_mapping;
317  std::unordered_map<ElemType, unsigned int> elem_type_id_mapping;
321  std::vector<Node *> local_nodes;
325  std::unordered_map<const Node *, ContiguousNodeID> semi_local_node_id_mapping;
329  std::unordered_map<SubdomainID, std::pair<ContiguousElementID, ContiguousElementID>>
335  std::unordered_map<SubdomainID, std::vector<ContiguousNodeID>> subdomain_node_ids;
340  std::unordered_map<BoundaryID, std::set<ContiguousNodeID>> boundary_node_ids;
341  };
345  std::shared_ptr<MeshMap> _maps;
346 
407 };
408 
409 #ifdef MOOSE_KOKKOS_SCOPE
410 KOKKOS_FUNCTION inline bool
412 {
413  if (!_boundary_nodes[boundary].size())
414  return false;
415 
416  auto begin = &_boundary_nodes[boundary].begin();
417  auto end = &_boundary_nodes[boundary].end();
418  auto target = Utils::find(node, begin, end);
419 
420  return target != end;
421 }
422 #endif
423 
431 {
432 public:
441  MeshHolder(const MeshHolder & holder)
442  : _mesh_host(holder._mesh_host), _mesh_device(holder._mesh_host)
443  {
444  }
445 
446 #ifdef MOOSE_KOKKOS_SCOPE
447 
452  KOKKOS_FUNCTION const Mesh & kokkosMesh() const
453  {
454  KOKKOS_IF_ON_HOST(
456  "kokkosMesh() was called too early. Kokkos mesh is available after problem "
457  "initialization. Override initialSetup() if you need to setup your object data "
458  "using the Kokkos mesh.");
459 
460  return _mesh_host;)
461 
462  return _mesh_device;
463  }
464 #endif
465 
466 private:
470  const Mesh & _mesh_host;
475 };
476 
477 } // namespace Moose::Kokkos
Array2D< dof_id_type > _extra_elem_ids
Extra element IDs.
Definition: KokkosMesh.h:378
auto getNumSubdomainLocalElements(const SubdomainID subdomain) const
Get the number of local elements in a MOOSE subdomain.
Definition: KokkosMesh.h:99
const auto & getElementTypeMap() const
Get the element type ID map.
Definition: KokkosMesh.h:88
The Kokkos array class.
Definition: KokkosArray.h:64
The Kokkos mesh object.
Definition: KokkosMesh.h:52
Array< Array2D< unsigned int > > _local_side_node
Map from local side node index to local element node index.
Definition: KokkosMesh.h:394
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
Definition: KokkosUtils.h:40
The Kokkos object that contains the information of an element The IDs used in Kokkos are different fr...
Definition: KokkosMesh.h:33
The wrapper of host maps.
Definition: KokkosMesh.h:304
Array< ContiguousElementID > _starting_elem_id
Starting contiguous element ID of each subdomain.
Definition: KokkosMesh.h:374
const unsigned int invalid_uint
std::unordered_map< BoundaryID, ContiguousBoundaryID > boundary_id_mapping
Map from the MOOSE boundary ID to the contiguous boundary ID.
Definition: KokkosMesh.h:313
void initElement()
Initialize device element data.
bool _initialized
Flag whether the mesh was initialized.
Definition: KokkosMesh.h:299
MPI_Info info
dof_id_type ContiguousElementID
Definition: KokkosMesh.h:20
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
KOKKOS_FUNCTION dof_id_type getStartingContiguousElementID(ContiguousSubdomainID subdomain) const
Get the starting contiguous element ID of a subdomain.
Definition: KokkosMesh.h:239
void initMap()
Initialize host maps.
MeshBase & mesh
BoundaryID ContiguousBoundaryID
Definition: KokkosMesh.h:19
unsigned int _elem_id_integer
Element integer for Kokkos contiguous element ID.
Definition: KokkosMesh.h:350
const auto & getSubdomainContiguousNodeIDs(const SubdomainID subdomain) const
Get the list of local contiguous node IDs for a subdomain NOTE: This list excludes semi-local nodes...
Definition: KokkosMesh.h:160
const auto & getBoundaryContiguousNodeIDs(const BoundaryID boundary) const
Get the list of local contiguous node IDs for a boundary NOTE: This list excludes semi-local nodes...
Definition: KokkosMesh.h:170
std::vector< Node * > local_nodes
List of local nodes including semi-local nodes.
Definition: KokkosMesh.h:321
The Kokkos interface that holds the host reference of the Kokkos mesh and copies it to device during ...
Definition: KokkosMesh.h:430
dof_id_type _num_local_elems
Number of local elements.
Definition: KokkosMesh.h:358
KOKKOS_FUNCTION bool isBoundaryNode(ContiguousNodeID node, ContiguousBoundaryID boundary) const
Get whether a node is on a boundary.
Definition: KokkosMesh.h:411
auto max(const L &left, const R &right)
auto getNumSubdomains() const
Get the number of subdomains.
Definition: KokkosMesh.h:78
ContiguousNodeID getContiguousNodeID(const Node *node) const
Get the contiguous node ID of a node.
Array< Array< ContiguousNodeID > > _boundary_nodes
Contiguous node IDs on each boundary.
Definition: KokkosMesh.h:406
KOKKOS_FUNCTION unsigned int getNumNodes(unsigned int elem_type) const
Get the number of nodes of an element type.
Definition: KokkosMesh.h:208
Mesh(MooseMesh &mesh)
Constructor.
Definition: KokkosMesh.h:59
KOKKOS_FUNCTION ContiguousNodeID getContiguousNodeID(ElementInfo info, unsigned int side, unsigned int node) const
Get the contiguous node ID for a side.
Definition: KokkosMesh.h:261
const MooseMesh & getMesh()
Get the underyling MOOSE mesh.
Definition: KokkosMesh.h:64
bool initialized() const
Get whether the mesh was initialized.
Definition: KokkosMesh.h:68
static constexpr dof_id_type invalid_id
dof_id_type ContiguousNodeID
Definition: KokkosMesh.h:21
const Mesh _mesh_device
Device copy of the Kokkos mesh.
Definition: KokkosMesh.h:474
auto getNumLocalElements() const
Get the number of local elements.
Definition: KokkosMesh.h:93
std::unordered_map< ElemType, unsigned int > elem_type_id_mapping
Map from the MOOSE element type to the element type ID.
Definition: KokkosMesh.h:317
boundary_id_type BoundaryID
std::unordered_map< SubdomainID, ContiguousSubdomainID > subdomain_id_mapping
Map from the MOOSE subdomain ID to the contiguous subdomain ID.
Definition: KokkosMesh.h:309
dof_id_type _num_local_nodes
Number of local nodes including semi-local nodes.
Definition: KokkosMesh.h:362
KOKKOS_FUNCTION ContiguousElementID getNeighbor(ContiguousElementID elem, unsigned int side) const
Get the neighbor contiguous element ID.
Definition: KokkosMesh.h:190
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:93
unsigned int type
Element type ID.
Definition: KokkosMesh.h:38
MeshHolder(const Mesh &mesh)
Constructor.
Definition: KokkosMesh.h:437
std::unordered_map< SubdomainID, std::pair< ContiguousElementID, ContiguousElementID > > subdomain_elem_id_ranges
Range of the contiguous element IDs in each subdomain.
Definition: KokkosMesh.h:330
KOKKOS_FUNCTION unsigned int getNumNodes(unsigned int elem_type, unsigned int side) const
Get the number of nodes on a side of an element type.
Definition: KokkosMesh.h:218
KOKKOS_FUNCTION const auto & getElementInfo(ContiguousElementID elem) const
Get the element information object.
Definition: KokkosMesh.h:180
subdomain_id_type SubdomainID
Array2D< ContiguousNodeID > _nodes
Contiguous node IDs of each element.
Definition: KokkosMesh.h:402
std::unordered_map< BoundaryID, std::set< ContiguousNodeID > > boundary_node_ids
List of the contiguous node IDs on each boundary NOTE: This list excludes semi-local nodes...
Definition: KokkosMesh.h:340
KOKKOS_FUNCTION dof_id_type getExtraElementID(ContiguousElementID elem, unsigned int index) const
Get the extra element ID of an element.
Definition: KokkosMesh.h:228
Array2D< ContiguousElementID > _elem_neighbor
Neighbor contiguous element IDs of each element.
Definition: KokkosMesh.h:370
auto getNumLocalElementTypes() const
Get the number of local elements types.
Definition: KokkosMesh.h:83
KOKKOS_FUNCTION Real3 getNodePoint(ContiguousNodeID node) const
Get the coordinate of a node.
Definition: KokkosMesh.h:272
KOKKOS_FUNCTION const Mesh & kokkosMesh() const
Get the const reference of the Kokkos mesh.
Definition: KokkosMesh.h:452
MeshHolder(const MeshHolder &holder)
Copy constructor.
Definition: KokkosMesh.h:441
ContiguousSubdomainID subdomain
Contiguous subdomain ID.
Definition: KokkosMesh.h:46
auto getNumLocalNodes() const
Get the number of local nodes including semi-local nodes.
Definition: KokkosMesh.h:108
Array< Real3 > _points
Node coordinates.
Definition: KokkosMesh.h:398
Array< unsigned int > _num_sides
Number of sides of each element type.
Definition: KokkosMesh.h:382
ContiguousBoundaryID getContiguousBoundaryID(const BoundaryID boundary) const
Get the contiguous boundary ID of a boundary.
KOKKOS_FUNCTION unsigned int getNumSides(unsigned int elem_type) const
Get the number of sides of an element type.
Definition: KokkosMesh.h:199
IntRange< T > make_range(T beg, T end)
MooseMesh & _mesh
Reference of the MOOSE mesh.
Definition: KokkosMesh.h:295
const auto & getLocalNodes() const
Get the list of local nodes including semi-local nodes.
Definition: KokkosMesh.h:113
unsigned int _node_id_integer
Node integer for Kokkos contiguous node ID.
Definition: KokkosMesh.h:354
const Mesh & _mesh_host
Host reference of the Kokkos mesh.
Definition: KokkosMesh.h:470
ContiguousElementID getContiguousElementID(const Elem *elem) const
Get the contiguous element ID of an element.
void update()
Update the mesh.
ContiguousSubdomainID getContiguousSubdomainID(const SubdomainID subdomain) const
Get the contiguous subdomain ID of a MOOSE subdomain.
KOKKOS_FUNCTION ContiguousNodeID getContiguousNodeID(ContiguousElementID elem, unsigned int node) const
Get the contiguous node ID for an element.
Definition: KokkosMesh.h:249
std::shared_ptr< MeshMap > _maps
A shared pointer holding all the host maps to avoid deep copy.
Definition: KokkosMesh.h:345
Array< ElementInfo > _elem_info
Element information.
Definition: KokkosMesh.h:366
unsigned int getElementTypeID(const Elem *elem) const
Get the element type ID of an element.
Array< Array< unsigned int > > _num_side_nodes
Number of nodes per side of each element side.
Definition: KokkosMesh.h:390
auto getSubdomainContiguousElementIDRange(const SubdomainID subdomain) const
Get the range of contiguous element IDs for a subdomain.
Definition: KokkosMesh.h:143
Array< unsigned int > _num_nodes
Number of nodes of each element type.
Definition: KokkosMesh.h:386
uint8_t dof_id_type
std::unordered_map< SubdomainID, std::vector< ContiguousNodeID > > subdomain_node_ids
List of the contiguous node IDs in each subdomain NOTE: This list excludes semi-local nodes...
Definition: KokkosMesh.h:335
std::unordered_map< const Node *, ContiguousNodeID > semi_local_node_id_mapping
Map from the libMesh semi-local node to the contiguous node ID.
Definition: KokkosMesh.h:325