MFEM  v4.5.1
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
util.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 
12 #ifndef MFEM_LIBCEED_UTIL
13 #define MFEM_LIBCEED_UTIL
14 
15 #include "../../../config/config.hpp"
16 #include <tuple>
17 #include <unordered_map>
18 #include <string>
19 
20 #include "ceed.hpp"
21 #ifdef MFEM_USE_CEED
22 #include <ceed/hash.h>
23 #include <ceed/backend.h> // for CeedOperatorField
24 #endif
25 
26 namespace mfem
27 {
28 
29 class FiniteElement;
30 class FiniteElementSpace;
31 class ElementTransformation;
32 class IntegrationRule;
33 class Vector;
34 
35 /** @brief Function that determines if a CEED kernel should be used, based on
36  the current mfem::Device configuration. */
37 bool DeviceCanUseCeed();
38 
39 namespace ceed
40 {
41 
42 /** @brief Remove from ceed_basis_map and ceed_restr_map the entries associated
43  with the given @a fes. */
45 
46 #ifdef MFEM_USE_CEED
47 
48 #define PCeedChk(err) do { \
49  if ((err)) \
50  { \
51  const char * errmsg; \
52  CeedGetErrorMessage(internal::ceed, &errmsg); \
53  MFEM_ABORT(errmsg); \
54  } \
55  } while(0);
56 
57 /// Initialize a CeedVector from an mfem::Vector
58 void InitVector(const mfem::Vector &v, CeedVector &cv);
59 
60 /** @brief Initialize a CeedBasis and a CeedElemRestriction based on an
61  mfem::FiniteElementSpace @a fes, and an mfem::IntegrationRule @a ir.
62 
63  @param[in] fes The finite element space.
64  @param[in] ir The integration rule.
65  @param[in] ceed The Ceed object.
66  @param[out] basis The `CeedBasis` to initialize.
67  @param[out] restr The `CeedElemRestriction` to initialize.
68 
69  @warning Only for non-mixed finite element spaces. */
71  const mfem::IntegrationRule &ir,
72  Ceed ceed, CeedBasis *basis,
73  CeedElemRestriction *restr);
74 
75 /** @brief Initialize a CeedBasis and a CeedElemRestriction based on an
76  mfem::FiniteElementSpace @a fes, and an mfem::IntegrationRule @a ir,
77  and a list of @a nelem elements of indices @a indices.
78 
79  @param[in] fes The finite element space.
80  @param[in] ir The integration rule.
81  @param[in] nelem The number of elements.
82  @param[in] indices The indices of the elements of same type in the
83  `FiniteElementSpace`. If `indices == nullptr`, assumes
84  that the `FiniteElementSpace` is not mixed.
85  @param[in] ceed The Ceed object.
86  @param[out] basis The `CeedBasis` to initialize.
87  @param[out] restr The `CeedElemRestriction` to initialize. */
88 void InitBasisAndRestriction(const FiniteElementSpace &fes,
89  const IntegrationRule &ir,
90  int nelem,
91  const int* indices,
92  Ceed ceed, CeedBasis *basis,
93  CeedElemRestriction *restr);
94 
95 int CeedOperatorGetActiveField(CeedOperator oper, CeedOperatorField *field);
96 
97 
98 template <typename Integrator>
99 const IntegrationRule & GetRule(
100  const Integrator &integ,
101  const FiniteElement &trial_fe,
102  const FiniteElement &test_fe,
103  ElementTransformation &Trans);
104 
105 /// Return the path to the libCEED q-function headers.
106 const std::string &GetCeedPath();
107 
108 // Hash table for CeedBasis
109 using BasisKey = std::tuple<const mfem::FiniteElementSpace*,
110  const mfem::IntegrationRule*,
111  int, int, int>;
112 struct BasisHash
113 {
114  std::size_t operator()(const BasisKey& k) const
115  {
116  return CeedHashCombine(
117  CeedHashCombine(
118  CeedHashInt(reinterpret_cast<CeedHash64_t>(std::get<0>(k))),
119  CeedHashInt(reinterpret_cast<CeedHash64_t>(std::get<1>(k)))),
120  CeedHashCombine(
121  CeedHashCombine(CeedHashInt(std::get<2>(k)),
122  CeedHashInt(std::get<3>(k))),
123  CeedHashInt(std::get<4>(k))));
124  }
125 };
126 using BasisMap = std::unordered_map<const BasisKey, CeedBasis, BasisHash>;
127 
129 
130 // Hash table for CeedElemRestriction
131 using RestrKey =
132  std::tuple<const mfem::FiniteElementSpace*, int, int, int, int>;
133 struct RestrHash
134 {
135  std::size_t operator()(const RestrKey& k) const
136  {
137  return CeedHashCombine(
138  CeedHashCombine(
139  CeedHashCombine(
140  CeedHashInt(reinterpret_cast<CeedHash64_t>(std::get<0>(k))),
141  CeedHashInt(std::get<1>(k))),
142  CeedHashCombine(CeedHashInt(std::get<2>(k)),
143  CeedHashInt(std::get<3>(k)))),
144  CeedHashInt(std::get<4>(k)));
145  }
146 };
147 using RestrMap =
148  std::unordered_map<const RestrKey, CeedElemRestriction, RestrHash>;
149 
150 #endif
151 
152 } // namespace ceed
153 
154 namespace internal
155 {
156 
157 #ifdef MFEM_USE_CEED
158 /** @warning These maps have a tendency to create bugs when adding new "types"
159  of CeedBasis and CeedElemRestriction. */
160 extern ceed::BasisMap ceed_basis_map;
161 extern ceed::RestrMap ceed_restr_map;
162 #endif
163 
164 } // namespace internal
165 
166 } // namespace mfem
167 
168 #endif // MFEM_LIBCEED_UTIL
std::unordered_map< const RestrKey, CeedElemRestriction, RestrHash > RestrMap
Definition: util.hpp:148
Class for an integration rule - an Array of IntegrationPoint.
Definition: intrules.hpp:90
void InitVector(const mfem::Vector &v, CeedVector &cv)
Initialize a CeedVector from an mfem::Vector.
Definition: util.cpp:75
std::size_t operator()(const RestrKey &k) const
Definition: util.hpp:135
std::tuple< const mfem::FiniteElementSpace *, int, int, int, int > RestrKey
Definition: util.hpp:132
int CeedOperatorGetActiveField(CeedOperator oper, CeedOperatorField *field)
Definition: util.cpp:131
std::tuple< const mfem::FiniteElementSpace *, const mfem::IntegrationRule *, int, int, int > BasisKey
Definition: util.hpp:111
std::unordered_map< const BasisKey, CeedBasis, BasisHash > BasisMap
Definition: util.hpp:126
std::size_t operator()(const BasisKey &k) const
Definition: util.hpp:114
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:96
const std::string & GetCeedPath()
Return the path to the libCEED q-function headers.
Definition: util.cpp:255
void RemoveBasisAndRestriction(const mfem::FiniteElementSpace *fes)
Remove from ceed_basis_map and ceed_restr_map the entries associated with the given fes...
Definition: util.cpp:41
bool DeviceCanUseCeed()
Function that determines if a CEED kernel should be used, based on the current mfem::Device configura...
Definition: util.cpp:33
const IntegrationRule & GetRule(const Integrator &integ, const FiniteElement &trial_fe, const FiniteElement &test_fe, ElementTransformation &Trans)
Vector data type.
Definition: vector.hpp:60
void InitBasisAndRestriction(const FiniteElementSpace &fes, const IntegrationRule &irm, Ceed ceed, CeedBasis *basis, CeedElemRestriction *restr)
Initialize a CeedBasis and a CeedElemRestriction based on an mfem::FiniteElementSpace fes...
Definition: util.cpp:93