xref: /petsc/include/petscsys.h (revision c69effb2e05caa58810396c101d616d7ad9da0a7)
1d382aafbSBarry Smith /*
2d382aafbSBarry Smith    This is the main PETSc include file (for C and C++).  It is included by all
3d382aafbSBarry Smith    other PETSc include files, so it almost never has to be specifically included.
447d993e7Ssuyashtn    Portions of this code are under:
547d993e7Ssuyashtn    Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved.
6d382aafbSBarry Smith */
76524c165SJacob Faibussowitsch #ifndef PETSCSYS_H
826bd1501SBarry Smith #define PETSCSYS_H
93ca90d2dSJacob Faibussowitsch 
10d382aafbSBarry Smith /* ========================================================================== */
11d382aafbSBarry Smith /*
12d382aafbSBarry Smith    petscconf.h is contained in ${PETSC_ARCH}/include/petscconf.h it is
131c77a0c5SBarry Smith    found automatically by the compiler due to the -I${PETSC_DIR}/${PETSC_ARCH}/include that
141c77a0c5SBarry Smith    PETSc's makefiles add to the compiler rules.
151c77a0c5SBarry Smith    For --prefix installs the directory ${PETSC_ARCH} does not exist and petscconf.h is in the same
161cc8b206SBarry Smith    directory as the other PETSc include files.
17d382aafbSBarry Smith */
182c8e378dSBarry Smith #include <petscconf.h>
191c77a0c5SBarry Smith #include <petscconf_poison.h>
202c8e378dSBarry Smith #include <petscfix.h>
21bde483f2SJacob Faibussowitsch #include <petscmacros.h>
2233bb201bSJacob Faibussowitsch 
23ac09b921SBarry Smith /* SUBMANSEC = Sys */
24ac09b921SBarry Smith 
2573fca5a0SBarry Smith #if defined(PETSC_DESIRE_FEATURE_TEST_MACROS)
2673fca5a0SBarry Smith   /*
2773fca5a0SBarry Smith    Feature test macros must be included before headers defined by IEEE Std 1003.1-2001
2873fca5a0SBarry Smith    We only turn these in PETSc source files that require them by setting PETSC_DESIRE_FEATURE_TEST_MACROS
2973fca5a0SBarry Smith */
30216f1ae6SBarry Smith   #if defined(PETSC__POSIX_C_SOURCE_200112L) && !defined(_POSIX_C_SOURCE)
3173fca5a0SBarry Smith     #define _POSIX_C_SOURCE 200112L
3273fca5a0SBarry Smith   #endif
33216f1ae6SBarry Smith   #if defined(PETSC__BSD_SOURCE) && !defined(_BSD_SOURCE)
3473fca5a0SBarry Smith     #define _BSD_SOURCE
3573fca5a0SBarry Smith   #endif
3607f00807SSatish Balay   #if defined(PETSC__DEFAULT_SOURCE) && !defined(_DEFAULT_SOURCE)
3707f00807SSatish Balay     #define _DEFAULT_SOURCE
3807f00807SSatish Balay   #endif
39ea0fecefSShri Abhyankar   #if defined(PETSC__GNU_SOURCE) && !defined(_GNU_SOURCE)
40ea0fecefSShri Abhyankar     #define _GNU_SOURCE
41ea0fecefSShri Abhyankar   #endif
4273fca5a0SBarry Smith #endif
4373fca5a0SBarry Smith 
44df4397b0SStefano Zampini #include <petscsystypes.h>
45df4397b0SStefano Zampini 
46d382aafbSBarry Smith /* ========================================================================== */
47d382aafbSBarry Smith 
48d382aafbSBarry Smith /*
49d382aafbSBarry Smith     Defines the interface to MPI allowing the use of all MPI functions.
50d382aafbSBarry Smith 
51d382aafbSBarry Smith     PETSc does not use the C++ binding of MPI at ALL. The following flag
52d382aafbSBarry Smith     makes sure the C++ bindings are not included. The C++ bindings REQUIRE
53d382aafbSBarry Smith     putting mpi.h before ANY C++ include files, we cannot control this
54d382aafbSBarry Smith     with all PETSc users. Users who want to use the MPI C++ bindings can include
55d382aafbSBarry Smith     mpicxx.h directly in their code
56d382aafbSBarry Smith */
57fa9e63e2SJed Brown #if !defined(MPICH_SKIP_MPICXX)
58d382aafbSBarry Smith   #define MPICH_SKIP_MPICXX 1
59fa9e63e2SJed Brown #endif
60fa9e63e2SJed Brown #if !defined(OMPI_SKIP_MPICXX)
61d382aafbSBarry Smith   #define OMPI_SKIP_MPICXX 1
62fa9e63e2SJed Brown #endif
63e771154cSJed Brown #if defined(PETSC_HAVE_MPIUNI)
64e771154cSJed Brown   #include <petsc/mpiuni/mpi.h>
65e771154cSJed Brown #else
662c8e378dSBarry Smith   #include <mpi.h>
67e771154cSJed Brown #endif
6865013866SBarry Smith 
69d382aafbSBarry Smith /*
70a7886beaSBarry Smith    Perform various sanity checks that the correct mpi.h is being included at compile time.
71a7886beaSBarry Smith    This usually happens because
72a7886beaSBarry Smith       * either an unexpected mpi.h is in the default compiler path (i.e. in /usr/include) or
73a7886beaSBarry Smith       * an extra include path -I/something (which contains the unexpected mpi.h) is being passed to the compiler
74a7886beaSBarry Smith */
75a7886beaSBarry Smith #if defined(PETSC_HAVE_MPIUNI)
766524c165SJacob Faibussowitsch   #ifndef MPIUNI_H
77a7886beaSBarry Smith     #error "PETSc was configured with --with-mpi=0 but now appears to be compiling using a different mpi.h"
78a7886beaSBarry Smith   #endif
7941f4af4cSSatish Balay #elif defined(PETSC_HAVE_I_MPI_NUMVERSION)
8041f4af4cSSatish Balay   #if !defined(I_MPI_NUMVERSION)
8141f4af4cSSatish Balay     #error "PETSc was configured with I_MPI but now appears to be compiling using a non-I_MPI mpi.h"
8241f4af4cSSatish Balay   #elif I_MPI_NUMVERSION != PETSC_HAVE_I_MPI_NUMVERSION
8341f4af4cSSatish Balay     #error "PETSc was configured with one I_MPI mpi.h version but now appears to be compiling using a different I_MPI mpi.h version"
8441f4af4cSSatish Balay   #endif
8541f4af4cSSatish Balay #elif defined(PETSC_HAVE_MVAPICH2_NUMVERSION)
8641f4af4cSSatish Balay   #if !defined(MVAPICH2_NUMVERSION)
8741f4af4cSSatish Balay     #error "PETSc was configured with MVAPICH2 but now appears to be compiling using a non-MVAPICH2 mpi.h"
8841f4af4cSSatish Balay   #elif MVAPICH2_NUMVERSION != PETSC_HAVE_MVAPICH2_NUMVERSION
8941f4af4cSSatish Balay     #error "PETSc was configured with one MVAPICH2 mpi.h version but now appears to be compiling using a different MVAPICH2 mpi.h version"
9041f4af4cSSatish Balay   #endif
91a7886beaSBarry Smith #elif defined(PETSC_HAVE_MPICH_NUMVERSION)
9241f4af4cSSatish Balay   #if !defined(MPICH_NUMVERSION) || defined(MVAPICH2_NUMVERSION) || defined(I_MPI_NUMVERSION)
93a7886beaSBarry Smith     #error "PETSc was configured with MPICH but now appears to be compiling using a non-MPICH mpi.h"
941c284764SMin RK   #elif (MPICH_NUMVERSION / 100000000 != PETSC_HAVE_MPICH_NUMVERSION / 100000000) || (MPICH_NUMVERSION / 100000 < PETSC_HAVE_MPICH_NUMVERSION / 100000) || (MPICH_NUMVERSION / 100000 == PETSC_HAVE_MPICH_NUMVERSION / 100000 && MPICH_NUMVERSION % 100000 / 1000 < PETSC_HAVE_MPICH_NUMVERSION % 100000 / 1000)
95a7886beaSBarry Smith     #error "PETSc was configured with one MPICH mpi.h version but now appears to be compiling using a different MPICH mpi.h version"
96a7886beaSBarry Smith   #endif
97a7886beaSBarry Smith #elif defined(PETSC_HAVE_OMPI_MAJOR_VERSION)
98a7886beaSBarry Smith   #if !defined(OMPI_MAJOR_VERSION)
99a7886beaSBarry Smith     #error "PETSc was configured with OpenMPI but now appears to be compiling using a non-OpenMPI mpi.h"
1001c284764SMin RK   #elif (OMPI_MAJOR_VERSION != PETSC_HAVE_OMPI_MAJOR_VERSION) || (OMPI_MINOR_VERSION < PETSC_HAVE_OMPI_MINOR_VERSION) || (OMPI_MINOR_VERSION == PETSC_HAVE_OMPI_MINOR_VERSION && OMPI_RELEASE_VERSION < PETSC_HAVE_OMPI_RELEASE_VERSION)
101c3ac57cfSSatish Balay     #error "PETSc was configured with one OpenMPI mpi.h version but now appears to be compiling using a different OpenMPI mpi.h version"
102a7886beaSBarry Smith   #endif
103d97fd468SStefano Zampini #elif defined(PETSC_HAVE_MSMPI_VERSION)
104d97fd468SStefano Zampini   #if !defined(MSMPI_VER)
105d97fd468SStefano Zampini     #error "PETSc was configured with MSMPI but now appears to be compiling using a non-MSMPI mpi.h"
106d97fd468SStefano Zampini   #elif (MSMPI_VER != PETSC_HAVE_MSMPI_VERSION)
107d97fd468SStefano Zampini     #error "PETSc was configured with one MSMPI mpi.h version but now appears to be compiling using a different MSMPI mpi.h version"
108d97fd468SStefano Zampini   #endif
109d97fd468SStefano Zampini #elif defined(OMPI_MAJOR_VERSION) || defined(MPICH_NUMVERSION) || defined(MSMPI_VER)
110d97fd468SStefano Zampini   #error "PETSc was configured with undetermined MPI - but now appears to be compiling using any of OpenMPI, MS-MPI or a MPICH variant"
111a7886beaSBarry Smith #endif
112a7886beaSBarry Smith 
113a7886beaSBarry Smith /*
1142981ebdbSBarry Smith     Need to put stdio.h AFTER mpi.h for MPICH2 with C++ compiler
115d382aafbSBarry Smith     see the top of mpicxx.h in the MPICH2 distribution.
116d382aafbSBarry Smith */
117d382aafbSBarry Smith #include <stdio.h>
118d382aafbSBarry Smith 
119d382aafbSBarry Smith /* MSMPI on 32bit windows requires this yukky hack - that breaks MPI standard compliance */
120d382aafbSBarry Smith #if !defined(MPIAPI)
121d382aafbSBarry Smith   #define MPIAPI
122d382aafbSBarry Smith #endif
123d382aafbSBarry Smith 
12493d501b3SJacob Faibussowitsch PETSC_EXTERN MPI_Datatype MPIU_ENUM PETSC_ATTRIBUTE_MPI_TYPE_TAG(PetscEnum);
12593d501b3SJacob Faibussowitsch PETSC_EXTERN MPI_Datatype MPIU_BOOL PETSC_ATTRIBUTE_MPI_TYPE_TAG(PetscBool);
126e28ce29aSPatrick Sanan 
127e28ce29aSPatrick Sanan /*MC
12887497f52SBarry Smith    MPIU_INT - Portable MPI datatype corresponding to `PetscInt` independent of the precision of `PetscInt`
129e28ce29aSPatrick Sanan 
130667f096bSBarry Smith    Level: beginner
131667f096bSBarry Smith 
132e28ce29aSPatrick Sanan    Notes:
13387497f52SBarry Smith    In MPI calls that require an MPI datatype that matches a `PetscInt` or array of `PetscInt` values, pass this value.
134e28ce29aSPatrick Sanan 
135db781477SPatrick Sanan .seealso: `PetscReal`, `PetscScalar`, `PetscComplex`, `PetscInt`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`
136e28ce29aSPatrick Sanan M*/
137e28ce29aSPatrick Sanan 
1387cdaf61dSJed Brown PETSC_EXTERN MPI_Datatype MPIU_FORTRANADDR;
139b7b8f77aSBarry Smith 
140d382aafbSBarry Smith #if defined(PETSC_USE_64BIT_INDICES)
141bd84f1cfSSatish Balay   #define MPIU_INT MPIU_INT64
142d382aafbSBarry Smith #else
143d382aafbSBarry Smith   #define MPIU_INT MPI_INT
144d382aafbSBarry Smith #endif
145d382aafbSBarry Smith 
146503cfb0cSBarry Smith /*
147503cfb0cSBarry Smith     For the rare cases when one needs to send a size_t object with MPI
148503cfb0cSBarry Smith */
14993d501b3SJacob Faibussowitsch PETSC_EXTERN MPI_Datatype MPIU_SIZE_T PETSC_ATTRIBUTE_MPI_TYPE_TAG(size_t);
150d382aafbSBarry Smith 
151d382aafbSBarry Smith /*
152d382aafbSBarry Smith       You can use PETSC_STDOUT as a replacement of stdout. You can also change
153d382aafbSBarry Smith     the value of PETSC_STDOUT to redirect all standard output elsewhere
154d382aafbSBarry Smith */
155014dd563SJed Brown PETSC_EXTERN FILE *PETSC_STDOUT;
156d382aafbSBarry Smith 
157d382aafbSBarry Smith /*
158d382aafbSBarry Smith       You can use PETSC_STDERR as a replacement of stderr. You can also change
159d382aafbSBarry Smith     the value of PETSC_STDERR to redirect all standard error elsewhere
160d382aafbSBarry Smith */
161014dd563SJed Brown PETSC_EXTERN FILE *PETSC_STDERR;
162d382aafbSBarry Smith 
163c8b9133aSSatish Balay /* PetscPragmaSIMD - from CeedPragmaSIMD */
164c8b9133aSSatish Balay 
1655c993f7fSStefano Zampini #if defined(__NEC__)
1665c993f7fSStefano Zampini   #define PetscPragmaSIMD _Pragma("_NEC ivdep")
1675c993f7fSStefano Zampini #elif defined(__INTEL_COMPILER) && !defined(_WIN32)
168c8b9133aSSatish Balay   #define PetscPragmaSIMD _Pragma("vector")
1691dba8834SJose E. Roman #elif defined(__GNUC__) && __GNUC__ >= 5 && !defined(__PGI)
170c8b9133aSSatish Balay   #define PetscPragmaSIMD _Pragma("GCC ivdep")
171e5518150SSatish Balay #elif defined(_OPENMP) && _OPENMP >= 201307
172e5518150SSatish Balay   #if defined(_MSC_VER)
173160a5775SAli Reza Khaz'ali     #define PetscPragmaSIMD __pragma(omp simd)
174e5518150SSatish Balay   #else
175e5518150SSatish Balay     #define PetscPragmaSIMD _Pragma("omp simd")
176e5518150SSatish Balay   #endif
177c8b9133aSSatish Balay #elif defined(PETSC_HAVE_CRAY_VECTOR)
178c8b9133aSSatish Balay   #define PetscPragmaSIMD _Pragma("_CRI ivdep")
179c8b9133aSSatish Balay #else
180c8b9133aSSatish Balay   #define PetscPragmaSIMD
181c8b9133aSSatish Balay #endif
182c8b9133aSSatish Balay 
183d382aafbSBarry Smith /*
184dc07da0dSStefano Zampini   Handle inclusion when using clang compiler with CUDA support
185dc07da0dSStefano Zampini   __float128 is not available for the device
186dc07da0dSStefano Zampini */
187dc07da0dSStefano Zampini #if defined(__clang__) && defined(__CUDA_ARCH__)
188dc07da0dSStefano Zampini   #define PETSC_SKIP_REAL___FLOAT128
189dc07da0dSStefano Zampini #endif
190dc07da0dSStefano Zampini 
191dc07da0dSStefano Zampini /*
192d382aafbSBarry Smith     Declare extern C stuff after including external header files
193d382aafbSBarry Smith */
194d382aafbSBarry Smith 
1956edef35eSSatish Balay PETSC_EXTERN PetscBool PETSC_RUNNING_ON_VALGRIND;
1968b49ba18SBarry Smith /*
197390e1bf2SBarry Smith     Defines elementary mathematics functions and constants.
1988b49ba18SBarry Smith */
1998b49ba18SBarry Smith #include <petscmath.h>
2008b49ba18SBarry Smith 
201d382aafbSBarry Smith /*MC
202667f096bSBarry Smith     PETSC_IGNORE - same as `NULL`, means PETSc will ignore this argument
203d382aafbSBarry Smith 
204d382aafbSBarry Smith    Level: beginner
205d382aafbSBarry Smith 
206e28ce29aSPatrick Sanan    Note:
207667f096bSBarry Smith    Accepted by many PETSc functions to not set a parameter and instead use a default value
208d382aafbSBarry Smith 
20987497f52SBarry Smith    Fortran Note:
210667f096bSBarry Smith    Use `PETSC_NULL_INTEGER`, `PETSC_NULL_DOUBLE_PRECISION` etc
211d382aafbSBarry Smith 
212db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_DEFAULT`, `PETSC_DETERMINE`
213d382aafbSBarry Smith M*/
214f3fa974cSJacob Faibussowitsch #define PETSC_IGNORE PETSC_NULLPTR
215f3fa974cSJacob Faibussowitsch #define PETSC_NULL   PETSC_DEPRECATED_MACRO("GCC warning \"PETSC_NULL is deprecated, use PETSC_NULLPTR instead (since version 3.19)\"") PETSC_NULLPTR
216c528d872SBarry Smith 
217d382aafbSBarry Smith /*MC
218557d4da8SBarry Smith     PETSC_DECIDE - standard way of passing in integer or floating point parameter
219557d4da8SBarry Smith        where you wish PETSc to use the default.
220557d4da8SBarry Smith 
221557d4da8SBarry Smith    Level: beginner
222557d4da8SBarry Smith 
223db781477SPatrick Sanan .seealso: `PETSC_DEFAULT`, `PETSC_IGNORE`, `PETSC_DETERMINE`
224557d4da8SBarry Smith M*/
225557d4da8SBarry Smith 
226557d4da8SBarry Smith /*MC
227d382aafbSBarry Smith     PETSC_DETERMINE - standard way of passing in integer or floating point parameter
228d382aafbSBarry Smith        where you wish PETSc to compute the required value.
229d382aafbSBarry Smith 
230d382aafbSBarry Smith    Level: beginner
231d382aafbSBarry Smith 
232e28ce29aSPatrick Sanan    Developer Note:
23387497f52SBarry Smith    I would like to use const `PetscInt` `PETSC_DETERMINE` = `PETSC_DECIDE`; but for
23487497f52SBarry Smith      some reason this is not allowed by the standard even though `PETSC_DECIDE` is a constant value.
235557d4da8SBarry Smith 
236db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_DEFAULT`, `PETSC_IGNORE`, `VecSetSizes()`
237d382aafbSBarry Smith M*/
238d382aafbSBarry Smith 
239d382aafbSBarry Smith /*MC
240557d4da8SBarry Smith     PETSC_DEFAULT - standard way of passing in integer or floating point parameter
241557d4da8SBarry Smith        where you wish PETSc to use the default.
242557d4da8SBarry Smith 
243557d4da8SBarry Smith    Level: beginner
244557d4da8SBarry Smith 
24587497f52SBarry Smith    Fortran Note:
24687497f52SBarry Smith    You need to use `PETSC_DEFAULT_INTEGER` or `PETSC_DEFAULT_REAL`.
247557d4da8SBarry Smith 
248db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_IGNORE`, `PETSC_DETERMINE`
249557d4da8SBarry Smith M*/
250f3fa974cSJacob Faibussowitsch enum {
251f3fa974cSJacob Faibussowitsch   PETSC_DECIDE    = -1,
252f3fa974cSJacob Faibussowitsch   PETSC_DETERMINE = PETSC_DECIDE,
253f3fa974cSJacob Faibussowitsch   PETSC_DEFAULT   = -2
254f3fa974cSJacob Faibussowitsch };
255557d4da8SBarry Smith 
256557d4da8SBarry Smith /*MC
25787497f52SBarry Smith     PETSC_COMM_WORLD - the equivalent of the `MPI_COMM_WORLD` communicator which represents
258ea8fe74dSBarry Smith            all the processes that PETSc knows about.
259d382aafbSBarry Smith 
260d382aafbSBarry Smith    Level: beginner
261d382aafbSBarry Smith 
262e28ce29aSPatrick Sanan    Notes:
26387497f52SBarry Smith    By default `PETSC_COMM_WORLD` and `MPI_COMM_WORLD` are identical unless you wish to
26487497f52SBarry Smith           run PETSc on ONLY a subset of `MPI_COMM_WORLD`. In that case create your new (smaller)
26587497f52SBarry Smith           communicator, call it, say comm, and set `PETSC_COMM_WORLD` = comm BEFORE calling
26687497f52SBarry Smith           PetscInitialize(), but after `MPI_Init()` has been called.
267a990b902SBarry Smith 
26887497f52SBarry Smith           The value of `PETSC_COMM_WORLD` should never be USED/accessed before `PetscInitialize()`
269a990b902SBarry Smith           is called because it may not have a valid value yet.
270d382aafbSBarry Smith 
271db781477SPatrick Sanan .seealso: `PETSC_COMM_SELF`
272d382aafbSBarry Smith M*/
273014dd563SJed Brown PETSC_EXTERN MPI_Comm PETSC_COMM_WORLD;
274d382aafbSBarry Smith 
275d382aafbSBarry Smith /*MC
27687497f52SBarry Smith     PETSC_COMM_SELF - This is always `MPI_COMM_SELF`
277d382aafbSBarry Smith 
278d382aafbSBarry Smith    Level: beginner
279d382aafbSBarry Smith 
280e28ce29aSPatrick Sanan    Notes:
281e28ce29aSPatrick Sanan    Do not USE/access or set this variable before PetscInitialize() has been called.
282a990b902SBarry Smith 
283db781477SPatrick Sanan .seealso: `PETSC_COMM_WORLD`
284d382aafbSBarry Smith M*/
285d382aafbSBarry Smith #define PETSC_COMM_SELF MPI_COMM_SELF
286d382aafbSBarry Smith 
2876de5d289SStefano Zampini /*MC
2886de5d289SStefano Zampini     PETSC_MPI_THREAD_REQUIRED - the required threading support used if PETSc initializes
28987497f52SBarry Smith            MPI with `MPI_Init_thread()`.
2906de5d289SStefano Zampini 
2916de5d289SStefano Zampini    Level: beginner
2926de5d289SStefano Zampini 
2936de5d289SStefano Zampini    Notes:
29487497f52SBarry Smith    By default `PETSC_MPI_THREAD_REQUIRED` equals `MPI_THREAD_FUNNELED`.
2956de5d289SStefano Zampini 
296db781477SPatrick Sanan .seealso: `PetscInitialize()`
2976de5d289SStefano Zampini M*/
2986de5d289SStefano Zampini PETSC_EXTERN PetscMPIInt PETSC_MPI_THREAD_REQUIRED;
2996de5d289SStefano Zampini 
300d6f2c3cbSBarry Smith PETSC_EXTERN PetscBool PetscBeganMPI;
3018ad20175SVaclav Hapla PETSC_EXTERN PetscBool PetscErrorHandlingInitialized;
302014dd563SJed Brown PETSC_EXTERN PetscBool PetscInitializeCalled;
303014dd563SJed Brown PETSC_EXTERN PetscBool PetscFinalizeCalled;
3044cf1874eSKarl Rupp PETSC_EXTERN PetscBool PetscViennaCLSynchronize;
305d382aafbSBarry Smith 
306014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*)(MPI_Comm), PetscErrorCode (*)(MPI_Comm));
307014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommDuplicate(MPI_Comm, MPI_Comm *, int *);
308014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommDestroy(MPI_Comm *);
30957f21012SBarry Smith PETSC_EXTERN PetscErrorCode PetscCommGetComm(MPI_Comm, MPI_Comm *);
31057f21012SBarry Smith PETSC_EXTERN PetscErrorCode PetscCommRestoreComm(MPI_Comm, MPI_Comm *);
311d382aafbSBarry Smith 
31259e55d94SJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
31359e55d94SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscKokkosInitializeCheck(void); /* Initialize Kokkos if not yet. */
31459e55d94SJunchao Zhang #endif
31559e55d94SJunchao Zhang 
31671438e86SJunchao Zhang #if defined(PETSC_HAVE_NVSHMEM)
31771438e86SJunchao Zhang PETSC_EXTERN PetscBool      PetscBeganNvshmem;
31871438e86SJunchao Zhang PETSC_EXTERN PetscBool      PetscNvshmemInitialized;
31971438e86SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscNvshmemFinalize(void);
32071438e86SJunchao Zhang #endif
32171438e86SJunchao Zhang 
322540e20f2SPierre Jolivet #if defined(PETSC_HAVE_ELEMENTAL)
323540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalInitializePackage(void);
324540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalInitialized(PetscBool *);
325540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalFinalizePackage(void);
326540e20f2SPierre Jolivet #endif
327540e20f2SPierre Jolivet 
328d382aafbSBarry Smith /*MC
32987497f52SBarry Smith    PetscMalloc - Allocates memory, One should use `PetscNew()`, `PetscMalloc1()` or `PetscCalloc1()` usually instead of this
330d382aafbSBarry Smith 
331eca87e8dSBarry Smith    Synopsis:
332aaa7dc30SBarry Smith     #include <petscsys.h>
333eca87e8dSBarry Smith    PetscErrorCode PetscMalloc(size_t m,void **result)
334eca87e8dSBarry Smith 
335eca87e8dSBarry Smith    Not Collective
336eca87e8dSBarry Smith 
337d382aafbSBarry Smith    Input Parameter:
338d382aafbSBarry Smith .  m - number of bytes to allocate
339d382aafbSBarry Smith 
340d382aafbSBarry Smith    Output Parameter:
341d382aafbSBarry Smith .  result - memory allocated
342d382aafbSBarry Smith 
343d382aafbSBarry Smith    Level: beginner
344d382aafbSBarry Smith 
34549d7da52SJed Brown    Notes:
34649d7da52SJed Brown    Memory is always allocated at least double aligned
347d382aafbSBarry Smith 
34887497f52SBarry Smith    It is safe to allocate size 0 and pass the resulting pointer (which may or may not be NULL) to `PetscFree()`.
349d382aafbSBarry Smith 
350db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`
351d382aafbSBarry Smith M*/
352071fcb05SBarry Smith #define PetscMalloc(a, b) ((*PetscTrMalloc)((a), PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))
353d382aafbSBarry Smith 
354d382aafbSBarry Smith /*MC
355d5b43468SJose E. Roman    PetscRealloc - Reallocates memory
3563221ece2SMatthew G. Knepley 
3573221ece2SMatthew G. Knepley    Synopsis:
3583221ece2SMatthew G. Knepley     #include <petscsys.h>
3593221ece2SMatthew G. Knepley    PetscErrorCode PetscRealloc(size_t m,void **result)
3603221ece2SMatthew G. Knepley 
3613221ece2SMatthew G. Knepley    Not Collective
3623221ece2SMatthew G. Knepley 
3633221ece2SMatthew G. Knepley    Input Parameters:
3643221ece2SMatthew G. Knepley +  m - number of bytes to allocate
36592f119d6SBarry Smith -  result - previous memory
3663221ece2SMatthew G. Knepley 
3673221ece2SMatthew G. Knepley    Output Parameter:
3683221ece2SMatthew G. Knepley .  result - new memory allocated
3693221ece2SMatthew G. Knepley 
3703221ece2SMatthew G. Knepley    Level: developer
3713221ece2SMatthew G. Knepley 
3723221ece2SMatthew G. Knepley    Notes:
3733221ece2SMatthew G. Knepley    Memory is always allocated at least double aligned
3743221ece2SMatthew G. Knepley 
375db781477SPatrick Sanan .seealso: `PetscMalloc()`, `PetscFree()`, `PetscNew()`
3763221ece2SMatthew G. Knepley M*/
3773221ece2SMatthew G. Knepley #define PetscRealloc(a, b) ((*PetscTrRealloc)((a), __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))
3783221ece2SMatthew G. Knepley 
3793221ece2SMatthew G. Knepley /*MC
38087497f52SBarry Smith    PetscAddrAlign - Rounds up an address to `PETSC_MEMALIGN` alignment
381d382aafbSBarry Smith 
382d382aafbSBarry Smith    Synopsis:
383aaa7dc30SBarry Smith     #include <petscsys.h>
384d382aafbSBarry Smith    void *PetscAddrAlign(void *addr)
385d382aafbSBarry Smith 
386eca87e8dSBarry Smith    Not Collective
387eca87e8dSBarry Smith 
388eca87e8dSBarry Smith    Input Parameters:
389eca87e8dSBarry Smith .  addr - address to align (any pointer type)
390eca87e8dSBarry Smith 
391d382aafbSBarry Smith    Level: developer
392d382aafbSBarry Smith 
393db781477SPatrick Sanan .seealso: `PetscMallocAlign()`
394d382aafbSBarry Smith M*/
395f3fa974cSJacob Faibussowitsch #define PetscAddrAlign(a) ((void *)((((PETSC_UINTPTR_T)(a)) + (PETSC_MEMALIGN - 1)) & ~(PETSC_MEMALIGN - 1)))
396d382aafbSBarry Smith 
397d382aafbSBarry Smith /*MC
39887497f52SBarry Smith    PetscCalloc - Allocates a cleared (zeroed) memory region aligned to `PETSC_MEMALIGN`
3998f51dc47SJunchao Zhang 
4008f51dc47SJunchao Zhang    Synopsis:
4018f51dc47SJunchao Zhang     #include <petscsys.h>
4028f51dc47SJunchao Zhang    PetscErrorCode PetscCalloc(size_t m,void **result)
4038f51dc47SJunchao Zhang 
4048f51dc47SJunchao Zhang    Not Collective
4058f51dc47SJunchao Zhang 
4068f51dc47SJunchao Zhang    Input Parameter:
4078f51dc47SJunchao Zhang .  m - number of bytes to allocate
4088f51dc47SJunchao Zhang 
4098f51dc47SJunchao Zhang    Output Parameter:
4108f51dc47SJunchao Zhang .  result - memory allocated
4118f51dc47SJunchao Zhang 
4128f51dc47SJunchao Zhang    Level: beginner
4138f51dc47SJunchao Zhang 
4148f51dc47SJunchao Zhang    Notes:
4158f51dc47SJunchao Zhang    Memory is always allocated at least double aligned. This macro is useful in allocating memory pointed by void pointers
4168f51dc47SJunchao Zhang 
4178f51dc47SJunchao Zhang    It is safe to allocate size 0 and pass the resulting pointer (which may or may not be NULL) to PetscFree().
4188f51dc47SJunchao Zhang 
419db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`
4208f51dc47SJunchao Zhang M*/
421f3fa974cSJacob Faibussowitsch #define PetscCalloc(m, result) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)m), (result))
4228f51dc47SJunchao Zhang 
4238f51dc47SJunchao Zhang /*MC
42487497f52SBarry Smith    PetscMalloc1 - Allocates an array of memory aligned to `PETSC_MEMALIGN`
425d382aafbSBarry Smith 
426eca87e8dSBarry Smith    Synopsis:
427aaa7dc30SBarry Smith     #include <petscsys.h>
428785e854fSJed Brown    PetscErrorCode PetscMalloc1(size_t m1,type **r1)
429785e854fSJed Brown 
430785e854fSJed Brown    Not Collective
431785e854fSJed Brown 
432785e854fSJed Brown    Input Parameter:
433390e1bf2SBarry Smith .  m1 - number of elements to allocate  (may be zero)
434785e854fSJed Brown 
435785e854fSJed Brown    Output Parameter:
4361fe1b817SJunchao Zhang .  r1 - memory allocated
437785e854fSJed Brown 
438e28ce29aSPatrick Sanan    Note:
439e28ce29aSPatrick Sanan    This uses the sizeof() of the memory type requested to determine the total memory to be allocated, therefore you should not
44087497f52SBarry Smith          multiply the number of elements requested by the `sizeof()` the type. For example use
441390e1bf2SBarry Smith $  PetscInt *id;
442390e1bf2SBarry Smith $  PetscMalloc1(10,&id);
443390e1bf2SBarry Smith         not
444390e1bf2SBarry Smith $  PetscInt *id;
445390e1bf2SBarry Smith $  PetscMalloc1(10*sizeof(PetscInt),&id);
446390e1bf2SBarry Smith 
44787497f52SBarry Smith         Does not zero the memory allocated, use `PetscCalloc1()` to obtain memory that has been zeroed.
448390e1bf2SBarry Smith 
449390e1bf2SBarry Smith    Level: beginner
450785e854fSJed Brown 
451db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
452785e854fSJed Brown M*/
453f3fa974cSJacob Faibussowitsch #define PetscMalloc1(m1, r1) PetscMallocA(1, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1))
454785e854fSJed Brown 
455785e854fSJed Brown /*MC
45687497f52SBarry Smith    PetscCalloc1 - Allocates a cleared (zeroed) array of memory aligned to `PETSC_MEMALIGN`
4571795a4d1SJed Brown 
4581795a4d1SJed Brown    Synopsis:
459aaa7dc30SBarry Smith     #include <petscsys.h>
4601795a4d1SJed Brown    PetscErrorCode PetscCalloc1(size_t m1,type **r1)
4611795a4d1SJed Brown 
4621795a4d1SJed Brown    Not Collective
4631795a4d1SJed Brown 
4641795a4d1SJed Brown    Input Parameter:
4651795a4d1SJed Brown .  m1 - number of elements to allocate in 1st chunk  (may be zero)
4661795a4d1SJed Brown 
4671795a4d1SJed Brown    Output Parameter:
4681fe1b817SJunchao Zhang .  r1 - memory allocated
4691795a4d1SJed Brown 
470e28ce29aSPatrick Sanan    Notes:
47187497f52SBarry Smith    See `PetsMalloc1()` for more details on usage.
472390e1bf2SBarry Smith 
473390e1bf2SBarry Smith    Level: beginner
4741795a4d1SJed Brown 
475db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
4761795a4d1SJed Brown M*/
477f3fa974cSJacob Faibussowitsch #define PetscCalloc1(m1, r1) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1))
4781795a4d1SJed Brown 
4791795a4d1SJed Brown /*MC
48087497f52SBarry Smith    PetscMalloc2 - Allocates 2 arrays of memory both aligned to `PETSC_MEMALIGN`
481d382aafbSBarry Smith 
482eca87e8dSBarry Smith    Synopsis:
483aaa7dc30SBarry Smith     #include <petscsys.h>
484dcca6d9dSJed Brown    PetscErrorCode PetscMalloc2(size_t m1,type **r1,size_t m2,type **r2)
485eca87e8dSBarry Smith 
486eca87e8dSBarry Smith    Not Collective
487eca87e8dSBarry Smith 
488d8d19677SJose E. Roman    Input Parameters:
489d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
490dcca6d9dSJed Brown -  m2 - number of elements to allocate in 2nd chunk  (may be zero)
491d382aafbSBarry Smith 
492d8d19677SJose E. Roman    Output Parameters:
493d382aafbSBarry Smith +  r1 - memory allocated in first chunk
494d382aafbSBarry Smith -  r2 - memory allocated in second chunk
495d382aafbSBarry Smith 
496d382aafbSBarry Smith    Level: developer
497d382aafbSBarry Smith 
498db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
499d382aafbSBarry Smith M*/
500f3fa974cSJacob Faibussowitsch #define PetscMalloc2(m1, r1, m2, r2) PetscMallocA(2, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2))
501d382aafbSBarry Smith 
502d382aafbSBarry Smith /*MC
50387497f52SBarry Smith    PetscCalloc2 - Allocates 2 cleared (zeroed) arrays of memory both aligned to `PETSC_MEMALIGN`
504d382aafbSBarry Smith 
505eca87e8dSBarry Smith    Synopsis:
506aaa7dc30SBarry Smith     #include <petscsys.h>
5071795a4d1SJed Brown    PetscErrorCode PetscCalloc2(size_t m1,type **r1,size_t m2,type **r2)
508eca87e8dSBarry Smith 
509eca87e8dSBarry Smith    Not Collective
510eca87e8dSBarry Smith 
511d8d19677SJose E. Roman    Input Parameters:
512d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
5131795a4d1SJed Brown -  m2 - number of elements to allocate in 2nd chunk  (may be zero)
5141795a4d1SJed Brown 
515d8d19677SJose E. Roman    Output Parameters:
5161795a4d1SJed Brown +  r1 - memory allocated in first chunk
5171795a4d1SJed Brown -  r2 - memory allocated in second chunk
5181795a4d1SJed Brown 
5191795a4d1SJed Brown    Level: developer
5201795a4d1SJed Brown 
521db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
5221795a4d1SJed Brown M*/
523f3fa974cSJacob Faibussowitsch #define PetscCalloc2(m1, r1, m2, r2) PetscMallocA(2, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2))
5241795a4d1SJed Brown 
5251795a4d1SJed Brown /*MC
52687497f52SBarry Smith    PetscMalloc3 - Allocates 3 arrays of memory, all aligned to `PETSC_MEMALIGN`
527d382aafbSBarry Smith 
528d382aafbSBarry Smith    Synopsis:
529aaa7dc30SBarry Smith     #include <petscsys.h>
530dcca6d9dSJed Brown    PetscErrorCode PetscMalloc3(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3)
531d382aafbSBarry Smith 
532d382aafbSBarry Smith    Not Collective
533d382aafbSBarry Smith 
534d8d19677SJose E. Roman    Input Parameters:
535d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
536d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
537dcca6d9dSJed Brown -  m3 - number of elements to allocate in 3rd chunk  (may be zero)
538d382aafbSBarry Smith 
539d8d19677SJose E. Roman    Output Parameters:
540d382aafbSBarry Smith +  r1 - memory allocated in first chunk
541d382aafbSBarry Smith .  r2 - memory allocated in second chunk
542d382aafbSBarry Smith -  r3 - memory allocated in third chunk
543d382aafbSBarry Smith 
544d382aafbSBarry Smith    Level: developer
545d382aafbSBarry Smith 
546db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc3()`, `PetscFree3()`
547d382aafbSBarry Smith M*/
548f3fa974cSJacob Faibussowitsch #define PetscMalloc3(m1, r1, m2, r2, m3, r3) \
549f3fa974cSJacob Faibussowitsch   PetscMallocA(3, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3))
550d382aafbSBarry Smith 
551d382aafbSBarry Smith /*MC
55287497f52SBarry Smith    PetscCalloc3 - Allocates 3 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
553d382aafbSBarry Smith 
554eca87e8dSBarry Smith    Synopsis:
555aaa7dc30SBarry Smith     #include <petscsys.h>
5561795a4d1SJed Brown    PetscErrorCode PetscCalloc3(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3)
557eca87e8dSBarry Smith 
558eca87e8dSBarry Smith    Not Collective
559eca87e8dSBarry Smith 
560d8d19677SJose E. Roman    Input Parameters:
561d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
562d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
5631795a4d1SJed Brown -  m3 - number of elements to allocate in 3rd chunk  (may be zero)
5641795a4d1SJed Brown 
565d8d19677SJose E. Roman    Output Parameters:
5661795a4d1SJed Brown +  r1 - memory allocated in first chunk
5671795a4d1SJed Brown .  r2 - memory allocated in second chunk
5681795a4d1SJed Brown -  r3 - memory allocated in third chunk
5691795a4d1SJed Brown 
5701795a4d1SJed Brown    Level: developer
5711795a4d1SJed Brown 
572db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc2()`, `PetscMalloc3()`, `PetscFree3()`
5731795a4d1SJed Brown M*/
574f3fa974cSJacob Faibussowitsch #define PetscCalloc3(m1, r1, m2, r2, m3, r3) \
575f3fa974cSJacob Faibussowitsch   PetscMallocA(3, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3))
5761795a4d1SJed Brown 
5771795a4d1SJed Brown /*MC
57887497f52SBarry Smith    PetscMalloc4 - Allocates 4 arrays of memory, all aligned to `PETSC_MEMALIGN`
579d382aafbSBarry Smith 
580d382aafbSBarry Smith    Synopsis:
581aaa7dc30SBarry Smith     #include <petscsys.h>
582dcca6d9dSJed Brown    PetscErrorCode PetscMalloc4(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4)
583d382aafbSBarry Smith 
584d382aafbSBarry Smith    Not Collective
585d382aafbSBarry Smith 
586d8d19677SJose E. Roman    Input Parameters:
587d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
588d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
589d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
590dcca6d9dSJed Brown -  m4 - number of elements to allocate in 4th chunk  (may be zero)
591d382aafbSBarry Smith 
592d8d19677SJose E. Roman    Output Parameters:
593d382aafbSBarry Smith +  r1 - memory allocated in first chunk
594d382aafbSBarry Smith .  r2 - memory allocated in second chunk
595d382aafbSBarry Smith .  r3 - memory allocated in third chunk
596d382aafbSBarry Smith -  r4 - memory allocated in fourth chunk
597d382aafbSBarry Smith 
598d382aafbSBarry Smith    Level: developer
599d382aafbSBarry Smith 
600db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
601d382aafbSBarry Smith M*/
6029371c9d4SSatish Balay #define PetscMalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
603f3fa974cSJacob Faibussowitsch   PetscMallocA(4, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4))
604d382aafbSBarry Smith 
605d382aafbSBarry Smith /*MC
60687497f52SBarry Smith    PetscCalloc4 - Allocates 4 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
607d382aafbSBarry Smith 
608eca87e8dSBarry Smith    Synopsis:
609aaa7dc30SBarry Smith     #include <petscsys.h>
6101795a4d1SJed Brown    PetscErrorCode PetscCalloc4(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4)
611eca87e8dSBarry Smith 
612eca87e8dSBarry Smith    Not Collective
613eca87e8dSBarry Smith 
614e28ce29aSPatrick Sanan    Input Parameters:
615d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
616d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
617d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
6181795a4d1SJed Brown -  m4 - number of elements to allocate in 4th chunk  (may be zero)
6191795a4d1SJed Brown 
620e28ce29aSPatrick Sanan    Output Parameters:
6211795a4d1SJed Brown +  r1 - memory allocated in first chunk
6221795a4d1SJed Brown .  r2 - memory allocated in second chunk
6231795a4d1SJed Brown .  r3 - memory allocated in third chunk
6241795a4d1SJed Brown -  r4 - memory allocated in fourth chunk
6251795a4d1SJed Brown 
6261795a4d1SJed Brown    Level: developer
6271795a4d1SJed Brown 
628db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
6291795a4d1SJed Brown M*/
6309371c9d4SSatish Balay #define PetscCalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
631f3fa974cSJacob Faibussowitsch   PetscMallocA(4, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4))
6321795a4d1SJed Brown 
6331795a4d1SJed Brown /*MC
63487497f52SBarry Smith    PetscMalloc5 - Allocates 5 arrays of memory, all aligned to `PETSC_MEMALIGN`
635d382aafbSBarry Smith 
636d382aafbSBarry Smith    Synopsis:
637aaa7dc30SBarry Smith     #include <petscsys.h>
638dcca6d9dSJed Brown    PetscErrorCode PetscMalloc5(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5)
639d382aafbSBarry Smith 
640d382aafbSBarry Smith    Not Collective
641d382aafbSBarry Smith 
642e28ce29aSPatrick Sanan    Input Parameters:
643d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
644d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
645d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
646d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
647dcca6d9dSJed Brown -  m5 - number of elements to allocate in 5th chunk  (may be zero)
648d382aafbSBarry Smith 
649e28ce29aSPatrick Sanan    Output Parameters:
650d382aafbSBarry Smith +  r1 - memory allocated in first chunk
651d382aafbSBarry Smith .  r2 - memory allocated in second chunk
652d382aafbSBarry Smith .  r3 - memory allocated in third chunk
653d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
654d382aafbSBarry Smith -  r5 - memory allocated in fifth chunk
655d382aafbSBarry Smith 
656d382aafbSBarry Smith    Level: developer
657d382aafbSBarry Smith 
658db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc5()`, `PetscFree5()`
659d382aafbSBarry Smith M*/
6609371c9d4SSatish Balay #define PetscMalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
661f3fa974cSJacob Faibussowitsch   PetscMallocA(5, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5))
662d382aafbSBarry Smith 
663d382aafbSBarry Smith /*MC
66487497f52SBarry Smith    PetscCalloc5 - Allocates 5 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
665d382aafbSBarry Smith 
666eca87e8dSBarry Smith    Synopsis:
667aaa7dc30SBarry Smith     #include <petscsys.h>
6681795a4d1SJed Brown    PetscErrorCode PetscCalloc5(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5)
669eca87e8dSBarry Smith 
670eca87e8dSBarry Smith    Not Collective
671eca87e8dSBarry Smith 
672e28ce29aSPatrick Sanan    Input Parameters:
673d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
674d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
675d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
676d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
6771795a4d1SJed Brown -  m5 - number of elements to allocate in 5th chunk  (may be zero)
6781795a4d1SJed Brown 
679e28ce29aSPatrick Sanan    Output Parameters:
6801795a4d1SJed Brown +  r1 - memory allocated in first chunk
6811795a4d1SJed Brown .  r2 - memory allocated in second chunk
6821795a4d1SJed Brown .  r3 - memory allocated in third chunk
6831795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
6841795a4d1SJed Brown -  r5 - memory allocated in fifth chunk
6851795a4d1SJed Brown 
6861795a4d1SJed Brown    Level: developer
6871795a4d1SJed Brown 
688db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc5()`, `PetscFree5()`
6891795a4d1SJed Brown M*/
6909371c9d4SSatish Balay #define PetscCalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
691f3fa974cSJacob Faibussowitsch   PetscMallocA(5, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5))
692d382aafbSBarry Smith 
693d382aafbSBarry Smith /*MC
69487497f52SBarry Smith    PetscMalloc6 - Allocates 6 arrays of memory, all aligned to `PETSC_MEMALIGN`
695d382aafbSBarry Smith 
696d382aafbSBarry Smith    Synopsis:
697aaa7dc30SBarry Smith     #include <petscsys.h>
698dcca6d9dSJed Brown    PetscErrorCode PetscMalloc6(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5,size_t m6,type **r6)
699d382aafbSBarry Smith 
700d382aafbSBarry Smith    Not Collective
701d382aafbSBarry Smith 
702e28ce29aSPatrick Sanan    Input Parameters:
703d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
704d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
705d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
706d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
707d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
708dcca6d9dSJed Brown -  m6 - number of elements to allocate in 6th chunk  (may be zero)
709d382aafbSBarry Smith 
710e28ce29aSPatrick Sanan    Output Parameteasr:
711d382aafbSBarry Smith +  r1 - memory allocated in first chunk
712d382aafbSBarry Smith .  r2 - memory allocated in second chunk
713d382aafbSBarry Smith .  r3 - memory allocated in third chunk
714d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
715d382aafbSBarry Smith .  r5 - memory allocated in fifth chunk
716d382aafbSBarry Smith -  r6 - memory allocated in sixth chunk
717d382aafbSBarry Smith 
718d382aafbSBarry Smith    Level: developer
719d382aafbSBarry Smith 
720db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc6()`, `PetscFree3()`, `PetscFree4()`, `PetscFree5()`, `PetscFree6()`
721d382aafbSBarry Smith M*/
7229371c9d4SSatish Balay #define PetscMalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
723f3fa974cSJacob Faibussowitsch   PetscMallocA(6, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5), ((size_t)((size_t)m6) * sizeof(**(r6))), (r6))
724d382aafbSBarry Smith 
725d382aafbSBarry Smith /*MC
72687497f52SBarry Smith    PetscCalloc6 - Allocates 6 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
727d382aafbSBarry Smith 
728eca87e8dSBarry Smith    Synopsis:
729aaa7dc30SBarry Smith     #include <petscsys.h>
7301795a4d1SJed Brown    PetscErrorCode PetscCalloc6(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5,size_t m6,type **r6)
731eca87e8dSBarry Smith 
732eca87e8dSBarry Smith    Not Collective
733eca87e8dSBarry Smith 
734e28ce29aSPatrick Sanan    Input Parameters:
735d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
736d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
737d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
738d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
739d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
7401795a4d1SJed Brown -  m6 - number of elements to allocate in 6th chunk  (may be zero)
7411795a4d1SJed Brown 
742e28ce29aSPatrick Sanan    Output Parameters:
7431795a4d1SJed Brown +  r1 - memory allocated in first chunk
7441795a4d1SJed Brown .  r2 - memory allocated in second chunk
7451795a4d1SJed Brown .  r3 - memory allocated in third chunk
7461795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
7471795a4d1SJed Brown .  r5 - memory allocated in fifth chunk
7481795a4d1SJed Brown -  r6 - memory allocated in sixth chunk
7491795a4d1SJed Brown 
7501795a4d1SJed Brown    Level: developer
7511795a4d1SJed Brown 
752db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc6()`, `PetscFree6()`
7531795a4d1SJed Brown M*/
7549371c9d4SSatish Balay #define PetscCalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
755f3fa974cSJacob Faibussowitsch   PetscMallocA(6, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5), ((size_t)((size_t)m6) * sizeof(**(r6))), (r6))
7561795a4d1SJed Brown 
7571795a4d1SJed Brown /*MC
75887497f52SBarry Smith    PetscMalloc7 - Allocates 7 arrays of memory, all aligned to `PETSC_MEMALIGN`
759d382aafbSBarry Smith 
760d382aafbSBarry Smith    Synopsis:
761aaa7dc30SBarry Smith     #include <petscsys.h>
762dcca6d9dSJed Brown    PetscErrorCode PetscMalloc7(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5,size_t m6,type **r6,size_t m7,type **r7)
763d382aafbSBarry Smith 
764d382aafbSBarry Smith    Not Collective
765d382aafbSBarry Smith 
766e28ce29aSPatrick Sanan    Input Parameters:
767d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
768d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
769d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
770d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
771d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
772d382aafbSBarry Smith .  m6 - number of elements to allocate in 6th chunk  (may be zero)
773dcca6d9dSJed Brown -  m7 - number of elements to allocate in 7th chunk  (may be zero)
774d382aafbSBarry Smith 
775e28ce29aSPatrick Sanan    Output Parameters:
776d382aafbSBarry Smith +  r1 - memory allocated in first chunk
777d382aafbSBarry Smith .  r2 - memory allocated in second chunk
778d382aafbSBarry Smith .  r3 - memory allocated in third chunk
779d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
780d382aafbSBarry Smith .  r5 - memory allocated in fifth chunk
781d382aafbSBarry Smith .  r6 - memory allocated in sixth chunk
782eca87e8dSBarry Smith -  r7 - memory allocated in seventh chunk
783d382aafbSBarry Smith 
784d382aafbSBarry Smith    Level: developer
785d382aafbSBarry Smith 
786db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc7()`, `PetscFree7()`
787d382aafbSBarry Smith M*/
7889371c9d4SSatish Balay #define PetscMalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
789f3fa974cSJacob Faibussowitsch   PetscMallocA(7, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5), ((size_t)((size_t)m6) * sizeof(**(r6))), (r6), ((size_t)((size_t)m7) * sizeof(**(r7))), (r7))
790d382aafbSBarry Smith 
791d382aafbSBarry Smith /*MC
79287497f52SBarry Smith    PetscCalloc7 - Allocates 7 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
7931795a4d1SJed Brown 
7941795a4d1SJed Brown    Synopsis:
795aaa7dc30SBarry Smith     #include <petscsys.h>
7961795a4d1SJed Brown    PetscErrorCode PetscCalloc7(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5,size_t m6,type **r6,size_t m7,type **r7)
7971795a4d1SJed Brown 
7981795a4d1SJed Brown    Not Collective
7991795a4d1SJed Brown 
800e28ce29aSPatrick Sanan    Input Parameters:
8011795a4d1SJed Brown +  m1 - number of elements to allocate in 1st chunk  (may be zero)
8021795a4d1SJed Brown .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
8031795a4d1SJed Brown .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
8041795a4d1SJed Brown .  m4 - number of elements to allocate in 4th chunk  (may be zero)
8051795a4d1SJed Brown .  m5 - number of elements to allocate in 5th chunk  (may be zero)
8061795a4d1SJed Brown .  m6 - number of elements to allocate in 6th chunk  (may be zero)
8071795a4d1SJed Brown -  m7 - number of elements to allocate in 7th chunk  (may be zero)
8081795a4d1SJed Brown 
809e28ce29aSPatrick Sanan    Output Parameters:
8101795a4d1SJed Brown +  r1 - memory allocated in first chunk
8111795a4d1SJed Brown .  r2 - memory allocated in second chunk
8121795a4d1SJed Brown .  r3 - memory allocated in third chunk
8131795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
8141795a4d1SJed Brown .  r5 - memory allocated in fifth chunk
8151795a4d1SJed Brown .  r6 - memory allocated in sixth chunk
8161795a4d1SJed Brown -  r7 - memory allocated in seventh chunk
8171795a4d1SJed Brown 
8181795a4d1SJed Brown    Level: developer
8191795a4d1SJed Brown 
820db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc7()`, `PetscFree7()`
8211795a4d1SJed Brown M*/
8229371c9d4SSatish Balay #define PetscCalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
823f3fa974cSJacob Faibussowitsch   PetscMallocA(7, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5), ((size_t)((size_t)m6) * sizeof(**(r6))), (r6), ((size_t)((size_t)m7) * sizeof(**(r7))), (r7))
8241795a4d1SJed Brown 
8251795a4d1SJed Brown /*MC
82687497f52SBarry Smith    PetscNew - Allocates memory of a particular type, zeros the memory! Aligned to `PETSC_MEMALIGN`
827d382aafbSBarry Smith 
828eca87e8dSBarry Smith    Synopsis:
829aaa7dc30SBarry Smith     #include <petscsys.h>
830b00a9115SJed Brown    PetscErrorCode PetscNew(type **result)
831eca87e8dSBarry Smith 
832eca87e8dSBarry Smith    Not Collective
833eca87e8dSBarry Smith 
834d382aafbSBarry Smith    Output Parameter:
835b00a9115SJed Brown .  result - memory allocated, sized to match pointer type
836d382aafbSBarry Smith 
837d382aafbSBarry Smith    Level: beginner
838d382aafbSBarry Smith 
839fea72eb4SStefano Zampini .seealso: `PetscFree()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc1()`
840d382aafbSBarry Smith M*/
841b00a9115SJed Brown #define PetscNew(b) PetscCalloc1(1, (b))
842ad0619ddSBarry Smith 
8434dfa11a4SJacob Faibussowitsch #define PetscNewLog(o, b) PETSC_DEPRECATED_MACRO("GCC warning \"PetscNewLog is deprecated, use PetscNew() instead (since version 3.18)\"") PetscNew((b))
844d382aafbSBarry Smith 
845d382aafbSBarry Smith /*MC
846d382aafbSBarry Smith    PetscFree - Frees memory
847d382aafbSBarry Smith 
848eca87e8dSBarry Smith    Synopsis:
849aaa7dc30SBarry Smith     #include <petscsys.h>
850eca87e8dSBarry Smith    PetscErrorCode PetscFree(void *memory)
851eca87e8dSBarry Smith 
852eca87e8dSBarry Smith    Not Collective
853eca87e8dSBarry Smith 
854d382aafbSBarry Smith    Input Parameter:
8557f4976b7SJacob Faibussowitsch .   memory - memory to free (the pointer is ALWAYS set to NULL upon success)
856d382aafbSBarry Smith 
857d382aafbSBarry Smith    Level: beginner
858d382aafbSBarry Smith 
85949d7da52SJed Brown    Notes:
86087497f52SBarry Smith    Do not free memory obtained with `PetscMalloc2()`, `PetscCalloc2()` etc, they must be freed with `PetscFree2()` etc.
861390e1bf2SBarry Smith 
86287497f52SBarry Smith    It is safe to call `PetscFree()` on a NULL pointer.
863d382aafbSBarry Smith 
864fea72eb4SStefano Zampini .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc1()`
865d382aafbSBarry Smith M*/
8663ba16761SJacob Faibussowitsch #define PetscFree(a) ((PetscErrorCode)((*PetscTrFree)((void *)(a), __LINE__, PETSC_FUNCTION_NAME, __FILE__) || ((a) = PETSC_NULLPTR, PETSC_SUCCESS)))
867d382aafbSBarry Smith 
868d382aafbSBarry Smith /*MC
86987497f52SBarry Smith    PetscFree2 - Frees 2 chunks of memory obtained with `PetscMalloc2()`
870d382aafbSBarry Smith 
871eca87e8dSBarry Smith    Synopsis:
872aaa7dc30SBarry Smith     #include <petscsys.h>
873eca87e8dSBarry Smith    PetscErrorCode PetscFree2(void *memory1,void *memory2)
874eca87e8dSBarry Smith 
875eca87e8dSBarry Smith    Not Collective
876eca87e8dSBarry Smith 
877e28ce29aSPatrick Sanan    Input Parameters:
878d382aafbSBarry Smith +   memory1 - memory to free
879d382aafbSBarry Smith -   memory2 - 2nd memory to free
880d382aafbSBarry Smith 
881d382aafbSBarry Smith    Level: developer
882d382aafbSBarry Smith 
88395452b02SPatrick Sanan    Notes:
88487497f52SBarry Smith     Memory must have been obtained with `PetscMalloc2()`
885d382aafbSBarry Smith 
886db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`
887d382aafbSBarry Smith M*/
888ba282f50SJed Brown #define PetscFree2(m1, m2) PetscFreeA(2, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2))
889d382aafbSBarry Smith 
890d382aafbSBarry Smith /*MC
89187497f52SBarry Smith    PetscFree3 - Frees 3 chunks of memory obtained with `PetscMalloc3()`
892d382aafbSBarry Smith 
893eca87e8dSBarry Smith    Synopsis:
894aaa7dc30SBarry Smith     #include <petscsys.h>
895eca87e8dSBarry Smith    PetscErrorCode PetscFree3(void *memory1,void *memory2,void *memory3)
896eca87e8dSBarry Smith 
897eca87e8dSBarry Smith    Not Collective
898eca87e8dSBarry Smith 
899e28ce29aSPatrick Sanan    Input Parameters:
900d382aafbSBarry Smith +   memory1 - memory to free
901d382aafbSBarry Smith .   memory2 - 2nd memory to free
902d382aafbSBarry Smith -   memory3 - 3rd memory to free
903d382aafbSBarry Smith 
904d382aafbSBarry Smith    Level: developer
905d382aafbSBarry Smith 
90695452b02SPatrick Sanan    Notes:
90787497f52SBarry Smith     Memory must have been obtained with `PetscMalloc3()`
908d382aafbSBarry Smith 
909db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`
910d382aafbSBarry Smith M*/
911ba282f50SJed Brown #define PetscFree3(m1, m2, m3) PetscFreeA(3, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3))
912d382aafbSBarry Smith 
913d382aafbSBarry Smith /*MC
91487497f52SBarry Smith    PetscFree4 - Frees 4 chunks of memory obtained with `PetscMalloc4()`
915d382aafbSBarry Smith 
916eca87e8dSBarry Smith    Synopsis:
917aaa7dc30SBarry Smith     #include <petscsys.h>
918eca87e8dSBarry Smith    PetscErrorCode PetscFree4(void *m1,void *m2,void *m3,void *m4)
919eca87e8dSBarry Smith 
920eca87e8dSBarry Smith    Not Collective
921eca87e8dSBarry Smith 
922e28ce29aSPatrick Sanan    Input Parameters:
923d382aafbSBarry Smith +   m1 - memory to free
924d382aafbSBarry Smith .   m2 - 2nd memory to free
925d382aafbSBarry Smith .   m3 - 3rd memory to free
926d382aafbSBarry Smith -   m4 - 4th memory to free
927d382aafbSBarry Smith 
928d382aafbSBarry Smith    Level: developer
929d382aafbSBarry Smith 
93095452b02SPatrick Sanan    Notes:
93187497f52SBarry Smith     Memory must have been obtained with `PetscMalloc4()`
932d382aafbSBarry Smith 
933db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`
934d382aafbSBarry Smith M*/
935ba282f50SJed Brown #define PetscFree4(m1, m2, m3, m4) PetscFreeA(4, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4))
936d382aafbSBarry Smith 
937d382aafbSBarry Smith /*MC
93887497f52SBarry Smith    PetscFree5 - Frees 5 chunks of memory obtained with `PetscMalloc5()`
939d382aafbSBarry Smith 
940eca87e8dSBarry Smith    Synopsis:
941aaa7dc30SBarry Smith     #include <petscsys.h>
942eca87e8dSBarry Smith    PetscErrorCode PetscFree5(void *m1,void *m2,void *m3,void *m4,void *m5)
943eca87e8dSBarry Smith 
944eca87e8dSBarry Smith    Not Collective
945eca87e8dSBarry Smith 
946e28ce29aSPatrick Sanan    Input Parameters:
947d382aafbSBarry Smith +   m1 - memory to free
948d382aafbSBarry Smith .   m2 - 2nd memory to free
949d382aafbSBarry Smith .   m3 - 3rd memory to free
950d382aafbSBarry Smith .   m4 - 4th memory to free
951d382aafbSBarry Smith -   m5 - 5th memory to free
952d382aafbSBarry Smith 
953d382aafbSBarry Smith    Level: developer
954d382aafbSBarry Smith 
95595452b02SPatrick Sanan    Notes:
95687497f52SBarry Smith     Memory must have been obtained with `PetscMalloc5()`
957d382aafbSBarry Smith 
958db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`
959d382aafbSBarry Smith M*/
960ba282f50SJed Brown #define PetscFree5(m1, m2, m3, m4, m5) PetscFreeA(5, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5))
961d382aafbSBarry Smith 
962d382aafbSBarry Smith /*MC
96387497f52SBarry Smith    PetscFree6 - Frees 6 chunks of memory obtained with `PetscMalloc6()`
964d382aafbSBarry Smith 
965eca87e8dSBarry Smith    Synopsis:
966aaa7dc30SBarry Smith     #include <petscsys.h>
967eca87e8dSBarry Smith    PetscErrorCode PetscFree6(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6)
968eca87e8dSBarry Smith 
969eca87e8dSBarry Smith    Not Collective
970eca87e8dSBarry Smith 
971e28ce29aSPatrick Sanan    Input Parameters:
972d382aafbSBarry Smith +   m1 - memory to free
973d382aafbSBarry Smith .   m2 - 2nd memory to free
974d382aafbSBarry Smith .   m3 - 3rd memory to free
975d382aafbSBarry Smith .   m4 - 4th memory to free
976d382aafbSBarry Smith .   m5 - 5th memory to free
977d382aafbSBarry Smith -   m6 - 6th memory to free
978d382aafbSBarry Smith 
979d382aafbSBarry Smith    Level: developer
980d382aafbSBarry Smith 
98195452b02SPatrick Sanan    Notes:
98287497f52SBarry Smith     Memory must have been obtained with `PetscMalloc6()`
983d382aafbSBarry Smith 
984db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`
985d382aafbSBarry Smith M*/
986ba282f50SJed Brown #define PetscFree6(m1, m2, m3, m4, m5, m6) PetscFreeA(6, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5), &(m6))
987d382aafbSBarry Smith 
988d382aafbSBarry Smith /*MC
98987497f52SBarry Smith    PetscFree7 - Frees 7 chunks of memory obtained with `PetscMalloc7()`
990d382aafbSBarry Smith 
991eca87e8dSBarry Smith    Synopsis:
992aaa7dc30SBarry Smith     #include <petscsys.h>
993eca87e8dSBarry Smith    PetscErrorCode PetscFree7(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6,void *m7)
994eca87e8dSBarry Smith 
995eca87e8dSBarry Smith    Not Collective
996eca87e8dSBarry Smith 
997e28ce29aSPatrick Sanan    Input Parameters:
998d382aafbSBarry Smith +   m1 - memory to free
999d382aafbSBarry Smith .   m2 - 2nd memory to free
1000d382aafbSBarry Smith .   m3 - 3rd memory to free
1001d382aafbSBarry Smith .   m4 - 4th memory to free
1002d382aafbSBarry Smith .   m5 - 5th memory to free
1003d382aafbSBarry Smith .   m6 - 6th memory to free
1004d382aafbSBarry Smith -   m7 - 7th memory to free
1005d382aafbSBarry Smith 
1006d382aafbSBarry Smith    Level: developer
1007d382aafbSBarry Smith 
100895452b02SPatrick Sanan    Notes:
100987497f52SBarry Smith     Memory must have been obtained with `PetscMalloc7()`
1010d382aafbSBarry Smith 
1011db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`,
1012db781477SPatrick Sanan           `PetscMalloc7()`
1013d382aafbSBarry Smith M*/
1014ba282f50SJed Brown #define PetscFree7(m1, m2, m3, m4, m5, m6, m7) PetscFreeA(7, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5), &(m6), &(m7))
1015d382aafbSBarry Smith 
1016ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocA(int, PetscBool, int, const char *, const char *, size_t, void *, ...);
1017ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscFreeA(int, int, const char *, const char *, void *, ...);
1018071fcb05SBarry Smith PETSC_EXTERN                PetscErrorCode (*PetscTrMalloc)(size_t, PetscBool, int, const char[], const char[], void **);
1019efca3c55SSatish Balay PETSC_EXTERN                PetscErrorCode (*PetscTrFree)(void *, int, const char[], const char[]);
10203221ece2SMatthew G. Knepley PETSC_EXTERN                PetscErrorCode (*PetscTrRealloc)(size_t, int, const char[], const char[], void **);
1021ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocSetCoalesce(PetscBool);
102292f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocSet(PetscErrorCode (*)(size_t, PetscBool, int, const char[], const char[], void **), PetscErrorCode (*)(void *, int, const char[], const char[]), PetscErrorCode (*)(size_t, int, const char[], const char[], void **));
1023014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocClear(void);
1024d382aafbSBarry Smith 
1025d382aafbSBarry Smith /*
1026c1706be6SHong Zhang   Unlike PetscMallocSet and PetscMallocClear which overwrite the existing settings, these two functions save the previous choice of allocator, and should be used in pair.
1027c1706be6SHong Zhang */
1028c1706be6SHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocSetDRAM(void);
1029c1706be6SHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocResetDRAM(void);
103031f06eaaSHong Zhang #if defined(PETSC_HAVE_CUDA)
103131f06eaaSHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocSetCUDAHost(void);
103231f06eaaSHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocResetCUDAHost(void);
103331f06eaaSHong Zhang #endif
103459af0bd3SScott Kruger #if defined(PETSC_HAVE_HIP)
103559af0bd3SScott Kruger PETSC_EXTERN PetscErrorCode PetscMallocSetHIPHost(void);
103659af0bd3SScott Kruger PETSC_EXTERN PetscErrorCode PetscMallocResetHIPHost(void);
103759af0bd3SScott Kruger #endif
103859af0bd3SScott Kruger 
1039a5057860SBarry Smith #define MPIU_PETSCLOGDOUBLE  MPI_DOUBLE
104036763ca0SBas van 't Hof #define MPIU_2PETSCLOGDOUBLE MPI_2DOUBLE_PRECISION
1041a5057860SBarry Smith 
1042a5057860SBarry Smith /*
104366d669d6SBarry Smith    Routines for tracing memory corruption/bleeding with default PETSc memory allocation
1044d382aafbSBarry Smith */
1045014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocDump(FILE *);
104692f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocView(FILE *);
1047014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocGetCurrentUsage(PetscLogDouble *);
1048014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocGetMaximumUsage(PetscLogDouble *);
1049e3ed9ee7SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocPushMaximumUsage(int);
1050e3ed9ee7SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocPopMaximumUsage(int, PetscLogDouble *);
105192f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocSetDebug(PetscBool, PetscBool);
105292f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocGetDebug(PetscBool *, PetscBool *, PetscBool *);
1053efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscMallocValidate(int, const char[], const char[]);
105492f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocViewSet(PetscLogDouble);
105592f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocViewGet(PetscBool *);
1056608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeSet(PetscBool);
1057608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeGet(PetscBool *);
1058d382aafbSBarry Smith 
1059014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType, MPI_Datatype *);
1060014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIDataTypeToPetscDataType(MPI_Datatype, PetscDataType *);
1061014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDataTypeGetSize(PetscDataType, size_t *);
10628e4b2d1dSBarry Smith PETSC_EXTERN PetscErrorCode PetscDataTypeFromString(const char *, PetscDataType *, PetscBool *);
1063d382aafbSBarry Smith 
1064d382aafbSBarry Smith /*
1065d382aafbSBarry Smith    These are MPI operations for MPI_Allreduce() etc
1066d382aafbSBarry Smith */
1067367daffbSBarry Smith PETSC_EXTERN MPI_Op MPIU_MAXSUM_OP;
1068570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
1069de272c7aSSatish Balay PETSC_EXTERN MPI_Op MPIU_SUM;
1070014dd563SJed Brown PETSC_EXTERN MPI_Op MPIU_MAX;
1071014dd563SJed Brown PETSC_EXTERN MPI_Op MPIU_MIN;
1072d9822059SBarry Smith #else
1073de272c7aSSatish Balay   #define MPIU_SUM MPI_SUM
1074d9822059SBarry Smith   #define MPIU_MAX MPI_MAX
1075d9822059SBarry Smith   #define MPIU_MIN MPI_MIN
1076d9822059SBarry Smith #endif
107762e5d2d2SJDBetteridge PETSC_EXTERN MPI_Op         Petsc_Garbage_SetIntersectOp;
107862e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscMaxSum(MPI_Comm, const PetscInt[], PetscInt *, PetscInt *);
107962e5d2d2SJDBetteridge 
1080a2498233SPierre Jolivet #if (defined(PETSC_HAVE_REAL___FLOAT128) && !defined(PETSC_SKIP_REAL___FLOAT128)) || (defined(PETSC_HAVE_REAL___FP16) && !defined(PETSC_SKIP_REAL___FP16))
1081613bf2b2SPierre Jolivet /*MC
10829e517322SPierre Jolivet     MPIU_SUM___FP16___FLOAT128 - MPI_Op that acts as a replacement for MPI_SUM with
10839e517322SPierre Jolivet     custom MPI_Datatype `MPIU___FLOAT128`, `MPIU___COMPLEX128`, and `MPIU___FP16`.
1084613bf2b2SPierre Jolivet 
1085613bf2b2SPierre Jolivet    Level: advanced
1086613bf2b2SPierre Jolivet 
1087613bf2b2SPierre Jolivet    Developer Note:
1088613bf2b2SPierre Jolivet    This should be unified with `MPIU_SUM`
1089613bf2b2SPierre Jolivet 
1090613bf2b2SPierre Jolivet .seealso: `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`
1091613bf2b2SPierre Jolivet M*/
10929e517322SPierre Jolivet PETSC_EXTERN MPI_Op MPIU_SUM___FP16___FLOAT128;
1093613bf2b2SPierre Jolivet #endif
1094014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMaxSum(MPI_Comm, const PetscInt[], PetscInt *, PetscInt *);
1095d382aafbSBarry Smith 
109693d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIULong_Send(void *, PetscInt, MPI_Datatype, PetscMPIInt, PetscMPIInt, MPI_Comm) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(1, 3);
109793d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIULong_Recv(void *, PetscInt, MPI_Datatype, PetscMPIInt, PetscMPIInt, MPI_Comm) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(1, 3);
1098eb3f98d2SBarry Smith 
1099639ff905SBarry Smith /*
1100639ff905SBarry Smith     Defines PETSc error handling.
1101639ff905SBarry Smith */
1102639ff905SBarry Smith #include <petscerror.h>
1103d382aafbSBarry Smith 
1104660278c0SBarry Smith PETSC_EXTERN PetscBool   PetscCIEnabled;                    /* code is running in the PETSc test harness CI */
1105660278c0SBarry Smith PETSC_EXTERN PetscBool   PetscCIEnabledPortableErrorOutput; /* error output is stripped to ensure portability of error messages across systems */
1106660278c0SBarry Smith PETSC_EXTERN const char *PetscCIFilename(const char *);
1107660278c0SBarry Smith PETSC_EXTERN int         PetscCILinenumber(int);
1108660278c0SBarry Smith 
1109f3fa974cSJacob Faibussowitsch #define PETSC_SMALLEST_CLASSID ((PetscClassId)1211211)
1110014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_LARGEST_CLASSID;
1111014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_OBJECT_CLASSID;
1112014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscClassIdRegister(const char[], PetscClassId *);
111381256985SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscObjectGetId(PetscObject, PetscObjectId *);
111481256985SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscObjectCompareId(PetscObject, PetscObjectId, PetscBool *);
111581256985SMatthew G. Knepley 
1116d382aafbSBarry Smith /*
1117d382aafbSBarry Smith    Routines that get memory usage information from the OS
1118d382aafbSBarry Smith */
1119014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemoryGetCurrentUsage(PetscLogDouble *);
1120014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemoryGetMaximumUsage(PetscLogDouble *);
1121014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemorySetGetMaximumUsage(void);
1122b44d5720SBarry Smith PETSC_EXTERN PetscErrorCode PetscMemoryTrace(const char[]);
1123d382aafbSBarry Smith 
1124014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSleep(PetscReal);
1125d382aafbSBarry Smith 
1126d382aafbSBarry Smith /*
1127d382aafbSBarry Smith    Initialization of PETSc
1128d382aafbSBarry Smith */
1129014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitialize(int *, char ***, const char[], const char[]);
1130014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeNoPointers(int, char **, const char[], const char[]);
1131014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeNoArguments(void);
1132014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitialized(PetscBool *);
1133014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFinalized(PetscBool *);
1134014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFinalize(void);
1135014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeFortran(void);
1136014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArgs(int *, char ***);
1137014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArguments(char ***);
1138014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFreeArguments(char **);
1139d382aafbSBarry Smith 
1140014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscEnd(void);
1141607a6623SBarry Smith PETSC_EXTERN PetscErrorCode PetscSysInitializePackage(void);
1142d382aafbSBarry Smith 
1143014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonInitialize(const char[], const char[]);
1144014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonFinalize(void);
1145014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonPrintError(void);
1146014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonMonitorSet(PetscObject, const char[]);
1147d382aafbSBarry Smith 
1148a50e088cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMonitorCompare(PetscErrorCode (*)(void), void *, PetscErrorCode (*)(void **), PetscErrorCode (*)(void), void *, PetscErrorCode (*)(void **), PetscBool *);
1149a50e088cSMatthew G. Knepley 
1150d382aafbSBarry Smith /*
1151d382aafbSBarry Smith      These are so that in extern C code we can caste function pointers to non-extern C
11522981ebdbSBarry Smith    function pointers. Since the regular C++ code expects its function pointers to be C++
1153d382aafbSBarry Smith */
1154638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef void (**PetscVoidStarFunction)(void);
1155638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef void (*PetscVoidFunction)(void);
1156638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef PetscErrorCode (*PetscErrorCodeFunction)(void);
1157d382aafbSBarry Smith 
1158d382aafbSBarry Smith /*
1159d382aafbSBarry Smith     Functions that can act on any PETSc object.
1160d382aafbSBarry Smith */
1161014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectDestroy(PetscObject *);
1162014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetComm(PetscObject, MPI_Comm *);
1163014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetClassId(PetscObject, PetscClassId *);
1164014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetClassName(PetscObject, const char *[]);
1165014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetType(PetscObject, const char *[]);
1166014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetName(PetscObject, const char[]);
1167014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetName(PetscObject, const char *[]);
1168014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetTabLevel(PetscObject, PetscInt);
1169014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetTabLevel(PetscObject, PetscInt *);
1170014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectIncrementTabLevel(PetscObject, PetscObject, PetscInt);
1171014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectReference(PetscObject);
1172014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetReference(PetscObject, PetscInt *);
1173014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectDereference(PetscObject);
1174014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetNewTag(PetscObject, PetscMPIInt *);
1175014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectCompose(PetscObject, const char[], PetscObject);
1176014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRemoveReference(PetscObject, const char[]);
1177014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectQuery(PetscObject, const char[], PetscObject *);
1178bdf89e91SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectComposeFunction_Private(PetscObject, const char[], void (*)(void));
1179f3fa974cSJacob Faibussowitsch #define PetscObjectComposeFunction(a, b, d) PetscObjectComposeFunction_Private((a), (b), (PetscVoidFunction)(d))
1180014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetFromOptions(PetscObject);
1181014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetUp(PetscObject);
11820eb63584SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSetPrintedOptions(PetscObject);
11830eb63584SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectInheritPrintedOptions(PetscObject, PetscObject);
1184014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommGetNewTag(MPI_Comm, PetscMPIInt *);
1185d382aafbSBarry Smith 
1186665c2dedSJed Brown #include <petscviewertypes.h>
1187639ff905SBarry Smith #include <petscoptions.h>
1188639ff905SBarry Smith 
1189608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocTraceSet(PetscViewer, PetscBool, PetscLogDouble);
1190608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocTraceGet(PetscBool *);
1191608c71bfSMatthew G. Knepley 
11920633abcbSJed Brown PETSC_EXTERN PetscErrorCode PetscObjectsListGetGlobalNumbering(MPI_Comm, PetscInt, PetscObject *, PetscInt *, PetscInt *);
11930633abcbSJed Brown 
1194c5e4d11fSDmitry Karpeev PETSC_EXTERN PetscErrorCode PetscMemoryView(PetscViewer, const char[]);
1195dae58748SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject, PetscViewer);
1196639ff905SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectView(PetscObject, PetscViewer);
11970005d66cSJed Brown #define PetscObjectQueryFunction(obj, name, fptr) PetscObjectQueryFunction_Private((obj), (name), (PetscVoidFunction *)(fptr))
11980005d66cSJed Brown PETSC_EXTERN PetscErrorCode PetscObjectQueryFunction_Private(PetscObject, const char[], void (**)(void));
1199014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetOptionsPrefix(PetscObject, const char[]);
1200014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject, const char[]);
1201014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject, const char[]);
1202014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject, const char *[]);
1203014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectChangeTypeName(PetscObject, const char[]);
1204014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroy(PetscObject);
1205014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroyAll(void);
1206685405a1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectViewFromOptions(PetscObject, PetscObject, const char[]);
1207014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectName(PetscObject);
1208014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectTypeCompare(PetscObject, const char[], PetscBool *);
1209013e2dc7SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectObjectTypeCompare(PetscObject, PetscObject, PetscBool *);
12104099cc6bSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompare(PetscObject, const char[], PetscBool *);
1211014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1212b9e7e5c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1213014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*)(void));
1214014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRegisterFinalizeAll(void);
1215d382aafbSBarry Smith 
1216e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
12177aab2a10SBarry Smith PETSC_EXTERN PetscErrorCode PetscSAWsBlock(void);
1218e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsViewOff(PetscObject);
1219e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsSetBlock(PetscObject, PetscBool);
1220e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsBlock(PetscObject);
1221e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsGrantAccess(PetscObject);
1222e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsTakeAccess(PetscObject);
1223e04113cfSBarry Smith PETSC_EXTERN void           PetscStackSAWsGrantAccess(void);
1224e04113cfSBarry Smith PETSC_EXTERN void           PetscStackSAWsTakeAccess(void);
1225e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscStackViewSAWs(void);
1226e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscStackSAWsViewOff(void);
122715681b3cSBarry Smith 
1228ec7429eaSBarry Smith #else
12293ba16761SJacob Faibussowitsch   #define PetscSAWsBlock()                  PETSC_SUCCESS
12303ba16761SJacob Faibussowitsch   #define PetscObjectSAWsViewOff(obj)       PETSC_SUCCESS
12313ba16761SJacob Faibussowitsch   #define PetscObjectSAWsSetBlock(obj, flg) PETSC_SUCCESS
12323ba16761SJacob Faibussowitsch   #define PetscObjectSAWsBlock(obj)         PETSC_SUCCESS
12333ba16761SJacob Faibussowitsch   #define PetscObjectSAWsGrantAccess(obj)   PETSC_SUCCESS
12343ba16761SJacob Faibussowitsch   #define PetscObjectSAWsTakeAccess(obj)    PETSC_SUCCESS
12353ba16761SJacob Faibussowitsch   #define PetscStackViewSAWs()              PETSC_SUCCESS
12363ba16761SJacob Faibussowitsch   #define PetscStackSAWsViewOff()           PETSC_SUCCESS
1237e04113cfSBarry Smith   #define PetscStackSAWsTakeAccess()
1238e04113cfSBarry Smith   #define PetscStackSAWsGrantAccess()
123915681b3cSBarry Smith 
1240ec7429eaSBarry Smith #endif
1241b90c6cbeSBarry Smith 
124282b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLOpen(const char[], PetscDLMode, PetscDLHandle *);
124382b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLClose(PetscDLHandle *);
124482b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLSym(PetscDLHandle, const char[], void **);
1245258ec3d2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscDLAddr(void (*)(void), char **);
1246258ec3d2SMatthew G. Knepley #ifdef PETSC_HAVE_CXX
1247258ec3d2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscDemangleSymbol(const char *, char **);
1248258ec3d2SMatthew G. Knepley #endif
12497d5d4d99SBarry Smith 
1250a64a8e02SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocGetStack(void *, PetscStack **);
1251a64a8e02SBarry Smith 
1252dfb7d7afSStefano Zampini PETSC_EXTERN PetscErrorCode PetscObjectsDump(FILE *, PetscBool);
1253140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListDestroy(PetscObjectList *);
1254140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListFind(PetscObjectList, const char[], PetscObject *);
1255140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListReverseFind(PetscObjectList, PetscObject, char **, PetscBool *);
1256140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListAdd(PetscObjectList *, const char[], PetscObject);
1257140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListRemoveReference(PetscObjectList *, const char[]);
1258140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListDuplicate(PetscObjectList, PetscObjectList *);
1259d382aafbSBarry Smith 
1260d382aafbSBarry Smith /*
1261503cfb0cSBarry Smith     Dynamic library lists. Lists of names of routines in objects or in dynamic
1262d382aafbSBarry Smith   link libraries that will be loaded as needed.
1263d382aafbSBarry Smith */
1264a240a19fSJed Brown 
1265a240a19fSJed Brown #define PetscFunctionListAdd(list, name, fptr) PetscFunctionListAdd_Private((list), (name), (PetscVoidFunction)(fptr))
1266ed170139SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *, const char[], PetscVoidFunction);
1267140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListDestroy(PetscFunctionList *);
12680e6b6b59SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFunctionListClear(PetscFunctionList);
12691c9cd337SJed Brown #define PetscFunctionListFind(list, name, fptr) PetscFunctionListFind_Private((list), (name), (PetscVoidFunction *)(fptr))
1270ed170139SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList, const char[], PetscVoidFunction *);
127144ef3d73SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListPrintTypes(MPI_Comm, FILE *, const char[], const char[], const char[], const char[], PetscFunctionList, const char[], const char[]);
1272140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListDuplicate(PetscFunctionList, PetscFunctionList *);
1273140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListView(PetscFunctionList, PetscViewer);
1274140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListGet(PetscFunctionList, const char ***, int *);
12752e956fe4SStefano Zampini PETSC_EXTERN PetscErrorCode PetscFunctionListPrintNonEmpty(PetscFunctionList);
12762e956fe4SStefano Zampini PETSC_EXTERN PetscErrorCode PetscFunctionListPrintAll(void);
1277d382aafbSBarry Smith 
1278014dd563SJed Brown PETSC_EXTERN PetscDLLibrary PetscDLLibrariesLoaded;
1279014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryAppend(MPI_Comm, PetscDLLibrary *, const char[]);
1280014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryPrepend(MPI_Comm, PetscDLLibrary *, const char[]);
1281014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibrarySym(MPI_Comm, PetscDLLibrary *, const char[], const char[], void **);
1282014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryPrintPath(PetscDLLibrary);
1283014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryRetrieve(MPI_Comm, const char[], char *, size_t, PetscBool *);
1284014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryOpen(MPI_Comm, const char[], PetscDLLibrary *);
1285014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryClose(PetscDLLibrary);
1286d382aafbSBarry Smith 
1287d382aafbSBarry Smith /*
1288d382aafbSBarry Smith      Useful utility routines
1289d382aafbSBarry Smith */
1290014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSplitOwnership(MPI_Comm, PetscInt *, PetscInt *);
1291014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSplitOwnershipBlock(MPI_Comm, PetscInt, PetscInt *, PetscInt *);
1292d24d4204SJose E. Roman PETSC_EXTERN PetscErrorCode PetscSplitOwnershipEqual(MPI_Comm, PetscInt *, PetscInt *);
1293014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSequentialPhaseBegin(MPI_Comm, PetscMPIInt);
1294014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSequentialPhaseEnd(MPI_Comm, PetscMPIInt);
1295014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBarrier(PetscObject);
1296014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIDump(FILE *);
129758b5cd2aSSatish Balay PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxInt(MPI_Comm, const PetscInt[2], PetscInt[2]);
129858b5cd2aSSatish Balay PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxReal(MPI_Comm, const PetscReal[2], PetscReal[2]);
1299d382aafbSBarry Smith 
130064ac3b0dSPatrick Sanan /*MC
130187497f52SBarry Smith     PetscNot - negates a logical type value and returns result as a `PetscBool`
1302503cfb0cSBarry Smith 
130364ac3b0dSPatrick Sanan     Level: beginner
130464ac3b0dSPatrick Sanan 
1305a1cb98faSBarry Smith     Note:
1306a1cb98faSBarry Smith     This is useful in cases like
1307a1cb98faSBarry Smith .vb
1308a1cb98faSBarry Smith      int        *a;
1309a1cb98faSBarry Smith      PetscBool  flag = PetscNot(a)
1310a1cb98faSBarry Smith .ve
1311a1cb98faSBarry Smith      where !a would not return a `PetscBool` because we cannot provide a cast from int to `PetscBool` in C.
1312a1cb98faSBarry Smith 
1313a1cb98faSBarry Smith .seealso: `PetscBool`, `PETSC_TRUE`, `PETSC_FALSE`
131464ac3b0dSPatrick Sanan M*/
1315d382aafbSBarry Smith #define PetscNot(a) ((a) ? PETSC_FALSE : PETSC_TRUE)
1316503cfb0cSBarry Smith 
1317d382aafbSBarry Smith /*MC
1318d382aafbSBarry Smith     PetscHelpPrintf - Prints help messages.
1319d382aafbSBarry Smith 
1320cd05f99aSJacob Faibussowitsch     Not Collective, only applies on rank 0; No Fortran Support
1321cf53795eSBarry Smith 
1322d382aafbSBarry Smith    Synopsis:
1323aaa7dc30SBarry Smith     #include <petscsys.h>
1324659f7ba0SBarry Smith      PetscErrorCode (*PetscHelpPrintf)(MPI_Comm comm, const char format[],args);
1325d382aafbSBarry Smith 
1326d382aafbSBarry Smith     Input Parameters:
1327659f7ba0SBarry Smith +  comm - the MPI communicator over which the help message is printed
1328d382aafbSBarry Smith .  format - the usual printf() format string
1329659f7ba0SBarry Smith -  args - arguments to be printed
1330d382aafbSBarry Smith 
1331d382aafbSBarry Smith    Level: developer
1332d382aafbSBarry Smith 
1333659f7ba0SBarry Smith    Note:
1334659f7ba0SBarry Smith      You can change how help messages are printed by replacing the function pointer with a function that does not simply write to stdout.
1335659f7ba0SBarry Smith 
1336659f7ba0SBarry Smith       To use, write your own function, for example,
1337659f7ba0SBarry Smith $PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....)
1338659f7ba0SBarry Smith ${
13393ba16761SJacob Faibussowitsch $ PetscFunctionReturn(PETSC_SUCCESS);
1340659f7ba0SBarry Smith $}
1341d5b43468SJose E. Roman then do the assignment
1342659f7ba0SBarry Smith $    PetscHelpPrintf = mypetschelpprintf;
134387497f52SBarry Smith    You can do the assignment before `PetscInitialize()`.
1344659f7ba0SBarry Smith 
134587497f52SBarry Smith   The default routine used is called `PetscHelpPrintfDefault()`.
1346d382aafbSBarry Smith 
1347db781477SPatrick Sanan .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscErrorPrintf()`
1348d382aafbSBarry Smith M*/
13493ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode (*PetscHelpPrintf)(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1350d382aafbSBarry Smith 
1351fcfd50ebSBarry Smith /*
1352fcfd50ebSBarry Smith      Defines PETSc profiling.
1353fcfd50ebSBarry Smith */
13542c8e378dSBarry Smith #include <petsclog.h>
1355fcfd50ebSBarry Smith 
1356fcfd50ebSBarry Smith /*
1357fcfd50ebSBarry Smith       Simple PETSc parallel IO for ASCII printing
1358fcfd50ebSBarry Smith */
1359014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFixFilename(const char[], char[]);
1360014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFOpen(MPI_Comm, const char[], const char[], FILE **);
1361014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFClose(MPI_Comm, FILE *);
13623ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
1363*c69effb2SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFFlush(FILE *);
13643ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
13653ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSNPrintf(char *, size_t, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
13663ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSNPrintfCount(char *, size_t, const char[], size_t *, ...) PETSC_ATTRIBUTE_FORMAT(3, 5);
13671b5687a1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatRealArray(char[], size_t, const char *, PetscInt, const PetscReal[]);
1368fcfd50ebSBarry Smith 
13693ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscErrorPrintfDefault(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
13703ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscErrorPrintfNone(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
13713ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscHelpPrintfDefault(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1372d382aafbSBarry Smith 
1373d781fa04SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatConvertGetSize(const char *, size_t *);
1374d781fa04SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatConvert(const char *, char *);
1375d781fa04SBarry Smith 
1376d382aafbSBarry Smith #if defined(PETSC_HAVE_POPEN)
1377014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPOpen(MPI_Comm, const char[], const char[], const char[], FILE **);
1378016831caSBarry Smith PETSC_EXTERN PetscErrorCode PetscPClose(MPI_Comm, FILE *);
137974ba8654SBarry Smith PETSC_EXTERN PetscErrorCode PetscPOpenSetMachine(const char[]);
1380d382aafbSBarry Smith #endif
1381d382aafbSBarry Smith 
13823ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSynchronizedPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
13833ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
13840ec8b6e3SBarry Smith PETSC_EXTERN PetscErrorCode PetscSynchronizedFlush(MPI_Comm, FILE *);
1385014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSynchronizedFGets(MPI_Comm, FILE *, size_t, char[]);
1386014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStartMatlab(MPI_Comm, const char[], const char[], FILE **);
1387014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStartJava(MPI_Comm, const char[], const char[], FILE **);
1388014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetPetscDir(const char *[]);
1389d382aafbSBarry Smith 
1390014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_CONTAINER_CLASSID;
1391014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerGetPointer(PetscContainer, void **);
1392014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerSetPointer(PetscContainer, void *);
1393014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerDestroy(PetscContainer *);
1394014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerCreate(MPI_Comm, PetscContainer *);
1395014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerSetUserDestroy(PetscContainer, PetscErrorCode (*)(void *));
1396b61ba218SStefano Zampini PETSC_EXTERN PetscErrorCode PetscContainerUserDestroyDefault(void *);
1397d382aafbSBarry Smith 
1398d382aafbSBarry Smith /*
1399d382aafbSBarry Smith    For use in debuggers
1400d382aafbSBarry Smith */
1401014dd563SJed Brown PETSC_EXTERN PetscMPIInt    PetscGlobalRank;
1402014dd563SJed Brown PETSC_EXTERN PetscMPIInt    PetscGlobalSize;
1403014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIntView(PetscInt, const PetscInt[], PetscViewer);
1404014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRealView(PetscInt, const PetscReal[], PetscViewer);
1405014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscScalarView(PetscInt, const PetscScalar[], PetscViewer);
1406d382aafbSBarry Smith 
1407bbcf679cSJacob Faibussowitsch /*
1408bbcf679cSJacob Faibussowitsch     Basic memory and string operations. These are usually simple wrappers
1409bbcf679cSJacob Faibussowitsch    around the basic Unix system calls, but a few of them have additional
1410bbcf679cSJacob Faibussowitsch    functionality and/or error checking.
1411bbcf679cSJacob Faibussowitsch */
1412bbcf679cSJacob Faibussowitsch #include <petscstring.h>
1413bbcf679cSJacob Faibussowitsch 
1414a663daf8SBarry Smith #include <stddef.h>
1415d382aafbSBarry Smith #include <stdlib.h>
14166c7e564aSBarry Smith 
14174a92a979SJed Brown #if defined(PETSC_HAVE_XMMINTRIN_H) && !defined(__CUDACC__)
1418d382aafbSBarry Smith   #include <xmmintrin.h>
1419d382aafbSBarry Smith #endif
1420d382aafbSBarry Smith 
1421447bcd8fSJacob Faibussowitsch #if defined(PETSC_CLANG_STATIC_ANALYZER)
1422447bcd8fSJacob Faibussowitsch   #define PetscPrefetchBlock(a, b, c, d)
1423447bcd8fSJacob Faibussowitsch #else
1424d382aafbSBarry Smith   /*MC
1425d382aafbSBarry Smith    PetscPrefetchBlock - Prefetches a block of memory
1426d382aafbSBarry Smith 
1427eca87e8dSBarry Smith    Synopsis:
1428aaa7dc30SBarry Smith     #include <petscsys.h>
1429eca87e8dSBarry Smith     void PetscPrefetchBlock(const anytype *a,size_t n,int rw,int t)
1430eca87e8dSBarry Smith 
1431d382aafbSBarry Smith    Not Collective
1432d382aafbSBarry Smith 
1433d382aafbSBarry Smith    Input Parameters:
1434d382aafbSBarry Smith +  a - pointer to first element to fetch (any type but usually PetscInt or PetscScalar)
1435d382aafbSBarry Smith .  n - number of elements to fetch
1436d382aafbSBarry Smith .  rw - 1 if the memory will be written to, otherwise 0 (ignored by many processors)
143750d8bf02SJed Brown -  t - temporal locality (PETSC_PREFETCH_HINT_{NTA,T0,T1,T2}), see note
1438d382aafbSBarry Smith 
1439d382aafbSBarry Smith    Level: developer
1440d382aafbSBarry Smith 
1441d382aafbSBarry Smith    Notes:
1442d382aafbSBarry Smith    The last two arguments (rw and t) must be compile-time constants.
1443d382aafbSBarry Smith 
144450d8bf02SJed Brown    Adopting Intel's x86/x86-64 conventions, there are four levels of temporal locality.  Not all architectures offer
144550d8bf02SJed Brown    equivalent locality hints, but the following macros are always defined to their closest analogue.
144687497f52SBarry Smith +  `PETSC_PREFETCH_HINT_NTA` - Non-temporal.  Prefetches directly to L1, evicts to memory (skips higher level cache unless it was already there when prefetched).
144787497f52SBarry Smith .  `PETSC_PREFETCH_HINT_T0` - Fetch to all levels of cache and evict to the closest level.  Use this when the memory will be reused regularly despite necessary eviction from L1.
144887497f52SBarry Smith .  `PETSC_PREFETCH_HINT_T1` - Fetch to level 2 and higher (not L1).
144987497f52SBarry Smith -  `PETSC_PREFETCH_HINT_T2` - Fetch to high-level cache only.  (On many systems, T0 and T1 are equivalent.)
1450d382aafbSBarry Smith 
1451d382aafbSBarry Smith    This function does nothing on architectures that do not support prefetch and never errors (even if passed an invalid
1452d382aafbSBarry Smith    address).
1453d382aafbSBarry Smith 
1454d382aafbSBarry Smith M*/
14559371c9d4SSatish Balay   #define PetscPrefetchBlock(a, n, rw, t) \
14569371c9d4SSatish Balay     do { \
1457d382aafbSBarry Smith       const char *_p = (const char *)(a), *_end = (const char *)((a) + (n)); \
1458d382aafbSBarry Smith       for (; _p < _end; _p += PETSC_LEVEL1_DCACHE_LINESIZE) PETSC_Prefetch(_p, (rw), (t)); \
1459d382aafbSBarry Smith     } while (0)
1460447bcd8fSJacob Faibussowitsch #endif
1461d382aafbSBarry Smith /*
1462d382aafbSBarry Smith       Determine if some of the kernel computation routines use
1463d382aafbSBarry Smith    Fortran (rather than C) for the numerical calculations. On some machines
1464d382aafbSBarry Smith    and compilers (like complex numbers) the Fortran version of the routines
1465d382aafbSBarry Smith    is faster than the C/C++ versions. The flag --with-fortran-kernels
1466e2e64c6bSBarry Smith    should be used with ./configure to turn these on.
1467d382aafbSBarry Smith */
1468d382aafbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNELS)
1469d382aafbSBarry Smith 
1470d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTCRL)
1471d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTCRL
1472d382aafbSBarry Smith   #endif
1473d382aafbSBarry Smith 
14745a11e1b2SBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJPERM)
14755a11e1b2SBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTAIJPERM
1476d382aafbSBarry Smith   #endif
1477d382aafbSBarry Smith 
1478d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1479d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTAIJ
1480d382aafbSBarry Smith   #endif
1481d382aafbSBarry Smith 
1482d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1483d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ
1484d382aafbSBarry Smith   #endif
1485d382aafbSBarry Smith 
1486d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_NORM)
1487d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_NORM
1488d382aafbSBarry Smith   #endif
1489d382aafbSBarry Smith 
1490d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MAXPY)
1491d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MAXPY
1492d382aafbSBarry Smith   #endif
1493d382aafbSBarry Smith 
1494d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
1495d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ
1496d382aafbSBarry Smith   #endif
1497d382aafbSBarry Smith 
1498d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
1499d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_RELAXAIJ
1500d382aafbSBarry Smith   #endif
1501d382aafbSBarry Smith 
1502d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ)
1503d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ
1504d382aafbSBarry Smith   #endif
1505d382aafbSBarry Smith 
1506d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1507d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ
1508d382aafbSBarry Smith   #endif
1509d382aafbSBarry Smith 
1510d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MDOT)
1511d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MDOT
1512d382aafbSBarry Smith   #endif
1513d382aafbSBarry Smith 
1514d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_XTIMESY)
1515d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_XTIMESY
1516d382aafbSBarry Smith   #endif
1517d382aafbSBarry Smith 
1518d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_AYPX)
1519d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_AYPX
1520d382aafbSBarry Smith   #endif
1521d382aafbSBarry Smith 
1522d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_WAXPY)
1523d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_WAXPY
1524d382aafbSBarry Smith   #endif
1525d382aafbSBarry Smith 
1526d382aafbSBarry Smith #endif
1527d382aafbSBarry Smith 
1528d382aafbSBarry Smith /*
1529d382aafbSBarry Smith     Macros for indicating code that should be compiled with a C interface,
1530d382aafbSBarry Smith    rather than a C++ interface. Any routines that are dynamically loaded
1531d382aafbSBarry Smith    (such as the PCCreate_XXX() routines) must be wrapped so that the name
1532d382aafbSBarry Smith    mangler does not change the functions symbol name. This just hides the
1533d382aafbSBarry Smith    ugly extern "C" {} wrappers.
1534d382aafbSBarry Smith */
1535d382aafbSBarry Smith #if defined(__cplusplus)
1536d382aafbSBarry Smith   #define EXTERN_C_BEGIN extern "C" {
1537d382aafbSBarry Smith   #define EXTERN_C_END   }
1538d382aafbSBarry Smith #else
1539d382aafbSBarry Smith   #define EXTERN_C_BEGIN
1540d382aafbSBarry Smith   #define EXTERN_C_END
1541d382aafbSBarry Smith #endif
1542d382aafbSBarry Smith 
1543d382aafbSBarry Smith /* --------------------------------------------------------------------*/
1544d382aafbSBarry Smith 
1545d382aafbSBarry Smith /*MC
1546d382aafbSBarry Smith     MPI_Comm - the basic object used by MPI to determine which processes are involved in a
1547d382aafbSBarry Smith         communication
1548d382aafbSBarry Smith 
1549d382aafbSBarry Smith    Level: beginner
1550d382aafbSBarry Smith 
1551d382aafbSBarry Smith    Note: This manual page is a place-holder because MPICH does not have a manual page for MPI_Comm
1552d382aafbSBarry Smith 
1553db781477SPatrick Sanan .seealso: `PETSC_COMM_WORLD`, `PETSC_COMM_SELF`
1554d382aafbSBarry Smith M*/
1555d382aafbSBarry Smith 
1556d382aafbSBarry Smith #if defined(PETSC_HAVE_MPIIO)
155793d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_write_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(2, 4);
155893d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_read_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(2, 4);
155993d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_write_at(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(3, 5);
156093d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_read_at(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(3, 5);
156193d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_write_at_all(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(3, 5);
156293d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_read_at_all(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(3, 5);
1563d382aafbSBarry Smith #endif
1564d382aafbSBarry Smith 
156561b0d812SBarry Smith /*@C
156687497f52SBarry Smith     PetscIntCast - casts a `PetscInt64` (which is 64 bits in size) to a `PetscInt` (which may be 32 bits in size), generates an
156787497f52SBarry Smith          error if the `PetscInt` is not large enough to hold the number.
1568c73702f5SBarry Smith 
1569c73702f5SBarry Smith    Not Collective
1570c73702f5SBarry Smith 
1571c73702f5SBarry Smith    Input Parameter:
157287497f52SBarry Smith .     a - the `PetscInt64` value
1573c73702f5SBarry Smith 
1574c73702f5SBarry Smith    Output Parameter:
157587497f52SBarry Smith .     b - the resulting `PetscInt` value
1576c73702f5SBarry Smith 
1577c73702f5SBarry Smith    Level: advanced
1578c73702f5SBarry Smith 
157987497f52SBarry Smith    Notes:
158087497f52SBarry Smith    If integers needed for the applications are too large to fit in 32 bit ints you can ./configure using --with-64-bit-indices to make `PetscInt` use 64 bit ints
1581c73702f5SBarry Smith 
1582c73702f5SBarry Smith    Not available from Fortran
1583c73702f5SBarry Smith 
1584db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscIntMultError()`, `PetscIntSumError()`
1585c73702f5SBarry Smith @*/
1586d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscIntCast(PetscInt64 a, PetscInt *b)
1587d71ae5a4SJacob Faibussowitsch {
1588c73702f5SBarry Smith   PetscFunctionBegin;
15893ca90d2dSJacob Faibussowitsch   *b = 0;
1590f3fa974cSJacob Faibussowitsch   // if using 64-bit indices already then this comparison is tautologically true
1591f3fa974cSJacob Faibussowitsch   PetscCheck(a < PETSC_MAX_INT, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt64_FMT " is too big for PetscInt, you may need to ./configure using --with-64-bit-indices", a);
1592f3fa974cSJacob Faibussowitsch   *b = (PetscInt)a;
15933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1594c73702f5SBarry Smith }
1595c73702f5SBarry Smith 
1596c73702f5SBarry Smith /*@C
159787497f52SBarry Smith     PetscCountCast - casts a `PetscCount` to a `PetscInt` (which may be 32 bits in size), generates an
159887497f52SBarry Smith          error if the `PetscInt` is not large enough to hold the number.
1599981bb840SJunchao Zhang 
1600667f096bSBarry Smith    Not Collective; No Fortran Support
1601981bb840SJunchao Zhang 
1602981bb840SJunchao Zhang    Input Parameter:
160387497f52SBarry Smith .     a - the `PetscCount` value
1604981bb840SJunchao Zhang 
1605981bb840SJunchao Zhang    Output Parameter:
160687497f52SBarry Smith .     b - the resulting `PetscInt` value
1607981bb840SJunchao Zhang 
1608981bb840SJunchao Zhang    Level: advanced
1609981bb840SJunchao Zhang 
1610667f096bSBarry Smith    Note:
1611667f096bSBarry Smith      If integers needed for the applications are too large to fit in 32 bit ints you can ./configure using --with-64-bit-indices to make `PetscInt` use 64 bit ints
1612981bb840SJunchao Zhang 
1613667f096bSBarry Smith .seealso: `PetscCount`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscIntMultError()`, `PetscIntSumError()`, `PetscIntCast()`
1614981bb840SJunchao Zhang @*/
1615d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscCountCast(PetscCount a, PetscInt *b)
1616d71ae5a4SJacob Faibussowitsch {
1617981bb840SJunchao Zhang   PetscFunctionBegin;
1618981bb840SJunchao Zhang   *b = 0;
1619f3fa974cSJacob Faibussowitsch   PetscCheck(sizeof(PetscCount) <= sizeof(PetscInt) || a <= PETSC_MAX_INT, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscCount_FMT " is too big for PetscInt, you may need to ./configure using --with-64-bit-indices", a);
1620f3fa974cSJacob Faibussowitsch   *b = (PetscInt)a;
16213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1622981bb840SJunchao Zhang }
1623981bb840SJunchao Zhang 
1624981bb840SJunchao Zhang /*@C
162587497f52SBarry Smith     PetscBLASIntCast - casts a `PetscInt` (which may be 64 bits in size) to a `PetscBLASInt` (which may be 32 bits in size), generates an
162687497f52SBarry Smith          error if the `PetscBLASInt` is not large enough to hold the number.
162761b0d812SBarry Smith 
1628667f096bSBarry Smith    Not Collective; No Fortran Support
162961b0d812SBarry Smith 
163061b0d812SBarry Smith    Input Parameter:
163187497f52SBarry Smith .     a - the `PetscInt` value
163261b0d812SBarry Smith 
163361b0d812SBarry Smith    Output Parameter:
163487497f52SBarry Smith .     b - the resulting `PetscBLASInt` value
163561b0d812SBarry Smith 
163661b0d812SBarry Smith    Level: advanced
163761b0d812SBarry Smith 
1638667f096bSBarry Smith    Note:
1639f0b7f91aSBarry Smith    Errors if the integer is negative since PETSc calls to BLAS/LAPACK never need to cast negative integer inputs
16401fd49c25SBarry Smith 
1641db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscIntCast()`, `PetscCountCast()`
164261b0d812SBarry Smith @*/
1643d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscBLASIntCast(PetscInt a, PetscBLASInt *b)
1644d71ae5a4SJacob Faibussowitsch {
1645c5df96a5SBarry Smith   PetscFunctionBegin;
164654c59aa7SJacob Faibussowitsch   *b = 0;
164754c59aa7SJacob Faibussowitsch   if (PetscDefined(USE_64BIT_INDICES) && !PetscDefined(HAVE_64BIT_BLAS_INDICES)) {
164854c59aa7SJacob Faibussowitsch     PetscCheck(a <= PETSC_BLAS_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt_FMT " is too big for BLAS/LAPACK, which is restricted to 32 bit integers. Either you have an invalidly large integer error in your code or you must ./configure PETSc with --with-64-bit-blas-indices for the case you are running", a);
164954c59aa7SJacob Faibussowitsch   }
165054c59aa7SJacob Faibussowitsch   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer to BLAS/LAPACK routine");
1651f3fa974cSJacob Faibussowitsch   *b = (PetscBLASInt)a;
16523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1653c5df96a5SBarry Smith }
1654c5df96a5SBarry Smith 
165561b0d812SBarry Smith /*@C
165687497f52SBarry Smith     PetscCuBLASIntCast - like `PetscBLASIntCast()`, but for `PetscCuBLASInt`.
1657eee0c0a6SToby Isaac 
1658667f096bSBarry Smith    Not Collective; No Fortran Support
1659eee0c0a6SToby Isaac 
1660eee0c0a6SToby Isaac    Input Parameter:
166187497f52SBarry Smith .     a - the `PetscInt` value
1662eee0c0a6SToby Isaac 
1663eee0c0a6SToby Isaac    Output Parameter:
166487497f52SBarry Smith .     b - the resulting `PetscCuBLASInt` value
1665eee0c0a6SToby Isaac 
1666eee0c0a6SToby Isaac    Level: advanced
1667eee0c0a6SToby Isaac 
1668667f096bSBarry Smith    Note:
1669eee0c0a6SToby Isaac       Errors if the integer is negative since PETSc calls to cuBLAS and friends never need to cast negative integer inputs
1670eee0c0a6SToby Isaac 
1671db781477SPatrick Sanan .seealso: `PetscCuBLASInt`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscMPIIntCast()`, `PetscIntCast()`
1672eee0c0a6SToby Isaac @*/
1673d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscCuBLASIntCast(PetscInt a, PetscCuBLASInt *b)
1674d71ae5a4SJacob Faibussowitsch {
1675eee0c0a6SToby Isaac   PetscFunctionBegin;
167654c59aa7SJacob Faibussowitsch   *b = 0;
1677f3fa974cSJacob Faibussowitsch   PetscCheck(a <= PETSC_CUBLAS_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt_FMT " is too big for cuBLAS, which is restricted to 32 bit integers.", a);
1678f3fa974cSJacob Faibussowitsch   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer %" PetscInt_FMT "to cuBLAS routine", a);
1679f3fa974cSJacob Faibussowitsch   *b = (PetscCuBLASInt)a;
16803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1681eee0c0a6SToby Isaac }
1682eee0c0a6SToby Isaac 
1683eee0c0a6SToby Isaac /*@C
168447d993e7Ssuyashtn     PetscHipBLASIntCast - like `PetscBLASIntCast()`, but for `PetscHipBLASInt`.
168547d993e7Ssuyashtn 
1686667f096bSBarry Smith    Not Collective; No Fortran Support
168747d993e7Ssuyashtn 
168847d993e7Ssuyashtn    Input Parameter:
168947d993e7Ssuyashtn .     a - the `PetscInt` value
169047d993e7Ssuyashtn 
169147d993e7Ssuyashtn    Output Parameter:
169247d993e7Ssuyashtn .     b - the resulting `PetscHipBLASInt` value
169347d993e7Ssuyashtn 
169447d993e7Ssuyashtn    Level: advanced
169547d993e7Ssuyashtn 
1696667f096bSBarry Smith    Note:
169747d993e7Ssuyashtn       Errors if the integer is negative since PETSc calls to hipBLAS and friends never need to cast negative integer inputs
169847d993e7Ssuyashtn 
169947d993e7Ssuyashtn .seealso: `PetscHipBLASInt`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscMPIIntCast()`, `PetscIntCast()`
170047d993e7Ssuyashtn @*/
170147d993e7Ssuyashtn static inline PetscErrorCode PetscHipBLASIntCast(PetscInt a, PetscHipBLASInt *b)
170247d993e7Ssuyashtn {
170347d993e7Ssuyashtn   PetscFunctionBegin;
170447d993e7Ssuyashtn   *b = 0;
1705f3fa974cSJacob Faibussowitsch   PetscCheck(a <= PETSC_HIPBLAS_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt_FMT " is too big for hipBLAS, which is restricted to 32 bit integers.", a);
1706f3fa974cSJacob Faibussowitsch   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer %" PetscInt_FMT "to hipBLAS routine", a);
1707f3fa974cSJacob Faibussowitsch   *b = (PetscHipBLASInt)a;
17083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
170947d993e7Ssuyashtn }
171047d993e7Ssuyashtn 
171147d993e7Ssuyashtn /*@C
1712667f096bSBarry Smith     PetscMPIIntCast - casts a `PetscInt` (which may be 64 bits in size) to a PetscMPIInt (which may be 32 bits in size), generates an
1713667f096bSBarry Smith          error if the `PetscMPIInt` is not large enough to hold the number.
171461b0d812SBarry Smith 
1715667f096bSBarry Smith    Not Collective; No Fortran Support
171661b0d812SBarry Smith 
171761b0d812SBarry Smith    Input Parameter:
171887497f52SBarry Smith .     a - the `PetscInt` value
171961b0d812SBarry Smith 
172061b0d812SBarry Smith    Output Parameter:
172187497f52SBarry Smith .     b - the resulting `PetscMPIInt` value
172261b0d812SBarry Smith 
172361b0d812SBarry Smith    Level: advanced
172461b0d812SBarry Smith 
1725db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscIntCast()`
172661b0d812SBarry Smith @*/
1727d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscMPIIntCast(PetscInt a, PetscMPIInt *b)
1728d71ae5a4SJacob Faibussowitsch {
17294dc2109aSBarry Smith   PetscFunctionBegin;
173054c59aa7SJacob Faibussowitsch   *b = 0;
1731f3fa974cSJacob Faibussowitsch   PetscCheck(a <= PETSC_MPI_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt_FMT " is too big for MPI buffer length. Maximum supported value is %d", a, PETSC_MPI_INT_MAX);
1732f3fa974cSJacob Faibussowitsch   *b = (PetscMPIInt)a;
17333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17344dc2109aSBarry Smith }
17354dc2109aSBarry Smith 
1736f3fa974cSJacob Faibussowitsch #define PetscInt64Mult(a, b) (((PetscInt64)(a)) * ((PetscInt64)(b)))
17372f18eb33SBarry Smith 
17382f18eb33SBarry Smith /*@C
1739f3fa974cSJacob Faibussowitsch   PetscRealIntMultTruncate - Computes the product of a positive `PetscReal` and a positive
1740f3fa974cSJacob Faibussowitsch   `PetscInt` and truncates the value to slightly less than the maximal possible value.
17412f18eb33SBarry Smith 
1742f3fa974cSJacob Faibussowitsch   Not Collective, Not available from Fortran
17432f18eb33SBarry Smith 
1744d8d19677SJose E. Roman   Input Parameters:
1745f3fa974cSJacob Faibussowitsch + a - The `PetscReal` value
1746f3fa974cSJacob Faibussowitsch - b - The `PetscInt` value
17472f18eb33SBarry Smith 
1748667f096bSBarry Smith   Level: advanced
1749667f096bSBarry Smith 
1750f3fa974cSJacob Faibussowitsch   Notes:
1751f3fa974cSJacob Faibussowitsch   Returns the result as a `PetscInt` value.
17522f18eb33SBarry Smith 
1753f3fa974cSJacob Faibussowitsch   Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`.
1754667f096bSBarry Smith 
1755f3fa974cSJacob Faibussowitsch   Use `PetscIntMultTruncate()` to compute the product of two positive `PetscInt` and truncate
1756f3fa974cSJacob Faibussowitsch   to fit a `PetscInt`.
1757667f096bSBarry Smith 
1758f3fa974cSJacob Faibussowitsch   Use `PetscIntMultError()` to compute the product of two `PetscInt` if you wish to generate an
1759f3fa974cSJacob Faibussowitsch   error if the result will not fit in a `PetscInt`.
17602f18eb33SBarry Smith 
1761667f096bSBarry Smith   Developers Notes:
1762f3fa974cSJacob Faibussowitsch   We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but
1763f3fa974cSJacob Faibussowitsch   requires many more checks.
17642f18eb33SBarry Smith 
1765f3fa974cSJacob Faibussowitsch   This is used where we compute approximate sizes for workspace and need to insure the
1766f3fa974cSJacob Faibussowitsch   workspace is index-able.
17671fd49c25SBarry Smith 
1768f3fa974cSJacob Faibussowitsch .seealso: `PetscReal`, `PetscInt`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`
17692f18eb33SBarry Smith @*/
1770d71ae5a4SJacob Faibussowitsch static inline PetscInt PetscRealIntMultTruncate(PetscReal a, PetscInt b)
1771d71ae5a4SJacob Faibussowitsch {
17725f80ce2aSJacob Faibussowitsch   PetscInt64 r = (PetscInt64)(a * (PetscReal)b);
17732f18eb33SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
17742f18eb33SBarry Smith   return (PetscInt)r;
17752f18eb33SBarry Smith }
17762f18eb33SBarry Smith 
17772f18eb33SBarry Smith /*@C
17782f18eb33SBarry Smith 
177987497f52SBarry Smith    PetscIntMultTruncate - Computes the product of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value
17802f18eb33SBarry Smith 
1781667f096bSBarry Smith    Not Collective; No Fortran Support
17822f18eb33SBarry Smith 
1783d8d19677SJose E. Roman    Input Parameters:
17842f18eb33SBarry Smith +     a - the PetscInt value
17852f18eb33SBarry Smith -     b - the second value
17862f18eb33SBarry Smith 
1787390e1bf2SBarry Smith    Returns:
178887497f52SBarry Smith       the result as a `PetscInt` value
17892f18eb33SBarry Smith 
1790667f096bSBarry Smith    Level: advanced
1791667f096bSBarry Smith 
1792667f096bSBarry Smith    Notes:
179387497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`
1794667f096bSBarry Smith 
179587497f52SBarry Smith    Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`
1796667f096bSBarry Smith 
179787497f52SBarry Smith    Use `PetscIntMultError()` to compute the product of two `PetscInt` if you wish to generate an error if the result will not fit in a `PetscInt`
17982f18eb33SBarry Smith 
1799667f096bSBarry Smith    Developers Notes:
180087497f52SBarry Smith    We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but requires many more checks.
18012f18eb33SBarry Smith 
18022f18eb33SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
18032f18eb33SBarry Smith 
1804667f096bSBarry Smith .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`,
1805667f096bSBarry Smith           `PetscIntSumTruncate()`
18062f18eb33SBarry Smith @*/
1807d71ae5a4SJacob Faibussowitsch static inline PetscInt PetscIntMultTruncate(PetscInt a, PetscInt b)
1808d71ae5a4SJacob Faibussowitsch {
18095f80ce2aSJacob Faibussowitsch   PetscInt64 r = PetscInt64Mult(a, b);
18102f18eb33SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
18112f18eb33SBarry Smith   return (PetscInt)r;
18122f18eb33SBarry Smith }
18132f18eb33SBarry Smith 
1814f91af8c7SBarry Smith /*@C
1815f91af8c7SBarry Smith 
181687497f52SBarry Smith    PetscIntSumTruncate - Computes the sum of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value
1817f91af8c7SBarry Smith 
1818667f096bSBarry Smith    Not Collective; No Fortran Support
1819f91af8c7SBarry Smith 
1820d8d19677SJose E. Roman    Input Parameters:
182187497f52SBarry Smith +     a - the `PetscInt` value
1822f91af8c7SBarry Smith -     b - the second value
1823f91af8c7SBarry Smith 
1824390e1bf2SBarry Smith    Returns:
182587497f52SBarry Smith      the result as a `PetscInt` value
1826f91af8c7SBarry Smith 
1827667f096bSBarry Smith    Level: advanced
1828667f096bSBarry Smith 
1829667f096bSBarry Smith    Notes:
183087497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`
1831667f096bSBarry Smith 
183287497f52SBarry Smith    Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`
1833667f096bSBarry Smith 
183487497f52SBarry Smith    Use `PetscIntMultError()` to compute the product of two `PetscInt` if you wish to generate an error if the result will not fit in a `PetscInt`
1835f91af8c7SBarry Smith 
1836667f096bSBarry Smith    Developers Notes:
1837f91af8c7SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
1838f91af8c7SBarry Smith 
1839db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
1840f91af8c7SBarry Smith @*/
1841d71ae5a4SJacob Faibussowitsch static inline PetscInt PetscIntSumTruncate(PetscInt a, PetscInt b)
1842d71ae5a4SJacob Faibussowitsch {
18435f80ce2aSJacob Faibussowitsch   PetscInt64 r = ((PetscInt64)a) + ((PetscInt64)b);
1844f91af8c7SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
1845f91af8c7SBarry Smith   return (PetscInt)r;
1846f91af8c7SBarry Smith }
1847f91af8c7SBarry Smith 
18482f18eb33SBarry Smith /*@C
18492f18eb33SBarry Smith 
185087497f52SBarry Smith    PetscIntMultError - Computes the product of two positive `PetscInt` and generates an error with overflow.
18512f18eb33SBarry Smith 
1852667f096bSBarry Smith    Not Collective; No Fortran Support
18532f18eb33SBarry Smith 
1854d8d19677SJose E. Roman    Input Parameters:
185587497f52SBarry Smith +     a - the `PetscInt` value
18562f18eb33SBarry Smith -     b - the second value
18572f18eb33SBarry Smith 
1858d8d19677SJose E. Roman    Output Parameter:
185987497f52SBarry Smith .     result - the result as a `PetscInt` value, or NULL if you do not want the result, you just want to check if it overflows
18602f18eb33SBarry Smith 
1861667f096bSBarry Smith    Level: advanced
18622f18eb33SBarry Smith 
1863667f096bSBarry Smith    Notes:
1864667f096bSBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` and store in a `PetscInt64`
1865667f096bSBarry Smith 
1866667f096bSBarry Smith    Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`
1867f5f57ec0SBarry Smith 
186887497f52SBarry Smith    Developers Note:
186987497f52SBarry Smith    We currently assume that `PetscInt` addition does not overflow, this is obviously wrong but requires many more checks.
18702f18eb33SBarry Smith 
1871db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscIntMult64()`, `PetscIntSumError()`
18722f18eb33SBarry Smith @*/
1873d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscIntMultError(PetscInt a, PetscInt b, PetscInt *result)
1874d71ae5a4SJacob Faibussowitsch {
1875f3fa974cSJacob Faibussowitsch   PetscInt64 r = PetscInt64Mult(a, b);
18762f18eb33SBarry Smith 
18772f18eb33SBarry Smith   PetscFunctionBegin;
1878f3fa974cSJacob Faibussowitsch   if (!PetscDefined(USE_64BIT_INDICES)) {
1879f3fa974cSJacob Faibussowitsch     PetscCheck(r <= PETSC_MAX_INT, PETSC_COMM_SELF, PETSC_ERR_SUP, "Product of two integers %" PetscInt_FMT " %" PetscInt_FMT " overflow, either you have an invalidly large integer error in your code or you must ./configure PETSc with --with-64-bit-indices for the case you are running", a, b);
1880f3fa974cSJacob Faibussowitsch   }
1881f91af8c7SBarry Smith   if (result) *result = (PetscInt)r;
18823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1883f91af8c7SBarry Smith }
1884f91af8c7SBarry Smith 
1885f91af8c7SBarry Smith /*@C
1886f91af8c7SBarry Smith 
188787497f52SBarry Smith    PetscIntSumError - Computes the sum of two positive `PetscInt` and generates an error with overflow.
1888f91af8c7SBarry Smith 
1889667f096bSBarry Smith    Not Collective; No Fortran Support
1890f91af8c7SBarry Smith 
1891d8d19677SJose E. Roman    Input Parameters:
189287497f52SBarry Smith +     a - the `PetscInt` value
1893f91af8c7SBarry Smith -     b - the second value
1894f91af8c7SBarry Smith 
1895d8d19677SJose E. Roman    Output Parameter:
189687497f52SBarry Smith .     c - the result as a `PetscInt` value,  or NULL if you do not want the result, you just want to check if it overflows
1897f91af8c7SBarry Smith 
1898f91af8c7SBarry Smith    Level: advanced
1899f91af8c7SBarry Smith 
1900667f096bSBarry Smith    Notes:
1901667f096bSBarry Smith    Use `PetscInt64Mult()` to compute the product of two 32 bit PetscInt and store in a `PetscInt64`
1902667f096bSBarry Smith 
1903667f096bSBarry Smith    Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`
1904667f096bSBarry Smith 
1905db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
1906f91af8c7SBarry Smith @*/
1907d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscIntSumError(PetscInt a, PetscInt b, PetscInt *result)
1908d71ae5a4SJacob Faibussowitsch {
1909f3fa974cSJacob Faibussowitsch   PetscInt64 r = ((PetscInt64)a) + ((PetscInt64)b);
1910f91af8c7SBarry Smith 
1911f91af8c7SBarry Smith   PetscFunctionBegin;
1912f3fa974cSJacob Faibussowitsch   if (!PetscDefined(USE_64BIT_INDICES)) {
1913f3fa974cSJacob Faibussowitsch     PetscCheck(r <= PETSC_MAX_INT, PETSC_COMM_SELF, PETSC_ERR_SUP, "Sum of two integers %" PetscInt_FMT " %" PetscInt_FMT " overflow, either you have an invalidly large integer error in your code or you must ./configure PETSc with --with-64-bit-indices for the case you are running", a, b);
1914f3fa974cSJacob Faibussowitsch   }
1915f91af8c7SBarry Smith   if (result) *result = (PetscInt)r;
19163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19172f18eb33SBarry Smith }
1918c5df96a5SBarry Smith 
1919d382aafbSBarry Smith /*
19202981ebdbSBarry Smith      The IBM include files define hz, here we hide it so that it may be used as a regular user variable.
1921d382aafbSBarry Smith */
1922d382aafbSBarry Smith #if defined(hz)
1923d382aafbSBarry Smith   #undef hz
1924d382aafbSBarry Smith #endif
1925d382aafbSBarry Smith 
192615a5570dSLisandro Dalcin #include <limits.h>
192715a5570dSLisandro Dalcin 
192815a5570dSLisandro Dalcin /* The number of bits in a byte */
192915a5570dSLisandro Dalcin 
193015a5570dSLisandro Dalcin #define PETSC_BITS_PER_BYTE CHAR_BIT
193115a5570dSLisandro Dalcin 
1932d382aafbSBarry Smith #if defined(PETSC_HAVE_SYS_TYPES_H)
1933d382aafbSBarry Smith   #include <sys/types.h>
1934d382aafbSBarry Smith #endif
1935d382aafbSBarry Smith 
1936d382aafbSBarry Smith /*MC
1937d382aafbSBarry Smith 
193866d79e26SBarry Smith     PETSC_VERSION - This manual page provides information about how PETSc documents and uses its version information. This information is available to both C/C++
193966d79e26SBarry Smith                     and Fortran compilers when petscsys.h is included.
194066d79e26SBarry Smith 
19416274ba02SSatish Balay     The current PETSc version and the API for accessing it are defined in <A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscversion.h.html">include/petscverson.html</A>
194266d79e26SBarry Smith 
194366d79e26SBarry Smith     The complete version number is given as the triple  PETSC_VERSION_MAJOR.PETSC_VERSION_MINOR.PETSC_VERSION_SUBMINOR (in short hand x.y.z)
194466d79e26SBarry Smith 
194566d79e26SBarry Smith     A change in the minor version number (y) indicates possible/likely changes in the PETSc API. Note this is different than with the semantic versioning convention
194666d79e26SBarry Smith     where only a change in the major version number (x) indicates a change in the API.
194766d79e26SBarry Smith 
194866d79e26SBarry Smith     A subminor greater than zero indicates a patch release. Version x.y.z maintains source and binary compatibility with version x.y.w for all z and w
194966d79e26SBarry Smith 
195066d79e26SBarry Smith     Use the macros PETSC_VERSION_EQ(x,y,z), PETSC_VERSION_LT(x,y,z), PETSC_VERSION_LE(x,y,z), PETSC_VERSION_GT(x,y,z),
195166d79e26SBarry Smith     PETSC_VERSION_GE(x,y,z) to determine if the current version is equal to, less than, less than or equal to, greater than or greater than or equal to a given
195266d79e26SBarry Smith     version number (x.y.z).
195366d79e26SBarry Smith 
195487497f52SBarry Smith     `PETSC_RELEASE_DATE` is the date the x.y version was released (i.e. the version before any patch releases)
195566d79e26SBarry Smith 
195687497f52SBarry Smith     `PETSC_VERSION_DATE` is the date the x.y.z version was released
195766d79e26SBarry Smith 
195887497f52SBarry Smith     `PETSC_VERSION_GIT` is the last git commit to the repository given in the form vx.y.z-wwwww
195966d79e26SBarry Smith 
196087497f52SBarry Smith     `PETSC_VERSION_DATE_GIT` is the date of the last git commit to the repository
196166d79e26SBarry Smith 
196287497f52SBarry Smith     `PETSC_VERSION_()` is deprecated and will eventually be removed.
196366d79e26SBarry Smith 
1964cbc12506SBarry Smith     Level: intermediate
196566d79e26SBarry Smith M*/
196666d79e26SBarry Smith 
1967014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArchType(char[], size_t);
1968014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetHostName(char[], size_t);
1969014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetUserName(char[], size_t);
1970014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetProgramName(char[], size_t);
1971014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetProgramName(const char[]);
1972014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetDate(char[], size_t);
197327710113SBarry Smith PETSC_EXTERN PetscErrorCode PetscGetVersion(char[], size_t);
19745f309d01SBarry Smith PETSC_EXTERN PetscErrorCode PetscGetVersionNumber(PetscInt *, PetscInt *, PetscInt *, PetscInt *);
1975d382aafbSBarry Smith 
19766a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedInt(PetscInt, const PetscInt[], PetscBool *);
197762e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscSortedInt64(PetscInt, const PetscInt64[], PetscBool *);
19786a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedMPIInt(PetscInt, const PetscMPIInt[], PetscBool *);
19796a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedReal(PetscInt, const PetscReal[], PetscBool *);
1980014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortInt(PetscInt, PetscInt[]);
198162e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscSortInt64(PetscInt, PetscInt64[]);
198262e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscSortCount(PetscInt, PetscCount[]);
1983ce605777SToby Isaac PETSC_EXTERN PetscErrorCode PetscSortReverseInt(PetscInt, PetscInt[]);
198422ab5688SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscSortedRemoveDupsInt(PetscInt *, PetscInt[]);
1985b6a18d21SVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedCheckDupsInt(PetscInt, const PetscInt[], PetscBool *);
1986014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsInt(PetscInt *, PetscInt[]);
1987f1cab4e1SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscCheckDupsInt(PetscInt, const PetscInt[], PetscBool *);
198860e03357SMatthew G Knepley PETSC_EXTERN PetscErrorCode PetscFindInt(PetscInt, PetscInt, const PetscInt[], PetscInt *);
1989d2aeb606SJed Brown PETSC_EXTERN PetscErrorCode PetscFindMPIInt(PetscMPIInt, PetscInt, const PetscMPIInt[], PetscInt *);
1990014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithPermutation(PetscInt, const PetscInt[], PetscInt[]);
1991014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortStrWithPermutation(PetscInt, const char *[], PetscInt[]);
1992014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithArray(PetscInt, PetscInt[], PetscInt[]);
1993981bb840SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortIntWithCountArray(PetscCount, PetscInt[], PetscCount[]);
19946c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscSortIntWithArrayPair(PetscInt, PetscInt[], PetscInt[], PetscInt[]);
1995981bb840SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortIntWithIntCountArrayPair(PetscCount, PetscInt[], PetscInt[], PetscCount[]);
199617d7d925SStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortMPIInt(PetscInt, PetscMPIInt[]);
199717d7d925SStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsMPIInt(PetscInt *, PetscMPIInt[]);
1998014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithArray(PetscMPIInt, PetscMPIInt[], PetscMPIInt[]);
19995569a785SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithIntArray(PetscMPIInt, PetscMPIInt[], PetscInt[]);
2000014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithScalarArray(PetscInt, PetscInt[], PetscScalar[]);
200117df18f2SToby Isaac PETSC_EXTERN PetscErrorCode PetscSortIntWithDataArray(PetscInt, PetscInt[], void *, size_t, void *);
2002014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortReal(PetscInt, PetscReal[]);
200339f41f7fSStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortRealWithArrayInt(PetscInt, PetscReal[], PetscInt[]);
2004014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortRealWithPermutation(PetscInt, const PetscReal[], PetscInt[]);
2005745b41b2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsReal(PetscInt *, PetscReal[]);
200639f41f7fSStefano Zampini PETSC_EXTERN PetscErrorCode PetscFindReal(PetscReal, PetscInt, const PetscReal[], PetscReal, PetscInt *);
2007014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortSplit(PetscInt, PetscInt, PetscScalar[], PetscInt[]);
2008014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortSplitReal(PetscInt, PetscInt, PetscReal[], PetscInt[]);
2009014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscProcessTree(PetscInt, const PetscBool[], const PetscInt[], PetscInt *, PetscInt **, PetscInt **, PetscInt **, PetscInt **);
20106c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscMergeIntArrayPair(PetscInt, const PetscInt[], const PetscInt[], PetscInt, const PetscInt[], const PetscInt[], PetscInt *, PetscInt **, PetscInt **);
20116c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscMergeIntArray(PetscInt, const PetscInt[], PetscInt, const PetscInt[], PetscInt *, PetscInt **);
2012e498c390SJed Brown PETSC_EXTERN PetscErrorCode PetscMergeMPIIntArray(PetscInt, const PetscMPIInt[], PetscInt, const PetscMPIInt[], PetscInt *, PetscMPIInt **);
2013ce605777SToby Isaac PETSC_EXTERN PetscErrorCode PetscParallelSortedInt(MPI_Comm, PetscInt, const PetscInt[], PetscBool *);
2014ce605777SToby Isaac 
20154d3610e3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscTimSort(PetscInt, void *, size_t, int (*)(const void *, const void *, void *), void *);
2016676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrdered(PetscInt, PetscInt[]);
2017676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrdered(PetscInt, PetscMPIInt[]);
2018676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrdered(PetscInt, PetscReal[]);
20194d3610e3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscTimSortWithArray(PetscInt, void *, size_t, void *, size_t, int (*)(const void *, const void *, void *), void *);
2020676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrderedWithArray(PetscInt, PetscInt[], PetscInt[]);
2021676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrderedWithArray(PetscInt, PetscMPIInt[], PetscMPIInt[]);
2022676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrderedWithArrayInt(PetscInt, PetscReal[], PetscInt[]);
2023676f2a66SJacob Faibussowitsch 
2024014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDisplay(void);
2025014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetDisplay(char[], size_t);
2026d382aafbSBarry Smith 
202776bdecfbSBarry Smith /*J
2028d382aafbSBarry Smith     PetscRandomType - String with the name of a PETSc randomizer
2029d382aafbSBarry Smith 
2030d382aafbSBarry Smith    Level: beginner
2031d382aafbSBarry Smith 
2032667f096bSBarry Smith    Note:
203387497f52SBarry Smith    To use `PETSCSPRNG` or `PETSCRANDOM123` you must have ./configure PETSc
203425ccb61fSToby Isaac    with the option --download-sprng or --download-random123
2035d382aafbSBarry Smith 
2036db781477SPatrick Sanan .seealso: `PetscRandomSetType()`, `PetscRandom`, `PetscRandomCreate()`
203776bdecfbSBarry Smith J*/
203819fd82e9SBarry Smith typedef const char *PetscRandomType;
2039d382aafbSBarry Smith #define PETSCRAND      "rand"
2040d382aafbSBarry Smith #define PETSCRAND48    "rand48"
2041d382aafbSBarry Smith #define PETSCSPRNG     "sprng"
2042c5e4d11fSDmitry Karpeev #define PETSCRANDER48  "rander48"
204325ccb61fSToby Isaac #define PETSCRANDOM123 "random123"
2044808ba619SStefano Zampini #define PETSCCURAND    "curand"
2045d382aafbSBarry Smith 
2046d382aafbSBarry Smith /* Logging support */
2047014dd563SJed Brown PETSC_EXTERN PetscClassId PETSC_RANDOM_CLASSID;
2048d382aafbSBarry Smith 
2049607a6623SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomInitializePackage(void);
2050d382aafbSBarry Smith 
2051d382aafbSBarry Smith /* Dynamic creation and loading functions */
2052140e18c1SBarry Smith PETSC_EXTERN PetscFunctionList PetscRandomList;
2053d382aafbSBarry Smith 
2054bdf89e91SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomRegister(const char[], PetscErrorCode (*)(PetscRandom));
205519fd82e9SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomSetType(PetscRandom, PetscRandomType);
2056014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetFromOptions(PetscRandom);
205719fd82e9SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomGetType(PetscRandom, PetscRandomType *);
2058fe2efc57SMark PETSC_EXTERN PetscErrorCode PetscRandomViewFromOptions(PetscRandom, PetscObject, const char[]);
2059014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomView(PetscRandom, PetscViewer);
2060d382aafbSBarry Smith 
2061014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomCreate(MPI_Comm, PetscRandom *);
2062014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetValue(PetscRandom, PetscScalar *);
2063014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetValueReal(PetscRandom, PetscReal *);
2064808ba619SStefano Zampini PETSC_EXTERN PetscErrorCode PetscRandomGetValues(PetscRandom, PetscInt, PetscScalar *);
2065808ba619SStefano Zampini PETSC_EXTERN PetscErrorCode PetscRandomGetValuesReal(PetscRandom, PetscInt, PetscReal *);
2066014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetInterval(PetscRandom, PetscScalar *, PetscScalar *);
2067014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetInterval(PetscRandom, PetscScalar, PetscScalar);
2068014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetSeed(PetscRandom, unsigned long);
2069014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetSeed(PetscRandom, unsigned long *);
2070014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSeed(PetscRandom);
2071014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomDestroy(PetscRandom *);
2072d382aafbSBarry Smith 
2073014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetFullPath(const char[], char[], size_t);
2074014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetRelativePath(const char[], char[], size_t);
2075014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetWorkingDirectory(char[], size_t);
2076014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetRealPath(const char[], char[]);
2077014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetHomeDirectory(char[], size_t);
2078014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTestFile(const char[], char, PetscBool *);
2079014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTestDirectory(const char[], char, PetscBool *);
208097540f07SSatish Balay PETSC_EXTERN PetscErrorCode PetscMkdir(const char[]);
20818a10d460SHong Zhang PETSC_EXTERN PetscErrorCode PetscMkdtemp(char[]);
208297540f07SSatish Balay PETSC_EXTERN PetscErrorCode PetscRMTree(const char[]);
2083d382aafbSBarry Smith 
2084d71ae5a4SJacob Faibussowitsch static inline PetscBool PetscBinaryBigEndian(void)
2085d71ae5a4SJacob Faibussowitsch {
20869371c9d4SSatish Balay   long _petsc_v = 1;
20879371c9d4SSatish Balay   return ((char *)&_petsc_v)[0] ? PETSC_FALSE : PETSC_TRUE;
20889371c9d4SSatish Balay }
208930815ce0SLisandro Dalcin 
20909860990eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinaryRead(int, void *, PetscInt, PetscInt *, PetscDataType);
20919860990eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedRead(MPI_Comm, int, void *, PetscInt, PetscInt *, PetscDataType);
2092f253e43cSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinaryWrite(int, const void *, PetscInt, PetscDataType);
2093f253e43cSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedWrite(MPI_Comm, int, const void *, PetscInt, PetscDataType);
2094014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinaryOpen(const char[], PetscFileMode, int *);
2095014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinaryClose(int);
2096014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSharedTmp(MPI_Comm, PetscBool *);
2097014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSharedWorkingDirectory(MPI_Comm, PetscBool *);
2098014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetTmp(MPI_Comm, char[], size_t);
2099014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFileRetrieve(MPI_Comm, const char[], char[], size_t, PetscBool *);
2100014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLs(MPI_Comm, const char[], char[], size_t, PetscBool *);
2101c891a504SStefano Zampini #if defined(PETSC_USE_SOCKET_VIEWER)
2102e2fc02c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscOpenSocket(const char[], int, int *);
2103c891a504SStefano Zampini #endif
2104d382aafbSBarry Smith 
2105014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinarySeek(int, off_t, PetscBinarySeekType, off_t *);
2106014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedSeek(MPI_Comm, int, off_t, PetscBinarySeekType, off_t *);
2107014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscByteSwap(void *, PetscDataType, PetscInt);
2108d382aafbSBarry Smith 
2109014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebugTerminal(const char[]);
2110014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebugger(const char[], PetscBool);
2111014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDefaultDebugger(void);
2112014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebuggerFromString(const char *);
2113014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAttachDebugger(void);
2114014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStopForDebugger(void);
21152a2a2941SBarry Smith PETSC_EXTERN PetscErrorCode PetscWaitOnError(void);
2116d382aafbSBarry Smith 
2117014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherNumberOfMessages(MPI_Comm, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt *);
2118014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], PetscMPIInt **, PetscMPIInt **);
2119014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths2(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt **, PetscMPIInt **, PetscMPIInt **);
2120014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPostIrecvInt(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscInt ***, MPI_Request **);
2121014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPostIrecvScalar(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscScalar ***, MPI_Request **);
212293d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSided(MPI_Comm, PetscMPIInt, MPI_Datatype, PetscMPIInt, const PetscMPIInt *, const void *, PetscMPIInt *, PetscMPIInt **, void *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(6, 3);
212393d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedF(MPI_Comm, PetscMPIInt, MPI_Datatype, PetscMPIInt, const PetscMPIInt[], const void *, PetscMPIInt *, PetscMPIInt **, void *, PetscMPIInt, PetscErrorCode (*send)(MPI_Comm, const PetscMPIInt[], PetscMPIInt, PetscMPIInt, void *, MPI_Request[], void *), PetscErrorCode (*recv)(MPI_Comm, const PetscMPIInt[], PetscMPIInt, void *, MPI_Request[], void *), void *ctx) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(6, 3);
212493d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedFReq(MPI_Comm, PetscMPIInt, MPI_Datatype, PetscMPIInt, const PetscMPIInt[], const void *, PetscMPIInt *, PetscMPIInt **, void *, PetscMPIInt, MPI_Request **, MPI_Request **, PetscErrorCode (*send)(MPI_Comm, const PetscMPIInt[], PetscMPIInt, PetscMPIInt, void *, MPI_Request[], void *), PetscErrorCode (*recv)(MPI_Comm, const PetscMPIInt[], PetscMPIInt, void *, MPI_Request[], void *), void *ctx) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(6, 3);
2125d382aafbSBarry Smith 
21266145cd65SJed Brown PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedSetType(MPI_Comm, PetscBuildTwoSidedType);
21276145cd65SJed Brown PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedGetType(MPI_Comm, PetscBuildTwoSidedType *);
21286145cd65SJed Brown 
2129014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSSEIsEnabled(MPI_Comm, PetscBool *, PetscBool *);
2130d382aafbSBarry Smith 
2131ce94432eSBarry Smith PETSC_EXTERN MPI_Comm PetscObjectComm(PetscObject);
2132ce94432eSBarry Smith 
2133d382aafbSBarry Smith struct _n_PetscSubcomm {
2134d382aafbSBarry Smith   MPI_Comm         parent;    /* parent communicator */
2135d382aafbSBarry Smith   MPI_Comm         dupparent; /* duplicate parent communicator, under which the processors of this subcomm have contiguous rank */
2136306c2d5bSBarry Smith   MPI_Comm         child;     /* the sub-communicator */
213745487dadSJed Brown   PetscMPIInt      n;         /* num of subcommunicators under the parent communicator */
213845487dadSJed Brown   PetscMPIInt      color;     /* color of processors belong to this communicator */
2139708d824cSJed Brown   PetscMPIInt     *subsize;   /* size of subcommunicator[color] */
214045487dadSJed Brown   PetscSubcommType type;
2141e5acf8a4SHong Zhang   char            *subcommprefix;
2142d382aafbSBarry Smith };
2143d382aafbSBarry Smith 
2144d71ae5a4SJacob Faibussowitsch static inline MPI_Comm PetscSubcommParent(PetscSubcomm scomm)
2145d71ae5a4SJacob Faibussowitsch {
21469371c9d4SSatish Balay   return scomm->parent;
21479371c9d4SSatish Balay }
2148d71ae5a4SJacob Faibussowitsch static inline MPI_Comm PetscSubcommChild(PetscSubcomm scomm)
2149d71ae5a4SJacob Faibussowitsch {
21509371c9d4SSatish Balay   return scomm->child;
21519371c9d4SSatish Balay }
2152d71ae5a4SJacob Faibussowitsch static inline MPI_Comm PetscSubcommContiguousParent(PetscSubcomm scomm)
2153d71ae5a4SJacob Faibussowitsch {
21549371c9d4SSatish Balay   return scomm->dupparent;
21559371c9d4SSatish Balay }
2156014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommCreate(MPI_Comm, PetscSubcomm *);
2157014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommDestroy(PetscSubcomm *);
2158014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommSetNumber(PetscSubcomm, PetscInt);
2159014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommSetType(PetscSubcomm, PetscSubcommType);
216065d9b8f1SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetTypeGeneral(PetscSubcomm, PetscMPIInt, PetscMPIInt);
2161053d1c95SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommView(PetscSubcomm, PetscViewer);
2162f68be91cSHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetFromOptions(PetscSubcomm);
2163e5acf8a4SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetOptionsPrefix(PetscSubcomm, const char[]);
2164a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetParent(PetscSubcomm, MPI_Comm *);
2165a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetContiguousParent(PetscSubcomm, MPI_Comm *);
2166a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetChild(PetscSubcomm, MPI_Comm *);
2167d382aafbSBarry Smith 
21689962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapCreate(PetscInt, PetscHeap *);
21699962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapAdd(PetscHeap, PetscInt, PetscInt);
21709962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapPop(PetscHeap, PetscInt *, PetscInt *);
21719962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapPeek(PetscHeap, PetscInt *, PetscInt *);
21729962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapStash(PetscHeap, PetscInt, PetscInt);
21739962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapUnstash(PetscHeap);
21749962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapDestroy(PetscHeap *);
21759962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapView(PetscHeap, PetscViewer);
21769962606eSBarry Smith 
21775e71baefSBarry Smith PETSC_EXTERN PetscErrorCode PetscProcessPlacementView(PetscViewer);
21785f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGet(MPI_Comm, PetscShmComm *);
21795f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGlobalToLocal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
21805f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommLocalToGlobal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
21815f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGetMpiShmComm(PetscShmComm, MPI_Comm *);
21825e71baefSBarry Smith 
2183a32e93adSJunchao Zhang /* routines to better support OpenMP multithreading needs of some PETSc third party libraries */
2184a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlCreate(MPI_Comm, PetscInt, PetscOmpCtrl *);
2185a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlGetOmpComms(PetscOmpCtrl, MPI_Comm *, MPI_Comm *, PetscBool *);
2186a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlDestroy(PetscOmpCtrl *);
2187a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlBarrier(PetscOmpCtrl);
2188a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterBegin(PetscOmpCtrl);
2189a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterEnd(PetscOmpCtrl);
2190a32e93adSJunchao Zhang 
219113e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferCreate(size_t, size_t, PetscSegBuffer *);
21920f453b92SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferDestroy(PetscSegBuffer *);
219313e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferGet(PetscSegBuffer, size_t, void *);
2194137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractAlloc(PetscSegBuffer, void *);
2195137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractTo(PetscSegBuffer, void *);
2196137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractInPlace(PetscSegBuffer, void *);
219713e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferGetSize(PetscSegBuffer, size_t *);
219813e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferUnuse(PetscSegBuffer, size_t);
21990f453b92SJed Brown 
2200471ecdacSJed Brown /* Type-safe wrapper to encourage use of PETSC_RESTRICT. Does not use PetscFunctionBegin because the error handling
2201471ecdacSJed Brown  * prevents the compiler from completely erasing the stub. This is called in inner loops so it has to be as fast as
2202471ecdacSJed Brown  * possible. */
2203d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscSegBufferGetInts(PetscSegBuffer seg, size_t count, PetscInt *PETSC_RESTRICT *slot)
2204d71ae5a4SJacob Faibussowitsch {
22059371c9d4SSatish Balay   return PetscSegBufferGet(seg, count, (void **)slot);
22069371c9d4SSatish Balay }
2207d382aafbSBarry Smith 
22089de0f6ecSBarry Smith extern PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton;
22099de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *);
22109de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *);
22119de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted, const char *, const char *, PetscBool *);
22129de0f6ecSBarry Smith 
2213ace2fbaeSBarry Smith #include <stdarg.h>
2214ace2fbaeSBarry Smith PETSC_EXTERN PetscErrorCode PetscVSNPrintf(char *, size_t, const char[], size_t *, va_list);
2215ace2fbaeSBarry Smith PETSC_EXTERN                PetscErrorCode (*PetscVFPrintf)(FILE *, const char[], va_list);
2216ace2fbaeSBarry Smith 
2217268b0dfdSSatish Balay PETSC_EXTERN PetscSegBuffer PetscCitationsList;
2218fbfcfee5SBarry Smith 
22191f817a21SBarry Smith /*@C
22201f817a21SBarry Smith       PetscCitationsRegister - Register a bibtex item to obtain credit for an implemented algorithm used in the code.
22211f817a21SBarry Smith 
2222667f096bSBarry Smith      Not Collective; No Fortran Support
22231f817a21SBarry Smith 
22241f817a21SBarry Smith      Input Parameters:
222535cb6cd3SPierre Jolivet +      cite - the bibtex item, formatted to displayed on multiple lines nicely
222687497f52SBarry Smith -      set - a boolean variable initially set to `PETSC_FALSE`; this is used to insure only a single registration of the citation
22271f817a21SBarry Smith 
2228667f096bSBarry Smith      Options Database Key:
22293c7db156SBarry Smith .     -citations [filename]   - print out the bibtex entries for the given computation
22303c7db156SBarry Smith 
2231850545d0SSatish Balay      Level: intermediate
22321f817a21SBarry Smith @*/
2233d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscCitationsRegister(const char cit[], PetscBool *set)
2234d71ae5a4SJacob Faibussowitsch {
2235dff31646SBarry Smith   size_t len;
2236dff31646SBarry Smith   char  *vstring;
2237dff31646SBarry Smith 
22381f817a21SBarry Smith   PetscFunctionBegin;
22393ba16761SJacob Faibussowitsch   if (set && *set) PetscFunctionReturn(PETSC_SUCCESS);
22409566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(cit, &len));
22419566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferGet(PetscCitationsList, len, &vstring));
22429566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(vstring, cit, len));
2243dff31646SBarry Smith   if (set) *set = PETSC_TRUE;
22443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2245dff31646SBarry Smith }
2246dff31646SBarry Smith 
2247d8174014SToby Isaac PETSC_EXTERN                PETSC_DEPRECATED_FUNCTION("Google has discontinued its URL shortener service") PetscErrorCode PetscURLShorten(const char[], char[], size_t c);
2248fe278a28SBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm, char[], char[], size_t);
2249fe278a28SBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm, const char[], char[], size_t);
2250f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveUpload(MPI_Comm, const char[], const char[]);
2251fe278a28SBarry Smith 
2252f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscBoxAuthorize(MPI_Comm, char[], char[], size_t);
2253f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscBoxRefresh(MPI_Comm, const char[], char[], char[], size_t);
2254b967cddfSBarry Smith 
22559f4d3c52SBarry Smith PETSC_EXTERN PetscErrorCode PetscGlobusGetTransfers(MPI_Comm, const char[], char[], size_t);
22569f4d3c52SBarry Smith 
225704102261SBarry Smith PETSC_EXTERN PetscErrorCode PetscTextBelt(MPI_Comm, const char[], const char[], PetscBool *);
2258763ec7b1SBarry Smith PETSC_EXTERN PetscErrorCode PetscTellMyCell(MPI_Comm, const char[], const char[], PetscBool *);
2259dff31646SBarry Smith 
226068e69593SBarry Smith PETSC_EXTERN PetscErrorCode PetscPullJSONValue(const char[], const char[], char[], size_t, PetscBool *);
22615dc0f0a4SBarry Smith PETSC_EXTERN PetscErrorCode PetscPushJSONValue(char[], const char[], const char[], size_t);
2262d382aafbSBarry Smith 
2263b2566f29SBarry Smith #if defined(PETSC_USE_DEBUG)
2264d71ae5a4SJacob Faibussowitsch static inline unsigned int PetscStrHash(const char *str)
2265d71ae5a4SJacob Faibussowitsch {
2266df05ca09SBarry Smith   unsigned int c, hash = 5381;
2267df05ca09SBarry Smith 
2268503e1a2cSLisandro Dalcin   while ((c = (unsigned int)*str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
2269df05ca09SBarry Smith   return hash;
2270df05ca09SBarry Smith }
227177a5e07dSBarry Smith 
227277a5e07dSBarry Smith   /*MC
227387497f52SBarry Smith    MPIU_Allreduce - a PETSc replacement for `MPI_Allreduce()` that tries to determine if the call from all the MPI ranks occur from the
227487497f52SBarry Smith                     same place in the PETSc code. This helps to detect bugs where different MPI ranks follow different code paths
227587497f52SBarry Smith                     resulting in inconsistent and incorrect calls to `MPI_Allreduce()`.
227677a5e07dSBarry Smith 
2277d083f849SBarry Smith    Collective
227877a5e07dSBarry Smith 
227977a5e07dSBarry Smith    Synopsis:
228077a5e07dSBarry Smith      #include <petscsys.h>
228177a5e07dSBarry Smith      PetscErrorCode MPIU_Allreduce(void *indata,void *outdata,PetscMPIInt count,MPI_Datatype datatype, MPI_Op op, MPI_Comm comm);
228277a5e07dSBarry Smith 
228377a5e07dSBarry Smith    Input Parameters:
2284df05ca09SBarry Smith +  a - pointer to the input data to be reduced
2285df05ca09SBarry Smith .  c - the number of MPI data items in a and b
228687497f52SBarry Smith .  d - the MPI datatype, for example `MPI_INT`
228787497f52SBarry Smith .  e - the MPI operation, for example `MPI_SUM`
2288df05ca09SBarry Smith -  fcomm - the MPI communicator on which the operation occurs
228977a5e07dSBarry Smith 
229077a5e07dSBarry Smith    Output Parameter:
2291df05ca09SBarry Smith .  b - the reduced values
229277a5e07dSBarry Smith 
2293667f096bSBarry Smith    Level: developer
2294667f096bSBarry Smith 
2295e28ce29aSPatrick Sanan    Notes:
229687497f52SBarry Smith      In optimized mode this directly calls `MPI_Allreduce()`
229777a5e07dSBarry Smith 
2298df05ca09SBarry Smith      This is defined as a macro that can return error codes internally so it cannot be used in a subroutine that returns void.
2299df05ca09SBarry Smith 
230087497f52SBarry Smith      The error code this returns should be checked with `PetscCall()` even though it looks like an MPI function because it always returns PETSc error codes
2301df05ca09SBarry Smith 
2302db781477SPatrick Sanan .seealso: `MPI_Allreduce()`
230377a5e07dSBarry Smith M*/
23043ba16761SJacob Faibussowitsch   // clang-format off
23059371c9d4SSatish Balay #define MPIU_Allreduce(a, b, c, d, e, fcomm) \
23063ba16761SJacob Faibussowitsch   PetscMacroReturnStandard( \
23073ba16761SJacob Faibussowitsch     PetscMPIInt a_b1[6], a_b2[6]; \
23083ba16761SJacob Faibussowitsch     int _mpiu_allreduce_c_int = (int)(c); \
23093ba16761SJacob Faibussowitsch     a_b1[0] = -(PetscMPIInt)__LINE__; \
23103ba16761SJacob Faibussowitsch     a_b1[1] = -a_b1[0]; \
23113ba16761SJacob Faibussowitsch     a_b1[2] = -(PetscMPIInt)PetscStrHash(PETSC_FUNCTION_NAME); \
23123ba16761SJacob Faibussowitsch     a_b1[3] = -a_b1[2]; \
23133ba16761SJacob Faibussowitsch     a_b1[4] = -(PetscMPIInt)(c); \
23143ba16761SJacob Faibussowitsch     a_b1[5] = -a_b1[4]; \
23153ba16761SJacob Faibussowitsch     \
23163ba16761SJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(a_b1, a_b2, 6, MPI_INT, MPI_MAX, fcomm)); \
23173ba16761SJacob Faibussowitsch     PetscCheck(-a_b2[0] == a_b2[1], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MPI_Allreduce() called in different locations (code lines) on different processors"); \
23183ba16761SJacob Faibussowitsch     PetscCheck(-a_b2[2] == a_b2[3], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MPI_Allreduce() called in different locations (functions) on different processors"); \
23193ba16761SJacob Faibussowitsch     PetscCheck(-a_b2[4] == a_b2[5], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MPI_Allreduce() called with different counts %d on different processors", _mpiu_allreduce_c_int); \
23203ba16761SJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce((a), (b), (c), (d), (e), (fcomm)));)
23213ba16761SJacob Faibussowitsch // clang-format on
2322b2566f29SBarry Smith #else
2323f3fa974cSJacob Faibussowitsch   #define MPIU_Allreduce(a, b, c, d, e, fcomm) PetscMacroReturnStandard(PetscCallMPI(MPI_Allreduce((a), (b), (c), (d), (e), (fcomm))))
2324b2566f29SBarry Smith #endif
2325b2566f29SBarry Smith 
2326b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
23278198064fSBarry Smith PETSC_EXTERN PetscErrorCode MPIU_Win_allocate_shared(MPI_Aint, PetscMPIInt, MPI_Info, MPI_Comm, void *, MPI_Win *);
23288198064fSBarry Smith PETSC_EXTERN PetscErrorCode MPIU_Win_shared_query(MPI_Win, PetscMPIInt, MPI_Aint *, PetscMPIInt *, void *);
23292f065d89SBarry Smith #endif
23308198064fSBarry Smith 
2331cd88fca5SStefano Zampini /* this is a vile hack */
2332cd88fca5SStefano Zampini #if defined(PETSC_HAVE_NECMPI)
23339371c9d4SSatish Balay   #if !defined(PETSC_NECMPI_VERSION_MAJOR) || !defined(PETSC_NECMPI_VERSION_MINOR) || PETSC_NECMPI_VERSION_MAJOR < 2 || (PETSC_NECMPI_VERSION_MAJOR == 2 && PETSC_NECMPI_VERSION_MINOR < 18)
2334cd88fca5SStefano Zampini     #define MPI_Type_free(a) (*(a) = MPI_DATATYPE_NULL, 0);
2335cd88fca5SStefano Zampini   #endif
2336961fb248SStefano Zampini #endif
2337cd88fca5SStefano Zampini 
233812801b39SBarry Smith /*
233960fbe2beSVaclav Hapla     List of external packages and queries on it
234060fbe2beSVaclav Hapla */
234160fbe2beSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscHasExternalPackage(const char[], PetscBool *);
234260fbe2beSVaclav Hapla 
2343a52b435bSMark Adams /*
2344a52b435bSMark Adams  OpenMP support
2345a52b435bSMark Adams */
2346a52b435bSMark Adams #if defined(_OPENMP)
2347a52b435bSMark Adams   #define PetscPragmaOMP(...) _Pragma(PetscStringize(omp __VA_ARGS__))
2348a52b435bSMark Adams #else // no OpenMP so no threads
2349a52b435bSMark Adams   #define PetscPragmaOMP(...)
2350a52b435bSMark Adams #endif
2351a52b435bSMark Adams 
2352f1f2ae84SBarry Smith /* this cannot go here because it may be in a different shared library */
2353f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPIServerBegin(void);
2354f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPIServerEnd(void);
2355f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPICommsDestroy(void);
2356d382aafbSBarry Smith #endif
2357