xref: /petsc/include/petscsys.h (revision bbcf679c7ce315810b13005931dd3a7fb75b5053)
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 
130e28ce29aSPatrick Sanan    Notes:
13187497f52SBarry Smith    In MPI calls that require an MPI datatype that matches a `PetscInt` or array of `PetscInt` values, pass this value.
132e28ce29aSPatrick Sanan 
133e28ce29aSPatrick Sanan    Level: beginner
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
2020298fd71SBarry Smith     PETSC_IGNORE - same as NULL, means PETSc will ignore this argument
203d382aafbSBarry Smith 
204d382aafbSBarry Smith    Level: beginner
205d382aafbSBarry Smith 
206e28ce29aSPatrick Sanan    Note:
20787497f52SBarry Smith    Accepted by many PETSc functions to not set a parameter and instead use some default
208d382aafbSBarry Smith 
20987497f52SBarry Smith    Fortran Note:
21087497f52SBarry Smith    This macro does not exist in Fortran; you must use `PETSC_NULL_INTEGER`, `PETSC_NULL_DOUBLE_PRECISION` etc
211d382aafbSBarry Smith 
212db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_DEFAULT`, `PETSC_DETERMINE`
213d382aafbSBarry Smith 
214d382aafbSBarry Smith M*/
215f3fa974cSJacob Faibussowitsch #define PETSC_IGNORE PETSC_NULLPTR
216f3fa974cSJacob Faibussowitsch #define PETSC_NULL   PETSC_DEPRECATED_MACRO("GCC warning \"PETSC_NULL is deprecated, use PETSC_NULLPTR instead (since version 3.19)\"") PETSC_NULLPTR
217c528d872SBarry Smith 
218d382aafbSBarry Smith /*MC
219557d4da8SBarry Smith     PETSC_DECIDE - standard way of passing in integer or floating point parameter
220557d4da8SBarry Smith        where you wish PETSc to use the default.
221557d4da8SBarry Smith 
222557d4da8SBarry Smith    Level: beginner
223557d4da8SBarry Smith 
224db781477SPatrick Sanan .seealso: `PETSC_DEFAULT`, `PETSC_IGNORE`, `PETSC_DETERMINE`
225557d4da8SBarry Smith 
226557d4da8SBarry Smith M*/
227557d4da8SBarry Smith 
228557d4da8SBarry Smith /*MC
229d382aafbSBarry Smith     PETSC_DETERMINE - standard way of passing in integer or floating point parameter
230d382aafbSBarry Smith        where you wish PETSc to compute the required value.
231d382aafbSBarry Smith 
232d382aafbSBarry Smith    Level: beginner
233d382aafbSBarry Smith 
234e28ce29aSPatrick Sanan    Developer Note:
23587497f52SBarry Smith    I would like to use const `PetscInt` `PETSC_DETERMINE` = `PETSC_DECIDE`; but for
23687497f52SBarry Smith      some reason this is not allowed by the standard even though `PETSC_DECIDE` is a constant value.
237557d4da8SBarry Smith 
238db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_DEFAULT`, `PETSC_IGNORE`, `VecSetSizes()`
239d382aafbSBarry Smith 
240d382aafbSBarry Smith M*/
241d382aafbSBarry Smith 
242d382aafbSBarry Smith /*MC
243557d4da8SBarry Smith     PETSC_DEFAULT - standard way of passing in integer or floating point parameter
244557d4da8SBarry Smith        where you wish PETSc to use the default.
245557d4da8SBarry Smith 
246557d4da8SBarry Smith    Level: beginner
247557d4da8SBarry Smith 
24887497f52SBarry Smith    Fortran Note:
24987497f52SBarry Smith    You need to use `PETSC_DEFAULT_INTEGER` or `PETSC_DEFAULT_REAL`.
250557d4da8SBarry Smith 
251db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_IGNORE`, `PETSC_DETERMINE`
252557d4da8SBarry Smith 
253557d4da8SBarry Smith M*/
254f3fa974cSJacob Faibussowitsch enum {
255f3fa974cSJacob Faibussowitsch   PETSC_DECIDE    = -1,
256f3fa974cSJacob Faibussowitsch   PETSC_DETERMINE = PETSC_DECIDE,
257f3fa974cSJacob Faibussowitsch   PETSC_DEFAULT   = -2
258f3fa974cSJacob Faibussowitsch };
259557d4da8SBarry Smith 
260557d4da8SBarry Smith /*MC
26187497f52SBarry Smith     PETSC_COMM_WORLD - the equivalent of the `MPI_COMM_WORLD` communicator which represents
262ea8fe74dSBarry Smith            all the processes that PETSc knows about.
263d382aafbSBarry Smith 
264d382aafbSBarry Smith    Level: beginner
265d382aafbSBarry Smith 
266e28ce29aSPatrick Sanan    Notes:
26787497f52SBarry Smith    By default `PETSC_COMM_WORLD` and `MPI_COMM_WORLD` are identical unless you wish to
26887497f52SBarry Smith           run PETSc on ONLY a subset of `MPI_COMM_WORLD`. In that case create your new (smaller)
26987497f52SBarry Smith           communicator, call it, say comm, and set `PETSC_COMM_WORLD` = comm BEFORE calling
27087497f52SBarry Smith           PetscInitialize(), but after `MPI_Init()` has been called.
271a990b902SBarry Smith 
27287497f52SBarry Smith           The value of `PETSC_COMM_WORLD` should never be USED/accessed before `PetscInitialize()`
273a990b902SBarry Smith           is called because it may not have a valid value yet.
274d382aafbSBarry Smith 
275db781477SPatrick Sanan .seealso: `PETSC_COMM_SELF`
276d382aafbSBarry Smith 
277d382aafbSBarry Smith M*/
278014dd563SJed Brown PETSC_EXTERN MPI_Comm PETSC_COMM_WORLD;
279d382aafbSBarry Smith 
280d382aafbSBarry Smith /*MC
28187497f52SBarry Smith     PETSC_COMM_SELF - This is always `MPI_COMM_SELF`
282d382aafbSBarry Smith 
283d382aafbSBarry Smith    Level: beginner
284d382aafbSBarry Smith 
285e28ce29aSPatrick Sanan    Notes:
286e28ce29aSPatrick Sanan    Do not USE/access or set this variable before PetscInitialize() has been called.
287a990b902SBarry Smith 
288db781477SPatrick Sanan .seealso: `PETSC_COMM_WORLD`
289d382aafbSBarry Smith 
290d382aafbSBarry Smith M*/
291d382aafbSBarry Smith #define PETSC_COMM_SELF MPI_COMM_SELF
292d382aafbSBarry Smith 
2936de5d289SStefano Zampini /*MC
2946de5d289SStefano Zampini     PETSC_MPI_THREAD_REQUIRED - the required threading support used if PETSc initializes
29587497f52SBarry Smith            MPI with `MPI_Init_thread()`.
2966de5d289SStefano Zampini 
2976de5d289SStefano Zampini    Level: beginner
2986de5d289SStefano Zampini 
2996de5d289SStefano Zampini    Notes:
30087497f52SBarry Smith    By default `PETSC_MPI_THREAD_REQUIRED` equals `MPI_THREAD_FUNNELED`.
3016de5d289SStefano Zampini 
302db781477SPatrick Sanan .seealso: `PetscInitialize()`
3036de5d289SStefano Zampini 
3046de5d289SStefano Zampini M*/
3056de5d289SStefano Zampini PETSC_EXTERN PetscMPIInt PETSC_MPI_THREAD_REQUIRED;
3066de5d289SStefano Zampini 
307d6f2c3cbSBarry Smith PETSC_EXTERN PetscBool PetscBeganMPI;
3088ad20175SVaclav Hapla PETSC_EXTERN PetscBool PetscErrorHandlingInitialized;
309014dd563SJed Brown PETSC_EXTERN PetscBool PetscInitializeCalled;
310014dd563SJed Brown PETSC_EXTERN PetscBool PetscFinalizeCalled;
3114cf1874eSKarl Rupp PETSC_EXTERN PetscBool PetscViennaCLSynchronize;
312d382aafbSBarry Smith 
313014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*)(MPI_Comm), PetscErrorCode (*)(MPI_Comm));
314014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommDuplicate(MPI_Comm, MPI_Comm *, int *);
315014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommDestroy(MPI_Comm *);
31657f21012SBarry Smith PETSC_EXTERN PetscErrorCode PetscCommGetComm(MPI_Comm, MPI_Comm *);
31757f21012SBarry Smith PETSC_EXTERN PetscErrorCode PetscCommRestoreComm(MPI_Comm, MPI_Comm *);
318d382aafbSBarry Smith 
31959e55d94SJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
32059e55d94SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscKokkosInitializeCheck(void); /* Initialize Kokkos if not yet. */
32159e55d94SJunchao Zhang #endif
32259e55d94SJunchao Zhang 
32371438e86SJunchao Zhang #if defined(PETSC_HAVE_NVSHMEM)
32471438e86SJunchao Zhang PETSC_EXTERN PetscBool      PetscBeganNvshmem;
32571438e86SJunchao Zhang PETSC_EXTERN PetscBool      PetscNvshmemInitialized;
32671438e86SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscNvshmemFinalize(void);
32771438e86SJunchao Zhang #endif
32871438e86SJunchao Zhang 
329540e20f2SPierre Jolivet #if defined(PETSC_HAVE_ELEMENTAL)
330540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalInitializePackage(void);
331540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalInitialized(PetscBool *);
332540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalFinalizePackage(void);
333540e20f2SPierre Jolivet #endif
334540e20f2SPierre Jolivet 
335d382aafbSBarry Smith /*MC
33687497f52SBarry Smith    PetscMalloc - Allocates memory, One should use `PetscNew()`, `PetscMalloc1()` or `PetscCalloc1()` usually instead of this
337d382aafbSBarry Smith 
338eca87e8dSBarry Smith    Synopsis:
339aaa7dc30SBarry Smith     #include <petscsys.h>
340eca87e8dSBarry Smith    PetscErrorCode PetscMalloc(size_t m,void **result)
341eca87e8dSBarry Smith 
342eca87e8dSBarry Smith    Not Collective
343eca87e8dSBarry Smith 
344d382aafbSBarry Smith    Input Parameter:
345d382aafbSBarry Smith .  m - number of bytes to allocate
346d382aafbSBarry Smith 
347d382aafbSBarry Smith    Output Parameter:
348d382aafbSBarry Smith .  result - memory allocated
349d382aafbSBarry Smith 
350d382aafbSBarry Smith    Level: beginner
351d382aafbSBarry Smith 
35249d7da52SJed Brown    Notes:
35349d7da52SJed Brown    Memory is always allocated at least double aligned
354d382aafbSBarry Smith 
35587497f52SBarry Smith    It is safe to allocate size 0 and pass the resulting pointer (which may or may not be NULL) to `PetscFree()`.
356d382aafbSBarry Smith 
357db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`
358d382aafbSBarry Smith 
359d382aafbSBarry Smith M*/
360071fcb05SBarry Smith #define PetscMalloc(a, b) ((*PetscTrMalloc)((a), PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))
361d382aafbSBarry Smith 
362d382aafbSBarry Smith /*MC
363d5b43468SJose E. Roman    PetscRealloc - Reallocates memory
3643221ece2SMatthew G. Knepley 
3653221ece2SMatthew G. Knepley    Synopsis:
3663221ece2SMatthew G. Knepley     #include <petscsys.h>
3673221ece2SMatthew G. Knepley    PetscErrorCode PetscRealloc(size_t m,void **result)
3683221ece2SMatthew G. Knepley 
3693221ece2SMatthew G. Knepley    Not Collective
3703221ece2SMatthew G. Knepley 
3713221ece2SMatthew G. Knepley    Input Parameters:
3723221ece2SMatthew G. Knepley +  m - number of bytes to allocate
37392f119d6SBarry Smith -  result - previous memory
3743221ece2SMatthew G. Knepley 
3753221ece2SMatthew G. Knepley    Output Parameter:
3763221ece2SMatthew G. Knepley .  result - new memory allocated
3773221ece2SMatthew G. Knepley 
3783221ece2SMatthew G. Knepley    Level: developer
3793221ece2SMatthew G. Knepley 
3803221ece2SMatthew G. Knepley    Notes:
3813221ece2SMatthew G. Knepley    Memory is always allocated at least double aligned
3823221ece2SMatthew G. Knepley 
383db781477SPatrick Sanan .seealso: `PetscMalloc()`, `PetscFree()`, `PetscNew()`
3843221ece2SMatthew G. Knepley 
3853221ece2SMatthew G. Knepley M*/
3863221ece2SMatthew G. Knepley #define PetscRealloc(a, b) ((*PetscTrRealloc)((a), __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))
3873221ece2SMatthew G. Knepley 
3883221ece2SMatthew G. Knepley /*MC
38987497f52SBarry Smith    PetscAddrAlign - Rounds up an address to `PETSC_MEMALIGN` alignment
390d382aafbSBarry Smith 
391d382aafbSBarry Smith    Synopsis:
392aaa7dc30SBarry Smith     #include <petscsys.h>
393d382aafbSBarry Smith    void *PetscAddrAlign(void *addr)
394d382aafbSBarry Smith 
395eca87e8dSBarry Smith    Not Collective
396eca87e8dSBarry Smith 
397eca87e8dSBarry Smith    Input Parameters:
398eca87e8dSBarry Smith .  addr - address to align (any pointer type)
399eca87e8dSBarry Smith 
400d382aafbSBarry Smith    Level: developer
401d382aafbSBarry Smith 
402db781477SPatrick Sanan .seealso: `PetscMallocAlign()`
403d382aafbSBarry Smith 
404d382aafbSBarry Smith M*/
405f3fa974cSJacob Faibussowitsch #define PetscAddrAlign(a) ((void *)((((PETSC_UINTPTR_T)(a)) + (PETSC_MEMALIGN - 1)) & ~(PETSC_MEMALIGN - 1)))
406d382aafbSBarry Smith 
407d382aafbSBarry Smith /*MC
40887497f52SBarry Smith    PetscCalloc - Allocates a cleared (zeroed) memory region aligned to `PETSC_MEMALIGN`
4098f51dc47SJunchao Zhang 
4108f51dc47SJunchao Zhang    Synopsis:
4118f51dc47SJunchao Zhang     #include <petscsys.h>
4128f51dc47SJunchao Zhang    PetscErrorCode PetscCalloc(size_t m,void **result)
4138f51dc47SJunchao Zhang 
4148f51dc47SJunchao Zhang    Not Collective
4158f51dc47SJunchao Zhang 
4168f51dc47SJunchao Zhang    Input Parameter:
4178f51dc47SJunchao Zhang .  m - number of bytes to allocate
4188f51dc47SJunchao Zhang 
4198f51dc47SJunchao Zhang    Output Parameter:
4208f51dc47SJunchao Zhang .  result - memory allocated
4218f51dc47SJunchao Zhang 
4228f51dc47SJunchao Zhang    Level: beginner
4238f51dc47SJunchao Zhang 
4248f51dc47SJunchao Zhang    Notes:
4258f51dc47SJunchao Zhang    Memory is always allocated at least double aligned. This macro is useful in allocating memory pointed by void pointers
4268f51dc47SJunchao Zhang 
4278f51dc47SJunchao Zhang    It is safe to allocate size 0 and pass the resulting pointer (which may or may not be NULL) to PetscFree().
4288f51dc47SJunchao Zhang 
429db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`
4308f51dc47SJunchao Zhang 
4318f51dc47SJunchao Zhang M*/
432f3fa974cSJacob Faibussowitsch #define PetscCalloc(m, result) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)m), (result))
4338f51dc47SJunchao Zhang 
4348f51dc47SJunchao Zhang /*MC
43587497f52SBarry Smith    PetscMalloc1 - Allocates an array of memory aligned to `PETSC_MEMALIGN`
436d382aafbSBarry Smith 
437eca87e8dSBarry Smith    Synopsis:
438aaa7dc30SBarry Smith     #include <petscsys.h>
439785e854fSJed Brown    PetscErrorCode PetscMalloc1(size_t m1,type **r1)
440785e854fSJed Brown 
441785e854fSJed Brown    Not Collective
442785e854fSJed Brown 
443785e854fSJed Brown    Input Parameter:
444390e1bf2SBarry Smith .  m1 - number of elements to allocate  (may be zero)
445785e854fSJed Brown 
446785e854fSJed Brown    Output Parameter:
4471fe1b817SJunchao Zhang .  r1 - memory allocated
448785e854fSJed Brown 
449e28ce29aSPatrick Sanan    Note:
450e28ce29aSPatrick Sanan    This uses the sizeof() of the memory type requested to determine the total memory to be allocated, therefore you should not
45187497f52SBarry Smith          multiply the number of elements requested by the `sizeof()` the type. For example use
452390e1bf2SBarry Smith $  PetscInt *id;
453390e1bf2SBarry Smith $  PetscMalloc1(10,&id);
454390e1bf2SBarry Smith         not
455390e1bf2SBarry Smith $  PetscInt *id;
456390e1bf2SBarry Smith $  PetscMalloc1(10*sizeof(PetscInt),&id);
457390e1bf2SBarry Smith 
45887497f52SBarry Smith         Does not zero the memory allocated, use `PetscCalloc1()` to obtain memory that has been zeroed.
459390e1bf2SBarry Smith 
460390e1bf2SBarry Smith    Level: beginner
461785e854fSJed Brown 
462db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
463785e854fSJed Brown 
464785e854fSJed Brown M*/
465f3fa974cSJacob Faibussowitsch #define PetscMalloc1(m1, r1) PetscMallocA(1, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1))
466785e854fSJed Brown 
467785e854fSJed Brown /*MC
46887497f52SBarry Smith    PetscCalloc1 - Allocates a cleared (zeroed) array of memory aligned to `PETSC_MEMALIGN`
4691795a4d1SJed Brown 
4701795a4d1SJed Brown    Synopsis:
471aaa7dc30SBarry Smith     #include <petscsys.h>
4721795a4d1SJed Brown    PetscErrorCode PetscCalloc1(size_t m1,type **r1)
4731795a4d1SJed Brown 
4741795a4d1SJed Brown    Not Collective
4751795a4d1SJed Brown 
4761795a4d1SJed Brown    Input Parameter:
4771795a4d1SJed Brown .  m1 - number of elements to allocate in 1st chunk  (may be zero)
4781795a4d1SJed Brown 
4791795a4d1SJed Brown    Output Parameter:
4801fe1b817SJunchao Zhang .  r1 - memory allocated
4811795a4d1SJed Brown 
482e28ce29aSPatrick Sanan    Notes:
48387497f52SBarry Smith    See `PetsMalloc1()` for more details on usage.
484390e1bf2SBarry Smith 
485390e1bf2SBarry Smith    Level: beginner
4861795a4d1SJed Brown 
487db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
4881795a4d1SJed Brown 
4891795a4d1SJed Brown M*/
490f3fa974cSJacob Faibussowitsch #define PetscCalloc1(m1, r1) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1))
4911795a4d1SJed Brown 
4921795a4d1SJed Brown /*MC
49387497f52SBarry Smith    PetscMalloc2 - Allocates 2 arrays of memory both aligned to `PETSC_MEMALIGN`
494d382aafbSBarry Smith 
495eca87e8dSBarry Smith    Synopsis:
496aaa7dc30SBarry Smith     #include <petscsys.h>
497dcca6d9dSJed Brown    PetscErrorCode PetscMalloc2(size_t m1,type **r1,size_t m2,type **r2)
498eca87e8dSBarry Smith 
499eca87e8dSBarry Smith    Not Collective
500eca87e8dSBarry Smith 
501d8d19677SJose E. Roman    Input Parameters:
502d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
503dcca6d9dSJed Brown -  m2 - number of elements to allocate in 2nd chunk  (may be zero)
504d382aafbSBarry Smith 
505d8d19677SJose E. Roman    Output Parameters:
506d382aafbSBarry Smith +  r1 - memory allocated in first chunk
507d382aafbSBarry Smith -  r2 - memory allocated in second chunk
508d382aafbSBarry Smith 
509d382aafbSBarry Smith    Level: developer
510d382aafbSBarry Smith 
511db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
512d382aafbSBarry Smith 
513d382aafbSBarry Smith M*/
514f3fa974cSJacob 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))
515d382aafbSBarry Smith 
516d382aafbSBarry Smith /*MC
51787497f52SBarry Smith    PetscCalloc2 - Allocates 2 cleared (zeroed) arrays of memory both aligned to `PETSC_MEMALIGN`
518d382aafbSBarry Smith 
519eca87e8dSBarry Smith    Synopsis:
520aaa7dc30SBarry Smith     #include <petscsys.h>
5211795a4d1SJed Brown    PetscErrorCode PetscCalloc2(size_t m1,type **r1,size_t m2,type **r2)
522eca87e8dSBarry Smith 
523eca87e8dSBarry Smith    Not Collective
524eca87e8dSBarry Smith 
525d8d19677SJose E. Roman    Input Parameters:
526d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
5271795a4d1SJed Brown -  m2 - number of elements to allocate in 2nd chunk  (may be zero)
5281795a4d1SJed Brown 
529d8d19677SJose E. Roman    Output Parameters:
5301795a4d1SJed Brown +  r1 - memory allocated in first chunk
5311795a4d1SJed Brown -  r2 - memory allocated in second chunk
5321795a4d1SJed Brown 
5331795a4d1SJed Brown    Level: developer
5341795a4d1SJed Brown 
535db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
5361795a4d1SJed Brown 
5371795a4d1SJed Brown M*/
538f3fa974cSJacob 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))
5391795a4d1SJed Brown 
5401795a4d1SJed Brown /*MC
54187497f52SBarry Smith    PetscMalloc3 - Allocates 3 arrays of memory, all aligned to `PETSC_MEMALIGN`
542d382aafbSBarry Smith 
543d382aafbSBarry Smith    Synopsis:
544aaa7dc30SBarry Smith     #include <petscsys.h>
545dcca6d9dSJed Brown    PetscErrorCode PetscMalloc3(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3)
546d382aafbSBarry Smith 
547d382aafbSBarry Smith    Not Collective
548d382aafbSBarry Smith 
549d8d19677SJose E. Roman    Input Parameters:
550d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
551d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
552dcca6d9dSJed Brown -  m3 - number of elements to allocate in 3rd chunk  (may be zero)
553d382aafbSBarry Smith 
554d8d19677SJose E. Roman    Output Parameters:
555d382aafbSBarry Smith +  r1 - memory allocated in first chunk
556d382aafbSBarry Smith .  r2 - memory allocated in second chunk
557d382aafbSBarry Smith -  r3 - memory allocated in third chunk
558d382aafbSBarry Smith 
559d382aafbSBarry Smith    Level: developer
560d382aafbSBarry Smith 
561db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc3()`, `PetscFree3()`
562d382aafbSBarry Smith 
563d382aafbSBarry Smith M*/
564f3fa974cSJacob Faibussowitsch #define PetscMalloc3(m1, r1, m2, r2, m3, r3) \
565f3fa974cSJacob 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))
566d382aafbSBarry Smith 
567d382aafbSBarry Smith /*MC
56887497f52SBarry Smith    PetscCalloc3 - Allocates 3 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
569d382aafbSBarry Smith 
570eca87e8dSBarry Smith    Synopsis:
571aaa7dc30SBarry Smith     #include <petscsys.h>
5721795a4d1SJed Brown    PetscErrorCode PetscCalloc3(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3)
573eca87e8dSBarry Smith 
574eca87e8dSBarry Smith    Not Collective
575eca87e8dSBarry Smith 
576d8d19677SJose E. Roman    Input Parameters:
577d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
578d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
5791795a4d1SJed Brown -  m3 - number of elements to allocate in 3rd chunk  (may be zero)
5801795a4d1SJed Brown 
581d8d19677SJose E. Roman    Output Parameters:
5821795a4d1SJed Brown +  r1 - memory allocated in first chunk
5831795a4d1SJed Brown .  r2 - memory allocated in second chunk
5841795a4d1SJed Brown -  r3 - memory allocated in third chunk
5851795a4d1SJed Brown 
5861795a4d1SJed Brown    Level: developer
5871795a4d1SJed Brown 
588db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc2()`, `PetscMalloc3()`, `PetscFree3()`
5891795a4d1SJed Brown 
5901795a4d1SJed Brown M*/
591f3fa974cSJacob Faibussowitsch #define PetscCalloc3(m1, r1, m2, r2, m3, r3) \
592f3fa974cSJacob 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))
5931795a4d1SJed Brown 
5941795a4d1SJed Brown /*MC
59587497f52SBarry Smith    PetscMalloc4 - Allocates 4 arrays of memory, all aligned to `PETSC_MEMALIGN`
596d382aafbSBarry Smith 
597d382aafbSBarry Smith    Synopsis:
598aaa7dc30SBarry Smith     #include <petscsys.h>
599dcca6d9dSJed Brown    PetscErrorCode PetscMalloc4(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4)
600d382aafbSBarry Smith 
601d382aafbSBarry Smith    Not Collective
602d382aafbSBarry Smith 
603d8d19677SJose E. Roman    Input Parameters:
604d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
605d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
606d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
607dcca6d9dSJed Brown -  m4 - number of elements to allocate in 4th chunk  (may be zero)
608d382aafbSBarry Smith 
609d8d19677SJose E. Roman    Output Parameters:
610d382aafbSBarry Smith +  r1 - memory allocated in first chunk
611d382aafbSBarry Smith .  r2 - memory allocated in second chunk
612d382aafbSBarry Smith .  r3 - memory allocated in third chunk
613d382aafbSBarry Smith -  r4 - memory allocated in fourth chunk
614d382aafbSBarry Smith 
615d382aafbSBarry Smith    Level: developer
616d382aafbSBarry Smith 
617db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
618d382aafbSBarry Smith 
619d382aafbSBarry Smith M*/
6209371c9d4SSatish Balay #define PetscMalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
621f3fa974cSJacob 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))
622d382aafbSBarry Smith 
623d382aafbSBarry Smith /*MC
62487497f52SBarry Smith    PetscCalloc4 - Allocates 4 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
625d382aafbSBarry Smith 
626eca87e8dSBarry Smith    Synopsis:
627aaa7dc30SBarry Smith     #include <petscsys.h>
6281795a4d1SJed Brown    PetscErrorCode PetscCalloc4(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4)
629eca87e8dSBarry Smith 
630eca87e8dSBarry Smith    Not Collective
631eca87e8dSBarry Smith 
632e28ce29aSPatrick Sanan    Input Parameters:
633d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
634d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
635d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
6361795a4d1SJed Brown -  m4 - number of elements to allocate in 4th chunk  (may be zero)
6371795a4d1SJed Brown 
638e28ce29aSPatrick Sanan    Output Parameters:
6391795a4d1SJed Brown +  r1 - memory allocated in first chunk
6401795a4d1SJed Brown .  r2 - memory allocated in second chunk
6411795a4d1SJed Brown .  r3 - memory allocated in third chunk
6421795a4d1SJed Brown -  r4 - memory allocated in fourth chunk
6431795a4d1SJed Brown 
6441795a4d1SJed Brown    Level: developer
6451795a4d1SJed Brown 
646db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
6471795a4d1SJed Brown 
6481795a4d1SJed Brown M*/
6499371c9d4SSatish Balay #define PetscCalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
650f3fa974cSJacob 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))
6511795a4d1SJed Brown 
6521795a4d1SJed Brown /*MC
65387497f52SBarry Smith    PetscMalloc5 - Allocates 5 arrays of memory, all aligned to `PETSC_MEMALIGN`
654d382aafbSBarry Smith 
655d382aafbSBarry Smith    Synopsis:
656aaa7dc30SBarry Smith     #include <petscsys.h>
657dcca6d9dSJed 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)
658d382aafbSBarry Smith 
659d382aafbSBarry Smith    Not Collective
660d382aafbSBarry Smith 
661e28ce29aSPatrick Sanan    Input Parameters:
662d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
663d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
664d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
665d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
666dcca6d9dSJed Brown -  m5 - number of elements to allocate in 5th chunk  (may be zero)
667d382aafbSBarry Smith 
668e28ce29aSPatrick Sanan    Output Parameters:
669d382aafbSBarry Smith +  r1 - memory allocated in first chunk
670d382aafbSBarry Smith .  r2 - memory allocated in second chunk
671d382aafbSBarry Smith .  r3 - memory allocated in third chunk
672d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
673d382aafbSBarry Smith -  r5 - memory allocated in fifth chunk
674d382aafbSBarry Smith 
675d382aafbSBarry Smith    Level: developer
676d382aafbSBarry Smith 
677db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc5()`, `PetscFree5()`
678d382aafbSBarry Smith 
679d382aafbSBarry Smith M*/
6809371c9d4SSatish Balay #define PetscMalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
681f3fa974cSJacob 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))
682d382aafbSBarry Smith 
683d382aafbSBarry Smith /*MC
68487497f52SBarry Smith    PetscCalloc5 - Allocates 5 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
685d382aafbSBarry Smith 
686eca87e8dSBarry Smith    Synopsis:
687aaa7dc30SBarry Smith     #include <petscsys.h>
6881795a4d1SJed 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)
689eca87e8dSBarry Smith 
690eca87e8dSBarry Smith    Not Collective
691eca87e8dSBarry Smith 
692e28ce29aSPatrick Sanan    Input Parameters:
693d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
694d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
695d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
696d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
6971795a4d1SJed Brown -  m5 - number of elements to allocate in 5th chunk  (may be zero)
6981795a4d1SJed Brown 
699e28ce29aSPatrick Sanan    Output Parameters:
7001795a4d1SJed Brown +  r1 - memory allocated in first chunk
7011795a4d1SJed Brown .  r2 - memory allocated in second chunk
7021795a4d1SJed Brown .  r3 - memory allocated in third chunk
7031795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
7041795a4d1SJed Brown -  r5 - memory allocated in fifth chunk
7051795a4d1SJed Brown 
7061795a4d1SJed Brown    Level: developer
7071795a4d1SJed Brown 
708db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc5()`, `PetscFree5()`
7091795a4d1SJed Brown 
7101795a4d1SJed Brown M*/
7119371c9d4SSatish Balay #define PetscCalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
712f3fa974cSJacob 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))
713d382aafbSBarry Smith 
714d382aafbSBarry Smith /*MC
71587497f52SBarry Smith    PetscMalloc6 - Allocates 6 arrays of memory, all aligned to `PETSC_MEMALIGN`
716d382aafbSBarry Smith 
717d382aafbSBarry Smith    Synopsis:
718aaa7dc30SBarry Smith     #include <petscsys.h>
719dcca6d9dSJed 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)
720d382aafbSBarry Smith 
721d382aafbSBarry Smith    Not Collective
722d382aafbSBarry Smith 
723e28ce29aSPatrick Sanan    Input Parameters:
724d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
725d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
726d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
727d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
728d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
729dcca6d9dSJed Brown -  m6 - number of elements to allocate in 6th chunk  (may be zero)
730d382aafbSBarry Smith 
731e28ce29aSPatrick Sanan    Output Parameteasr:
732d382aafbSBarry Smith +  r1 - memory allocated in first chunk
733d382aafbSBarry Smith .  r2 - memory allocated in second chunk
734d382aafbSBarry Smith .  r3 - memory allocated in third chunk
735d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
736d382aafbSBarry Smith .  r5 - memory allocated in fifth chunk
737d382aafbSBarry Smith -  r6 - memory allocated in sixth chunk
738d382aafbSBarry Smith 
739d382aafbSBarry Smith    Level: developer
740d382aafbSBarry Smith 
741db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc6()`, `PetscFree3()`, `PetscFree4()`, `PetscFree5()`, `PetscFree6()`
742d382aafbSBarry Smith 
743d382aafbSBarry Smith M*/
7449371c9d4SSatish Balay #define PetscMalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
745f3fa974cSJacob 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))
746d382aafbSBarry Smith 
747d382aafbSBarry Smith /*MC
74887497f52SBarry Smith    PetscCalloc6 - Allocates 6 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
749d382aafbSBarry Smith 
750eca87e8dSBarry Smith    Synopsis:
751aaa7dc30SBarry Smith     #include <petscsys.h>
7521795a4d1SJed 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)
753eca87e8dSBarry Smith 
754eca87e8dSBarry Smith    Not Collective
755eca87e8dSBarry Smith 
756e28ce29aSPatrick Sanan    Input Parameters:
757d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
758d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
759d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
760d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
761d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
7621795a4d1SJed Brown -  m6 - number of elements to allocate in 6th chunk  (may be zero)
7631795a4d1SJed Brown 
764e28ce29aSPatrick Sanan    Output Parameters:
7651795a4d1SJed Brown +  r1 - memory allocated in first chunk
7661795a4d1SJed Brown .  r2 - memory allocated in second chunk
7671795a4d1SJed Brown .  r3 - memory allocated in third chunk
7681795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
7691795a4d1SJed Brown .  r5 - memory allocated in fifth chunk
7701795a4d1SJed Brown -  r6 - memory allocated in sixth chunk
7711795a4d1SJed Brown 
7721795a4d1SJed Brown    Level: developer
7731795a4d1SJed Brown 
774db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc6()`, `PetscFree6()`
7751795a4d1SJed Brown 
7761795a4d1SJed Brown M*/
7779371c9d4SSatish Balay #define PetscCalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
778f3fa974cSJacob 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))
7791795a4d1SJed Brown 
7801795a4d1SJed Brown /*MC
78187497f52SBarry Smith    PetscMalloc7 - Allocates 7 arrays of memory, all aligned to `PETSC_MEMALIGN`
782d382aafbSBarry Smith 
783d382aafbSBarry Smith    Synopsis:
784aaa7dc30SBarry Smith     #include <petscsys.h>
785dcca6d9dSJed 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)
786d382aafbSBarry Smith 
787d382aafbSBarry Smith    Not Collective
788d382aafbSBarry Smith 
789e28ce29aSPatrick Sanan    Input Parameters:
790d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
791d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
792d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
793d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
794d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
795d382aafbSBarry Smith .  m6 - number of elements to allocate in 6th chunk  (may be zero)
796dcca6d9dSJed Brown -  m7 - number of elements to allocate in 7th chunk  (may be zero)
797d382aafbSBarry Smith 
798e28ce29aSPatrick Sanan    Output Parameters:
799d382aafbSBarry Smith +  r1 - memory allocated in first chunk
800d382aafbSBarry Smith .  r2 - memory allocated in second chunk
801d382aafbSBarry Smith .  r3 - memory allocated in third chunk
802d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
803d382aafbSBarry Smith .  r5 - memory allocated in fifth chunk
804d382aafbSBarry Smith .  r6 - memory allocated in sixth chunk
805eca87e8dSBarry Smith -  r7 - memory allocated in seventh chunk
806d382aafbSBarry Smith 
807d382aafbSBarry Smith    Level: developer
808d382aafbSBarry Smith 
809db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc7()`, `PetscFree7()`
810d382aafbSBarry Smith 
811d382aafbSBarry Smith M*/
8129371c9d4SSatish Balay #define PetscMalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
813f3fa974cSJacob 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))
814d382aafbSBarry Smith 
815d382aafbSBarry Smith /*MC
81687497f52SBarry Smith    PetscCalloc7 - Allocates 7 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
8171795a4d1SJed Brown 
8181795a4d1SJed Brown    Synopsis:
819aaa7dc30SBarry Smith     #include <petscsys.h>
8201795a4d1SJed 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)
8211795a4d1SJed Brown 
8221795a4d1SJed Brown    Not Collective
8231795a4d1SJed Brown 
824e28ce29aSPatrick Sanan    Input Parameters:
8251795a4d1SJed Brown +  m1 - number of elements to allocate in 1st chunk  (may be zero)
8261795a4d1SJed Brown .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
8271795a4d1SJed Brown .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
8281795a4d1SJed Brown .  m4 - number of elements to allocate in 4th chunk  (may be zero)
8291795a4d1SJed Brown .  m5 - number of elements to allocate in 5th chunk  (may be zero)
8301795a4d1SJed Brown .  m6 - number of elements to allocate in 6th chunk  (may be zero)
8311795a4d1SJed Brown -  m7 - number of elements to allocate in 7th chunk  (may be zero)
8321795a4d1SJed Brown 
833e28ce29aSPatrick Sanan    Output Parameters:
8341795a4d1SJed Brown +  r1 - memory allocated in first chunk
8351795a4d1SJed Brown .  r2 - memory allocated in second chunk
8361795a4d1SJed Brown .  r3 - memory allocated in third chunk
8371795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
8381795a4d1SJed Brown .  r5 - memory allocated in fifth chunk
8391795a4d1SJed Brown .  r6 - memory allocated in sixth chunk
8401795a4d1SJed Brown -  r7 - memory allocated in seventh chunk
8411795a4d1SJed Brown 
8421795a4d1SJed Brown    Level: developer
8431795a4d1SJed Brown 
844db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc7()`, `PetscFree7()`
8451795a4d1SJed Brown 
8461795a4d1SJed Brown M*/
8479371c9d4SSatish Balay #define PetscCalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
848f3fa974cSJacob 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))
8491795a4d1SJed Brown 
8501795a4d1SJed Brown /*MC
85187497f52SBarry Smith    PetscNew - Allocates memory of a particular type, zeros the memory! Aligned to `PETSC_MEMALIGN`
852d382aafbSBarry Smith 
853eca87e8dSBarry Smith    Synopsis:
854aaa7dc30SBarry Smith     #include <petscsys.h>
855b00a9115SJed Brown    PetscErrorCode PetscNew(type **result)
856eca87e8dSBarry Smith 
857eca87e8dSBarry Smith    Not Collective
858eca87e8dSBarry Smith 
859d382aafbSBarry Smith    Output Parameter:
860b00a9115SJed Brown .  result - memory allocated, sized to match pointer type
861d382aafbSBarry Smith 
862d382aafbSBarry Smith    Level: beginner
863d382aafbSBarry Smith 
864fea72eb4SStefano Zampini .seealso: `PetscFree()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc1()`
865d382aafbSBarry Smith 
866d382aafbSBarry Smith M*/
867b00a9115SJed Brown #define PetscNew(b) PetscCalloc1(1, (b))
868ad0619ddSBarry Smith 
8694dfa11a4SJacob Faibussowitsch #define PetscNewLog(o, b) PETSC_DEPRECATED_MACRO("GCC warning \"PetscNewLog is deprecated, use PetscNew() instead (since version 3.18)\"") PetscNew((b))
870d382aafbSBarry Smith 
871d382aafbSBarry Smith /*MC
872d382aafbSBarry Smith    PetscFree - Frees memory
873d382aafbSBarry Smith 
874eca87e8dSBarry Smith    Synopsis:
875aaa7dc30SBarry Smith     #include <petscsys.h>
876eca87e8dSBarry Smith    PetscErrorCode PetscFree(void *memory)
877eca87e8dSBarry Smith 
878eca87e8dSBarry Smith    Not Collective
879eca87e8dSBarry Smith 
880d382aafbSBarry Smith    Input Parameter:
8817f4976b7SJacob Faibussowitsch .   memory - memory to free (the pointer is ALWAYS set to NULL upon success)
882d382aafbSBarry Smith 
883d382aafbSBarry Smith    Level: beginner
884d382aafbSBarry Smith 
88549d7da52SJed Brown    Notes:
88687497f52SBarry Smith    Do not free memory obtained with `PetscMalloc2()`, `PetscCalloc2()` etc, they must be freed with `PetscFree2()` etc.
887390e1bf2SBarry Smith 
88887497f52SBarry Smith    It is safe to call `PetscFree()` on a NULL pointer.
889d382aafbSBarry Smith 
890fea72eb4SStefano Zampini .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc1()`
891d382aafbSBarry Smith 
892d382aafbSBarry Smith M*/
8933ba16761SJacob Faibussowitsch #define PetscFree(a) ((PetscErrorCode)((*PetscTrFree)((void *)(a), __LINE__, PETSC_FUNCTION_NAME, __FILE__) || ((a) = PETSC_NULLPTR, PETSC_SUCCESS)))
894d382aafbSBarry Smith 
895d382aafbSBarry Smith /*MC
89687497f52SBarry Smith    PetscFree2 - Frees 2 chunks of memory obtained with `PetscMalloc2()`
897d382aafbSBarry Smith 
898eca87e8dSBarry Smith    Synopsis:
899aaa7dc30SBarry Smith     #include <petscsys.h>
900eca87e8dSBarry Smith    PetscErrorCode PetscFree2(void *memory1,void *memory2)
901eca87e8dSBarry Smith 
902eca87e8dSBarry Smith    Not Collective
903eca87e8dSBarry Smith 
904e28ce29aSPatrick Sanan    Input Parameters:
905d382aafbSBarry Smith +   memory1 - memory to free
906d382aafbSBarry Smith -   memory2 - 2nd memory to free
907d382aafbSBarry Smith 
908d382aafbSBarry Smith    Level: developer
909d382aafbSBarry Smith 
91095452b02SPatrick Sanan    Notes:
91187497f52SBarry Smith     Memory must have been obtained with `PetscMalloc2()`
912d382aafbSBarry Smith 
913db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`
914d382aafbSBarry Smith 
915d382aafbSBarry Smith M*/
916ba282f50SJed Brown #define PetscFree2(m1, m2) PetscFreeA(2, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2))
917d382aafbSBarry Smith 
918d382aafbSBarry Smith /*MC
91987497f52SBarry Smith    PetscFree3 - Frees 3 chunks of memory obtained with `PetscMalloc3()`
920d382aafbSBarry Smith 
921eca87e8dSBarry Smith    Synopsis:
922aaa7dc30SBarry Smith     #include <petscsys.h>
923eca87e8dSBarry Smith    PetscErrorCode PetscFree3(void *memory1,void *memory2,void *memory3)
924eca87e8dSBarry Smith 
925eca87e8dSBarry Smith    Not Collective
926eca87e8dSBarry Smith 
927e28ce29aSPatrick Sanan    Input Parameters:
928d382aafbSBarry Smith +   memory1 - memory to free
929d382aafbSBarry Smith .   memory2 - 2nd memory to free
930d382aafbSBarry Smith -   memory3 - 3rd memory to free
931d382aafbSBarry Smith 
932d382aafbSBarry Smith    Level: developer
933d382aafbSBarry Smith 
93495452b02SPatrick Sanan    Notes:
93587497f52SBarry Smith     Memory must have been obtained with `PetscMalloc3()`
936d382aafbSBarry Smith 
937db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`
938d382aafbSBarry Smith 
939d382aafbSBarry Smith M*/
940ba282f50SJed Brown #define PetscFree3(m1, m2, m3) PetscFreeA(3, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3))
941d382aafbSBarry Smith 
942d382aafbSBarry Smith /*MC
94387497f52SBarry Smith    PetscFree4 - Frees 4 chunks of memory obtained with `PetscMalloc4()`
944d382aafbSBarry Smith 
945eca87e8dSBarry Smith    Synopsis:
946aaa7dc30SBarry Smith     #include <petscsys.h>
947eca87e8dSBarry Smith    PetscErrorCode PetscFree4(void *m1,void *m2,void *m3,void *m4)
948eca87e8dSBarry Smith 
949eca87e8dSBarry Smith    Not Collective
950eca87e8dSBarry Smith 
951e28ce29aSPatrick Sanan    Input Parameters:
952d382aafbSBarry Smith +   m1 - memory to free
953d382aafbSBarry Smith .   m2 - 2nd memory to free
954d382aafbSBarry Smith .   m3 - 3rd memory to free
955d382aafbSBarry Smith -   m4 - 4th memory to free
956d382aafbSBarry Smith 
957d382aafbSBarry Smith    Level: developer
958d382aafbSBarry Smith 
95995452b02SPatrick Sanan    Notes:
96087497f52SBarry Smith     Memory must have been obtained with `PetscMalloc4()`
961d382aafbSBarry Smith 
962db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`
963d382aafbSBarry Smith 
964d382aafbSBarry Smith M*/
965ba282f50SJed Brown #define PetscFree4(m1, m2, m3, m4) PetscFreeA(4, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4))
966d382aafbSBarry Smith 
967d382aafbSBarry Smith /*MC
96887497f52SBarry Smith    PetscFree5 - Frees 5 chunks of memory obtained with `PetscMalloc5()`
969d382aafbSBarry Smith 
970eca87e8dSBarry Smith    Synopsis:
971aaa7dc30SBarry Smith     #include <petscsys.h>
972eca87e8dSBarry Smith    PetscErrorCode PetscFree5(void *m1,void *m2,void *m3,void *m4,void *m5)
973eca87e8dSBarry Smith 
974eca87e8dSBarry Smith    Not Collective
975eca87e8dSBarry Smith 
976e28ce29aSPatrick Sanan    Input Parameters:
977d382aafbSBarry Smith +   m1 - memory to free
978d382aafbSBarry Smith .   m2 - 2nd memory to free
979d382aafbSBarry Smith .   m3 - 3rd memory to free
980d382aafbSBarry Smith .   m4 - 4th memory to free
981d382aafbSBarry Smith -   m5 - 5th memory to free
982d382aafbSBarry Smith 
983d382aafbSBarry Smith    Level: developer
984d382aafbSBarry Smith 
98595452b02SPatrick Sanan    Notes:
98687497f52SBarry Smith     Memory must have been obtained with `PetscMalloc5()`
987d382aafbSBarry Smith 
988db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`
989d382aafbSBarry Smith 
990d382aafbSBarry Smith M*/
991ba282f50SJed Brown #define PetscFree5(m1, m2, m3, m4, m5) PetscFreeA(5, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5))
992d382aafbSBarry Smith 
993d382aafbSBarry Smith /*MC
99487497f52SBarry Smith    PetscFree6 - Frees 6 chunks of memory obtained with `PetscMalloc6()`
995d382aafbSBarry Smith 
996eca87e8dSBarry Smith    Synopsis:
997aaa7dc30SBarry Smith     #include <petscsys.h>
998eca87e8dSBarry Smith    PetscErrorCode PetscFree6(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6)
999eca87e8dSBarry Smith 
1000eca87e8dSBarry Smith    Not Collective
1001eca87e8dSBarry Smith 
1002e28ce29aSPatrick Sanan    Input Parameters:
1003d382aafbSBarry Smith +   m1 - memory to free
1004d382aafbSBarry Smith .   m2 - 2nd memory to free
1005d382aafbSBarry Smith .   m3 - 3rd memory to free
1006d382aafbSBarry Smith .   m4 - 4th memory to free
1007d382aafbSBarry Smith .   m5 - 5th memory to free
1008d382aafbSBarry Smith -   m6 - 6th memory to free
1009d382aafbSBarry Smith 
1010d382aafbSBarry Smith    Level: developer
1011d382aafbSBarry Smith 
101295452b02SPatrick Sanan    Notes:
101387497f52SBarry Smith     Memory must have been obtained with `PetscMalloc6()`
1014d382aafbSBarry Smith 
1015db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`
1016d382aafbSBarry Smith 
1017d382aafbSBarry Smith M*/
1018ba282f50SJed Brown #define PetscFree6(m1, m2, m3, m4, m5, m6) PetscFreeA(6, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5), &(m6))
1019d382aafbSBarry Smith 
1020d382aafbSBarry Smith /*MC
102187497f52SBarry Smith    PetscFree7 - Frees 7 chunks of memory obtained with `PetscMalloc7()`
1022d382aafbSBarry Smith 
1023eca87e8dSBarry Smith    Synopsis:
1024aaa7dc30SBarry Smith     #include <petscsys.h>
1025eca87e8dSBarry Smith    PetscErrorCode PetscFree7(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6,void *m7)
1026eca87e8dSBarry Smith 
1027eca87e8dSBarry Smith    Not Collective
1028eca87e8dSBarry Smith 
1029e28ce29aSPatrick Sanan    Input Parameters:
1030d382aafbSBarry Smith +   m1 - memory to free
1031d382aafbSBarry Smith .   m2 - 2nd memory to free
1032d382aafbSBarry Smith .   m3 - 3rd memory to free
1033d382aafbSBarry Smith .   m4 - 4th memory to free
1034d382aafbSBarry Smith .   m5 - 5th memory to free
1035d382aafbSBarry Smith .   m6 - 6th memory to free
1036d382aafbSBarry Smith -   m7 - 7th memory to free
1037d382aafbSBarry Smith 
1038d382aafbSBarry Smith    Level: developer
1039d382aafbSBarry Smith 
104095452b02SPatrick Sanan    Notes:
104187497f52SBarry Smith     Memory must have been obtained with `PetscMalloc7()`
1042d382aafbSBarry Smith 
1043db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`,
1044db781477SPatrick Sanan           `PetscMalloc7()`
1045d382aafbSBarry Smith 
1046d382aafbSBarry Smith M*/
1047ba282f50SJed Brown #define PetscFree7(m1, m2, m3, m4, m5, m6, m7) PetscFreeA(7, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5), &(m6), &(m7))
1048d382aafbSBarry Smith 
1049ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocA(int, PetscBool, int, const char *, const char *, size_t, void *, ...);
1050ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscFreeA(int, int, const char *, const char *, void *, ...);
1051071fcb05SBarry Smith PETSC_EXTERN                PetscErrorCode (*PetscTrMalloc)(size_t, PetscBool, int, const char[], const char[], void **);
1052efca3c55SSatish Balay PETSC_EXTERN                PetscErrorCode (*PetscTrFree)(void *, int, const char[], const char[]);
10533221ece2SMatthew G. Knepley PETSC_EXTERN                PetscErrorCode (*PetscTrRealloc)(size_t, int, const char[], const char[], void **);
1054ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocSetCoalesce(PetscBool);
105592f119d6SBarry 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 **));
1056014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocClear(void);
1057d382aafbSBarry Smith 
1058d382aafbSBarry Smith /*
1059c1706be6SHong 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.
1060c1706be6SHong Zhang */
1061c1706be6SHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocSetDRAM(void);
1062c1706be6SHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocResetDRAM(void);
106331f06eaaSHong Zhang #if defined(PETSC_HAVE_CUDA)
106431f06eaaSHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocSetCUDAHost(void);
106531f06eaaSHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocResetCUDAHost(void);
106631f06eaaSHong Zhang #endif
106759af0bd3SScott Kruger #if defined(PETSC_HAVE_HIP)
106859af0bd3SScott Kruger PETSC_EXTERN PetscErrorCode PetscMallocSetHIPHost(void);
106959af0bd3SScott Kruger PETSC_EXTERN PetscErrorCode PetscMallocResetHIPHost(void);
107059af0bd3SScott Kruger #endif
107159af0bd3SScott Kruger 
1072a5057860SBarry Smith #define MPIU_PETSCLOGDOUBLE  MPI_DOUBLE
107336763ca0SBas van 't Hof #define MPIU_2PETSCLOGDOUBLE MPI_2DOUBLE_PRECISION
1074a5057860SBarry Smith 
1075a5057860SBarry Smith /*
107666d669d6SBarry Smith    Routines for tracing memory corruption/bleeding with default PETSc memory allocation
1077d382aafbSBarry Smith */
1078014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocDump(FILE *);
107992f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocView(FILE *);
1080014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocGetCurrentUsage(PetscLogDouble *);
1081014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocGetMaximumUsage(PetscLogDouble *);
1082e3ed9ee7SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocPushMaximumUsage(int);
1083e3ed9ee7SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocPopMaximumUsage(int, PetscLogDouble *);
108492f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocSetDebug(PetscBool, PetscBool);
108592f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocGetDebug(PetscBool *, PetscBool *, PetscBool *);
1086efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscMallocValidate(int, const char[], const char[]);
108792f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocViewSet(PetscLogDouble);
108892f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocViewGet(PetscBool *);
1089608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeSet(PetscBool);
1090608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeGet(PetscBool *);
1091d382aafbSBarry Smith 
1092014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType, MPI_Datatype *);
1093014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIDataTypeToPetscDataType(MPI_Datatype, PetscDataType *);
1094014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDataTypeGetSize(PetscDataType, size_t *);
10958e4b2d1dSBarry Smith PETSC_EXTERN PetscErrorCode PetscDataTypeFromString(const char *, PetscDataType *, PetscBool *);
1096d382aafbSBarry Smith 
1097d382aafbSBarry Smith /*
1098d382aafbSBarry Smith    These are MPI operations for MPI_Allreduce() etc
1099d382aafbSBarry Smith */
1100367daffbSBarry Smith PETSC_EXTERN MPI_Op MPIU_MAXSUM_OP;
1101570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
1102de272c7aSSatish Balay PETSC_EXTERN MPI_Op MPIU_SUM;
1103014dd563SJed Brown PETSC_EXTERN MPI_Op MPIU_MAX;
1104014dd563SJed Brown PETSC_EXTERN MPI_Op MPIU_MIN;
1105d9822059SBarry Smith #else
1106de272c7aSSatish Balay   #define MPIU_SUM MPI_SUM
1107d9822059SBarry Smith   #define MPIU_MAX MPI_MAX
1108d9822059SBarry Smith   #define MPIU_MIN MPI_MIN
1109d9822059SBarry Smith #endif
111062e5d2d2SJDBetteridge PETSC_EXTERN MPI_Op         Petsc_Garbage_SetIntersectOp;
111162e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscMaxSum(MPI_Comm, const PetscInt[], PetscInt *, PetscInt *);
111262e5d2d2SJDBetteridge 
1113a2498233SPierre Jolivet #if (defined(PETSC_HAVE_REAL___FLOAT128) && !defined(PETSC_SKIP_REAL___FLOAT128)) || (defined(PETSC_HAVE_REAL___FP16) && !defined(PETSC_SKIP_REAL___FP16))
1114613bf2b2SPierre Jolivet /*MC
11159e517322SPierre Jolivet     MPIU_SUM___FP16___FLOAT128 - MPI_Op that acts as a replacement for MPI_SUM with
11169e517322SPierre Jolivet     custom MPI_Datatype `MPIU___FLOAT128`, `MPIU___COMPLEX128`, and `MPIU___FP16`.
1117613bf2b2SPierre Jolivet 
1118613bf2b2SPierre Jolivet    Level: advanced
1119613bf2b2SPierre Jolivet 
1120613bf2b2SPierre Jolivet    Developer Note:
1121613bf2b2SPierre Jolivet    This should be unified with `MPIU_SUM`
1122613bf2b2SPierre Jolivet 
1123613bf2b2SPierre Jolivet .seealso: `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`
1124613bf2b2SPierre Jolivet M*/
11259e517322SPierre Jolivet PETSC_EXTERN MPI_Op MPIU_SUM___FP16___FLOAT128;
1126613bf2b2SPierre Jolivet #endif
1127014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMaxSum(MPI_Comm, const PetscInt[], PetscInt *, PetscInt *);
1128d382aafbSBarry Smith 
112993d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIULong_Send(void *, PetscInt, MPI_Datatype, PetscMPIInt, PetscMPIInt, MPI_Comm) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(1, 3);
113093d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIULong_Recv(void *, PetscInt, MPI_Datatype, PetscMPIInt, PetscMPIInt, MPI_Comm) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(1, 3);
1131eb3f98d2SBarry Smith 
1132639ff905SBarry Smith /*
1133639ff905SBarry Smith     Defines PETSc error handling.
1134639ff905SBarry Smith */
1135639ff905SBarry Smith #include <petscerror.h>
1136d382aafbSBarry Smith 
1137660278c0SBarry Smith PETSC_EXTERN PetscBool   PetscCIEnabled;                    /* code is running in the PETSc test harness CI */
1138660278c0SBarry Smith PETSC_EXTERN PetscBool   PetscCIEnabledPortableErrorOutput; /* error output is stripped to ensure portability of error messages across systems */
1139660278c0SBarry Smith PETSC_EXTERN const char *PetscCIFilename(const char *);
1140660278c0SBarry Smith PETSC_EXTERN int         PetscCILinenumber(int);
1141660278c0SBarry Smith 
1142f3fa974cSJacob Faibussowitsch #define PETSC_SMALLEST_CLASSID ((PetscClassId)1211211)
1143014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_LARGEST_CLASSID;
1144014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_OBJECT_CLASSID;
1145014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscClassIdRegister(const char[], PetscClassId *);
114681256985SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscObjectGetId(PetscObject, PetscObjectId *);
114781256985SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscObjectCompareId(PetscObject, PetscObjectId, PetscBool *);
114881256985SMatthew G. Knepley 
1149d382aafbSBarry Smith /*
1150d382aafbSBarry Smith    Routines that get memory usage information from the OS
1151d382aafbSBarry Smith */
1152014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemoryGetCurrentUsage(PetscLogDouble *);
1153014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemoryGetMaximumUsage(PetscLogDouble *);
1154014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemorySetGetMaximumUsage(void);
1155b44d5720SBarry Smith PETSC_EXTERN PetscErrorCode PetscMemoryTrace(const char[]);
1156d382aafbSBarry Smith 
1157014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSleep(PetscReal);
1158d382aafbSBarry Smith 
1159d382aafbSBarry Smith /*
1160d382aafbSBarry Smith    Initialization of PETSc
1161d382aafbSBarry Smith */
1162014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitialize(int *, char ***, const char[], const char[]);
1163014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeNoPointers(int, char **, const char[], const char[]);
1164014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeNoArguments(void);
1165014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitialized(PetscBool *);
1166014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFinalized(PetscBool *);
1167014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFinalize(void);
1168014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeFortran(void);
1169014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArgs(int *, char ***);
1170014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArguments(char ***);
1171014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFreeArguments(char **);
1172d382aafbSBarry Smith 
1173014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscEnd(void);
1174607a6623SBarry Smith PETSC_EXTERN PetscErrorCode PetscSysInitializePackage(void);
1175d382aafbSBarry Smith 
1176014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonInitialize(const char[], const char[]);
1177014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonFinalize(void);
1178014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonPrintError(void);
1179014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonMonitorSet(PetscObject, const char[]);
1180d382aafbSBarry Smith 
1181a50e088cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMonitorCompare(PetscErrorCode (*)(void), void *, PetscErrorCode (*)(void **), PetscErrorCode (*)(void), void *, PetscErrorCode (*)(void **), PetscBool *);
1182a50e088cSMatthew G. Knepley 
1183d382aafbSBarry Smith /*
1184d382aafbSBarry Smith      These are so that in extern C code we can caste function pointers to non-extern C
11852981ebdbSBarry Smith    function pointers. Since the regular C++ code expects its function pointers to be C++
1186d382aafbSBarry Smith */
1187638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef void (**PetscVoidStarFunction)(void);
1188638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef void (*PetscVoidFunction)(void);
1189638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef PetscErrorCode (*PetscErrorCodeFunction)(void);
1190d382aafbSBarry Smith 
1191d382aafbSBarry Smith /*
1192d382aafbSBarry Smith     Functions that can act on any PETSc object.
1193d382aafbSBarry Smith */
1194014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectDestroy(PetscObject *);
1195014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetComm(PetscObject, MPI_Comm *);
1196014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetClassId(PetscObject, PetscClassId *);
1197014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetClassName(PetscObject, const char *[]);
1198014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetType(PetscObject, const char *[]);
1199014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetName(PetscObject, const char[]);
1200014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetName(PetscObject, const char *[]);
1201014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetTabLevel(PetscObject, PetscInt);
1202014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetTabLevel(PetscObject, PetscInt *);
1203014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectIncrementTabLevel(PetscObject, PetscObject, PetscInt);
1204014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectReference(PetscObject);
1205014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetReference(PetscObject, PetscInt *);
1206014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectDereference(PetscObject);
1207014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetNewTag(PetscObject, PetscMPIInt *);
1208014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectCompose(PetscObject, const char[], PetscObject);
1209014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRemoveReference(PetscObject, const char[]);
1210014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectQuery(PetscObject, const char[], PetscObject *);
1211bdf89e91SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectComposeFunction_Private(PetscObject, const char[], void (*)(void));
1212f3fa974cSJacob Faibussowitsch #define PetscObjectComposeFunction(a, b, d) PetscObjectComposeFunction_Private((a), (b), (PetscVoidFunction)(d))
1213014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetFromOptions(PetscObject);
1214014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetUp(PetscObject);
12150eb63584SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSetPrintedOptions(PetscObject);
12160eb63584SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectInheritPrintedOptions(PetscObject, PetscObject);
1217014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommGetNewTag(MPI_Comm, PetscMPIInt *);
1218d382aafbSBarry Smith 
1219665c2dedSJed Brown #include <petscviewertypes.h>
1220639ff905SBarry Smith #include <petscoptions.h>
1221639ff905SBarry Smith 
1222608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocTraceSet(PetscViewer, PetscBool, PetscLogDouble);
1223608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocTraceGet(PetscBool *);
1224608c71bfSMatthew G. Knepley 
12250633abcbSJed Brown PETSC_EXTERN PetscErrorCode PetscObjectsListGetGlobalNumbering(MPI_Comm, PetscInt, PetscObject *, PetscInt *, PetscInt *);
12260633abcbSJed Brown 
1227c5e4d11fSDmitry Karpeev PETSC_EXTERN PetscErrorCode PetscMemoryView(PetscViewer, const char[]);
1228dae58748SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject, PetscViewer);
1229639ff905SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectView(PetscObject, PetscViewer);
12300005d66cSJed Brown #define PetscObjectQueryFunction(obj, name, fptr) PetscObjectQueryFunction_Private((obj), (name), (PetscVoidFunction *)(fptr))
12310005d66cSJed Brown PETSC_EXTERN PetscErrorCode PetscObjectQueryFunction_Private(PetscObject, const char[], void (**)(void));
1232014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetOptionsPrefix(PetscObject, const char[]);
1233014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject, const char[]);
1234014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject, const char[]);
1235014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject, const char *[]);
1236014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectChangeTypeName(PetscObject, const char[]);
1237014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroy(PetscObject);
1238014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroyAll(void);
1239685405a1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectViewFromOptions(PetscObject, PetscObject, const char[]);
1240014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectName(PetscObject);
1241014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectTypeCompare(PetscObject, const char[], PetscBool *);
1242013e2dc7SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectObjectTypeCompare(PetscObject, PetscObject, PetscBool *);
12434099cc6bSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompare(PetscObject, const char[], PetscBool *);
1244014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1245b9e7e5c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1246014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*)(void));
1247014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRegisterFinalizeAll(void);
1248d382aafbSBarry Smith 
1249e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
12507aab2a10SBarry Smith PETSC_EXTERN PetscErrorCode PetscSAWsBlock(void);
1251e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsViewOff(PetscObject);
1252e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsSetBlock(PetscObject, PetscBool);
1253e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsBlock(PetscObject);
1254e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsGrantAccess(PetscObject);
1255e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsTakeAccess(PetscObject);
1256e04113cfSBarry Smith PETSC_EXTERN void           PetscStackSAWsGrantAccess(void);
1257e04113cfSBarry Smith PETSC_EXTERN void           PetscStackSAWsTakeAccess(void);
1258e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscStackViewSAWs(void);
1259e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscStackSAWsViewOff(void);
126015681b3cSBarry Smith 
1261ec7429eaSBarry Smith #else
12623ba16761SJacob Faibussowitsch   #define PetscSAWsBlock()                  PETSC_SUCCESS
12633ba16761SJacob Faibussowitsch   #define PetscObjectSAWsViewOff(obj)       PETSC_SUCCESS
12643ba16761SJacob Faibussowitsch   #define PetscObjectSAWsSetBlock(obj, flg) PETSC_SUCCESS
12653ba16761SJacob Faibussowitsch   #define PetscObjectSAWsBlock(obj)         PETSC_SUCCESS
12663ba16761SJacob Faibussowitsch   #define PetscObjectSAWsGrantAccess(obj)   PETSC_SUCCESS
12673ba16761SJacob Faibussowitsch   #define PetscObjectSAWsTakeAccess(obj)    PETSC_SUCCESS
12683ba16761SJacob Faibussowitsch   #define PetscStackViewSAWs()              PETSC_SUCCESS
12693ba16761SJacob Faibussowitsch   #define PetscStackSAWsViewOff()           PETSC_SUCCESS
1270e04113cfSBarry Smith   #define PetscStackSAWsTakeAccess()
1271e04113cfSBarry Smith   #define PetscStackSAWsGrantAccess()
127215681b3cSBarry Smith 
1273ec7429eaSBarry Smith #endif
1274b90c6cbeSBarry Smith 
127582b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLOpen(const char[], PetscDLMode, PetscDLHandle *);
127682b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLClose(PetscDLHandle *);
127782b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLSym(PetscDLHandle, const char[], void **);
1278258ec3d2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscDLAddr(void (*)(void), char **);
1279258ec3d2SMatthew G. Knepley #ifdef PETSC_HAVE_CXX
1280258ec3d2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscDemangleSymbol(const char *, char **);
1281258ec3d2SMatthew G. Knepley #endif
12827d5d4d99SBarry Smith 
1283a64a8e02SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocGetStack(void *, PetscStack **);
1284a64a8e02SBarry Smith 
1285dfb7d7afSStefano Zampini PETSC_EXTERN PetscErrorCode PetscObjectsDump(FILE *, PetscBool);
1286140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListDestroy(PetscObjectList *);
1287140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListFind(PetscObjectList, const char[], PetscObject *);
1288140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListReverseFind(PetscObjectList, PetscObject, char **, PetscBool *);
1289140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListAdd(PetscObjectList *, const char[], PetscObject);
1290140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListRemoveReference(PetscObjectList *, const char[]);
1291140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListDuplicate(PetscObjectList, PetscObjectList *);
1292d382aafbSBarry Smith 
1293d382aafbSBarry Smith /*
1294503cfb0cSBarry Smith     Dynamic library lists. Lists of names of routines in objects or in dynamic
1295d382aafbSBarry Smith   link libraries that will be loaded as needed.
1296d382aafbSBarry Smith */
1297a240a19fSJed Brown 
1298a240a19fSJed Brown #define PetscFunctionListAdd(list, name, fptr) PetscFunctionListAdd_Private((list), (name), (PetscVoidFunction)(fptr))
1299ed170139SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *, const char[], PetscVoidFunction);
1300140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListDestroy(PetscFunctionList *);
13010e6b6b59SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFunctionListClear(PetscFunctionList);
13021c9cd337SJed Brown #define PetscFunctionListFind(list, name, fptr) PetscFunctionListFind_Private((list), (name), (PetscVoidFunction *)(fptr))
1303ed170139SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList, const char[], PetscVoidFunction *);
130444ef3d73SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListPrintTypes(MPI_Comm, FILE *, const char[], const char[], const char[], const char[], PetscFunctionList, const char[], const char[]);
1305140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListDuplicate(PetscFunctionList, PetscFunctionList *);
1306140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListView(PetscFunctionList, PetscViewer);
1307140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListGet(PetscFunctionList, const char ***, int *);
13082e956fe4SStefano Zampini PETSC_EXTERN PetscErrorCode PetscFunctionListPrintNonEmpty(PetscFunctionList);
13092e956fe4SStefano Zampini PETSC_EXTERN PetscErrorCode PetscFunctionListPrintAll(void);
1310d382aafbSBarry Smith 
1311014dd563SJed Brown PETSC_EXTERN PetscDLLibrary PetscDLLibrariesLoaded;
1312014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryAppend(MPI_Comm, PetscDLLibrary *, const char[]);
1313014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryPrepend(MPI_Comm, PetscDLLibrary *, const char[]);
1314014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibrarySym(MPI_Comm, PetscDLLibrary *, const char[], const char[], void **);
1315014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryPrintPath(PetscDLLibrary);
1316014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryRetrieve(MPI_Comm, const char[], char *, size_t, PetscBool *);
1317014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryOpen(MPI_Comm, const char[], PetscDLLibrary *);
1318014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryClose(PetscDLLibrary);
1319d382aafbSBarry Smith 
1320d382aafbSBarry Smith /*
1321d382aafbSBarry Smith      Useful utility routines
1322d382aafbSBarry Smith */
1323014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSplitOwnership(MPI_Comm, PetscInt *, PetscInt *);
1324014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSplitOwnershipBlock(MPI_Comm, PetscInt, PetscInt *, PetscInt *);
1325d24d4204SJose E. Roman PETSC_EXTERN PetscErrorCode PetscSplitOwnershipEqual(MPI_Comm, PetscInt *, PetscInt *);
1326014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSequentialPhaseBegin(MPI_Comm, PetscMPIInt);
1327014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSequentialPhaseEnd(MPI_Comm, PetscMPIInt);
1328014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBarrier(PetscObject);
1329014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIDump(FILE *);
133058b5cd2aSSatish Balay PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxInt(MPI_Comm, const PetscInt[2], PetscInt[2]);
133158b5cd2aSSatish Balay PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxReal(MPI_Comm, const PetscReal[2], PetscReal[2]);
1332d382aafbSBarry Smith 
133364ac3b0dSPatrick Sanan /*MC
133487497f52SBarry Smith     PetscNot - negates a logical type value and returns result as a `PetscBool`
1335503cfb0cSBarry Smith 
133664ac3b0dSPatrick Sanan     Level: beginner
133764ac3b0dSPatrick Sanan 
1338a1cb98faSBarry Smith     Note:
1339a1cb98faSBarry Smith     This is useful in cases like
1340a1cb98faSBarry Smith .vb
1341a1cb98faSBarry Smith      int        *a;
1342a1cb98faSBarry Smith      PetscBool  flag = PetscNot(a)
1343a1cb98faSBarry Smith .ve
1344a1cb98faSBarry Smith      where !a would not return a `PetscBool` because we cannot provide a cast from int to `PetscBool` in C.
1345a1cb98faSBarry Smith 
1346a1cb98faSBarry Smith .seealso: `PetscBool`, `PETSC_TRUE`, `PETSC_FALSE`
134764ac3b0dSPatrick Sanan M*/
1348d382aafbSBarry Smith #define PetscNot(a) ((a) ? PETSC_FALSE : PETSC_TRUE)
1349503cfb0cSBarry Smith 
1350d382aafbSBarry Smith /*MC
1351d382aafbSBarry Smith     PetscHelpPrintf - Prints help messages.
1352d382aafbSBarry Smith 
1353cd05f99aSJacob Faibussowitsch     Not Collective, only applies on rank 0; No Fortran Support
1354cf53795eSBarry Smith 
1355d382aafbSBarry Smith    Synopsis:
1356aaa7dc30SBarry Smith     #include <petscsys.h>
1357659f7ba0SBarry Smith      PetscErrorCode (*PetscHelpPrintf)(MPI_Comm comm, const char format[],args);
1358d382aafbSBarry Smith 
1359d382aafbSBarry Smith     Input Parameters:
1360659f7ba0SBarry Smith +  comm - the MPI communicator over which the help message is printed
1361d382aafbSBarry Smith .  format - the usual printf() format string
1362659f7ba0SBarry Smith -  args - arguments to be printed
1363d382aafbSBarry Smith 
1364d382aafbSBarry Smith    Level: developer
1365d382aafbSBarry Smith 
1366659f7ba0SBarry Smith    Note:
1367659f7ba0SBarry Smith      You can change how help messages are printed by replacing the function pointer with a function that does not simply write to stdout.
1368659f7ba0SBarry Smith 
1369659f7ba0SBarry Smith       To use, write your own function, for example,
1370659f7ba0SBarry Smith $PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....)
1371659f7ba0SBarry Smith ${
13723ba16761SJacob Faibussowitsch $ PetscFunctionReturn(PETSC_SUCCESS);
1373659f7ba0SBarry Smith $}
1374d5b43468SJose E. Roman then do the assignment
1375659f7ba0SBarry Smith $    PetscHelpPrintf = mypetschelpprintf;
137687497f52SBarry Smith    You can do the assignment before `PetscInitialize()`.
1377659f7ba0SBarry Smith 
137887497f52SBarry Smith   The default routine used is called `PetscHelpPrintfDefault()`.
1379d382aafbSBarry Smith 
1380db781477SPatrick Sanan .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscErrorPrintf()`
1381d382aafbSBarry Smith M*/
13823ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode (*PetscHelpPrintf)(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1383d382aafbSBarry Smith 
1384fcfd50ebSBarry Smith /*
1385fcfd50ebSBarry Smith      Defines PETSc profiling.
1386fcfd50ebSBarry Smith */
13872c8e378dSBarry Smith #include <petsclog.h>
1388fcfd50ebSBarry Smith 
1389fcfd50ebSBarry Smith /*
1390fcfd50ebSBarry Smith       Simple PETSc parallel IO for ASCII printing
1391fcfd50ebSBarry Smith */
1392014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFixFilename(const char[], char[]);
1393014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFOpen(MPI_Comm, const char[], const char[], FILE **);
1394014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFClose(MPI_Comm, FILE *);
13953ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
13963ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
13973ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSNPrintf(char *, size_t, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
13983ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSNPrintfCount(char *, size_t, const char[], size_t *, ...) PETSC_ATTRIBUTE_FORMAT(3, 5);
13991b5687a1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatRealArray(char[], size_t, const char *, PetscInt, const PetscReal[]);
1400fcfd50ebSBarry Smith 
14013ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscErrorPrintfDefault(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
14023ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscErrorPrintfNone(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
14033ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscHelpPrintfDefault(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1404d382aafbSBarry Smith 
1405d781fa04SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatConvertGetSize(const char *, size_t *);
1406d781fa04SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatConvert(const char *, char *);
1407d781fa04SBarry Smith 
1408d382aafbSBarry Smith #if defined(PETSC_HAVE_POPEN)
1409014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPOpen(MPI_Comm, const char[], const char[], const char[], FILE **);
1410016831caSBarry Smith PETSC_EXTERN PetscErrorCode PetscPClose(MPI_Comm, FILE *);
141174ba8654SBarry Smith PETSC_EXTERN PetscErrorCode PetscPOpenSetMachine(const char[]);
1412d382aafbSBarry Smith #endif
1413d382aafbSBarry Smith 
14143ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSynchronizedPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
14153ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
14160ec8b6e3SBarry Smith PETSC_EXTERN PetscErrorCode PetscSynchronizedFlush(MPI_Comm, FILE *);
1417014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSynchronizedFGets(MPI_Comm, FILE *, size_t, char[]);
1418014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStartMatlab(MPI_Comm, const char[], const char[], FILE **);
1419014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStartJava(MPI_Comm, const char[], const char[], FILE **);
1420014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetPetscDir(const char *[]);
1421d382aafbSBarry Smith 
1422014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_CONTAINER_CLASSID;
1423014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerGetPointer(PetscContainer, void **);
1424014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerSetPointer(PetscContainer, void *);
1425014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerDestroy(PetscContainer *);
1426014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerCreate(MPI_Comm, PetscContainer *);
1427014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerSetUserDestroy(PetscContainer, PetscErrorCode (*)(void *));
1428b61ba218SStefano Zampini PETSC_EXTERN PetscErrorCode PetscContainerUserDestroyDefault(void *);
1429d382aafbSBarry Smith 
1430d382aafbSBarry Smith /*
1431d382aafbSBarry Smith    For use in debuggers
1432d382aafbSBarry Smith */
1433014dd563SJed Brown PETSC_EXTERN PetscMPIInt    PetscGlobalRank;
1434014dd563SJed Brown PETSC_EXTERN PetscMPIInt    PetscGlobalSize;
1435014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIntView(PetscInt, const PetscInt[], PetscViewer);
1436014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRealView(PetscInt, const PetscReal[], PetscViewer);
1437014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscScalarView(PetscInt, const PetscScalar[], PetscViewer);
1438d382aafbSBarry Smith 
1439*bbcf679cSJacob Faibussowitsch /*
1440*bbcf679cSJacob Faibussowitsch     Basic memory and string operations. These are usually simple wrappers
1441*bbcf679cSJacob Faibussowitsch    around the basic Unix system calls, but a few of them have additional
1442*bbcf679cSJacob Faibussowitsch    functionality and/or error checking.
1443*bbcf679cSJacob Faibussowitsch */
1444*bbcf679cSJacob Faibussowitsch #include <petscstring.h>
1445*bbcf679cSJacob Faibussowitsch 
1446a663daf8SBarry Smith #include <stddef.h>
1447d382aafbSBarry Smith #include <stdlib.h>
14486c7e564aSBarry Smith 
14494a92a979SJed Brown #if defined(PETSC_HAVE_XMMINTRIN_H) && !defined(__CUDACC__)
1450d382aafbSBarry Smith   #include <xmmintrin.h>
1451d382aafbSBarry Smith #endif
1452d382aafbSBarry Smith 
1453447bcd8fSJacob Faibussowitsch #if defined(PETSC_CLANG_STATIC_ANALYZER)
1454447bcd8fSJacob Faibussowitsch   #define PetscPrefetchBlock(a, b, c, d)
1455447bcd8fSJacob Faibussowitsch #else
1456d382aafbSBarry Smith   /*MC
1457d382aafbSBarry Smith    PetscPrefetchBlock - Prefetches a block of memory
1458d382aafbSBarry Smith 
1459eca87e8dSBarry Smith    Synopsis:
1460aaa7dc30SBarry Smith     #include <petscsys.h>
1461eca87e8dSBarry Smith     void PetscPrefetchBlock(const anytype *a,size_t n,int rw,int t)
1462eca87e8dSBarry Smith 
1463d382aafbSBarry Smith    Not Collective
1464d382aafbSBarry Smith 
1465d382aafbSBarry Smith    Input Parameters:
1466d382aafbSBarry Smith +  a - pointer to first element to fetch (any type but usually PetscInt or PetscScalar)
1467d382aafbSBarry Smith .  n - number of elements to fetch
1468d382aafbSBarry Smith .  rw - 1 if the memory will be written to, otherwise 0 (ignored by many processors)
146950d8bf02SJed Brown -  t - temporal locality (PETSC_PREFETCH_HINT_{NTA,T0,T1,T2}), see note
1470d382aafbSBarry Smith 
1471d382aafbSBarry Smith    Level: developer
1472d382aafbSBarry Smith 
1473d382aafbSBarry Smith    Notes:
1474d382aafbSBarry Smith    The last two arguments (rw and t) must be compile-time constants.
1475d382aafbSBarry Smith 
147650d8bf02SJed Brown    Adopting Intel's x86/x86-64 conventions, there are four levels of temporal locality.  Not all architectures offer
147750d8bf02SJed Brown    equivalent locality hints, but the following macros are always defined to their closest analogue.
147887497f52SBarry 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).
147987497f52SBarry 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.
148087497f52SBarry Smith .  `PETSC_PREFETCH_HINT_T1` - Fetch to level 2 and higher (not L1).
148187497f52SBarry Smith -  `PETSC_PREFETCH_HINT_T2` - Fetch to high-level cache only.  (On many systems, T0 and T1 are equivalent.)
1482d382aafbSBarry Smith 
1483d382aafbSBarry Smith    This function does nothing on architectures that do not support prefetch and never errors (even if passed an invalid
1484d382aafbSBarry Smith    address).
1485d382aafbSBarry Smith 
1486d382aafbSBarry Smith M*/
14879371c9d4SSatish Balay   #define PetscPrefetchBlock(a, n, rw, t) \
14889371c9d4SSatish Balay     do { \
1489d382aafbSBarry Smith       const char *_p = (const char *)(a), *_end = (const char *)((a) + (n)); \
1490d382aafbSBarry Smith       for (; _p < _end; _p += PETSC_LEVEL1_DCACHE_LINESIZE) PETSC_Prefetch(_p, (rw), (t)); \
1491d382aafbSBarry Smith     } while (0)
1492447bcd8fSJacob Faibussowitsch #endif
1493d382aafbSBarry Smith /*
1494d382aafbSBarry Smith       Determine if some of the kernel computation routines use
1495d382aafbSBarry Smith    Fortran (rather than C) for the numerical calculations. On some machines
1496d382aafbSBarry Smith    and compilers (like complex numbers) the Fortran version of the routines
1497d382aafbSBarry Smith    is faster than the C/C++ versions. The flag --with-fortran-kernels
1498e2e64c6bSBarry Smith    should be used with ./configure to turn these on.
1499d382aafbSBarry Smith */
1500d382aafbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNELS)
1501d382aafbSBarry Smith 
1502d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTCRL)
1503d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTCRL
1504d382aafbSBarry Smith   #endif
1505d382aafbSBarry Smith 
15065a11e1b2SBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJPERM)
15075a11e1b2SBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTAIJPERM
1508d382aafbSBarry Smith   #endif
1509d382aafbSBarry Smith 
1510d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1511d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTAIJ
1512d382aafbSBarry Smith   #endif
1513d382aafbSBarry Smith 
1514d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1515d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ
1516d382aafbSBarry Smith   #endif
1517d382aafbSBarry Smith 
1518d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_NORM)
1519d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_NORM
1520d382aafbSBarry Smith   #endif
1521d382aafbSBarry Smith 
1522d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MAXPY)
1523d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MAXPY
1524d382aafbSBarry Smith   #endif
1525d382aafbSBarry Smith 
1526d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
1527d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ
1528d382aafbSBarry Smith   #endif
1529d382aafbSBarry Smith 
1530d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
1531d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_RELAXAIJ
1532d382aafbSBarry Smith   #endif
1533d382aafbSBarry Smith 
1534d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ)
1535d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ
1536d382aafbSBarry Smith   #endif
1537d382aafbSBarry Smith 
1538d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1539d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ
1540d382aafbSBarry Smith   #endif
1541d382aafbSBarry Smith 
1542d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MDOT)
1543d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MDOT
1544d382aafbSBarry Smith   #endif
1545d382aafbSBarry Smith 
1546d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_XTIMESY)
1547d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_XTIMESY
1548d382aafbSBarry Smith   #endif
1549d382aafbSBarry Smith 
1550d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_AYPX)
1551d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_AYPX
1552d382aafbSBarry Smith   #endif
1553d382aafbSBarry Smith 
1554d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_WAXPY)
1555d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_WAXPY
1556d382aafbSBarry Smith   #endif
1557d382aafbSBarry Smith 
1558d382aafbSBarry Smith #endif
1559d382aafbSBarry Smith 
1560d382aafbSBarry Smith /*
1561d382aafbSBarry Smith     Macros for indicating code that should be compiled with a C interface,
1562d382aafbSBarry Smith    rather than a C++ interface. Any routines that are dynamically loaded
1563d382aafbSBarry Smith    (such as the PCCreate_XXX() routines) must be wrapped so that the name
1564d382aafbSBarry Smith    mangler does not change the functions symbol name. This just hides the
1565d382aafbSBarry Smith    ugly extern "C" {} wrappers.
1566d382aafbSBarry Smith */
1567d382aafbSBarry Smith #if defined(__cplusplus)
1568d382aafbSBarry Smith   #define EXTERN_C_BEGIN extern "C" {
1569d382aafbSBarry Smith   #define EXTERN_C_END   }
1570d382aafbSBarry Smith #else
1571d382aafbSBarry Smith   #define EXTERN_C_BEGIN
1572d382aafbSBarry Smith   #define EXTERN_C_END
1573d382aafbSBarry Smith #endif
1574d382aafbSBarry Smith 
1575d382aafbSBarry Smith /* --------------------------------------------------------------------*/
1576d382aafbSBarry Smith 
1577d382aafbSBarry Smith /*MC
1578d382aafbSBarry Smith     MPI_Comm - the basic object used by MPI to determine which processes are involved in a
1579d382aafbSBarry Smith         communication
1580d382aafbSBarry Smith 
1581d382aafbSBarry Smith    Level: beginner
1582d382aafbSBarry Smith 
1583d382aafbSBarry Smith    Note: This manual page is a place-holder because MPICH does not have a manual page for MPI_Comm
1584d382aafbSBarry Smith 
1585db781477SPatrick Sanan .seealso: `PETSC_COMM_WORLD`, `PETSC_COMM_SELF`
1586d382aafbSBarry Smith M*/
1587d382aafbSBarry Smith 
1588d382aafbSBarry Smith #if defined(PETSC_HAVE_MPIIO)
158993d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_write_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(2, 4);
159093d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_read_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(2, 4);
159193d501b3SJacob 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);
159293d501b3SJacob 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);
159393d501b3SJacob 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);
159493d501b3SJacob 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);
1595d382aafbSBarry Smith #endif
1596d382aafbSBarry Smith 
159761b0d812SBarry Smith /*@C
159887497f52SBarry Smith     PetscIntCast - casts a `PetscInt64` (which is 64 bits in size) to a `PetscInt` (which may be 32 bits in size), generates an
159987497f52SBarry Smith          error if the `PetscInt` is not large enough to hold the number.
1600c73702f5SBarry Smith 
1601c73702f5SBarry Smith    Not Collective
1602c73702f5SBarry Smith 
1603c73702f5SBarry Smith    Input Parameter:
160487497f52SBarry Smith .     a - the `PetscInt64` value
1605c73702f5SBarry Smith 
1606c73702f5SBarry Smith    Output Parameter:
160787497f52SBarry Smith .     b - the resulting `PetscInt` value
1608c73702f5SBarry Smith 
1609c73702f5SBarry Smith    Level: advanced
1610c73702f5SBarry Smith 
161187497f52SBarry Smith    Notes:
161287497f52SBarry 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
1613c73702f5SBarry Smith 
1614c73702f5SBarry Smith    Not available from Fortran
1615c73702f5SBarry Smith 
1616db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscIntMultError()`, `PetscIntSumError()`
1617c73702f5SBarry Smith @*/
1618d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscIntCast(PetscInt64 a, PetscInt *b)
1619d71ae5a4SJacob Faibussowitsch {
1620c73702f5SBarry Smith   PetscFunctionBegin;
16213ca90d2dSJacob Faibussowitsch   *b = 0;
1622f3fa974cSJacob Faibussowitsch   // if using 64-bit indices already then this comparison is tautologically true
1623f3fa974cSJacob 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);
1624f3fa974cSJacob Faibussowitsch   *b = (PetscInt)a;
16253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1626c73702f5SBarry Smith }
1627c73702f5SBarry Smith 
1628c73702f5SBarry Smith /*@C
162987497f52SBarry Smith     PetscCountCast - casts a `PetscCount` to a `PetscInt` (which may be 32 bits in size), generates an
163087497f52SBarry Smith          error if the `PetscInt` is not large enough to hold the number.
1631981bb840SJunchao Zhang 
1632981bb840SJunchao Zhang    Not Collective
1633981bb840SJunchao Zhang 
1634981bb840SJunchao Zhang    Input Parameter:
163587497f52SBarry Smith .     a - the `PetscCount` value
1636981bb840SJunchao Zhang 
1637981bb840SJunchao Zhang    Output Parameter:
163887497f52SBarry Smith .     b - the resulting `PetscInt` value
1639981bb840SJunchao Zhang 
1640981bb840SJunchao Zhang    Level: advanced
1641981bb840SJunchao Zhang 
1642981bb840SJunchao Zhang    Notes:
1643981bb840SJunchao Zhang      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
1644981bb840SJunchao Zhang 
1645981bb840SJunchao Zhang    Not available from Fortran
1646981bb840SJunchao Zhang 
1647db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscIntMultError()`, `PetscIntSumError()`, `PetscIntCast()`
1648981bb840SJunchao Zhang @*/
1649d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscCountCast(PetscCount a, PetscInt *b)
1650d71ae5a4SJacob Faibussowitsch {
1651981bb840SJunchao Zhang   PetscFunctionBegin;
1652981bb840SJunchao Zhang   *b = 0;
1653f3fa974cSJacob 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);
1654f3fa974cSJacob Faibussowitsch   *b = (PetscInt)a;
16553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1656981bb840SJunchao Zhang }
1657981bb840SJunchao Zhang 
1658981bb840SJunchao Zhang /*@C
165987497f52SBarry Smith     PetscBLASIntCast - casts a `PetscInt` (which may be 64 bits in size) to a `PetscBLASInt` (which may be 32 bits in size), generates an
166087497f52SBarry Smith          error if the `PetscBLASInt` is not large enough to hold the number.
166161b0d812SBarry Smith 
166261b0d812SBarry Smith    Not Collective
166361b0d812SBarry Smith 
166461b0d812SBarry Smith    Input Parameter:
166587497f52SBarry Smith .     a - the `PetscInt` value
166661b0d812SBarry Smith 
166761b0d812SBarry Smith    Output Parameter:
166887497f52SBarry Smith .     b - the resulting `PetscBLASInt` value
166961b0d812SBarry Smith 
167061b0d812SBarry Smith    Level: advanced
167161b0d812SBarry Smith 
16720bc7d38cSBarry Smith    Notes:
16731fd49c25SBarry Smith    Not available from Fortran
167487497f52SBarry Smith 
1675f0b7f91aSBarry Smith    Errors if the integer is negative since PETSc calls to BLAS/LAPACK never need to cast negative integer inputs
16761fd49c25SBarry Smith 
1677db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscIntCast()`, `PetscCountCast()`
167861b0d812SBarry Smith @*/
1679d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscBLASIntCast(PetscInt a, PetscBLASInt *b)
1680d71ae5a4SJacob Faibussowitsch {
1681c5df96a5SBarry Smith   PetscFunctionBegin;
168254c59aa7SJacob Faibussowitsch   *b = 0;
168354c59aa7SJacob Faibussowitsch   if (PetscDefined(USE_64BIT_INDICES) && !PetscDefined(HAVE_64BIT_BLAS_INDICES)) {
168454c59aa7SJacob 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);
168554c59aa7SJacob Faibussowitsch   }
168654c59aa7SJacob Faibussowitsch   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer to BLAS/LAPACK routine");
1687f3fa974cSJacob Faibussowitsch   *b = (PetscBLASInt)a;
16883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1689c5df96a5SBarry Smith }
1690c5df96a5SBarry Smith 
169161b0d812SBarry Smith /*@C
169287497f52SBarry Smith     PetscCuBLASIntCast - like `PetscBLASIntCast()`, but for `PetscCuBLASInt`.
1693eee0c0a6SToby Isaac 
1694eee0c0a6SToby Isaac    Not Collective
1695eee0c0a6SToby Isaac 
1696eee0c0a6SToby Isaac    Input Parameter:
169787497f52SBarry Smith .     a - the `PetscInt` value
1698eee0c0a6SToby Isaac 
1699eee0c0a6SToby Isaac    Output Parameter:
170087497f52SBarry Smith .     b - the resulting `PetscCuBLASInt` value
1701eee0c0a6SToby Isaac 
1702eee0c0a6SToby Isaac    Level: advanced
1703eee0c0a6SToby Isaac 
1704eee0c0a6SToby Isaac    Notes:
1705eee0c0a6SToby Isaac       Errors if the integer is negative since PETSc calls to cuBLAS and friends never need to cast negative integer inputs
1706eee0c0a6SToby Isaac 
1707db781477SPatrick Sanan .seealso: `PetscCuBLASInt`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscMPIIntCast()`, `PetscIntCast()`
1708eee0c0a6SToby Isaac @*/
1709d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscCuBLASIntCast(PetscInt a, PetscCuBLASInt *b)
1710d71ae5a4SJacob Faibussowitsch {
1711eee0c0a6SToby Isaac   PetscFunctionBegin;
171254c59aa7SJacob Faibussowitsch   *b = 0;
1713f3fa974cSJacob 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);
1714f3fa974cSJacob Faibussowitsch   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer %" PetscInt_FMT "to cuBLAS routine", a);
1715f3fa974cSJacob Faibussowitsch   *b = (PetscCuBLASInt)a;
17163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1717eee0c0a6SToby Isaac }
1718eee0c0a6SToby Isaac 
1719eee0c0a6SToby Isaac /*@C
172047d993e7Ssuyashtn     PetscHipBLASIntCast - like `PetscBLASIntCast()`, but for `PetscHipBLASInt`.
172147d993e7Ssuyashtn 
172247d993e7Ssuyashtn    Not Collective
172347d993e7Ssuyashtn 
172447d993e7Ssuyashtn    Input Parameter:
172547d993e7Ssuyashtn .     a - the `PetscInt` value
172647d993e7Ssuyashtn 
172747d993e7Ssuyashtn    Output Parameter:
172847d993e7Ssuyashtn .     b - the resulting `PetscHipBLASInt` value
172947d993e7Ssuyashtn 
173047d993e7Ssuyashtn    Level: advanced
173147d993e7Ssuyashtn 
173247d993e7Ssuyashtn    Notes:
173347d993e7Ssuyashtn       Errors if the integer is negative since PETSc calls to hipBLAS and friends never need to cast negative integer inputs
173447d993e7Ssuyashtn 
173547d993e7Ssuyashtn .seealso: `PetscHipBLASInt`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscMPIIntCast()`, `PetscIntCast()`
173647d993e7Ssuyashtn @*/
173747d993e7Ssuyashtn static inline PetscErrorCode PetscHipBLASIntCast(PetscInt a, PetscHipBLASInt *b)
173847d993e7Ssuyashtn {
173947d993e7Ssuyashtn   PetscFunctionBegin;
174047d993e7Ssuyashtn   *b = 0;
1741f3fa974cSJacob 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);
1742f3fa974cSJacob Faibussowitsch   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer %" PetscInt_FMT "to hipBLAS routine", a);
1743f3fa974cSJacob Faibussowitsch   *b = (PetscHipBLASInt)a;
17443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
174547d993e7Ssuyashtn }
174647d993e7Ssuyashtn 
174747d993e7Ssuyashtn /*@C
174847d993e7Ssuyashtn     PetscMPIIntCast - casts a PetscInt (which may be 64 bits in size) to a PetscMPIInt (which may be 32 bits in size), generates an
174947d993e7Ssuyashtn          error if the PetscMPIInt is not large enough to hold the number.
175061b0d812SBarry Smith 
175161b0d812SBarry Smith    Not Collective
175261b0d812SBarry Smith 
175361b0d812SBarry Smith    Input Parameter:
175487497f52SBarry Smith .     a - the `PetscInt` value
175561b0d812SBarry Smith 
175661b0d812SBarry Smith    Output Parameter:
175787497f52SBarry Smith .     b - the resulting `PetscMPIInt` value
175861b0d812SBarry Smith 
175961b0d812SBarry Smith    Level: advanced
176061b0d812SBarry Smith 
17611fd49c25SBarry Smith    Not available from Fortran
17621fd49c25SBarry Smith 
1763db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscIntCast()`
176461b0d812SBarry Smith @*/
1765d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscMPIIntCast(PetscInt a, PetscMPIInt *b)
1766d71ae5a4SJacob Faibussowitsch {
17674dc2109aSBarry Smith   PetscFunctionBegin;
176854c59aa7SJacob Faibussowitsch   *b = 0;
1769f3fa974cSJacob 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);
1770f3fa974cSJacob Faibussowitsch   *b = (PetscMPIInt)a;
17713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17724dc2109aSBarry Smith }
17734dc2109aSBarry Smith 
1774f3fa974cSJacob Faibussowitsch #define PetscInt64Mult(a, b) (((PetscInt64)(a)) * ((PetscInt64)(b)))
17752f18eb33SBarry Smith 
17762f18eb33SBarry Smith /*@C
1777f3fa974cSJacob Faibussowitsch   PetscRealIntMultTruncate - Computes the product of a positive `PetscReal` and a positive
1778f3fa974cSJacob Faibussowitsch   `PetscInt` and truncates the value to slightly less than the maximal possible value.
17792f18eb33SBarry Smith 
1780f3fa974cSJacob Faibussowitsch   Not Collective, Not available from Fortran
17812f18eb33SBarry Smith 
1782d8d19677SJose E. Roman   Input Parameters:
1783f3fa974cSJacob Faibussowitsch + a - The `PetscReal` value
1784f3fa974cSJacob Faibussowitsch - b - The `PetscInt` value
17852f18eb33SBarry Smith 
1786f3fa974cSJacob Faibussowitsch   Notes:
1787f3fa974cSJacob Faibussowitsch   Returns the result as a `PetscInt` value.
17882f18eb33SBarry Smith 
1789f3fa974cSJacob Faibussowitsch   Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`.
1790f3fa974cSJacob Faibussowitsch   Use `PetscIntMultTruncate()` to compute the product of two positive `PetscInt` and truncate
1791f3fa974cSJacob Faibussowitsch   to fit a `PetscInt`.
1792f3fa974cSJacob Faibussowitsch   Use `PetscIntMultError()` to compute the product of two `PetscInt` if you wish to generate an
1793f3fa974cSJacob Faibussowitsch   error if the result will not fit in a `PetscInt`.
17942f18eb33SBarry Smith 
1795e28ce29aSPatrick Sanan   Developers Note:
1796f3fa974cSJacob Faibussowitsch   We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but
1797f3fa974cSJacob Faibussowitsch   requires many more checks.
17982f18eb33SBarry Smith 
1799f3fa974cSJacob Faibussowitsch   This is used where we compute approximate sizes for workspace and need to insure the
1800f3fa974cSJacob Faibussowitsch   workspace is index-able.
18011fd49c25SBarry Smith 
18022f18eb33SBarry Smith   Level: advanced
18032f18eb33SBarry Smith 
1804f3fa974cSJacob Faibussowitsch .seealso: `PetscReal`, `PetscInt`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`
18052f18eb33SBarry Smith @*/
1806d71ae5a4SJacob Faibussowitsch static inline PetscInt PetscRealIntMultTruncate(PetscReal a, PetscInt b)
1807d71ae5a4SJacob Faibussowitsch {
18085f80ce2aSJacob Faibussowitsch   PetscInt64 r = (PetscInt64)(a * (PetscReal)b);
18092f18eb33SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
18102f18eb33SBarry Smith   return (PetscInt)r;
18112f18eb33SBarry Smith }
18122f18eb33SBarry Smith 
18132f18eb33SBarry Smith /*@C
18142f18eb33SBarry Smith 
181587497f52SBarry Smith    PetscIntMultTruncate - Computes the product of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value
18162f18eb33SBarry Smith 
18172f18eb33SBarry Smith    Not Collective
18182f18eb33SBarry Smith 
1819d8d19677SJose E. Roman    Input Parameters:
18202f18eb33SBarry Smith +     a - the PetscInt value
18212f18eb33SBarry Smith -     b - the second value
18222f18eb33SBarry Smith 
1823390e1bf2SBarry Smith    Returns:
182487497f52SBarry Smith       the result as a `PetscInt` value
18252f18eb33SBarry Smith 
182687497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`
182787497f52SBarry Smith    Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`
182887497f52SBarry 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`
18292f18eb33SBarry Smith 
18301fd49c25SBarry Smith    Not available from Fortran
18311fd49c25SBarry Smith 
183287497f52SBarry Smith    Developers Note:
183387497f52SBarry Smith    We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but requires many more checks.
18342f18eb33SBarry Smith 
18352f18eb33SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
18362f18eb33SBarry Smith 
18372f18eb33SBarry Smith    Level: advanced
18382f18eb33SBarry Smith 
1839db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`
18402f18eb33SBarry Smith @*/
1841d71ae5a4SJacob Faibussowitsch static inline PetscInt PetscIntMultTruncate(PetscInt a, PetscInt b)
1842d71ae5a4SJacob Faibussowitsch {
18435f80ce2aSJacob Faibussowitsch   PetscInt64 r = PetscInt64Mult(a, b);
18442f18eb33SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
18452f18eb33SBarry Smith   return (PetscInt)r;
18462f18eb33SBarry Smith }
18472f18eb33SBarry Smith 
1848f91af8c7SBarry Smith /*@C
1849f91af8c7SBarry Smith 
185087497f52SBarry Smith    PetscIntSumTruncate - Computes the sum of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value
1851f91af8c7SBarry Smith 
1852f91af8c7SBarry Smith    Not Collective
1853f91af8c7SBarry Smith 
1854d8d19677SJose E. Roman    Input Parameters:
185587497f52SBarry Smith +     a - the `PetscInt` value
1856f91af8c7SBarry Smith -     b - the second value
1857f91af8c7SBarry Smith 
1858390e1bf2SBarry Smith    Returns:
185987497f52SBarry Smith      the result as a `PetscInt` value
1860f91af8c7SBarry Smith 
186187497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`
186287497f52SBarry Smith    Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`
186387497f52SBarry 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`
1864f91af8c7SBarry Smith 
1865f91af8c7SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
1866f91af8c7SBarry Smith 
1867f5f57ec0SBarry Smith    Not available from Fortran
1868f5f57ec0SBarry Smith 
1869f91af8c7SBarry Smith    Level: advanced
1870f91af8c7SBarry Smith 
1871db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
1872f91af8c7SBarry Smith @*/
1873d71ae5a4SJacob Faibussowitsch static inline PetscInt PetscIntSumTruncate(PetscInt a, PetscInt b)
1874d71ae5a4SJacob Faibussowitsch {
18755f80ce2aSJacob Faibussowitsch   PetscInt64 r = ((PetscInt64)a) + ((PetscInt64)b);
1876f91af8c7SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
1877f91af8c7SBarry Smith   return (PetscInt)r;
1878f91af8c7SBarry Smith }
1879f91af8c7SBarry Smith 
18802f18eb33SBarry Smith /*@C
18812f18eb33SBarry Smith 
188287497f52SBarry Smith    PetscIntMultError - Computes the product of two positive `PetscInt` and generates an error with overflow.
18832f18eb33SBarry Smith 
18842f18eb33SBarry Smith    Not Collective
18852f18eb33SBarry Smith 
1886d8d19677SJose E. Roman    Input Parameters:
188787497f52SBarry Smith +     a - the `PetscInt` value
18882f18eb33SBarry Smith -     b - the second value
18892f18eb33SBarry Smith 
1890d8d19677SJose E. Roman    Output Parameter:
189187497f52SBarry 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
18922f18eb33SBarry Smith 
189387497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` and store in a `PetscInt64`
189487497f52SBarry Smith    Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`
18952f18eb33SBarry Smith 
1896f5f57ec0SBarry Smith    Not available from Fortran
1897f5f57ec0SBarry Smith 
189887497f52SBarry Smith    Developers Note:
189987497f52SBarry Smith    We currently assume that `PetscInt` addition does not overflow, this is obviously wrong but requires many more checks.
19002f18eb33SBarry Smith 
19012f18eb33SBarry Smith    Level: advanced
19022f18eb33SBarry Smith 
1903db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscIntMult64()`, `PetscIntSumError()`
19042f18eb33SBarry Smith @*/
1905d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscIntMultError(PetscInt a, PetscInt b, PetscInt *result)
1906d71ae5a4SJacob Faibussowitsch {
1907f3fa974cSJacob Faibussowitsch   PetscInt64 r = PetscInt64Mult(a, b);
19082f18eb33SBarry Smith 
19092f18eb33SBarry Smith   PetscFunctionBegin;
1910f3fa974cSJacob Faibussowitsch   if (!PetscDefined(USE_64BIT_INDICES)) {
1911f3fa974cSJacob 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);
1912f3fa974cSJacob Faibussowitsch   }
1913f91af8c7SBarry Smith   if (result) *result = (PetscInt)r;
19143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1915f91af8c7SBarry Smith }
1916f91af8c7SBarry Smith 
1917f91af8c7SBarry Smith /*@C
1918f91af8c7SBarry Smith 
191987497f52SBarry Smith    PetscIntSumError - Computes the sum of two positive `PetscInt` and generates an error with overflow.
1920f91af8c7SBarry Smith 
1921f91af8c7SBarry Smith    Not Collective
1922f91af8c7SBarry Smith 
1923d8d19677SJose E. Roman    Input Parameters:
192487497f52SBarry Smith +     a - the `PetscInt` value
1925f91af8c7SBarry Smith -     b - the second value
1926f91af8c7SBarry Smith 
1927d8d19677SJose E. Roman    Output Parameter:
192887497f52SBarry 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
1929f91af8c7SBarry Smith 
193087497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two 32 bit PetscInt and store in a `PetscInt64`
193187497f52SBarry Smith    Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`
1932f91af8c7SBarry Smith 
1933f5f57ec0SBarry Smith    Not available from Fortran
1934f5f57ec0SBarry Smith 
1935f91af8c7SBarry Smith    Level: advanced
1936f91af8c7SBarry Smith 
1937db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
1938f91af8c7SBarry Smith @*/
1939d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscIntSumError(PetscInt a, PetscInt b, PetscInt *result)
1940d71ae5a4SJacob Faibussowitsch {
1941f3fa974cSJacob Faibussowitsch   PetscInt64 r = ((PetscInt64)a) + ((PetscInt64)b);
1942f91af8c7SBarry Smith 
1943f91af8c7SBarry Smith   PetscFunctionBegin;
1944f3fa974cSJacob Faibussowitsch   if (!PetscDefined(USE_64BIT_INDICES)) {
1945f3fa974cSJacob 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);
1946f3fa974cSJacob Faibussowitsch   }
1947f91af8c7SBarry Smith   if (result) *result = (PetscInt)r;
19483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19492f18eb33SBarry Smith }
1950c5df96a5SBarry Smith 
1951d382aafbSBarry Smith /*
19522981ebdbSBarry Smith      The IBM include files define hz, here we hide it so that it may be used as a regular user variable.
1953d382aafbSBarry Smith */
1954d382aafbSBarry Smith #if defined(hz)
1955d382aafbSBarry Smith   #undef hz
1956d382aafbSBarry Smith #endif
1957d382aafbSBarry Smith 
195815a5570dSLisandro Dalcin #include <limits.h>
195915a5570dSLisandro Dalcin 
196015a5570dSLisandro Dalcin /* The number of bits in a byte */
196115a5570dSLisandro Dalcin 
196215a5570dSLisandro Dalcin #define PETSC_BITS_PER_BYTE CHAR_BIT
196315a5570dSLisandro Dalcin 
1964d382aafbSBarry Smith #if defined(PETSC_HAVE_SYS_TYPES_H)
1965d382aafbSBarry Smith   #include <sys/types.h>
1966d382aafbSBarry Smith #endif
1967d382aafbSBarry Smith 
1968d382aafbSBarry Smith /*MC
1969d382aafbSBarry Smith 
197066d79e26SBarry 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++
197166d79e26SBarry Smith                     and Fortran compilers when petscsys.h is included.
197266d79e26SBarry Smith 
19736274ba02SSatish 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>
197466d79e26SBarry Smith 
197566d79e26SBarry 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)
197666d79e26SBarry Smith 
197766d79e26SBarry 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
197866d79e26SBarry Smith     where only a change in the major version number (x) indicates a change in the API.
197966d79e26SBarry Smith 
198066d79e26SBarry 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
198166d79e26SBarry Smith 
198266d79e26SBarry 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),
198366d79e26SBarry 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
198466d79e26SBarry Smith     version number (x.y.z).
198566d79e26SBarry Smith 
198687497f52SBarry Smith     `PETSC_RELEASE_DATE` is the date the x.y version was released (i.e. the version before any patch releases)
198766d79e26SBarry Smith 
198887497f52SBarry Smith     `PETSC_VERSION_DATE` is the date the x.y.z version was released
198966d79e26SBarry Smith 
199087497f52SBarry Smith     `PETSC_VERSION_GIT` is the last git commit to the repository given in the form vx.y.z-wwwww
199166d79e26SBarry Smith 
199287497f52SBarry Smith     `PETSC_VERSION_DATE_GIT` is the date of the last git commit to the repository
199366d79e26SBarry Smith 
199487497f52SBarry Smith     `PETSC_VERSION_()` is deprecated and will eventually be removed.
199566d79e26SBarry Smith 
1996cbc12506SBarry Smith     Level: intermediate
199766d79e26SBarry Smith 
199866d79e26SBarry Smith M*/
199966d79e26SBarry Smith 
2000014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArchType(char[], size_t);
2001014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetHostName(char[], size_t);
2002014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetUserName(char[], size_t);
2003014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetProgramName(char[], size_t);
2004014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetProgramName(const char[]);
2005014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetDate(char[], size_t);
200627710113SBarry Smith PETSC_EXTERN PetscErrorCode PetscGetVersion(char[], size_t);
20075f309d01SBarry Smith PETSC_EXTERN PetscErrorCode PetscGetVersionNumber(PetscInt *, PetscInt *, PetscInt *, PetscInt *);
2008d382aafbSBarry Smith 
20096a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedInt(PetscInt, const PetscInt[], PetscBool *);
201062e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscSortedInt64(PetscInt, const PetscInt64[], PetscBool *);
20116a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedMPIInt(PetscInt, const PetscMPIInt[], PetscBool *);
20126a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedReal(PetscInt, const PetscReal[], PetscBool *);
2013014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortInt(PetscInt, PetscInt[]);
201462e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscSortInt64(PetscInt, PetscInt64[]);
201562e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscSortCount(PetscInt, PetscCount[]);
2016ce605777SToby Isaac PETSC_EXTERN PetscErrorCode PetscSortReverseInt(PetscInt, PetscInt[]);
201722ab5688SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscSortedRemoveDupsInt(PetscInt *, PetscInt[]);
2018b6a18d21SVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedCheckDupsInt(PetscInt, const PetscInt[], PetscBool *);
2019014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsInt(PetscInt *, PetscInt[]);
2020f1cab4e1SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscCheckDupsInt(PetscInt, const PetscInt[], PetscBool *);
202160e03357SMatthew G Knepley PETSC_EXTERN PetscErrorCode PetscFindInt(PetscInt, PetscInt, const PetscInt[], PetscInt *);
2022d2aeb606SJed Brown PETSC_EXTERN PetscErrorCode PetscFindMPIInt(PetscMPIInt, PetscInt, const PetscMPIInt[], PetscInt *);
2023014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithPermutation(PetscInt, const PetscInt[], PetscInt[]);
2024014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortStrWithPermutation(PetscInt, const char *[], PetscInt[]);
2025014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithArray(PetscInt, PetscInt[], PetscInt[]);
2026981bb840SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortIntWithCountArray(PetscCount, PetscInt[], PetscCount[]);
20276c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscSortIntWithArrayPair(PetscInt, PetscInt[], PetscInt[], PetscInt[]);
2028981bb840SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortIntWithIntCountArrayPair(PetscCount, PetscInt[], PetscInt[], PetscCount[]);
202917d7d925SStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortMPIInt(PetscInt, PetscMPIInt[]);
203017d7d925SStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsMPIInt(PetscInt *, PetscMPIInt[]);
2031014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithArray(PetscMPIInt, PetscMPIInt[], PetscMPIInt[]);
20325569a785SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithIntArray(PetscMPIInt, PetscMPIInt[], PetscInt[]);
2033014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithScalarArray(PetscInt, PetscInt[], PetscScalar[]);
203417df18f2SToby Isaac PETSC_EXTERN PetscErrorCode PetscSortIntWithDataArray(PetscInt, PetscInt[], void *, size_t, void *);
2035014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortReal(PetscInt, PetscReal[]);
203639f41f7fSStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortRealWithArrayInt(PetscInt, PetscReal[], PetscInt[]);
2037014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortRealWithPermutation(PetscInt, const PetscReal[], PetscInt[]);
2038745b41b2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsReal(PetscInt *, PetscReal[]);
203939f41f7fSStefano Zampini PETSC_EXTERN PetscErrorCode PetscFindReal(PetscReal, PetscInt, const PetscReal[], PetscReal, PetscInt *);
2040014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortSplit(PetscInt, PetscInt, PetscScalar[], PetscInt[]);
2041014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortSplitReal(PetscInt, PetscInt, PetscReal[], PetscInt[]);
2042014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscProcessTree(PetscInt, const PetscBool[], const PetscInt[], PetscInt *, PetscInt **, PetscInt **, PetscInt **, PetscInt **);
20436c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscMergeIntArrayPair(PetscInt, const PetscInt[], const PetscInt[], PetscInt, const PetscInt[], const PetscInt[], PetscInt *, PetscInt **, PetscInt **);
20446c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscMergeIntArray(PetscInt, const PetscInt[], PetscInt, const PetscInt[], PetscInt *, PetscInt **);
2045e498c390SJed Brown PETSC_EXTERN PetscErrorCode PetscMergeMPIIntArray(PetscInt, const PetscMPIInt[], PetscInt, const PetscMPIInt[], PetscInt *, PetscMPIInt **);
2046ce605777SToby Isaac PETSC_EXTERN PetscErrorCode PetscParallelSortedInt(MPI_Comm, PetscInt, const PetscInt[], PetscBool *);
2047ce605777SToby Isaac 
20484d3610e3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscTimSort(PetscInt, void *, size_t, int (*)(const void *, const void *, void *), void *);
2049676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrdered(PetscInt, PetscInt[]);
2050676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrdered(PetscInt, PetscMPIInt[]);
2051676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrdered(PetscInt, PetscReal[]);
20524d3610e3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscTimSortWithArray(PetscInt, void *, size_t, void *, size_t, int (*)(const void *, const void *, void *), void *);
2053676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrderedWithArray(PetscInt, PetscInt[], PetscInt[]);
2054676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrderedWithArray(PetscInt, PetscMPIInt[], PetscMPIInt[]);
2055676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrderedWithArrayInt(PetscInt, PetscReal[], PetscInt[]);
2056676f2a66SJacob Faibussowitsch 
2057014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDisplay(void);
2058014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetDisplay(char[], size_t);
2059d382aafbSBarry Smith 
206076bdecfbSBarry Smith /*J
2061d382aafbSBarry Smith     PetscRandomType - String with the name of a PETSc randomizer
2062d382aafbSBarry Smith 
2063d382aafbSBarry Smith    Level: beginner
2064d382aafbSBarry Smith 
2065e28ce29aSPatrick Sanan    Notes:
206687497f52SBarry Smith    To use `PETSCSPRNG` or `PETSCRANDOM123` you must have ./configure PETSc
206725ccb61fSToby Isaac    with the option --download-sprng or --download-random123
2068d382aafbSBarry Smith 
2069db781477SPatrick Sanan .seealso: `PetscRandomSetType()`, `PetscRandom`, `PetscRandomCreate()`
207076bdecfbSBarry Smith J*/
207119fd82e9SBarry Smith typedef const char *PetscRandomType;
2072d382aafbSBarry Smith #define PETSCRAND      "rand"
2073d382aafbSBarry Smith #define PETSCRAND48    "rand48"
2074d382aafbSBarry Smith #define PETSCSPRNG     "sprng"
2075c5e4d11fSDmitry Karpeev #define PETSCRANDER48  "rander48"
207625ccb61fSToby Isaac #define PETSCRANDOM123 "random123"
2077808ba619SStefano Zampini #define PETSCCURAND    "curand"
2078d382aafbSBarry Smith 
2079d382aafbSBarry Smith /* Logging support */
2080014dd563SJed Brown PETSC_EXTERN PetscClassId PETSC_RANDOM_CLASSID;
2081d382aafbSBarry Smith 
2082607a6623SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomInitializePackage(void);
2083d382aafbSBarry Smith 
2084d382aafbSBarry Smith /* Dynamic creation and loading functions */
2085140e18c1SBarry Smith PETSC_EXTERN PetscFunctionList PetscRandomList;
2086d382aafbSBarry Smith 
2087bdf89e91SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomRegister(const char[], PetscErrorCode (*)(PetscRandom));
208819fd82e9SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomSetType(PetscRandom, PetscRandomType);
2089014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetFromOptions(PetscRandom);
209019fd82e9SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomGetType(PetscRandom, PetscRandomType *);
2091fe2efc57SMark PETSC_EXTERN PetscErrorCode PetscRandomViewFromOptions(PetscRandom, PetscObject, const char[]);
2092014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomView(PetscRandom, PetscViewer);
2093d382aafbSBarry Smith 
2094014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomCreate(MPI_Comm, PetscRandom *);
2095014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetValue(PetscRandom, PetscScalar *);
2096014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetValueReal(PetscRandom, PetscReal *);
2097808ba619SStefano Zampini PETSC_EXTERN PetscErrorCode PetscRandomGetValues(PetscRandom, PetscInt, PetscScalar *);
2098808ba619SStefano Zampini PETSC_EXTERN PetscErrorCode PetscRandomGetValuesReal(PetscRandom, PetscInt, PetscReal *);
2099014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetInterval(PetscRandom, PetscScalar *, PetscScalar *);
2100014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetInterval(PetscRandom, PetscScalar, PetscScalar);
2101014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetSeed(PetscRandom, unsigned long);
2102014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetSeed(PetscRandom, unsigned long *);
2103014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSeed(PetscRandom);
2104014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomDestroy(PetscRandom *);
2105d382aafbSBarry Smith 
2106014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetFullPath(const char[], char[], size_t);
2107014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetRelativePath(const char[], char[], size_t);
2108014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetWorkingDirectory(char[], size_t);
2109014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetRealPath(const char[], char[]);
2110014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetHomeDirectory(char[], size_t);
2111014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTestFile(const char[], char, PetscBool *);
2112014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTestDirectory(const char[], char, PetscBool *);
211397540f07SSatish Balay PETSC_EXTERN PetscErrorCode PetscMkdir(const char[]);
21148a10d460SHong Zhang PETSC_EXTERN PetscErrorCode PetscMkdtemp(char[]);
211597540f07SSatish Balay PETSC_EXTERN PetscErrorCode PetscRMTree(const char[]);
2116d382aafbSBarry Smith 
2117d71ae5a4SJacob Faibussowitsch static inline PetscBool PetscBinaryBigEndian(void)
2118d71ae5a4SJacob Faibussowitsch {
21199371c9d4SSatish Balay   long _petsc_v = 1;
21209371c9d4SSatish Balay   return ((char *)&_petsc_v)[0] ? PETSC_FALSE : PETSC_TRUE;
21219371c9d4SSatish Balay }
212230815ce0SLisandro Dalcin 
21239860990eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinaryRead(int, void *, PetscInt, PetscInt *, PetscDataType);
21249860990eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedRead(MPI_Comm, int, void *, PetscInt, PetscInt *, PetscDataType);
2125f253e43cSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinaryWrite(int, const void *, PetscInt, PetscDataType);
2126f253e43cSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedWrite(MPI_Comm, int, const void *, PetscInt, PetscDataType);
2127014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinaryOpen(const char[], PetscFileMode, int *);
2128014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinaryClose(int);
2129014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSharedTmp(MPI_Comm, PetscBool *);
2130014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSharedWorkingDirectory(MPI_Comm, PetscBool *);
2131014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetTmp(MPI_Comm, char[], size_t);
2132014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFileRetrieve(MPI_Comm, const char[], char[], size_t, PetscBool *);
2133014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLs(MPI_Comm, const char[], char[], size_t, PetscBool *);
2134c891a504SStefano Zampini #if defined(PETSC_USE_SOCKET_VIEWER)
2135e2fc02c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscOpenSocket(const char[], int, int *);
2136c891a504SStefano Zampini #endif
2137d382aafbSBarry Smith 
2138014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinarySeek(int, off_t, PetscBinarySeekType, off_t *);
2139014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedSeek(MPI_Comm, int, off_t, PetscBinarySeekType, off_t *);
2140014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscByteSwap(void *, PetscDataType, PetscInt);
2141d382aafbSBarry Smith 
2142014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebugTerminal(const char[]);
2143014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebugger(const char[], PetscBool);
2144014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDefaultDebugger(void);
2145014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebuggerFromString(const char *);
2146014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAttachDebugger(void);
2147014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStopForDebugger(void);
21482a2a2941SBarry Smith PETSC_EXTERN PetscErrorCode PetscWaitOnError(void);
2149d382aafbSBarry Smith 
2150014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherNumberOfMessages(MPI_Comm, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt *);
2151014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], PetscMPIInt **, PetscMPIInt **);
2152014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths2(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt **, PetscMPIInt **, PetscMPIInt **);
2153014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPostIrecvInt(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscInt ***, MPI_Request **);
2154014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPostIrecvScalar(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscScalar ***, MPI_Request **);
215593d501b3SJacob 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);
215693d501b3SJacob 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);
215793d501b3SJacob 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);
2158d382aafbSBarry Smith 
21596145cd65SJed Brown PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedSetType(MPI_Comm, PetscBuildTwoSidedType);
21606145cd65SJed Brown PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedGetType(MPI_Comm, PetscBuildTwoSidedType *);
21616145cd65SJed Brown 
2162014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSSEIsEnabled(MPI_Comm, PetscBool *, PetscBool *);
2163d382aafbSBarry Smith 
2164ce94432eSBarry Smith PETSC_EXTERN MPI_Comm PetscObjectComm(PetscObject);
2165ce94432eSBarry Smith 
2166d382aafbSBarry Smith struct _n_PetscSubcomm {
2167d382aafbSBarry Smith   MPI_Comm         parent;    /* parent communicator */
2168d382aafbSBarry Smith   MPI_Comm         dupparent; /* duplicate parent communicator, under which the processors of this subcomm have contiguous rank */
2169306c2d5bSBarry Smith   MPI_Comm         child;     /* the sub-communicator */
217045487dadSJed Brown   PetscMPIInt      n;         /* num of subcommunicators under the parent communicator */
217145487dadSJed Brown   PetscMPIInt      color;     /* color of processors belong to this communicator */
2172708d824cSJed Brown   PetscMPIInt     *subsize;   /* size of subcommunicator[color] */
217345487dadSJed Brown   PetscSubcommType type;
2174e5acf8a4SHong Zhang   char            *subcommprefix;
2175d382aafbSBarry Smith };
2176d382aafbSBarry Smith 
2177d71ae5a4SJacob Faibussowitsch static inline MPI_Comm PetscSubcommParent(PetscSubcomm scomm)
2178d71ae5a4SJacob Faibussowitsch {
21799371c9d4SSatish Balay   return scomm->parent;
21809371c9d4SSatish Balay }
2181d71ae5a4SJacob Faibussowitsch static inline MPI_Comm PetscSubcommChild(PetscSubcomm scomm)
2182d71ae5a4SJacob Faibussowitsch {
21839371c9d4SSatish Balay   return scomm->child;
21849371c9d4SSatish Balay }
2185d71ae5a4SJacob Faibussowitsch static inline MPI_Comm PetscSubcommContiguousParent(PetscSubcomm scomm)
2186d71ae5a4SJacob Faibussowitsch {
21879371c9d4SSatish Balay   return scomm->dupparent;
21889371c9d4SSatish Balay }
2189014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommCreate(MPI_Comm, PetscSubcomm *);
2190014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommDestroy(PetscSubcomm *);
2191014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommSetNumber(PetscSubcomm, PetscInt);
2192014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommSetType(PetscSubcomm, PetscSubcommType);
219365d9b8f1SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetTypeGeneral(PetscSubcomm, PetscMPIInt, PetscMPIInt);
2194053d1c95SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommView(PetscSubcomm, PetscViewer);
2195f68be91cSHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetFromOptions(PetscSubcomm);
2196e5acf8a4SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetOptionsPrefix(PetscSubcomm, const char[]);
2197a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetParent(PetscSubcomm, MPI_Comm *);
2198a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetContiguousParent(PetscSubcomm, MPI_Comm *);
2199a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetChild(PetscSubcomm, MPI_Comm *);
2200d382aafbSBarry Smith 
22019962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapCreate(PetscInt, PetscHeap *);
22029962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapAdd(PetscHeap, PetscInt, PetscInt);
22039962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapPop(PetscHeap, PetscInt *, PetscInt *);
22049962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapPeek(PetscHeap, PetscInt *, PetscInt *);
22059962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapStash(PetscHeap, PetscInt, PetscInt);
22069962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapUnstash(PetscHeap);
22079962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapDestroy(PetscHeap *);
22089962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapView(PetscHeap, PetscViewer);
22099962606eSBarry Smith 
22105e71baefSBarry Smith PETSC_EXTERN PetscErrorCode PetscProcessPlacementView(PetscViewer);
22115f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGet(MPI_Comm, PetscShmComm *);
22125f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGlobalToLocal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
22135f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommLocalToGlobal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
22145f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGetMpiShmComm(PetscShmComm, MPI_Comm *);
22155e71baefSBarry Smith 
2216a32e93adSJunchao Zhang /* routines to better support OpenMP multithreading needs of some PETSc third party libraries */
2217a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlCreate(MPI_Comm, PetscInt, PetscOmpCtrl *);
2218a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlGetOmpComms(PetscOmpCtrl, MPI_Comm *, MPI_Comm *, PetscBool *);
2219a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlDestroy(PetscOmpCtrl *);
2220a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlBarrier(PetscOmpCtrl);
2221a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterBegin(PetscOmpCtrl);
2222a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterEnd(PetscOmpCtrl);
2223a32e93adSJunchao Zhang 
222413e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferCreate(size_t, size_t, PetscSegBuffer *);
22250f453b92SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferDestroy(PetscSegBuffer *);
222613e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferGet(PetscSegBuffer, size_t, void *);
2227137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractAlloc(PetscSegBuffer, void *);
2228137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractTo(PetscSegBuffer, void *);
2229137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractInPlace(PetscSegBuffer, void *);
223013e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferGetSize(PetscSegBuffer, size_t *);
223113e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferUnuse(PetscSegBuffer, size_t);
22320f453b92SJed Brown 
2233471ecdacSJed Brown /* Type-safe wrapper to encourage use of PETSC_RESTRICT. Does not use PetscFunctionBegin because the error handling
2234471ecdacSJed Brown  * prevents the compiler from completely erasing the stub. This is called in inner loops so it has to be as fast as
2235471ecdacSJed Brown  * possible. */
2236d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscSegBufferGetInts(PetscSegBuffer seg, size_t count, PetscInt *PETSC_RESTRICT *slot)
2237d71ae5a4SJacob Faibussowitsch {
22389371c9d4SSatish Balay   return PetscSegBufferGet(seg, count, (void **)slot);
22399371c9d4SSatish Balay }
2240d382aafbSBarry Smith 
22419de0f6ecSBarry Smith extern PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton;
22429de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *);
22439de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *);
22449de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted, const char *, const char *, PetscBool *);
22459de0f6ecSBarry Smith 
2246ace2fbaeSBarry Smith #include <stdarg.h>
2247ace2fbaeSBarry Smith PETSC_EXTERN PetscErrorCode PetscVSNPrintf(char *, size_t, const char[], size_t *, va_list);
2248ace2fbaeSBarry Smith PETSC_EXTERN                PetscErrorCode (*PetscVFPrintf)(FILE *, const char[], va_list);
2249ace2fbaeSBarry Smith 
2250268b0dfdSSatish Balay PETSC_EXTERN PetscSegBuffer PetscCitationsList;
2251fbfcfee5SBarry Smith 
22521f817a21SBarry Smith /*@C
22531f817a21SBarry Smith       PetscCitationsRegister - Register a bibtex item to obtain credit for an implemented algorithm used in the code.
22541f817a21SBarry Smith 
225587497f52SBarry Smith      Not Collective - only what is registered on rank 0 of `PETSC_COMM_WORLD` will be printed
22561f817a21SBarry Smith 
22571f817a21SBarry Smith      Input Parameters:
225835cb6cd3SPierre Jolivet +      cite - the bibtex item, formatted to displayed on multiple lines nicely
225987497f52SBarry Smith -      set - a boolean variable initially set to `PETSC_FALSE`; this is used to insure only a single registration of the citation
22601f817a21SBarry Smith 
22613c7db156SBarry Smith      Options Database: Key
22623c7db156SBarry Smith .     -citations [filename]   - print out the bibtex entries for the given computation
22633c7db156SBarry Smith 
2264850545d0SSatish Balay      Level: intermediate
2265850545d0SSatish Balay 
22663c7db156SBarry Smith      Fortran Note:
2267f5f57ec0SBarry Smith      Not available from Fortran
2268f5f57ec0SBarry Smith 
22691f817a21SBarry Smith @*/
2270d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscCitationsRegister(const char cit[], PetscBool *set)
2271d71ae5a4SJacob Faibussowitsch {
2272dff31646SBarry Smith   size_t len;
2273dff31646SBarry Smith   char  *vstring;
2274dff31646SBarry Smith 
22751f817a21SBarry Smith   PetscFunctionBegin;
22763ba16761SJacob Faibussowitsch   if (set && *set) PetscFunctionReturn(PETSC_SUCCESS);
22779566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(cit, &len));
22789566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferGet(PetscCitationsList, len, &vstring));
22799566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(vstring, cit, len));
2280dff31646SBarry Smith   if (set) *set = PETSC_TRUE;
22813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2282dff31646SBarry Smith }
2283dff31646SBarry Smith 
2284d8174014SToby Isaac PETSC_EXTERN                PETSC_DEPRECATED_FUNCTION("Google has discontinued its URL shortener service") PetscErrorCode PetscURLShorten(const char[], char[], size_t c);
2285fe278a28SBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm, char[], char[], size_t);
2286fe278a28SBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm, const char[], char[], size_t);
2287f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveUpload(MPI_Comm, const char[], const char[]);
2288fe278a28SBarry Smith 
2289f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscBoxAuthorize(MPI_Comm, char[], char[], size_t);
2290f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscBoxRefresh(MPI_Comm, const char[], char[], char[], size_t);
2291b967cddfSBarry Smith 
22929f4d3c52SBarry Smith PETSC_EXTERN PetscErrorCode PetscGlobusGetTransfers(MPI_Comm, const char[], char[], size_t);
22939f4d3c52SBarry Smith 
229404102261SBarry Smith PETSC_EXTERN PetscErrorCode PetscTextBelt(MPI_Comm, const char[], const char[], PetscBool *);
2295763ec7b1SBarry Smith PETSC_EXTERN PetscErrorCode PetscTellMyCell(MPI_Comm, const char[], const char[], PetscBool *);
2296dff31646SBarry Smith 
229768e69593SBarry Smith PETSC_EXTERN PetscErrorCode PetscPullJSONValue(const char[], const char[], char[], size_t, PetscBool *);
22985dc0f0a4SBarry Smith PETSC_EXTERN PetscErrorCode PetscPushJSONValue(char[], const char[], const char[], size_t);
2299d382aafbSBarry Smith 
2300b2566f29SBarry Smith #if defined(PETSC_USE_DEBUG)
2301d71ae5a4SJacob Faibussowitsch static inline unsigned int PetscStrHash(const char *str)
2302d71ae5a4SJacob Faibussowitsch {
2303df05ca09SBarry Smith   unsigned int c, hash = 5381;
2304df05ca09SBarry Smith 
2305503e1a2cSLisandro Dalcin   while ((c = (unsigned int)*str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
2306df05ca09SBarry Smith   return hash;
2307df05ca09SBarry Smith }
230877a5e07dSBarry Smith 
230977a5e07dSBarry Smith   /*MC
231087497f52SBarry Smith    MPIU_Allreduce - a PETSc replacement for `MPI_Allreduce()` that tries to determine if the call from all the MPI ranks occur from the
231187497f52SBarry Smith                     same place in the PETSc code. This helps to detect bugs where different MPI ranks follow different code paths
231287497f52SBarry Smith                     resulting in inconsistent and incorrect calls to `MPI_Allreduce()`.
231377a5e07dSBarry Smith 
2314d083f849SBarry Smith    Collective
231577a5e07dSBarry Smith 
231677a5e07dSBarry Smith    Synopsis:
231777a5e07dSBarry Smith      #include <petscsys.h>
231877a5e07dSBarry Smith      PetscErrorCode MPIU_Allreduce(void *indata,void *outdata,PetscMPIInt count,MPI_Datatype datatype, MPI_Op op, MPI_Comm comm);
231977a5e07dSBarry Smith 
232077a5e07dSBarry Smith    Input Parameters:
2321df05ca09SBarry Smith +  a - pointer to the input data to be reduced
2322df05ca09SBarry Smith .  c - the number of MPI data items in a and b
232387497f52SBarry Smith .  d - the MPI datatype, for example `MPI_INT`
232487497f52SBarry Smith .  e - the MPI operation, for example `MPI_SUM`
2325df05ca09SBarry Smith -  fcomm - the MPI communicator on which the operation occurs
232677a5e07dSBarry Smith 
232777a5e07dSBarry Smith    Output Parameter:
2328df05ca09SBarry Smith .  b - the reduced values
232977a5e07dSBarry Smith 
2330e28ce29aSPatrick Sanan    Notes:
233187497f52SBarry Smith      In optimized mode this directly calls `MPI_Allreduce()`
233277a5e07dSBarry Smith 
2333df05ca09SBarry Smith      This is defined as a macro that can return error codes internally so it cannot be used in a subroutine that returns void.
2334df05ca09SBarry Smith 
233587497f52SBarry 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
2336df05ca09SBarry Smith 
233777a5e07dSBarry Smith    Level: developer
233877a5e07dSBarry Smith 
2339db781477SPatrick Sanan .seealso: `MPI_Allreduce()`
234077a5e07dSBarry Smith M*/
23413ba16761SJacob Faibussowitsch   // clang-format off
23429371c9d4SSatish Balay #define MPIU_Allreduce(a, b, c, d, e, fcomm) \
23433ba16761SJacob Faibussowitsch   PetscMacroReturnStandard( \
23443ba16761SJacob Faibussowitsch     PetscMPIInt a_b1[6], a_b2[6]; \
23453ba16761SJacob Faibussowitsch     int _mpiu_allreduce_c_int = (int)(c); \
23463ba16761SJacob Faibussowitsch     a_b1[0] = -(PetscMPIInt)__LINE__; \
23473ba16761SJacob Faibussowitsch     a_b1[1] = -a_b1[0]; \
23483ba16761SJacob Faibussowitsch     a_b1[2] = -(PetscMPIInt)PetscStrHash(PETSC_FUNCTION_NAME); \
23493ba16761SJacob Faibussowitsch     a_b1[3] = -a_b1[2]; \
23503ba16761SJacob Faibussowitsch     a_b1[4] = -(PetscMPIInt)(c); \
23513ba16761SJacob Faibussowitsch     a_b1[5] = -a_b1[4]; \
23523ba16761SJacob Faibussowitsch     \
23533ba16761SJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(a_b1, a_b2, 6, MPI_INT, MPI_MAX, fcomm)); \
23543ba16761SJacob 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"); \
23553ba16761SJacob Faibussowitsch     PetscCheck(-a_b2[2] == a_b2[3], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MPI_Allreduce() called in different locations (functions) on different processors"); \
23563ba16761SJacob 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); \
23573ba16761SJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce((a), (b), (c), (d), (e), (fcomm)));)
23583ba16761SJacob Faibussowitsch // clang-format on
2359b2566f29SBarry Smith #else
2360f3fa974cSJacob Faibussowitsch   #define MPIU_Allreduce(a, b, c, d, e, fcomm) PetscMacroReturnStandard(PetscCallMPI(MPI_Allreduce((a), (b), (c), (d), (e), (fcomm))))
2361b2566f29SBarry Smith #endif
2362b2566f29SBarry Smith 
2363b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
23648198064fSBarry Smith PETSC_EXTERN PetscErrorCode MPIU_Win_allocate_shared(MPI_Aint, PetscMPIInt, MPI_Info, MPI_Comm, void *, MPI_Win *);
23658198064fSBarry Smith PETSC_EXTERN PetscErrorCode MPIU_Win_shared_query(MPI_Win, PetscMPIInt, MPI_Aint *, PetscMPIInt *, void *);
23662f065d89SBarry Smith #endif
23678198064fSBarry Smith 
2368cd88fca5SStefano Zampini /* this is a vile hack */
2369cd88fca5SStefano Zampini #if defined(PETSC_HAVE_NECMPI)
23709371c9d4SSatish 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)
2371cd88fca5SStefano Zampini     #define MPI_Type_free(a) (*(a) = MPI_DATATYPE_NULL, 0);
2372cd88fca5SStefano Zampini   #endif
2373961fb248SStefano Zampini #endif
2374cd88fca5SStefano Zampini 
237512801b39SBarry Smith /*
237660fbe2beSVaclav Hapla     List of external packages and queries on it
237760fbe2beSVaclav Hapla */
237860fbe2beSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscHasExternalPackage(const char[], PetscBool *);
237960fbe2beSVaclav Hapla 
2380a52b435bSMark Adams /*
2381a52b435bSMark Adams  OpenMP support
2382a52b435bSMark Adams */
2383a52b435bSMark Adams #if defined(_OPENMP)
2384a52b435bSMark Adams   #define PetscPragmaOMP(...) _Pragma(PetscStringize(omp __VA_ARGS__))
2385a52b435bSMark Adams #else // no OpenMP so no threads
2386a52b435bSMark Adams   #define PetscPragmaOMP(...)
2387a52b435bSMark Adams #endif
2388a52b435bSMark Adams 
2389f1f2ae84SBarry Smith /* this cannot go here because it may be in a different shared library */
2390f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPIServerBegin(void);
2391f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPIServerEnd(void);
2392f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPICommsDestroy(void);
2393d382aafbSBarry Smith #endif
2394