xref: /petsc/include/petscsystypes.h (revision 37d05b0256c1e9ba4bc423c4eccb3df226931ef0)
1 /* Portions of this code are under:
2    Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved.
3 */
4 
5 #ifndef PETSCSYSTYPES_H
6 #define PETSCSYSTYPES_H
7 
8 #include <petscconf.h>
9 #include <petscconf_poison.h>
10 #include <petscfix.h>
11 #include <petscmacros.h> // PETSC_NODISCARD, PETSC_CPP_VERSION
12 #include <stddef.h>
13 
14 /* SUBMANSEC = Sys */
15 
16 #include <limits.h> // INT_MIN, INT_MAX
17 
18 #if defined(__clang__) || (PETSC_CPP_VERSION >= 17)
19   // clang allows both [[nodiscard]] and __attribute__((warn_unused_result)) on type
20   // definitions. GCC, however, does not, so check that we are using C++17 [[nodiscard]]
21   // instead of __attribute__((warn_unused_result))
22   #define PETSC_ERROR_CODE_NODISCARD PETSC_NODISCARD
23 #else
24   #define PETSC_ERROR_CODE_NODISCARD
25 #endif
26 
27 #ifdef PETSC_USE_STRICT_PETSCERRORCODE
28   #define PETSC_ERROR_CODE_TYPEDEF   typedef
29   #define PETSC_ERROR_CODE_ENUM_NAME PetscErrorCode
30 #else
31   #define PETSC_ERROR_CODE_TYPEDEF
32   #define PETSC_ERROR_CODE_ENUM_NAME
33 #endif
34 
35 /*E
36   PetscErrorCode - Datatype used to return PETSc error codes.
37 
38   Level: beginner
39 
40   Notes:
41   Virtually all PETSc functions return an error code. It is the callers responsibility to check
42   the value of the returned error code after each PETSc call to determine if any errors
43   occurred. A set of convenience macros (e.g. `PetscCall()`, `PetscCallVoid()`) are provided
44   for this purpose. Failing to properly check for errors is not supported, as errors may leave
45   PETSc in an undetermined state.
46 
47   One can retrieve the error string corresponding to a particular error code using
48   `PetscErrorMessage()`.
49 
50   The user can also configure PETSc with the `--with-strict-petscerrorcode` option to enable
51   compiler warnings when the returned error codes are not captured and checked. Users are
52   *heavily* encouraged to opt-in to this option, as it will become enabled by default in a
53   future release.
54 
55   Developer Notes:
56 
57   These are the generic error codes. These error codes are used in many different places in the
58   PETSc source code. The C-string versions are at defined in `PetscErrorStrings[]` in
59   `src/sys/error/err.c`, while the fortran versions are defined in
60   `src/sys/f90-mod/petscerror.h`. Any changes here must also be made in both locations.
61 
62 .seealso: `PetscErrorMessage()`, `PetscCall()`, `SETERRQ()`
63 E*/
64 PETSC_ERROR_CODE_TYPEDEF enum PETSC_ERROR_CODE_NODISCARD {
65   PETSC_SUCCESS                   = 0,
66   PETSC_ERR_BOOLEAN_MACRO_FAILURE = 1, /* do not use */
67 
68   PETSC_ERR_MIN_VALUE = 54, /* should always be one less then the smallest value */
69 
70   PETSC_ERR_MEM            = 55, /* unable to allocate requested memory */
71   PETSC_ERR_SUP            = 56, /* no support for requested operation */
72   PETSC_ERR_SUP_SYS        = 57, /* no support for requested operation on this computer system */
73   PETSC_ERR_ORDER          = 58, /* operation done in wrong order */
74   PETSC_ERR_SIG            = 59, /* signal received */
75   PETSC_ERR_FP             = 72, /* floating point exception */
76   PETSC_ERR_COR            = 74, /* corrupted PETSc object */
77   PETSC_ERR_LIB            = 76, /* error in library called by PETSc */
78   PETSC_ERR_PLIB           = 77, /* PETSc library generated inconsistent data */
79   PETSC_ERR_MEMC           = 78, /* memory corruption */
80   PETSC_ERR_CONV_FAILED    = 82, /* iterative method (KSP or SNES) failed */
81   PETSC_ERR_USER           = 83, /* user has not provided needed function */
82   PETSC_ERR_SYS            = 88, /* error in system call */
83   PETSC_ERR_POINTER        = 70, /* pointer does not point to valid address */
84   PETSC_ERR_MPI_LIB_INCOMP = 87, /* MPI library at runtime is not compatible with MPI user compiled with */
85 
86   PETSC_ERR_ARG_SIZ          = 60, /* nonconforming object sizes used in operation */
87   PETSC_ERR_ARG_IDN          = 61, /* two arguments not allowed to be the same */
88   PETSC_ERR_ARG_WRONG        = 62, /* wrong argument (but object probably ok) */
89   PETSC_ERR_ARG_CORRUPT      = 64, /* null or corrupted PETSc object as argument */
90   PETSC_ERR_ARG_OUTOFRANGE   = 63, /* input argument, out of range */
91   PETSC_ERR_ARG_BADPTR       = 68, /* invalid pointer argument */
92   PETSC_ERR_ARG_NOTSAMETYPE  = 69, /* two args must be same object type */
93   PETSC_ERR_ARG_NOTSAMECOMM  = 80, /* two args must be same communicators */
94   PETSC_ERR_ARG_WRONGSTATE   = 73, /* object in argument is in wrong state, e.g. unassembled mat */
95   PETSC_ERR_ARG_TYPENOTSET   = 89, /* the type of the object has not yet been set */
96   PETSC_ERR_ARG_INCOMP       = 75, /* two arguments are incompatible */
97   PETSC_ERR_ARG_NULL         = 85, /* argument is null that should not be */
98   PETSC_ERR_ARG_UNKNOWN_TYPE = 86, /* type name doesn't match any registered type */
99 
100   PETSC_ERR_FILE_OPEN       = 65, /* unable to open file */
101   PETSC_ERR_FILE_READ       = 66, /* unable to read from file */
102   PETSC_ERR_FILE_WRITE      = 67, /* unable to write to file */
103   PETSC_ERR_FILE_UNEXPECTED = 79, /* unexpected data in file */
104 
105   PETSC_ERR_MAT_LU_ZRPVT = 71, /* detected a zero pivot during LU factorization */
106   PETSC_ERR_MAT_CH_ZRPVT = 81, /* detected a zero pivot during Cholesky factorization */
107 
108   PETSC_ERR_INT_OVERFLOW   = 84,
109   PETSC_ERR_FLOP_COUNT     = 90,
110   PETSC_ERR_NOT_CONVERGED  = 91,  /* solver did not converge */
111   PETSC_ERR_MISSING_FACTOR = 92,  /* MatGetFactor() failed */
112   PETSC_ERR_OPT_OVERWRITE  = 93,  /* attempted to over write options which should not be changed */
113   PETSC_ERR_WRONG_MPI_SIZE = 94,  /* example/application run with number of MPI ranks it does not support */
114   PETSC_ERR_USER_INPUT     = 95,  /* missing or incorrect user input */
115   PETSC_ERR_GPU_RESOURCE   = 96,  /* unable to load a GPU resource, for example cuBLAS */
116   PETSC_ERR_GPU            = 97,  /* An error from a GPU call, this may be due to lack of resources on the GPU or a true error in the call */
117   PETSC_ERR_MPI            = 98,  /* general MPI error */
118   PETSC_ERR_RETURN         = 99,  /* PetscError() incorrectly returned an error code of 0 */
119   PETSC_ERR_MAX_VALUE      = 100, /* this is always the one more than the largest error code */
120 
121   /*
122     do not use, exist purely to make the enum bounds equal that of a regular int (so conversion
123     to int in main() is not undefined behavior)
124   */
125   PETSC_ERR_MIN_SIGNED_BOUND_DO_NOT_USE = INT_MIN,
126   PETSC_ERR_MAX_SIGNED_BOUND_DO_NOT_USE = INT_MAX
127 } PETSC_ERROR_CODE_ENUM_NAME;
128 
129 #ifndef PETSC_USE_STRICT_PETSCERRORCODE
130 typedef int PetscErrorCode;
131 
132   /*
133   Needed so that C++ lambdas can deduce the return type as PetscErrorCode from
134   PetscFunctionReturn(PETSC_SUCCESS). Otherwise we get
135 
136   error: return type '(unnamed enum at include/petscsystypes.h:50:1)' must match previous
137   return type 'int' when lambda expression has unspecified explicit return type
138   PetscFunctionReturn(PETSC_SUCCESS);
139   ^
140 */
141   #define PETSC_SUCCESS ((PetscErrorCode)0)
142 #endif
143 
144 #undef PETSC_ERROR_CODE_NODISCARD
145 #undef PETSC_ERROR_CODE_TYPEDEF
146 #undef PETSC_ERROR_CODE_ENUM_NAME
147 
148 /*MC
149 
150     PetscClassId - A unique id used to identify each PETSc class.
151 
152     Notes:
153     Use `PetscClassIdRegister()` to obtain a new value for a new class being created. Usually
154          XXXInitializePackage() calls it for each class it defines.
155 
156     Developer Notes:
157     Internal integer stored in the `_p_PetscObject` data structure.
158          These are all computed by an offset from the lowest one, `PETSC_SMALLEST_CLASSID`.
159 
160     Level: developer
161 
162 .seealso: `PetscClassIdRegister()`, `PetscLogEventRegister()`, `PetscHeaderCreate()`
163 M*/
164 typedef int PetscClassId;
165 
166 /*MC
167     PetscMPIInt - datatype used to represent 'int' parameters to MPI functions.
168 
169     Level: intermediate
170 
171     Notes:
172     This is always a 32 bit integer, sometimes it is the same as `PetscInt`, but if PETSc was built with --with-64-bit-indices but
173            standard C/Fortran integers are 32 bit then this is NOT the same as `PetscInt`; it remains 32 bit.
174 
175     `PetscMPIIntCast`(a,&b) checks if the given `PetscInt` a will fit in a `PetscMPIInt`, if not it
176       generates a `PETSC_ERR_ARG_OUTOFRANGE` error.
177 
178 .seealso: `PetscBLASInt`, `PetscInt`, `PetscMPIIntCast()`
179 
180 M*/
181 typedef int PetscMPIInt;
182 
183 /* Limit MPI to 32-bits */
184 enum {
185   PETSC_MPI_INT_MIN = INT_MIN,
186   PETSC_MPI_INT_MAX = INT_MAX
187 };
188 
189 /*MC
190     PetscSizeT - datatype used to represent sizes in memory (like size_t)
191 
192     Level: intermediate
193 
194     Notes:
195     This is equivalent to size_t, but defined for consistency with Fortran, which lacks a native equivalent of size_t.
196 
197 .seealso: `PetscInt`, `PetscInt64`, `PetscCount`
198 
199 M*/
200 typedef size_t PetscSizeT;
201 
202 /*MC
203     PetscCount - signed datatype used to represent counts
204 
205     Level: intermediate
206 
207     Notes:
208     This is equivalent to ptrdiff_t, but defined for consistency with Fortran, which lacks a native equivalent of ptrdiff_t.
209 
210     Use `PetscCount_FMT` to format with `PetscPrintf()`, `printf()`, and related functions.
211 
212 .seealso: `PetscInt`, `PetscInt64`, `PetscSizeT`
213 
214 M*/
215 typedef ptrdiff_t PetscCount;
216 #define PetscCount_FMT "td"
217 
218 /*MC
219     PetscEnum - datatype used to pass enum types within PETSc functions.
220 
221     Level: intermediate
222 
223 .seealso: `PetscOptionsGetEnum()`, `PetscOptionsEnum()`, `PetscBagRegisterEnum()`
224 M*/
225 typedef enum {
226   ENUM_DUMMY
227 } PetscEnum;
228 
229 typedef short PetscShort;
230 typedef char  PetscChar;
231 typedef float PetscFloat;
232 
233 /*MC
234   PetscInt - PETSc type that represents an integer, used primarily to
235       represent size of arrays and indexing into arrays. Its size can be configured with the option --with-64-bit-indices to be either 32-bit (default) or 64-bit.
236 
237   Notes:
238   For MPI calls that require datatypes, use `MPIU_INT` as the datatype for `PetscInt`. It will automatically work correctly regardless of the size of PetscInt.
239 
240   Level: beginner
241 
242 .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscReal`, `PetscScalar`, `PetscComplex`, `PetscInt`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`, `MPIU_INT`
243 M*/
244 
245 #if defined(PETSC_HAVE_STDINT_H)
246   #include <stdint.h>
247 #endif
248 #if defined(PETSC_HAVE_INTTYPES_H)
249   #if !defined(__STDC_FORMAT_MACROS)
250     #define __STDC_FORMAT_MACROS /* required for using PRId64 from c++ */
251   #endif
252   #include <inttypes.h>
253   #if !defined(PRId64)
254     #define PRId64 "ld"
255   #endif
256 #endif
257 
258 #if defined(PETSC_HAVE_STDINT_H) && defined(PETSC_HAVE_INTTYPES_H) && defined(PETSC_HAVE_MPI_INT64_T) /* MPI_INT64_T is not guaranteed to be a macro */
259 typedef int64_t PetscInt64;
260 
261   #define PETSC_INT64_MIN INT64_MIN
262   #define PETSC_INT64_MAX INT64_MAX
263 
264 #elif (PETSC_SIZEOF_LONG_LONG == 8)
265 typedef long long PetscInt64;
266 
267   #define PETSC_INT64_MIN LLONG_MIN
268   #define PETSC_INT64_MAX LLONG_MAX
269 
270 #elif defined(PETSC_HAVE___INT64)
271 typedef __int64 PetscInt64;
272 
273   #define PETSC_INT64_MIN INT64_MIN
274   #define PETSC_INT64_MAX INT64_MAX
275 
276 #else
277   #error "cannot determine PetscInt64 type"
278 #endif
279 
280 #if defined(PETSC_USE_64BIT_INDICES)
281 typedef PetscInt64 PetscInt;
282 
283   #define PETSC_INT_MIN PETSC_INT64_MIN
284   #define PETSC_INT_MAX PETSC_INT64_MAX
285   #define PetscInt_FMT  PetscInt64_FMT
286 #else
287 typedef int       PetscInt;
288 
289 enum {
290   PETSC_INT_MIN = INT_MIN,
291   PETSC_INT_MAX = INT_MAX
292 };
293 
294   #define PetscInt_FMT "d"
295 #endif
296 
297 #define PETSC_MIN_INT    PETSC_INT_MIN
298 #define PETSC_MAX_INT    PETSC_INT_MAX
299 #define PETSC_MAX_UINT16 65535
300 
301 #if defined(PETSC_HAVE_STDINT_H) && defined(PETSC_HAVE_INTTYPES_H) && defined(PETSC_HAVE_MPI_INT64_T) /* MPI_INT64_T is not guaranteed to be a macro */
302   #define MPIU_INT64     MPI_INT64_T
303   #define PetscInt64_FMT PRId64
304 #elif (PETSC_SIZEOF_LONG_LONG == 8)
305   #define MPIU_INT64     MPI_LONG_LONG_INT
306   #define PetscInt64_FMT "lld"
307 #elif defined(PETSC_HAVE___INT64)
308   #define MPIU_INT64     MPI_INT64_T
309   #define PetscInt64_FMT "ld"
310 #else
311   #error "cannot determine PetscInt64 type"
312 #endif
313 
314 /*MC
315    PetscBLASInt - datatype used to represent 'int' parameters to BLAS/LAPACK functions.
316 
317    Notes:
318     Usually this is the same as `PetscIn`t, but if PETSc was built with --with-64-bit-indices but
319            standard C/Fortran integers are 32 bit then this may not be the same as `PetscInt`,
320            except on some BLAS/LAPACK implementations that support 64 bit integers see the notes below.
321 
322     `PetscErrorCode` `PetscBLASIntCast`(a,&b) checks if the given `PetscInt` a will fit in a `PetscBLASInt`, if not it
323       generates a `PETSC_ERR_ARG_OUTOFRANGE` error
324 
325    Installation Notes:
326     ./configure automatically determines the size of the integers used by BLAS/LAPACK except when --with-batch is used
327     in that situation one must know (by some other means) if the integers used by BLAS/LAPACK are 64 bit and if so pass the flag --known-64-bit-blas-indice
328 
329     MATLAB ships with BLAS and LAPACK that use 64 bit integers, for example if you run ./configure with, the option
330      --with-blaslapack-lib=[/Applications/MATLAB_R2010b.app/bin/maci64/libmwblas.dylib,/Applications/MATLAB_R2010b.app/bin/maci64/libmwlapack.dylib]
331 
332     MKL ships with both 32 and 64 bit integer versions of the BLAS and LAPACK. If you pass the flag -with-64-bit-blas-indices PETSc will link
333     against the 64 bit version, otherwise it use the 32 bit version
334 
335     OpenBLAS can be built to use 64 bit integers. The ./configure options --download-openblas -with-64-bit-blas-indices will build a 64 bit integer version
336 
337     External packages such as hypre, ML, SuperLU etc do not provide any support for passing 64 bit integers to BLAS/LAPACK so cannot
338     be used with PETSc when PETSc links against 64 bit integer BLAS/LAPACK. ./configure will generate an error if you attempt to link PETSc against any of
339     these external libraries while using 64 bit integer BLAS/LAPACK.
340 
341    Level: intermediate
342 
343 .seealso: `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`
344 
345 M*/
346 #if defined(PETSC_HAVE_64BIT_BLAS_INDICES)
347 typedef PetscInt64 PetscBLASInt;
348 
349   #define PETSC_BLAS_INT_MIN PETSC_INT64_MIN
350   #define PETSC_BLAS_INT_MAX PETSC_INT64_MAX
351   #define PetscBLASInt_FMT   PetscInt64_FMT
352 #else
353 typedef int PetscBLASInt;
354 
355 enum {
356   PETSC_BLAS_INT_MIN = INT_MIN,
357   PETSC_BLAS_INT_MAX = INT_MAX
358 };
359 
360   #define PetscBLASInt_FMT "d"
361 #endif
362 
363 /*MC
364    PetscCuBLASInt - datatype used to represent 'int' parameters to cuBLAS/cuSOLVER functions.
365 
366    Notes:
367     As of this writing PetscCuBLASInt is always the system `int`.
368 
369     `PetscErrorCode` `PetscCuBLASIntCast`(a,&b) checks if the given `PetscInt` a will fit in a `PetscCuBLASInt`, if not it
370       generates a `PETSC_ERR_ARG_OUTOFRANGE` error
371 
372    Level: intermediate
373 
374 .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscCuBLASIntCast()`
375 
376 M*/
377 typedef int PetscCuBLASInt;
378 
379 enum {
380   PETSC_CUBLAS_INT_MIN = INT_MIN,
381   PETSC_CUBLAS_INT_MAX = INT_MAX
382 };
383 
384 /*MC
385    PetscHipBLASInt - datatype used to represent 'int' parameters to hipBLAS/hipSOLVER functions.
386 
387    Notes:
388     As of this writing PetscHipBLASInt is always the system `int`.
389 
390     PetscErrorCode PetscHipBLASIntCast(a,&b) checks if the given PetscInt a will fit in a PetscHipBLASInt, if not it
391       generates a PETSC_ERR_ARG_OUTOFRANGE error
392 
393    Level: intermediate
394 
395 .seealso: PetscBLASInt, PetscMPIInt, PetscInt, PetscHipBLASIntCast()
396 
397 M*/
398 typedef int PetscHipBLASInt;
399 
400 enum {
401   PETSC_HIPBLAS_INT_MIN = INT_MIN,
402   PETSC_HIPBLAS_INT_MAX = INT_MAX
403 };
404 
405 /*E
406     PetscBool  - Logical variable. Actually an enum in C and a logical in Fortran.
407 
408    Level: beginner
409 
410    Developer Note:
411    Why have `PetscBool`, why not use bool in C? The problem is that K and R C, C99 and C++ all have different mechanisms for
412       boolean values. It is not easy to have a simple macro that that will work properly in all circumstances with all three mechanisms.
413 
414 .seealso: `PETSC_TRUE`, `PETSC_FALSE`, `PetscNot()`, `PetscBool3`
415 E*/
416 typedef enum {
417   PETSC_FALSE,
418   PETSC_TRUE
419 } PetscBool;
420 PETSC_EXTERN const char *const PetscBools[];
421 
422 /*E
423     PetscBool3  - Ternary logical variable. Actually an enum in C and a 4 byte integer in Fortran.
424 
425    Level: beginner
426 
427    Note:
428    Should not be used with the if (flg) or if (!flg) syntax.
429 
430 .seealso: `PETSC_TRUE`, `PETSC_FALSE`, `PetscNot()`, `PETSC_BOOL3_TRUE`, `PETSC_BOOL3_FALSE`, `PETSC_BOOL3_UNKNOWN`
431 E*/
432 typedef enum {
433   PETSC_BOOL3_FALSE,
434   PETSC_BOOL3_TRUE,
435   PETSC_BOOL3_UNKNOWN = -1
436 } PetscBool3;
437 
438 #define PetscBool3ToBool(a) ((a) == PETSC_BOOL3_TRUE ? PETSC_TRUE : PETSC_FALSE)
439 #define PetscBoolToBool3(a) ((a) == PETSC_TRUE ? PETSC_BOOL3_TRUE : PETSC_BOOL3_FALSE)
440 
441 /*MC
442    PetscReal - PETSc type that represents a real number version of `PetscScalar`
443 
444    Notes:
445    For MPI calls that require datatypes, use `MPIU_REAL` as the datatype for `PetscReal` and `MPIU_SUM`, `MPIU_MAX`, etc. for operations.
446           They will automatically work correctly regardless of the size of `PetscReal`.
447 
448           See `PetscScalar` for details on how to ./configure the size of `PetscReal`.
449 
450    Level: beginner
451 
452 .seealso: `PetscScalar`, `PetscComplex`, `PetscInt`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`, `MPIU_INT`
453 M*/
454 
455 #if defined(PETSC_USE_REAL_SINGLE)
456 typedef float PetscReal;
457 #elif defined(PETSC_USE_REAL_DOUBLE)
458 typedef double    PetscReal;
459 #elif defined(PETSC_USE_REAL___FLOAT128)
460   #if defined(__cplusplus)
461 extern "C" {
462   #endif
463   #include <quadmath.h>
464   #if defined(__cplusplus)
465 }
466   #endif
467 typedef __float128 PetscReal;
468 #elif defined(PETSC_USE_REAL___FP16)
469 typedef __fp16 PetscReal;
470 #endif /* PETSC_USE_REAL_* */
471 
472 /*MC
473    PetscComplex - PETSc type that represents a complex number with precision matching that of `PetscReal`.
474 
475    Synopsis:
476    #include <petscsys.h>
477    PetscComplex number = 1. + 2.*PETSC_i;
478 
479    Notes:
480    For MPI calls that require datatypes, use `MPIU_COMPLEX` as the datatype for `PetscComplex` and `MPIU_SUM` etc for operations.
481           They will automatically work correctly regardless of the size of `PetscComplex`.
482 
483           See PetscScalar for details on how to ./configure the size of `PetscReal`
484 
485           Complex numbers are automatically available if PETSc was able to find a working complex implementation
486 
487     Petsc has a 'fix' for complex numbers to support expressions such as std::complex<PetscReal> + `PetscInt`, which are not supported by the standard
488     C++ library, but are convenient for petsc users. If the C++ compiler is able to compile code in petsccxxcomplexfix.h (This is checked by
489     configure), we include petsccxxcomplexfix.h to provide this convenience.
490 
491     If the fix causes conflicts, or one really does not want this fix for a particular C++ file, one can define `PETSC_SKIP_CXX_COMPLEX_FIX`
492     at the beginning of the C++ file to skip the fix.
493 
494    Level: beginner
495 
496 .seealso: `PetscReal`, `PetscScalar`, `PetscComplex`, `PetscInt`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`, `MPIU_INT`, `PETSC_i`
497 M*/
498 #if !defined(PETSC_SKIP_COMPLEX)
499   #if defined(PETSC_CLANGUAGE_CXX)
500     #if !defined(PETSC_USE_REAL___FP16) && !defined(PETSC_USE_REAL___FLOAT128)
501       #if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) /* enable complex for library code */
502         #define PETSC_HAVE_COMPLEX 1
503       #elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX) && defined(PETSC_HAVE_CXX_COMPLEX) /* User code only - conditional on library code complex support */
504         #define PETSC_HAVE_COMPLEX 1
505       #endif
506     #elif defined(PETSC_USE_REAL___FLOAT128) && defined(PETSC_HAVE_C99_COMPLEX)
507       #define PETSC_HAVE_COMPLEX 1
508     #endif
509   #else /* !PETSC_CLANGUAGE_CXX */
510     #if !defined(PETSC_USE_REAL___FP16)
511       #if !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX) /* enable complex for library code */
512         #define PETSC_HAVE_COMPLEX 1
513       #elif defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX) && defined(PETSC_HAVE_CXX_COMPLEX) /* User code only - conditional on library code complex support */
514         #define PETSC_HAVE_COMPLEX 1
515       #endif
516     #endif
517   #endif /* PETSC_CLANGUAGE_CXX */
518 #endif   /* !PETSC_SKIP_COMPLEX */
519 
520 #if defined(PETSC_HAVE_COMPLEX)
521   #if defined(__cplusplus) /* C++ complex support */
522     /* Locate a C++ complex template library */
523     #if defined(PETSC_DESIRE_KOKKOS_COMPLEX) /* Defined in petscvec_kokkos.hpp for *.kokkos.cxx files */
524       #define petsccomplexlib Kokkos
525       #include <Kokkos_Complex.hpp>
526     #elif defined(__CUDACC__) || defined(__HIPCC__)
527       #define petsccomplexlib thrust
528       #include <thrust/complex.h>
529     #elif defined(PETSC_USE_REAL___FLOAT128)
530       #include <complex.h>
531     #else
532       #define petsccomplexlib std
533       #include <complex>
534     #endif
535 
536     /* Define PetscComplex based on the precision */
537     #if defined(PETSC_USE_REAL_SINGLE)
538 typedef petsccomplexlib::complex<float> PetscComplex;
539     #elif defined(PETSC_USE_REAL_DOUBLE)
540 typedef petsccomplexlib::complex<double> PetscComplex;
541     #elif defined(PETSC_USE_REAL___FLOAT128)
542 typedef __complex128 PetscComplex;
543     #endif
544 
545     /* Include a PETSc C++ complex 'fix'. Check PetscComplex manual page for details */
546     #if defined(PETSC_HAVE_CXX_COMPLEX_FIX) && !defined(PETSC_SKIP_CXX_COMPLEX_FIX)
547       #include <petsccxxcomplexfix.h>
548     #endif
549   #else /* c99 complex support */
550     #include <complex.h>
551     #if defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL___FP16)
552 typedef float _Complex PetscComplex;
553     #elif defined(PETSC_USE_REAL_DOUBLE)
554 typedef double _Complex PetscComplex;
555     #elif defined(PETSC_USE_REAL___FLOAT128)
556 typedef __complex128 PetscComplex;
557     #endif /* PETSC_USE_REAL_* */
558   #endif   /* !__cplusplus */
559 #endif     /* PETSC_HAVE_COMPLEX */
560 
561 /*MC
562    PetscScalar - PETSc type that represents either a double precision real number, a double precision
563        complex number, a single precision real number, a __float128 real or complex or a __fp16 real - if the code is configured
564        with --with-scalar-type=real,complex --with-precision=single,double,__float128,__fp16
565 
566    Notes:
567    For MPI calls that require datatypes, use `MPIU_SCALAR` as the datatype for `PetscScalar` and `MPIU_SUM`, etc for operations. They will automatically work correctly regardless of the size of `PetscScalar`.
568 
569    Level: beginner
570 
571 .seealso: `PetscReal`, `PetscComplex`, `PetscInt`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`, `MPIU_INT`, `PetscRealPart()`, `PetscImaginaryPart()`
572 M*/
573 
574 #if defined(PETSC_USE_COMPLEX) && defined(PETSC_HAVE_COMPLEX)
575 typedef PetscComplex PetscScalar;
576 #else  /* PETSC_USE_COMPLEX */
577 typedef PetscReal PetscScalar;
578 #endif /* PETSC_USE_COMPLEX */
579 
580 /*E
581     PetscCopyMode  - Determines how an array or `PetscObject` passed to certain functions is copied or retained by the aggregate `PetscObject`
582 
583    Level: beginner
584 
585    For the array input:
586 $   `PETSC_COPY_VALUES` - the array values are copied into new space, the user is free to reuse or delete the passed in array
587 $   `PETSC_OWN_POINTER` - the array values are NOT copied, the object takes ownership of the array and will free it later, the user cannot change or
588 $                       delete the array. The array MUST have been obtained with PetscMalloc(). Hence this mode cannot be used in Fortran.
589 $   `PETSC_USE_POINTER` - the array values are NOT copied, the object uses the array but does NOT take ownership of the array. The user cannot use
590 $                       the array but the user must delete the array after the object is destroyed.
591 
592    For the PetscObject input:
593 $   `PETSC_COPY_VALUES` - the input `PetscObject` is cloned into the aggregate `PetscObject`; the user is free to reuse/modify the input `PetscObject` without side effects.
594 $   `PETSC_OWN_POINTER` - the input `PetscObject` is referenced by pointer (with reference count), thus should not be modified by the user. (Modification may cause errors or unintended side-effects in this or a future version of PETSc.)
595    For either case above, the input `PetscObject` should be destroyed by the user when no longer needed (the aggregate object increases its reference count).
596 $   `PETSC_USE_POINTER` - invalid for `PetscObject` inputs.
597 
598 E*/
599 typedef enum {
600   PETSC_COPY_VALUES,
601   PETSC_OWN_POINTER,
602   PETSC_USE_POINTER
603 } PetscCopyMode;
604 PETSC_EXTERN const char *const PetscCopyModes[];
605 
606 /*MC
607     PETSC_FALSE - False value of `PetscBool`
608 
609     Level: beginner
610 
611     Note:
612     Zero integer
613 
614 .seealso: `PetscBool`, `PetscBool3`, `PETSC_TRUE`
615 M*/
616 
617 /*MC
618     PETSC_TRUE - True value of `PetscBool`
619 
620     Level: beginner
621 
622     Note:
623     Nonzero integer
624 
625 .seealso: `PetscBool`, `PetscBool3`, `PETSC_FALSE`
626 M*/
627 
628 /*MC
629     PetscLogDouble - Used for logging times
630 
631   Notes:
632   Contains double precision numbers that are not used in the numerical computations, but rather in logging, timing etc.
633 
634   Level: developer
635 
636 M*/
637 typedef double PetscLogDouble;
638 
639 /*E
640     PetscDataType - Used for handling different basic data types.
641 
642    Level: beginner
643 
644    Notes:
645    Use of this should be avoided if one can directly use `MPI_Datatype` instead.
646 
647    `PETSC_INT` is the datatype for a `PetscInt`, regardless of whether it is 4 or 8 bytes.
648    `PETSC_REAL`, `PETSC_COMPLEX` and `PETSC_SCALAR` are the datatypes for `PetscReal`, `PetscComplex` and `PetscScalar`, regardless of their sizes.
649 
650    Developer Notes:
651    It would be nice if we could always just use MPI Datatypes, why can we not?
652 
653    If you change any values in `PetscDatatype` make sure you update their usage in
654    share/petsc/matlab/PetscBagRead.m and share/petsc/matlab/@PetscOpenSocket/read/write.m
655 
656    TODO: Add PETSC_INT32 and remove use of improper PETSC_ENUM
657 
658 .seealso: `PetscBinaryRead()`, `PetscBinaryWrite()`, `PetscDataTypeToMPIDataType()`,
659           `PetscDataTypeGetSize()`
660 
661 E*/
662 typedef enum {
663   PETSC_DATATYPE_UNKNOWN = 0,
664   PETSC_DOUBLE           = 1,
665   PETSC_COMPLEX          = 2,
666   PETSC_LONG             = 3,
667   PETSC_SHORT            = 4,
668   PETSC_FLOAT            = 5,
669   PETSC_CHAR             = 6,
670   PETSC_BIT_LOGICAL      = 7,
671   PETSC_ENUM             = 8,
672   PETSC_BOOL             = 9,
673   PETSC___FLOAT128       = 10,
674   PETSC_OBJECT           = 11,
675   PETSC_FUNCTION         = 12,
676   PETSC_STRING           = 13,
677   PETSC___FP16           = 14,
678   PETSC_STRUCT           = 15,
679   PETSC_INT              = 16,
680   PETSC_INT64            = 17,
681   PETSC_COUNT            = 18
682 } PetscDataType;
683 PETSC_EXTERN const char *const PetscDataTypes[];
684 
685 #if defined(PETSC_USE_REAL_SINGLE)
686   #define PETSC_REAL PETSC_FLOAT
687 #elif defined(PETSC_USE_REAL_DOUBLE)
688   #define PETSC_REAL PETSC_DOUBLE
689 #elif defined(PETSC_USE_REAL___FLOAT128)
690   #define PETSC_REAL PETSC___FLOAT128
691 #elif defined(PETSC_USE_REAL___FP16)
692   #define PETSC_REAL PETSC___FP16
693 #else
694   #define PETSC_REAL PETSC_DOUBLE
695 #endif
696 
697 #if defined(PETSC_USE_COMPLEX)
698   #define PETSC_SCALAR PETSC_COMPLEX
699 #else
700   #define PETSC_SCALAR PETSC_REAL
701 #endif
702 
703 #define PETSC_FORTRANADDR PETSC_LONG
704 
705 /*S
706     PetscToken - 'Token' used for managing tokenizing strings
707 
708   Level: intermediate
709 
710 .seealso: `PetscTokenCreate()`, `PetscTokenFind()`, `PetscTokenDestroy()`
711 S*/
712 typedef struct _p_PetscToken *PetscToken;
713 
714 /*S
715      PetscObject - any PETSc object, `PetscViewer`, `Mat`, `Vec`, `KSP` etc
716 
717    Level: beginner
718 
719    Notes:
720    This is the base class from which all PETSc objects are derived from.
721 
722    In certain situations one can cast an object, for example a `Vec`, to a `PetscObject` with (`PetscObject`)vec
723 
724 .seealso: `PetscObjectDestroy()`, `PetscObjectView()`, `PetscObjectGetName()`, `PetscObjectSetName()`, `PetscObjectReference()`, `PetscObjectDereference()`
725 S*/
726 typedef struct _p_PetscObject *PetscObject;
727 
728 /*MC
729     PetscObjectId - unique integer Id for a `PetscObject`
730 
731     Level: developer
732 
733     Note:
734     Unlike pointer values, object ids are never reused so one may save a `PetscObjectId` and compare it to one obtained later from a `PetscObject` to determine
735     if the objects are the same. Never compare two object pointer values.
736 
737 .seealso: `PetscObjectState`, `PetscObjectGetId()`
738 M*/
739 typedef PetscInt64 PetscObjectId;
740 
741 /*MC
742     PetscObjectState - integer state for a `PetscObject`
743 
744     Level: developer
745 
746     Notes:
747     Object state is always-increasing and (for objects that track state) can be used to determine if an object has
748     changed since the last time you interacted with it.  It is 64-bit so that it will not overflow for a very long time.
749 
750 .seealso: `PetscObjectId`, `PetscObjectStateGet()`, `PetscObjectStateIncrease()`, `PetscObjectStateSet()`
751 M*/
752 typedef PetscInt64 PetscObjectState;
753 
754 /*S
755      PetscFunctionList - Linked list of functions, possibly stored in dynamic libraries, accessed
756       by string name
757 
758    Level: advanced
759 
760 .seealso: `PetscFunctionListAdd()`, `PetscFunctionListDestroy()`
761 S*/
762 typedef struct _n_PetscFunctionList *PetscFunctionList;
763 
764 /*E
765   PetscFileMode - Access mode for a file.
766 
767   Level: beginner
768 
769 $  `FILE_MODE_UNDEFINED` - initial invalid value
770 $  `FILE_MODE_READ` - open a file at its beginning for reading
771 $  `FILE_MODE_WRITE` - open a file at its beginning for writing (will create if the file does not exist)
772 $  `FILE_MODE_APPEND` - open a file at end for writing
773 $  `FILE_MODE_UPDATE` - open a file for updating, meaning for reading and writing
774 $  `FILE_MODE_APPEND_UPDATE` - open a file for updating, meaning for reading and writing, at the end
775 
776 .seealso: `PetscViewerFileSetMode()`
777 E*/
778 typedef enum {
779   FILE_MODE_UNDEFINED = -1,
780   FILE_MODE_READ      = 0,
781   FILE_MODE_WRITE,
782   FILE_MODE_APPEND,
783   FILE_MODE_UPDATE,
784   FILE_MODE_APPEND_UPDATE
785 } PetscFileMode;
786 PETSC_EXTERN const char *const PetscFileModes[];
787 
788 typedef void *PetscDLHandle;
789 typedef enum {
790   PETSC_DL_DECIDE = 0,
791   PETSC_DL_NOW    = 1,
792   PETSC_DL_LOCAL  = 2
793 } PetscDLMode;
794 
795 /*S
796      PetscObjectList - Linked list of PETSc objects, each accessible by string name
797 
798    Level: developer
799 
800    Note:
801    Used by `PetscObjectCompose()` and `PetscObjectQuery()`
802 
803 .seealso: `PetscObjectListAdd()`, `PetscObjectListDestroy()`, `PetscObjectListFind()`, `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscFunctionList`
804 S*/
805 typedef struct _n_PetscObjectList *PetscObjectList;
806 
807 /*S
808      PetscDLLibrary - Linked list of dynamics libraries to search for functions
809 
810    Level: advanced
811 
812 .seealso: `PetscDLLibraryOpen()`
813 S*/
814 typedef struct _n_PetscDLLibrary *PetscDLLibrary;
815 
816 /*S
817      PetscContainer - Simple PETSc object that contains a pointer to any required data
818 
819    Level: advanced
820 
821    Note:
822    This is useful to attach arbitrary data to a `PetscObject` with `PetscObjectCompose()` and `PetscObjectQuery()`
823 
824 .seealso: `PetscObject`, `PetscContainerCreate()`, `PetscObjectCompose()`, `PetscObjectQuery()`
825 S*/
826 typedef struct _p_PetscContainer *PetscContainer;
827 
828 /*S
829      PetscRandom - Abstract PETSc object that manages generating random numbers
830 
831    Level: intermediate
832 
833 .seealso: `PetscRandomCreate()`, `PetscRandomGetValue()`, `PetscRandomType`
834 S*/
835 typedef struct _p_PetscRandom *PetscRandom;
836 
837 /*
838    In binary files variables are stored using the following lengths,
839   regardless of how they are stored in memory on any one particular
840   machine. Use these rather then sizeof() in computing sizes for
841   PetscBinarySeek().
842 */
843 #define PETSC_BINARY_INT_SIZE    (32 / 8)
844 #define PETSC_BINARY_FLOAT_SIZE  (32 / 8)
845 #define PETSC_BINARY_CHAR_SIZE   (8 / 8)
846 #define PETSC_BINARY_SHORT_SIZE  (16 / 8)
847 #define PETSC_BINARY_DOUBLE_SIZE (64 / 8)
848 #define PETSC_BINARY_SCALAR_SIZE sizeof(PetscScalar)
849 
850 /*E
851   PetscBinarySeekType - argument to `PetscBinarySeek()`
852 
853   Level: advanced
854 
855 .seealso: `PetscBinarySeek()`, `PetscBinarySynchronizedSeek()`
856 E*/
857 typedef enum {
858   PETSC_BINARY_SEEK_SET = 0,
859   PETSC_BINARY_SEEK_CUR = 1,
860   PETSC_BINARY_SEEK_END = 2
861 } PetscBinarySeekType;
862 
863 /*E
864     PetscBuildTwoSidedType - algorithm for setting up two-sided communication
865 
866 $  `PETSC_BUILDTWOSIDED_ALLREDUCE` - classical algorithm using an MPI_Allreduce with
867 $      a buffer of length equal to the communicator size. Not memory-scalable due to
868 $      the large reduction size. Requires only MPI-1.
869 $  `PETSC_BUILDTWOSIDED_IBARRIER` - nonblocking algorithm based on MPI_Issend and MPI_Ibarrier.
870 $      Proved communication-optimal in Hoefler, Siebert, and Lumsdaine (2010). Requires MPI-3.
871 $  `PETSC_BUILDTWOSIDED_REDSCATTER` - similar to above, but use more optimized function
872 $      that only communicates the part of the reduction that is necessary.  Requires MPI-2.
873 
874    Level: developer
875 
876 .seealso: `PetscCommBuildTwoSided()`, `PetscCommBuildTwoSidedSetType()`, `PetscCommBuildTwoSidedGetType()`
877 E*/
878 typedef enum {
879   PETSC_BUILDTWOSIDED_NOTSET     = -1,
880   PETSC_BUILDTWOSIDED_ALLREDUCE  = 0,
881   PETSC_BUILDTWOSIDED_IBARRIER   = 1,
882   PETSC_BUILDTWOSIDED_REDSCATTER = 2
883   /* Updates here must be accompanied by updates in finclude/petscsys.h and the string array in mpits.c */
884 } PetscBuildTwoSidedType;
885 PETSC_EXTERN const char *const PetscBuildTwoSidedTypes[];
886 
887 /* NOTE: If you change this, you must also change the values in src/vec/f90-mod/petscvec.h */
888 /*E
889   InsertMode - Whether entries are inserted or added into vectors or matrices
890 
891   Level: beginner
892 
893 .seealso: `VecSetValues()`, `MatSetValues()`, `VecSetValue()`, `VecSetValuesBlocked()`,
894           `VecSetValuesLocal()`, `VecSetValuesBlockedLocal()`, `MatSetValuesBlocked()`,
895           `MatSetValuesBlockedLocal()`, `MatSetValuesLocal()`, `VecScatterBegin()`, `VecScatterEnd()`
896 E*/
897 typedef enum {
898   NOT_SET_VALUES,
899   INSERT_VALUES,
900   ADD_VALUES,
901   MAX_VALUES,
902   MIN_VALUES,
903   INSERT_ALL_VALUES,
904   ADD_ALL_VALUES,
905   INSERT_BC_VALUES,
906   ADD_BC_VALUES
907 } InsertMode;
908 
909 /*MC
910     INSERT_VALUES - Put a value into a vector or matrix, overwrites any previous value
911 
912     Level: beginner
913 
914 .seealso: `InsertMode`, `VecSetValues()`, `MatSetValues()`, `VecSetValue()`, `VecSetValuesBlocked()`,
915           `VecSetValuesLocal()`, `VecSetValuesBlockedLocal()`, `MatSetValuesBlocked()`, `ADD_VALUES`,
916           `MatSetValuesBlockedLocal()`, `MatSetValuesLocal()`, `VecScatterBegin()`, `VecScatterEnd()`, `MAX_VALUES`
917 
918 M*/
919 
920 /*MC
921     ADD_VALUES - Adds a value into a vector or matrix, if there previously was no value, just puts the
922                 value into that location
923 
924     Level: beginner
925 
926 .seealso: `InsertMode`, `VecSetValues()`, `MatSetValues()`, `VecSetValue()`, `VecSetValuesBlocked()`,
927           `VecSetValuesLocal()`, `VecSetValuesBlockedLocal()`, `MatSetValuesBlocked()`, `INSERT_VALUES`,
928           `MatSetValuesBlockedLocal()`, `MatSetValuesLocal()`, `VecScatterBegin()`, `VecScatterEnd()`, `MAX_VALUES`
929 
930 M*/
931 
932 /*MC
933     MAX_VALUES - Puts the maximum of the scattered/gathered value and the current value into each location
934 
935     Level: beginner
936 
937 .seealso: `InsertMode`, `VecScatterBegin()`, `VecScatterEnd()`, `ADD_VALUES`, `INSERT_VALUES`
938 
939 M*/
940 
941 /*MC
942     MIN_VALUES - Puts the minimal of the scattered/gathered value and the current value into each location
943 
944     Level: beginner
945 
946 .seealso: `InsertMode`, `VecScatterBegin()`, `VecScatterEnd()`, `ADD_VALUES`, `INSERT_VALUES`
947 
948 M*/
949 
950 /*S
951    PetscSubcomm - A decomposition of an MPI communicator into subcommunicators
952 
953    Notes:
954    After a call to `PetscSubcommSetType()`, `PetscSubcommSetTypeGeneral()`, or `PetscSubcommSetFromOptions()` one may call
955 $     `PetscSubcommChild()` returns the associated subcommunicator on this process
956 $     `PetscSubcommContiguousParent()` returns a parent communitor but with all child of the same subcommunicator having contiguous rank
957 
958    Sample Usage:
959 .vb
960        `PetscSubcommCreate()`
961        `PetscSubcommSetNumber()`
962        `PetscSubcommSetType`(`PETSC_SUBCOMM_INTERLACED`);
963        ccomm = `PetscSubcommChild()`
964        `PetscSubcommDestroy()`
965 .ve
966 
967    Level: advanced
968 
969    Notes:
970 $   `PETSC_SUBCOMM_GENERAL` - similar to `MPI_Comm_split()` each process sets the new communicator (color) they will belong to and the order within that communicator
971 $   `PETSC_SUBCOMM_CONTIGUOUS` - each new communicator contains a set of process with contiguous ranks in the original MPI communicator
972 $   `PETSC_SUBCOMM_INTERLACED` - each new communictor contains a set of processes equally far apart in rank from the others in that new communicator
973 
974    Example: Consider a communicator with six processes split into 3 subcommunicators.
975 $     `PETSC_SUBCOMM_CONTIGUOUS` - the first communicator contains rank 0,1  the second rank 2,3 and the third rank 4,5 in the original ordering of the original communicator
976 $     `PETSC_SUBCOMM_INTERLACED` - the first communicator contains rank 0,3, the second 1,4 and the third 2,5
977 
978    Developer Note:
979    This is used in objects such as `PCREDUNDANT` to manage the subcommunicators on which the redundant computations
980    are performed.
981 
982 .seealso: `PetscSubcommCreate()`, `PetscSubcommSetNumber()`, `PetscSubcommSetType()`, `PetscSubcommView()`, `PetscSubcommSetFromOptions()`
983 
984 S*/
985 typedef struct _n_PetscSubcomm *PetscSubcomm;
986 typedef enum {
987   PETSC_SUBCOMM_GENERAL    = 0,
988   PETSC_SUBCOMM_CONTIGUOUS = 1,
989   PETSC_SUBCOMM_INTERLACED = 2
990 } PetscSubcommType;
991 PETSC_EXTERN const char *const PetscSubcommTypes[];
992 
993 /*S
994      PetscHeap - A simple class for managing heaps
995 
996    Level: intermediate
997 
998 .seealso: `PetscHeapCreate()`, `PetscHeapAdd()`, `PetscHeapPop()`, `PetscHeapPeek()`, `PetscHeapStash()`, `PetscHeapUnstash()`, `PetscHeapView()`, `PetscHeapDestroy()`
999 S*/
1000 typedef struct _PetscHeap *PetscHeap;
1001 
1002 typedef struct _n_PetscShmComm *PetscShmComm;
1003 typedef struct _n_PetscOmpCtrl *PetscOmpCtrl;
1004 
1005 /*S
1006    PetscSegBuffer - a segmented extendable buffer
1007 
1008    Level: developer
1009 
1010 .seealso: `PetscSegBufferCreate()`, `PetscSegBufferGet()`, `PetscSegBufferExtract()`, `PetscSegBufferDestroy()`
1011 S*/
1012 typedef struct _n_PetscSegBuffer *PetscSegBuffer;
1013 
1014 typedef struct _n_PetscOptionsHelpPrinted *PetscOptionsHelpPrinted;
1015 #endif
1016