xref: /libCEED/include/ceed/types.h (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
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 #endif
35 
36 /**
37   @ingroup CeedQFunction
38   This macro populates the correct function annotations for User QFunction source for code generation backends or populates default values for CPU
39 backends. It also creates a variable `name_loc` populated with the correct source path for creating the respective User QFunction.
40 **/
41 #ifndef CEED_QFUNCTION
42 #define CEED_QFUNCTION(name)                                        \
43   static const char              name##_loc[] = __FILE__ ":" #name; \
44   CEED_QFUNCTION_ATTR static int name
45 #endif
46 
47 /**
48   @ingroup CeedQFunction
49   This macro populates the correct function annotations for User QFunction helper function source for code generation backends or populates default
50 values for CPU backends.
51 **/
52 #ifndef CEED_QFUNCTION_HELPER
53 #define CEED_QFUNCTION_HELPER CEED_QFUNCTION_ATTR static inline
54 #endif
55 
56 /**
57   @ingroup CeedQFunction
58   Using VLA syntax to reshape User QFunction inputs and outputs can make user code more readable.
59     VLA is a C99 feature that is not supported by the C++ dialect used by CUDA.
60     This macro allows users to use the VLA syntax with the CUDA backends.
61 **/
62 #ifndef CEED_Q_VLA
63 #define CEED_Q_VLA Q
64 #endif
65 
66 /**
67   @ingroup Ceed
68   This macro provides the appropriate SIMD Pragma for the compilation environment.
69     Code generation backends may redefine this macro, as needed.
70 **/
71 #ifndef CeedPragmaSIMD
72 #if defined(__INTEL_COMPILER)
73 #define CeedPragmaSIMD _Pragma("vector")
74 /// Cannot use Intel pragma ivdep because it miscompiles unpacking symmetric tensors, as in Poisson2DApply, where the SIMD loop body contains
75 /// temporaries such as the following.
76 ///
77 ///     const CeedScalar dXdxdXdxT[2][2] = {{qd[i+0*Q], qd[i+2*Q]},
78 ///                                         {qd[i+2*Q], qd[i+1*Q]}};
79 ///     for (int j=0; j<2; j++)
80 ///        vg[i+j*Q] = (du[0] * dXdxdXdxT[0][j] + du[1] * dXdxdXdxT[1][j]);
81 ///
82 /// Miscompilation with pragma ivdep observed with icc (ICC) 19.0.5.281 20190815 at -O2 and above.
83 #elif defined(__GNUC__) && __GNUC__ >= 5
84 #define CeedPragmaSIMD _Pragma("GCC ivdep")
85 #elif defined(_OPENMP) && _OPENMP >= 201307  // OpenMP-4.0 (July, 2013)
86 #define CeedPragmaSIMD _Pragma("omp simd")
87 #else
88 #define CeedPragmaSIMD
89 #endif
90 #endif
91 
92 /// Integer type, used for indexing
93 /// @ingroup Ceed
94 typedef int32_t CeedInt;
95 #define CeedInt_FMT "d"
96 
97 /// Integer type, used array sizes
98 /// @ingroup Ceed
99 typedef ptrdiff_t CeedSize;
100 
101 /// Scalar (floating point) types
102 ///
103 /// @ingroup Ceed
104 typedef enum {
105   /// Single precision
106   CEED_SCALAR_FP32,
107   /// Double precision
108   CEED_SCALAR_FP64
109 } CeedScalarType;
110 /// Base scalar type for the library to use: change which header is included to change the precision.
111 #include "ceed-f64.h"
112 
113 /// Ceed Errors
114 ///
115 /// This enum is used to specify the type of error returned by a function.
116 /// A zero error code is success, negative error codes indicate terminal errors and positive error codes indicate nonterminal errors.
117 /// With nonterminal errors the object state has not been modified, but with terminal errors the object data is likely modified or corrupted.
118 /// @ingroup Ceed
119 typedef enum {
120   /// Success error code
121   CEED_ERROR_SUCCESS = 0,
122   /// Minor error, generic
123   CEED_ERROR_MINOR = 1,
124   /// Minor error, dimension mismatch in inputs
125   CEED_ERROR_DIMENSION = 2,
126   /// Minor error, incomplete object setup
127   CEED_ERROR_INCOMPLETE = 3,
128   /// Minor error, incompatible arguments/configuration
129   CEED_ERROR_INCOMPATIBLE = 4,
130   /// Minor error, access lock problem
131   CEED_ERROR_ACCESS = 5,
132   /// Major error, generic
133   CEED_ERROR_MAJOR = -1,
134   /// Major error, internal backend error
135   CEED_ERROR_BACKEND = -2,
136   /// Major error, operation unsupported by current backend
137   CEED_ERROR_UNSUPPORTED = -3,
138 } CeedErrorType;
139 
140 #endif
141