MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
util.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, 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
17
18#include <functional>
19#include <string>
20#include <tuple>
21#include <unordered_map>
22
23#include "ceed.hpp"
24
25#ifdef MFEM_USE_CEED
26#include <ceed/backend.h> // for CeedOperatorField
27#endif
28
29namespace mfem
30{
31
32class FiniteElement;
33class FiniteElementSpace;
34class ElementTransformation;
35class IntegrationRule;
36class Vector;
37
38/** @brief Function that determines if a CEED kernel should be used, based on
39 the current mfem::Device configuration. */
40bool DeviceCanUseCeed();
41
42namespace ceed
43{
44
45/** @brief Remove from ceed_basis_map and ceed_restr_map the entries associated
46 with the given @a fes. */
48
49#ifdef MFEM_USE_CEED
50
51#define PCeedChk(err) do { \
52 if ((err)) \
53 { \
54 const char * errmsg; \
55 CeedGetErrorMessage(internal::ceed, &errmsg); \
56 MFEM_ABORT(errmsg); \
57 } \
58 } while(0);
59
60/// Initialize a CeedVector from an mfem::Vector
61void InitVector(const mfem::Vector &v, CeedVector &cv);
62
63/** @brief Initialize a CeedBasis and a CeedElemRestriction based on an
64 mfem::FiniteElementSpace @a fes, and an mfem::IntegrationRule @a ir.
65
66 @param[in] fes The finite element space.
67 @param[in] ir The integration rule.
68 @param[in] ceed The Ceed object.
69 @param[out] basis The `CeedBasis` to initialize.
70 @param[out] restr The `CeedElemRestriction` to initialize.
71
72 @warning Only for non-mixed finite element spaces. */
74 const mfem::IntegrationRule &ir,
75 Ceed ceed, CeedBasis *basis,
76 CeedElemRestriction *restr);
77
78/** @brief Initialize a CeedBasis and a CeedElemRestriction based on an
79 mfem::FiniteElementSpace @a fes, and an mfem::IntegrationRule @a ir,
80 and a list of @a nelem elements of indices @a indices.
81
82 @param[in] fes The finite element space.
83 @param[in] ir The integration rule.
84 @param[in] nelem The number of elements.
85 @param[in] indices The indices of the elements of same type in the
86 `FiniteElementSpace`. If `indices == nullptr`, assumes
87 that the `FiniteElementSpace` is not mixed.
88 @param[in] ceed The Ceed object.
89 @param[out] basis The `CeedBasis` to initialize.
90 @param[out] restr The `CeedElemRestriction` to initialize. */
91void InitBasisAndRestriction(const FiniteElementSpace &fes,
92 const IntegrationRule &ir,
93 int nelem,
94 const int* indices,
95 Ceed ceed, CeedBasis *basis,
96 CeedElemRestriction *restr);
97
98int CeedOperatorGetActiveField(CeedOperator oper, CeedOperatorField *field);
99
100
101template <typename Integrator>
103 const Integrator &integ,
104 const FiniteElement &trial_fe,
105 const FiniteElement &test_fe,
106 ElementTransformation &Trans);
107
108/// Return the path to the libCEED q-function headers.
109const std::string &GetCeedPath();
110
111/// Wrapper for std::hash.
112template <typename T>
113inline std::size_t CeedHash(const T key)
114{
115 return std::hash<T> {}(key);
116}
117
118/// Effective way to combine hashes (from libCEED).
119inline std::size_t CeedHashCombine(std::size_t seed, std::size_t hash)
120{
121 // See https://doi.org/10.1002/asi.10170, or
122 // https://dl.acm.org/citation.cfm?id=759509.
123 return seed ^ (hash + (seed << 6) + (seed >> 2));
124}
125
126// Hash table for CeedBasis
127using BasisKey = std::tuple<const mfem::FiniteElementSpace*,
129 int, int, int>;
131{
132 std::size_t operator()(const BasisKey& k) const
133 {
134 return CeedHashCombine(
136 CeedHash(std::get<0>(k)),
137 CeedHash(std::get<1>(k))),
139 CeedHashCombine(CeedHash(std::get<2>(k)),
140 CeedHash(std::get<3>(k))),
141 CeedHash(std::get<4>(k))));
142 }
143};
144using BasisMap = std::unordered_map<const BasisKey, CeedBasis, BasisHash>;
145
147
148// Hash table for CeedElemRestriction
149using RestrKey =
150 std::tuple<const mfem::FiniteElementSpace*, int, int, int, int>;
152{
153 std::size_t operator()(const RestrKey& k) const
154 {
155 return CeedHashCombine(
158 CeedHash(std::get<0>(k)),
159 CeedHash(std::get<1>(k))),
160 CeedHashCombine(CeedHash(std::get<2>(k)),
161 CeedHash(std::get<3>(k)))),
162 CeedHash(std::get<4>(k)));
163 }
164};
165using RestrMap =
166 std::unordered_map<const RestrKey, CeedElemRestriction, RestrHash>;
167
168#endif
169
170} // namespace ceed
171
172namespace internal
173{
174
175#ifdef MFEM_USE_CEED
176
177/** @warning These maps have a tendency to create bugs when adding new "types"
178 of CeedBasis and CeedElemRestriction. */
179extern ceed::BasisMap ceed_basis_map;
180extern ceed::RestrMap ceed_restr_map;
181
182#endif
183
184} // namespace internal
185
186} // namespace mfem
187
188#endif // MFEM_LIBCEED_UTIL
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition fespace.hpp:220
Abstract class for all finite elements.
Definition fe_base.hpp:239
Class for an integration rule - an Array of IntegrationPoint.
Definition intrules.hpp:100
Vector data type.
Definition vector.hpp:80
const std::string & GetCeedPath()
Return the path to the libCEED q-function headers.
Definition util.cpp:255
std::size_t CeedHashCombine(std::size_t seed, std::size_t hash)
Effective way to combine hashes (from libCEED).
Definition util.hpp:119
void InitVector(const mfem::Vector &v, CeedVector &cv)
Initialize a CeedVector from an mfem::Vector.
Definition util.cpp:75
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
std::unordered_map< const BasisKey, CeedBasis, BasisHash > BasisMap
Definition util.hpp:144
std::size_t CeedHash(const T key)
Wrapper for std::hash.
Definition util.hpp:113
std::tuple< const mfem::FiniteElementSpace *, const mfem::IntegrationRule *, int, int, int > BasisKey
Definition util.hpp:127
std::unordered_map< const RestrKey, CeedElemRestriction, RestrHash > RestrMap
Definition util.hpp:165
const IntegrationRule & GetRule(const Integrator &integ, const FiniteElement &trial_fe, const FiniteElement &test_fe, ElementTransformation &Trans)
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
int CeedOperatorGetActiveField(CeedOperator oper, CeedOperatorField *field)
Definition util.cpp:131
std::tuple< const mfem::FiniteElementSpace *, int, int, int, int > RestrKey
Definition util.hpp:149
bool DeviceCanUseCeed()
Function that determines if a CEED kernel should be used, based on the current mfem::Device configura...
Definition util.cpp:33
std::size_t operator()(const BasisKey &k) const
Definition util.hpp:132
std::size_t operator()(const RestrKey &k) const
Definition util.hpp:153