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