xref: /libCEED/include/ceed/types.h (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson /// Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
2c9c2c079SJeremy L Thompson /// All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3c9c2c079SJeremy L Thompson ///
4c9c2c079SJeremy L Thompson /// SPDX-License-Identifier: BSD-2-Clause
5c9c2c079SJeremy L Thompson ///
6c9c2c079SJeremy L Thompson /// This file is part of CEED:  http://github.com/ceed
7c9c2c079SJeremy L Thompson 
8c9c2c079SJeremy L Thompson /// @file
9c9c2c079SJeremy L Thompson /// Public header for types and macros used in user QFunction source code
107f836c31SJames Wright #ifndef CEED_QFUNCTION_DEFS_H
117f836c31SJames Wright #define CEED_QFUNCTION_DEFS_H
12c9c2c079SJeremy L Thompson 
13a491a57eSJeremy L Thompson #ifndef CEED_RUNNING_JIT_PASS
14c9c2c079SJeremy L Thompson #include <stddef.h>
15c9c2c079SJeremy L Thompson #include <stdint.h>
16a491a57eSJeremy L Thompson #endif
17c9c2c079SJeremy L Thompson 
18c9c2c079SJeremy L Thompson /**
19c9c2c079SJeremy L Thompson   @ingroup CeedQFunction
20ea61e9acSJeremy L Thompson   This macro defines compiler attributes to the CEED_QFUNCTION to force inlining for called functions.
21ea61e9acSJeremy L Thompson     The `inline` declaration does not necessarily enforce a compiler to inline a function.
22ea61e9acSJeremy L Thompson     This can be detrimental to performance, so here we force inlining to occur unless inlining has been forced off (like during debugging).
23228d9efbSJames Wright **/
24228d9efbSJames Wright #ifndef CEED_QFUNCTION_ATTR
25228d9efbSJames Wright #ifndef __NO_INLINE__
26228d9efbSJames Wright #if defined(__GNUC__) || defined(__clang__)
27228d9efbSJames Wright #define CEED_QFUNCTION_ATTR __attribute__((flatten))
28b413b6aeSGabriele Bozzola #elif defined(__INTEL_COMPILER) || defined(__INTEL_LLVM_COMPILER)
29228d9efbSJames Wright #define CEED_QFUNCTION_ATTR _Pragma("forceinline")
30228d9efbSJames Wright #else
31228d9efbSJames Wright #define CEED_QFUNCTION_ATTR
32228d9efbSJames Wright #endif
33228d9efbSJames Wright #else
34228d9efbSJames Wright #define CEED_QFUNCTION_ATTR
35228d9efbSJames Wright #endif
360d409435SJed Brown #if defined(__GNUC__) || defined(__clang__)
370d409435SJed Brown #define CEED_QFUNCTION_HELPER_ATTR CEED_QFUNCTION_ATTR __attribute__((always_inline))
380d409435SJed Brown #else
390d409435SJed Brown #define CEED_QFUNCTION_HELPER_ATTR CEED_QFUNCTION_ATTR
400d409435SJed Brown #endif
41228d9efbSJames Wright #endif
42228d9efbSJames Wright 
43228d9efbSJames Wright /**
44228d9efbSJames Wright   @ingroup CeedQFunction
45ea61e9acSJeremy L Thompson   This macro populates the correct function annotations for User QFunction source for code generation backends or populates default values for CPU
46ea61e9acSJeremy L Thompson backends. It also creates a variable `name_loc` populated with the correct source path for creating the respective User QFunction.
47c9c2c079SJeremy L Thompson **/
48c9c2c079SJeremy L Thompson #ifndef CEED_QFUNCTION
49c9c2c079SJeremy L Thompson #define CEED_QFUNCTION(name)                                        \
50c9c2c079SJeremy L Thompson   static const char              name##_loc[] = __FILE__ ":" #name; \
51228d9efbSJames Wright   CEED_QFUNCTION_ATTR static int name
52c9c2c079SJeremy L Thompson #endif
53c9c2c079SJeremy L Thompson 
54c9c2c079SJeremy L Thompson /**
55c9c2c079SJeremy L Thompson     @ingroup CeedQFunction
562027fb9dSSirAlienTheGreat This macro populates the correct function for Rust-based User QFunction source for code generation backends or populates default values for CPU backends. It also creates a variable `name_loc` populated with the correct source path for creating the respective User QFunction. Note that the function, as named in rust, must be called `name_rs`. When referencing it in C, use just `name` (no `_rs`)
572027fb9dSSirAlienTheGreat Example:
582027fb9dSSirAlienTheGreat //ex1-volume.h
592027fb9dSSirAlienTheGreat CEED_QFUNCTION_RUST(build_mass)
602027fb9dSSirAlienTheGreat //ex1-volume.c
612027fb9dSSirAlienTheGreat CeedAddRustSourceRoot(ceed, "examples/ceed/ex1-volume-rs");
622027fb9dSSirAlienTheGreat // ex1-volume-rs/src/lib.rs
632027fb9dSSirAlienTheGreat #[no_mangle]
642027fb9dSSirAlienTheGreat pub unsafe extern "C" fn build_mass_rs(
652027fb9dSSirAlienTheGreat     ctx: *mut c_void,
662027fb9dSSirAlienTheGreat     Q: i32,
672027fb9dSSirAlienTheGreat     in: *const *const f64,
682027fb9dSSirAlienTheGreat     out: *mut *mut f64,
692027fb9dSSirAlienTheGreat ) -> i8
702027fb9dSSirAlienTheGreat **/
712027fb9dSSirAlienTheGreat #ifndef CEED_QFUNCTION_RUST
722027fb9dSSirAlienTheGreat #define CEED_QFUNCTION_RUST(name)                                                                                            \
732027fb9dSSirAlienTheGreat   CEED_QFUNCTION_ATTR int        name##_rs(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out); \
742027fb9dSSirAlienTheGreat   CEED_QFUNCTION_ATTR static int name(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {     \
752027fb9dSSirAlienTheGreat     return name##_rs(ctx, Q, in, out);                                                                                       \
762027fb9dSSirAlienTheGreat   }                                                                                                                          \
772027fb9dSSirAlienTheGreat   static const char name##_loc[] = __FILE__ ":" #name;
782027fb9dSSirAlienTheGreat #endif
792027fb9dSSirAlienTheGreat // Note: placing the _loc of the function below the function in the macro is required because python cffi will exclude the previous line (the }) based on the backslash at the end of it, which is required for our python build script to exclude macros. See /python/build_ceed_cffi.py for more details
802027fb9dSSirAlienTheGreat 
812027fb9dSSirAlienTheGreat /**
822027fb9dSSirAlienTheGreat   @ingroup CeedQFunction
83ea61e9acSJeremy L Thompson   This macro populates the correct function annotations for User QFunction helper function source for code generation backends or populates default
84c9c2c079SJeremy L Thompson values for CPU backends.
85c9c2c079SJeremy L Thompson **/
86c9c2c079SJeremy L Thompson #ifndef CEED_QFUNCTION_HELPER
870d409435SJed Brown #define CEED_QFUNCTION_HELPER CEED_QFUNCTION_HELPER_ATTR static inline
88c9c2c079SJeremy L Thompson #endif
89c9c2c079SJeremy L Thompson 
90c9c2c079SJeremy L Thompson /**
91c9c2c079SJeremy L Thompson   @ingroup CeedQFunction
92ea61e9acSJeremy L Thompson   Using VLA syntax to reshape User QFunction inputs and outputs can make user code more readable.
93ea61e9acSJeremy L Thompson     VLA is a C99 feature that is not supported by the C++ dialect used by CUDA.
94ea61e9acSJeremy L Thompson     This macro allows users to use the VLA syntax with the CUDA backends.
95c9c2c079SJeremy L Thompson **/
96c9c2c079SJeremy L Thompson #ifndef CEED_Q_VLA
97c9c2c079SJeremy L Thompson #define CEED_Q_VLA Q
98c9c2c079SJeremy L Thompson #endif
99c9c2c079SJeremy L Thompson 
100c9c2c079SJeremy L Thompson /**
101c9c2c079SJeremy L Thompson   @ingroup Ceed
102ea61e9acSJeremy L Thompson   This macro provides the appropriate SIMD Pragma for the compilation environment.
103ea61e9acSJeremy L Thompson     Code generation backends may redefine this macro, as needed.
104c9c2c079SJeremy L Thompson **/
105c9c2c079SJeremy L Thompson #ifndef CeedPragmaSIMD
106b413b6aeSGabriele Bozzola #if defined(__INTEL_COMPILER) || defined(__INTEL_LLVM_COMPILER)
107c9c2c079SJeremy L Thompson #define CeedPragmaSIMD _Pragma("vector")
108ea61e9acSJeremy L Thompson /// Cannot use Intel pragma ivdep because it miscompiles unpacking symmetric tensors, as in Poisson2DApply, where the SIMD loop body contains
109ea61e9acSJeremy L Thompson /// temporaries such as the following.
110c9c2c079SJeremy L Thompson ///
111c9c2c079SJeremy L Thompson ///     const CeedScalar dXdxdXdxT[2][2] = {{qd[i+0*Q], qd[i+2*Q]},
112c9c2c079SJeremy L Thompson ///                                         {qd[i+2*Q], qd[i+1*Q]}};
113c9c2c079SJeremy L Thompson ///     for (int j=0; j<2; j++)
114c9c2c079SJeremy L Thompson ///        vg[i+j*Q] = (du[0] * dXdxdXdxT[0][j] + du[1] * dXdxdXdxT[1][j]);
115c9c2c079SJeremy L Thompson ///
116ea61e9acSJeremy L Thompson /// Miscompilation with pragma ivdep observed with icc (ICC) 19.0.5.281 20190815 at -O2 and above.
117c9c2c079SJeremy L Thompson #elif defined(__GNUC__) && __GNUC__ >= 5
118c9c2c079SJeremy L Thompson #define CeedPragmaSIMD _Pragma("GCC ivdep")
119b569baafSSebastian Grimberg #elif defined(__clang__)
120b569baafSSebastian Grimberg #define CeedPragmaSIMD _Pragma("clang loop vectorize(enable)")
121c9c2c079SJeremy L Thompson #elif defined(_OPENMP) && _OPENMP >= 201307  // OpenMP-4.0 (July, 2013)
122c9c2c079SJeremy L Thompson #define CeedPragmaSIMD _Pragma("omp simd")
123c9c2c079SJeremy L Thompson #else
124c9c2c079SJeremy L Thompson #define CeedPragmaSIMD
125c9c2c079SJeremy L Thompson #endif
126c9c2c079SJeremy L Thompson #endif
127c9c2c079SJeremy L Thompson 
128c9c2c079SJeremy L Thompson /// Integer type, used for indexing
129c9c2c079SJeremy L Thompson /// @ingroup Ceed
130c9c2c079SJeremy L Thompson typedef int32_t CeedInt;
131c9c2c079SJeremy L Thompson #define CeedInt_FMT "d"
132c9c2c079SJeremy L Thompson 
133c9c2c079SJeremy L Thompson /// Integer type, used array sizes
134c9c2c079SJeremy L Thompson /// @ingroup Ceed
135c9c2c079SJeremy L Thompson typedef ptrdiff_t CeedSize;
1364021610dSJames Wright #define CeedSize_FMT "td"
137c9c2c079SJeremy L Thompson 
1380c73c039SSebastian Grimberg /// Integer type, for small integers
1390c73c039SSebastian Grimberg /// @ingroup Ceed
14089edb9e3SSebastian Grimberg typedef signed char CeedInt8;
1410c73c039SSebastian Grimberg #define CeedInt8_FMT "d"
1420c73c039SSebastian Grimberg 
143c9c2c079SJeremy L Thompson /// Scalar (floating point) types
144c9c2c079SJeremy L Thompson ///
145c9c2c079SJeremy L Thompson /// @ingroup Ceed
146c9c2c079SJeremy L Thompson typedef enum {
147c9c2c079SJeremy L Thompson   /// Single precision
148c9c2c079SJeremy L Thompson   CEED_SCALAR_FP32,
149c9c2c079SJeremy L Thompson   /// Double precision
150c9c2c079SJeremy L Thompson   CEED_SCALAR_FP64
151c9c2c079SJeremy L Thompson } CeedScalarType;
152ea61e9acSJeremy L Thompson /// Base scalar type for the library to use: change which header is included to change the precision.
15349aac155SJeremy L Thompson #include "ceed-f64.h"  // IWYU pragma: export
154c9c2c079SJeremy L Thompson 
1559bd0a4deSJeremy L Thompson /// Ceed error code.
156c9c2c079SJeremy L Thompson ///
157c9c2c079SJeremy L Thompson /// This enum is used to specify the type of error returned by a function.
158ea61e9acSJeremy L Thompson /// A zero error code is success, negative error codes indicate terminal errors and positive error codes indicate nonterminal errors.
159ea61e9acSJeremy L Thompson /// With nonterminal errors the object state has not been modified, but with terminal errors the object data is likely modified or corrupted.
160c9c2c079SJeremy L Thompson /// @ingroup Ceed
161c9c2c079SJeremy L Thompson typedef enum {
162c9c2c079SJeremy L Thompson   /// Success error code
163c9c2c079SJeremy L Thompson   CEED_ERROR_SUCCESS = 0,
164c9c2c079SJeremy L Thompson   /// Minor error, generic
165c9c2c079SJeremy L Thompson   CEED_ERROR_MINOR = 1,
166c9c2c079SJeremy L Thompson   /// Minor error, dimension mismatch in inputs
167c9c2c079SJeremy L Thompson   CEED_ERROR_DIMENSION = 2,
168c9c2c079SJeremy L Thompson   /// Minor error, incomplete object setup
169c9c2c079SJeremy L Thompson   CEED_ERROR_INCOMPLETE = 3,
170c9c2c079SJeremy L Thompson   /// Minor error, incompatible arguments/configuration
171c9c2c079SJeremy L Thompson   CEED_ERROR_INCOMPATIBLE = 4,
172c9c2c079SJeremy L Thompson   /// Minor error, access lock problem
173c9c2c079SJeremy L Thompson   CEED_ERROR_ACCESS = 5,
174c9c2c079SJeremy L Thompson   /// Major error, generic
175c9c2c079SJeremy L Thompson   CEED_ERROR_MAJOR = -1,
176c9c2c079SJeremy L Thompson   /// Major error, internal backend error
177c9c2c079SJeremy L Thompson   CEED_ERROR_BACKEND = -2,
178c9c2c079SJeremy L Thompson   /// Major error, operation unsupported by current backend
179c9c2c079SJeremy L Thompson   CEED_ERROR_UNSUPPORTED = -3,
180c9c2c079SJeremy L Thompson } CeedErrorType;
181c9c2c079SJeremy L Thompson 
1829bd0a4deSJeremy L Thompson /// Specify memory type.
1839bd0a4deSJeremy L Thompson /// Many Ceed interfaces take or return pointers to memory.
1849bd0a4deSJeremy L Thompson /// This enum is used to specify where the memory being provided or requested must reside.
1859bd0a4deSJeremy L Thompson /// @ingroup Ceed
1869bd0a4deSJeremy L Thompson typedef enum {
1879bd0a4deSJeremy L Thompson   /// Memory resides on the host
1889bd0a4deSJeremy L Thompson   CEED_MEM_HOST,
1899bd0a4deSJeremy L Thompson   /// Memory resides on a device (corresponding to \ref Ceed resource)
1909bd0a4deSJeremy L Thompson   CEED_MEM_DEVICE,
1919bd0a4deSJeremy L Thompson } CeedMemType;
1929bd0a4deSJeremy L Thompson 
1939bd0a4deSJeremy L Thompson /// Conveys ownership status of arrays passed to Ceed interfaces.
1949bd0a4deSJeremy L Thompson /// @ingroup Ceed
1959bd0a4deSJeremy L Thompson typedef enum {
1969bd0a4deSJeremy L Thompson   /// Implementation will copy the values and not store the passed pointer.
1979bd0a4deSJeremy L Thompson   CEED_COPY_VALUES,
1989bd0a4deSJeremy L Thompson   /// Implementation can use and modify the data provided by the user, but does not take ownership.
1999bd0a4deSJeremy L Thompson   CEED_USE_POINTER,
2009bd0a4deSJeremy L Thompson   /// Implementation takes ownership of the pointer and will free using CeedFree() when done using it.
2019bd0a4deSJeremy L Thompson   /// The user should not assume that the pointer remains valid after ownership has been transferred.
2029bd0a4deSJeremy L Thompson   /// Note that arrays allocated using C++ operator new or other allocators cannot generally be freed using CeedFree().
2039bd0a4deSJeremy L Thompson   /// CeedFree() is capable of freeing any memory that can be freed using free().
2049bd0a4deSJeremy L Thompson   CEED_OWN_POINTER,
2059bd0a4deSJeremy L Thompson } CeedCopyMode;
2069bd0a4deSJeremy L Thompson 
2079bd0a4deSJeremy L Thompson /// Denotes type of vector norm to be computed
2089bd0a4deSJeremy L Thompson /// @ingroup CeedVector
2099bd0a4deSJeremy L Thompson typedef enum {
2109bd0a4deSJeremy L Thompson   /// \f$\Vert \bm{x}\Vert_1 = \sum_i \vert x_i\vert\f$
2119bd0a4deSJeremy L Thompson   CEED_NORM_1,
2129bd0a4deSJeremy L Thompson   /// \f$\Vert \bm{x} \Vert_2 = \sqrt{\sum_i x_i^2}\f$
2139bd0a4deSJeremy L Thompson   CEED_NORM_2,
2149bd0a4deSJeremy L Thompson   /// \f$\Vert \bm{x} \Vert_\infty = \max_i \vert x_i \vert\f$
2159bd0a4deSJeremy L Thompson   CEED_NORM_MAX,
2169bd0a4deSJeremy L Thompson } CeedNormType;
2179bd0a4deSJeremy L Thompson 
2189bd0a4deSJeremy L Thompson /// Denotes whether a linear transformation or its transpose should be applied
2199bd0a4deSJeremy L Thompson /// @ingroup CeedBasis
2209bd0a4deSJeremy L Thompson typedef enum {
2219bd0a4deSJeremy L Thompson   /// Apply the linear transformation
2229bd0a4deSJeremy L Thompson   CEED_NOTRANSPOSE,
2239bd0a4deSJeremy L Thompson   /// Apply the transpose
2249bd0a4deSJeremy L Thompson   CEED_TRANSPOSE
2259bd0a4deSJeremy L Thompson } CeedTransposeMode;
2269bd0a4deSJeremy L Thompson 
2279bd0a4deSJeremy L Thompson /// Basis evaluation mode
2289bd0a4deSJeremy L Thompson /// @ingroup CeedBasis
2299bd0a4deSJeremy L Thompson typedef enum {
2309bd0a4deSJeremy L Thompson   /// Perform no evaluation (either because there is no data or it is already at quadrature points)
2319bd0a4deSJeremy L Thompson   CEED_EVAL_NONE = 0,
2329bd0a4deSJeremy L Thompson   /// Interpolate from nodes to quadrature points
2339bd0a4deSJeremy L Thompson   CEED_EVAL_INTERP = 1,
234c4e3f59bSSebastian Grimberg   /// Evaluate gradients at quadrature points from input in the basis
2359bd0a4deSJeremy L Thompson   CEED_EVAL_GRAD = 2,
236c4e3f59bSSebastian Grimberg   /// Evaluate divergence at quadrature points from input in the basis
2379bd0a4deSJeremy L Thompson   CEED_EVAL_DIV = 4,
238c4e3f59bSSebastian Grimberg   /// Evaluate curl at quadrature points from input in the basis
2399bd0a4deSJeremy L Thompson   CEED_EVAL_CURL = 8,
2409bd0a4deSJeremy L Thompson   /// Using no input, evaluate quadrature weights on the reference element
2419bd0a4deSJeremy L Thompson   CEED_EVAL_WEIGHT = 16,
2429bd0a4deSJeremy L Thompson } CeedEvalMode;
2439bd0a4deSJeremy L Thompson 
2449bd0a4deSJeremy L Thompson /// Type of quadrature; also used for location of nodes
2459bd0a4deSJeremy L Thompson /// @ingroup CeedBasis
2469bd0a4deSJeremy L Thompson typedef enum {
2479bd0a4deSJeremy L Thompson   /// Gauss-Legendre quadrature
2489bd0a4deSJeremy L Thompson   CEED_GAUSS = 0,
2499bd0a4deSJeremy L Thompson   /// Gauss-Legendre-Lobatto quadrature
2509bd0a4deSJeremy L Thompson   CEED_GAUSS_LOBATTO = 1,
2519bd0a4deSJeremy L Thompson } CeedQuadMode;
2529bd0a4deSJeremy L Thompson 
253c4e3f59bSSebastian Grimberg /// Type of basis shape to create non-tensor element basis.
2549bd0a4deSJeremy L Thompson /// Dimension can be extracted with bitwise AND (CeedElemTopology & 2**(dim + 2)) == TRUE
2559bd0a4deSJeremy L Thompson /// @ingroup CeedBasis
2569bd0a4deSJeremy L Thompson typedef enum {
2579bd0a4deSJeremy L Thompson   /// Line
2589bd0a4deSJeremy L Thompson   CEED_TOPOLOGY_LINE = 1 << 16 | 0,
2599bd0a4deSJeremy L Thompson   /// Triangle - 2D shape
2609bd0a4deSJeremy L Thompson   CEED_TOPOLOGY_TRIANGLE = 2 << 16 | 1,
2619bd0a4deSJeremy L Thompson   /// Quadralateral - 2D shape
2629bd0a4deSJeremy L Thompson   CEED_TOPOLOGY_QUAD = 2 << 16 | 2,
2639bd0a4deSJeremy L Thompson   /// Tetrahedron - 3D shape
2649bd0a4deSJeremy L Thompson   CEED_TOPOLOGY_TET = 3 << 16 | 3,
2659bd0a4deSJeremy L Thompson   /// Pyramid - 3D shape
2669bd0a4deSJeremy L Thompson   CEED_TOPOLOGY_PYRAMID = 3 << 16 | 4,
2679bd0a4deSJeremy L Thompson   /// Prism - 3D shape
2689bd0a4deSJeremy L Thompson   CEED_TOPOLOGY_PRISM = 3 << 16 | 5,
2699bd0a4deSJeremy L Thompson   /// Hexehedron - 3D shape
2709bd0a4deSJeremy L Thompson   CEED_TOPOLOGY_HEX = 3 << 16 | 6,
2719bd0a4deSJeremy L Thompson } CeedElemTopology;
2729bd0a4deSJeremy L Thompson 
2739bd0a4deSJeremy L Thompson /// Denotes type of data stored in a CeedQFunctionContext field
2749bd0a4deSJeremy L Thompson /// @ingroup CeedQFunction
2759bd0a4deSJeremy L Thompson typedef enum {
2769bd0a4deSJeremy L Thompson   /// Double precision value
2779bd0a4deSJeremy L Thompson   CEED_CONTEXT_FIELD_DOUBLE = 1,
2789bd0a4deSJeremy L Thompson   /// 32 bit integer value
2799bd0a4deSJeremy L Thompson   CEED_CONTEXT_FIELD_INT32 = 2,
2805b6ec284SJeremy L Thompson   /// Boolean value
2815b6ec284SJeremy L Thompson   CEED_CONTEXT_FIELD_BOOL = 3,
2829bd0a4deSJeremy L Thompson } CeedContextFieldType;
2837f836c31SJames Wright 
2847f836c31SJames Wright #endif  // CEED_QFUNCTION_DEFS_H
285