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
26 {
27 namespace Kokkos
28 {
29 
36 {
40  unsigned int type;
49 };
50 
54 class Mesh
55 {
56 public:
61  Mesh(const MooseMesh & mesh) : _mesh(mesh) {}
66  const MooseMesh & getMesh() { return _mesh; }
70  void update();
71 
89  unsigned int getElementTypeID(const Elem * elem) const;
95  ContiguousElementID getContiguousElementID(const Elem * elem) const;
100  const auto & getElementTypeMap() const { return _maps->_elem_type_id_mapping; }
105  const auto & getContiguousElementMap() const { return _maps->_local_elem_id_mapping; }
111  const auto & getSubdomainContiguousElementIDs(const SubdomainID subdomain) const
112  {
113  return _maps->_subdomain_elem_ids.at(subdomain);
114  }
120  ContiguousNodeID getContiguousNodeID(const Node * node) const;
126  const auto & getContiguousNodeMap() const { return _maps->_local_node_id_mapping; }
133  const auto & getSubdomainContiguousNodeIDs(const SubdomainID subdomain) const
134  {
135  return _maps->_subdomain_node_ids.at(subdomain);
136  }
137 
138 #ifdef MOOSE_KOKKOS_SCOPE
139 
144  KOKKOS_FUNCTION const auto & getElementInfo(ContiguousElementID elem) const
145  {
146  return _elem_info[elem];
147  }
154  KOKKOS_FUNCTION ContiguousElementID getNeighbor(ContiguousElementID elem, unsigned int side) const
155  {
156  return _elem_neighbor(side, elem);
157  }
163  KOKKOS_FUNCTION unsigned int getNumSides(unsigned int elem_type) const
164  {
165  return _num_sides[elem_type];
166  }
172  KOKKOS_FUNCTION unsigned int getNumNodes(unsigned int elem_type) const
173  {
174  return _num_nodes[elem_type];
175  }
182  KOKKOS_FUNCTION unsigned int getNumNodes(unsigned int elem_type, unsigned int side) const
183  {
184  return _num_side_nodes[elem_type][side];
185  }
193  unsigned int node) const
194  {
195  return _nodes(node, elem);
196  }
205  unsigned int side,
206  unsigned int node) const
207  {
208  return _nodes_face(node, side, elem);
209  }
215  KOKKOS_FUNCTION Real3 getNodePoint(ContiguousNodeID node) const { return _points[node]; }
222  KOKKOS_FUNCTION bool isBoundaryNode(ContiguousNodeID node, ContiguousBoundaryID boundary) const;
223 #endif
224 
225 private:
229  void initMap();
233  void initElement();
234 
238  const MooseMesh & _mesh;
242  struct MeshMap
243  {
247  std::unordered_map<SubdomainID, ContiguousSubdomainID> _subdomain_id_mapping;
251  std::unordered_map<BoundaryID, ContiguousBoundaryID> _boundary_id_mapping;
255  std::unordered_map<ElemType, unsigned int> _elem_type_id_mapping;
259  std::unordered_map<const Elem *, ContiguousElementID> _local_elem_id_mapping;
264  std::unordered_map<const Node *, ContiguousNodeID> _local_node_id_mapping;
268  std::unordered_map<SubdomainID, std::unordered_set<ContiguousElementID>> _subdomain_elem_ids;
273  std::unordered_map<SubdomainID, std::unordered_set<ContiguousNodeID>> _subdomain_node_ids;
274  };
278  std::shared_ptr<MeshMap> _maps;
279 
311 
315 };
316 
317 #ifdef MOOSE_KOKKOS_SCOPE
318 KOKKOS_FUNCTION inline bool
320 {
321  if (!_boundary_nodes[boundary].size())
322  return false;
323 
324  auto begin = &_boundary_nodes[boundary].begin();
325  auto end = &_boundary_nodes[boundary].end();
326  auto target = Utils::find(node, begin, end);
327 
328  return target != end;
329 }
330 #endif
331 
339 {
340 public:
349  MeshHolder(const MeshHolder & holder)
350  : _mesh_host(holder._mesh_host), _mesh_device(holder._mesh_host)
351  {
352  }
353 
354 #ifdef MOOSE_KOKKOS_SCOPE
355 
360  KOKKOS_FUNCTION const Mesh & kokkosMesh() const
361  {
362  KOKKOS_IF_ON_HOST(return _mesh_host;)
363 
364  return _mesh_device;
365  }
366 #endif
367 
368 private:
372  const Mesh & _mesh_host;
377 };
378 
379 } // namespace Kokkos
380 } // namespace Moose
const auto & getElementTypeMap() const
Get the element type ID map.
Definition: KokkosMesh.h:100
The Kokkos array class.
Definition: KokkosArray.h:56
The Kokkos mesh object.
Definition: KokkosMesh.h:54
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:30
The Kokkos object that contains the information of an element The IDs used in Kokkos are different fr...
Definition: KokkosMesh.h:35
The wrapper of host maps.
Definition: KokkosMesh.h:242
void initElement()
Initialize device element data.
dof_id_type ContiguousElementID
Definition: KokkosMesh.h:20
const auto & getContiguousNodeMap() const
Get the contiguous node ID map This list contains the nodes of local elements, so some nodes may belo...
Definition: KokkosMesh.h:126
std::unordered_map< BoundaryID, ContiguousBoundaryID > _boundary_id_mapping
Map from the MOOSE boundary ID to the contiguous boundary ID.
Definition: KokkosMesh.h:251
void initMap()
Initialize host maps.
MeshBase & mesh
BoundaryID ContiguousBoundaryID
Definition: KokkosMesh.h:19
const auto & getSubdomainContiguousNodeIDs(const SubdomainID subdomain) const
Get the list of contiguous node IDs for a subdomain This list strictly contains the nodes local to th...
Definition: KokkosMesh.h:133
Array3D< ContiguousNodeID > _nodes_face
Definition: KokkosMesh.h:309
The Kokkos interface that holds the host reference of the Kokkos mesh and copies it to device during ...
Definition: KokkosMesh.h:338
KOKKOS_FUNCTION bool isBoundaryNode(ContiguousNodeID node, ContiguousBoundaryID boundary) const
Get whether a node is on a boundary.
Definition: KokkosMesh.h:319
KOKKOS_FUNCTION ContiguousNodeID getContiguousNodeID(ContiguousElementID elem, unsigned int side, unsigned int node) const
Get the contiguous node ID for a side.
Definition: KokkosMesh.h:204
ContiguousNodeID getContiguousNodeID(const Node *node) const
Get the contiguous node ID of a node.
std::unordered_map< SubdomainID, ContiguousSubdomainID > _subdomain_id_mapping
Map from the MOOSE subdomain ID to the contiguous subdomain ID.
Definition: KokkosMesh.h:247
Array< Array< ContiguousNodeID > > _boundary_nodes
Contiguous node IDs on each boundary.
Definition: KokkosMesh.h:314
KOKKOS_FUNCTION unsigned int getNumNodes(unsigned int elem_type) const
Get the number of nodes of an element type.
Definition: KokkosMesh.h:172
const auto & getSubdomainContiguousElementIDs(const SubdomainID subdomain) const
Get the list of contiguous element IDs for a subdomain.
Definition: KokkosMesh.h:111
const MooseMesh & getMesh()
Get the underyling MOOSE mesh.
Definition: KokkosMesh.h:66
dof_id_type ContiguousNodeID
Definition: KokkosMesh.h:21
const Mesh _mesh_device
Device copy of the Kokkos mesh.
Definition: KokkosMesh.h:376
std::unordered_map< SubdomainID, std::unordered_set< ContiguousNodeID > > _subdomain_node_ids
List of the contiguous node IDs in each subdomain This list strictly contains the nodes local to the ...
Definition: KokkosMesh.h:273
boundary_id_type BoundaryID
KOKKOS_FUNCTION ContiguousElementID getNeighbor(ContiguousElementID elem, unsigned int side) const
Get the neighbor contiguous element ID.
Definition: KokkosMesh.h:154
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:92
Mesh(const MooseMesh &mesh)
Constructor.
Definition: KokkosMesh.h:61
unsigned int type
Element type ID.
Definition: KokkosMesh.h:40
const auto & getContiguousElementMap() const
Get the contiguous element ID map.
Definition: KokkosMesh.h:105
MeshHolder(const Mesh &mesh)
Constructor.
Definition: KokkosMesh.h:345
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:182
KOKKOS_FUNCTION const auto & getElementInfo(ContiguousElementID elem) const
Get the element information object.
Definition: KokkosMesh.h:144
subdomain_id_type SubdomainID
Array2D< ContiguousNodeID > _nodes
Contiguous node IDs of each element and side.
Definition: KokkosMesh.h:308
Array2D< ContiguousElementID > _elem_neighbor
Neighbor contiguous element IDs of each element.
Definition: KokkosMesh.h:287
KOKKOS_FUNCTION Real3 getNodePoint(ContiguousNodeID node) const
Get the coordinate of a node.
Definition: KokkosMesh.h:215
const MooseMesh & _mesh
Reference of the MOOSE mesh.
Definition: KokkosMesh.h:238
KOKKOS_FUNCTION const Mesh & kokkosMesh() const
Get the const reference of the Kokkos mesh.
Definition: KokkosMesh.h:360
MeshHolder(const MeshHolder &holder)
Copy constructor.
Definition: KokkosMesh.h:349
ContiguousSubdomainID subdomain
Contiguous subdomain ID.
Definition: KokkosMesh.h:48
Array< Real3 > _points
Node coordinates.
Definition: KokkosMesh.h:303
Array< unsigned int > _num_sides
Number of sides of each element type.
Definition: KokkosMesh.h:291
ContiguousBoundaryID getContiguousBoundaryID(const BoundaryID boundary) const
Get the contiguous boundary ID of a boundary.
std::unordered_map< const Node *, ContiguousNodeID > _local_node_id_mapping
Map from the libMesh node to the contiguous node ID This list contains the nodes of local elements...
Definition: KokkosMesh.h:264
KOKKOS_FUNCTION unsigned int getNumSides(unsigned int elem_type) const
Get the number of sides of an element type.
Definition: KokkosMesh.h:163
const Mesh & _mesh_host
Host reference of the Kokkos mesh.
Definition: KokkosMesh.h:372
ContiguousElementID getContiguousElementID(const Elem *elem) const
Get the contiguous element ID of an element.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
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:192
std::unordered_map< const Elem *, ContiguousElementID > _local_elem_id_mapping
Map from the libMesh element to the contiguous element ID.
Definition: KokkosMesh.h:259
std::shared_ptr< MeshMap > _maps
A shared pointer holding all the host maps to avoid deep copy.
Definition: KokkosMesh.h:278
std::unordered_map< SubdomainID, std::unordered_set< ContiguousElementID > > _subdomain_elem_ids
List of the contiguous element IDs in each subdomain.
Definition: KokkosMesh.h:268
Array< ElementInfo > _elem_info
Element information.
Definition: KokkosMesh.h:283
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:299
std::unordered_map< ElemType, unsigned int > _elem_type_id_mapping
Map from the MOOSE element type to the element type ID.
Definition: KokkosMesh.h:255
ContiguousElementID id
Contiguous element ID.
Definition: KokkosMesh.h:44
Array< unsigned int > _num_nodes
Number of nodes of each element type.
Definition: KokkosMesh.h:295
uint8_t dof_id_type