xref: /petsc/include/petscsys.h (revision 013e2dc7137d1d9dc4e6b44a828a8fb0f1bd6f29)
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.
4d382aafbSBarry Smith */
526bd1501SBarry Smith #if !defined(PETSCSYS_H)
626bd1501SBarry Smith #define PETSCSYS_H
73ca90d2dSJacob Faibussowitsch 
8d382aafbSBarry Smith /* ========================================================================== */
9d382aafbSBarry Smith /*
10d382aafbSBarry Smith    petscconf.h is contained in ${PETSC_ARCH}/include/petscconf.h it is
111c77a0c5SBarry Smith    found automatically by the compiler due to the -I${PETSC_DIR}/${PETSC_ARCH}/include that
121c77a0c5SBarry Smith    PETSc's makefiles add to the compiler rules.
131c77a0c5SBarry Smith    For --prefix installs the directory ${PETSC_ARCH} does not exist and petscconf.h is in the same
141cc8b206SBarry Smith    directory as the other PETSc include files.
15d382aafbSBarry Smith */
162c8e378dSBarry Smith #include <petscconf.h>
171c77a0c5SBarry Smith #include <petscconf_poison.h>
182c8e378dSBarry Smith #include <petscfix.h>
19bde483f2SJacob Faibussowitsch #include <petscmacros.h>
2033bb201bSJacob Faibussowitsch 
21ac09b921SBarry Smith /* SUBMANSEC = Sys */
22ac09b921SBarry Smith 
2373fca5a0SBarry Smith #if defined(PETSC_DESIRE_FEATURE_TEST_MACROS)
2473fca5a0SBarry Smith /*
2573fca5a0SBarry Smith    Feature test macros must be included before headers defined by IEEE Std 1003.1-2001
2673fca5a0SBarry Smith    We only turn these in PETSc source files that require them by setting PETSC_DESIRE_FEATURE_TEST_MACROS
2773fca5a0SBarry Smith */
28216f1ae6SBarry Smith #if defined(PETSC__POSIX_C_SOURCE_200112L) && !defined(_POSIX_C_SOURCE)
2973fca5a0SBarry Smith #define _POSIX_C_SOURCE 200112L
3073fca5a0SBarry Smith #endif
31216f1ae6SBarry Smith #if defined(PETSC__BSD_SOURCE) && !defined(_BSD_SOURCE)
3273fca5a0SBarry Smith #define _BSD_SOURCE
3373fca5a0SBarry Smith #endif
3407f00807SSatish Balay #if defined(PETSC__DEFAULT_SOURCE) && !defined(_DEFAULT_SOURCE)
3507f00807SSatish Balay #define _DEFAULT_SOURCE
3607f00807SSatish Balay #endif
37ea0fecefSShri Abhyankar #if defined(PETSC__GNU_SOURCE) && !defined(_GNU_SOURCE)
38ea0fecefSShri Abhyankar #define _GNU_SOURCE
39ea0fecefSShri Abhyankar #endif
4073fca5a0SBarry Smith #endif
4173fca5a0SBarry Smith 
42df4397b0SStefano Zampini #include <petscsystypes.h>
43df4397b0SStefano Zampini 
44d382aafbSBarry Smith /* ========================================================================== */
45d382aafbSBarry Smith 
46d382aafbSBarry Smith /*
47d382aafbSBarry Smith     Defines the interface to MPI allowing the use of all MPI functions.
48d382aafbSBarry Smith 
49d382aafbSBarry Smith     PETSc does not use the C++ binding of MPI at ALL. The following flag
50d382aafbSBarry Smith     makes sure the C++ bindings are not included. The C++ bindings REQUIRE
51d382aafbSBarry Smith     putting mpi.h before ANY C++ include files, we cannot control this
52d382aafbSBarry Smith     with all PETSc users. Users who want to use the MPI C++ bindings can include
53d382aafbSBarry Smith     mpicxx.h directly in their code
54d382aafbSBarry Smith */
55fa9e63e2SJed Brown #if !defined(MPICH_SKIP_MPICXX)
56d382aafbSBarry Smith #define MPICH_SKIP_MPICXX 1
57fa9e63e2SJed Brown #endif
58fa9e63e2SJed Brown #if !defined(OMPI_SKIP_MPICXX)
59d382aafbSBarry Smith #define OMPI_SKIP_MPICXX 1
60fa9e63e2SJed Brown #endif
61e771154cSJed Brown #if defined(PETSC_HAVE_MPIUNI)
62e771154cSJed Brown #include <petsc/mpiuni/mpi.h>
63e771154cSJed Brown #else
642c8e378dSBarry Smith #include <mpi.h>
65e771154cSJed Brown #endif
6665013866SBarry Smith 
67d382aafbSBarry Smith /*
68a7886beaSBarry Smith    Perform various sanity checks that the correct mpi.h is being included at compile time.
69a7886beaSBarry Smith    This usually happens because
70a7886beaSBarry Smith       * either an unexpected mpi.h is in the default compiler path (i.e. in /usr/include) or
71a7886beaSBarry Smith       * an extra include path -I/something (which contains the unexpected mpi.h) is being passed to the compiler
72a7886beaSBarry Smith */
73a7886beaSBarry Smith #if defined(PETSC_HAVE_MPIUNI)
74c9b973beSBarry Smith #if !defined(MPIUNI_H)
75a7886beaSBarry Smith #error "PETSc was configured with --with-mpi=0 but now appears to be compiling using a different mpi.h"
76a7886beaSBarry Smith #endif
7741f4af4cSSatish Balay #elif defined(PETSC_HAVE_I_MPI_NUMVERSION)
7841f4af4cSSatish Balay #if !defined(I_MPI_NUMVERSION)
7941f4af4cSSatish Balay #error "PETSc was configured with I_MPI but now appears to be compiling using a non-I_MPI mpi.h"
8041f4af4cSSatish Balay #elif I_MPI_NUMVERSION != PETSC_HAVE_I_MPI_NUMVERSION
8141f4af4cSSatish 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"
8241f4af4cSSatish Balay #endif
8341f4af4cSSatish Balay #elif defined(PETSC_HAVE_MVAPICH2_NUMVERSION)
8441f4af4cSSatish Balay #if !defined(MVAPICH2_NUMVERSION)
8541f4af4cSSatish Balay #error "PETSc was configured with MVAPICH2 but now appears to be compiling using a non-MVAPICH2 mpi.h"
8641f4af4cSSatish Balay #elif MVAPICH2_NUMVERSION != PETSC_HAVE_MVAPICH2_NUMVERSION
8741f4af4cSSatish Balay #error "PETSc was configured with one MVAPICH2 mpi.h version but now appears to be compiling using a different MVAPICH2 mpi.h version"
8841f4af4cSSatish Balay #endif
89a7886beaSBarry Smith #elif defined(PETSC_HAVE_MPICH_NUMVERSION)
9041f4af4cSSatish Balay #if !defined(MPICH_NUMVERSION) || defined(MVAPICH2_NUMVERSION) || defined(I_MPI_NUMVERSION)
91a7886beaSBarry Smith #error "PETSc was configured with MPICH but now appears to be compiling using a non-MPICH mpi.h"
921c284764SMin 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)
93a7886beaSBarry Smith #error "PETSc was configured with one MPICH mpi.h version but now appears to be compiling using a different MPICH mpi.h version"
94a7886beaSBarry Smith #endif
95a7886beaSBarry Smith #elif defined(PETSC_HAVE_OMPI_MAJOR_VERSION)
96a7886beaSBarry Smith #if !defined(OMPI_MAJOR_VERSION)
97a7886beaSBarry Smith #error "PETSc was configured with OpenMPI but now appears to be compiling using a non-OpenMPI mpi.h"
981c284764SMin 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)
99c3ac57cfSSatish Balay #error "PETSc was configured with one OpenMPI mpi.h version but now appears to be compiling using a different OpenMPI mpi.h version"
100a7886beaSBarry Smith #endif
101c93fae50SJacob Faibussowitsch #define PETSC_MPI_COMM_FMT "p"
102c93fae50SJacob Faibussowitsch #define PETSC_MPI_WIN_FMT  "p"
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 
113c93fae50SJacob Faibussowitsch /* Format specifier for printing MPI_Comm (most implementations use 'int' as type) */
114c93fae50SJacob Faibussowitsch #if !defined(PETSC_MPI_COMM_FMT)
115c93fae50SJacob Faibussowitsch #define PETSC_MPI_COMM_FMT "d"
116c93fae50SJacob Faibussowitsch #endif
117c93fae50SJacob Faibussowitsch 
118c93fae50SJacob Faibussowitsch #if !defined(PETSC_MPI_WIN_FMT)
119c93fae50SJacob Faibussowitsch #define PETSC_MPI_WIN_FMT "d"
120c93fae50SJacob Faibussowitsch #endif
121c93fae50SJacob Faibussowitsch 
122a7886beaSBarry Smith /*
1232981ebdbSBarry Smith     Need to put stdio.h AFTER mpi.h for MPICH2 with C++ compiler
124d382aafbSBarry Smith     see the top of mpicxx.h in the MPICH2 distribution.
125d382aafbSBarry Smith */
126d382aafbSBarry Smith #include <stdio.h>
127d382aafbSBarry Smith 
128d382aafbSBarry Smith /* MSMPI on 32bit windows requires this yukky hack - that breaks MPI standard compliance */
129d382aafbSBarry Smith #if !defined(MPIAPI)
130d382aafbSBarry Smith #define MPIAPI
131d382aafbSBarry Smith #endif
132d382aafbSBarry Smith 
1331cc8b206SBarry Smith /*
1341cc8b206SBarry Smith    Support for Clang (>=3.2) matching type tag arguments with void* buffer types.
1351cc8b206SBarry Smith    This allows the compiler to detect cases where the MPI datatype argument passed to a MPI routine
1361cc8b206SBarry Smith    does not match the actual type of the argument being passed in
1371cc8b206SBarry Smith */
138c5e4d11fSDmitry Karpeev #if defined(__has_attribute) && defined(works_with_const_which_is_not_true)
139894dd566SJed Brown #if __has_attribute(argument_with_type_tag) && __has_attribute(pointer_with_type_tag) && __has_attribute(type_tag_for_datatype)
140894dd566SJed Brown #define PetscAttrMPIPointerWithType(bufno, typeno) __attribute__((pointer_with_type_tag(MPI, bufno, typeno)))
1418ad47952SJed Brown #define PetscAttrMPITypeTag(type)                  __attribute__((type_tag_for_datatype(MPI, type)))
1428ad47952SJed Brown #define PetscAttrMPITypeTagLayoutCompatible(type)  __attribute__((type_tag_for_datatype(MPI, type, layout_compatible)))
143894dd566SJed Brown #endif
144894dd566SJed Brown #endif
145894dd566SJed Brown #if !defined(PetscAttrMPIPointerWithType)
146894dd566SJed Brown #define PetscAttrMPIPointerWithType(bufno, typeno)
1478ad47952SJed Brown #define PetscAttrMPITypeTag(type)
1488ad47952SJed Brown #define PetscAttrMPITypeTagLayoutCompatible(type)
149894dd566SJed Brown #endif
150b3506946SBarry Smith 
1515a576424SJed Brown PETSC_EXTERN MPI_Datatype MPIU_ENUM PetscAttrMPITypeTag(PetscEnum);
152df4397b0SStefano Zampini PETSC_EXTERN MPI_Datatype MPIU_BOOL PetscAttrMPITypeTag(PetscBool);
153e28ce29aSPatrick Sanan 
154e28ce29aSPatrick Sanan /*MC
15587497f52SBarry Smith    MPIU_INT - Portable MPI datatype corresponding to `PetscInt` independent of the precision of `PetscInt`
156e28ce29aSPatrick Sanan 
157e28ce29aSPatrick Sanan    Notes:
15887497f52SBarry Smith    In MPI calls that require an MPI datatype that matches a `PetscInt` or array of `PetscInt` values, pass this value.
159e28ce29aSPatrick Sanan 
160e28ce29aSPatrick Sanan    Level: beginner
161e28ce29aSPatrick Sanan 
162db781477SPatrick Sanan .seealso: `PetscReal`, `PetscScalar`, `PetscComplex`, `PetscInt`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`
163e28ce29aSPatrick Sanan M*/
164e28ce29aSPatrick Sanan 
1657cdaf61dSJed Brown PETSC_EXTERN MPI_Datatype MPIU_FORTRANADDR;
166b7b8f77aSBarry Smith 
167d382aafbSBarry Smith #if defined(PETSC_USE_64BIT_INDICES)
168bd84f1cfSSatish Balay #define MPIU_INT     MPIU_INT64
169a05e1a72SSatish Balay #define PetscInt_FMT PetscInt64_FMT
170d382aafbSBarry Smith #else
171d382aafbSBarry Smith #define MPIU_INT     MPI_INT
172a05e1a72SSatish Balay #define PetscInt_FMT "d"
173d382aafbSBarry Smith #endif
174d382aafbSBarry Smith 
175503cfb0cSBarry Smith /*
176503cfb0cSBarry Smith     For the rare cases when one needs to send a size_t object with MPI
177503cfb0cSBarry Smith */
178e316c87fSJed Brown PETSC_EXTERN MPI_Datatype MPIU_SIZE_T;
179d382aafbSBarry Smith 
180d382aafbSBarry Smith /*
181d382aafbSBarry Smith       You can use PETSC_STDOUT as a replacement of stdout. You can also change
182d382aafbSBarry Smith     the value of PETSC_STDOUT to redirect all standard output elsewhere
183d382aafbSBarry Smith */
184014dd563SJed Brown PETSC_EXTERN FILE *PETSC_STDOUT;
185d382aafbSBarry Smith 
186d382aafbSBarry Smith /*
187d382aafbSBarry Smith       You can use PETSC_STDERR as a replacement of stderr. You can also change
188d382aafbSBarry Smith     the value of PETSC_STDERR to redirect all standard error elsewhere
189d382aafbSBarry Smith */
190014dd563SJed Brown PETSC_EXTERN FILE *PETSC_STDERR;
191d382aafbSBarry Smith 
192c8b9133aSSatish Balay /* PetscPragmaSIMD - from CeedPragmaSIMD */
193c8b9133aSSatish Balay 
1945c993f7fSStefano Zampini #if defined(__NEC__)
1955c993f7fSStefano Zampini #define PetscPragmaSIMD _Pragma("_NEC ivdep")
1965c993f7fSStefano Zampini #elif defined(__INTEL_COMPILER) && !defined(_WIN32)
197c8b9133aSSatish Balay #define PetscPragmaSIMD _Pragma("vector")
1981dba8834SJose E. Roman #elif defined(__GNUC__) && __GNUC__ >= 5 && !defined(__PGI)
199c8b9133aSSatish Balay #define PetscPragmaSIMD _Pragma("GCC ivdep")
200e5518150SSatish Balay #elif defined(_OPENMP) && _OPENMP >= 201307
201e5518150SSatish Balay #if defined(_MSC_VER)
202160a5775SAli Reza Khaz'ali #define PetscPragmaSIMD __pragma(omp simd)
203e5518150SSatish Balay #else
204e5518150SSatish Balay #define PetscPragmaSIMD _Pragma("omp simd")
205e5518150SSatish Balay #endif
206c8b9133aSSatish Balay #elif defined(PETSC_HAVE_CRAY_VECTOR)
207c8b9133aSSatish Balay #define PetscPragmaSIMD _Pragma("_CRI ivdep")
208c8b9133aSSatish Balay #else
209c8b9133aSSatish Balay #define PetscPragmaSIMD
210c8b9133aSSatish Balay #endif
211c8b9133aSSatish Balay 
212d382aafbSBarry Smith /*
213d382aafbSBarry Smith     Declare extern C stuff after including external header files
214d382aafbSBarry Smith */
215d382aafbSBarry Smith 
2166a6fc655SJed Brown PETSC_EXTERN const char *const PetscBools[];
21770b3c8c7SBarry Smith 
2186edef35eSSatish Balay PETSC_EXTERN PetscBool PETSC_RUNNING_ON_VALGRIND;
2198b49ba18SBarry Smith /*
220390e1bf2SBarry Smith     Defines elementary mathematics functions and constants.
2218b49ba18SBarry Smith */
2228b49ba18SBarry Smith #include <petscmath.h>
2238b49ba18SBarry Smith 
2246a6fc655SJed Brown PETSC_EXTERN const char *const PetscCopyModes[];
225d382aafbSBarry Smith 
226d382aafbSBarry Smith /*MC
2270298fd71SBarry Smith     PETSC_IGNORE - same as NULL, means PETSc will ignore this argument
228d382aafbSBarry Smith 
229d382aafbSBarry Smith    Level: beginner
230d382aafbSBarry Smith 
231e28ce29aSPatrick Sanan    Note:
23287497f52SBarry Smith    Accepted by many PETSc functions to not set a parameter and instead use some default
233d382aafbSBarry Smith 
23487497f52SBarry Smith    Fortran Note:
23587497f52SBarry Smith    This macro does not exist in Fortran; you must use `PETSC_NULL_INTEGER`, `PETSC_NULL_DOUBLE_PRECISION` etc
236d382aafbSBarry Smith 
237db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_DEFAULT`, `PETSC_DETERMINE`
238d382aafbSBarry Smith 
239d382aafbSBarry Smith M*/
2400298fd71SBarry Smith #define PETSC_IGNORE NULL
241d382aafbSBarry Smith 
242c528d872SBarry Smith /* This is deprecated */
243c528d872SBarry Smith #define PETSC_NULL NULL
244c528d872SBarry Smith 
245d382aafbSBarry Smith /*MC
246557d4da8SBarry Smith     PETSC_DECIDE - standard way of passing in integer or floating point parameter
247557d4da8SBarry Smith        where you wish PETSc to use the default.
248557d4da8SBarry Smith 
249557d4da8SBarry Smith    Level: beginner
250557d4da8SBarry Smith 
251db781477SPatrick Sanan .seealso: `PETSC_DEFAULT`, `PETSC_IGNORE`, `PETSC_DETERMINE`
252557d4da8SBarry Smith 
253557d4da8SBarry Smith M*/
254557d4da8SBarry Smith #define PETSC_DECIDE -1
255557d4da8SBarry Smith 
256557d4da8SBarry Smith /*MC
257d382aafbSBarry Smith     PETSC_DETERMINE - standard way of passing in integer or floating point parameter
258d382aafbSBarry Smith        where you wish PETSc to compute the required value.
259d382aafbSBarry Smith 
260d382aafbSBarry Smith    Level: beginner
261d382aafbSBarry Smith 
262e28ce29aSPatrick Sanan    Developer Note:
26387497f52SBarry Smith    I would like to use const `PetscInt` `PETSC_DETERMINE` = `PETSC_DECIDE`; but for
26487497f52SBarry Smith      some reason this is not allowed by the standard even though `PETSC_DECIDE` is a constant value.
265557d4da8SBarry Smith 
266db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_DEFAULT`, `PETSC_IGNORE`, `VecSetSizes()`
267d382aafbSBarry Smith 
268d382aafbSBarry Smith M*/
269d382aafbSBarry Smith #define PETSC_DETERMINE PETSC_DECIDE
270d382aafbSBarry Smith 
271d382aafbSBarry Smith /*MC
272557d4da8SBarry Smith     PETSC_DEFAULT - standard way of passing in integer or floating point parameter
273557d4da8SBarry Smith        where you wish PETSc to use the default.
274557d4da8SBarry Smith 
275557d4da8SBarry Smith    Level: beginner
276557d4da8SBarry Smith 
27787497f52SBarry Smith    Fortran Note:
27887497f52SBarry Smith    You need to use `PETSC_DEFAULT_INTEGER` or `PETSC_DEFAULT_REAL`.
279557d4da8SBarry Smith 
280db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_IGNORE`, `PETSC_DETERMINE`
281557d4da8SBarry Smith 
282557d4da8SBarry Smith M*/
283557d4da8SBarry Smith #define PETSC_DEFAULT -2
284557d4da8SBarry Smith 
285557d4da8SBarry Smith /*MC
28687497f52SBarry Smith     PETSC_COMM_WORLD - the equivalent of the `MPI_COMM_WORLD` communicator which represents
287ea8fe74dSBarry Smith            all the processes that PETSc knows about.
288d382aafbSBarry Smith 
289d382aafbSBarry Smith    Level: beginner
290d382aafbSBarry Smith 
291e28ce29aSPatrick Sanan    Notes:
29287497f52SBarry Smith    By default `PETSC_COMM_WORLD` and `MPI_COMM_WORLD` are identical unless you wish to
29387497f52SBarry Smith           run PETSc on ONLY a subset of `MPI_COMM_WORLD`. In that case create your new (smaller)
29487497f52SBarry Smith           communicator, call it, say comm, and set `PETSC_COMM_WORLD` = comm BEFORE calling
29587497f52SBarry Smith           PetscInitialize(), but after `MPI_Init()` has been called.
296a990b902SBarry Smith 
29787497f52SBarry Smith           The value of `PETSC_COMM_WORLD` should never be USED/accessed before `PetscInitialize()`
298a990b902SBarry Smith           is called because it may not have a valid value yet.
299d382aafbSBarry Smith 
300db781477SPatrick Sanan .seealso: `PETSC_COMM_SELF`
301d382aafbSBarry Smith 
302d382aafbSBarry Smith M*/
303014dd563SJed Brown PETSC_EXTERN MPI_Comm PETSC_COMM_WORLD;
304d382aafbSBarry Smith 
305d382aafbSBarry Smith /*MC
30687497f52SBarry Smith     PETSC_COMM_SELF - This is always `MPI_COMM_SELF`
307d382aafbSBarry Smith 
308d382aafbSBarry Smith    Level: beginner
309d382aafbSBarry Smith 
310e28ce29aSPatrick Sanan    Notes:
311e28ce29aSPatrick Sanan    Do not USE/access or set this variable before PetscInitialize() has been called.
312a990b902SBarry Smith 
313db781477SPatrick Sanan .seealso: `PETSC_COMM_WORLD`
314d382aafbSBarry Smith 
315d382aafbSBarry Smith M*/
316d382aafbSBarry Smith #define PETSC_COMM_SELF MPI_COMM_SELF
317d382aafbSBarry Smith 
3186de5d289SStefano Zampini /*MC
3196de5d289SStefano Zampini     PETSC_MPI_THREAD_REQUIRED - the required threading support used if PETSc initializes
32087497f52SBarry Smith            MPI with `MPI_Init_thread()`.
3216de5d289SStefano Zampini 
3226de5d289SStefano Zampini    Level: beginner
3236de5d289SStefano Zampini 
3246de5d289SStefano Zampini    Notes:
32587497f52SBarry Smith    By default `PETSC_MPI_THREAD_REQUIRED` equals `MPI_THREAD_FUNNELED`.
3266de5d289SStefano Zampini 
327db781477SPatrick Sanan .seealso: `PetscInitialize()`
3286de5d289SStefano Zampini 
3296de5d289SStefano Zampini M*/
3306de5d289SStefano Zampini PETSC_EXTERN PetscMPIInt PETSC_MPI_THREAD_REQUIRED;
3316de5d289SStefano Zampini 
332d6f2c3cbSBarry Smith PETSC_EXTERN PetscBool PetscBeganMPI;
3338ad20175SVaclav Hapla PETSC_EXTERN PetscBool PetscErrorHandlingInitialized;
334014dd563SJed Brown PETSC_EXTERN PetscBool PetscInitializeCalled;
335014dd563SJed Brown PETSC_EXTERN PetscBool PetscFinalizeCalled;
3364cf1874eSKarl Rupp PETSC_EXTERN PetscBool PetscViennaCLSynchronize;
337d382aafbSBarry Smith 
338014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*)(MPI_Comm), PetscErrorCode (*)(MPI_Comm));
339014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommDuplicate(MPI_Comm, MPI_Comm *, int *);
340014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommDestroy(MPI_Comm *);
34157f21012SBarry Smith PETSC_EXTERN PetscErrorCode PetscCommGetComm(MPI_Comm, MPI_Comm *);
34257f21012SBarry Smith PETSC_EXTERN PetscErrorCode PetscCommRestoreComm(MPI_Comm, MPI_Comm *);
343d382aafbSBarry Smith 
34459e55d94SJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
34559e55d94SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscKokkosInitializeCheck(void); /* Initialize Kokkos if not yet. */
34659e55d94SJunchao Zhang #endif
34759e55d94SJunchao Zhang 
34871438e86SJunchao Zhang #if defined(PETSC_HAVE_NVSHMEM)
34971438e86SJunchao Zhang PETSC_EXTERN PetscBool      PetscBeganNvshmem;
35071438e86SJunchao Zhang PETSC_EXTERN PetscBool      PetscNvshmemInitialized;
35171438e86SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscNvshmemFinalize(void);
35271438e86SJunchao Zhang #endif
35371438e86SJunchao Zhang 
354540e20f2SPierre Jolivet #if defined(PETSC_HAVE_ELEMENTAL)
355540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalInitializePackage(void);
356540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalInitialized(PetscBool *);
357540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalFinalizePackage(void);
358540e20f2SPierre Jolivet #endif
359540e20f2SPierre Jolivet 
360d382aafbSBarry Smith /*MC
36187497f52SBarry Smith    PetscMalloc - Allocates memory, One should use `PetscNew()`, `PetscMalloc1()` or `PetscCalloc1()` usually instead of this
362d382aafbSBarry Smith 
363eca87e8dSBarry Smith    Synopsis:
364aaa7dc30SBarry Smith     #include <petscsys.h>
365eca87e8dSBarry Smith    PetscErrorCode PetscMalloc(size_t m,void **result)
366eca87e8dSBarry Smith 
367eca87e8dSBarry Smith    Not Collective
368eca87e8dSBarry Smith 
369d382aafbSBarry Smith    Input Parameter:
370d382aafbSBarry Smith .  m - number of bytes to allocate
371d382aafbSBarry Smith 
372d382aafbSBarry Smith    Output Parameter:
373d382aafbSBarry Smith .  result - memory allocated
374d382aafbSBarry Smith 
375d382aafbSBarry Smith    Level: beginner
376d382aafbSBarry Smith 
37749d7da52SJed Brown    Notes:
37849d7da52SJed Brown    Memory is always allocated at least double aligned
379d382aafbSBarry Smith 
38087497f52SBarry Smith    It is safe to allocate size 0 and pass the resulting pointer (which may or may not be NULL) to `PetscFree()`.
381d382aafbSBarry Smith 
382db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`
383d382aafbSBarry Smith 
384d382aafbSBarry Smith M*/
385071fcb05SBarry Smith #define PetscMalloc(a, b) ((*PetscTrMalloc)((a), PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))
386d382aafbSBarry Smith 
387d382aafbSBarry Smith /*MC
3883221ece2SMatthew G. Knepley    PetscRealloc - Rellocates memory
3893221ece2SMatthew G. Knepley 
3903221ece2SMatthew G. Knepley    Synopsis:
3913221ece2SMatthew G. Knepley     #include <petscsys.h>
3923221ece2SMatthew G. Knepley    PetscErrorCode PetscRealloc(size_t m,void **result)
3933221ece2SMatthew G. Knepley 
3943221ece2SMatthew G. Knepley    Not Collective
3953221ece2SMatthew G. Knepley 
3963221ece2SMatthew G. Knepley    Input Parameters:
3973221ece2SMatthew G. Knepley +  m - number of bytes to allocate
39892f119d6SBarry Smith -  result - previous memory
3993221ece2SMatthew G. Knepley 
4003221ece2SMatthew G. Knepley    Output Parameter:
4013221ece2SMatthew G. Knepley .  result - new memory allocated
4023221ece2SMatthew G. Knepley 
4033221ece2SMatthew G. Knepley    Level: developer
4043221ece2SMatthew G. Knepley 
4053221ece2SMatthew G. Knepley    Notes:
4063221ece2SMatthew G. Knepley    Memory is always allocated at least double aligned
4073221ece2SMatthew G. Knepley 
408db781477SPatrick Sanan .seealso: `PetscMalloc()`, `PetscFree()`, `PetscNew()`
4093221ece2SMatthew G. Knepley 
4103221ece2SMatthew G. Knepley M*/
4113221ece2SMatthew G. Knepley #define PetscRealloc(a, b) ((*PetscTrRealloc)((a), __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))
4123221ece2SMatthew G. Knepley 
4133221ece2SMatthew G. Knepley /*MC
41487497f52SBarry Smith    PetscAddrAlign - Rounds up an address to `PETSC_MEMALIGN` alignment
415d382aafbSBarry Smith 
416d382aafbSBarry Smith    Synopsis:
417aaa7dc30SBarry Smith     #include <petscsys.h>
418d382aafbSBarry Smith    void *PetscAddrAlign(void *addr)
419d382aafbSBarry Smith 
420eca87e8dSBarry Smith    Not Collective
421eca87e8dSBarry Smith 
422eca87e8dSBarry Smith    Input Parameters:
423eca87e8dSBarry Smith .  addr - address to align (any pointer type)
424eca87e8dSBarry Smith 
425d382aafbSBarry Smith    Level: developer
426d382aafbSBarry Smith 
427db781477SPatrick Sanan .seealso: `PetscMallocAlign()`
428d382aafbSBarry Smith 
429d382aafbSBarry Smith M*/
430d382aafbSBarry Smith #define PetscAddrAlign(a) (void *)((((PETSC_UINTPTR_T)(a)) + (PETSC_MEMALIGN - 1)) & ~(PETSC_MEMALIGN - 1))
431d382aafbSBarry Smith 
432d382aafbSBarry Smith /*MC
43387497f52SBarry Smith    PetscCalloc - Allocates a cleared (zeroed) memory region aligned to `PETSC_MEMALIGN`
4348f51dc47SJunchao Zhang 
4358f51dc47SJunchao Zhang    Synopsis:
4368f51dc47SJunchao Zhang     #include <petscsys.h>
4378f51dc47SJunchao Zhang    PetscErrorCode PetscCalloc(size_t m,void **result)
4388f51dc47SJunchao Zhang 
4398f51dc47SJunchao Zhang    Not Collective
4408f51dc47SJunchao Zhang 
4418f51dc47SJunchao Zhang    Input Parameter:
4428f51dc47SJunchao Zhang .  m - number of bytes to allocate
4438f51dc47SJunchao Zhang 
4448f51dc47SJunchao Zhang    Output Parameter:
4458f51dc47SJunchao Zhang .  result - memory allocated
4468f51dc47SJunchao Zhang 
4478f51dc47SJunchao Zhang    Level: beginner
4488f51dc47SJunchao Zhang 
4498f51dc47SJunchao Zhang    Notes:
4508f51dc47SJunchao Zhang    Memory is always allocated at least double aligned. This macro is useful in allocating memory pointed by void pointers
4518f51dc47SJunchao Zhang 
4528f51dc47SJunchao Zhang    It is safe to allocate size 0 and pass the resulting pointer (which may or may not be NULL) to PetscFree().
4538f51dc47SJunchao Zhang 
454db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`
4558f51dc47SJunchao Zhang 
4568f51dc47SJunchao Zhang M*/
45766122d6dSBarry Smith #define PetscCalloc(m, result) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (size_t)((size_t)m), (result))
4588f51dc47SJunchao Zhang 
4598f51dc47SJunchao Zhang /*MC
46087497f52SBarry Smith    PetscMalloc1 - Allocates an array of memory aligned to `PETSC_MEMALIGN`
461d382aafbSBarry Smith 
462eca87e8dSBarry Smith    Synopsis:
463aaa7dc30SBarry Smith     #include <petscsys.h>
464785e854fSJed Brown    PetscErrorCode PetscMalloc1(size_t m1,type **r1)
465785e854fSJed Brown 
466785e854fSJed Brown    Not Collective
467785e854fSJed Brown 
468785e854fSJed Brown    Input Parameter:
469390e1bf2SBarry Smith .  m1 - number of elements to allocate  (may be zero)
470785e854fSJed Brown 
471785e854fSJed Brown    Output Parameter:
4721fe1b817SJunchao Zhang .  r1 - memory allocated
473785e854fSJed Brown 
474e28ce29aSPatrick Sanan    Note:
475e28ce29aSPatrick Sanan    This uses the sizeof() of the memory type requested to determine the total memory to be allocated, therefore you should not
47687497f52SBarry Smith          multiply the number of elements requested by the `sizeof()` the type. For example use
477390e1bf2SBarry Smith $  PetscInt *id;
478390e1bf2SBarry Smith $  PetscMalloc1(10,&id);
479390e1bf2SBarry Smith         not
480390e1bf2SBarry Smith $  PetscInt *id;
481390e1bf2SBarry Smith $  PetscMalloc1(10*sizeof(PetscInt),&id);
482390e1bf2SBarry Smith 
48387497f52SBarry Smith         Does not zero the memory allocated, use `PetscCalloc1()` to obtain memory that has been zeroed.
484390e1bf2SBarry Smith 
485390e1bf2SBarry Smith    Level: beginner
486785e854fSJed Brown 
487db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
488785e854fSJed Brown 
489785e854fSJed Brown M*/
49066122d6dSBarry Smith #define PetscMalloc1(m1, r1) PetscMallocA(1, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (size_t)((size_t)m1) * sizeof(**(r1)), (r1))
491785e854fSJed Brown 
492785e854fSJed Brown /*MC
49387497f52SBarry Smith    PetscCalloc1 - Allocates a cleared (zeroed) array of memory aligned to `PETSC_MEMALIGN`
4941795a4d1SJed Brown 
4951795a4d1SJed Brown    Synopsis:
496aaa7dc30SBarry Smith     #include <petscsys.h>
4971795a4d1SJed Brown    PetscErrorCode PetscCalloc1(size_t m1,type **r1)
4981795a4d1SJed Brown 
4991795a4d1SJed Brown    Not Collective
5001795a4d1SJed Brown 
5011795a4d1SJed Brown    Input Parameter:
5021795a4d1SJed Brown .  m1 - number of elements to allocate in 1st chunk  (may be zero)
5031795a4d1SJed Brown 
5041795a4d1SJed Brown    Output Parameter:
5051fe1b817SJunchao Zhang .  r1 - memory allocated
5061795a4d1SJed Brown 
507e28ce29aSPatrick Sanan    Notes:
50887497f52SBarry Smith    See `PetsMalloc1()` for more details on usage.
509390e1bf2SBarry Smith 
510390e1bf2SBarry Smith    Level: beginner
5111795a4d1SJed Brown 
512db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
5131795a4d1SJed Brown 
5141795a4d1SJed Brown M*/
51566122d6dSBarry Smith #define PetscCalloc1(m1, r1) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (size_t)((size_t)m1) * sizeof(**(r1)), (r1))
5161795a4d1SJed Brown 
5171795a4d1SJed Brown /*MC
51887497f52SBarry Smith    PetscMalloc2 - Allocates 2 arrays of memory both aligned to `PETSC_MEMALIGN`
519d382aafbSBarry Smith 
520eca87e8dSBarry Smith    Synopsis:
521aaa7dc30SBarry Smith     #include <petscsys.h>
522dcca6d9dSJed Brown    PetscErrorCode PetscMalloc2(size_t m1,type **r1,size_t m2,type **r2)
523eca87e8dSBarry Smith 
524eca87e8dSBarry Smith    Not Collective
525eca87e8dSBarry Smith 
526d8d19677SJose E. Roman    Input Parameters:
527d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
528dcca6d9dSJed Brown -  m2 - number of elements to allocate in 2nd chunk  (may be zero)
529d382aafbSBarry Smith 
530d8d19677SJose E. Roman    Output Parameters:
531d382aafbSBarry Smith +  r1 - memory allocated in first chunk
532d382aafbSBarry Smith -  r2 - memory allocated in second chunk
533d382aafbSBarry Smith 
534d382aafbSBarry Smith    Level: developer
535d382aafbSBarry Smith 
536db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
537d382aafbSBarry Smith 
538d382aafbSBarry Smith M*/
53966122d6dSBarry Smith #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))
540d382aafbSBarry Smith 
541d382aafbSBarry Smith /*MC
54287497f52SBarry Smith    PetscCalloc2 - Allocates 2 cleared (zeroed) arrays of memory both aligned to `PETSC_MEMALIGN`
543d382aafbSBarry Smith 
544eca87e8dSBarry Smith    Synopsis:
545aaa7dc30SBarry Smith     #include <petscsys.h>
5461795a4d1SJed Brown    PetscErrorCode PetscCalloc2(size_t m1,type **r1,size_t m2,type **r2)
547eca87e8dSBarry Smith 
548eca87e8dSBarry Smith    Not Collective
549eca87e8dSBarry Smith 
550d8d19677SJose E. Roman    Input Parameters:
551d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
5521795a4d1SJed Brown -  m2 - number of elements to allocate in 2nd chunk  (may be zero)
5531795a4d1SJed Brown 
554d8d19677SJose E. Roman    Output Parameters:
5551795a4d1SJed Brown +  r1 - memory allocated in first chunk
5561795a4d1SJed Brown -  r2 - memory allocated in second chunk
5571795a4d1SJed Brown 
5581795a4d1SJed Brown    Level: developer
5591795a4d1SJed Brown 
560db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
5611795a4d1SJed Brown 
5621795a4d1SJed Brown M*/
56366122d6dSBarry Smith #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))
5641795a4d1SJed Brown 
5651795a4d1SJed Brown /*MC
56687497f52SBarry Smith    PetscMalloc3 - Allocates 3 arrays of memory, all aligned to `PETSC_MEMALIGN`
567d382aafbSBarry Smith 
568d382aafbSBarry Smith    Synopsis:
569aaa7dc30SBarry Smith     #include <petscsys.h>
570dcca6d9dSJed Brown    PetscErrorCode PetscMalloc3(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3)
571d382aafbSBarry Smith 
572d382aafbSBarry Smith    Not Collective
573d382aafbSBarry Smith 
574d8d19677SJose E. Roman    Input Parameters:
575d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
576d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
577dcca6d9dSJed Brown -  m3 - number of elements to allocate in 3rd chunk  (may be zero)
578d382aafbSBarry Smith 
579d8d19677SJose E. Roman    Output Parameters:
580d382aafbSBarry Smith +  r1 - memory allocated in first chunk
581d382aafbSBarry Smith .  r2 - memory allocated in second chunk
582d382aafbSBarry Smith -  r3 - memory allocated in third chunk
583d382aafbSBarry Smith 
584d382aafbSBarry Smith    Level: developer
585d382aafbSBarry Smith 
586db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc3()`, `PetscFree3()`
587d382aafbSBarry Smith 
588d382aafbSBarry Smith M*/
58966122d6dSBarry Smith #define PetscMalloc3(m1, r1, m2, r2, m3, r3) 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))
590d382aafbSBarry Smith 
591d382aafbSBarry Smith /*MC
59287497f52SBarry Smith    PetscCalloc3 - Allocates 3 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
593d382aafbSBarry Smith 
594eca87e8dSBarry Smith    Synopsis:
595aaa7dc30SBarry Smith     #include <petscsys.h>
5961795a4d1SJed Brown    PetscErrorCode PetscCalloc3(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3)
597eca87e8dSBarry Smith 
598eca87e8dSBarry Smith    Not Collective
599eca87e8dSBarry Smith 
600d8d19677SJose E. Roman    Input Parameters:
601d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
602d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
6031795a4d1SJed Brown -  m3 - number of elements to allocate in 3rd chunk  (may be zero)
6041795a4d1SJed Brown 
605d8d19677SJose E. Roman    Output Parameters:
6061795a4d1SJed Brown +  r1 - memory allocated in first chunk
6071795a4d1SJed Brown .  r2 - memory allocated in second chunk
6081795a4d1SJed Brown -  r3 - memory allocated in third chunk
6091795a4d1SJed Brown 
6101795a4d1SJed Brown    Level: developer
6111795a4d1SJed Brown 
612db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc2()`, `PetscMalloc3()`, `PetscFree3()`
6131795a4d1SJed Brown 
6141795a4d1SJed Brown M*/
61566122d6dSBarry Smith #define PetscCalloc3(m1, r1, m2, r2, m3, r3) 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))
6161795a4d1SJed Brown 
6171795a4d1SJed Brown /*MC
61887497f52SBarry Smith    PetscMalloc4 - Allocates 4 arrays of memory, all aligned to `PETSC_MEMALIGN`
619d382aafbSBarry Smith 
620d382aafbSBarry Smith    Synopsis:
621aaa7dc30SBarry Smith     #include <petscsys.h>
622dcca6d9dSJed Brown    PetscErrorCode PetscMalloc4(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4)
623d382aafbSBarry Smith 
624d382aafbSBarry Smith    Not Collective
625d382aafbSBarry Smith 
626d8d19677SJose E. Roman    Input Parameters:
627d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
628d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
629d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
630dcca6d9dSJed Brown -  m4 - number of elements to allocate in 4th chunk  (may be zero)
631d382aafbSBarry Smith 
632d8d19677SJose E. Roman    Output Parameters:
633d382aafbSBarry Smith +  r1 - memory allocated in first chunk
634d382aafbSBarry Smith .  r2 - memory allocated in second chunk
635d382aafbSBarry Smith .  r3 - memory allocated in third chunk
636d382aafbSBarry Smith -  r4 - memory allocated in fourth chunk
637d382aafbSBarry Smith 
638d382aafbSBarry Smith    Level: developer
639d382aafbSBarry Smith 
640db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
641d382aafbSBarry Smith 
642d382aafbSBarry Smith M*/
6439371c9d4SSatish Balay #define PetscMalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
6449371c9d4SSatish Balay   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))
645d382aafbSBarry Smith 
646d382aafbSBarry Smith /*MC
64787497f52SBarry Smith    PetscCalloc4 - Allocates 4 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
648d382aafbSBarry Smith 
649eca87e8dSBarry Smith    Synopsis:
650aaa7dc30SBarry Smith     #include <petscsys.h>
6511795a4d1SJed Brown    PetscErrorCode PetscCalloc4(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4)
652eca87e8dSBarry Smith 
653eca87e8dSBarry Smith    Not Collective
654eca87e8dSBarry Smith 
655e28ce29aSPatrick Sanan    Input Parameters:
656d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
657d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
658d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
6591795a4d1SJed Brown -  m4 - number of elements to allocate in 4th chunk  (may be zero)
6601795a4d1SJed Brown 
661e28ce29aSPatrick Sanan    Output Parameters:
6621795a4d1SJed Brown +  r1 - memory allocated in first chunk
6631795a4d1SJed Brown .  r2 - memory allocated in second chunk
6641795a4d1SJed Brown .  r3 - memory allocated in third chunk
6651795a4d1SJed Brown -  r4 - memory allocated in fourth chunk
6661795a4d1SJed Brown 
6671795a4d1SJed Brown    Level: developer
6681795a4d1SJed Brown 
669db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
6701795a4d1SJed Brown 
6711795a4d1SJed Brown M*/
6729371c9d4SSatish Balay #define PetscCalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
6739371c9d4SSatish Balay   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))
6741795a4d1SJed Brown 
6751795a4d1SJed Brown /*MC
67687497f52SBarry Smith    PetscMalloc5 - Allocates 5 arrays of memory, all aligned to `PETSC_MEMALIGN`
677d382aafbSBarry Smith 
678d382aafbSBarry Smith    Synopsis:
679aaa7dc30SBarry Smith     #include <petscsys.h>
680dcca6d9dSJed 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)
681d382aafbSBarry Smith 
682d382aafbSBarry Smith    Not Collective
683d382aafbSBarry Smith 
684e28ce29aSPatrick Sanan    Input Parameters:
685d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
686d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
687d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
688d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
689dcca6d9dSJed Brown -  m5 - number of elements to allocate in 5th chunk  (may be zero)
690d382aafbSBarry Smith 
691e28ce29aSPatrick Sanan    Output Parameters:
692d382aafbSBarry Smith +  r1 - memory allocated in first chunk
693d382aafbSBarry Smith .  r2 - memory allocated in second chunk
694d382aafbSBarry Smith .  r3 - memory allocated in third chunk
695d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
696d382aafbSBarry Smith -  r5 - memory allocated in fifth chunk
697d382aafbSBarry Smith 
698d382aafbSBarry Smith    Level: developer
699d382aafbSBarry Smith 
700db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc5()`, `PetscFree5()`
701d382aafbSBarry Smith 
702d382aafbSBarry Smith M*/
7039371c9d4SSatish Balay #define PetscMalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
7049371c9d4SSatish Balay   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))
705d382aafbSBarry Smith 
706d382aafbSBarry Smith /*MC
70787497f52SBarry Smith    PetscCalloc5 - Allocates 5 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
708d382aafbSBarry Smith 
709eca87e8dSBarry Smith    Synopsis:
710aaa7dc30SBarry Smith     #include <petscsys.h>
7111795a4d1SJed 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)
712eca87e8dSBarry Smith 
713eca87e8dSBarry Smith    Not Collective
714eca87e8dSBarry Smith 
715e28ce29aSPatrick Sanan    Input Parameters:
716d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
717d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
718d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
719d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
7201795a4d1SJed Brown -  m5 - number of elements to allocate in 5th chunk  (may be zero)
7211795a4d1SJed Brown 
722e28ce29aSPatrick Sanan    Output Parameters:
7231795a4d1SJed Brown +  r1 - memory allocated in first chunk
7241795a4d1SJed Brown .  r2 - memory allocated in second chunk
7251795a4d1SJed Brown .  r3 - memory allocated in third chunk
7261795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
7271795a4d1SJed Brown -  r5 - memory allocated in fifth chunk
7281795a4d1SJed Brown 
7291795a4d1SJed Brown    Level: developer
7301795a4d1SJed Brown 
731db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc5()`, `PetscFree5()`
7321795a4d1SJed Brown 
7331795a4d1SJed Brown M*/
7349371c9d4SSatish Balay #define PetscCalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
7359371c9d4SSatish Balay   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))
736d382aafbSBarry Smith 
737d382aafbSBarry Smith /*MC
73887497f52SBarry Smith    PetscMalloc6 - Allocates 6 arrays of memory, all aligned to `PETSC_MEMALIGN`
739d382aafbSBarry Smith 
740d382aafbSBarry Smith    Synopsis:
741aaa7dc30SBarry Smith     #include <petscsys.h>
742dcca6d9dSJed 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)
743d382aafbSBarry Smith 
744d382aafbSBarry Smith    Not Collective
745d382aafbSBarry Smith 
746e28ce29aSPatrick Sanan    Input Parameters:
747d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
748d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
749d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
750d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
751d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
752dcca6d9dSJed Brown -  m6 - number of elements to allocate in 6th chunk  (may be zero)
753d382aafbSBarry Smith 
754e28ce29aSPatrick Sanan    Output Parameteasr:
755d382aafbSBarry Smith +  r1 - memory allocated in first chunk
756d382aafbSBarry Smith .  r2 - memory allocated in second chunk
757d382aafbSBarry Smith .  r3 - memory allocated in third chunk
758d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
759d382aafbSBarry Smith .  r5 - memory allocated in fifth chunk
760d382aafbSBarry Smith -  r6 - memory allocated in sixth chunk
761d382aafbSBarry Smith 
762d382aafbSBarry Smith    Level: developer
763d382aafbSBarry Smith 
764db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc6()`, `PetscFree3()`, `PetscFree4()`, `PetscFree5()`, `PetscFree6()`
765d382aafbSBarry Smith 
766d382aafbSBarry Smith M*/
7679371c9d4SSatish Balay #define PetscMalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
7689371c9d4SSatish Balay   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))
769d382aafbSBarry Smith 
770d382aafbSBarry Smith /*MC
77187497f52SBarry Smith    PetscCalloc6 - Allocates 6 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
772d382aafbSBarry Smith 
773eca87e8dSBarry Smith    Synopsis:
774aaa7dc30SBarry Smith     #include <petscsys.h>
7751795a4d1SJed 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)
776eca87e8dSBarry Smith 
777eca87e8dSBarry Smith    Not Collective
778eca87e8dSBarry Smith 
779e28ce29aSPatrick Sanan    Input Parameters:
780d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
781d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
782d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
783d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
784d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
7851795a4d1SJed Brown -  m6 - number of elements to allocate in 6th chunk  (may be zero)
7861795a4d1SJed Brown 
787e28ce29aSPatrick Sanan    Output Parameters:
7881795a4d1SJed Brown +  r1 - memory allocated in first chunk
7891795a4d1SJed Brown .  r2 - memory allocated in second chunk
7901795a4d1SJed Brown .  r3 - memory allocated in third chunk
7911795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
7921795a4d1SJed Brown .  r5 - memory allocated in fifth chunk
7931795a4d1SJed Brown -  r6 - memory allocated in sixth chunk
7941795a4d1SJed Brown 
7951795a4d1SJed Brown    Level: developer
7961795a4d1SJed Brown 
797db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc6()`, `PetscFree6()`
7981795a4d1SJed Brown 
7991795a4d1SJed Brown M*/
8009371c9d4SSatish Balay #define PetscCalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
8019371c9d4SSatish Balay   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))
8021795a4d1SJed Brown 
8031795a4d1SJed Brown /*MC
80487497f52SBarry Smith    PetscMalloc7 - Allocates 7 arrays of memory, all aligned to `PETSC_MEMALIGN`
805d382aafbSBarry Smith 
806d382aafbSBarry Smith    Synopsis:
807aaa7dc30SBarry Smith     #include <petscsys.h>
808dcca6d9dSJed 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)
809d382aafbSBarry Smith 
810d382aafbSBarry Smith    Not Collective
811d382aafbSBarry Smith 
812e28ce29aSPatrick Sanan    Input Parameters:
813d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
814d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
815d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
816d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
817d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
818d382aafbSBarry Smith .  m6 - number of elements to allocate in 6th chunk  (may be zero)
819dcca6d9dSJed Brown -  m7 - number of elements to allocate in 7th chunk  (may be zero)
820d382aafbSBarry Smith 
821e28ce29aSPatrick Sanan    Output Parameters:
822d382aafbSBarry Smith +  r1 - memory allocated in first chunk
823d382aafbSBarry Smith .  r2 - memory allocated in second chunk
824d382aafbSBarry Smith .  r3 - memory allocated in third chunk
825d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
826d382aafbSBarry Smith .  r5 - memory allocated in fifth chunk
827d382aafbSBarry Smith .  r6 - memory allocated in sixth chunk
828eca87e8dSBarry Smith -  r7 - memory allocated in seventh chunk
829d382aafbSBarry Smith 
830d382aafbSBarry Smith    Level: developer
831d382aafbSBarry Smith 
832db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc7()`, `PetscFree7()`
833d382aafbSBarry Smith 
834d382aafbSBarry Smith M*/
8359371c9d4SSatish Balay #define PetscMalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
8369371c9d4SSatish Balay   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))
837d382aafbSBarry Smith 
838d382aafbSBarry Smith /*MC
83987497f52SBarry Smith    PetscCalloc7 - Allocates 7 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
8401795a4d1SJed Brown 
8411795a4d1SJed Brown    Synopsis:
842aaa7dc30SBarry Smith     #include <petscsys.h>
8431795a4d1SJed 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)
8441795a4d1SJed Brown 
8451795a4d1SJed Brown    Not Collective
8461795a4d1SJed Brown 
847e28ce29aSPatrick Sanan    Input Parameters:
8481795a4d1SJed Brown +  m1 - number of elements to allocate in 1st chunk  (may be zero)
8491795a4d1SJed Brown .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
8501795a4d1SJed Brown .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
8511795a4d1SJed Brown .  m4 - number of elements to allocate in 4th chunk  (may be zero)
8521795a4d1SJed Brown .  m5 - number of elements to allocate in 5th chunk  (may be zero)
8531795a4d1SJed Brown .  m6 - number of elements to allocate in 6th chunk  (may be zero)
8541795a4d1SJed Brown -  m7 - number of elements to allocate in 7th chunk  (may be zero)
8551795a4d1SJed Brown 
856e28ce29aSPatrick Sanan    Output Parameters:
8571795a4d1SJed Brown +  r1 - memory allocated in first chunk
8581795a4d1SJed Brown .  r2 - memory allocated in second chunk
8591795a4d1SJed Brown .  r3 - memory allocated in third chunk
8601795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
8611795a4d1SJed Brown .  r5 - memory allocated in fifth chunk
8621795a4d1SJed Brown .  r6 - memory allocated in sixth chunk
8631795a4d1SJed Brown -  r7 - memory allocated in seventh chunk
8641795a4d1SJed Brown 
8651795a4d1SJed Brown    Level: developer
8661795a4d1SJed Brown 
867db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc7()`, `PetscFree7()`
8681795a4d1SJed Brown 
8691795a4d1SJed Brown M*/
8709371c9d4SSatish Balay #define PetscCalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
8719371c9d4SSatish Balay   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))
8721795a4d1SJed Brown 
8731795a4d1SJed Brown /*MC
87487497f52SBarry Smith    PetscNew - Allocates memory of a particular type, zeros the memory! Aligned to `PETSC_MEMALIGN`
875d382aafbSBarry Smith 
876eca87e8dSBarry Smith    Synopsis:
877aaa7dc30SBarry Smith     #include <petscsys.h>
878b00a9115SJed Brown    PetscErrorCode PetscNew(type **result)
879eca87e8dSBarry Smith 
880eca87e8dSBarry Smith    Not Collective
881eca87e8dSBarry Smith 
882d382aafbSBarry Smith    Output Parameter:
883b00a9115SJed Brown .  result - memory allocated, sized to match pointer type
884d382aafbSBarry Smith 
885d382aafbSBarry Smith    Level: beginner
886d382aafbSBarry Smith 
887db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscMalloc()`, `PetscNewLog()`, `PetscCalloc1()`, `PetscMalloc1()`
888d382aafbSBarry Smith 
889d382aafbSBarry Smith M*/
890b00a9115SJed Brown #define PetscNew(b) PetscCalloc1(1, (b))
891ad0619ddSBarry Smith 
892ad0619ddSBarry Smith /*MC
89387497f52SBarry Smith    PetscNewLog - Allocates memory of a type matching pointer, zeros the memory! Aligned to `PETSC_MEMALIGN`. Associates the memory allocated
89487497f52SBarry Smith          with the given object using `PetscLogObjectMemory()`.
895ad0619ddSBarry Smith 
896ad0619ddSBarry Smith    Synopsis:
897aaa7dc30SBarry Smith     #include <petscsys.h>
898b00a9115SJed Brown    PetscErrorCode PetscNewLog(PetscObject obj,type **result)
899ad0619ddSBarry Smith 
900ad0619ddSBarry Smith    Not Collective
901ad0619ddSBarry Smith 
902ad0619ddSBarry Smith    Input Parameter:
903b00a9115SJed Brown .  obj - object memory is logged to
904ad0619ddSBarry Smith 
905ad0619ddSBarry Smith    Output Parameter:
906b00a9115SJed Brown .  result - memory allocated, sized to match pointer type
907ad0619ddSBarry Smith 
908ad0619ddSBarry Smith    Level: developer
909ad0619ddSBarry Smith 
910db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscMalloc()`, `PetscNew()`, `PetscLogObjectMemory()`, `PetscCalloc1()`, `PetscMalloc1()`
911ad0619ddSBarry Smith 
912ad0619ddSBarry Smith M*/
913d12f57a0SLisandro Dalcin #define PetscNewLog(o, b) (PetscNew((b)) || PetscLogObjectMemory((PetscObject)o, sizeof(**(b))))
914d382aafbSBarry Smith 
915d382aafbSBarry Smith /*MC
916d382aafbSBarry Smith    PetscFree - Frees memory
917d382aafbSBarry Smith 
918eca87e8dSBarry Smith    Synopsis:
919aaa7dc30SBarry Smith     #include <petscsys.h>
920eca87e8dSBarry Smith    PetscErrorCode PetscFree(void *memory)
921eca87e8dSBarry Smith 
922eca87e8dSBarry Smith    Not Collective
923eca87e8dSBarry Smith 
924d382aafbSBarry Smith    Input Parameter:
9257f4976b7SJacob Faibussowitsch .   memory - memory to free (the pointer is ALWAYS set to NULL upon success)
926d382aafbSBarry Smith 
927d382aafbSBarry Smith    Level: beginner
928d382aafbSBarry Smith 
92949d7da52SJed Brown    Notes:
93087497f52SBarry Smith    Do not free memory obtained with `PetscMalloc2()`, `PetscCalloc2()` etc, they must be freed with `PetscFree2()` etc.
931390e1bf2SBarry Smith 
93287497f52SBarry Smith    It is safe to call `PetscFree()` on a NULL pointer.
933d382aafbSBarry Smith 
934db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscNewLog()`, `PetscMalloc1()`, `PetscCalloc1()`
935d382aafbSBarry Smith 
936d382aafbSBarry Smith M*/
937ffc31a0eSLisandro Dalcin #define PetscFree(a) ((*PetscTrFree)((void *)(a), __LINE__, PETSC_FUNCTION_NAME, __FILE__) || ((a) = NULL, 0))
938d382aafbSBarry Smith 
939d382aafbSBarry Smith /*MC
94087497f52SBarry Smith    PetscFree2 - Frees 2 chunks of memory obtained with `PetscMalloc2()`
941d382aafbSBarry Smith 
942eca87e8dSBarry Smith    Synopsis:
943aaa7dc30SBarry Smith     #include <petscsys.h>
944eca87e8dSBarry Smith    PetscErrorCode PetscFree2(void *memory1,void *memory2)
945eca87e8dSBarry Smith 
946eca87e8dSBarry Smith    Not Collective
947eca87e8dSBarry Smith 
948e28ce29aSPatrick Sanan    Input Parameters:
949d382aafbSBarry Smith +   memory1 - memory to free
950d382aafbSBarry Smith -   memory2 - 2nd memory to free
951d382aafbSBarry Smith 
952d382aafbSBarry Smith    Level: developer
953d382aafbSBarry Smith 
95495452b02SPatrick Sanan    Notes:
95587497f52SBarry Smith     Memory must have been obtained with `PetscMalloc2()`
956d382aafbSBarry Smith 
957db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`
958d382aafbSBarry Smith 
959d382aafbSBarry Smith M*/
960ba282f50SJed Brown #define PetscFree2(m1, m2) PetscFreeA(2, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2))
961d382aafbSBarry Smith 
962d382aafbSBarry Smith /*MC
96387497f52SBarry Smith    PetscFree3 - Frees 3 chunks of memory obtained with `PetscMalloc3()`
964d382aafbSBarry Smith 
965eca87e8dSBarry Smith    Synopsis:
966aaa7dc30SBarry Smith     #include <petscsys.h>
967eca87e8dSBarry Smith    PetscErrorCode PetscFree3(void *memory1,void *memory2,void *memory3)
968eca87e8dSBarry Smith 
969eca87e8dSBarry Smith    Not Collective
970eca87e8dSBarry Smith 
971e28ce29aSPatrick Sanan    Input Parameters:
972d382aafbSBarry Smith +   memory1 - memory to free
973d382aafbSBarry Smith .   memory2 - 2nd memory to free
974d382aafbSBarry Smith -   memory3 - 3rd memory to free
975d382aafbSBarry Smith 
976d382aafbSBarry Smith    Level: developer
977d382aafbSBarry Smith 
97895452b02SPatrick Sanan    Notes:
97987497f52SBarry Smith     Memory must have been obtained with `PetscMalloc3()`
980d382aafbSBarry Smith 
981db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`
982d382aafbSBarry Smith 
983d382aafbSBarry Smith M*/
984ba282f50SJed Brown #define PetscFree3(m1, m2, m3) PetscFreeA(3, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3))
985d382aafbSBarry Smith 
986d382aafbSBarry Smith /*MC
98787497f52SBarry Smith    PetscFree4 - Frees 4 chunks of memory obtained with `PetscMalloc4()`
988d382aafbSBarry Smith 
989eca87e8dSBarry Smith    Synopsis:
990aaa7dc30SBarry Smith     #include <petscsys.h>
991eca87e8dSBarry Smith    PetscErrorCode PetscFree4(void *m1,void *m2,void *m3,void *m4)
992eca87e8dSBarry Smith 
993eca87e8dSBarry Smith    Not Collective
994eca87e8dSBarry Smith 
995e28ce29aSPatrick Sanan    Input Parameters:
996d382aafbSBarry Smith +   m1 - memory to free
997d382aafbSBarry Smith .   m2 - 2nd memory to free
998d382aafbSBarry Smith .   m3 - 3rd memory to free
999d382aafbSBarry Smith -   m4 - 4th memory to free
1000d382aafbSBarry Smith 
1001d382aafbSBarry Smith    Level: developer
1002d382aafbSBarry Smith 
100395452b02SPatrick Sanan    Notes:
100487497f52SBarry Smith     Memory must have been obtained with `PetscMalloc4()`
1005d382aafbSBarry Smith 
1006db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`
1007d382aafbSBarry Smith 
1008d382aafbSBarry Smith M*/
1009ba282f50SJed Brown #define PetscFree4(m1, m2, m3, m4) PetscFreeA(4, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4))
1010d382aafbSBarry Smith 
1011d382aafbSBarry Smith /*MC
101287497f52SBarry Smith    PetscFree5 - Frees 5 chunks of memory obtained with `PetscMalloc5()`
1013d382aafbSBarry Smith 
1014eca87e8dSBarry Smith    Synopsis:
1015aaa7dc30SBarry Smith     #include <petscsys.h>
1016eca87e8dSBarry Smith    PetscErrorCode PetscFree5(void *m1,void *m2,void *m3,void *m4,void *m5)
1017eca87e8dSBarry Smith 
1018eca87e8dSBarry Smith    Not Collective
1019eca87e8dSBarry Smith 
1020e28ce29aSPatrick Sanan    Input Parameters:
1021d382aafbSBarry Smith +   m1 - memory to free
1022d382aafbSBarry Smith .   m2 - 2nd memory to free
1023d382aafbSBarry Smith .   m3 - 3rd memory to free
1024d382aafbSBarry Smith .   m4 - 4th memory to free
1025d382aafbSBarry Smith -   m5 - 5th memory to free
1026d382aafbSBarry Smith 
1027d382aafbSBarry Smith    Level: developer
1028d382aafbSBarry Smith 
102995452b02SPatrick Sanan    Notes:
103087497f52SBarry Smith     Memory must have been obtained with `PetscMalloc5()`
1031d382aafbSBarry Smith 
1032db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`
1033d382aafbSBarry Smith 
1034d382aafbSBarry Smith M*/
1035ba282f50SJed Brown #define PetscFree5(m1, m2, m3, m4, m5) PetscFreeA(5, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5))
1036d382aafbSBarry Smith 
1037d382aafbSBarry Smith /*MC
103887497f52SBarry Smith    PetscFree6 - Frees 6 chunks of memory obtained with `PetscMalloc6()`
1039d382aafbSBarry Smith 
1040eca87e8dSBarry Smith    Synopsis:
1041aaa7dc30SBarry Smith     #include <petscsys.h>
1042eca87e8dSBarry Smith    PetscErrorCode PetscFree6(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6)
1043eca87e8dSBarry Smith 
1044eca87e8dSBarry Smith    Not Collective
1045eca87e8dSBarry Smith 
1046e28ce29aSPatrick Sanan    Input Parameters:
1047d382aafbSBarry Smith +   m1 - memory to free
1048d382aafbSBarry Smith .   m2 - 2nd memory to free
1049d382aafbSBarry Smith .   m3 - 3rd memory to free
1050d382aafbSBarry Smith .   m4 - 4th memory to free
1051d382aafbSBarry Smith .   m5 - 5th memory to free
1052d382aafbSBarry Smith -   m6 - 6th memory to free
1053d382aafbSBarry Smith 
1054d382aafbSBarry Smith    Level: developer
1055d382aafbSBarry Smith 
105695452b02SPatrick Sanan    Notes:
105787497f52SBarry Smith     Memory must have been obtained with `PetscMalloc6()`
1058d382aafbSBarry Smith 
1059db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`
1060d382aafbSBarry Smith 
1061d382aafbSBarry Smith M*/
1062ba282f50SJed Brown #define PetscFree6(m1, m2, m3, m4, m5, m6) PetscFreeA(6, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5), &(m6))
1063d382aafbSBarry Smith 
1064d382aafbSBarry Smith /*MC
106587497f52SBarry Smith    PetscFree7 - Frees 7 chunks of memory obtained with `PetscMalloc7()`
1066d382aafbSBarry Smith 
1067eca87e8dSBarry Smith    Synopsis:
1068aaa7dc30SBarry Smith     #include <petscsys.h>
1069eca87e8dSBarry Smith    PetscErrorCode PetscFree7(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6,void *m7)
1070eca87e8dSBarry Smith 
1071eca87e8dSBarry Smith    Not Collective
1072eca87e8dSBarry Smith 
1073e28ce29aSPatrick Sanan    Input Parameters:
1074d382aafbSBarry Smith +   m1 - memory to free
1075d382aafbSBarry Smith .   m2 - 2nd memory to free
1076d382aafbSBarry Smith .   m3 - 3rd memory to free
1077d382aafbSBarry Smith .   m4 - 4th memory to free
1078d382aafbSBarry Smith .   m5 - 5th memory to free
1079d382aafbSBarry Smith .   m6 - 6th memory to free
1080d382aafbSBarry Smith -   m7 - 7th memory to free
1081d382aafbSBarry Smith 
1082d382aafbSBarry Smith    Level: developer
1083d382aafbSBarry Smith 
108495452b02SPatrick Sanan    Notes:
108587497f52SBarry Smith     Memory must have been obtained with `PetscMalloc7()`
1086d382aafbSBarry Smith 
1087db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`,
1088db781477SPatrick Sanan           `PetscMalloc7()`
1089d382aafbSBarry Smith 
1090d382aafbSBarry Smith M*/
1091ba282f50SJed Brown #define PetscFree7(m1, m2, m3, m4, m5, m6, m7) PetscFreeA(7, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5), &(m6), &(m7))
1092d382aafbSBarry Smith 
1093ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocA(int, PetscBool, int, const char *, const char *, size_t, void *, ...);
1094ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscFreeA(int, int, const char *, const char *, void *, ...);
1095071fcb05SBarry Smith PETSC_EXTERN                PetscErrorCode (*PetscTrMalloc)(size_t, PetscBool, int, const char[], const char[], void **);
1096efca3c55SSatish Balay PETSC_EXTERN                PetscErrorCode (*PetscTrFree)(void *, int, const char[], const char[]);
10973221ece2SMatthew G. Knepley PETSC_EXTERN                PetscErrorCode (*PetscTrRealloc)(size_t, int, const char[], const char[], void **);
1098ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocSetCoalesce(PetscBool);
109992f119d6SBarry 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 **));
1100014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocClear(void);
1101d382aafbSBarry Smith 
1102d382aafbSBarry Smith /*
1103c1706be6SHong 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.
1104c1706be6SHong Zhang */
1105c1706be6SHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocSetDRAM(void);
1106c1706be6SHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocResetDRAM(void);
110731f06eaaSHong Zhang #if defined(PETSC_HAVE_CUDA)
110831f06eaaSHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocSetCUDAHost(void);
110931f06eaaSHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocResetCUDAHost(void);
111031f06eaaSHong Zhang #endif
111159af0bd3SScott Kruger #if defined(PETSC_HAVE_HIP)
111259af0bd3SScott Kruger PETSC_EXTERN PetscErrorCode PetscMallocSetHIPHost(void);
111359af0bd3SScott Kruger PETSC_EXTERN PetscErrorCode PetscMallocResetHIPHost(void);
111459af0bd3SScott Kruger #endif
111559af0bd3SScott Kruger 
1116a5057860SBarry Smith #define MPIU_PETSCLOGDOUBLE  MPI_DOUBLE
111736763ca0SBas van 't Hof #define MPIU_2PETSCLOGDOUBLE MPI_2DOUBLE_PRECISION
1118a5057860SBarry Smith 
1119a5057860SBarry Smith /*
112066d669d6SBarry Smith    Routines for tracing memory corruption/bleeding with default PETSc memory allocation
1121d382aafbSBarry Smith */
1122014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocDump(FILE *);
112392f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocView(FILE *);
1124014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocGetCurrentUsage(PetscLogDouble *);
1125014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocGetMaximumUsage(PetscLogDouble *);
1126e3ed9ee7SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocPushMaximumUsage(int);
1127e3ed9ee7SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocPopMaximumUsage(int, PetscLogDouble *);
112892f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocSetDebug(PetscBool, PetscBool);
112992f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocGetDebug(PetscBool *, PetscBool *, PetscBool *);
1130efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscMallocValidate(int, const char[], const char[]);
113192f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocViewSet(PetscLogDouble);
113292f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocViewGet(PetscBool *);
1133608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeSet(PetscBool);
1134608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeGet(PetscBool *);
1135d382aafbSBarry Smith 
11366a6fc655SJed Brown PETSC_EXTERN const char *const PetscDataTypes[];
1137014dd563SJed Brown PETSC_EXTERN PetscErrorCode    PetscDataTypeToMPIDataType(PetscDataType, MPI_Datatype *);
1138014dd563SJed Brown PETSC_EXTERN PetscErrorCode    PetscMPIDataTypeToPetscDataType(MPI_Datatype, PetscDataType *);
1139014dd563SJed Brown PETSC_EXTERN PetscErrorCode    PetscDataTypeGetSize(PetscDataType, size_t *);
11408e4b2d1dSBarry Smith PETSC_EXTERN PetscErrorCode    PetscDataTypeFromString(const char *, PetscDataType *, PetscBool *);
1141d382aafbSBarry Smith 
1142d382aafbSBarry Smith /*
1143d382aafbSBarry Smith     Basic memory and string operations. These are usually simple wrappers
1144d382aafbSBarry Smith    around the basic Unix system calls, but a few of them have additional
1145d382aafbSBarry Smith    functionality and/or error checking.
1146d382aafbSBarry Smith */
1147014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemcmp(const void *, const void *, size_t, PetscBool *);
1148014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrlen(const char[], size_t *);
1149d67fe73bSBarry Smith PETSC_EXTERN PetscErrorCode PetscStrToArray(const char[], char, int *, char ***);
1150014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrToArrayDestroy(int, char **);
1151014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrcmp(const char[], const char[], PetscBool *);
1152014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrgrt(const char[], const char[], PetscBool *);
1153014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrcasecmp(const char[], const char[], PetscBool *);
1154014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrncmp(const char[], const char[], size_t, PetscBool *);
1155014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrcpy(char[], const char[]);
1156014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrcat(char[], const char[]);
1157a126751eSBarry Smith PETSC_EXTERN PetscErrorCode PetscStrlcat(char[], const char[], size_t);
1158014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrncpy(char[], const char[], size_t);
1159014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrchr(const char[], char, char *[]);
1160014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrtolower(char[]);
11612f234a98SBarry Smith PETSC_EXTERN PetscErrorCode PetscStrtoupper(char[]);
1162014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrrchr(const char[], char, char *[]);
1163014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrstr(const char[], const char[], char *[]);
1164014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrrstr(const char[], const char[], char *[]);
1165014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrendswith(const char[], const char[], PetscBool *);
11662c9581d2SBarry Smith PETSC_EXTERN PetscErrorCode PetscStrbeginswith(const char[], const char[], PetscBool *);
1167014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrendswithwhich(const char[], const char *const *, PetscInt *);
1168014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrallocpy(const char[], char *[]);
116947340559SBarry Smith PETSC_EXTERN PetscErrorCode PetscStrArrayallocpy(const char *const *, char ***);
11706fed8037SJed Brown PETSC_EXTERN PetscErrorCode PetscStrArrayDestroy(char ***);
11716991f827SBarry Smith PETSC_EXTERN PetscErrorCode PetscStrNArrayallocpy(PetscInt, const char *const *, char ***);
11726991f827SBarry Smith PETSC_EXTERN PetscErrorCode PetscStrNArrayDestroy(PetscInt, char ***);
1173014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrreplace(MPI_Comm, const char[], char[], size_t);
1174d382aafbSBarry Smith 
1175573b0fb4SBarry Smith PETSC_EXTERN void PetscStrcmpNoError(const char[], const char[], PetscBool *);
1176573b0fb4SBarry Smith 
1177014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTokenCreate(const char[], const char, PetscToken *);
1178014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTokenFind(PetscToken, char *[]);
1179014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTokenDestroy(PetscToken *);
1180d382aafbSBarry Smith 
1181e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscStrInList(const char[], const char[], char, PetscBool *);
118280b92c66SBarry Smith PETSC_EXTERN const char    *PetscBasename(const char[]);
1183a53986e1SJed Brown PETSC_EXTERN PetscErrorCode PetscEListFind(PetscInt, const char *const *, const char *, PetscInt *, PetscBool *);
1184a53986e1SJed Brown PETSC_EXTERN PetscErrorCode PetscEnumFind(const char *const *, const char *, PetscEnum *, PetscBool *);
1185a53986e1SJed Brown 
1186d382aafbSBarry Smith /*
1187d382aafbSBarry Smith    These are MPI operations for MPI_Allreduce() etc
1188d382aafbSBarry Smith */
1189367daffbSBarry Smith PETSC_EXTERN MPI_Op MPIU_MAXSUM_OP;
1190570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
1191de272c7aSSatish Balay PETSC_EXTERN MPI_Op MPIU_SUM;
1192014dd563SJed Brown PETSC_EXTERN MPI_Op MPIU_MAX;
1193014dd563SJed Brown PETSC_EXTERN MPI_Op MPIU_MIN;
1194d9822059SBarry Smith #else
1195de272c7aSSatish Balay #define MPIU_SUM MPI_SUM
1196d9822059SBarry Smith #define MPIU_MAX MPI_MAX
1197d9822059SBarry Smith #define MPIU_MIN MPI_MIN
1198d9822059SBarry Smith #endif
11999e517322SPierre Jolivet #if defined(PETSC_HAVE_REAL___FLOAT128) || defined(PETSC_HAVE_REAL___FP16)
1200613bf2b2SPierre Jolivet /*MC
12019e517322SPierre Jolivet     MPIU_SUM___FP16___FLOAT128 - MPI_Op that acts as a replacement for MPI_SUM with
12029e517322SPierre Jolivet     custom MPI_Datatype `MPIU___FLOAT128`, `MPIU___COMPLEX128`, and `MPIU___FP16`.
1203613bf2b2SPierre Jolivet 
1204613bf2b2SPierre Jolivet    Level: advanced
1205613bf2b2SPierre Jolivet 
1206613bf2b2SPierre Jolivet    Developer Note:
1207613bf2b2SPierre Jolivet    This should be unified with `MPIU_SUM`
1208613bf2b2SPierre Jolivet 
1209613bf2b2SPierre Jolivet .seealso: `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`
1210613bf2b2SPierre Jolivet M*/
12119e517322SPierre Jolivet PETSC_EXTERN MPI_Op MPIU_SUM___FP16___FLOAT128;
1212613bf2b2SPierre Jolivet #endif
1213014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMaxSum(MPI_Comm, const PetscInt[], PetscInt *, PetscInt *);
1214d382aafbSBarry Smith 
1215014dd563SJed Brown PETSC_EXTERN PetscErrorCode MPIULong_Send(void *, PetscInt, MPI_Datatype, PetscMPIInt, PetscMPIInt, MPI_Comm);
1216014dd563SJed Brown PETSC_EXTERN PetscErrorCode MPIULong_Recv(void *, PetscInt, MPI_Datatype, PetscMPIInt, PetscMPIInt, MPI_Comm);
1217eb3f98d2SBarry Smith 
121895c0884eSLisandro Dalcin PETSC_EXTERN const char *const PetscFileModes[];
1219d6a4318aSJed Brown 
1220639ff905SBarry Smith /*
1221639ff905SBarry Smith     Defines PETSc error handling.
1222639ff905SBarry Smith */
1223639ff905SBarry Smith #include <petscerror.h>
1224d382aafbSBarry Smith 
1225660278c0SBarry Smith PETSC_EXTERN PetscBool   PetscCIEnabled;                    /* code is running in the PETSc test harness CI */
1226660278c0SBarry Smith PETSC_EXTERN PetscBool   PetscCIEnabledPortableErrorOutput; /* error output is stripped to ensure portability of error messages across systems */
1227660278c0SBarry Smith PETSC_EXTERN const char *PetscCIFilename(const char *);
1228660278c0SBarry Smith PETSC_EXTERN int         PetscCILinenumber(int);
1229660278c0SBarry Smith 
12300700a824SBarry Smith #define PETSC_SMALLEST_CLASSID 1211211
1231014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_LARGEST_CLASSID;
1232014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_OBJECT_CLASSID;
1233014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscClassIdRegister(const char[], PetscClassId *);
123481256985SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscObjectGetId(PetscObject, PetscObjectId *);
123581256985SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscObjectCompareId(PetscObject, PetscObjectId, PetscBool *);
123681256985SMatthew G. Knepley 
1237d382aafbSBarry Smith /*
1238d382aafbSBarry Smith    Routines that get memory usage information from the OS
1239d382aafbSBarry Smith */
1240014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemoryGetCurrentUsage(PetscLogDouble *);
1241014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemoryGetMaximumUsage(PetscLogDouble *);
1242014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemorySetGetMaximumUsage(void);
1243b44d5720SBarry Smith PETSC_EXTERN PetscErrorCode PetscMemoryTrace(const char[]);
1244d382aafbSBarry Smith 
1245014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSleep(PetscReal);
1246d382aafbSBarry Smith 
1247d382aafbSBarry Smith /*
1248d382aafbSBarry Smith    Initialization of PETSc
1249d382aafbSBarry Smith */
1250014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitialize(int *, char ***, const char[], const char[]);
1251014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeNoPointers(int, char **, const char[], const char[]);
1252014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeNoArguments(void);
1253014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitialized(PetscBool *);
1254014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFinalized(PetscBool *);
1255014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFinalize(void);
1256014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeFortran(void);
1257014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArgs(int *, char ***);
1258014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArguments(char ***);
1259014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFreeArguments(char **);
1260d382aafbSBarry Smith 
1261014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscEnd(void);
1262607a6623SBarry Smith PETSC_EXTERN PetscErrorCode PetscSysInitializePackage(void);
1263d382aafbSBarry Smith 
1264014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonInitialize(const char[], const char[]);
1265014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonFinalize(void);
1266014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonPrintError(void);
1267014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonMonitorSet(PetscObject, const char[]);
1268d382aafbSBarry Smith 
1269a50e088cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMonitorCompare(PetscErrorCode (*)(void), void *, PetscErrorCode (*)(void **), PetscErrorCode (*)(void), void *, PetscErrorCode (*)(void **), PetscBool *);
1270a50e088cSMatthew G. Knepley 
1271d382aafbSBarry Smith /*
1272d382aafbSBarry Smith      These are so that in extern C code we can caste function pointers to non-extern C
12732981ebdbSBarry Smith    function pointers. Since the regular C++ code expects its function pointers to be C++
1274d382aafbSBarry Smith */
1275638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef void (**PetscVoidStarFunction)(void);
1276638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef void (*PetscVoidFunction)(void);
1277638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef PetscErrorCode (*PetscErrorCodeFunction)(void);
1278d382aafbSBarry Smith 
1279d382aafbSBarry Smith /*
1280d382aafbSBarry Smith     Functions that can act on any PETSc object.
1281d382aafbSBarry Smith */
1282014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectDestroy(PetscObject *);
1283014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetComm(PetscObject, MPI_Comm *);
1284014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetClassId(PetscObject, PetscClassId *);
1285014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetClassName(PetscObject, const char *[]);
1286014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetType(PetscObject, const char *[]);
1287014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetName(PetscObject, const char[]);
1288014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetName(PetscObject, const char *[]);
1289014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetTabLevel(PetscObject, PetscInt);
1290014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetTabLevel(PetscObject, PetscInt *);
1291014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectIncrementTabLevel(PetscObject, PetscObject, PetscInt);
1292014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectReference(PetscObject);
1293014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetReference(PetscObject, PetscInt *);
1294014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectDereference(PetscObject);
1295014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetNewTag(PetscObject, PetscMPIInt *);
1296014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectCompose(PetscObject, const char[], PetscObject);
1297014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRemoveReference(PetscObject, const char[]);
1298014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectQuery(PetscObject, const char[], PetscObject *);
1299bdf89e91SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectComposeFunction_Private(PetscObject, const char[], void (*)(void));
1300bdf89e91SBarry Smith #define PetscObjectComposeFunction(a, b, d) PetscObjectComposeFunction_Private(a, b, (PetscVoidFunction)(d))
1301014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetFromOptions(PetscObject);
1302014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetUp(PetscObject);
13030eb63584SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSetPrintedOptions(PetscObject);
13040eb63584SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectInheritPrintedOptions(PetscObject, PetscObject);
1305014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommGetNewTag(MPI_Comm, PetscMPIInt *);
1306d382aafbSBarry Smith 
1307665c2dedSJed Brown #include <petscviewertypes.h>
1308639ff905SBarry Smith #include <petscoptions.h>
1309639ff905SBarry Smith 
1310608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocTraceSet(PetscViewer, PetscBool, PetscLogDouble);
1311608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocTraceGet(PetscBool *);
1312608c71bfSMatthew G. Knepley 
13130633abcbSJed Brown PETSC_EXTERN PetscErrorCode PetscObjectsListGetGlobalNumbering(MPI_Comm, PetscInt, PetscObject *, PetscInt *, PetscInt *);
13140633abcbSJed Brown 
1315c5e4d11fSDmitry Karpeev PETSC_EXTERN PetscErrorCode PetscMemoryView(PetscViewer, const char[]);
1316dae58748SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject, PetscViewer);
1317639ff905SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectView(PetscObject, PetscViewer);
13180005d66cSJed Brown #define PetscObjectQueryFunction(obj, name, fptr) PetscObjectQueryFunction_Private((obj), (name), (PetscVoidFunction *)(fptr))
13190005d66cSJed Brown PETSC_EXTERN PetscErrorCode PetscObjectQueryFunction_Private(PetscObject, const char[], void (**)(void));
1320014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetOptionsPrefix(PetscObject, const char[]);
1321014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject, const char[]);
1322014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject, const char[]);
1323014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject, const char *[]);
1324014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectChangeTypeName(PetscObject, const char[]);
1325014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroy(PetscObject);
1326014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroyAll(void);
1327685405a1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectViewFromOptions(PetscObject, PetscObject, const char[]);
1328014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectName(PetscObject);
1329014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectTypeCompare(PetscObject, const char[], PetscBool *);
1330*013e2dc7SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectObjectTypeCompare(PetscObject, PetscObject, PetscBool *);
13314099cc6bSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompare(PetscObject, const char[], PetscBool *);
1332014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1333b9e7e5c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1334014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*)(void));
1335014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRegisterFinalizeAll(void);
1336d382aafbSBarry Smith 
1337e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
13387aab2a10SBarry Smith PETSC_EXTERN PetscErrorCode PetscSAWsBlock(void);
1339e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsViewOff(PetscObject);
1340e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsSetBlock(PetscObject, PetscBool);
1341e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsBlock(PetscObject);
1342e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsGrantAccess(PetscObject);
1343e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsTakeAccess(PetscObject);
1344e04113cfSBarry Smith PETSC_EXTERN void           PetscStackSAWsGrantAccess(void);
1345e04113cfSBarry Smith PETSC_EXTERN void           PetscStackSAWsTakeAccess(void);
1346e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscStackViewSAWs(void);
1347e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscStackSAWsViewOff(void);
134815681b3cSBarry Smith 
1349ec7429eaSBarry Smith #else
13507aab2a10SBarry Smith #define PetscSAWsBlock()                  0
1351e04113cfSBarry Smith #define PetscObjectSAWsViewOff(obj)       0
1352e04113cfSBarry Smith #define PetscObjectSAWsSetBlock(obj, flg) 0
1353e04113cfSBarry Smith #define PetscObjectSAWsBlock(obj)         0
1354e04113cfSBarry Smith #define PetscObjectSAWsGrantAccess(obj)   0
1355e04113cfSBarry Smith #define PetscObjectSAWsTakeAccess(obj)    0
1356e04113cfSBarry Smith #define PetscStackViewSAWs()              0
1357e04113cfSBarry Smith #define PetscStackSAWsViewOff()           0
1358e04113cfSBarry Smith #define PetscStackSAWsTakeAccess()
1359e04113cfSBarry Smith #define PetscStackSAWsGrantAccess()
136015681b3cSBarry Smith 
1361ec7429eaSBarry Smith #endif
1362b90c6cbeSBarry Smith 
136382b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLOpen(const char[], PetscDLMode, PetscDLHandle *);
136482b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLClose(PetscDLHandle *);
136582b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLSym(PetscDLHandle, const char[], void **);
1366258ec3d2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscDLAddr(void (*)(void), char **);
1367258ec3d2SMatthew G. Knepley #ifdef PETSC_HAVE_CXX
1368258ec3d2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscDemangleSymbol(const char *, char **);
1369258ec3d2SMatthew G. Knepley #endif
13707d5d4d99SBarry Smith 
13715fe58b6fSBarry Smith #if defined(PETSC_USE_DEBUG)
1372a64a8e02SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocGetStack(void *, PetscStack **);
13735fe58b6fSBarry Smith #endif
13747eb1d149SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectsDump(FILE *, PetscBool);
1375a64a8e02SBarry Smith 
1376140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListDestroy(PetscObjectList *);
1377140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListFind(PetscObjectList, const char[], PetscObject *);
1378140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListReverseFind(PetscObjectList, PetscObject, char **, PetscBool *);
1379140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListAdd(PetscObjectList *, const char[], PetscObject);
1380140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListRemoveReference(PetscObjectList *, const char[]);
1381140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListDuplicate(PetscObjectList, PetscObjectList *);
1382d382aafbSBarry Smith 
1383d382aafbSBarry Smith /*
1384503cfb0cSBarry Smith     Dynamic library lists. Lists of names of routines in objects or in dynamic
1385d382aafbSBarry Smith   link libraries that will be loaded as needed.
1386d382aafbSBarry Smith */
1387a240a19fSJed Brown 
1388a240a19fSJed Brown #define PetscFunctionListAdd(list, name, fptr) PetscFunctionListAdd_Private((list), (name), (PetscVoidFunction)(fptr))
1389a240a19fSJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *, const char[], void (*)(void));
1390140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListDestroy(PetscFunctionList *);
13911c9cd337SJed Brown #define PetscFunctionListFind(list, name, fptr) PetscFunctionListFind_Private((list), (name), (PetscVoidFunction *)(fptr))
13921c9cd337SJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList, const char[], void (**)(void));
139344ef3d73SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListPrintTypes(MPI_Comm, FILE *, const char[], const char[], const char[], const char[], PetscFunctionList, const char[], const char[]);
1394140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListDuplicate(PetscFunctionList, PetscFunctionList *);
1395140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListView(PetscFunctionList, PetscViewer);
1396140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListGet(PetscFunctionList, const char ***, int *);
13972e956fe4SStefano Zampini PETSC_EXTERN PetscErrorCode PetscFunctionListPrintNonEmpty(PetscFunctionList);
13982e956fe4SStefano Zampini PETSC_EXTERN PetscErrorCode PetscFunctionListPrintAll(void);
1399d382aafbSBarry Smith 
1400014dd563SJed Brown PETSC_EXTERN PetscDLLibrary PetscDLLibrariesLoaded;
1401014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryAppend(MPI_Comm, PetscDLLibrary *, const char[]);
1402014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryPrepend(MPI_Comm, PetscDLLibrary *, const char[]);
1403014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibrarySym(MPI_Comm, PetscDLLibrary *, const char[], const char[], void **);
1404014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryPrintPath(PetscDLLibrary);
1405014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryRetrieve(MPI_Comm, const char[], char *, size_t, PetscBool *);
1406014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryOpen(MPI_Comm, const char[], PetscDLLibrary *);
1407014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryClose(PetscDLLibrary);
1408d382aafbSBarry Smith 
1409d382aafbSBarry Smith /*
1410d382aafbSBarry Smith      Useful utility routines
1411d382aafbSBarry Smith */
1412014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSplitOwnership(MPI_Comm, PetscInt *, PetscInt *);
1413014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSplitOwnershipBlock(MPI_Comm, PetscInt, PetscInt *, PetscInt *);
1414d24d4204SJose E. Roman PETSC_EXTERN PetscErrorCode PetscSplitOwnershipEqual(MPI_Comm, PetscInt *, PetscInt *);
1415014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSequentialPhaseBegin(MPI_Comm, PetscMPIInt);
1416014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSequentialPhaseEnd(MPI_Comm, PetscMPIInt);
1417014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBarrier(PetscObject);
1418014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIDump(FILE *);
141958b5cd2aSSatish Balay PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxInt(MPI_Comm, const PetscInt[2], PetscInt[2]);
142058b5cd2aSSatish Balay PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxReal(MPI_Comm, const PetscReal[2], PetscReal[2]);
1421d382aafbSBarry Smith 
142264ac3b0dSPatrick Sanan /*MC
142387497f52SBarry Smith     PetscNot - negates a logical type value and returns result as a `PetscBool`
1424503cfb0cSBarry Smith 
1425e28ce29aSPatrick Sanan     Notes:
1426e28ce29aSPatrick Sanan     This is useful in cases like
1427503cfb0cSBarry Smith $     int        *a;
1428ace3abfcSBarry Smith $     PetscBool  flag = PetscNot(a)
14291cc8b206SBarry Smith      where !a would not return a PetscBool because we cannot provide a cast from int to PetscBool in C.
143064ac3b0dSPatrick Sanan 
143164ac3b0dSPatrick Sanan     Level: beginner
143264ac3b0dSPatrick Sanan 
1433db781477SPatrick Sanan     .seealso `:` `PetscBool`, `PETSC_TRUE`, `PETSC_FALSE`
143464ac3b0dSPatrick Sanan M*/
1435d382aafbSBarry Smith #define PetscNot(a) ((a) ? PETSC_FALSE : PETSC_TRUE)
1436503cfb0cSBarry Smith 
1437d382aafbSBarry Smith /*MC
1438d382aafbSBarry Smith     PetscHelpPrintf - Prints help messages.
1439d382aafbSBarry Smith 
1440d382aafbSBarry Smith    Synopsis:
1441aaa7dc30SBarry Smith     #include <petscsys.h>
1442659f7ba0SBarry Smith      PetscErrorCode (*PetscHelpPrintf)(MPI_Comm comm, const char format[],args);
1443d382aafbSBarry Smith 
1444659f7ba0SBarry Smith     Collective on comm
1445eca87e8dSBarry Smith 
1446d382aafbSBarry Smith     Input Parameters:
1447659f7ba0SBarry Smith +  comm - the MPI communicator over which the help message is printed
1448d382aafbSBarry Smith .  format - the usual printf() format string
1449659f7ba0SBarry Smith -  args - arguments to be printed
1450d382aafbSBarry Smith 
1451d382aafbSBarry Smith    Level: developer
1452d382aafbSBarry Smith 
1453d382aafbSBarry Smith    Fortran Note:
1454d382aafbSBarry Smith      This routine is not supported in Fortran.
1455d382aafbSBarry Smith 
1456659f7ba0SBarry Smith    Note:
1457659f7ba0SBarry Smith      You can change how help messages are printed by replacing the function pointer with a function that does not simply write to stdout.
1458659f7ba0SBarry Smith 
1459659f7ba0SBarry Smith       To use, write your own function, for example,
1460659f7ba0SBarry Smith $PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....)
1461659f7ba0SBarry Smith ${
1462659f7ba0SBarry Smith $ PetscFunctionReturn(0);
1463659f7ba0SBarry Smith $}
1464659f7ba0SBarry Smith then do the assigment
1465659f7ba0SBarry Smith $    PetscHelpPrintf = mypetschelpprintf;
146687497f52SBarry Smith    You can do the assignment before `PetscInitialize()`.
1467659f7ba0SBarry Smith 
146887497f52SBarry Smith   The default routine used is called `PetscHelpPrintfDefault()`.
1469d382aafbSBarry Smith 
1470db781477SPatrick Sanan .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscErrorPrintf()`
1471d382aafbSBarry Smith M*/
14723ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode (*PetscHelpPrintf)(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1473d382aafbSBarry Smith 
1474fcfd50ebSBarry Smith /*
1475fcfd50ebSBarry Smith      Defines PETSc profiling.
1476fcfd50ebSBarry Smith */
14772c8e378dSBarry Smith #include <petsclog.h>
1478fcfd50ebSBarry Smith 
1479fcfd50ebSBarry Smith /*
1480fcfd50ebSBarry Smith       Simple PETSc parallel IO for ASCII printing
1481fcfd50ebSBarry Smith */
1482014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFixFilename(const char[], char[]);
1483014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFOpen(MPI_Comm, const char[], const char[], FILE **);
1484014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFClose(MPI_Comm, FILE *);
14853ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
14863ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
14873ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSNPrintf(char *, size_t, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
14883ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSNPrintfCount(char *, size_t, const char[], size_t *, ...) PETSC_ATTRIBUTE_FORMAT(3, 5);
14891b5687a1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatRealArray(char[], size_t, const char *, PetscInt, const PetscReal[]);
1490fcfd50ebSBarry Smith 
14913ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscErrorPrintfDefault(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
14923ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscErrorPrintfNone(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
14933ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscHelpPrintfDefault(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1494d382aafbSBarry Smith 
1495d781fa04SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatConvertGetSize(const char *, size_t *);
1496d781fa04SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatConvert(const char *, char *);
1497d781fa04SBarry Smith 
1498d382aafbSBarry Smith #if defined(PETSC_HAVE_POPEN)
1499014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPOpen(MPI_Comm, const char[], const char[], const char[], FILE **);
1500016831caSBarry Smith PETSC_EXTERN PetscErrorCode PetscPClose(MPI_Comm, FILE *);
150174ba8654SBarry Smith PETSC_EXTERN PetscErrorCode PetscPOpenSetMachine(const char[]);
1502d382aafbSBarry Smith #endif
1503d382aafbSBarry Smith 
15043ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSynchronizedPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
15053ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
15060ec8b6e3SBarry Smith PETSC_EXTERN PetscErrorCode PetscSynchronizedFlush(MPI_Comm, FILE *);
1507014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSynchronizedFGets(MPI_Comm, FILE *, size_t, char[]);
1508014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStartMatlab(MPI_Comm, const char[], const char[], FILE **);
1509014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStartJava(MPI_Comm, const char[], const char[], FILE **);
1510014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetPetscDir(const char *[]);
1511d382aafbSBarry Smith 
1512014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_CONTAINER_CLASSID;
1513014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerGetPointer(PetscContainer, void **);
1514014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerSetPointer(PetscContainer, void *);
1515014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerDestroy(PetscContainer *);
1516014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerCreate(MPI_Comm, PetscContainer *);
1517014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerSetUserDestroy(PetscContainer, PetscErrorCode (*)(void *));
1518b61ba218SStefano Zampini PETSC_EXTERN PetscErrorCode PetscContainerUserDestroyDefault(void *);
1519d382aafbSBarry Smith 
1520d382aafbSBarry Smith /*
1521d382aafbSBarry Smith    For use in debuggers
1522d382aafbSBarry Smith */
1523014dd563SJed Brown PETSC_EXTERN PetscMPIInt    PetscGlobalRank;
1524014dd563SJed Brown PETSC_EXTERN PetscMPIInt    PetscGlobalSize;
1525014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIntView(PetscInt, const PetscInt[], PetscViewer);
1526014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRealView(PetscInt, const PetscReal[], PetscViewer);
1527014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscScalarView(PetscInt, const PetscScalar[], PetscViewer);
1528d382aafbSBarry Smith 
1529a663daf8SBarry Smith #include <stddef.h>
1530a3aaec0aSJed Brown #include <string.h> /* for memcpy, memset */
1531d382aafbSBarry Smith #include <stdlib.h>
15326c7e564aSBarry Smith 
15334a92a979SJed Brown #if defined(PETSC_HAVE_XMMINTRIN_H) && !defined(__CUDACC__)
1534d382aafbSBarry Smith #include <xmmintrin.h>
1535d382aafbSBarry Smith #endif
1536d382aafbSBarry Smith 
1537d382aafbSBarry Smith /*@C
1538580bdb30SBarry Smith    PetscMemmove - Copies n bytes, beginning at location b, to the space
1539580bdb30SBarry Smith    beginning at location a. Copying  between regions that overlap will
154087497f52SBarry Smith    take place correctly. Use `PetscMemcpy()` if the locations do not overlap
1541580bdb30SBarry Smith 
1542580bdb30SBarry Smith    Not Collective
1543580bdb30SBarry Smith 
1544580bdb30SBarry Smith    Input Parameters:
1545580bdb30SBarry Smith +  b - pointer to initial memory space
1546580bdb30SBarry Smith .  a - pointer to copy space
1547580bdb30SBarry Smith -  n - length (in bytes) of space to copy
1548580bdb30SBarry Smith 
1549580bdb30SBarry Smith    Level: intermediate
1550580bdb30SBarry Smith 
155187497f52SBarry Smith    Notes:
155287497f52SBarry Smith    `PetscArraymove()` is preferred
1553580bdb30SBarry Smith 
155487497f52SBarry Smith    This routine is analogous to `memmove()`.
155587497f52SBarry Smith 
155687497f52SBarry Smith    Developers Note:
155787497f52SBarry Smith    This is inlined for performance
1558580bdb30SBarry Smith 
1559db781477SPatrick Sanan .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscMemzero()`, `PetscArraycmp()`, `PetscArraycpy()`, `PetscStrallocpy()`,
1560db781477SPatrick Sanan           `PetscArraymove()`
1561580bdb30SBarry Smith @*/
15629371c9d4SSatish Balay static inline PetscErrorCode PetscMemmove(void *a, const void *b, size_t n) {
1563580bdb30SBarry Smith   PetscFunctionBegin;
15649a202e32SJacob Faibussowitsch   if (n > 0) {
15652c71b3e2SJacob Faibussowitsch     PetscCheck(a, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Trying to copy to null pointer");
15662c71b3e2SJacob Faibussowitsch     PetscCheck(b, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Trying to copy from a null pointer");
15679a202e32SJacob Faibussowitsch   }
1568580bdb30SBarry Smith #if !defined(PETSC_HAVE_MEMMOVE)
1569580bdb30SBarry Smith   if (a < b) {
1570580bdb30SBarry Smith     if (a <= b - n) memcpy(a, b, n);
1571580bdb30SBarry Smith     else {
1572580bdb30SBarry Smith       memcpy(a, b, (int)(b - a));
1573580bdb30SBarry Smith       PetscMemmove(b, b + (int)(b - a), n - (int)(b - a));
1574580bdb30SBarry Smith     }
1575580bdb30SBarry Smith   } else {
1576580bdb30SBarry Smith     if (b <= a - n) memcpy(a, b, n);
1577580bdb30SBarry Smith     else {
1578580bdb30SBarry Smith       memcpy(b + n, b + (n - (int)(a - b)), (int)(a - b));
1579580bdb30SBarry Smith       PetscMemmove(a, b, n - (int)(a - b));
1580580bdb30SBarry Smith     }
1581580bdb30SBarry Smith   }
1582580bdb30SBarry Smith #else
1583580bdb30SBarry Smith   memmove((char *)(a), (char *)(b), n);
1584580bdb30SBarry Smith #endif
1585580bdb30SBarry Smith   PetscFunctionReturn(0);
1586580bdb30SBarry Smith }
1587580bdb30SBarry Smith 
1588d382aafbSBarry Smith /*@C
1589d382aafbSBarry Smith    PetscMemcpy - Copies n bytes, beginning at location b, to the space
1590d382aafbSBarry Smith    beginning at location a. The two memory regions CANNOT overlap, use
159187497f52SBarry Smith    `PetscMemmove()` in that case.
1592d382aafbSBarry Smith 
1593d382aafbSBarry Smith    Not Collective
1594d382aafbSBarry Smith 
1595d382aafbSBarry Smith    Input Parameters:
1596d382aafbSBarry Smith +  b - pointer to initial memory space
1597d382aafbSBarry Smith -  n - length (in bytes) of space to copy
1598d382aafbSBarry Smith 
1599d382aafbSBarry Smith    Output Parameter:
1600d382aafbSBarry Smith .  a - pointer to copy space
1601d382aafbSBarry Smith 
1602d382aafbSBarry Smith    Level: intermediate
1603d382aafbSBarry Smith 
1604d382aafbSBarry Smith    Compile Option:
160587497f52SBarry Smith     `PETSC_PREFER_DCOPY_FOR_MEMCPY` will cause the BLAS dcopy() routine to be used
1606d382aafbSBarry Smith                                   for memory copies on double precision values.
160787497f52SBarry Smith     `PETSC_PREFER_COPY_FOR_MEMCPY` will cause C code to be used
1608d382aafbSBarry Smith                                   for memory copies on double precision values.
160987497f52SBarry Smith     `PETSC_PREFER_FORTRAN_FORMEMCPY` will cause Fortran code to be used
1610d382aafbSBarry Smith                                   for memory copies on double precision values.
1611d382aafbSBarry Smith 
161287497f52SBarry Smith    Notes:
161387497f52SBarry Smith    Prefer `PetscArraycpy()`
161487497f52SBarry Smith 
161587497f52SBarry Smith    This routine is analogous to `memcpy()`.
161687497f52SBarry Smith 
16171fd49c25SBarry Smith    Not available from Fortran
16181fd49c25SBarry Smith 
161987497f52SBarry Smith    Developer Note:
162087497f52SBarry Smith    This is inlined for fastest performance
1621f5f57ec0SBarry Smith 
1622db781477SPatrick Sanan .seealso: `PetscMemzero()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscArraycmp()`, `PetscArraycpy()`, `PetscMemmove()`, `PetscStrallocpy()`
1623d382aafbSBarry Smith 
1624d382aafbSBarry Smith @*/
16259371c9d4SSatish Balay static inline PetscErrorCode PetscMemcpy(void *a, const void *b, size_t n) {
1626d382aafbSBarry Smith #if defined(PETSC_USE_DEBUG)
1627c5e4d11fSDmitry Karpeev   size_t al = (size_t)a, bl = (size_t)b;
1628c5e4d11fSDmitry Karpeev   size_t nl = (size_t)n;
1629a7e36092SMatthew G Knepley   PetscFunctionBegin;
16309a202e32SJacob Faibussowitsch   if (n > 0) {
16312c71b3e2SJacob Faibussowitsch     PetscCheck(b, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Trying to copy from a null pointer");
16322c71b3e2SJacob Faibussowitsch     PetscCheck(a, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Trying to copy to a null pointer");
16339a202e32SJacob Faibussowitsch   }
1634a7e36092SMatthew G Knepley #else
1635d382aafbSBarry Smith   PetscFunctionBegin;
1636a7e36092SMatthew G Knepley #endif
1637a47a537aSLisandro Dalcin   if (a != b && n > 0) {
1638d382aafbSBarry Smith #if defined(PETSC_USE_DEBUG)
16392c71b3e2SJacob Faibussowitsch     PetscCheck(!((al > bl && (al - bl) < nl) || (bl - al) < nl), PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Memory regions overlap: either use PetscMemmov()\n\
1640d382aafbSBarry Smith               or make sure your copy regions and lengths are correct. \n\
16419371c9d4SSatish Balay               Length (bytes) %zu first address %zu second address %zu",
16429371c9d4SSatish Balay                nl, al, bl);
1643d382aafbSBarry Smith #endif
1644d382aafbSBarry Smith #if (defined(PETSC_PREFER_DCOPY_FOR_MEMCPY) || defined(PETSC_PREFER_COPY_FOR_MEMCPY) || defined(PETSC_PREFER_FORTRAN_FORMEMCPY))
1645c5e4d11fSDmitry Karpeev     if (!(a % sizeof(PetscScalar)) && !(n % sizeof(PetscScalar))) {
1646d382aafbSBarry Smith       size_t len = n / sizeof(PetscScalar);
1647d382aafbSBarry Smith #if defined(PETSC_PREFER_DCOPY_FOR_MEMCPY)
1648c5df96a5SBarry Smith       PetscBLASInt one = 1, blen;
16499566063dSJacob Faibussowitsch       PetscCall(PetscBLASIntCast(len, &blen));
1650792fecdfSBarry Smith       PetscCallBLAS("BLAScopy", BLAScopy_(&blen, (PetscScalar *)b, &one, (PetscScalar *)a, &one));
1651d382aafbSBarry Smith #elif defined(PETSC_PREFER_FORTRAN_FORMEMCPY)
1652d382aafbSBarry Smith       fortrancopy_(&len, (PetscScalar *)b, (PetscScalar *)a);
1653d382aafbSBarry Smith #else
1654d382aafbSBarry Smith       PetscScalar *x = (PetscScalar *)b, *y = (PetscScalar *)a;
16555f80ce2aSJacob Faibussowitsch       for (size_t i = 0; i < len; i++) y[i] = x[i];
1656d382aafbSBarry Smith #endif
1657d382aafbSBarry Smith     } else {
1658d382aafbSBarry Smith       memcpy((char *)(a), (char *)(b), n);
1659d382aafbSBarry Smith     }
1660d382aafbSBarry Smith #else
1661d382aafbSBarry Smith     memcpy((char *)(a), (char *)(b), n);
1662d382aafbSBarry Smith #endif
1663d382aafbSBarry Smith   }
1664d382aafbSBarry Smith   PetscFunctionReturn(0);
1665d382aafbSBarry Smith }
1666d382aafbSBarry Smith 
1667d382aafbSBarry Smith /*@C
1668d382aafbSBarry Smith    PetscMemzero - Zeros the specified memory.
1669d382aafbSBarry Smith 
1670d382aafbSBarry Smith    Not Collective
1671d382aafbSBarry Smith 
1672d382aafbSBarry Smith    Input Parameters:
1673d382aafbSBarry Smith +  a - pointer to beginning memory location
1674d382aafbSBarry Smith -  n - length (in bytes) of memory to initialize
1675d382aafbSBarry Smith 
1676d382aafbSBarry Smith    Level: intermediate
1677d382aafbSBarry Smith 
1678d382aafbSBarry Smith    Compile Option:
167987497f52SBarry Smith    `PETSC_PREFER_BZERO` - on certain machines (the IBM RS6000) the bzero() routine happens
1680d382aafbSBarry Smith   to be faster than the memset() routine. This flag causes the bzero() routine to be used.
1681d382aafbSBarry Smith 
168287497f52SBarry Smith    Notes:
16831fd49c25SBarry Smith    Not available from Fortran
16841fd49c25SBarry Smith 
168587497f52SBarry Smith    Prefer `PetscArrayzero()`
168687497f52SBarry Smith 
168787497f52SBarry Smith    Developer Note:
168887497f52SBarry Smith    This is inlined for fastest performance
1689503cfb0cSBarry Smith 
1690db781477SPatrick Sanan .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscArraycmp()`, `PetscArraycpy()`, `PetscMemmove()`, `PetscStrallocpy()`
1691d382aafbSBarry Smith @*/
16929371c9d4SSatish Balay static inline PetscErrorCode PetscMemzero(void *a, size_t n) {
1693d382aafbSBarry Smith   if (n > 0) {
1694d382aafbSBarry Smith #if defined(PETSC_USE_DEBUG)
16952c71b3e2SJacob Faibussowitsch     PetscCheck(a, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Trying to zero at a null pointer with %zu bytes", n);
1696d382aafbSBarry Smith #endif
1697d382aafbSBarry Smith #if defined(PETSC_PREFER_ZERO_FOR_MEMZERO)
1698d382aafbSBarry Smith     if (!(((long)a) % sizeof(PetscScalar)) && !(n % sizeof(PetscScalar))) {
1699d382aafbSBarry Smith       size_t       i, len = n / sizeof(PetscScalar);
1700d382aafbSBarry Smith       PetscScalar *x = (PetscScalar *)a;
1701d382aafbSBarry Smith       for (i = 0; i < len; i++) x[i] = 0.0;
1702d382aafbSBarry Smith     } else {
1703d382aafbSBarry Smith #elif defined(PETSC_PREFER_FORTRAN_FOR_MEMZERO)
1704d382aafbSBarry Smith     if (!(((long)a) % sizeof(PetscScalar)) && !(n % sizeof(PetscScalar))) {
1705d382aafbSBarry Smith       PetscInt len = n / sizeof(PetscScalar);
1706d382aafbSBarry Smith       fortranzero_(&len, (PetscScalar *)a);
1707d382aafbSBarry Smith     } else {
1708d382aafbSBarry Smith #endif
1709d382aafbSBarry Smith #if defined(PETSC_PREFER_BZERO)
1710d382aafbSBarry Smith       bzero((char *)a, n);
1711d382aafbSBarry Smith #else
1712d382aafbSBarry Smith       memset((char *)a, 0, n);
1713d382aafbSBarry Smith #endif
1714d382aafbSBarry Smith #if defined(PETSC_PREFER_ZERO_FOR_MEMZERO) || defined(PETSC_PREFER_FORTRAN_FOR_MEMZERO)
1715d382aafbSBarry Smith     }
1716d382aafbSBarry Smith #endif
1717d382aafbSBarry Smith   }
1718d382aafbSBarry Smith   return 0;
1719d382aafbSBarry Smith }
1720d382aafbSBarry Smith 
1721d382aafbSBarry Smith /*MC
1722580bdb30SBarry Smith    PetscArraycmp - Compares two arrays in memory.
1723580bdb30SBarry Smith 
1724580bdb30SBarry Smith    Synopsis:
1725580bdb30SBarry Smith     #include <petscsys.h>
1726580bdb30SBarry Smith     PetscErrorCode PetscArraycmp(const anytype *str1,const anytype *str2,size_t cnt,PetscBool *e)
1727580bdb30SBarry Smith 
1728580bdb30SBarry Smith    Not Collective
1729580bdb30SBarry Smith 
1730580bdb30SBarry Smith    Input Parameters:
1731580bdb30SBarry Smith +  str1 - First array
1732580bdb30SBarry Smith .  str2 - Second array
1733580bdb30SBarry Smith -  cnt  - Count of the array, not in bytes, but number of entries in the arrays
1734580bdb30SBarry Smith 
1735580bdb30SBarry Smith    Output Parameters:
173687497f52SBarry Smith .   e - `PETSC_TRUE` if equal else `PETSC_FALSE`.
1737580bdb30SBarry Smith 
1738580bdb30SBarry Smith    Level: intermediate
1739580bdb30SBarry Smith 
174087497f52SBarry Smith    Notes:
174187497f52SBarry Smith    This routine is a preferred replacement to `PetscMemcmp()`
174287497f52SBarry Smith 
1743580bdb30SBarry Smith    The arrays must be of the same type
1744580bdb30SBarry Smith 
1745db781477SPatrick Sanan .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscMemzero()`, `PetscArraycpy()`, `PetscMemmove()`, `PetscStrallocpy()`,
1746db781477SPatrick Sanan           `PetscArraymove()`
1747580bdb30SBarry Smith M*/
17489e2232c7SLisandro Dalcin #define PetscArraycmp(str1, str2, cnt, e) ((sizeof(*(str1)) != sizeof(*(str2))) || PetscMemcmp(str1, str2, (size_t)(cnt) * sizeof(*(str1)), e))
1749580bdb30SBarry Smith 
17508f8f2f0dSBarry Smith /*MC
175187497f52SBarry Smith    PetscArraymove - Copies from one array in memory to another, the arrays may overlap. Use `PetscArraycpy()` when the arrays
1752580bdb30SBarry Smith                     do not overlap
1753580bdb30SBarry Smith 
1754580bdb30SBarry Smith    Synopsis:
1755580bdb30SBarry Smith     #include <petscsys.h>
1756580bdb30SBarry Smith     PetscErrorCode PetscArraymove(anytype *str1,const anytype *str2,size_t cnt)
1757580bdb30SBarry Smith 
1758580bdb30SBarry Smith    Not Collective
1759580bdb30SBarry Smith 
1760580bdb30SBarry Smith    Input Parameters:
1761580bdb30SBarry Smith +  str1 - First array
1762580bdb30SBarry Smith .  str2 - Second array
1763580bdb30SBarry Smith -  cnt  - Count of the array, not in bytes, but number of entries in the arrays
1764580bdb30SBarry Smith 
1765580bdb30SBarry Smith    Level: intermediate
1766580bdb30SBarry Smith 
176787497f52SBarry Smith    Notes:
176887497f52SBarry Smith    This routine is a preferred replacement to `PetscMemmove()`
176987497f52SBarry Smith 
1770580bdb30SBarry Smith    The arrays must be of the same type
1771580bdb30SBarry Smith 
1772db781477SPatrick Sanan .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscMemzero()`, `PetscArraycpy()`, `PetscMemmove()`, `PetscArraycmp()`, `PetscStrallocpy()`
1773580bdb30SBarry Smith M*/
17749e2232c7SLisandro Dalcin #define PetscArraymove(str1, str2, cnt) ((sizeof(*(str1)) != sizeof(*(str2))) || PetscMemmove(str1, str2, (size_t)(cnt) * sizeof(*(str1))))
1775580bdb30SBarry Smith 
17768f8f2f0dSBarry Smith /*MC
1777580bdb30SBarry Smith    PetscArraycpy - Copies from one array in memory to another
1778580bdb30SBarry Smith 
1779580bdb30SBarry Smith    Synopsis:
1780580bdb30SBarry Smith     #include <petscsys.h>
1781580bdb30SBarry Smith     PetscErrorCode PetscArraycpy(anytype *str1,const anytype *str2,size_t cnt)
1782580bdb30SBarry Smith 
1783580bdb30SBarry Smith    Not Collective
1784580bdb30SBarry Smith 
1785580bdb30SBarry Smith    Input Parameters:
1786de25709eSStefano Zampini +  str1 - First array (destination)
1787de25709eSStefano Zampini .  str2 - Second array (source)
1788580bdb30SBarry Smith -  cnt  - Count of the array, not in bytes, but number of entries in the arrays
1789580bdb30SBarry Smith 
1790580bdb30SBarry Smith    Level: intermediate
1791580bdb30SBarry Smith 
179287497f52SBarry Smith    Notes:
179387497f52SBarry Smith    This routine is a preferred replacement to `PetscMemcpy()`
179487497f52SBarry Smith 
1795580bdb30SBarry Smith    The arrays must be of the same type
1796580bdb30SBarry Smith 
1797db781477SPatrick Sanan .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscArrayzero()`, `PetscMemzero()`, `PetscArraymove()`, `PetscMemmove()`, `PetscArraycmp()`, `PetscStrallocpy()`
1798580bdb30SBarry Smith M*/
17999e2232c7SLisandro Dalcin #define PetscArraycpy(str1, str2, cnt) ((sizeof(*(str1)) != sizeof(*(str2))) || PetscMemcpy(str1, str2, (size_t)(cnt) * sizeof(*(str1))))
1800580bdb30SBarry Smith 
18018f8f2f0dSBarry Smith /*MC
1802580bdb30SBarry Smith    PetscArrayzero - Zeros an array in memory.
1803580bdb30SBarry Smith 
1804580bdb30SBarry Smith    Synopsis:
1805580bdb30SBarry Smith     #include <petscsys.h>
1806580bdb30SBarry Smith     PetscErrorCode PetscArrayzero(anytype *str1,size_t cnt)
1807580bdb30SBarry Smith 
1808580bdb30SBarry Smith    Not Collective
1809580bdb30SBarry Smith 
1810580bdb30SBarry Smith    Input Parameters:
1811580bdb30SBarry Smith +  str1 - array
1812580bdb30SBarry Smith -  cnt  - Count of the array, not in bytes, but number of entries in the array
1813580bdb30SBarry Smith 
1814580bdb30SBarry Smith    Level: intermediate
1815580bdb30SBarry Smith 
1816580bdb30SBarry Smith    Note:
181787497f52SBarry Smith    This routine is a preferred replacement to `PetscMemzero()`
1818580bdb30SBarry Smith 
1819db781477SPatrick Sanan .seealso: `PetscMemcpy()`, `PetscMemcmp()`, `PetscMemzero()`, `PetscArraycmp()`, `PetscArraycpy()`, `PetscMemmove()`, `PetscStrallocpy()`, `PetscArraymove()`
1820580bdb30SBarry Smith M*/
18219e2232c7SLisandro Dalcin #define PetscArrayzero(str1, cnt) PetscMemzero(str1, (size_t)(cnt) * sizeof(*(str1)))
1822580bdb30SBarry Smith 
1823d382aafbSBarry Smith /*MC
1824d382aafbSBarry Smith    PetscPrefetchBlock - Prefetches a block of memory
1825d382aafbSBarry Smith 
1826eca87e8dSBarry Smith    Synopsis:
1827aaa7dc30SBarry Smith     #include <petscsys.h>
1828eca87e8dSBarry Smith     void PetscPrefetchBlock(const anytype *a,size_t n,int rw,int t)
1829eca87e8dSBarry Smith 
1830d382aafbSBarry Smith    Not Collective
1831d382aafbSBarry Smith 
1832d382aafbSBarry Smith    Input Parameters:
1833d382aafbSBarry Smith +  a - pointer to first element to fetch (any type but usually PetscInt or PetscScalar)
1834d382aafbSBarry Smith .  n - number of elements to fetch
1835d382aafbSBarry Smith .  rw - 1 if the memory will be written to, otherwise 0 (ignored by many processors)
183650d8bf02SJed Brown -  t - temporal locality (PETSC_PREFETCH_HINT_{NTA,T0,T1,T2}), see note
1837d382aafbSBarry Smith 
1838d382aafbSBarry Smith    Level: developer
1839d382aafbSBarry Smith 
1840d382aafbSBarry Smith    Notes:
1841d382aafbSBarry Smith    The last two arguments (rw and t) must be compile-time constants.
1842d382aafbSBarry Smith 
184350d8bf02SJed Brown    Adopting Intel's x86/x86-64 conventions, there are four levels of temporal locality.  Not all architectures offer
184450d8bf02SJed Brown    equivalent locality hints, but the following macros are always defined to their closest analogue.
184587497f52SBarry 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).
184687497f52SBarry 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.
184787497f52SBarry Smith .  `PETSC_PREFETCH_HINT_T1` - Fetch to level 2 and higher (not L1).
184887497f52SBarry Smith -  `PETSC_PREFETCH_HINT_T2` - Fetch to high-level cache only.  (On many systems, T0 and T1 are equivalent.)
1849d382aafbSBarry Smith 
1850d382aafbSBarry Smith    This function does nothing on architectures that do not support prefetch and never errors (even if passed an invalid
1851d382aafbSBarry Smith    address).
1852d382aafbSBarry Smith 
1853d382aafbSBarry Smith M*/
18549371c9d4SSatish Balay #define PetscPrefetchBlock(a, n, rw, t) \
18559371c9d4SSatish Balay   do { \
1856d382aafbSBarry Smith     const char *_p = (const char *)(a), *_end = (const char *)((a) + (n)); \
1857d382aafbSBarry Smith     for (; _p < _end; _p += PETSC_LEVEL1_DCACHE_LINESIZE) PETSC_Prefetch(_p, (rw), (t)); \
1858d382aafbSBarry Smith   } while (0)
1859d382aafbSBarry Smith 
1860d382aafbSBarry Smith /*
1861d382aafbSBarry Smith       Determine if some of the kernel computation routines use
1862d382aafbSBarry Smith    Fortran (rather than C) for the numerical calculations. On some machines
1863d382aafbSBarry Smith    and compilers (like complex numbers) the Fortran version of the routines
1864d382aafbSBarry Smith    is faster than the C/C++ versions. The flag --with-fortran-kernels
1865e2e64c6bSBarry Smith    should be used with ./configure to turn these on.
1866d382aafbSBarry Smith */
1867d382aafbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNELS)
1868d382aafbSBarry Smith 
1869d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTCRL)
1870d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MULTCRL
1871d382aafbSBarry Smith #endif
1872d382aafbSBarry Smith 
18735a11e1b2SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJPERM)
18745a11e1b2SBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MULTAIJPERM
1875d382aafbSBarry Smith #endif
1876d382aafbSBarry Smith 
1877d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1878d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MULTAIJ
1879d382aafbSBarry Smith #endif
1880d382aafbSBarry Smith 
1881d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1882d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ
1883d382aafbSBarry Smith #endif
1884d382aafbSBarry Smith 
1885d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_NORM)
1886d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_NORM
1887d382aafbSBarry Smith #endif
1888d382aafbSBarry Smith 
1889d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MAXPY)
1890d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MAXPY
1891d382aafbSBarry Smith #endif
1892d382aafbSBarry Smith 
1893d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
1894d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ
1895d382aafbSBarry Smith #endif
1896d382aafbSBarry Smith 
1897d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
1898d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_RELAXAIJ
1899d382aafbSBarry Smith #endif
1900d382aafbSBarry Smith 
1901d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ)
1902d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ
1903d382aafbSBarry Smith #endif
1904d382aafbSBarry Smith 
1905d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1906d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ
1907d382aafbSBarry Smith #endif
1908d382aafbSBarry Smith 
1909d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MDOT)
1910d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MDOT
1911d382aafbSBarry Smith #endif
1912d382aafbSBarry Smith 
1913d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_XTIMESY)
1914d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_XTIMESY
1915d382aafbSBarry Smith #endif
1916d382aafbSBarry Smith 
1917d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_AYPX)
1918d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_AYPX
1919d382aafbSBarry Smith #endif
1920d382aafbSBarry Smith 
1921d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_WAXPY)
1922d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_WAXPY
1923d382aafbSBarry Smith #endif
1924d382aafbSBarry Smith 
1925d382aafbSBarry Smith #endif
1926d382aafbSBarry Smith 
1927d382aafbSBarry Smith /*
1928d382aafbSBarry Smith     Macros for indicating code that should be compiled with a C interface,
1929d382aafbSBarry Smith    rather than a C++ interface. Any routines that are dynamically loaded
1930d382aafbSBarry Smith    (such as the PCCreate_XXX() routines) must be wrapped so that the name
1931d382aafbSBarry Smith    mangler does not change the functions symbol name. This just hides the
1932d382aafbSBarry Smith    ugly extern "C" {} wrappers.
1933d382aafbSBarry Smith */
1934d382aafbSBarry Smith #if defined(__cplusplus)
1935d382aafbSBarry Smith #define EXTERN_C_BEGIN extern "C" {
1936d382aafbSBarry Smith #define EXTERN_C_END   }
1937d382aafbSBarry Smith #else
1938d382aafbSBarry Smith #define EXTERN_C_BEGIN
1939d382aafbSBarry Smith #define EXTERN_C_END
1940d382aafbSBarry Smith #endif
1941d382aafbSBarry Smith 
1942d382aafbSBarry Smith /* --------------------------------------------------------------------*/
1943d382aafbSBarry Smith 
1944d382aafbSBarry Smith /*MC
1945d382aafbSBarry Smith     MPI_Comm - the basic object used by MPI to determine which processes are involved in a
1946d382aafbSBarry Smith         communication
1947d382aafbSBarry Smith 
1948d382aafbSBarry Smith    Level: beginner
1949d382aafbSBarry Smith 
1950d382aafbSBarry Smith    Note: This manual page is a place-holder because MPICH does not have a manual page for MPI_Comm
1951d382aafbSBarry Smith 
1952db781477SPatrick Sanan .seealso: `PETSC_COMM_WORLD`, `PETSC_COMM_SELF`
1953d382aafbSBarry Smith M*/
1954d382aafbSBarry Smith 
1955d382aafbSBarry Smith #if defined(PETSC_HAVE_MPIIO)
1956014dd563SJed Brown PETSC_EXTERN PetscErrorCode MPIU_File_write_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *);
1957014dd563SJed Brown PETSC_EXTERN PetscErrorCode MPIU_File_read_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *);
1958c4e82887SLisandro Dalcin PETSC_EXTERN PetscErrorCode MPIU_File_write_at(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *);
1959c4e82887SLisandro Dalcin PETSC_EXTERN PetscErrorCode MPIU_File_read_at(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *);
1960c4e82887SLisandro Dalcin PETSC_EXTERN PetscErrorCode MPIU_File_write_at_all(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *);
1961c4e82887SLisandro Dalcin PETSC_EXTERN PetscErrorCode MPIU_File_read_at_all(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *);
1962d382aafbSBarry Smith #endif
1963d382aafbSBarry Smith 
1964d382aafbSBarry Smith /* the following petsc_static_inline require petscerror.h */
1965d382aafbSBarry Smith 
1966d382aafbSBarry Smith /* Limit MPI to 32-bits */
1967d382aafbSBarry Smith #define PETSC_MPI_INT_MAX 2147483647
1968d382aafbSBarry Smith #define PETSC_MPI_INT_MIN -2147483647
1969d382aafbSBarry Smith /* Limit BLAS to 32-bits */
1970d382aafbSBarry Smith #define PETSC_BLAS_INT_MAX   2147483647
1971d382aafbSBarry Smith #define PETSC_BLAS_INT_MIN   -2147483647
1972eee0c0a6SToby Isaac #define PETSC_CUBLAS_INT_MAX 2147483647
1973d382aafbSBarry Smith 
197461b0d812SBarry Smith /*@C
197587497f52SBarry Smith     PetscIntCast - casts a `PetscInt64` (which is 64 bits in size) to a `PetscInt` (which may be 32 bits in size), generates an
197687497f52SBarry Smith          error if the `PetscInt` is not large enough to hold the number.
1977c73702f5SBarry Smith 
1978c73702f5SBarry Smith    Not Collective
1979c73702f5SBarry Smith 
1980c73702f5SBarry Smith    Input Parameter:
198187497f52SBarry Smith .     a - the `PetscInt64` value
1982c73702f5SBarry Smith 
1983c73702f5SBarry Smith    Output Parameter:
198487497f52SBarry Smith .     b - the resulting `PetscInt` value
1985c73702f5SBarry Smith 
1986c73702f5SBarry Smith    Level: advanced
1987c73702f5SBarry Smith 
198887497f52SBarry Smith    Notes:
198987497f52SBarry 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
1990c73702f5SBarry Smith 
1991c73702f5SBarry Smith    Not available from Fortran
1992c73702f5SBarry Smith 
1993db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscIntMultError()`, `PetscIntSumError()`
1994c73702f5SBarry Smith @*/
19959371c9d4SSatish Balay static inline PetscErrorCode PetscIntCast(PetscInt64 a, PetscInt *b) {
1996c73702f5SBarry Smith   PetscFunctionBegin;
1997c73702f5SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
19983ca90d2dSJacob Faibussowitsch   if (a > PETSC_MAX_INT) {
19993ca90d2dSJacob Faibussowitsch     *b = 0;
200098921bdaSJacob Faibussowitsch     SETERRQ(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);
20013ca90d2dSJacob Faibussowitsch   }
2002c73702f5SBarry Smith #endif
2003c73702f5SBarry Smith   *b = (PetscInt)(a);
2004c73702f5SBarry Smith   PetscFunctionReturn(0);
2005c73702f5SBarry Smith }
2006c73702f5SBarry Smith 
2007c73702f5SBarry Smith /*@C
200887497f52SBarry Smith     PetscCountCast - casts a `PetscCount` to a `PetscInt` (which may be 32 bits in size), generates an
200987497f52SBarry Smith          error if the `PetscInt` is not large enough to hold the number.
2010981bb840SJunchao Zhang 
2011981bb840SJunchao Zhang    Not Collective
2012981bb840SJunchao Zhang 
2013981bb840SJunchao Zhang    Input Parameter:
201487497f52SBarry Smith .     a - the `PetscCount` value
2015981bb840SJunchao Zhang 
2016981bb840SJunchao Zhang    Output Parameter:
201787497f52SBarry Smith .     b - the resulting `PetscInt` value
2018981bb840SJunchao Zhang 
2019981bb840SJunchao Zhang    Level: advanced
2020981bb840SJunchao Zhang 
2021981bb840SJunchao Zhang    Notes:
2022981bb840SJunchao 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
2023981bb840SJunchao Zhang 
2024981bb840SJunchao Zhang    Not available from Fortran
2025981bb840SJunchao Zhang 
2026db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscIntMultError()`, `PetscIntSumError()`, `PetscIntCast()`
2027981bb840SJunchao Zhang @*/
20289371c9d4SSatish Balay static inline PetscErrorCode PetscCountCast(PetscCount a, PetscInt *b) {
2029981bb840SJunchao Zhang   PetscFunctionBegin;
2030981bb840SJunchao Zhang   if (sizeof(PetscCount) > sizeof(PetscInt) && a > PETSC_MAX_INT) {
2031981bb840SJunchao Zhang     *b = 0;
2032981bb840SJunchao Zhang     SETERRQ(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);
2033981bb840SJunchao Zhang   }
2034981bb840SJunchao Zhang   *b = (PetscInt)(a);
2035981bb840SJunchao Zhang   PetscFunctionReturn(0);
2036981bb840SJunchao Zhang }
2037981bb840SJunchao Zhang 
2038981bb840SJunchao Zhang /*@C
203987497f52SBarry Smith     PetscBLASIntCast - casts a `PetscInt` (which may be 64 bits in size) to a `PetscBLASInt` (which may be 32 bits in size), generates an
204087497f52SBarry Smith          error if the `PetscBLASInt` is not large enough to hold the number.
204161b0d812SBarry Smith 
204261b0d812SBarry Smith    Not Collective
204361b0d812SBarry Smith 
204461b0d812SBarry Smith    Input Parameter:
204587497f52SBarry Smith .     a - the `PetscInt` value
204661b0d812SBarry Smith 
204761b0d812SBarry Smith    Output Parameter:
204887497f52SBarry Smith .     b - the resulting `PetscBLASInt` value
204961b0d812SBarry Smith 
205061b0d812SBarry Smith    Level: advanced
205161b0d812SBarry Smith 
20520bc7d38cSBarry Smith    Notes:
20531fd49c25SBarry Smith    Not available from Fortran
205487497f52SBarry Smith 
2055f0b7f91aSBarry Smith    Errors if the integer is negative since PETSc calls to BLAS/LAPACK never need to cast negative integer inputs
20561fd49c25SBarry Smith 
2057db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscIntCast()`, `PetscCountCast()`
205861b0d812SBarry Smith @*/
20599371c9d4SSatish Balay static inline PetscErrorCode PetscBLASIntCast(PetscInt a, PetscBLASInt *b) {
2060c5df96a5SBarry Smith   PetscFunctionBegin;
206154c59aa7SJacob Faibussowitsch   *b = 0;
206254c59aa7SJacob Faibussowitsch   if (PetscDefined(USE_64BIT_INDICES) && !PetscDefined(HAVE_64BIT_BLAS_INDICES)) {
206354c59aa7SJacob 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);
206454c59aa7SJacob Faibussowitsch   }
206554c59aa7SJacob Faibussowitsch   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer to BLAS/LAPACK routine");
2066c73702f5SBarry Smith   *b = (PetscBLASInt)(a);
2067c5df96a5SBarry Smith   PetscFunctionReturn(0);
2068c5df96a5SBarry Smith }
2069c5df96a5SBarry Smith 
207061b0d812SBarry Smith /*@C
207187497f52SBarry Smith     PetscCuBLASIntCast - like `PetscBLASIntCast()`, but for `PetscCuBLASInt`.
2072eee0c0a6SToby Isaac 
2073eee0c0a6SToby Isaac    Not Collective
2074eee0c0a6SToby Isaac 
2075eee0c0a6SToby Isaac    Input Parameter:
207687497f52SBarry Smith .     a - the `PetscInt` value
2077eee0c0a6SToby Isaac 
2078eee0c0a6SToby Isaac    Output Parameter:
207987497f52SBarry Smith .     b - the resulting `PetscCuBLASInt` value
2080eee0c0a6SToby Isaac 
2081eee0c0a6SToby Isaac    Level: advanced
2082eee0c0a6SToby Isaac 
2083eee0c0a6SToby Isaac    Notes:
2084eee0c0a6SToby Isaac       Errors if the integer is negative since PETSc calls to cuBLAS and friends never need to cast negative integer inputs
2085eee0c0a6SToby Isaac 
2086db781477SPatrick Sanan .seealso: `PetscCuBLASInt`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscMPIIntCast()`, `PetscIntCast()`
2087eee0c0a6SToby Isaac @*/
20889371c9d4SSatish Balay static inline PetscErrorCode PetscCuBLASIntCast(PetscInt a, PetscCuBLASInt *b) {
2089eee0c0a6SToby Isaac   PetscFunctionBegin;
209054c59aa7SJacob Faibussowitsch   *b = 0;
209154c59aa7SJacob Faibussowitsch   if (PetscDefined(USE_64BIT_INDICES)) 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);
209254c59aa7SJacob Faibussowitsch   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer to cuBLAS routine");
2093eee0c0a6SToby Isaac   *b = (PetscCuBLASInt)(a);
2094eee0c0a6SToby Isaac   PetscFunctionReturn(0);
2095eee0c0a6SToby Isaac }
2096eee0c0a6SToby Isaac 
2097eee0c0a6SToby Isaac /*@C
209887497f52SBarry Smith     PetscMPIIntCast - casts a `PetscInt` (which may be 64 bits in size) to a `PetscMPIInt` (which may be 32 bits in size), generates an
209987497f52SBarry Smith          error if the `PetscMPIInt` is not large enough to hold the number.
210061b0d812SBarry Smith 
210161b0d812SBarry Smith    Not Collective
210261b0d812SBarry Smith 
210361b0d812SBarry Smith    Input Parameter:
210487497f52SBarry Smith .     a - the `PetscInt` value
210561b0d812SBarry Smith 
210661b0d812SBarry Smith    Output Parameter:
210787497f52SBarry Smith .     b - the resulting `PetscMPIInt` value
210861b0d812SBarry Smith 
210961b0d812SBarry Smith    Level: advanced
211061b0d812SBarry Smith 
21111fd49c25SBarry Smith    Not available from Fortran
21121fd49c25SBarry Smith 
2113db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscIntCast()`
211461b0d812SBarry Smith @*/
21159371c9d4SSatish Balay static inline PetscErrorCode PetscMPIIntCast(PetscInt a, PetscMPIInt *b) {
21164dc2109aSBarry Smith   PetscFunctionBegin;
211754c59aa7SJacob Faibussowitsch   *b = 0;
211854c59aa7SJacob Faibussowitsch   if (PetscDefined(USE_64BIT_INDICES)) PetscCheck(a <= PETSC_MPI_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt_FMT " is too big for MPI buffer length. We currently only support 32 bit integers", a);
2119c73702f5SBarry Smith   *b = (PetscMPIInt)(a);
21204dc2109aSBarry Smith   PetscFunctionReturn(0);
21214dc2109aSBarry Smith }
21224dc2109aSBarry Smith 
2123bafee8b4SSatish Balay #define PetscInt64Mult(a, b) ((PetscInt64)(a)) * ((PetscInt64)(b))
21242f18eb33SBarry Smith 
21252f18eb33SBarry Smith /*@C
21262f18eb33SBarry Smith 
212787497f52SBarry Smith    PetscRealIntMultTruncate - Computes the product of a positive `PetscReal` and a positive `PetscInt` and truncates the value to slightly less than the maximal possible value
21282f18eb33SBarry Smith 
21292f18eb33SBarry Smith    Not Collective
21302f18eb33SBarry Smith 
2131d8d19677SJose E. Roman    Input Parameters:
213287497f52SBarry Smith +     a - the `PetscReal` value
21332f18eb33SBarry Smith -     b - the second value
21342f18eb33SBarry Smith 
2135390e1bf2SBarry Smith    Returns:
213687497f52SBarry Smith       the result as a `PetscInt` value
21372f18eb33SBarry Smith 
213887497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`
213987497f52SBarry Smith    Use `PetscIntMultTruncate()` to compute the product of two positive `PetscInt` and truncate to fit a `PetscInt`
214087497f52SBarry 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`
21412f18eb33SBarry Smith 
2142e28ce29aSPatrick Sanan    Developers Note:
214387497f52SBarry Smith    We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but requires many more checks.
21442f18eb33SBarry Smith 
21452f18eb33SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
21462f18eb33SBarry Smith 
21471fd49c25SBarry Smith    Not available from Fortran
21481fd49c25SBarry Smith 
21492f18eb33SBarry Smith    Level: advanced
21502f18eb33SBarry Smith 
2151db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`
21522f18eb33SBarry Smith @*/
21539371c9d4SSatish Balay static inline PetscInt PetscRealIntMultTruncate(PetscReal a, PetscInt b) {
21545f80ce2aSJacob Faibussowitsch   PetscInt64 r = (PetscInt64)(a * (PetscReal)b);
21552f18eb33SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
21562f18eb33SBarry Smith   return (PetscInt)r;
21572f18eb33SBarry Smith }
21582f18eb33SBarry Smith 
21592f18eb33SBarry Smith /*@C
21602f18eb33SBarry Smith 
216187497f52SBarry Smith    PetscIntMultTruncate - Computes the product of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value
21622f18eb33SBarry Smith 
21632f18eb33SBarry Smith    Not Collective
21642f18eb33SBarry Smith 
2165d8d19677SJose E. Roman    Input Parameters:
21662f18eb33SBarry Smith +     a - the PetscInt value
21672f18eb33SBarry Smith -     b - the second value
21682f18eb33SBarry Smith 
2169390e1bf2SBarry Smith    Returns:
217087497f52SBarry Smith       the result as a `PetscInt` value
21712f18eb33SBarry Smith 
217287497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`
217387497f52SBarry Smith    Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`
217487497f52SBarry 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`
21752f18eb33SBarry Smith 
21761fd49c25SBarry Smith    Not available from Fortran
21771fd49c25SBarry Smith 
217887497f52SBarry Smith    Developers Note:
217987497f52SBarry Smith    We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but requires many more checks.
21802f18eb33SBarry Smith 
21812f18eb33SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
21822f18eb33SBarry Smith 
21832f18eb33SBarry Smith    Level: advanced
21842f18eb33SBarry Smith 
2185db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`
21862f18eb33SBarry Smith @*/
21879371c9d4SSatish Balay static inline PetscInt PetscIntMultTruncate(PetscInt a, PetscInt b) {
21885f80ce2aSJacob Faibussowitsch   PetscInt64 r = PetscInt64Mult(a, b);
21892f18eb33SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
21902f18eb33SBarry Smith   return (PetscInt)r;
21912f18eb33SBarry Smith }
21922f18eb33SBarry Smith 
2193f91af8c7SBarry Smith /*@C
2194f91af8c7SBarry Smith 
219587497f52SBarry Smith    PetscIntSumTruncate - Computes the sum of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value
2196f91af8c7SBarry Smith 
2197f91af8c7SBarry Smith    Not Collective
2198f91af8c7SBarry Smith 
2199d8d19677SJose E. Roman    Input Parameters:
220087497f52SBarry Smith +     a - the `PetscInt` value
2201f91af8c7SBarry Smith -     b - the second value
2202f91af8c7SBarry Smith 
2203390e1bf2SBarry Smith    Returns:
220487497f52SBarry Smith      the result as a `PetscInt` value
2205f91af8c7SBarry Smith 
220687497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`
220787497f52SBarry Smith    Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`
220887497f52SBarry 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`
2209f91af8c7SBarry Smith 
2210f91af8c7SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
2211f91af8c7SBarry Smith 
2212f5f57ec0SBarry Smith    Not available from Fortran
2213f5f57ec0SBarry Smith 
2214f91af8c7SBarry Smith    Level: advanced
2215f91af8c7SBarry Smith 
2216db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
2217f91af8c7SBarry Smith @*/
22189371c9d4SSatish Balay static inline PetscInt PetscIntSumTruncate(PetscInt a, PetscInt b) {
22195f80ce2aSJacob Faibussowitsch   PetscInt64 r = ((PetscInt64)a) + ((PetscInt64)b);
2220f91af8c7SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
2221f91af8c7SBarry Smith   return (PetscInt)r;
2222f91af8c7SBarry Smith }
2223f91af8c7SBarry Smith 
22242f18eb33SBarry Smith /*@C
22252f18eb33SBarry Smith 
222687497f52SBarry Smith    PetscIntMultError - Computes the product of two positive `PetscInt` and generates an error with overflow.
22272f18eb33SBarry Smith 
22282f18eb33SBarry Smith    Not Collective
22292f18eb33SBarry Smith 
2230d8d19677SJose E. Roman    Input Parameters:
223187497f52SBarry Smith +     a - the `PetscInt` value
22322f18eb33SBarry Smith -     b - the second value
22332f18eb33SBarry Smith 
2234d8d19677SJose E. Roman    Output Parameter:
223587497f52SBarry 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
22362f18eb33SBarry Smith 
223787497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` and store in a `PetscInt64`
223887497f52SBarry Smith    Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`
22392f18eb33SBarry Smith 
2240f5f57ec0SBarry Smith    Not available from Fortran
2241f5f57ec0SBarry Smith 
224287497f52SBarry Smith    Developers Note:
224387497f52SBarry Smith    We currently assume that `PetscInt` addition does not overflow, this is obviously wrong but requires many more checks.
22442f18eb33SBarry Smith 
22452f18eb33SBarry Smith    Level: advanced
22462f18eb33SBarry Smith 
2247db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscIntMult64()`, `PetscIntSumError()`
22482f18eb33SBarry Smith @*/
22499371c9d4SSatish Balay static inline PetscErrorCode PetscIntMultError(PetscInt a, PetscInt b, PetscInt *result) {
2250bafee8b4SSatish Balay   PetscInt64 r;
22512f18eb33SBarry Smith 
22522f18eb33SBarry Smith   PetscFunctionBegin;
2253bafee8b4SSatish Balay   r = PetscInt64Mult(a, b);
22542f18eb33SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
22552c71b3e2SJacob Faibussowitsch   PetscCheck(r <= PETSC_MAX_INT, PETSC_COMM_SELF, PETSC_ERR_SUP, "Product of two integers %d %d 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);
22562f18eb33SBarry Smith #endif
2257f91af8c7SBarry Smith   if (result) *result = (PetscInt)r;
2258f91af8c7SBarry Smith   PetscFunctionReturn(0);
2259f91af8c7SBarry Smith }
2260f91af8c7SBarry Smith 
2261f91af8c7SBarry Smith /*@C
2262f91af8c7SBarry Smith 
226387497f52SBarry Smith    PetscIntSumError - Computes the sum of two positive `PetscInt` and generates an error with overflow.
2264f91af8c7SBarry Smith 
2265f91af8c7SBarry Smith    Not Collective
2266f91af8c7SBarry Smith 
2267d8d19677SJose E. Roman    Input Parameters:
226887497f52SBarry Smith +     a - the `PetscInt` value
2269f91af8c7SBarry Smith -     b - the second value
2270f91af8c7SBarry Smith 
2271d8d19677SJose E. Roman    Output Parameter:
227287497f52SBarry 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
2273f91af8c7SBarry Smith 
227487497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two 32 bit PetscInt and store in a `PetscInt64`
227587497f52SBarry Smith    Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`
2276f91af8c7SBarry Smith 
2277f5f57ec0SBarry Smith    Not available from Fortran
2278f5f57ec0SBarry Smith 
2279f91af8c7SBarry Smith    Level: advanced
2280f91af8c7SBarry Smith 
2281db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
2282f91af8c7SBarry Smith @*/
22839371c9d4SSatish Balay static inline PetscErrorCode PetscIntSumError(PetscInt a, PetscInt b, PetscInt *result) {
2284bafee8b4SSatish Balay   PetscInt64 r;
2285f91af8c7SBarry Smith 
2286f91af8c7SBarry Smith   PetscFunctionBegin;
2287bafee8b4SSatish Balay   r = ((PetscInt64)a) + ((PetscInt64)b);
2288f91af8c7SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
22892c71b3e2SJacob Faibussowitsch   PetscCheck(r <= PETSC_MAX_INT, PETSC_COMM_SELF, PETSC_ERR_SUP, "Sum of two integers %d %d 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);
2290f91af8c7SBarry Smith #endif
2291f91af8c7SBarry Smith   if (result) *result = (PetscInt)r;
22922f18eb33SBarry Smith   PetscFunctionReturn(0);
22932f18eb33SBarry Smith }
2294c5df96a5SBarry Smith 
2295d382aafbSBarry Smith /*
22962981ebdbSBarry Smith      The IBM include files define hz, here we hide it so that it may be used as a regular user variable.
2297d382aafbSBarry Smith */
2298d382aafbSBarry Smith #if defined(hz)
2299d382aafbSBarry Smith #undef hz
2300d382aafbSBarry Smith #endif
2301d382aafbSBarry Smith 
230215a5570dSLisandro Dalcin #include <limits.h>
230315a5570dSLisandro Dalcin 
230415a5570dSLisandro Dalcin /* The number of bits in a byte */
230515a5570dSLisandro Dalcin 
230615a5570dSLisandro Dalcin #define PETSC_BITS_PER_BYTE CHAR_BIT
230715a5570dSLisandro Dalcin 
2308d382aafbSBarry Smith #if defined(PETSC_HAVE_SYS_TYPES_H)
2309d382aafbSBarry Smith #include <sys/types.h>
2310d382aafbSBarry Smith #endif
2311d382aafbSBarry Smith 
2312d382aafbSBarry Smith /*MC
2313d382aafbSBarry Smith 
231466d79e26SBarry 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++
231566d79e26SBarry Smith                     and Fortran compilers when petscsys.h is included.
231666d79e26SBarry Smith 
231766d79e26SBarry Smith     The current PETSc version and the API for accessing it are defined in petscversion.h
231866d79e26SBarry Smith 
231966d79e26SBarry 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)
232066d79e26SBarry Smith 
232166d79e26SBarry 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
232266d79e26SBarry Smith     where only a change in the major version number (x) indicates a change in the API.
232366d79e26SBarry Smith 
232466d79e26SBarry 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
232566d79e26SBarry Smith 
232666d79e26SBarry 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),
232766d79e26SBarry 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
232866d79e26SBarry Smith     version number (x.y.z).
232966d79e26SBarry Smith 
233087497f52SBarry Smith     `PETSC_RELEASE_DATE` is the date the x.y version was released (i.e. the version before any patch releases)
233166d79e26SBarry Smith 
233287497f52SBarry Smith     `PETSC_VERSION_DATE` is the date the x.y.z version was released
233366d79e26SBarry Smith 
233487497f52SBarry Smith     `PETSC_VERSION_GIT` is the last git commit to the repository given in the form vx.y.z-wwwww
233566d79e26SBarry Smith 
233687497f52SBarry Smith     `PETSC_VERSION_DATE_GIT` is the date of the last git commit to the repository
233766d79e26SBarry Smith 
233887497f52SBarry Smith     `PETSC_VERSION_()` is deprecated and will eventually be removed.
233966d79e26SBarry Smith 
2340cbc12506SBarry Smith     Level: intermediate
234166d79e26SBarry Smith 
234266d79e26SBarry Smith M*/
234366d79e26SBarry Smith 
2344014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArchType(char[], size_t);
2345014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetHostName(char[], size_t);
2346014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetUserName(char[], size_t);
2347014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetProgramName(char[], size_t);
2348014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetProgramName(const char[]);
2349014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetDate(char[], size_t);
235027710113SBarry Smith PETSC_EXTERN PetscErrorCode PetscGetVersion(char[], size_t);
23515f309d01SBarry Smith PETSC_EXTERN PetscErrorCode PetscGetVersionNumber(PetscInt *, PetscInt *, PetscInt *, PetscInt *);
2352d382aafbSBarry Smith 
23536a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedInt(PetscInt, const PetscInt[], PetscBool *);
23546a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedMPIInt(PetscInt, const PetscMPIInt[], PetscBool *);
23556a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedReal(PetscInt, const PetscReal[], PetscBool *);
2356014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortInt(PetscInt, PetscInt[]);
2357ce605777SToby Isaac PETSC_EXTERN PetscErrorCode PetscSortReverseInt(PetscInt, PetscInt[]);
235822ab5688SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscSortedRemoveDupsInt(PetscInt *, PetscInt[]);
2359b6a18d21SVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedCheckDupsInt(PetscInt, const PetscInt[], PetscBool *);
2360014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsInt(PetscInt *, PetscInt[]);
2361f1cab4e1SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscCheckDupsInt(PetscInt, const PetscInt[], PetscBool *);
236260e03357SMatthew G Knepley PETSC_EXTERN PetscErrorCode PetscFindInt(PetscInt, PetscInt, const PetscInt[], PetscInt *);
2363d2aeb606SJed Brown PETSC_EXTERN PetscErrorCode PetscFindMPIInt(PetscMPIInt, PetscInt, const PetscMPIInt[], PetscInt *);
2364014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithPermutation(PetscInt, const PetscInt[], PetscInt[]);
2365014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortStrWithPermutation(PetscInt, const char *[], PetscInt[]);
2366014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithArray(PetscInt, PetscInt[], PetscInt[]);
2367981bb840SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortIntWithCountArray(PetscCount, PetscInt[], PetscCount[]);
23686c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscSortIntWithArrayPair(PetscInt, PetscInt[], PetscInt[], PetscInt[]);
2369981bb840SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortIntWithIntCountArrayPair(PetscCount, PetscInt[], PetscInt[], PetscCount[]);
237017d7d925SStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortMPIInt(PetscInt, PetscMPIInt[]);
237117d7d925SStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsMPIInt(PetscInt *, PetscMPIInt[]);
2372014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithArray(PetscMPIInt, PetscMPIInt[], PetscMPIInt[]);
23735569a785SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithIntArray(PetscMPIInt, PetscMPIInt[], PetscInt[]);
2374014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithScalarArray(PetscInt, PetscInt[], PetscScalar[]);
237517df18f2SToby Isaac PETSC_EXTERN PetscErrorCode PetscSortIntWithDataArray(PetscInt, PetscInt[], void *, size_t, void *);
2376014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortReal(PetscInt, PetscReal[]);
237739f41f7fSStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortRealWithArrayInt(PetscInt, PetscReal[], PetscInt[]);
2378014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortRealWithPermutation(PetscInt, const PetscReal[], PetscInt[]);
2379745b41b2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsReal(PetscInt *, PetscReal[]);
238039f41f7fSStefano Zampini PETSC_EXTERN PetscErrorCode PetscFindReal(PetscReal, PetscInt, const PetscReal[], PetscReal, PetscInt *);
2381014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortSplit(PetscInt, PetscInt, PetscScalar[], PetscInt[]);
2382014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortSplitReal(PetscInt, PetscInt, PetscReal[], PetscInt[]);
2383014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscProcessTree(PetscInt, const PetscBool[], const PetscInt[], PetscInt *, PetscInt **, PetscInt **, PetscInt **, PetscInt **);
23846c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscMergeIntArrayPair(PetscInt, const PetscInt[], const PetscInt[], PetscInt, const PetscInt[], const PetscInt[], PetscInt *, PetscInt **, PetscInt **);
23856c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscMergeIntArray(PetscInt, const PetscInt[], PetscInt, const PetscInt[], PetscInt *, PetscInt **);
2386e498c390SJed Brown PETSC_EXTERN PetscErrorCode PetscMergeMPIIntArray(PetscInt, const PetscMPIInt[], PetscInt, const PetscMPIInt[], PetscInt *, PetscMPIInt **);
2387ce605777SToby Isaac PETSC_EXTERN PetscErrorCode PetscParallelSortedInt(MPI_Comm, PetscInt, const PetscInt[], PetscBool *);
2388ce605777SToby Isaac 
23894d3610e3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscTimSort(PetscInt, void *, size_t, int (*)(const void *, const void *, void *), void *);
2390676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrdered(PetscInt, PetscInt[]);
2391676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrdered(PetscInt, PetscMPIInt[]);
2392676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrdered(PetscInt, PetscReal[]);
23934d3610e3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscTimSortWithArray(PetscInt, void *, size_t, void *, size_t, int (*)(const void *, const void *, void *), void *);
2394676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrderedWithArray(PetscInt, PetscInt[], PetscInt[]);
2395676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrderedWithArray(PetscInt, PetscMPIInt[], PetscMPIInt[]);
2396676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrderedWithArrayInt(PetscInt, PetscReal[], PetscInt[]);
2397676f2a66SJacob Faibussowitsch 
2398014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDisplay(void);
2399014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetDisplay(char[], size_t);
2400d382aafbSBarry Smith 
240176bdecfbSBarry Smith /*J
2402d382aafbSBarry Smith     PetscRandomType - String with the name of a PETSc randomizer
2403d382aafbSBarry Smith 
2404d382aafbSBarry Smith    Level: beginner
2405d382aafbSBarry Smith 
2406e28ce29aSPatrick Sanan    Notes:
240787497f52SBarry Smith    To use `PETSCSPRNG` or `PETSCRANDOM123` you must have ./configure PETSc
240825ccb61fSToby Isaac    with the option --download-sprng or --download-random123
2409d382aafbSBarry Smith 
2410db781477SPatrick Sanan .seealso: `PetscRandomSetType()`, `PetscRandom`, `PetscRandomCreate()`
241176bdecfbSBarry Smith J*/
241219fd82e9SBarry Smith typedef const char *PetscRandomType;
2413d382aafbSBarry Smith #define PETSCRAND      "rand"
2414d382aafbSBarry Smith #define PETSCRAND48    "rand48"
2415d382aafbSBarry Smith #define PETSCSPRNG     "sprng"
2416c5e4d11fSDmitry Karpeev #define PETSCRANDER48  "rander48"
241725ccb61fSToby Isaac #define PETSCRANDOM123 "random123"
2418808ba619SStefano Zampini #define PETSCCURAND    "curand"
2419d382aafbSBarry Smith 
2420d382aafbSBarry Smith /* Logging support */
2421014dd563SJed Brown PETSC_EXTERN PetscClassId PETSC_RANDOM_CLASSID;
2422d382aafbSBarry Smith 
2423607a6623SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomInitializePackage(void);
2424d382aafbSBarry Smith 
2425d382aafbSBarry Smith /* Dynamic creation and loading functions */
2426140e18c1SBarry Smith PETSC_EXTERN PetscFunctionList PetscRandomList;
2427d382aafbSBarry Smith 
2428bdf89e91SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomRegister(const char[], PetscErrorCode (*)(PetscRandom));
242919fd82e9SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomSetType(PetscRandom, PetscRandomType);
2430014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetFromOptions(PetscRandom);
243119fd82e9SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomGetType(PetscRandom, PetscRandomType *);
2432fe2efc57SMark PETSC_EXTERN PetscErrorCode PetscRandomViewFromOptions(PetscRandom, PetscObject, const char[]);
2433014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomView(PetscRandom, PetscViewer);
2434d382aafbSBarry Smith 
2435014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomCreate(MPI_Comm, PetscRandom *);
2436014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetValue(PetscRandom, PetscScalar *);
2437014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetValueReal(PetscRandom, PetscReal *);
2438808ba619SStefano Zampini PETSC_EXTERN PetscErrorCode PetscRandomGetValues(PetscRandom, PetscInt, PetscScalar *);
2439808ba619SStefano Zampini PETSC_EXTERN PetscErrorCode PetscRandomGetValuesReal(PetscRandom, PetscInt, PetscReal *);
2440014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetInterval(PetscRandom, PetscScalar *, PetscScalar *);
2441014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetInterval(PetscRandom, PetscScalar, PetscScalar);
2442014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetSeed(PetscRandom, unsigned long);
2443014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetSeed(PetscRandom, unsigned long *);
2444014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSeed(PetscRandom);
2445014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomDestroy(PetscRandom *);
2446d382aafbSBarry Smith 
2447014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetFullPath(const char[], char[], size_t);
2448014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetRelativePath(const char[], char[], size_t);
2449014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetWorkingDirectory(char[], size_t);
2450014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetRealPath(const char[], char[]);
2451014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetHomeDirectory(char[], size_t);
2452014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTestFile(const char[], char, PetscBool *);
2453014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTestDirectory(const char[], char, PetscBool *);
245497540f07SSatish Balay PETSC_EXTERN PetscErrorCode PetscMkdir(const char[]);
24558a10d460SHong Zhang PETSC_EXTERN PetscErrorCode PetscMkdtemp(char[]);
245697540f07SSatish Balay PETSC_EXTERN PetscErrorCode PetscRMTree(const char[]);
2457d382aafbSBarry Smith 
24589371c9d4SSatish Balay static inline PetscBool PetscBinaryBigEndian(void) {
24599371c9d4SSatish Balay   long _petsc_v = 1;
24609371c9d4SSatish Balay   return ((char *)&_petsc_v)[0] ? PETSC_FALSE : PETSC_TRUE;
24619371c9d4SSatish Balay }
246230815ce0SLisandro Dalcin 
24639860990eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinaryRead(int, void *, PetscInt, PetscInt *, PetscDataType);
24649860990eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedRead(MPI_Comm, int, void *, PetscInt, PetscInt *, PetscDataType);
2465f253e43cSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinaryWrite(int, const void *, PetscInt, PetscDataType);
2466f253e43cSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedWrite(MPI_Comm, int, const void *, PetscInt, PetscDataType);
2467014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinaryOpen(const char[], PetscFileMode, int *);
2468014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinaryClose(int);
2469014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSharedTmp(MPI_Comm, PetscBool *);
2470014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSharedWorkingDirectory(MPI_Comm, PetscBool *);
2471014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetTmp(MPI_Comm, char[], size_t);
2472014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFileRetrieve(MPI_Comm, const char[], char[], size_t, PetscBool *);
2473014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLs(MPI_Comm, const char[], char[], size_t, PetscBool *);
2474c891a504SStefano Zampini #if defined(PETSC_USE_SOCKET_VIEWER)
2475e2fc02c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscOpenSocket(const char[], int, int *);
2476c891a504SStefano Zampini #endif
2477d382aafbSBarry Smith 
2478014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinarySeek(int, off_t, PetscBinarySeekType, off_t *);
2479014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedSeek(MPI_Comm, int, off_t, PetscBinarySeekType, off_t *);
2480014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscByteSwap(void *, PetscDataType, PetscInt);
2481d382aafbSBarry Smith 
2482014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebugTerminal(const char[]);
2483014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebugger(const char[], PetscBool);
2484014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDefaultDebugger(void);
2485014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebuggerFromString(const char *);
2486014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAttachDebugger(void);
2487014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStopForDebugger(void);
24882a2a2941SBarry Smith PETSC_EXTERN PetscErrorCode PetscWaitOnError(void);
2489d382aafbSBarry Smith 
2490014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherNumberOfMessages(MPI_Comm, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt *);
2491014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], PetscMPIInt **, PetscMPIInt **);
2492014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths2(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt **, PetscMPIInt **, PetscMPIInt **);
2493014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPostIrecvInt(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscInt ***, MPI_Request **);
2494014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPostIrecvScalar(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscScalar ***, MPI_Request **);
24959371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSided(MPI_Comm, PetscMPIInt, MPI_Datatype, PetscMPIInt, const PetscMPIInt *, const void *, PetscMPIInt *, PetscMPIInt **, void *) PetscAttrMPIPointerWithType(6, 3);
24969371c9d4SSatish Balay 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) PetscAttrMPIPointerWithType(6, 3);
24979371c9d4SSatish Balay 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) PetscAttrMPIPointerWithType(6, 3);
2498d382aafbSBarry Smith 
24996145cd65SJed Brown PETSC_EXTERN const char *const PetscBuildTwoSidedTypes[];
25006145cd65SJed Brown PETSC_EXTERN PetscErrorCode    PetscCommBuildTwoSidedSetType(MPI_Comm, PetscBuildTwoSidedType);
25016145cd65SJed Brown PETSC_EXTERN PetscErrorCode    PetscCommBuildTwoSidedGetType(MPI_Comm, PetscBuildTwoSidedType *);
25026145cd65SJed Brown 
2503014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSSEIsEnabled(MPI_Comm, PetscBool *, PetscBool *);
2504d382aafbSBarry Smith 
2505ce94432eSBarry Smith PETSC_EXTERN MPI_Comm PetscObjectComm(PetscObject);
2506ce94432eSBarry Smith 
250745487dadSJed Brown PETSC_EXTERN const char *const PetscSubcommTypes[];
250845487dadSJed Brown 
2509d382aafbSBarry Smith struct _n_PetscSubcomm {
2510d382aafbSBarry Smith   MPI_Comm         parent;    /* parent communicator */
2511d382aafbSBarry Smith   MPI_Comm         dupparent; /* duplicate parent communicator, under which the processors of this subcomm have contiguous rank */
2512306c2d5bSBarry Smith   MPI_Comm         child;     /* the sub-communicator */
251345487dadSJed Brown   PetscMPIInt      n;         /* num of subcommunicators under the parent communicator */
251445487dadSJed Brown   PetscMPIInt      color;     /* color of processors belong to this communicator */
2515708d824cSJed Brown   PetscMPIInt     *subsize;   /* size of subcommunicator[color] */
251645487dadSJed Brown   PetscSubcommType type;
2517e5acf8a4SHong Zhang   char            *subcommprefix;
2518d382aafbSBarry Smith };
2519d382aafbSBarry Smith 
25209371c9d4SSatish Balay static inline MPI_Comm PetscSubcommParent(PetscSubcomm scomm) {
25219371c9d4SSatish Balay   return scomm->parent;
25229371c9d4SSatish Balay }
25239371c9d4SSatish Balay static inline MPI_Comm PetscSubcommChild(PetscSubcomm scomm) {
25249371c9d4SSatish Balay   return scomm->child;
25259371c9d4SSatish Balay }
25269371c9d4SSatish Balay static inline MPI_Comm PetscSubcommContiguousParent(PetscSubcomm scomm) {
25279371c9d4SSatish Balay   return scomm->dupparent;
25289371c9d4SSatish Balay }
2529014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommCreate(MPI_Comm, PetscSubcomm *);
2530014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommDestroy(PetscSubcomm *);
2531014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommSetNumber(PetscSubcomm, PetscInt);
2532014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommSetType(PetscSubcomm, PetscSubcommType);
253365d9b8f1SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetTypeGeneral(PetscSubcomm, PetscMPIInt, PetscMPIInt);
2534053d1c95SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommView(PetscSubcomm, PetscViewer);
2535f68be91cSHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetFromOptions(PetscSubcomm);
2536e5acf8a4SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetOptionsPrefix(PetscSubcomm, const char[]);
2537a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetParent(PetscSubcomm, MPI_Comm *);
2538a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetContiguousParent(PetscSubcomm, MPI_Comm *);
2539a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetChild(PetscSubcomm, MPI_Comm *);
2540d382aafbSBarry Smith 
25419962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapCreate(PetscInt, PetscHeap *);
25429962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapAdd(PetscHeap, PetscInt, PetscInt);
25439962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapPop(PetscHeap, PetscInt *, PetscInt *);
25449962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapPeek(PetscHeap, PetscInt *, PetscInt *);
25459962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapStash(PetscHeap, PetscInt, PetscInt);
25469962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapUnstash(PetscHeap);
25479962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapDestroy(PetscHeap *);
25489962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapView(PetscHeap, PetscViewer);
25499962606eSBarry Smith 
25505e71baefSBarry Smith PETSC_EXTERN PetscErrorCode PetscProcessPlacementView(PetscViewer);
25515f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGet(MPI_Comm, PetscShmComm *);
25525f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGlobalToLocal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
25535f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommLocalToGlobal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
25545f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGetMpiShmComm(PetscShmComm, MPI_Comm *);
25555e71baefSBarry Smith 
2556a32e93adSJunchao Zhang /* routines to better support OpenMP multithreading needs of some PETSc third party libraries */
2557a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlCreate(MPI_Comm, PetscInt, PetscOmpCtrl *);
2558a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlGetOmpComms(PetscOmpCtrl, MPI_Comm *, MPI_Comm *, PetscBool *);
2559a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlDestroy(PetscOmpCtrl *);
2560a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlBarrier(PetscOmpCtrl);
2561a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterBegin(PetscOmpCtrl);
2562a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterEnd(PetscOmpCtrl);
2563a32e93adSJunchao Zhang 
256413e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferCreate(size_t, size_t, PetscSegBuffer *);
25650f453b92SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferDestroy(PetscSegBuffer *);
256613e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferGet(PetscSegBuffer, size_t, void *);
2567137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractAlloc(PetscSegBuffer, void *);
2568137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractTo(PetscSegBuffer, void *);
2569137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractInPlace(PetscSegBuffer, void *);
257013e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferGetSize(PetscSegBuffer, size_t *);
257113e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferUnuse(PetscSegBuffer, size_t);
25720f453b92SJed Brown 
2573471ecdacSJed Brown /* Type-safe wrapper to encourage use of PETSC_RESTRICT. Does not use PetscFunctionBegin because the error handling
2574471ecdacSJed Brown  * prevents the compiler from completely erasing the stub. This is called in inner loops so it has to be as fast as
2575471ecdacSJed Brown  * possible. */
25769371c9d4SSatish Balay static inline PetscErrorCode PetscSegBufferGetInts(PetscSegBuffer seg, size_t count, PetscInt *PETSC_RESTRICT *slot) {
25779371c9d4SSatish Balay   return PetscSegBufferGet(seg, count, (void **)slot);
25789371c9d4SSatish Balay }
2579d382aafbSBarry Smith 
25809de0f6ecSBarry Smith extern PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton;
25819de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *);
25829de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *);
25839de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted, const char *, const char *, PetscBool *);
25849de0f6ecSBarry Smith 
2585ace2fbaeSBarry Smith #include <stdarg.h>
2586ace2fbaeSBarry Smith PETSC_EXTERN PetscErrorCode PetscVSNPrintf(char *, size_t, const char[], size_t *, va_list);
2587ace2fbaeSBarry Smith PETSC_EXTERN                PetscErrorCode (*PetscVFPrintf)(FILE *, const char[], va_list);
2588ace2fbaeSBarry Smith 
2589268b0dfdSSatish Balay PETSC_EXTERN PetscSegBuffer PetscCitationsList;
2590fbfcfee5SBarry Smith 
25911f817a21SBarry Smith /*@C
25921f817a21SBarry Smith       PetscCitationsRegister - Register a bibtex item to obtain credit for an implemented algorithm used in the code.
25931f817a21SBarry Smith 
259487497f52SBarry Smith      Not Collective - only what is registered on rank 0 of `PETSC_COMM_WORLD` will be printed
25951f817a21SBarry Smith 
25961f817a21SBarry Smith      Input Parameters:
25971f817a21SBarry Smith +      cite - the bibtex item, formated to displayed on multiple lines nicely
259887497f52SBarry Smith -      set - a boolean variable initially set to `PETSC_FALSE`; this is used to insure only a single registration of the citation
25991f817a21SBarry Smith 
2600850545d0SSatish Balay    Level: intermediate
2601850545d0SSatish Balay 
2602f5f57ec0SBarry Smith    Not available from Fortran
2603f5f57ec0SBarry Smith 
26041f817a21SBarry Smith      Options Database:
2605063414e0SPatrick Sanan .     -citations [filename]   - print out the bibtex entries for the given computation
26061f817a21SBarry Smith @*/
26079371c9d4SSatish Balay static inline PetscErrorCode PetscCitationsRegister(const char cit[], PetscBool *set) {
2608dff31646SBarry Smith   size_t len;
2609dff31646SBarry Smith   char  *vstring;
2610dff31646SBarry Smith 
26111f817a21SBarry Smith   PetscFunctionBegin;
26128d3683fcSBarry Smith   if (set && *set) PetscFunctionReturn(0);
26139566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(cit, &len));
26149566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferGet(PetscCitationsList, len, &vstring));
26159566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(vstring, cit, len));
2616dff31646SBarry Smith   if (set) *set = PETSC_TRUE;
26171f817a21SBarry Smith   PetscFunctionReturn(0);
2618dff31646SBarry Smith }
2619dff31646SBarry Smith 
2620d8174014SToby Isaac PETSC_EXTERN                PETSC_DEPRECATED_FUNCTION("Google has discontinued its URL shortener service") PetscErrorCode PetscURLShorten(const char[], char[], size_t c);
2621fe278a28SBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm, char[], char[], size_t);
2622fe278a28SBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm, const char[], char[], size_t);
2623f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveUpload(MPI_Comm, const char[], const char[]);
2624fe278a28SBarry Smith 
2625f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscBoxAuthorize(MPI_Comm, char[], char[], size_t);
2626f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscBoxRefresh(MPI_Comm, const char[], char[], char[], size_t);
2627b967cddfSBarry Smith 
26289f4d3c52SBarry Smith PETSC_EXTERN PetscErrorCode PetscGlobusGetTransfers(MPI_Comm, const char[], char[], size_t);
26299f4d3c52SBarry Smith 
263004102261SBarry Smith PETSC_EXTERN PetscErrorCode PetscTextBelt(MPI_Comm, const char[], const char[], PetscBool *);
2631763ec7b1SBarry Smith PETSC_EXTERN PetscErrorCode PetscTellMyCell(MPI_Comm, const char[], const char[], PetscBool *);
2632dff31646SBarry Smith 
263368e69593SBarry Smith PETSC_EXTERN PetscErrorCode PetscPullJSONValue(const char[], const char[], char[], size_t, PetscBool *);
26345dc0f0a4SBarry Smith PETSC_EXTERN PetscErrorCode PetscPushJSONValue(char[], const char[], const char[], size_t);
2635d382aafbSBarry Smith 
2636b2566f29SBarry Smith #if defined(PETSC_USE_DEBUG)
26379371c9d4SSatish Balay static inline unsigned int PetscStrHash(const char *str) {
2638df05ca09SBarry Smith   unsigned int c, hash = 5381;
2639df05ca09SBarry Smith 
2640503e1a2cSLisandro Dalcin   while ((c = (unsigned int)*str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
2641df05ca09SBarry Smith   return hash;
2642df05ca09SBarry Smith }
264377a5e07dSBarry Smith 
264477a5e07dSBarry Smith /*MC
264587497f52SBarry Smith    MPIU_Allreduce - a PETSc replacement for `MPI_Allreduce()` that tries to determine if the call from all the MPI ranks occur from the
264687497f52SBarry Smith                     same place in the PETSc code. This helps to detect bugs where different MPI ranks follow different code paths
264787497f52SBarry Smith                     resulting in inconsistent and incorrect calls to `MPI_Allreduce()`.
264877a5e07dSBarry Smith 
2649d083f849SBarry Smith    Collective
265077a5e07dSBarry Smith 
265177a5e07dSBarry Smith    Synopsis:
265277a5e07dSBarry Smith      #include <petscsys.h>
265377a5e07dSBarry Smith      PetscErrorCode MPIU_Allreduce(void *indata,void *outdata,PetscMPIInt count,MPI_Datatype datatype, MPI_Op op, MPI_Comm comm);
265477a5e07dSBarry Smith 
265577a5e07dSBarry Smith    Input Parameters:
2656df05ca09SBarry Smith +  a - pointer to the input data to be reduced
2657df05ca09SBarry Smith .  c - the number of MPI data items in a and b
265887497f52SBarry Smith .  d - the MPI datatype, for example `MPI_INT`
265987497f52SBarry Smith .  e - the MPI operation, for example `MPI_SUM`
2660df05ca09SBarry Smith -  fcomm - the MPI communicator on which the operation occurs
266177a5e07dSBarry Smith 
266277a5e07dSBarry Smith    Output Parameter:
2663df05ca09SBarry Smith .  b - the reduced values
266477a5e07dSBarry Smith 
2665e28ce29aSPatrick Sanan    Notes:
266687497f52SBarry Smith      In optimized mode this directly calls `MPI_Allreduce()`
266777a5e07dSBarry Smith 
2668df05ca09SBarry Smith      This is defined as a macro that can return error codes internally so it cannot be used in a subroutine that returns void.
2669df05ca09SBarry Smith 
267087497f52SBarry 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
2671df05ca09SBarry Smith 
267277a5e07dSBarry Smith    Level: developer
267377a5e07dSBarry Smith 
2674db781477SPatrick Sanan .seealso: `MPI_Allreduce()`
267577a5e07dSBarry Smith M*/
26769371c9d4SSatish Balay #define MPIU_Allreduce(a, b, c, d, e, fcomm) \
26779371c9d4SSatish Balay   PetscMacroReturnStandard(PetscMPIInt a_b1[6], a_b2[6]; int _mpiu_allreduce_c_int = (int)c; a_b1[0] = -(PetscMPIInt)__LINE__; a_b1[1] = -a_b1[0]; a_b1[2] = -(PetscMPIInt)PetscStrHash(PETSC_FUNCTION_NAME); a_b1[3] = -a_b1[2]; a_b1[4] = -(PetscMPIInt)(c); a_b1[5] = -a_b1[4]; PetscCallMPI(MPI_Allreduce(a_b1, a_b2, 6, MPI_INT, MPI_MAX, fcomm)); PetscCheck(-a_b2[0] == a_b2[1], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MPI_Allreduce() called in different locations (code lines) on different processors"); PetscCheck(-a_b2[2] == a_b2[3], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MPI_Allreduce() called in different locations (functions) on different processors"); 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); PetscCallMPI(MPI_Allreduce((a), (b), (c), d, e, (fcomm)));)
2678b2566f29SBarry Smith #else
26791c2dc1cbSBarry Smith #define MPIU_Allreduce(a, b, c, d, e, fcomm) PetscMacroReturnStandard(PetscCallMPI(MPI_Allreduce((a), (b), (c), d, e, (fcomm))))
2680b2566f29SBarry Smith #endif
2681b2566f29SBarry Smith 
2682b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
26838198064fSBarry Smith PETSC_EXTERN PetscErrorCode MPIU_Win_allocate_shared(MPI_Aint, PetscMPIInt, MPI_Info, MPI_Comm, void *, MPI_Win *);
26848198064fSBarry Smith PETSC_EXTERN PetscErrorCode MPIU_Win_shared_query(MPI_Win, PetscMPIInt, MPI_Aint *, PetscMPIInt *, void *);
26852f065d89SBarry Smith #endif
26868198064fSBarry Smith 
2687cd88fca5SStefano Zampini /* this is a vile hack */
2688cd88fca5SStefano Zampini #if defined(PETSC_HAVE_NECMPI)
26899371c9d4SSatish 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)
2690cd88fca5SStefano Zampini #define MPI_Type_free(a) (*(a) = MPI_DATATYPE_NULL, 0);
2691cd88fca5SStefano Zampini #endif
2692961fb248SStefano Zampini #endif
2693cd88fca5SStefano Zampini 
269412801b39SBarry Smith /*
269560fbe2beSVaclav Hapla     List of external packages and queries on it
269660fbe2beSVaclav Hapla */
269760fbe2beSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscHasExternalPackage(const char[], PetscBool *);
269860fbe2beSVaclav Hapla 
2699a52b435bSMark Adams /*
2700a52b435bSMark Adams  OpenMP support
2701a52b435bSMark Adams */
2702a52b435bSMark Adams #if defined(_OPENMP)
2703a52b435bSMark Adams #define PetscPragmaOMP(...) _Pragma(PetscStringize(omp __VA_ARGS__))
2704a52b435bSMark Adams #else // no OpenMP so no threads
2705a52b435bSMark Adams #define PetscPragmaOMP(...)
2706a52b435bSMark Adams #endif
2707a52b435bSMark Adams 
2708f1f2ae84SBarry Smith /* this cannot go here because it may be in a different shared library */
2709f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPIServerBegin(void);
2710f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPIServerEnd(void);
2711f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPICommsDestroy(void);
2712d382aafbSBarry Smith #endif
2713