xref: /libCEED/include/ceed/types.h (revision 3e551a327d6c97f9de071b988b42ffdb7bed19a7)
1 /// Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2 /// All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 ///
4 /// SPDX-License-Identifier: BSD-2-Clause
5 ///
6 /// This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Public header for types and macros used in user QFunction source code
10 #pragma once
11 
12 #ifndef CEED_RUNNING_JIT_PASS
13 #include <stddef.h>
14 #include <stdint.h>
15 #endif
16 
17 /**
18   @ingroup CeedQFunction
19   This macro defines compiler attributes to the CEED_QFUNCTION to force inlining for called functions.
20     The `inline` declaration does not necessarily enforce a compiler to inline a function.
21     This can be detrimental to performance, so here we force inlining to occur unless inlining has been forced off (like during debugging).
22 **/
23 #ifndef CEED_QFUNCTION_ATTR
24 #ifndef __NO_INLINE__
25 #if defined(__GNUC__) || defined(__clang__)
26 #define CEED_QFUNCTION_ATTR __attribute__((flatten))
27 #elif defined(__INTEL_COMPILER)
28 #define CEED_QFUNCTION_ATTR _Pragma("forceinline")
29 #else
30 #define CEED_QFUNCTION_ATTR
31 #endif
32 #else
33 #define CEED_QFUNCTION_ATTR
34 #endif
35 #if defined(__GNUC__) || defined(__clang__)
36 #define CEED_QFUNCTION_HELPER_ATTR CEED_QFUNCTION_ATTR __attribute__((always_inline))
37 #else
38 #define CEED_QFUNCTION_HELPER_ATTR CEED_QFUNCTION_ATTR
39 #endif
40 #endif
41 
42 /**
43   @ingroup CeedQFunction
44   This macro populates the correct function annotations for User QFunction source for code generation backends or populates default values for CPU
45 backends. It also creates a variable `name_loc` populated with the correct source path for creating the respective User QFunction.
46 **/
47 #ifndef CEED_QFUNCTION
48 #define CEED_QFUNCTION(name)                                        \
49   static const char              name##_loc[] = __FILE__ ":" #name; \
50   CEED_QFUNCTION_ATTR static int name
51 #endif
52 
53 /**
54   @ingroup CeedQFunction
55   This macro populates the correct function annotations for User QFunction helper function source for code generation backends or populates default
56 values for CPU backends.
57 **/
58 #ifndef CEED_QFUNCTION_HELPER
59 #define CEED_QFUNCTION_HELPER CEED_QFUNCTION_HELPER_ATTR static inline
60 #endif
61 
62 /**
63   @ingroup CeedQFunction
64   Using VLA syntax to reshape User QFunction inputs and outputs can make user code more readable.
65     VLA is a C99 feature that is not supported by the C++ dialect used by CUDA.
66     This macro allows users to use the VLA syntax with the CUDA backends.
67 **/
68 #ifndef CEED_Q_VLA
69 #define CEED_Q_VLA Q
70 #endif
71 
72 /**
73   @ingroup Ceed
74   This macro provides the appropriate SIMD Pragma for the compilation environment.
75     Code generation backends may redefine this macro, as needed.
76 **/
77 #ifndef CeedPragmaSIMD
78 #if defined(__INTEL_COMPILER)
79 #define CeedPragmaSIMD _Pragma("vector")
80 /// Cannot use Intel pragma ivdep because it miscompiles unpacking symmetric tensors, as in Poisson2DApply, where the SIMD loop body contains
81 /// temporaries such as the following.
82 ///
83 ///     const CeedScalar dXdxdXdxT[2][2] = {{qd[i+0*Q], qd[i+2*Q]},
84 ///                                         {qd[i+2*Q], qd[i+1*Q]}};
85 ///     for (int j=0; j<2; j++)
86 ///        vg[i+j*Q] = (du[0] * dXdxdXdxT[0][j] + du[1] * dXdxdXdxT[1][j]);
87 ///
88 /// Miscompilation with pragma ivdep observed with icc (ICC) 19.0.5.281 20190815 at -O2 and above.
89 #elif defined(__GNUC__) && __GNUC__ >= 5
90 #define CeedPragmaSIMD _Pragma("GCC ivdep")
91 #elif defined(__clang__)
92 #define CeedPragmaSIMD _Pragma("clang loop vectorize(enable)")
93 #elif defined(_OPENMP) && _OPENMP >= 201307  // OpenMP-4.0 (July, 2013)
94 #define CeedPragmaSIMD _Pragma("omp simd")
95 #else
96 #define CeedPragmaSIMD
97 #endif
98 #endif
99 
100 /// Integer type, used for indexing
101 /// @ingroup Ceed
102 typedef int32_t CeedInt;
103 #define CeedInt_FMT "d"
104 
105 /// Integer type, used array sizes
106 /// @ingroup Ceed
107 typedef ptrdiff_t CeedSize;
108 #define CeedSize_FMT "td"
109 
110 /// Integer type, for small integers
111 /// @ingroup Ceed
112 typedef signed char CeedInt8;
113 #define CeedInt8_FMT "d"
114 
115 /// Scalar (floating point) types
116 ///
117 /// @ingroup Ceed
118 typedef enum {
119   /// Single precision
120   CEED_SCALAR_FP32,
121   /// Double precision
122   CEED_SCALAR_FP64
123 } CeedScalarType;
124 /// Base scalar type for the library to use: change which header is included to change the precision.
125 #include "ceed-f64.h"  // IWYU pragma: export
126 
127 /// Ceed error code.
128 ///
129 /// This enum is used to specify the type of error returned by a function.
130 /// A zero error code is success, negative error codes indicate terminal errors and positive error codes indicate nonterminal errors.
131 /// With nonterminal errors the object state has not been modified, but with terminal errors the object data is likely modified or corrupted.
132 /// @ingroup Ceed
133 typedef enum {
134   /// Success error code
135   CEED_ERROR_SUCCESS = 0,
136   /// Minor error, generic
137   CEED_ERROR_MINOR = 1,
138   /// Minor error, dimension mismatch in inputs
139   CEED_ERROR_DIMENSION = 2,
140   /// Minor error, incomplete object setup
141   CEED_ERROR_INCOMPLETE = 3,
142   /// Minor error, incompatible arguments/configuration
143   CEED_ERROR_INCOMPATIBLE = 4,
144   /// Minor error, access lock problem
145   CEED_ERROR_ACCESS = 5,
146   /// Major error, generic
147   CEED_ERROR_MAJOR = -1,
148   /// Major error, internal backend error
149   CEED_ERROR_BACKEND = -2,
150   /// Major error, operation unsupported by current backend
151   CEED_ERROR_UNSUPPORTED = -3,
152 } CeedErrorType;
153 
154 /// Specify memory type.
155 /// Many Ceed interfaces take or return pointers to memory.
156 /// This enum is used to specify where the memory being provided or requested must reside.
157 /// @ingroup Ceed
158 typedef enum {
159   /// Memory resides on the host
160   CEED_MEM_HOST,
161   /// Memory resides on a device (corresponding to \ref Ceed resource)
162   CEED_MEM_DEVICE,
163 } CeedMemType;
164 
165 /// Conveys ownership status of arrays passed to Ceed interfaces.
166 /// @ingroup Ceed
167 typedef enum {
168   /// Implementation will copy the values and not store the passed pointer.
169   CEED_COPY_VALUES,
170   /// Implementation can use and modify the data provided by the user, but does not take ownership.
171   CEED_USE_POINTER,
172   /// Implementation takes ownership of the pointer and will free using CeedFree() when done using it.
173   /// The user should not assume that the pointer remains valid after ownership has been transferred.
174   /// Note that arrays allocated using C++ operator new or other allocators cannot generally be freed using CeedFree().
175   /// CeedFree() is capable of freeing any memory that can be freed using free().
176   CEED_OWN_POINTER,
177 } CeedCopyMode;
178 
179 /// Denotes type of vector norm to be computed
180 /// @ingroup CeedVector
181 typedef enum {
182   /// \f$\Vert \bm{x}\Vert_1 = \sum_i \vert x_i\vert\f$
183   CEED_NORM_1,
184   /// \f$\Vert \bm{x} \Vert_2 = \sqrt{\sum_i x_i^2}\f$
185   CEED_NORM_2,
186   /// \f$\Vert \bm{x} \Vert_\infty = \max_i \vert x_i \vert\f$
187   CEED_NORM_MAX,
188 } CeedNormType;
189 
190 /// Denotes whether a linear transformation or its transpose should be applied
191 /// @ingroup CeedBasis
192 typedef enum {
193   /// Apply the linear transformation
194   CEED_NOTRANSPOSE,
195   /// Apply the transpose
196   CEED_TRANSPOSE
197 } CeedTransposeMode;
198 
199 /// Basis evaluation mode
200 /// @ingroup CeedBasis
201 typedef enum {
202   /// Perform no evaluation (either because there is no data or it is already at quadrature points)
203   CEED_EVAL_NONE = 0,
204   /// Interpolate from nodes to quadrature points
205   CEED_EVAL_INTERP = 1,
206   /// Evaluate gradients at quadrature points from input in the basis
207   CEED_EVAL_GRAD = 2,
208   /// Evaluate divergence at quadrature points from input in the basis
209   CEED_EVAL_DIV = 4,
210   /// Evaluate curl at quadrature points from input in the basis
211   CEED_EVAL_CURL = 8,
212   /// Using no input, evaluate quadrature weights on the reference element
213   CEED_EVAL_WEIGHT = 16,
214 } CeedEvalMode;
215 
216 /// Type of quadrature; also used for location of nodes
217 /// @ingroup CeedBasis
218 typedef enum {
219   /// Gauss-Legendre quadrature
220   CEED_GAUSS = 0,
221   /// Gauss-Legendre-Lobatto quadrature
222   CEED_GAUSS_LOBATTO = 1,
223 } CeedQuadMode;
224 
225 /// Type of basis shape to create non-tensor element basis.
226 /// Dimension can be extracted with bitwise AND (CeedElemTopology & 2**(dim + 2)) == TRUE
227 /// @ingroup CeedBasis
228 typedef enum {
229   /// Line
230   CEED_TOPOLOGY_LINE = 1 << 16 | 0,
231   /// Triangle - 2D shape
232   CEED_TOPOLOGY_TRIANGLE = 2 << 16 | 1,
233   /// Quadralateral - 2D shape
234   CEED_TOPOLOGY_QUAD = 2 << 16 | 2,
235   /// Tetrahedron - 3D shape
236   CEED_TOPOLOGY_TET = 3 << 16 | 3,
237   /// Pyramid - 3D shape
238   CEED_TOPOLOGY_PYRAMID = 3 << 16 | 4,
239   /// Prism - 3D shape
240   CEED_TOPOLOGY_PRISM = 3 << 16 | 5,
241   /// Hexehedron - 3D shape
242   CEED_TOPOLOGY_HEX = 3 << 16 | 6,
243 } CeedElemTopology;
244 
245 /// Denotes type of data stored in a CeedQFunctionContext field
246 /// @ingroup CeedQFunction
247 typedef enum {
248   /// Double precision value
249   CEED_CONTEXT_FIELD_DOUBLE = 1,
250   /// 32 bit integer value
251   CEED_CONTEXT_FIELD_INT32 = 2,
252   /// Boolean value
253   CEED_CONTEXT_FIELD_BOOL = 3,
254 } CeedContextFieldType;
255