xref: /petsc/include/petscsys.h (revision 0241b27444902dd5927fea603f372e4bd012f3f4)
1d382aafbSBarry Smith /*
2d382aafbSBarry Smith    This is the main PETSc include file (for C and C++).  It is included by all
3d382aafbSBarry Smith    other PETSc include files, so it almost never has to be specifically included.
447d993e7Ssuyashtn    Portions of this code are under:
547d993e7Ssuyashtn    Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved.
6d382aafbSBarry Smith */
7a4963045SJacob Faibussowitsch #pragma once
83ca90d2dSJacob Faibussowitsch 
9d382aafbSBarry Smith /* ========================================================================== */
10d382aafbSBarry Smith /*
11d382aafbSBarry Smith    petscconf.h is contained in ${PETSC_ARCH}/include/petscconf.h it is
121c77a0c5SBarry Smith    found automatically by the compiler due to the -I${PETSC_DIR}/${PETSC_ARCH}/include that
131c77a0c5SBarry Smith    PETSc's makefiles add to the compiler rules.
141c77a0c5SBarry Smith    For --prefix installs the directory ${PETSC_ARCH} does not exist and petscconf.h is in the same
151cc8b206SBarry Smith    directory as the other PETSc include files.
16d382aafbSBarry Smith */
172c8e378dSBarry Smith #include <petscconf.h>
181c77a0c5SBarry Smith #include <petscconf_poison.h>
192c8e378dSBarry Smith #include <petscfix.h>
20bde483f2SJacob Faibussowitsch #include <petscmacros.h>
2133bb201bSJacob Faibussowitsch 
22ac09b921SBarry Smith /* SUBMANSEC = Sys */
23ac09b921SBarry Smith 
2473fca5a0SBarry Smith #if defined(PETSC_DESIRE_FEATURE_TEST_MACROS)
2573fca5a0SBarry Smith   /*
2673fca5a0SBarry Smith    Feature test macros must be included before headers defined by IEEE Std 1003.1-2001
2773fca5a0SBarry Smith    We only turn these in PETSc source files that require them by setting PETSC_DESIRE_FEATURE_TEST_MACROS
2873fca5a0SBarry Smith */
29216f1ae6SBarry Smith   #if defined(PETSC__POSIX_C_SOURCE_200112L) && !defined(_POSIX_C_SOURCE)
3073fca5a0SBarry Smith     #define _POSIX_C_SOURCE 200112L
3173fca5a0SBarry Smith   #endif
32216f1ae6SBarry Smith   #if defined(PETSC__BSD_SOURCE) && !defined(_BSD_SOURCE)
3373fca5a0SBarry Smith     #define _BSD_SOURCE
3473fca5a0SBarry Smith   #endif
3507f00807SSatish Balay   #if defined(PETSC__DEFAULT_SOURCE) && !defined(_DEFAULT_SOURCE)
3607f00807SSatish Balay     #define _DEFAULT_SOURCE
3707f00807SSatish Balay   #endif
38ea0fecefSShri Abhyankar   #if defined(PETSC__GNU_SOURCE) && !defined(_GNU_SOURCE)
39ea0fecefSShri Abhyankar     #define _GNU_SOURCE
40ea0fecefSShri Abhyankar   #endif
4173fca5a0SBarry Smith #endif
4273fca5a0SBarry Smith 
43df4397b0SStefano Zampini #include <petscsystypes.h>
44df4397b0SStefano Zampini 
45d382aafbSBarry Smith /* ========================================================================== */
46d382aafbSBarry Smith 
47d382aafbSBarry Smith /*
48d382aafbSBarry Smith     Defines the interface to MPI allowing the use of all MPI functions.
49d382aafbSBarry Smith 
50d382aafbSBarry Smith     PETSc does not use the C++ binding of MPI at ALL. The following flag
51d382aafbSBarry Smith     makes sure the C++ bindings are not included. The C++ bindings REQUIRE
52d382aafbSBarry Smith     putting mpi.h before ANY C++ include files, we cannot control this
53d382aafbSBarry Smith     with all PETSc users. Users who want to use the MPI C++ bindings can include
54d382aafbSBarry Smith     mpicxx.h directly in their code
55d382aafbSBarry Smith */
56fa9e63e2SJed Brown #if !defined(MPICH_SKIP_MPICXX)
57d382aafbSBarry Smith   #define MPICH_SKIP_MPICXX 1
58fa9e63e2SJed Brown #endif
59fa9e63e2SJed Brown #if !defined(OMPI_SKIP_MPICXX)
60d382aafbSBarry Smith   #define OMPI_SKIP_MPICXX 1
61fa9e63e2SJed Brown #endif
62e771154cSJed Brown #if defined(PETSC_HAVE_MPIUNI)
63e771154cSJed Brown   #include <petsc/mpiuni/mpi.h>
64e771154cSJed Brown #else
652c8e378dSBarry Smith   #include <mpi.h>
66e771154cSJed Brown #endif
6765013866SBarry Smith 
68d382aafbSBarry Smith /*
69a7886beaSBarry Smith    Perform various sanity checks that the correct mpi.h is being included at compile time.
70a7886beaSBarry Smith    This usually happens because
71a7886beaSBarry Smith       * either an unexpected mpi.h is in the default compiler path (i.e. in /usr/include) or
72a7886beaSBarry Smith       * an extra include path -I/something (which contains the unexpected mpi.h) is being passed to the compiler
73a7886beaSBarry Smith */
74a7886beaSBarry Smith #if defined(PETSC_HAVE_MPIUNI)
756524c165SJacob Faibussowitsch   #ifndef MPIUNI_H
76a7886beaSBarry Smith     #error "PETSc was configured with --with-mpi=0 but now appears to be compiling using a different mpi.h"
77a7886beaSBarry Smith   #endif
7841f4af4cSSatish Balay #elif defined(PETSC_HAVE_I_MPI_NUMVERSION)
7941f4af4cSSatish Balay   #if !defined(I_MPI_NUMVERSION)
8041f4af4cSSatish Balay     #error "PETSc was configured with I_MPI but now appears to be compiling using a non-I_MPI mpi.h"
8141f4af4cSSatish Balay   #elif I_MPI_NUMVERSION != PETSC_HAVE_I_MPI_NUMVERSION
8241f4af4cSSatish 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"
8341f4af4cSSatish Balay   #endif
8441f4af4cSSatish Balay #elif defined(PETSC_HAVE_MVAPICH2_NUMVERSION)
8541f4af4cSSatish Balay   #if !defined(MVAPICH2_NUMVERSION)
8641f4af4cSSatish Balay     #error "PETSc was configured with MVAPICH2 but now appears to be compiling using a non-MVAPICH2 mpi.h"
8741f4af4cSSatish Balay   #elif MVAPICH2_NUMVERSION != PETSC_HAVE_MVAPICH2_NUMVERSION
8841f4af4cSSatish Balay     #error "PETSc was configured with one MVAPICH2 mpi.h version but now appears to be compiling using a different MVAPICH2 mpi.h version"
8941f4af4cSSatish Balay   #endif
90a7886beaSBarry Smith #elif defined(PETSC_HAVE_MPICH_NUMVERSION)
9141f4af4cSSatish Balay   #if !defined(MPICH_NUMVERSION) || defined(MVAPICH2_NUMVERSION) || defined(I_MPI_NUMVERSION)
92a7886beaSBarry Smith     #error "PETSc was configured with MPICH but now appears to be compiling using a non-MPICH mpi.h"
931c284764SMin 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)
94a7886beaSBarry Smith     #error "PETSc was configured with one MPICH mpi.h version but now appears to be compiling using a different MPICH mpi.h version"
95a7886beaSBarry Smith   #endif
96a7886beaSBarry Smith #elif defined(PETSC_HAVE_OMPI_MAJOR_VERSION)
97a7886beaSBarry Smith   #if !defined(OMPI_MAJOR_VERSION)
98a7886beaSBarry Smith     #error "PETSc was configured with Open MPI but now appears to be compiling using a non-Open MPI mpi.h"
991c284764SMin 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)
100c3ac57cfSSatish Balay     #error "PETSc was configured with one Open MPI mpi.h version but now appears to be compiling using a different Open MPI mpi.h version"
101a7886beaSBarry Smith   #endif
102d97fd468SStefano Zampini #elif defined(PETSC_HAVE_MSMPI_VERSION)
103d97fd468SStefano Zampini   #if !defined(MSMPI_VER)
104d97fd468SStefano Zampini     #error "PETSc was configured with MSMPI but now appears to be compiling using a non-MSMPI mpi.h"
105d97fd468SStefano Zampini   #elif (MSMPI_VER != PETSC_HAVE_MSMPI_VERSION)
106d97fd468SStefano Zampini     #error "PETSc was configured with one MSMPI mpi.h version but now appears to be compiling using a different MSMPI mpi.h version"
107d97fd468SStefano Zampini   #endif
108d97fd468SStefano Zampini #elif defined(OMPI_MAJOR_VERSION) || defined(MPICH_NUMVERSION) || defined(MSMPI_VER)
109d97fd468SStefano Zampini   #error "PETSc was configured with undetermined MPI - but now appears to be compiling using any of Open MPI, MS-MPI or a MPICH variant"
110a7886beaSBarry Smith #endif
111a7886beaSBarry Smith 
112a7886beaSBarry Smith /*
1132981ebdbSBarry Smith     Need to put stdio.h AFTER mpi.h for MPICH2 with C++ compiler
114d382aafbSBarry Smith     see the top of mpicxx.h in the MPICH2 distribution.
115d382aafbSBarry Smith */
116d382aafbSBarry Smith #include <stdio.h>
117d382aafbSBarry Smith 
1187de69702SBarry Smith /* MSMPI on 32-bit Microsoft Windows requires this yukky hack - that breaks MPI standard compliance */
119d382aafbSBarry Smith #if !defined(MPIAPI)
120d382aafbSBarry Smith   #define MPIAPI
121d382aafbSBarry Smith #endif
122d382aafbSBarry Smith 
12393d501b3SJacob Faibussowitsch PETSC_EXTERN MPI_Datatype MPIU_ENUM PETSC_ATTRIBUTE_MPI_TYPE_TAG(PetscEnum);
12493d501b3SJacob Faibussowitsch PETSC_EXTERN MPI_Datatype MPIU_BOOL PETSC_ATTRIBUTE_MPI_TYPE_TAG(PetscBool);
125e28ce29aSPatrick Sanan 
126e28ce29aSPatrick Sanan /*MC
12787497f52SBarry Smith    MPIU_INT - Portable MPI datatype corresponding to `PetscInt` independent of the precision of `PetscInt`
128e28ce29aSPatrick Sanan 
129667f096bSBarry Smith    Level: beginner
130667f096bSBarry Smith 
13138b83642SBarry Smith    Note:
13287497f52SBarry Smith    In MPI calls that require an MPI datatype that matches a `PetscInt` or array of `PetscInt` values, pass this value.
133e28ce29aSPatrick Sanan 
13438b83642SBarry Smith .seealso: `PetscReal`, `PetscScalar`, `PetscComplex`, `PetscInt`, `MPIU_COUNT`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`
135e28ce29aSPatrick Sanan M*/
136e28ce29aSPatrick Sanan 
1377cdaf61dSJed Brown PETSC_EXTERN MPI_Datatype MPIU_FORTRANADDR;
138b7b8f77aSBarry Smith 
139d382aafbSBarry Smith #if defined(PETSC_USE_64BIT_INDICES)
140bd84f1cfSSatish Balay   #define MPIU_INT MPIU_INT64
141d382aafbSBarry Smith #else
142d382aafbSBarry Smith   #define MPIU_INT MPI_INT
143d382aafbSBarry Smith #endif
144d382aafbSBarry Smith 
14538b83642SBarry Smith /*MC
14638b83642SBarry Smith    MPIU_COUNT - Portable MPI datatype corresponding to `PetscCount` independent of the precision of `PetscCount`
14738b83642SBarry Smith 
14838b83642SBarry Smith    Level: beginner
14938b83642SBarry Smith 
15038b83642SBarry Smith    Note:
15138b83642SBarry Smith    In MPI calls that require an MPI datatype that matches a `PetscCount` or array of `PetscCount` values, pass this value.
15238b83642SBarry Smith 
15338b83642SBarry Smith   Developer Note:
15495bd0b28SBarry Smith   It seems `MPI_AINT` is unsigned so this may be the wrong choice here since `PetscCount` is signed
15538b83642SBarry Smith 
15638b83642SBarry Smith .seealso: `PetscReal`, `PetscScalar`, `PetscComplex`, `PetscInt`, `MPIU_INT`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`
15738b83642SBarry Smith M*/
15838b83642SBarry Smith #define MPIU_COUNT MPI_AINT
15938b83642SBarry Smith 
160503cfb0cSBarry Smith /*
161503cfb0cSBarry Smith     For the rare cases when one needs to send a size_t object with MPI
162503cfb0cSBarry Smith */
16393d501b3SJacob Faibussowitsch PETSC_EXTERN MPI_Datatype MPIU_SIZE_T PETSC_ATTRIBUTE_MPI_TYPE_TAG(size_t);
164d382aafbSBarry Smith 
165d382aafbSBarry Smith /*
166d382aafbSBarry Smith       You can use PETSC_STDOUT as a replacement of stdout. You can also change
167d382aafbSBarry Smith     the value of PETSC_STDOUT to redirect all standard output elsewhere
168d382aafbSBarry Smith */
169014dd563SJed Brown PETSC_EXTERN FILE *PETSC_STDOUT;
170d382aafbSBarry Smith 
171d382aafbSBarry Smith /*
172d382aafbSBarry Smith       You can use PETSC_STDERR as a replacement of stderr. You can also change
173d382aafbSBarry Smith     the value of PETSC_STDERR to redirect all standard error elsewhere
174d382aafbSBarry Smith */
175014dd563SJed Brown PETSC_EXTERN FILE *PETSC_STDERR;
176d382aafbSBarry Smith 
177d382aafbSBarry Smith /*
178dc07da0dSStefano Zampini   Handle inclusion when using clang compiler with CUDA support
179dc07da0dSStefano Zampini   __float128 is not available for the device
180dc07da0dSStefano Zampini */
181dc07da0dSStefano Zampini #if defined(__clang__) && defined(__CUDA_ARCH__)
182dc07da0dSStefano Zampini   #define PETSC_SKIP_REAL___FLOAT128
183dc07da0dSStefano Zampini #endif
184dc07da0dSStefano Zampini 
185dc07da0dSStefano Zampini /*
186d382aafbSBarry Smith     Declare extern C stuff after including external header files
187d382aafbSBarry Smith */
188d382aafbSBarry Smith 
1896edef35eSSatish Balay PETSC_EXTERN PetscBool PETSC_RUNNING_ON_VALGRIND;
1908b49ba18SBarry Smith /*
191390e1bf2SBarry Smith     Defines elementary mathematics functions and constants.
1928b49ba18SBarry Smith */
1938b49ba18SBarry Smith #include <petscmath.h>
1948b49ba18SBarry Smith 
195d382aafbSBarry Smith /*MC
196667f096bSBarry Smith     PETSC_IGNORE - same as `NULL`, means PETSc will ignore this argument
197d382aafbSBarry Smith 
198d382aafbSBarry Smith    Level: beginner
199d382aafbSBarry Smith 
200e28ce29aSPatrick Sanan    Note:
201667f096bSBarry Smith    Accepted by many PETSc functions to not set a parameter and instead use a default value
202d382aafbSBarry Smith 
20387497f52SBarry Smith    Fortran Note:
204667f096bSBarry Smith    Use `PETSC_NULL_INTEGER`, `PETSC_NULL_DOUBLE_PRECISION` etc
205d382aafbSBarry Smith 
206db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_DEFAULT`, `PETSC_DETERMINE`
207d382aafbSBarry Smith M*/
208f3fa974cSJacob Faibussowitsch #define PETSC_IGNORE PETSC_NULLPTR
209edd03b47SJacob Faibussowitsch #define PETSC_NULL   PETSC_DEPRECATED_MACRO(3, 19, 0, "PETSC_NULLPTR", ) PETSC_NULLPTR
210c528d872SBarry Smith 
211d382aafbSBarry Smith /*MC
212557d4da8SBarry Smith     PETSC_DECIDE - standard way of passing in integer or floating point parameter
213557d4da8SBarry Smith        where you wish PETSc to use the default.
214557d4da8SBarry Smith 
215557d4da8SBarry Smith    Level: beginner
216557d4da8SBarry Smith 
217db781477SPatrick Sanan .seealso: `PETSC_DEFAULT`, `PETSC_IGNORE`, `PETSC_DETERMINE`
218557d4da8SBarry Smith M*/
219557d4da8SBarry Smith 
220557d4da8SBarry Smith /*MC
221af27ebaaSBarry Smith    PETSC_DETERMINE - standard way of passing in integer or floating point parameter where you wish PETSc to compute the required value.
222d382aafbSBarry Smith 
223d382aafbSBarry Smith    Level: beginner
224d382aafbSBarry Smith 
225e28ce29aSPatrick Sanan    Developer Note:
22687497f52SBarry Smith    I would like to use const `PetscInt` `PETSC_DETERMINE` = `PETSC_DECIDE`; but for
22787497f52SBarry Smith    some reason this is not allowed by the standard even though `PETSC_DECIDE` is a constant value.
228557d4da8SBarry Smith 
229db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_DEFAULT`, `PETSC_IGNORE`, `VecSetSizes()`
230d382aafbSBarry Smith M*/
231d382aafbSBarry Smith 
232d382aafbSBarry Smith /*MC
233af27ebaaSBarry Smith    PETSC_DEFAULT - standard way of passing in integer or floating point parameter where you wish PETSc to use the default.
234557d4da8SBarry Smith 
235557d4da8SBarry Smith    Level: beginner
236557d4da8SBarry Smith 
23787497f52SBarry Smith    Fortran Note:
23887497f52SBarry Smith    You need to use `PETSC_DEFAULT_INTEGER` or `PETSC_DEFAULT_REAL`.
239557d4da8SBarry Smith 
240db781477SPatrick Sanan .seealso: `PETSC_DECIDE`, `PETSC_IGNORE`, `PETSC_DETERMINE`
241557d4da8SBarry Smith M*/
24205c2f48cSJacob Faibussowitsch 
24305c2f48cSJacob Faibussowitsch /* These MUST be preprocessor defines! see https://gitlab.com/petsc/petsc/-/issues/1370 */
24405c2f48cSJacob Faibussowitsch #define PETSC_DECIDE    (-1)
24505c2f48cSJacob Faibussowitsch #define PETSC_DETERMINE PETSC_DECIDE
24605c2f48cSJacob Faibussowitsch #define PETSC_DEFAULT   (-2)
247557d4da8SBarry Smith 
248557d4da8SBarry Smith /*MC
249af27ebaaSBarry Smith    PETSC_COMM_WORLD - the equivalent of the `MPI_COMM_WORLD` communicator which represents all the processes that PETSc knows about.
250d382aafbSBarry Smith 
251d382aafbSBarry Smith    Level: beginner
252d382aafbSBarry Smith 
253e28ce29aSPatrick Sanan    Notes:
25487497f52SBarry Smith    By default `PETSC_COMM_WORLD` and `MPI_COMM_WORLD` are identical unless you wish to
25587497f52SBarry Smith    run PETSc on ONLY a subset of `MPI_COMM_WORLD`. In that case create your new (smaller)
25687497f52SBarry Smith    communicator, call it, say comm, and set `PETSC_COMM_WORLD` = comm BEFORE calling
257af27ebaaSBarry Smith    `PetscInitialize()`, but after `MPI_Init()` has been called.
258a990b902SBarry Smith 
259af27ebaaSBarry Smith    The value of `PETSC_COMM_WORLD` should never be used or accessed before `PetscInitialize()`
260a990b902SBarry Smith    is called because it may not have a valid value yet.
261d382aafbSBarry Smith 
262db781477SPatrick Sanan .seealso: `PETSC_COMM_SELF`
263d382aafbSBarry Smith M*/
264014dd563SJed Brown PETSC_EXTERN MPI_Comm PETSC_COMM_WORLD;
265d382aafbSBarry Smith 
266d382aafbSBarry Smith /*MC
26787497f52SBarry Smith    PETSC_COMM_SELF - This is always `MPI_COMM_SELF`
268d382aafbSBarry Smith 
269d382aafbSBarry Smith    Level: beginner
270d382aafbSBarry Smith 
27195bd0b28SBarry Smith    Note:
27216a05f60SBarry Smith    Do not USE/access or set this variable before `PetscInitialize()` has been called.
273a990b902SBarry Smith 
274db781477SPatrick Sanan .seealso: `PETSC_COMM_WORLD`
275d382aafbSBarry Smith M*/
276d382aafbSBarry Smith #define PETSC_COMM_SELF MPI_COMM_SELF
277d382aafbSBarry Smith 
2786de5d289SStefano Zampini /*MC
279af27ebaaSBarry Smith    PETSC_MPI_THREAD_REQUIRED - the required threading support used if PETSc initializes MPI with `MPI_Init_thread()`.
2806de5d289SStefano Zampini 
281d598313aSBarry Smith    No Fortran Support
282d598313aSBarry Smith 
2836de5d289SStefano Zampini    Level: beginner
2846de5d289SStefano Zampini 
28595bd0b28SBarry Smith    Note:
286d598313aSBarry Smith    By default `PETSC_MPI_THREAD_REQUIRED` equals `MPI_THREAD_FUNNELED` when the MPI implementation provides `MPI_Init_thread()`, otherwise it equals `MPI_THREAD_SINGLE`
2876de5d289SStefano Zampini 
288db781477SPatrick Sanan .seealso: `PetscInitialize()`
2896de5d289SStefano Zampini M*/
2906de5d289SStefano Zampini PETSC_EXTERN PetscMPIInt PETSC_MPI_THREAD_REQUIRED;
2916de5d289SStefano Zampini 
292d598313aSBarry Smith /*MC
293d598313aSBarry Smith    PetscBeganMPI - indicates if PETSc initialized MPI during `PetscInitialize()` or if MPI was already initialized.
294d598313aSBarry Smith 
295d598313aSBarry Smith    Synopsis:
296d598313aSBarry Smith    #include <petscsys.h>
297d598313aSBarry Smith    PetscBool PetscBeganMPI;
298d598313aSBarry Smith 
299d598313aSBarry Smith    No Fortran Support
300d598313aSBarry Smith 
301d598313aSBarry Smith    Level: developer
302d598313aSBarry Smith 
303*0241b274SPierre Jolivet .seealso: `PetscInitialize()`, `PetscInitializeCalled`
304d598313aSBarry Smith M*/
305d6f2c3cbSBarry Smith PETSC_EXTERN PetscBool PetscBeganMPI;
306d598313aSBarry Smith 
3078ad20175SVaclav Hapla PETSC_EXTERN PetscBool PetscErrorHandlingInitialized;
308014dd563SJed Brown PETSC_EXTERN PetscBool PetscInitializeCalled;
309014dd563SJed Brown PETSC_EXTERN PetscBool PetscFinalizeCalled;
3104cf1874eSKarl Rupp PETSC_EXTERN PetscBool PetscViennaCLSynchronize;
311d382aafbSBarry Smith 
312014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*)(MPI_Comm), PetscErrorCode (*)(MPI_Comm));
313014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommDuplicate(MPI_Comm, MPI_Comm *, int *);
314014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommDestroy(MPI_Comm *);
31557f21012SBarry Smith PETSC_EXTERN PetscErrorCode PetscCommGetComm(MPI_Comm, MPI_Comm *);
31657f21012SBarry Smith PETSC_EXTERN PetscErrorCode PetscCommRestoreComm(MPI_Comm, MPI_Comm *);
317d382aafbSBarry Smith 
31859e55d94SJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
31959e55d94SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscKokkosInitializeCheck(void); /* Initialize Kokkos if not yet. */
32059e55d94SJunchao Zhang #endif
32159e55d94SJunchao Zhang 
32271438e86SJunchao Zhang #if defined(PETSC_HAVE_NVSHMEM)
32371438e86SJunchao Zhang PETSC_EXTERN PetscBool      PetscBeganNvshmem;
32471438e86SJunchao Zhang PETSC_EXTERN PetscBool      PetscNvshmemInitialized;
32571438e86SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscNvshmemFinalize(void);
32671438e86SJunchao Zhang #endif
32771438e86SJunchao Zhang 
328540e20f2SPierre Jolivet #if defined(PETSC_HAVE_ELEMENTAL)
329540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalInitializePackage(void);
330540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalInitialized(PetscBool *);
331540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalFinalizePackage(void);
332540e20f2SPierre Jolivet #endif
333540e20f2SPierre Jolivet 
334d382aafbSBarry Smith /*MC
33587497f52SBarry Smith    PetscMalloc - Allocates memory, One should use `PetscNew()`, `PetscMalloc1()` or `PetscCalloc1()` usually instead of this
336d382aafbSBarry Smith 
337eca87e8dSBarry Smith    Synopsis:
338aaa7dc30SBarry Smith     #include <petscsys.h>
339eca87e8dSBarry Smith    PetscErrorCode PetscMalloc(size_t m,void **result)
340eca87e8dSBarry Smith 
341eca87e8dSBarry Smith    Not Collective
342eca87e8dSBarry Smith 
343d382aafbSBarry Smith    Input Parameter:
344d382aafbSBarry Smith .  m - number of bytes to allocate
345d382aafbSBarry Smith 
346d382aafbSBarry Smith    Output Parameter:
347d382aafbSBarry Smith .  result - memory allocated
348d382aafbSBarry Smith 
349d382aafbSBarry Smith    Level: beginner
350d382aafbSBarry Smith 
35149d7da52SJed Brown    Notes:
35249d7da52SJed Brown    Memory is always allocated at least double aligned
353d382aafbSBarry Smith 
35416a05f60SBarry Smith    It is safe to allocate size 0 and pass the resulting pointer (which may or may not be `NULL`) to `PetscFree()`.
355d382aafbSBarry Smith 
356db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`
357d382aafbSBarry Smith M*/
358071fcb05SBarry Smith #define PetscMalloc(a, b) ((*PetscTrMalloc)((a), PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))
359d382aafbSBarry Smith 
360d382aafbSBarry Smith /*MC
361d5b43468SJose E. Roman    PetscRealloc - Reallocates memory
3623221ece2SMatthew G. Knepley 
3633221ece2SMatthew G. Knepley    Synopsis:
3643221ece2SMatthew G. Knepley     #include <petscsys.h>
3653221ece2SMatthew G. Knepley    PetscErrorCode PetscRealloc(size_t m,void **result)
3663221ece2SMatthew G. Knepley 
3673221ece2SMatthew G. Knepley    Not Collective
3683221ece2SMatthew G. Knepley 
3693221ece2SMatthew G. Knepley    Input Parameters:
3703221ece2SMatthew G. Knepley +  m - number of bytes to allocate
37192f119d6SBarry Smith -  result - previous memory
3723221ece2SMatthew G. Knepley 
3733221ece2SMatthew G. Knepley    Output Parameter:
3743221ece2SMatthew G. Knepley .  result - new memory allocated
3753221ece2SMatthew G. Knepley 
3763221ece2SMatthew G. Knepley    Level: developer
3773221ece2SMatthew G. Knepley 
37895bd0b28SBarry Smith    Note:
3793221ece2SMatthew G. Knepley    Memory is always allocated at least double aligned
3803221ece2SMatthew G. Knepley 
381db781477SPatrick Sanan .seealso: `PetscMalloc()`, `PetscFree()`, `PetscNew()`
3823221ece2SMatthew G. Knepley M*/
3833221ece2SMatthew G. Knepley #define PetscRealloc(a, b) ((*PetscTrRealloc)((a), __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))
3843221ece2SMatthew G. Knepley 
3853221ece2SMatthew G. Knepley /*MC
38687497f52SBarry Smith    PetscAddrAlign - Rounds up an address to `PETSC_MEMALIGN` alignment
387d382aafbSBarry Smith 
388d382aafbSBarry Smith    Synopsis:
389aaa7dc30SBarry Smith     #include <petscsys.h>
390d382aafbSBarry Smith    void *PetscAddrAlign(void *addr)
391d382aafbSBarry Smith 
392eca87e8dSBarry Smith    Not Collective
393eca87e8dSBarry Smith 
3942fe279fdSBarry Smith    Input Parameter:
395eca87e8dSBarry Smith .  addr - address to align (any pointer type)
396eca87e8dSBarry Smith 
397d382aafbSBarry Smith    Level: developer
398d382aafbSBarry Smith 
399db781477SPatrick Sanan .seealso: `PetscMallocAlign()`
400d382aafbSBarry Smith M*/
401f3fa974cSJacob Faibussowitsch #define PetscAddrAlign(a) ((void *)((((PETSC_UINTPTR_T)(a)) + (PETSC_MEMALIGN - 1)) & ~(PETSC_MEMALIGN - 1)))
402d382aafbSBarry Smith 
403d382aafbSBarry Smith /*MC
40487497f52SBarry Smith    PetscCalloc - Allocates a cleared (zeroed) memory region aligned to `PETSC_MEMALIGN`
4058f51dc47SJunchao Zhang 
4068f51dc47SJunchao Zhang    Synopsis:
4078f51dc47SJunchao Zhang     #include <petscsys.h>
4088f51dc47SJunchao Zhang    PetscErrorCode PetscCalloc(size_t m,void **result)
4098f51dc47SJunchao Zhang 
4108f51dc47SJunchao Zhang    Not Collective
4118f51dc47SJunchao Zhang 
4128f51dc47SJunchao Zhang    Input Parameter:
4138f51dc47SJunchao Zhang .  m - number of bytes to allocate
4148f51dc47SJunchao Zhang 
4158f51dc47SJunchao Zhang    Output Parameter:
4168f51dc47SJunchao Zhang .  result - memory allocated
4178f51dc47SJunchao Zhang 
4188f51dc47SJunchao Zhang    Level: beginner
4198f51dc47SJunchao Zhang 
4208f51dc47SJunchao Zhang    Notes:
4218f51dc47SJunchao Zhang    Memory is always allocated at least double aligned. This macro is useful in allocating memory pointed by void pointers
4228f51dc47SJunchao Zhang 
42316a05f60SBarry Smith    It is safe to allocate size 0 and pass the resulting pointer (which may or may not be `NULL`) to `PetscFree()`.
4248f51dc47SJunchao Zhang 
425db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`
4268f51dc47SJunchao Zhang M*/
427f3fa974cSJacob Faibussowitsch #define PetscCalloc(m, result) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)m), (result))
4288f51dc47SJunchao Zhang 
4298f51dc47SJunchao Zhang /*MC
43087497f52SBarry Smith    PetscMalloc1 - Allocates an array of memory aligned to `PETSC_MEMALIGN`
431d382aafbSBarry Smith 
432eca87e8dSBarry Smith    Synopsis:
433aaa7dc30SBarry Smith     #include <petscsys.h>
434785e854fSJed Brown    PetscErrorCode PetscMalloc1(size_t m1,type **r1)
435785e854fSJed Brown 
436785e854fSJed Brown    Not Collective
437785e854fSJed Brown 
438785e854fSJed Brown    Input Parameter:
439390e1bf2SBarry Smith .  m1 - number of elements to allocate  (may be zero)
440785e854fSJed Brown 
441785e854fSJed Brown    Output Parameter:
4421fe1b817SJunchao Zhang .  r1 - memory allocated
443785e854fSJed Brown 
44416a05f60SBarry Smith    Level: beginner
44516a05f60SBarry Smith 
446e28ce29aSPatrick Sanan    Note:
447e28ce29aSPatrick Sanan    This uses the sizeof() of the memory type requested to determine the total memory to be allocated, therefore you should not
44887497f52SBarry Smith          multiply the number of elements requested by the `sizeof()` the type. For example use
44916a05f60SBarry Smith .vb
45016a05f60SBarry Smith   PetscInt *id;
45116a05f60SBarry Smith   PetscMalloc1(10,&id);
45216a05f60SBarry Smith .ve
453390e1bf2SBarry Smith        not
45416a05f60SBarry Smith .vb
45516a05f60SBarry Smith   PetscInt *id;
45616a05f60SBarry Smith   PetscMalloc1(10*sizeof(PetscInt),&id);
45716a05f60SBarry Smith .ve
458390e1bf2SBarry Smith 
45987497f52SBarry Smith         Does not zero the memory allocated, use `PetscCalloc1()` to obtain memory that has been zeroed.
460390e1bf2SBarry Smith 
461db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
462785e854fSJed Brown M*/
463f3fa974cSJacob Faibussowitsch #define PetscMalloc1(m1, r1) PetscMallocA(1, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1))
464785e854fSJed Brown 
465785e854fSJed Brown /*MC
46687497f52SBarry Smith    PetscCalloc1 - Allocates a cleared (zeroed) array of memory aligned to `PETSC_MEMALIGN`
4671795a4d1SJed Brown 
4681795a4d1SJed Brown    Synopsis:
469aaa7dc30SBarry Smith     #include <petscsys.h>
4701795a4d1SJed Brown    PetscErrorCode PetscCalloc1(size_t m1,type **r1)
4711795a4d1SJed Brown 
4721795a4d1SJed Brown    Not Collective
4731795a4d1SJed Brown 
4741795a4d1SJed Brown    Input Parameter:
4751795a4d1SJed Brown .  m1 - number of elements to allocate in 1st chunk  (may be zero)
4761795a4d1SJed Brown 
4771795a4d1SJed Brown    Output Parameter:
4781fe1b817SJunchao Zhang .  r1 - memory allocated
4791795a4d1SJed Brown 
48016a05f60SBarry Smith    Level: beginner
48116a05f60SBarry Smith 
48295bd0b28SBarry Smith    Note:
48387497f52SBarry Smith    See `PetsMalloc1()` for more details on usage.
484390e1bf2SBarry Smith 
485db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
4861795a4d1SJed Brown M*/
487f3fa974cSJacob Faibussowitsch #define PetscCalloc1(m1, r1) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1))
4881795a4d1SJed Brown 
4891795a4d1SJed Brown /*MC
49087497f52SBarry Smith    PetscMalloc2 - Allocates 2 arrays of memory both aligned to `PETSC_MEMALIGN`
491d382aafbSBarry Smith 
492eca87e8dSBarry Smith    Synopsis:
493aaa7dc30SBarry Smith     #include <petscsys.h>
494dcca6d9dSJed Brown    PetscErrorCode PetscMalloc2(size_t m1,type **r1,size_t m2,type **r2)
495eca87e8dSBarry Smith 
496eca87e8dSBarry Smith    Not Collective
497eca87e8dSBarry Smith 
498d8d19677SJose E. Roman    Input Parameters:
499d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
500dcca6d9dSJed Brown -  m2 - number of elements to allocate in 2nd chunk  (may be zero)
501d382aafbSBarry Smith 
502d8d19677SJose E. Roman    Output Parameters:
503d382aafbSBarry Smith +  r1 - memory allocated in first chunk
504d382aafbSBarry Smith -  r2 - memory allocated in second chunk
505d382aafbSBarry Smith 
506d382aafbSBarry Smith    Level: developer
507d382aafbSBarry Smith 
508db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
509d382aafbSBarry Smith M*/
510f3fa974cSJacob Faibussowitsch #define PetscMalloc2(m1, r1, m2, r2) PetscMallocA(2, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2))
511d382aafbSBarry Smith 
512d382aafbSBarry Smith /*MC
51387497f52SBarry Smith    PetscCalloc2 - Allocates 2 cleared (zeroed) arrays of memory both aligned to `PETSC_MEMALIGN`
514d382aafbSBarry Smith 
515eca87e8dSBarry Smith    Synopsis:
516aaa7dc30SBarry Smith     #include <petscsys.h>
5171795a4d1SJed Brown    PetscErrorCode PetscCalloc2(size_t m1,type **r1,size_t m2,type **r2)
518eca87e8dSBarry Smith 
519eca87e8dSBarry Smith    Not Collective
520eca87e8dSBarry Smith 
521d8d19677SJose E. Roman    Input Parameters:
522d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
5231795a4d1SJed Brown -  m2 - number of elements to allocate in 2nd chunk  (may be zero)
5241795a4d1SJed Brown 
525d8d19677SJose E. Roman    Output Parameters:
5261795a4d1SJed Brown +  r1 - memory allocated in first chunk
5271795a4d1SJed Brown -  r2 - memory allocated in second chunk
5281795a4d1SJed Brown 
5291795a4d1SJed Brown    Level: developer
5301795a4d1SJed Brown 
531db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
5321795a4d1SJed Brown M*/
533f3fa974cSJacob Faibussowitsch #define PetscCalloc2(m1, r1, m2, r2) PetscMallocA(2, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2))
5341795a4d1SJed Brown 
5351795a4d1SJed Brown /*MC
53687497f52SBarry Smith    PetscMalloc3 - Allocates 3 arrays of memory, all aligned to `PETSC_MEMALIGN`
537d382aafbSBarry Smith 
538d382aafbSBarry Smith    Synopsis:
539aaa7dc30SBarry Smith     #include <petscsys.h>
540dcca6d9dSJed Brown    PetscErrorCode PetscMalloc3(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3)
541d382aafbSBarry Smith 
542d382aafbSBarry Smith    Not Collective
543d382aafbSBarry Smith 
544d8d19677SJose E. Roman    Input Parameters:
545d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
546d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
547dcca6d9dSJed Brown -  m3 - number of elements to allocate in 3rd chunk  (may be zero)
548d382aafbSBarry Smith 
549d8d19677SJose E. Roman    Output Parameters:
550d382aafbSBarry Smith +  r1 - memory allocated in first chunk
551d382aafbSBarry Smith .  r2 - memory allocated in second chunk
552d382aafbSBarry Smith -  r3 - memory allocated in third chunk
553d382aafbSBarry Smith 
554d382aafbSBarry Smith    Level: developer
555d382aafbSBarry Smith 
556db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc3()`, `PetscFree3()`
557d382aafbSBarry Smith M*/
558f3fa974cSJacob Faibussowitsch #define PetscMalloc3(m1, r1, m2, r2, m3, r3) \
559f3fa974cSJacob Faibussowitsch   PetscMallocA(3, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3))
560d382aafbSBarry Smith 
561d382aafbSBarry Smith /*MC
56287497f52SBarry Smith    PetscCalloc3 - Allocates 3 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
563d382aafbSBarry Smith 
564eca87e8dSBarry Smith    Synopsis:
565aaa7dc30SBarry Smith     #include <petscsys.h>
5661795a4d1SJed Brown    PetscErrorCode PetscCalloc3(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3)
567eca87e8dSBarry Smith 
568eca87e8dSBarry Smith    Not Collective
569eca87e8dSBarry Smith 
570d8d19677SJose E. Roman    Input Parameters:
571d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
572d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
5731795a4d1SJed Brown -  m3 - number of elements to allocate in 3rd chunk  (may be zero)
5741795a4d1SJed Brown 
575d8d19677SJose E. Roman    Output Parameters:
5761795a4d1SJed Brown +  r1 - memory allocated in first chunk
5771795a4d1SJed Brown .  r2 - memory allocated in second chunk
5781795a4d1SJed Brown -  r3 - memory allocated in third chunk
5791795a4d1SJed Brown 
5801795a4d1SJed Brown    Level: developer
5811795a4d1SJed Brown 
582db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc2()`, `PetscMalloc3()`, `PetscFree3()`
5831795a4d1SJed Brown M*/
584f3fa974cSJacob Faibussowitsch #define PetscCalloc3(m1, r1, m2, r2, m3, r3) \
585f3fa974cSJacob Faibussowitsch   PetscMallocA(3, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3))
5861795a4d1SJed Brown 
5871795a4d1SJed Brown /*MC
58887497f52SBarry Smith    PetscMalloc4 - Allocates 4 arrays of memory, all aligned to `PETSC_MEMALIGN`
589d382aafbSBarry Smith 
590d382aafbSBarry Smith    Synopsis:
591aaa7dc30SBarry Smith     #include <petscsys.h>
592dcca6d9dSJed Brown    PetscErrorCode PetscMalloc4(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4)
593d382aafbSBarry Smith 
594d382aafbSBarry Smith    Not Collective
595d382aafbSBarry Smith 
596d8d19677SJose E. Roman    Input Parameters:
597d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
598d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
599d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
600dcca6d9dSJed Brown -  m4 - number of elements to allocate in 4th chunk  (may be zero)
601d382aafbSBarry Smith 
602d8d19677SJose E. Roman    Output Parameters:
603d382aafbSBarry Smith +  r1 - memory allocated in first chunk
604d382aafbSBarry Smith .  r2 - memory allocated in second chunk
605d382aafbSBarry Smith .  r3 - memory allocated in third chunk
606d382aafbSBarry Smith -  r4 - memory allocated in fourth chunk
607d382aafbSBarry Smith 
608d382aafbSBarry Smith    Level: developer
609d382aafbSBarry Smith 
610db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
611d382aafbSBarry Smith M*/
6129371c9d4SSatish Balay #define PetscMalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
613f3fa974cSJacob Faibussowitsch   PetscMallocA(4, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4))
614d382aafbSBarry Smith 
615d382aafbSBarry Smith /*MC
61687497f52SBarry Smith    PetscCalloc4 - Allocates 4 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
617d382aafbSBarry Smith 
618eca87e8dSBarry Smith    Synopsis:
619aaa7dc30SBarry Smith     #include <petscsys.h>
6201795a4d1SJed Brown    PetscErrorCode PetscCalloc4(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4)
621eca87e8dSBarry Smith 
622eca87e8dSBarry Smith    Not Collective
623eca87e8dSBarry Smith 
624e28ce29aSPatrick Sanan    Input Parameters:
625d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
626d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
627d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
6281795a4d1SJed Brown -  m4 - number of elements to allocate in 4th chunk  (may be zero)
6291795a4d1SJed Brown 
630e28ce29aSPatrick Sanan    Output Parameters:
6311795a4d1SJed Brown +  r1 - memory allocated in first chunk
6321795a4d1SJed Brown .  r2 - memory allocated in second chunk
6331795a4d1SJed Brown .  r3 - memory allocated in third chunk
6341795a4d1SJed Brown -  r4 - memory allocated in fourth chunk
6351795a4d1SJed Brown 
6361795a4d1SJed Brown    Level: developer
6371795a4d1SJed Brown 
638db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
6391795a4d1SJed Brown M*/
6409371c9d4SSatish Balay #define PetscCalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
641f3fa974cSJacob Faibussowitsch   PetscMallocA(4, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4))
6421795a4d1SJed Brown 
6431795a4d1SJed Brown /*MC
64487497f52SBarry Smith    PetscMalloc5 - Allocates 5 arrays of memory, all aligned to `PETSC_MEMALIGN`
645d382aafbSBarry Smith 
646d382aafbSBarry Smith    Synopsis:
647aaa7dc30SBarry Smith     #include <petscsys.h>
648dcca6d9dSJed 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)
649d382aafbSBarry Smith 
650d382aafbSBarry Smith    Not Collective
651d382aafbSBarry Smith 
652e28ce29aSPatrick Sanan    Input Parameters:
653d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
654d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
655d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
656d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
657dcca6d9dSJed Brown -  m5 - number of elements to allocate in 5th chunk  (may be zero)
658d382aafbSBarry Smith 
659e28ce29aSPatrick Sanan    Output Parameters:
660d382aafbSBarry Smith +  r1 - memory allocated in first chunk
661d382aafbSBarry Smith .  r2 - memory allocated in second chunk
662d382aafbSBarry Smith .  r3 - memory allocated in third chunk
663d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
664d382aafbSBarry Smith -  r5 - memory allocated in fifth chunk
665d382aafbSBarry Smith 
666d382aafbSBarry Smith    Level: developer
667d382aafbSBarry Smith 
668db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc5()`, `PetscFree5()`
669d382aafbSBarry Smith M*/
6709371c9d4SSatish Balay #define PetscMalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
671f3fa974cSJacob Faibussowitsch   PetscMallocA(5, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5))
672d382aafbSBarry Smith 
673d382aafbSBarry Smith /*MC
67487497f52SBarry Smith    PetscCalloc5 - Allocates 5 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
675d382aafbSBarry Smith 
676eca87e8dSBarry Smith    Synopsis:
677aaa7dc30SBarry Smith     #include <petscsys.h>
6781795a4d1SJed 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)
679eca87e8dSBarry Smith 
680eca87e8dSBarry Smith    Not Collective
681eca87e8dSBarry Smith 
682e28ce29aSPatrick Sanan    Input Parameters:
683d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
684d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
685d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
686d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
6871795a4d1SJed Brown -  m5 - number of elements to allocate in 5th chunk  (may be zero)
6881795a4d1SJed Brown 
689e28ce29aSPatrick Sanan    Output Parameters:
6901795a4d1SJed Brown +  r1 - memory allocated in first chunk
6911795a4d1SJed Brown .  r2 - memory allocated in second chunk
6921795a4d1SJed Brown .  r3 - memory allocated in third chunk
6931795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
6941795a4d1SJed Brown -  r5 - memory allocated in fifth chunk
6951795a4d1SJed Brown 
6961795a4d1SJed Brown    Level: developer
6971795a4d1SJed Brown 
698db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc5()`, `PetscFree5()`
6991795a4d1SJed Brown M*/
7009371c9d4SSatish Balay #define PetscCalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
701f3fa974cSJacob Faibussowitsch   PetscMallocA(5, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5))
702d382aafbSBarry Smith 
703d382aafbSBarry Smith /*MC
70487497f52SBarry Smith    PetscMalloc6 - Allocates 6 arrays of memory, all aligned to `PETSC_MEMALIGN`
705d382aafbSBarry Smith 
706d382aafbSBarry Smith    Synopsis:
707aaa7dc30SBarry Smith     #include <petscsys.h>
708dcca6d9dSJed 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)
709d382aafbSBarry Smith 
710d382aafbSBarry Smith    Not Collective
711d382aafbSBarry Smith 
712e28ce29aSPatrick Sanan    Input Parameters:
713d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
714d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
715d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
716d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
717d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
718dcca6d9dSJed Brown -  m6 - number of elements to allocate in 6th chunk  (may be zero)
719d382aafbSBarry Smith 
720e28ce29aSPatrick Sanan    Output Parameteasr:
721d382aafbSBarry Smith +  r1 - memory allocated in first chunk
722d382aafbSBarry Smith .  r2 - memory allocated in second chunk
723d382aafbSBarry Smith .  r3 - memory allocated in third chunk
724d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
725d382aafbSBarry Smith .  r5 - memory allocated in fifth chunk
726d382aafbSBarry Smith -  r6 - memory allocated in sixth chunk
727d382aafbSBarry Smith 
728d382aafbSBarry Smith    Level: developer
729d382aafbSBarry Smith 
730db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc6()`, `PetscFree3()`, `PetscFree4()`, `PetscFree5()`, `PetscFree6()`
731d382aafbSBarry Smith M*/
7329371c9d4SSatish Balay #define PetscMalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
733f3fa974cSJacob Faibussowitsch   PetscMallocA(6, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5), ((size_t)((size_t)m6) * sizeof(**(r6))), (r6))
734d382aafbSBarry Smith 
735d382aafbSBarry Smith /*MC
73687497f52SBarry Smith    PetscCalloc6 - Allocates 6 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
737d382aafbSBarry Smith 
738eca87e8dSBarry Smith    Synopsis:
739aaa7dc30SBarry Smith     #include <petscsys.h>
7401795a4d1SJed 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)
741eca87e8dSBarry Smith 
742eca87e8dSBarry Smith    Not Collective
743eca87e8dSBarry Smith 
744e28ce29aSPatrick Sanan    Input Parameters:
745d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
746d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
747d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
748d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
749d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
7501795a4d1SJed Brown -  m6 - number of elements to allocate in 6th chunk  (may be zero)
7511795a4d1SJed Brown 
752e28ce29aSPatrick Sanan    Output Parameters:
7531795a4d1SJed Brown +  r1 - memory allocated in first chunk
7541795a4d1SJed Brown .  r2 - memory allocated in second chunk
7551795a4d1SJed Brown .  r3 - memory allocated in third chunk
7561795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
7571795a4d1SJed Brown .  r5 - memory allocated in fifth chunk
7581795a4d1SJed Brown -  r6 - memory allocated in sixth chunk
7591795a4d1SJed Brown 
7601795a4d1SJed Brown    Level: developer
7611795a4d1SJed Brown 
762db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc6()`, `PetscFree6()`
7631795a4d1SJed Brown M*/
7649371c9d4SSatish Balay #define PetscCalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
765f3fa974cSJacob Faibussowitsch   PetscMallocA(6, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5), ((size_t)((size_t)m6) * sizeof(**(r6))), (r6))
7661795a4d1SJed Brown 
7671795a4d1SJed Brown /*MC
76887497f52SBarry Smith    PetscMalloc7 - Allocates 7 arrays of memory, all aligned to `PETSC_MEMALIGN`
769d382aafbSBarry Smith 
770d382aafbSBarry Smith    Synopsis:
771aaa7dc30SBarry Smith     #include <petscsys.h>
772dcca6d9dSJed 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)
773d382aafbSBarry Smith 
774d382aafbSBarry Smith    Not Collective
775d382aafbSBarry Smith 
776e28ce29aSPatrick Sanan    Input Parameters:
777d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
778d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
779d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
780d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
781d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
782d382aafbSBarry Smith .  m6 - number of elements to allocate in 6th chunk  (may be zero)
783dcca6d9dSJed Brown -  m7 - number of elements to allocate in 7th chunk  (may be zero)
784d382aafbSBarry Smith 
785e28ce29aSPatrick Sanan    Output Parameters:
786d382aafbSBarry Smith +  r1 - memory allocated in first chunk
787d382aafbSBarry Smith .  r2 - memory allocated in second chunk
788d382aafbSBarry Smith .  r3 - memory allocated in third chunk
789d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
790d382aafbSBarry Smith .  r5 - memory allocated in fifth chunk
791d382aafbSBarry Smith .  r6 - memory allocated in sixth chunk
792eca87e8dSBarry Smith -  r7 - memory allocated in seventh chunk
793d382aafbSBarry Smith 
794d382aafbSBarry Smith    Level: developer
795d382aafbSBarry Smith 
796db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc7()`, `PetscFree7()`
797d382aafbSBarry Smith M*/
7989371c9d4SSatish Balay #define PetscMalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
799f3fa974cSJacob Faibussowitsch   PetscMallocA(7, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5), ((size_t)((size_t)m6) * sizeof(**(r6))), (r6), ((size_t)((size_t)m7) * sizeof(**(r7))), (r7))
800d382aafbSBarry Smith 
801d382aafbSBarry Smith /*MC
80287497f52SBarry Smith    PetscCalloc7 - Allocates 7 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
8031795a4d1SJed Brown 
8041795a4d1SJed Brown    Synopsis:
805aaa7dc30SBarry Smith     #include <petscsys.h>
8061795a4d1SJed 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)
8071795a4d1SJed Brown 
8081795a4d1SJed Brown    Not Collective
8091795a4d1SJed Brown 
810e28ce29aSPatrick Sanan    Input Parameters:
8111795a4d1SJed Brown +  m1 - number of elements to allocate in 1st chunk  (may be zero)
8121795a4d1SJed Brown .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
8131795a4d1SJed Brown .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
8141795a4d1SJed Brown .  m4 - number of elements to allocate in 4th chunk  (may be zero)
8151795a4d1SJed Brown .  m5 - number of elements to allocate in 5th chunk  (may be zero)
8161795a4d1SJed Brown .  m6 - number of elements to allocate in 6th chunk  (may be zero)
8171795a4d1SJed Brown -  m7 - number of elements to allocate in 7th chunk  (may be zero)
8181795a4d1SJed Brown 
819e28ce29aSPatrick Sanan    Output Parameters:
8201795a4d1SJed Brown +  r1 - memory allocated in first chunk
8211795a4d1SJed Brown .  r2 - memory allocated in second chunk
8221795a4d1SJed Brown .  r3 - memory allocated in third chunk
8231795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
8241795a4d1SJed Brown .  r5 - memory allocated in fifth chunk
8251795a4d1SJed Brown .  r6 - memory allocated in sixth chunk
8261795a4d1SJed Brown -  r7 - memory allocated in seventh chunk
8271795a4d1SJed Brown 
8281795a4d1SJed Brown    Level: developer
8291795a4d1SJed Brown 
830db781477SPatrick Sanan .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc7()`, `PetscFree7()`
8311795a4d1SJed Brown M*/
8329371c9d4SSatish Balay #define PetscCalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
833f3fa974cSJacob Faibussowitsch   PetscMallocA(7, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3), ((size_t)((size_t)m4) * sizeof(**(r4))), (r4), ((size_t)((size_t)m5) * sizeof(**(r5))), (r5), ((size_t)((size_t)m6) * sizeof(**(r6))), (r6), ((size_t)((size_t)m7) * sizeof(**(r7))), (r7))
8341795a4d1SJed Brown 
8351795a4d1SJed Brown /*MC
83687497f52SBarry Smith    PetscNew - Allocates memory of a particular type, zeros the memory! Aligned to `PETSC_MEMALIGN`
837d382aafbSBarry Smith 
838eca87e8dSBarry Smith    Synopsis:
839aaa7dc30SBarry Smith     #include <petscsys.h>
840b00a9115SJed Brown    PetscErrorCode PetscNew(type **result)
841eca87e8dSBarry Smith 
842eca87e8dSBarry Smith    Not Collective
843eca87e8dSBarry Smith 
844d382aafbSBarry Smith    Output Parameter:
845b00a9115SJed Brown .  result - memory allocated, sized to match pointer type
846d382aafbSBarry Smith 
847d382aafbSBarry Smith    Level: beginner
848d382aafbSBarry Smith 
849fea72eb4SStefano Zampini .seealso: `PetscFree()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc1()`
850d382aafbSBarry Smith M*/
851b00a9115SJed Brown #define PetscNew(b) PetscCalloc1(1, (b))
852ad0619ddSBarry Smith 
853f4f49eeaSPierre Jolivet #define PetscNewLog(o, b) PETSC_DEPRECATED_MACRO(3, 18, 0, "PetscNew()", ) PetscNew(b)
854d382aafbSBarry Smith 
855d382aafbSBarry Smith /*MC
856d382aafbSBarry Smith    PetscFree - Frees memory
857d382aafbSBarry Smith 
858eca87e8dSBarry Smith    Synopsis:
859aaa7dc30SBarry Smith     #include <petscsys.h>
860eca87e8dSBarry Smith    PetscErrorCode PetscFree(void *memory)
861eca87e8dSBarry Smith 
862eca87e8dSBarry Smith    Not Collective
863eca87e8dSBarry Smith 
864d382aafbSBarry Smith    Input Parameter:
86516a05f60SBarry Smith .   memory - memory to free (the pointer is ALWAYS set to `NULL` upon success)
866d382aafbSBarry Smith 
867d382aafbSBarry Smith    Level: beginner
868d382aafbSBarry Smith 
86995bd0b28SBarry Smith    Notes:
87087497f52SBarry Smith    Do not free memory obtained with `PetscMalloc2()`, `PetscCalloc2()` etc, they must be freed with `PetscFree2()` etc.
871390e1bf2SBarry Smith 
87216a05f60SBarry Smith    It is safe to call `PetscFree()` on a `NULL` pointer.
873d382aafbSBarry Smith 
874fea72eb4SStefano Zampini .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc1()`
875d382aafbSBarry Smith M*/
8763ba16761SJacob Faibussowitsch #define PetscFree(a) ((PetscErrorCode)((*PetscTrFree)((void *)(a), __LINE__, PETSC_FUNCTION_NAME, __FILE__) || ((a) = PETSC_NULLPTR, PETSC_SUCCESS)))
877d382aafbSBarry Smith 
878d382aafbSBarry Smith /*MC
87987497f52SBarry Smith    PetscFree2 - Frees 2 chunks of memory obtained with `PetscMalloc2()`
880d382aafbSBarry Smith 
881eca87e8dSBarry Smith    Synopsis:
882aaa7dc30SBarry Smith     #include <petscsys.h>
883eca87e8dSBarry Smith    PetscErrorCode PetscFree2(void *memory1,void *memory2)
884eca87e8dSBarry Smith 
885eca87e8dSBarry Smith    Not Collective
886eca87e8dSBarry Smith 
887e28ce29aSPatrick Sanan    Input Parameters:
888d382aafbSBarry Smith +   memory1 - memory to free
889d382aafbSBarry Smith -   memory2 - 2nd memory to free
890d382aafbSBarry Smith 
891d382aafbSBarry Smith    Level: developer
892d382aafbSBarry Smith 
8937e17494fSJames Wright    Notes:
89487497f52SBarry Smith     Memory must have been obtained with `PetscMalloc2()`
895d382aafbSBarry Smith 
8967e17494fSJames Wright     The arguments need to be in the same order as they were in the call to `PetscMalloc2()`
8977e17494fSJames Wright 
898db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`
899d382aafbSBarry Smith M*/
900ba282f50SJed Brown #define PetscFree2(m1, m2) PetscFreeA(2, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2))
901d382aafbSBarry Smith 
902d382aafbSBarry Smith /*MC
90387497f52SBarry Smith    PetscFree3 - Frees 3 chunks of memory obtained with `PetscMalloc3()`
904d382aafbSBarry Smith 
905eca87e8dSBarry Smith    Synopsis:
906aaa7dc30SBarry Smith     #include <petscsys.h>
907eca87e8dSBarry Smith    PetscErrorCode PetscFree3(void *memory1,void *memory2,void *memory3)
908eca87e8dSBarry Smith 
909eca87e8dSBarry Smith    Not Collective
910eca87e8dSBarry Smith 
911e28ce29aSPatrick Sanan    Input Parameters:
912d382aafbSBarry Smith +   memory1 - memory to free
913d382aafbSBarry Smith .   memory2 - 2nd memory to free
914d382aafbSBarry Smith -   memory3 - 3rd memory to free
915d382aafbSBarry Smith 
916d382aafbSBarry Smith    Level: developer
917d382aafbSBarry Smith 
9187e17494fSJames Wright    Notes:
91987497f52SBarry Smith     Memory must have been obtained with `PetscMalloc3()`
920d382aafbSBarry Smith 
9217e17494fSJames Wright     The arguments need to be in the same order as they were in the call to `PetscMalloc3()`
9227e17494fSJames Wright 
923db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`
924d382aafbSBarry Smith M*/
925ba282f50SJed Brown #define PetscFree3(m1, m2, m3) PetscFreeA(3, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3))
926d382aafbSBarry Smith 
927d382aafbSBarry Smith /*MC
92887497f52SBarry Smith    PetscFree4 - Frees 4 chunks of memory obtained with `PetscMalloc4()`
929d382aafbSBarry Smith 
930eca87e8dSBarry Smith    Synopsis:
931aaa7dc30SBarry Smith     #include <petscsys.h>
932eca87e8dSBarry Smith    PetscErrorCode PetscFree4(void *m1,void *m2,void *m3,void *m4)
933eca87e8dSBarry Smith 
934eca87e8dSBarry Smith    Not Collective
935eca87e8dSBarry Smith 
936e28ce29aSPatrick Sanan    Input Parameters:
937d382aafbSBarry Smith +   m1 - memory to free
938d382aafbSBarry Smith .   m2 - 2nd memory to free
939d382aafbSBarry Smith .   m3 - 3rd memory to free
940d382aafbSBarry Smith -   m4 - 4th memory to free
941d382aafbSBarry Smith 
942d382aafbSBarry Smith    Level: developer
943d382aafbSBarry Smith 
9447e17494fSJames Wright    Notes:
94587497f52SBarry Smith     Memory must have been obtained with `PetscMalloc4()`
946d382aafbSBarry Smith 
9477e17494fSJames Wright     The arguments need to be in the same order as they were in the call to `PetscMalloc4()`
9487e17494fSJames Wright 
949db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`
950d382aafbSBarry Smith M*/
951ba282f50SJed Brown #define PetscFree4(m1, m2, m3, m4) PetscFreeA(4, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4))
952d382aafbSBarry Smith 
953d382aafbSBarry Smith /*MC
95487497f52SBarry Smith    PetscFree5 - Frees 5 chunks of memory obtained with `PetscMalloc5()`
955d382aafbSBarry Smith 
956eca87e8dSBarry Smith    Synopsis:
957aaa7dc30SBarry Smith     #include <petscsys.h>
958eca87e8dSBarry Smith    PetscErrorCode PetscFree5(void *m1,void *m2,void *m3,void *m4,void *m5)
959eca87e8dSBarry Smith 
960eca87e8dSBarry Smith    Not Collective
961eca87e8dSBarry Smith 
962e28ce29aSPatrick Sanan    Input Parameters:
963d382aafbSBarry Smith +   m1 - memory to free
964d382aafbSBarry Smith .   m2 - 2nd memory to free
965d382aafbSBarry Smith .   m3 - 3rd memory to free
966d382aafbSBarry Smith .   m4 - 4th memory to free
967d382aafbSBarry Smith -   m5 - 5th memory to free
968d382aafbSBarry Smith 
969d382aafbSBarry Smith    Level: developer
970d382aafbSBarry Smith 
9717e17494fSJames Wright    Notes:
97287497f52SBarry Smith     Memory must have been obtained with `PetscMalloc5()`
973d382aafbSBarry Smith 
9747e17494fSJames Wright     The arguments need to be in the same order as they were in the call to `PetscMalloc5()`
9757e17494fSJames Wright 
976db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`
977d382aafbSBarry Smith M*/
978ba282f50SJed Brown #define PetscFree5(m1, m2, m3, m4, m5) PetscFreeA(5, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5))
979d382aafbSBarry Smith 
980d382aafbSBarry Smith /*MC
98187497f52SBarry Smith    PetscFree6 - Frees 6 chunks of memory obtained with `PetscMalloc6()`
982d382aafbSBarry Smith 
983eca87e8dSBarry Smith    Synopsis:
984aaa7dc30SBarry Smith     #include <petscsys.h>
985eca87e8dSBarry Smith    PetscErrorCode PetscFree6(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6)
986eca87e8dSBarry Smith 
987eca87e8dSBarry Smith    Not Collective
988eca87e8dSBarry Smith 
989e28ce29aSPatrick Sanan    Input Parameters:
990d382aafbSBarry Smith +   m1 - memory to free
991d382aafbSBarry Smith .   m2 - 2nd memory to free
992d382aafbSBarry Smith .   m3 - 3rd memory to free
993d382aafbSBarry Smith .   m4 - 4th memory to free
994d382aafbSBarry Smith .   m5 - 5th memory to free
995d382aafbSBarry Smith -   m6 - 6th memory to free
996d382aafbSBarry Smith 
997d382aafbSBarry Smith    Level: developer
998d382aafbSBarry Smith 
9997e17494fSJames Wright    Notes:
100087497f52SBarry Smith     Memory must have been obtained with `PetscMalloc6()`
1001d382aafbSBarry Smith 
10027e17494fSJames Wright     The arguments need to be in the same order as they were in the call to `PetscMalloc6()`
10037e17494fSJames Wright 
1004db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`
1005d382aafbSBarry Smith M*/
1006ba282f50SJed Brown #define PetscFree6(m1, m2, m3, m4, m5, m6) PetscFreeA(6, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5), &(m6))
1007d382aafbSBarry Smith 
1008d382aafbSBarry Smith /*MC
100987497f52SBarry Smith    PetscFree7 - Frees 7 chunks of memory obtained with `PetscMalloc7()`
1010d382aafbSBarry Smith 
1011eca87e8dSBarry Smith    Synopsis:
1012aaa7dc30SBarry Smith     #include <petscsys.h>
1013eca87e8dSBarry Smith    PetscErrorCode PetscFree7(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6,void *m7)
1014eca87e8dSBarry Smith 
1015eca87e8dSBarry Smith    Not Collective
1016eca87e8dSBarry Smith 
1017e28ce29aSPatrick Sanan    Input Parameters:
1018d382aafbSBarry Smith +   m1 - memory to free
1019d382aafbSBarry Smith .   m2 - 2nd memory to free
1020d382aafbSBarry Smith .   m3 - 3rd memory to free
1021d382aafbSBarry Smith .   m4 - 4th memory to free
1022d382aafbSBarry Smith .   m5 - 5th memory to free
1023d382aafbSBarry Smith .   m6 - 6th memory to free
1024d382aafbSBarry Smith -   m7 - 7th memory to free
1025d382aafbSBarry Smith 
1026d382aafbSBarry Smith    Level: developer
1027d382aafbSBarry Smith 
10287e17494fSJames Wright    Notes:
102987497f52SBarry Smith     Memory must have been obtained with `PetscMalloc7()`
1030d382aafbSBarry Smith 
10317e17494fSJames Wright     The arguments need to be in the same order as they were in the call to `PetscMalloc7()`
10327e17494fSJames Wright 
1033db781477SPatrick Sanan .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`,
1034db781477SPatrick Sanan           `PetscMalloc7()`
1035d382aafbSBarry Smith M*/
1036ba282f50SJed Brown #define PetscFree7(m1, m2, m3, m4, m5, m6, m7) PetscFreeA(7, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5), &(m6), &(m7))
1037d382aafbSBarry Smith 
1038ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocA(int, PetscBool, int, const char *, const char *, size_t, void *, ...);
1039ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscFreeA(int, int, const char *, const char *, void *, ...);
1040071fcb05SBarry Smith PETSC_EXTERN                PetscErrorCode (*PetscTrMalloc)(size_t, PetscBool, int, const char[], const char[], void **);
1041efca3c55SSatish Balay PETSC_EXTERN                PetscErrorCode (*PetscTrFree)(void *, int, const char[], const char[]);
10423221ece2SMatthew G. Knepley PETSC_EXTERN                PetscErrorCode (*PetscTrRealloc)(size_t, int, const char[], const char[], void **);
1043ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocSetCoalesce(PetscBool);
104492f119d6SBarry 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 **));
1045014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocClear(void);
1046d382aafbSBarry Smith 
1047d382aafbSBarry Smith /*
1048c1706be6SHong 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.
1049c1706be6SHong Zhang */
1050c1706be6SHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocSetDRAM(void);
1051c1706be6SHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocResetDRAM(void);
105231f06eaaSHong Zhang #if defined(PETSC_HAVE_CUDA)
105331f06eaaSHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocSetCUDAHost(void);
105431f06eaaSHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocResetCUDAHost(void);
105531f06eaaSHong Zhang #endif
105659af0bd3SScott Kruger #if defined(PETSC_HAVE_HIP)
105759af0bd3SScott Kruger PETSC_EXTERN PetscErrorCode PetscMallocSetHIPHost(void);
105859af0bd3SScott Kruger PETSC_EXTERN PetscErrorCode PetscMallocResetHIPHost(void);
105959af0bd3SScott Kruger #endif
106059af0bd3SScott Kruger 
1061a5057860SBarry Smith #define MPIU_PETSCLOGDOUBLE  MPI_DOUBLE
106236763ca0SBas van 't Hof #define MPIU_2PETSCLOGDOUBLE MPI_2DOUBLE_PRECISION
1063a5057860SBarry Smith 
1064a5057860SBarry Smith /*
106566d669d6SBarry Smith    Routines for tracing memory corruption/bleeding with default PETSc memory allocation
1066d382aafbSBarry Smith */
1067014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocDump(FILE *);
106892f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocView(FILE *);
1069014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocGetCurrentUsage(PetscLogDouble *);
1070014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocGetMaximumUsage(PetscLogDouble *);
1071e3ed9ee7SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocPushMaximumUsage(int);
1072e3ed9ee7SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocPopMaximumUsage(int, PetscLogDouble *);
107392f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocSetDebug(PetscBool, PetscBool);
107492f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocGetDebug(PetscBool *, PetscBool *, PetscBool *);
1075efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscMallocValidate(int, const char[], const char[]);
107692f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocViewSet(PetscLogDouble);
107792f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocViewGet(PetscBool *);
1078608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeSet(PetscBool);
1079608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeGet(PetscBool *);
1080d382aafbSBarry Smith 
1081014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType, MPI_Datatype *);
1082014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIDataTypeToPetscDataType(MPI_Datatype, PetscDataType *);
1083014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDataTypeGetSize(PetscDataType, size_t *);
10848e4b2d1dSBarry Smith PETSC_EXTERN PetscErrorCode PetscDataTypeFromString(const char *, PetscDataType *, PetscBool *);
1085d382aafbSBarry Smith 
1086d382aafbSBarry Smith /*
1087d382aafbSBarry Smith    These are MPI operations for MPI_Allreduce() etc
1088d382aafbSBarry Smith */
1089367daffbSBarry Smith PETSC_EXTERN MPI_Op MPIU_MAXSUM_OP;
1090570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
1091de272c7aSSatish Balay PETSC_EXTERN MPI_Op MPIU_SUM;
1092014dd563SJed Brown PETSC_EXTERN MPI_Op MPIU_MAX;
1093014dd563SJed Brown PETSC_EXTERN MPI_Op MPIU_MIN;
1094d9822059SBarry Smith #else
1095de272c7aSSatish Balay   #define MPIU_SUM MPI_SUM
1096d9822059SBarry Smith   #define MPIU_MAX MPI_MAX
1097d9822059SBarry Smith   #define MPIU_MIN MPI_MIN
1098d9822059SBarry Smith #endif
109962e5d2d2SJDBetteridge PETSC_EXTERN MPI_Op         Petsc_Garbage_SetIntersectOp;
110062e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscMaxSum(MPI_Comm, const PetscInt[], PetscInt *, PetscInt *);
110162e5d2d2SJDBetteridge 
1102a2498233SPierre Jolivet #if (defined(PETSC_HAVE_REAL___FLOAT128) && !defined(PETSC_SKIP_REAL___FLOAT128)) || (defined(PETSC_HAVE_REAL___FP16) && !defined(PETSC_SKIP_REAL___FP16))
1103613bf2b2SPierre Jolivet /*MC
110416a05f60SBarry Smith    MPIU_SUM___FP16___FLOAT128 - MPI_Op that acts as a replacement for `MPI_SUM` with
110516a05f60SBarry Smith    custom `MPI_Datatype` `MPIU___FLOAT128`, `MPIU___COMPLEX128`, and `MPIU___FP16`.
1106613bf2b2SPierre Jolivet 
1107613bf2b2SPierre Jolivet    Level: advanced
1108613bf2b2SPierre Jolivet 
1109613bf2b2SPierre Jolivet    Developer Note:
1110613bf2b2SPierre Jolivet    This should be unified with `MPIU_SUM`
1111613bf2b2SPierre Jolivet 
1112613bf2b2SPierre Jolivet .seealso: `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`
1113613bf2b2SPierre Jolivet M*/
11149e517322SPierre Jolivet PETSC_EXTERN MPI_Op MPIU_SUM___FP16___FLOAT128;
1115613bf2b2SPierre Jolivet #endif
1116014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMaxSum(MPI_Comm, const PetscInt[], PetscInt *, PetscInt *);
1117d382aafbSBarry Smith 
111893d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIULong_Send(void *, PetscInt, MPI_Datatype, PetscMPIInt, PetscMPIInt, MPI_Comm) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(1, 3);
111993d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIULong_Recv(void *, PetscInt, MPI_Datatype, PetscMPIInt, PetscMPIInt, MPI_Comm) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(1, 3);
1120eb3f98d2SBarry Smith 
1121639ff905SBarry Smith /*
1122639ff905SBarry Smith     Defines PETSc error handling.
1123639ff905SBarry Smith */
1124639ff905SBarry Smith #include <petscerror.h>
1125d382aafbSBarry Smith 
1126660278c0SBarry Smith PETSC_EXTERN PetscBool   PetscCIEnabled;                    /* code is running in the PETSc test harness CI */
1127660278c0SBarry Smith PETSC_EXTERN PetscBool   PetscCIEnabledPortableErrorOutput; /* error output is stripped to ensure portability of error messages across systems */
1128660278c0SBarry Smith PETSC_EXTERN const char *PetscCIFilename(const char *);
1129660278c0SBarry Smith PETSC_EXTERN int         PetscCILinenumber(int);
1130660278c0SBarry Smith 
1131f3fa974cSJacob Faibussowitsch #define PETSC_SMALLEST_CLASSID ((PetscClassId)1211211)
1132014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_LARGEST_CLASSID;
1133014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_OBJECT_CLASSID;
1134014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscClassIdRegister(const char[], PetscClassId *);
113581256985SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscObjectGetId(PetscObject, PetscObjectId *);
113681256985SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscObjectCompareId(PetscObject, PetscObjectId, PetscBool *);
113781256985SMatthew G. Knepley 
1138d382aafbSBarry Smith /*
1139d382aafbSBarry Smith    Routines that get memory usage information from the OS
1140d382aafbSBarry Smith */
1141014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemoryGetCurrentUsage(PetscLogDouble *);
1142014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemoryGetMaximumUsage(PetscLogDouble *);
1143014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemorySetGetMaximumUsage(void);
1144b44d5720SBarry Smith PETSC_EXTERN PetscErrorCode PetscMemoryTrace(const char[]);
1145d382aafbSBarry Smith 
1146014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSleep(PetscReal);
1147d382aafbSBarry Smith 
1148d382aafbSBarry Smith /*
1149d382aafbSBarry Smith    Initialization of PETSc
1150d382aafbSBarry Smith */
1151014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitialize(int *, char ***, const char[], const char[]);
1152014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeNoPointers(int, char **, const char[], const char[]);
1153014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeNoArguments(void);
1154014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitialized(PetscBool *);
1155014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFinalized(PetscBool *);
1156014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFinalize(void);
1157014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeFortran(void);
1158014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArgs(int *, char ***);
1159014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArguments(char ***);
1160014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFreeArguments(char **);
1161d382aafbSBarry Smith 
1162014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscEnd(void);
1163607a6623SBarry Smith PETSC_EXTERN PetscErrorCode PetscSysInitializePackage(void);
11644bf303faSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSysFinalizePackage(void);
1165d382aafbSBarry Smith 
1166014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonInitialize(const char[], const char[]);
1167014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonFinalize(void);
1168014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonPrintError(void);
1169014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonMonitorSet(PetscObject, const char[]);
1170d382aafbSBarry Smith 
1171a50e088cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMonitorCompare(PetscErrorCode (*)(void), void *, PetscErrorCode (*)(void **), PetscErrorCode (*)(void), void *, PetscErrorCode (*)(void **), PetscBool *);
1172a50e088cSMatthew G. Knepley 
1173d382aafbSBarry Smith /*
1174d382aafbSBarry Smith      These are so that in extern C code we can caste function pointers to non-extern C
11752981ebdbSBarry Smith    function pointers. Since the regular C++ code expects its function pointers to be C++
1176d382aafbSBarry Smith */
11775ddc805bSBarry Smith 
11785ddc805bSBarry Smith /*S
11798434afd1SBarry Smith   PetscVoidFn - A prototype of a void (fn)(void) function
11805ddc805bSBarry Smith 
11815ddc805bSBarry Smith   Level: developer
11825ddc805bSBarry Smith 
11835ddc805bSBarry Smith   Notes:
11848434afd1SBarry Smith   The deprecated `PetscVoidFunction` works as a replacement for `PetscVoidFn` *.
11855ddc805bSBarry Smith 
11868434afd1SBarry Smith   The deprecated `PetscVoidStarFunction` works as a replacement for `PetscVoidFn` **.
11875ddc805bSBarry Smith 
11885ddc805bSBarry Smith .seealso: `PetscObject`, `PetscObjectDestroy()`
11895ddc805bSBarry Smith S*/
11908434afd1SBarry Smith PETSC_EXTERN_TYPEDEF typedef void(PetscVoidFn)(void);
11915ddc805bSBarry Smith 
11928434afd1SBarry Smith PETSC_EXTERN_TYPEDEF typedef PetscVoidFn  *PetscVoidFunction;
11938434afd1SBarry Smith PETSC_EXTERN_TYPEDEF typedef PetscVoidFn **PetscVoidStarFunction;
11945ddc805bSBarry Smith 
11955ddc805bSBarry Smith /*S
11968434afd1SBarry Smith   PetscErrorCodeFn - A prototype of a PetscErrorCode (fn)(void) function
11975ddc805bSBarry Smith 
11985ddc805bSBarry Smith   Level: developer
11995ddc805bSBarry Smith 
12005ddc805bSBarry Smith   Notes:
12018434afd1SBarry Smith   The deprecated `PetscErrorCodeFunction` works as a replacement for `PetscErrorCodeFn` *.
12025ddc805bSBarry Smith 
12035ddc805bSBarry Smith .seealso: `PetscObject`, `PetscObjectDestroy()`
12045ddc805bSBarry Smith S*/
12058434afd1SBarry Smith PETSC_EXTERN_TYPEDEF typedef PetscErrorCode(PetscErrorCodeFn)(void);
12065ddc805bSBarry Smith 
12078434afd1SBarry Smith PETSC_EXTERN_TYPEDEF typedef PetscErrorCodeFn *PetscErrorCodeFunction;
1208d382aafbSBarry Smith 
1209d382aafbSBarry Smith /*
1210d382aafbSBarry Smith     Functions that can act on any PETSc object.
1211d382aafbSBarry Smith */
1212014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectDestroy(PetscObject *);
1213014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetComm(PetscObject, MPI_Comm *);
1214014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetClassId(PetscObject, PetscClassId *);
1215014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetClassName(PetscObject, const char *[]);
1216014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetType(PetscObject, const char *[]);
1217014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetName(PetscObject, const char[]);
1218014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetName(PetscObject, const char *[]);
1219014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetTabLevel(PetscObject, PetscInt);
1220014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetTabLevel(PetscObject, PetscInt *);
1221014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectIncrementTabLevel(PetscObject, PetscObject, PetscInt);
1222014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectReference(PetscObject);
1223014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetReference(PetscObject, PetscInt *);
1224014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectDereference(PetscObject);
1225014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetNewTag(PetscObject, PetscMPIInt *);
1226014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectCompose(PetscObject, const char[], PetscObject);
1227014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRemoveReference(PetscObject, const char[]);
1228014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectQuery(PetscObject, const char[], PetscObject *);
1229bdf89e91SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectComposeFunction_Private(PetscObject, const char[], void (*)(void));
12308434afd1SBarry Smith #define PetscObjectComposeFunction(a, b, ...) PetscObjectComposeFunction_Private((a), (b), (PetscVoidFn *)(__VA_ARGS__))
1231014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetFromOptions(PetscObject);
1232014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetUp(PetscObject);
12330eb63584SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSetPrintedOptions(PetscObject);
12340eb63584SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectInheritPrintedOptions(PetscObject, PetscObject);
1235014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommGetNewTag(MPI_Comm, PetscMPIInt *);
1236d382aafbSBarry Smith 
1237665c2dedSJed Brown #include <petscviewertypes.h>
1238639ff905SBarry Smith #include <petscoptions.h>
1239639ff905SBarry Smith 
1240608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocTraceSet(PetscViewer, PetscBool, PetscLogDouble);
1241608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocTraceGet(PetscBool *);
1242608c71bfSMatthew G. Knepley 
12430633abcbSJed Brown PETSC_EXTERN PetscErrorCode PetscObjectsListGetGlobalNumbering(MPI_Comm, PetscInt, PetscObject *, PetscInt *, PetscInt *);
12440633abcbSJed Brown 
1245c5e4d11fSDmitry Karpeev PETSC_EXTERN PetscErrorCode PetscMemoryView(PetscViewer, const char[]);
1246dae58748SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject, PetscViewer);
1247639ff905SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectView(PetscObject, PetscViewer);
12488434afd1SBarry Smith #define PetscObjectQueryFunction(obj, name, fptr) PetscObjectQueryFunction_Private((obj), (name), (PetscVoidFn **)(fptr))
12490005d66cSJed Brown PETSC_EXTERN PetscErrorCode PetscObjectQueryFunction_Private(PetscObject, const char[], void (**)(void));
1250014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetOptionsPrefix(PetscObject, const char[]);
1251014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject, const char[]);
1252014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject, const char[]);
1253014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject, const char *[]);
1254014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectChangeTypeName(PetscObject, const char[]);
1255014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroy(PetscObject);
1256014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroyAll(void);
1257685405a1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectViewFromOptions(PetscObject, PetscObject, const char[]);
1258014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectName(PetscObject);
1259014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectTypeCompare(PetscObject, const char[], PetscBool *);
1260013e2dc7SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectObjectTypeCompare(PetscObject, PetscObject, PetscBool *);
12614099cc6bSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompare(PetscObject, const char[], PetscBool *);
1262014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1263b9e7e5c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1264014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*)(void));
1265014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRegisterFinalizeAll(void);
1266d382aafbSBarry Smith 
1267e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
12687aab2a10SBarry Smith PETSC_EXTERN PetscErrorCode PetscSAWsBlock(void);
1269e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsViewOff(PetscObject);
1270e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsSetBlock(PetscObject, PetscBool);
1271e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsBlock(PetscObject);
1272e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsGrantAccess(PetscObject);
1273e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsTakeAccess(PetscObject);
1274e04113cfSBarry Smith PETSC_EXTERN void           PetscStackSAWsGrantAccess(void);
1275e04113cfSBarry Smith PETSC_EXTERN void           PetscStackSAWsTakeAccess(void);
1276e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscStackViewSAWs(void);
1277e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscStackSAWsViewOff(void);
127815681b3cSBarry Smith 
1279ec7429eaSBarry Smith #else
12803ba16761SJacob Faibussowitsch   #define PetscSAWsBlock()                  PETSC_SUCCESS
12813ba16761SJacob Faibussowitsch   #define PetscObjectSAWsViewOff(obj)       PETSC_SUCCESS
12823ba16761SJacob Faibussowitsch   #define PetscObjectSAWsSetBlock(obj, flg) PETSC_SUCCESS
12833ba16761SJacob Faibussowitsch   #define PetscObjectSAWsBlock(obj)         PETSC_SUCCESS
12843ba16761SJacob Faibussowitsch   #define PetscObjectSAWsGrantAccess(obj)   PETSC_SUCCESS
12853ba16761SJacob Faibussowitsch   #define PetscObjectSAWsTakeAccess(obj)    PETSC_SUCCESS
12863ba16761SJacob Faibussowitsch   #define PetscStackViewSAWs()              PETSC_SUCCESS
12873ba16761SJacob Faibussowitsch   #define PetscStackSAWsViewOff()           PETSC_SUCCESS
1288e04113cfSBarry Smith   #define PetscStackSAWsTakeAccess()
1289e04113cfSBarry Smith   #define PetscStackSAWsGrantAccess()
129015681b3cSBarry Smith 
1291ec7429eaSBarry Smith #endif
1292b90c6cbeSBarry Smith 
129382b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLOpen(const char[], PetscDLMode, PetscDLHandle *);
129482b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLClose(PetscDLHandle *);
129582b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLSym(PetscDLHandle, const char[], void **);
1296258ec3d2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscDLAddr(void (*)(void), char **);
1297258ec3d2SMatthew G. Knepley #ifdef PETSC_HAVE_CXX
1298258ec3d2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscDemangleSymbol(const char *, char **);
1299258ec3d2SMatthew G. Knepley #endif
13007d5d4d99SBarry Smith 
1301a64a8e02SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocGetStack(void *, PetscStack **);
1302a64a8e02SBarry Smith 
1303dfb7d7afSStefano Zampini PETSC_EXTERN PetscErrorCode PetscObjectsDump(FILE *, PetscBool);
13044bf303faSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscObjectsView(PetscViewer);
13054bf303faSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscObjectsGetObject(const char *, PetscObject *, char **);
1306140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListDestroy(PetscObjectList *);
1307140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListFind(PetscObjectList, const char[], PetscObject *);
1308140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListReverseFind(PetscObjectList, PetscObject, char **, PetscBool *);
1309140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListAdd(PetscObjectList *, const char[], PetscObject);
1310140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListRemoveReference(PetscObjectList *, const char[]);
1311140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListDuplicate(PetscObjectList, PetscObjectList *);
1312d382aafbSBarry Smith 
1313d382aafbSBarry Smith /*
1314503cfb0cSBarry Smith     Dynamic library lists. Lists of names of routines in objects or in dynamic
1315d382aafbSBarry Smith   link libraries that will be loaded as needed.
1316d382aafbSBarry Smith */
1317a240a19fSJed Brown 
13188434afd1SBarry Smith #define PetscFunctionListAdd(list, name, fptr) PetscFunctionListAdd_Private((list), (name), (PetscVoidFn *)(fptr))
13198434afd1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *, const char[], PetscVoidFn *);
1320140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListDestroy(PetscFunctionList *);
13210e6b6b59SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFunctionListClear(PetscFunctionList);
13228434afd1SBarry Smith #define PetscFunctionListFind(list, name, fptr) PetscFunctionListFind_Private((list), (name), (PetscVoidFn **)(fptr))
13238434afd1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList, const char[], PetscVoidFn **);
132444ef3d73SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListPrintTypes(MPI_Comm, FILE *, const char[], const char[], const char[], const char[], PetscFunctionList, const char[], const char[]);
1325140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListDuplicate(PetscFunctionList, PetscFunctionList *);
1326140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListView(PetscFunctionList, PetscViewer);
1327140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListGet(PetscFunctionList, const char ***, int *);
13282e956fe4SStefano Zampini PETSC_EXTERN PetscErrorCode PetscFunctionListPrintNonEmpty(PetscFunctionList);
13292e956fe4SStefano Zampini PETSC_EXTERN PetscErrorCode PetscFunctionListPrintAll(void);
1330d382aafbSBarry Smith 
1331014dd563SJed Brown PETSC_EXTERN PetscDLLibrary PetscDLLibrariesLoaded;
1332014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryAppend(MPI_Comm, PetscDLLibrary *, const char[]);
1333014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryPrepend(MPI_Comm, PetscDLLibrary *, const char[]);
1334014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibrarySym(MPI_Comm, PetscDLLibrary *, const char[], const char[], void **);
1335014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryPrintPath(PetscDLLibrary);
1336014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryRetrieve(MPI_Comm, const char[], char *, size_t, PetscBool *);
1337014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryOpen(MPI_Comm, const char[], PetscDLLibrary *);
1338014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryClose(PetscDLLibrary);
1339d382aafbSBarry Smith 
1340d382aafbSBarry Smith /*
1341d382aafbSBarry Smith      Useful utility routines
1342d382aafbSBarry Smith */
1343014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSplitOwnership(MPI_Comm, PetscInt *, PetscInt *);
1344014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSplitOwnershipBlock(MPI_Comm, PetscInt, PetscInt *, PetscInt *);
1345d24d4204SJose E. Roman PETSC_EXTERN PetscErrorCode PetscSplitOwnershipEqual(MPI_Comm, PetscInt *, PetscInt *);
1346014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSequentialPhaseBegin(MPI_Comm, PetscMPIInt);
1347014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSequentialPhaseEnd(MPI_Comm, PetscMPIInt);
1348014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBarrier(PetscObject);
1349014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIDump(FILE *);
135058b5cd2aSSatish Balay PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxInt(MPI_Comm, const PetscInt[2], PetscInt[2]);
135158b5cd2aSSatish Balay PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxReal(MPI_Comm, const PetscReal[2], PetscReal[2]);
1352d382aafbSBarry Smith 
135364ac3b0dSPatrick Sanan /*MC
135487497f52SBarry Smith     PetscNot - negates a logical type value and returns result as a `PetscBool`
1355503cfb0cSBarry Smith 
135664ac3b0dSPatrick Sanan     Level: beginner
135764ac3b0dSPatrick Sanan 
1358a1cb98faSBarry Smith     Note:
1359a1cb98faSBarry Smith     This is useful in cases like
1360a1cb98faSBarry Smith .vb
1361a1cb98faSBarry Smith      int        *a;
1362a1cb98faSBarry Smith      PetscBool  flag = PetscNot(a)
1363a1cb98faSBarry Smith .ve
1364a1cb98faSBarry Smith      where !a would not return a `PetscBool` because we cannot provide a cast from int to `PetscBool` in C.
1365a1cb98faSBarry Smith 
1366a1cb98faSBarry Smith .seealso: `PetscBool`, `PETSC_TRUE`, `PETSC_FALSE`
136764ac3b0dSPatrick Sanan M*/
1368d382aafbSBarry Smith #define PetscNot(a) ((a) ? PETSC_FALSE : PETSC_TRUE)
1369503cfb0cSBarry Smith 
1370d382aafbSBarry Smith /*MC
1371d382aafbSBarry Smith    PetscHelpPrintf - Prints help messages.
1372d382aafbSBarry Smith 
1373d382aafbSBarry Smith    Synopsis:
1374aaa7dc30SBarry Smith     #include <petscsys.h>
1375659f7ba0SBarry Smith      PetscErrorCode (*PetscHelpPrintf)(MPI_Comm comm, const char format[],args);
1376d382aafbSBarry Smith 
13777cdbe19fSJose E. Roman    Not Collective, only applies on MPI rank 0; No Fortran Support
13787cdbe19fSJose E. Roman 
1379d382aafbSBarry Smith    Input Parameters:
1380659f7ba0SBarry Smith +  comm - the MPI communicator over which the help message is printed
1381d382aafbSBarry Smith .  format - the usual printf() format string
1382659f7ba0SBarry Smith -  args - arguments to be printed
1383d382aafbSBarry Smith 
1384d382aafbSBarry Smith    Level: developer
1385d382aafbSBarry Smith 
138695bd0b28SBarry Smith    Notes:
1387659f7ba0SBarry Smith    You can change how help messages are printed by replacing the function pointer with a function that does not simply write to stdout.
1388659f7ba0SBarry Smith 
1389659f7ba0SBarry Smith    To use, write your own function, for example,
139016a05f60SBarry Smith .vb
139116a05f60SBarry Smith    PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....)
139216a05f60SBarry Smith    {
139316a05f60SBarry Smith      PetscFunctionReturn(PETSC_SUCCESS);
139416a05f60SBarry Smith    }
139516a05f60SBarry Smith .ve
1396d5b43468SJose E. Roman then do the assignment
1397af27ebaaSBarry Smith .vb
1398af27ebaaSBarry Smith   PetscHelpPrintf = mypetschelpprintf;
1399af27ebaaSBarry Smith .ve
1400af27ebaaSBarry Smith 
140187497f52SBarry Smith   You can do the assignment before `PetscInitialize()`.
1402659f7ba0SBarry Smith 
140387497f52SBarry Smith   The default routine used is called `PetscHelpPrintfDefault()`.
1404d382aafbSBarry Smith 
140516a05f60SBarry Smith .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscErrorPrintf()`, `PetscHelpPrintfDefault()`
1406d382aafbSBarry Smith M*/
14073ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode (*PetscHelpPrintf)(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1408d382aafbSBarry Smith 
1409fcfd50ebSBarry Smith /*
1410fcfd50ebSBarry Smith      Defines PETSc profiling.
1411fcfd50ebSBarry Smith */
14122c8e378dSBarry Smith #include <petsclog.h>
1413fcfd50ebSBarry Smith 
1414fcfd50ebSBarry Smith /*
1415fcfd50ebSBarry Smith       Simple PETSc parallel IO for ASCII printing
1416fcfd50ebSBarry Smith */
1417014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFixFilename(const char[], char[]);
1418014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFOpen(MPI_Comm, const char[], const char[], FILE **);
1419014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFClose(MPI_Comm, FILE *);
14203ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
1421c69effb2SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFFlush(FILE *);
14223ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
14233ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSNPrintf(char *, size_t, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
14243ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSNPrintfCount(char *, size_t, const char[], size_t *, ...) PETSC_ATTRIBUTE_FORMAT(3, 5);
14251b5687a1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatRealArray(char[], size_t, const char *, PetscInt, const PetscReal[]);
1426fcfd50ebSBarry Smith 
14273ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscErrorPrintfDefault(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
14283ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscErrorPrintfNone(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
14293ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscHelpPrintfDefault(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1430d382aafbSBarry Smith 
1431d781fa04SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatConvertGetSize(const char *, size_t *);
1432d781fa04SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatConvert(const char *, char *);
1433d781fa04SBarry Smith 
1434d382aafbSBarry Smith #if defined(PETSC_HAVE_POPEN)
1435014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPOpen(MPI_Comm, const char[], const char[], const char[], FILE **);
1436016831caSBarry Smith PETSC_EXTERN PetscErrorCode PetscPClose(MPI_Comm, FILE *);
143774ba8654SBarry Smith PETSC_EXTERN PetscErrorCode PetscPOpenSetMachine(const char[]);
1438d382aafbSBarry Smith #endif
1439d382aafbSBarry Smith 
14403ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSynchronizedPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
14413ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
14420ec8b6e3SBarry Smith PETSC_EXTERN PetscErrorCode PetscSynchronizedFlush(MPI_Comm, FILE *);
1443014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSynchronizedFGets(MPI_Comm, FILE *, size_t, char[]);
1444014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStartMatlab(MPI_Comm, const char[], const char[], FILE **);
1445014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetPetscDir(const char *[]);
1446d382aafbSBarry Smith 
1447014dd563SJed Brown PETSC_EXTERN PetscClassId   PETSC_CONTAINER_CLASSID;
1448014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerGetPointer(PetscContainer, void **);
1449014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerSetPointer(PetscContainer, void *);
1450014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerDestroy(PetscContainer *);
1451014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerCreate(MPI_Comm, PetscContainer *);
1452014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerSetUserDestroy(PetscContainer, PetscErrorCode (*)(void *));
1453b61ba218SStefano Zampini PETSC_EXTERN PetscErrorCode PetscContainerUserDestroyDefault(void *);
1454d382aafbSBarry Smith 
1455d382aafbSBarry Smith /*
1456d382aafbSBarry Smith    For use in debuggers
1457d382aafbSBarry Smith */
1458014dd563SJed Brown PETSC_EXTERN PetscMPIInt    PetscGlobalRank;
1459014dd563SJed Brown PETSC_EXTERN PetscMPIInt    PetscGlobalSize;
1460014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIntView(PetscInt, const PetscInt[], PetscViewer);
1461014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRealView(PetscInt, const PetscReal[], PetscViewer);
1462014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscScalarView(PetscInt, const PetscScalar[], PetscViewer);
1463d382aafbSBarry Smith 
1464bbcf679cSJacob Faibussowitsch /*
1465bbcf679cSJacob Faibussowitsch     Basic memory and string operations. These are usually simple wrappers
1466bbcf679cSJacob Faibussowitsch    around the basic Unix system calls, but a few of them have additional
1467bbcf679cSJacob Faibussowitsch    functionality and/or error checking.
1468bbcf679cSJacob Faibussowitsch */
1469bbcf679cSJacob Faibussowitsch #include <petscstring.h>
1470bbcf679cSJacob Faibussowitsch 
1471a663daf8SBarry Smith #include <stddef.h>
1472d382aafbSBarry Smith #include <stdlib.h>
14736c7e564aSBarry Smith 
1474447bcd8fSJacob Faibussowitsch #if defined(PETSC_CLANG_STATIC_ANALYZER)
1475447bcd8fSJacob Faibussowitsch   #define PetscPrefetchBlock(a, b, c, d)
1476447bcd8fSJacob Faibussowitsch #else
1477d382aafbSBarry Smith   /*MC
1478d382aafbSBarry Smith    PetscPrefetchBlock - Prefetches a block of memory
1479d382aafbSBarry Smith 
1480eca87e8dSBarry Smith    Synopsis:
1481aaa7dc30SBarry Smith     #include <petscsys.h>
1482eca87e8dSBarry Smith     void PetscPrefetchBlock(const anytype *a,size_t n,int rw,int t)
1483eca87e8dSBarry Smith 
1484d382aafbSBarry Smith    Not Collective
1485d382aafbSBarry Smith 
1486d382aafbSBarry Smith    Input Parameters:
148716a05f60SBarry Smith +  a  - pointer to first element to fetch (any type but usually `PetscInt` or `PetscScalar`)
1488d382aafbSBarry Smith .  n  - number of elements to fetch
1489d382aafbSBarry Smith .  rw - 1 if the memory will be written to, otherwise 0 (ignored by many processors)
149050d8bf02SJed Brown -  t  - temporal locality (PETSC_PREFETCH_HINT_{NTA,T0,T1,T2}), see note
1491d382aafbSBarry Smith 
1492d382aafbSBarry Smith    Level: developer
1493d382aafbSBarry Smith 
1494d382aafbSBarry Smith    Notes:
149516a05f60SBarry Smith    The last two arguments (`rw` and `t`) must be compile-time constants.
1496d382aafbSBarry Smith 
149750d8bf02SJed Brown    Adopting Intel's x86/x86-64 conventions, there are four levels of temporal locality.  Not all architectures offer
149850d8bf02SJed Brown    equivalent locality hints, but the following macros are always defined to their closest analogue.
149987497f52SBarry 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).
150087497f52SBarry 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.
150187497f52SBarry Smith .  `PETSC_PREFETCH_HINT_T1`  - Fetch to level 2 and higher (not L1).
150287497f52SBarry Smith -  `PETSC_PREFETCH_HINT_T2`  - Fetch to high-level cache only.  (On many systems, T0 and T1 are equivalent.)
1503d382aafbSBarry Smith 
1504d382aafbSBarry Smith    This function does nothing on architectures that do not support prefetch and never errors (even if passed an invalid
1505d382aafbSBarry Smith    address).
1506d382aafbSBarry Smith 
1507d382aafbSBarry Smith M*/
15089371c9d4SSatish Balay   #define PetscPrefetchBlock(a, n, rw, t) \
15099371c9d4SSatish Balay     do { \
1510d382aafbSBarry Smith       const char *_p = (const char *)(a), *_end = (const char *)((a) + (n)); \
1511d382aafbSBarry Smith       for (; _p < _end; _p += PETSC_LEVEL1_DCACHE_LINESIZE) PETSC_Prefetch(_p, (rw), (t)); \
1512d382aafbSBarry Smith     } while (0)
1513447bcd8fSJacob Faibussowitsch #endif
1514d382aafbSBarry Smith /*
1515d382aafbSBarry Smith       Determine if some of the kernel computation routines use
1516d382aafbSBarry Smith    Fortran (rather than C) for the numerical calculations. On some machines
1517d382aafbSBarry Smith    and compilers (like complex numbers) the Fortran version of the routines
1518d382aafbSBarry Smith    is faster than the C/C++ versions. The flag --with-fortran-kernels
1519e2e64c6bSBarry Smith    should be used with ./configure to turn these on.
1520d382aafbSBarry Smith */
1521d382aafbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNELS)
1522d382aafbSBarry Smith 
1523d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTCRL)
1524d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTCRL
1525d382aafbSBarry Smith   #endif
1526d382aafbSBarry Smith 
1527d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1528d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTAIJ
1529d382aafbSBarry Smith   #endif
1530d382aafbSBarry Smith 
1531d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1532d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ
1533d382aafbSBarry Smith   #endif
1534d382aafbSBarry Smith 
1535d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MAXPY)
1536d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MAXPY
1537d382aafbSBarry Smith   #endif
1538d382aafbSBarry Smith 
1539d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
1540d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ
1541d382aafbSBarry Smith   #endif
1542d382aafbSBarry Smith 
1543d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ)
1544d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ
1545d382aafbSBarry Smith   #endif
1546d382aafbSBarry Smith 
1547d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1548d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ
1549d382aafbSBarry Smith   #endif
1550d382aafbSBarry Smith 
1551d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_MDOT)
1552d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_MDOT
1553d382aafbSBarry Smith   #endif
1554d382aafbSBarry Smith 
1555d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_XTIMESY)
1556d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_XTIMESY
1557d382aafbSBarry Smith   #endif
1558d382aafbSBarry Smith 
1559d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_AYPX)
1560d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_AYPX
1561d382aafbSBarry Smith   #endif
1562d382aafbSBarry Smith 
1563d382aafbSBarry Smith   #if !defined(PETSC_USE_FORTRAN_KERNEL_WAXPY)
1564d382aafbSBarry Smith     #define PETSC_USE_FORTRAN_KERNEL_WAXPY
1565d382aafbSBarry Smith   #endif
1566d382aafbSBarry Smith 
1567d382aafbSBarry Smith #endif
1568d382aafbSBarry Smith 
1569d382aafbSBarry Smith /*
1570d382aafbSBarry Smith     Macros for indicating code that should be compiled with a C interface,
1571d382aafbSBarry Smith    rather than a C++ interface. Any routines that are dynamically loaded
1572d382aafbSBarry Smith    (such as the PCCreate_XXX() routines) must be wrapped so that the name
1573d382aafbSBarry Smith    mangler does not change the functions symbol name. This just hides the
1574d382aafbSBarry Smith    ugly extern "C" {} wrappers.
1575d382aafbSBarry Smith */
1576d382aafbSBarry Smith #if defined(__cplusplus)
1577d382aafbSBarry Smith   #define EXTERN_C_BEGIN extern "C" {
1578d382aafbSBarry Smith   #define EXTERN_C_END   }
1579d382aafbSBarry Smith #else
1580d382aafbSBarry Smith   #define EXTERN_C_BEGIN
1581d382aafbSBarry Smith   #define EXTERN_C_END
1582d382aafbSBarry Smith #endif
1583d382aafbSBarry Smith 
1584d382aafbSBarry Smith /*MC
1585d382aafbSBarry Smith    MPI_Comm - the basic object used by MPI to determine which processes are involved in a
1586d382aafbSBarry Smith    communication
1587d382aafbSBarry Smith 
1588d382aafbSBarry Smith    Level: beginner
1589d382aafbSBarry Smith 
159016a05f60SBarry Smith    Note:
159116a05f60SBarry Smith    This manual page is a place-holder because MPICH does not have a manual page for `MPI_Comm`
1592d382aafbSBarry Smith 
1593db781477SPatrick Sanan .seealso: `PETSC_COMM_WORLD`, `PETSC_COMM_SELF`
1594d382aafbSBarry Smith M*/
1595d382aafbSBarry Smith 
1596d382aafbSBarry Smith #if defined(PETSC_HAVE_MPIIO)
159793d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_write_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(2, 4);
159893d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_read_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(2, 4);
159993d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_write_at(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(3, 5);
160093d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_read_at(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(3, 5);
160193d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_write_at_all(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(3, 5);
160293d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode MPIU_File_read_at_all(MPI_File, MPI_Offset, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(3, 5);
1603d382aafbSBarry Smith #endif
1604d382aafbSBarry Smith 
160561b0d812SBarry Smith /*@C
16067de69702SBarry Smith    PetscIntCast - casts a `PetscInt64` (which is 64 bits in size) to a `PetscInt` (which may be 32-bits in size), generates an
160787497f52SBarry Smith    error if the `PetscInt` is not large enough to hold the number.
1608c73702f5SBarry Smith 
160920f4b53cSBarry Smith    Not Collective; No Fortran Support
1610c73702f5SBarry Smith 
1611c73702f5SBarry Smith    Input Parameter:
161287497f52SBarry Smith .  a - the `PetscInt64` value
1613c73702f5SBarry Smith 
1614c73702f5SBarry Smith    Output Parameter:
161587497f52SBarry Smith .  b - the resulting `PetscInt` value
1616c73702f5SBarry Smith 
1617c73702f5SBarry Smith    Level: advanced
1618c73702f5SBarry Smith 
161995bd0b28SBarry Smith    Note:
16207de69702SBarry 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 integers
1621c73702f5SBarry Smith 
1622db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscIntMultError()`, `PetscIntSumError()`
1623c73702f5SBarry Smith @*/
1624d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscIntCast(PetscInt64 a, PetscInt *b)
1625d71ae5a4SJacob Faibussowitsch {
1626c73702f5SBarry Smith   PetscFunctionBegin;
16273ca90d2dSJacob Faibussowitsch   *b = 0;
1628f3fa974cSJacob Faibussowitsch   // if using 64-bit indices already then this comparison is tautologically true
1629f3fa974cSJacob Faibussowitsch   PetscCheck(a < PETSC_MAX_INT, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt64_FMT " is too big for PetscInt, you may need to ./configure using --with-64-bit-indices", a);
1630f3fa974cSJacob Faibussowitsch   *b = (PetscInt)a;
16313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1632c73702f5SBarry Smith }
1633c73702f5SBarry Smith 
1634c73702f5SBarry Smith /*@C
16357de69702SBarry Smith    PetscCountCast - casts a `PetscCount` to a `PetscInt` (which may be 32-bits in size), generates an
163687497f52SBarry Smith    error if the `PetscInt` is not large enough to hold the number.
1637981bb840SJunchao Zhang 
1638667f096bSBarry Smith    Not Collective; No Fortran Support
1639981bb840SJunchao Zhang 
1640981bb840SJunchao Zhang    Input Parameter:
164187497f52SBarry Smith .  a - the `PetscCount` value
1642981bb840SJunchao Zhang 
1643981bb840SJunchao Zhang    Output Parameter:
164487497f52SBarry Smith .  b - the resulting `PetscInt` value
1645981bb840SJunchao Zhang 
1646981bb840SJunchao Zhang    Level: advanced
1647981bb840SJunchao Zhang 
1648667f096bSBarry Smith    Note:
16497de69702SBarry Smith    If integers needed for the applications are too large to fit in 32-bit integers you can ./configure using `--with-64-bit-indices` to make `PetscInt` use 64-bit integers
1650981bb840SJunchao Zhang 
1651667f096bSBarry Smith .seealso: `PetscCount`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscIntMultError()`, `PetscIntSumError()`, `PetscIntCast()`
1652981bb840SJunchao Zhang @*/
1653d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscCountCast(PetscCount a, PetscInt *b)
1654d71ae5a4SJacob Faibussowitsch {
1655981bb840SJunchao Zhang   PetscFunctionBegin;
1656981bb840SJunchao Zhang   *b = 0;
1657f3fa974cSJacob Faibussowitsch   PetscCheck(sizeof(PetscCount) <= sizeof(PetscInt) || a <= PETSC_MAX_INT, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscCount_FMT " is too big for PetscInt, you may need to ./configure using --with-64-bit-indices", a);
1658f3fa974cSJacob Faibussowitsch   *b = (PetscInt)a;
16593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1660981bb840SJunchao Zhang }
1661981bb840SJunchao Zhang 
1662981bb840SJunchao Zhang /*@C
16637de69702SBarry Smith    PetscBLASIntCast - casts a `PetscInt` (which may be 64-bits in size) to a `PetscBLASInt` (which may be 32-bits in size), generates an
166487497f52SBarry Smith    error if the `PetscBLASInt` is not large enough to hold the number.
166561b0d812SBarry Smith 
1666667f096bSBarry Smith    Not Collective; No Fortran Support
166761b0d812SBarry Smith 
166861b0d812SBarry Smith    Input Parameter:
166987497f52SBarry Smith .  a - the `PetscInt` value
167061b0d812SBarry Smith 
167161b0d812SBarry Smith    Output Parameter:
167287497f52SBarry Smith .  b - the resulting `PetscBLASInt` value
167361b0d812SBarry Smith 
167461b0d812SBarry Smith    Level: advanced
167561b0d812SBarry Smith 
1676667f096bSBarry Smith    Note:
1677f0b7f91aSBarry Smith    Errors if the integer is negative since PETSc calls to BLAS/LAPACK never need to cast negative integer inputs
16781fd49c25SBarry Smith 
1679db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscIntCast()`, `PetscCountCast()`
168061b0d812SBarry Smith @*/
1681d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscBLASIntCast(PetscInt a, PetscBLASInt *b)
1682d71ae5a4SJacob Faibussowitsch {
1683c5df96a5SBarry Smith   PetscFunctionBegin;
168454c59aa7SJacob Faibussowitsch   *b = 0;
168554c59aa7SJacob Faibussowitsch   if (PetscDefined(USE_64BIT_INDICES) && !PetscDefined(HAVE_64BIT_BLAS_INDICES)) {
16867de69702SBarry Smith     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);
168754c59aa7SJacob Faibussowitsch   }
168854c59aa7SJacob Faibussowitsch   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer to BLAS/LAPACK routine");
1689f3fa974cSJacob Faibussowitsch   *b = (PetscBLASInt)a;
16903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1691c5df96a5SBarry Smith }
1692c5df96a5SBarry Smith 
169361b0d812SBarry Smith /*@C
169487497f52SBarry Smith    PetscCuBLASIntCast - like `PetscBLASIntCast()`, but for `PetscCuBLASInt`.
1695eee0c0a6SToby Isaac 
1696667f096bSBarry Smith    Not Collective; No Fortran Support
1697eee0c0a6SToby Isaac 
1698eee0c0a6SToby Isaac    Input Parameter:
169987497f52SBarry Smith .  a - the `PetscInt` value
1700eee0c0a6SToby Isaac 
1701eee0c0a6SToby Isaac    Output Parameter:
170287497f52SBarry Smith .  b - the resulting `PetscCuBLASInt` value
1703eee0c0a6SToby Isaac 
1704eee0c0a6SToby Isaac    Level: advanced
1705eee0c0a6SToby Isaac 
1706667f096bSBarry Smith    Note:
1707eee0c0a6SToby Isaac    Errors if the integer is negative since PETSc calls to cuBLAS and friends never need to cast negative integer inputs
1708eee0c0a6SToby Isaac 
1709db781477SPatrick Sanan .seealso: `PetscCuBLASInt`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscMPIIntCast()`, `PetscIntCast()`
1710eee0c0a6SToby Isaac @*/
1711d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscCuBLASIntCast(PetscInt a, PetscCuBLASInt *b)
1712d71ae5a4SJacob Faibussowitsch {
1713eee0c0a6SToby Isaac   PetscFunctionBegin;
171454c59aa7SJacob Faibussowitsch   *b = 0;
17157de69702SBarry Smith   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);
1716f3fa974cSJacob Faibussowitsch   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer %" PetscInt_FMT "to cuBLAS routine", a);
1717f3fa974cSJacob Faibussowitsch   *b = (PetscCuBLASInt)a;
17183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1719eee0c0a6SToby Isaac }
1720eee0c0a6SToby Isaac 
1721eee0c0a6SToby Isaac /*@C
172247d993e7Ssuyashtn    PetscHipBLASIntCast - like `PetscBLASIntCast()`, but for `PetscHipBLASInt`.
172347d993e7Ssuyashtn 
1724667f096bSBarry Smith    Not Collective; No Fortran Support
172547d993e7Ssuyashtn 
172647d993e7Ssuyashtn    Input Parameter:
172747d993e7Ssuyashtn .  a - the `PetscInt` value
172847d993e7Ssuyashtn 
172947d993e7Ssuyashtn    Output Parameter:
173047d993e7Ssuyashtn .  b - the resulting `PetscHipBLASInt` value
173147d993e7Ssuyashtn 
173247d993e7Ssuyashtn    Level: advanced
173347d993e7Ssuyashtn 
1734667f096bSBarry Smith    Note:
173547d993e7Ssuyashtn    Errors if the integer is negative since PETSc calls to hipBLAS and friends never need to cast negative integer inputs
173647d993e7Ssuyashtn 
173747d993e7Ssuyashtn .seealso: `PetscHipBLASInt`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscMPIIntCast()`, `PetscIntCast()`
173847d993e7Ssuyashtn @*/
173947d993e7Ssuyashtn static inline PetscErrorCode PetscHipBLASIntCast(PetscInt a, PetscHipBLASInt *b)
174047d993e7Ssuyashtn {
174147d993e7Ssuyashtn   PetscFunctionBegin;
174247d993e7Ssuyashtn   *b = 0;
17437de69702SBarry Smith   PetscCheck(a <= PETSC_HIPBLAS_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt_FMT " is too big for hipBLAS, which is restricted to 32-bit integers.", a);
1744f3fa974cSJacob Faibussowitsch   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer %" PetscInt_FMT "to hipBLAS routine", a);
1745f3fa974cSJacob Faibussowitsch   *b = (PetscHipBLASInt)a;
17463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
174747d993e7Ssuyashtn }
174847d993e7Ssuyashtn 
174947d993e7Ssuyashtn /*@C
1750af27ebaaSBarry Smith    PetscMPIIntCast - casts a `PetscInt` (which may be 64-bits in size) to a `PetscMPIInt` (which may be 32-bits in size), generates an
1751667f096bSBarry Smith    error if the `PetscMPIInt` is not large enough to hold the number.
175261b0d812SBarry Smith 
1753667f096bSBarry Smith    Not Collective; No Fortran Support
175461b0d812SBarry Smith 
175561b0d812SBarry Smith    Input Parameter:
175687497f52SBarry Smith .  a - the `PetscInt` value
175761b0d812SBarry Smith 
175861b0d812SBarry Smith    Output Parameter:
175987497f52SBarry Smith .  b - the resulting `PetscMPIInt` value
176061b0d812SBarry Smith 
176161b0d812SBarry Smith    Level: advanced
176261b0d812SBarry Smith 
1763db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscIntCast()`
176461b0d812SBarry Smith @*/
1765d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscMPIIntCast(PetscInt a, PetscMPIInt *b)
1766d71ae5a4SJacob Faibussowitsch {
17674dc2109aSBarry Smith   PetscFunctionBegin;
176854c59aa7SJacob Faibussowitsch   *b = 0;
1769f3fa974cSJacob Faibussowitsch   PetscCheck(a <= PETSC_MPI_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt_FMT " is too big for MPI buffer length. Maximum supported value is %d", a, PETSC_MPI_INT_MAX);
1770f3fa974cSJacob Faibussowitsch   *b = (PetscMPIInt)a;
17713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17724dc2109aSBarry Smith }
17734dc2109aSBarry Smith 
1774f3fa974cSJacob Faibussowitsch #define PetscInt64Mult(a, b) (((PetscInt64)(a)) * ((PetscInt64)(b)))
17752f18eb33SBarry Smith 
17762f18eb33SBarry Smith /*@C
1777f3fa974cSJacob Faibussowitsch   PetscRealIntMultTruncate - Computes the product of a positive `PetscReal` and a positive
1778f3fa974cSJacob Faibussowitsch   `PetscInt` and truncates the value to slightly less than the maximal possible value.
17792f18eb33SBarry Smith 
178020f4b53cSBarry Smith   Not Collective; No Fortran Support
17812f18eb33SBarry Smith 
1782d8d19677SJose E. Roman   Input Parameters:
1783f3fa974cSJacob Faibussowitsch + a - The `PetscReal` value
1784f3fa974cSJacob Faibussowitsch - b - The `PetscInt` value
17852f18eb33SBarry Smith 
1786667f096bSBarry Smith   Level: advanced
1787667f096bSBarry Smith 
1788f3fa974cSJacob Faibussowitsch   Notes:
1789f3fa974cSJacob Faibussowitsch   Returns the result as a `PetscInt` value.
17902f18eb33SBarry Smith 
1791f3fa974cSJacob Faibussowitsch   Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`.
1792667f096bSBarry Smith 
1793f3fa974cSJacob Faibussowitsch   Use `PetscIntMultTruncate()` to compute the product of two positive `PetscInt` and truncate
1794f3fa974cSJacob Faibussowitsch   to fit a `PetscInt`.
1795667f096bSBarry Smith 
1796f3fa974cSJacob Faibussowitsch   Use `PetscIntMultError()` to compute the product of two `PetscInt` if you wish to generate an
1797f3fa974cSJacob Faibussowitsch   error if the result will not fit in a `PetscInt`.
17982f18eb33SBarry Smith 
1799b5e6e808SPierre Jolivet   Developer Notes:
1800f3fa974cSJacob Faibussowitsch   We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but
1801f3fa974cSJacob Faibussowitsch   requires many more checks.
18022f18eb33SBarry Smith 
1803f3fa974cSJacob Faibussowitsch   This is used where we compute approximate sizes for workspace and need to insure the
1804f3fa974cSJacob Faibussowitsch   workspace is index-able.
18051fd49c25SBarry Smith 
1806f3fa974cSJacob Faibussowitsch .seealso: `PetscReal`, `PetscInt`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`
18072f18eb33SBarry Smith @*/
1808d71ae5a4SJacob Faibussowitsch static inline PetscInt PetscRealIntMultTruncate(PetscReal a, PetscInt b)
1809d71ae5a4SJacob Faibussowitsch {
18105f80ce2aSJacob Faibussowitsch   PetscInt64 r = (PetscInt64)(a * (PetscReal)b);
18112f18eb33SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
18122f18eb33SBarry Smith   return (PetscInt)r;
18132f18eb33SBarry Smith }
18142f18eb33SBarry Smith 
18152f18eb33SBarry Smith /*@C
181687497f52SBarry Smith    PetscIntMultTruncate - Computes the product of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value
18172f18eb33SBarry Smith 
1818667f096bSBarry Smith    Not Collective; No Fortran Support
18192f18eb33SBarry Smith 
1820d8d19677SJose E. Roman    Input Parameters:
1821af27ebaaSBarry Smith +  a - the `PetscInt` value
18222f18eb33SBarry Smith -  b - the second value
18232f18eb33SBarry Smith 
1824390e1bf2SBarry Smith    Returns:
1825af27ebaaSBarry Smith    The result as a `PetscInt` value
18262f18eb33SBarry Smith 
1827667f096bSBarry Smith    Level: advanced
1828667f096bSBarry Smith 
1829667f096bSBarry Smith    Notes:
183087497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`
1831667f096bSBarry Smith 
183287497f52SBarry Smith    Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`
1833667f096bSBarry Smith 
183487497f52SBarry Smith    Use `PetscIntMultError()` to compute the product of two `PetscInt` if you wish to generate an error if the result will not fit in a `PetscInt`
18352f18eb33SBarry Smith 
1836b5e6e808SPierre Jolivet    Developer Notes:
183787497f52SBarry Smith    We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but requires many more checks.
18382f18eb33SBarry Smith 
18392f18eb33SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
18402f18eb33SBarry Smith 
1841667f096bSBarry Smith .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`,
1842667f096bSBarry Smith           `PetscIntSumTruncate()`
18432f18eb33SBarry Smith @*/
1844d71ae5a4SJacob Faibussowitsch static inline PetscInt PetscIntMultTruncate(PetscInt a, PetscInt b)
1845d71ae5a4SJacob Faibussowitsch {
18465f80ce2aSJacob Faibussowitsch   PetscInt64 r = PetscInt64Mult(a, b);
18472f18eb33SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
18482f18eb33SBarry Smith   return (PetscInt)r;
18492f18eb33SBarry Smith }
18502f18eb33SBarry Smith 
1851f91af8c7SBarry Smith /*@C
185287497f52SBarry Smith    PetscIntSumTruncate - Computes the sum of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value
1853f91af8c7SBarry Smith 
1854667f096bSBarry Smith    Not Collective; No Fortran Support
1855f91af8c7SBarry Smith 
1856d8d19677SJose E. Roman    Input Parameters:
185787497f52SBarry Smith +  a - the `PetscInt` value
1858f91af8c7SBarry Smith -  b - the second value
1859f91af8c7SBarry Smith 
1860390e1bf2SBarry Smith    Returns:
1861af27ebaaSBarry Smith    The result as a `PetscInt` value
1862f91af8c7SBarry Smith 
1863667f096bSBarry Smith    Level: advanced
1864667f096bSBarry Smith 
1865667f096bSBarry Smith    Notes:
186687497f52SBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`
1867667f096bSBarry Smith 
186887497f52SBarry Smith    Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`
1869667f096bSBarry Smith 
187087497f52SBarry 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`
1871f91af8c7SBarry Smith 
1872b5e6e808SPierre Jolivet    Developer Note:
1873f91af8c7SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
1874f91af8c7SBarry Smith 
1875db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
1876f91af8c7SBarry Smith @*/
1877d71ae5a4SJacob Faibussowitsch static inline PetscInt PetscIntSumTruncate(PetscInt a, PetscInt b)
1878d71ae5a4SJacob Faibussowitsch {
18795f80ce2aSJacob Faibussowitsch   PetscInt64 r = ((PetscInt64)a) + ((PetscInt64)b);
1880f91af8c7SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
1881f91af8c7SBarry Smith   return (PetscInt)r;
1882f91af8c7SBarry Smith }
1883f91af8c7SBarry Smith 
18842f18eb33SBarry Smith /*@C
188587497f52SBarry Smith    PetscIntMultError - Computes the product of two positive `PetscInt` and generates an error with overflow.
18862f18eb33SBarry Smith 
1887667f096bSBarry Smith    Not Collective; No Fortran Support
18882f18eb33SBarry Smith 
1889d8d19677SJose E. Roman    Input Parameters:
189087497f52SBarry Smith +  a - the `PetscInt` value
18912f18eb33SBarry Smith -  b - the second value
18922f18eb33SBarry Smith 
1893d8d19677SJose E. Roman    Output Parameter:
189416a05f60SBarry 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
18952f18eb33SBarry Smith 
1896667f096bSBarry Smith    Level: advanced
18972f18eb33SBarry Smith 
1898667f096bSBarry Smith    Notes:
1899667f096bSBarry Smith    Use `PetscInt64Mult()` to compute the product of two `PetscInt` and store in a `PetscInt64`
1900667f096bSBarry Smith 
1901667f096bSBarry Smith    Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`
1902f5f57ec0SBarry Smith 
1903b5e6e808SPierre Jolivet    Developer Note:
190416a05f60SBarry Smith    In most places in the source code we currently assume that `PetscInt` addition does not overflow, this is obviously wrong but requires many more checks.
190516a05f60SBarry Smith    `PetscIntSumError()` can be used to check for this situation.
19062f18eb33SBarry Smith 
1907db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscIntMult64()`, `PetscIntSumError()`
19082f18eb33SBarry Smith @*/
1909d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscIntMultError(PetscInt a, PetscInt b, PetscInt *result)
1910d71ae5a4SJacob Faibussowitsch {
1911f3fa974cSJacob Faibussowitsch   PetscInt64 r = PetscInt64Mult(a, b);
19122f18eb33SBarry Smith 
19132f18eb33SBarry Smith   PetscFunctionBegin;
1914c72d0b4cSJacob Faibussowitsch   if (result) *result = (PetscInt)r;
1915f3fa974cSJacob Faibussowitsch   if (!PetscDefined(USE_64BIT_INDICES)) {
1916f3fa974cSJacob Faibussowitsch     PetscCheck(r <= PETSC_MAX_INT, PETSC_COMM_SELF, PETSC_ERR_SUP, "Product of two integers %" PetscInt_FMT " %" PetscInt_FMT " overflow, either you have an invalidly large integer error in your code or you must ./configure PETSc with --with-64-bit-indices for the case you are running", a, b);
1917f3fa974cSJacob Faibussowitsch   }
19183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1919f91af8c7SBarry Smith }
1920f91af8c7SBarry Smith 
1921f91af8c7SBarry Smith /*@C
1922f91af8c7SBarry Smith 
192387497f52SBarry Smith    PetscIntSumError - Computes the sum of two positive `PetscInt` and generates an error with overflow.
1924f91af8c7SBarry Smith 
1925667f096bSBarry Smith    Not Collective; No Fortran Support
1926f91af8c7SBarry Smith 
1927d8d19677SJose E. Roman    Input Parameters:
192887497f52SBarry Smith +  a - the `PetscInt` value
1929f91af8c7SBarry Smith -  b - the second value
1930f91af8c7SBarry Smith 
1931d8d19677SJose E. Roman    Output Parameter:
193216a05f60SBarry 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
1933f91af8c7SBarry Smith 
1934f91af8c7SBarry Smith    Level: advanced
1935f91af8c7SBarry Smith 
1936667f096bSBarry Smith    Notes:
19377de69702SBarry Smith    Use `PetscInt64Mult()` to compute the product of two 32-bit `PetscInt` and store in a `PetscInt64`
1938667f096bSBarry Smith 
1939667f096bSBarry Smith    Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`
1940667f096bSBarry Smith 
1941db781477SPatrick Sanan .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
1942f91af8c7SBarry Smith @*/
1943d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscIntSumError(PetscInt a, PetscInt b, PetscInt *result)
1944d71ae5a4SJacob Faibussowitsch {
1945f3fa974cSJacob Faibussowitsch   PetscInt64 r = ((PetscInt64)a) + ((PetscInt64)b);
1946f91af8c7SBarry Smith 
1947f91af8c7SBarry Smith   PetscFunctionBegin;
1948c72d0b4cSJacob Faibussowitsch   if (result) *result = (PetscInt)r;
1949f3fa974cSJacob Faibussowitsch   if (!PetscDefined(USE_64BIT_INDICES)) {
1950f3fa974cSJacob Faibussowitsch     PetscCheck(r <= PETSC_MAX_INT, PETSC_COMM_SELF, PETSC_ERR_SUP, "Sum of two integers %" PetscInt_FMT " %" PetscInt_FMT " overflow, either you have an invalidly large integer error in your code or you must ./configure PETSc with --with-64-bit-indices for the case you are running", a, b);
1951f3fa974cSJacob Faibussowitsch   }
19523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19532f18eb33SBarry Smith }
1954c5df96a5SBarry Smith 
1955d382aafbSBarry Smith /*
19562981ebdbSBarry Smith      The IBM include files define hz, here we hide it so that it may be used as a regular user variable.
1957d382aafbSBarry Smith */
1958d382aafbSBarry Smith #if defined(hz)
1959d382aafbSBarry Smith   #undef hz
1960d382aafbSBarry Smith #endif
1961d382aafbSBarry Smith 
1962d382aafbSBarry Smith #if defined(PETSC_HAVE_SYS_TYPES_H)
1963d382aafbSBarry Smith   #include <sys/types.h>
1964d382aafbSBarry Smith #endif
1965d382aafbSBarry Smith 
1966d382aafbSBarry Smith /*MC
1967d382aafbSBarry Smith 
196866d79e26SBarry 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++
196916a05f60SBarry Smith                     and Fortran compilers when `petscsys.h` is included.
197066d79e26SBarry Smith 
1971baca6076SPierre Jolivet     The current PETSc version and the API for accessing it are defined in <A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/include/petscversion.h.html">include/petscversion.html</A>
197266d79e26SBarry Smith 
197366d79e26SBarry 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)
197466d79e26SBarry Smith 
197566d79e26SBarry 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
197666d79e26SBarry Smith     where only a change in the major version number (x) indicates a change in the API.
197766d79e26SBarry Smith 
197866d79e26SBarry 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
197966d79e26SBarry Smith 
198066d79e26SBarry 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),
198166d79e26SBarry 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
198266d79e26SBarry Smith     version number (x.y.z).
198366d79e26SBarry Smith 
198487497f52SBarry Smith     `PETSC_RELEASE_DATE` is the date the x.y version was released (i.e. the version before any patch releases)
198566d79e26SBarry Smith 
198687497f52SBarry Smith     `PETSC_VERSION_DATE` is the date the x.y.z version was released
198766d79e26SBarry Smith 
198887497f52SBarry Smith     `PETSC_VERSION_GIT` is the last git commit to the repository given in the form vx.y.z-wwwww
198966d79e26SBarry Smith 
199087497f52SBarry Smith     `PETSC_VERSION_DATE_GIT` is the date of the last git commit to the repository
199166d79e26SBarry Smith 
199287497f52SBarry Smith     `PETSC_VERSION_()` is deprecated and will eventually be removed.
199366d79e26SBarry Smith 
1994cbc12506SBarry Smith     Level: intermediate
199566d79e26SBarry Smith M*/
199666d79e26SBarry Smith 
1997014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArchType(char[], size_t);
1998014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetHostName(char[], size_t);
1999014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetUserName(char[], size_t);
2000014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetProgramName(char[], size_t);
2001014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetProgramName(const char[]);
2002014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetDate(char[], size_t);
200327710113SBarry Smith PETSC_EXTERN PetscErrorCode PetscGetVersion(char[], size_t);
20045f309d01SBarry Smith PETSC_EXTERN PetscErrorCode PetscGetVersionNumber(PetscInt *, PetscInt *, PetscInt *, PetscInt *);
2005d382aafbSBarry Smith 
20066a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedInt(PetscInt, const PetscInt[], PetscBool *);
200762e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscSortedInt64(PetscInt, const PetscInt64[], PetscBool *);
20086a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedMPIInt(PetscInt, const PetscMPIInt[], PetscBool *);
20096a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedReal(PetscInt, const PetscReal[], PetscBool *);
2010014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortInt(PetscInt, PetscInt[]);
201162e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscSortInt64(PetscInt, PetscInt64[]);
201262e5d2d2SJDBetteridge PETSC_EXTERN PetscErrorCode PetscSortCount(PetscInt, PetscCount[]);
2013ce605777SToby Isaac PETSC_EXTERN PetscErrorCode PetscSortReverseInt(PetscInt, PetscInt[]);
201422ab5688SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscSortedRemoveDupsInt(PetscInt *, PetscInt[]);
2015b6a18d21SVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedCheckDupsInt(PetscInt, const PetscInt[], PetscBool *);
2016014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsInt(PetscInt *, PetscInt[]);
2017f1cab4e1SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscCheckDupsInt(PetscInt, const PetscInt[], PetscBool *);
201860e03357SMatthew G Knepley PETSC_EXTERN PetscErrorCode PetscFindInt(PetscInt, PetscInt, const PetscInt[], PetscInt *);
2019d2aeb606SJed Brown PETSC_EXTERN PetscErrorCode PetscFindMPIInt(PetscMPIInt, PetscInt, const PetscMPIInt[], PetscInt *);
2020014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithPermutation(PetscInt, const PetscInt[], PetscInt[]);
2021014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortStrWithPermutation(PetscInt, const char *[], PetscInt[]);
2022014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithArray(PetscInt, PetscInt[], PetscInt[]);
2023981bb840SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortIntWithCountArray(PetscCount, PetscInt[], PetscCount[]);
20246c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscSortIntWithArrayPair(PetscInt, PetscInt[], PetscInt[], PetscInt[]);
2025981bb840SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortIntWithIntCountArrayPair(PetscCount, PetscInt[], PetscInt[], PetscCount[]);
202617d7d925SStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortMPIInt(PetscInt, PetscMPIInt[]);
202717d7d925SStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsMPIInt(PetscInt *, PetscMPIInt[]);
2028014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithArray(PetscMPIInt, PetscMPIInt[], PetscMPIInt[]);
20295569a785SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithIntArray(PetscMPIInt, PetscMPIInt[], PetscInt[]);
2030014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithScalarArray(PetscInt, PetscInt[], PetscScalar[]);
203117df18f2SToby Isaac PETSC_EXTERN PetscErrorCode PetscSortIntWithDataArray(PetscInt, PetscInt[], void *, size_t, void *);
2032014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortReal(PetscInt, PetscReal[]);
203339f41f7fSStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortRealWithArrayInt(PetscInt, PetscReal[], PetscInt[]);
2034014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortRealWithPermutation(PetscInt, const PetscReal[], PetscInt[]);
2035745b41b2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsReal(PetscInt *, PetscReal[]);
203639f41f7fSStefano Zampini PETSC_EXTERN PetscErrorCode PetscFindReal(PetscReal, PetscInt, const PetscReal[], PetscReal, PetscInt *);
2037014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortSplit(PetscInt, PetscInt, PetscScalar[], PetscInt[]);
2038014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortSplitReal(PetscInt, PetscInt, PetscReal[], PetscInt[]);
2039014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscProcessTree(PetscInt, const PetscBool[], const PetscInt[], PetscInt *, PetscInt **, PetscInt **, PetscInt **, PetscInt **);
20406c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscMergeIntArrayPair(PetscInt, const PetscInt[], const PetscInt[], PetscInt, const PetscInt[], const PetscInt[], PetscInt *, PetscInt **, PetscInt **);
20416c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscMergeIntArray(PetscInt, const PetscInt[], PetscInt, const PetscInt[], PetscInt *, PetscInt **);
2042e498c390SJed Brown PETSC_EXTERN PetscErrorCode PetscMergeMPIIntArray(PetscInt, const PetscMPIInt[], PetscInt, const PetscMPIInt[], PetscInt *, PetscMPIInt **);
2043ce605777SToby Isaac PETSC_EXTERN PetscErrorCode PetscParallelSortedInt(MPI_Comm, PetscInt, const PetscInt[], PetscBool *);
2044ce605777SToby Isaac 
20454d3610e3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscTimSort(PetscInt, void *, size_t, int (*)(const void *, const void *, void *), void *);
2046676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrdered(PetscInt, PetscInt[]);
2047676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrdered(PetscInt, PetscMPIInt[]);
2048676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrdered(PetscInt, PetscReal[]);
20494d3610e3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscTimSortWithArray(PetscInt, void *, size_t, void *, size_t, int (*)(const void *, const void *, void *), void *);
2050676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrderedWithArray(PetscInt, PetscInt[], PetscInt[]);
2051676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrderedWithArray(PetscInt, PetscMPIInt[], PetscMPIInt[]);
2052676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrderedWithArrayInt(PetscInt, PetscReal[], PetscInt[]);
2053676f2a66SJacob Faibussowitsch 
2054014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDisplay(void);
2055014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetDisplay(char[], size_t);
2056d382aafbSBarry Smith 
205776bdecfbSBarry Smith /*J
2058d382aafbSBarry Smith     PetscRandomType - String with the name of a PETSc randomizer
2059d382aafbSBarry Smith 
2060d382aafbSBarry Smith    Level: beginner
2061d382aafbSBarry Smith 
2062667f096bSBarry Smith    Note:
206387497f52SBarry Smith    To use `PETSCSPRNG` or `PETSCRANDOM123` you must have ./configure PETSc
206416a05f60SBarry Smith    with the option `--download-sprng` or `--download-random123`. We recommend the default provided with PETSc.
2065d382aafbSBarry Smith 
2066db781477SPatrick Sanan .seealso: `PetscRandomSetType()`, `PetscRandom`, `PetscRandomCreate()`
206776bdecfbSBarry Smith J*/
206819fd82e9SBarry Smith typedef const char *PetscRandomType;
2069d382aafbSBarry Smith #define PETSCRAND      "rand"
2070d382aafbSBarry Smith #define PETSCRAND48    "rand48"
2071d382aafbSBarry Smith #define PETSCSPRNG     "sprng"
2072c5e4d11fSDmitry Karpeev #define PETSCRANDER48  "rander48"
207325ccb61fSToby Isaac #define PETSCRANDOM123 "random123"
2074808ba619SStefano Zampini #define PETSCCURAND    "curand"
2075d382aafbSBarry Smith 
2076d382aafbSBarry Smith /* Logging support */
2077014dd563SJed Brown PETSC_EXTERN PetscClassId PETSC_RANDOM_CLASSID;
2078d382aafbSBarry Smith 
2079607a6623SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomInitializePackage(void);
20804bf303faSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscRandomFinalizePackage(void);
2081d382aafbSBarry Smith 
2082d382aafbSBarry Smith /* Dynamic creation and loading functions */
2083140e18c1SBarry Smith PETSC_EXTERN PetscFunctionList PetscRandomList;
2084d382aafbSBarry Smith 
2085bdf89e91SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomRegister(const char[], PetscErrorCode (*)(PetscRandom));
208619fd82e9SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomSetType(PetscRandom, PetscRandomType);
2087014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetFromOptions(PetscRandom);
208819fd82e9SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomGetType(PetscRandom, PetscRandomType *);
2089fe2efc57SMark PETSC_EXTERN PetscErrorCode PetscRandomViewFromOptions(PetscRandom, PetscObject, const char[]);
2090014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomView(PetscRandom, PetscViewer);
2091d382aafbSBarry Smith 
2092014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomCreate(MPI_Comm, PetscRandom *);
2093014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetValue(PetscRandom, PetscScalar *);
2094014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetValueReal(PetscRandom, PetscReal *);
2095808ba619SStefano Zampini PETSC_EXTERN PetscErrorCode PetscRandomGetValues(PetscRandom, PetscInt, PetscScalar *);
2096808ba619SStefano Zampini PETSC_EXTERN PetscErrorCode PetscRandomGetValuesReal(PetscRandom, PetscInt, PetscReal *);
2097014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetInterval(PetscRandom, PetscScalar *, PetscScalar *);
2098014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetInterval(PetscRandom, PetscScalar, PetscScalar);
2099014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetSeed(PetscRandom, unsigned long);
2100014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetSeed(PetscRandom, unsigned long *);
2101014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSeed(PetscRandom);
2102014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomDestroy(PetscRandom *);
2103d382aafbSBarry Smith 
2104014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetFullPath(const char[], char[], size_t);
2105014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetRelativePath(const char[], char[], size_t);
2106014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetWorkingDirectory(char[], size_t);
2107014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetRealPath(const char[], char[]);
2108014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetHomeDirectory(char[], size_t);
2109014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTestFile(const char[], char, PetscBool *);
2110014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTestDirectory(const char[], char, PetscBool *);
211197540f07SSatish Balay PETSC_EXTERN PetscErrorCode PetscMkdir(const char[]);
21128a10d460SHong Zhang PETSC_EXTERN PetscErrorCode PetscMkdtemp(char[]);
211397540f07SSatish Balay PETSC_EXTERN PetscErrorCode PetscRMTree(const char[]);
2114d382aafbSBarry Smith 
2115d598313aSBarry Smith /*MC
2116d598313aSBarry Smith    PetscBinaryBigEndian - indicates if values in memory are stored with big endian format
2117d598313aSBarry Smith 
2118d598313aSBarry Smith    Synopsis:
2119d598313aSBarry Smith    #include <petscsys.h>
2120d598313aSBarry Smith    PetscBool PetscBinaryBigEndian(void);
2121d598313aSBarry Smith 
2122d598313aSBarry Smith    No Fortran Support
2123d598313aSBarry Smith 
2124d598313aSBarry Smith    Level: developer
2125d598313aSBarry Smith 
2126d598313aSBarry Smith .seealso: `PetscInitialize()`, `PetscFinalize()`, `PetscInitializeCalled`
2127d598313aSBarry Smith M*/
2128d71ae5a4SJacob Faibussowitsch static inline PetscBool PetscBinaryBigEndian(void)
2129d71ae5a4SJacob Faibussowitsch {
21309371c9d4SSatish Balay   long _petsc_v = 1;
21319371c9d4SSatish Balay   return ((char *)&_petsc_v)[0] ? PETSC_FALSE : PETSC_TRUE;
21329371c9d4SSatish Balay }
213330815ce0SLisandro Dalcin 
21349860990eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinaryRead(int, void *, PetscInt, PetscInt *, PetscDataType);
21359860990eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedRead(MPI_Comm, int, void *, PetscInt, PetscInt *, PetscDataType);
2136f253e43cSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinaryWrite(int, const void *, PetscInt, PetscDataType);
2137f253e43cSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedWrite(MPI_Comm, int, const void *, PetscInt, PetscDataType);
2138014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinaryOpen(const char[], PetscFileMode, int *);
2139014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinaryClose(int);
2140014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSharedTmp(MPI_Comm, PetscBool *);
2141014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSharedWorkingDirectory(MPI_Comm, PetscBool *);
2142014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetTmp(MPI_Comm, char[], size_t);
2143014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFileRetrieve(MPI_Comm, const char[], char[], size_t, PetscBool *);
2144014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLs(MPI_Comm, const char[], char[], size_t, PetscBool *);
2145c891a504SStefano Zampini #if defined(PETSC_USE_SOCKET_VIEWER)
2146e2fc02c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscOpenSocket(const char[], int, int *);
2147c891a504SStefano Zampini #endif
2148d382aafbSBarry Smith 
2149014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinarySeek(int, off_t, PetscBinarySeekType, off_t *);
2150014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedSeek(MPI_Comm, int, off_t, PetscBinarySeekType, off_t *);
2151014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscByteSwap(void *, PetscDataType, PetscInt);
2152d382aafbSBarry Smith 
2153014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebugTerminal(const char[]);
2154014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebugger(const char[], PetscBool);
2155014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDefaultDebugger(void);
2156014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebuggerFromString(const char *);
2157014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAttachDebugger(void);
2158014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStopForDebugger(void);
21592a2a2941SBarry Smith PETSC_EXTERN PetscErrorCode PetscWaitOnError(void);
2160d382aafbSBarry Smith 
2161014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherNumberOfMessages(MPI_Comm, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt *);
2162014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], PetscMPIInt **, PetscMPIInt **);
2163014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths2(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt **, PetscMPIInt **, PetscMPIInt **);
2164014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPostIrecvInt(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscInt ***, MPI_Request **);
2165014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPostIrecvScalar(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscScalar ***, MPI_Request **);
216693d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSided(MPI_Comm, PetscMPIInt, MPI_Datatype, PetscMPIInt, const PetscMPIInt *, const void *, PetscMPIInt *, PetscMPIInt **, void *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(6, 3);
216793d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedF(MPI_Comm, PetscMPIInt, MPI_Datatype, PetscMPIInt, const PetscMPIInt[], const void *, PetscMPIInt *, PetscMPIInt **, void *, PetscMPIInt, PetscErrorCode (*send)(MPI_Comm, const PetscMPIInt[], PetscMPIInt, PetscMPIInt, void *, MPI_Request[], void *), PetscErrorCode (*recv)(MPI_Comm, const PetscMPIInt[], PetscMPIInt, void *, MPI_Request[], void *), void *ctx) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(6, 3);
216893d501b3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedFReq(MPI_Comm, PetscMPIInt, MPI_Datatype, PetscMPIInt, const PetscMPIInt[], const void *, PetscMPIInt *, PetscMPIInt **, void *, PetscMPIInt, MPI_Request **, MPI_Request **, PetscErrorCode (*send)(MPI_Comm, const PetscMPIInt[], PetscMPIInt, PetscMPIInt, void *, MPI_Request[], void *), PetscErrorCode (*recv)(MPI_Comm, const PetscMPIInt[], PetscMPIInt, void *, MPI_Request[], void *), void *ctx) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(6, 3);
2169d382aafbSBarry Smith 
21706145cd65SJed Brown PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedSetType(MPI_Comm, PetscBuildTwoSidedType);
21716145cd65SJed Brown PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedGetType(MPI_Comm, PetscBuildTwoSidedType *);
21726145cd65SJed Brown 
2173014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSSEIsEnabled(MPI_Comm, PetscBool *, PetscBool *);
2174d382aafbSBarry Smith 
2175ce94432eSBarry Smith PETSC_EXTERN MPI_Comm PetscObjectComm(PetscObject);
2176ce94432eSBarry Smith 
2177d382aafbSBarry Smith struct _n_PetscSubcomm {
2178d382aafbSBarry Smith   MPI_Comm         parent;    /* parent communicator */
2179d382aafbSBarry Smith   MPI_Comm         dupparent; /* duplicate parent communicator, under which the processors of this subcomm have contiguous rank */
2180306c2d5bSBarry Smith   MPI_Comm         child;     /* the sub-communicator */
218145487dadSJed Brown   PetscMPIInt      n;         /* num of subcommunicators under the parent communicator */
218245487dadSJed Brown   PetscMPIInt      color;     /* color of processors belong to this communicator */
2183708d824cSJed Brown   PetscMPIInt     *subsize;   /* size of subcommunicator[color] */
218445487dadSJed Brown   PetscSubcommType type;
2185e5acf8a4SHong Zhang   char            *subcommprefix;
2186d382aafbSBarry Smith };
2187d382aafbSBarry Smith 
2188d71ae5a4SJacob Faibussowitsch static inline MPI_Comm PetscSubcommParent(PetscSubcomm scomm)
2189d71ae5a4SJacob Faibussowitsch {
21909371c9d4SSatish Balay   return scomm->parent;
21919371c9d4SSatish Balay }
2192d71ae5a4SJacob Faibussowitsch static inline MPI_Comm PetscSubcommChild(PetscSubcomm scomm)
2193d71ae5a4SJacob Faibussowitsch {
21949371c9d4SSatish Balay   return scomm->child;
21959371c9d4SSatish Balay }
2196d71ae5a4SJacob Faibussowitsch static inline MPI_Comm PetscSubcommContiguousParent(PetscSubcomm scomm)
2197d71ae5a4SJacob Faibussowitsch {
21989371c9d4SSatish Balay   return scomm->dupparent;
21999371c9d4SSatish Balay }
2200014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommCreate(MPI_Comm, PetscSubcomm *);
2201014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommDestroy(PetscSubcomm *);
2202014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommSetNumber(PetscSubcomm, PetscInt);
2203014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommSetType(PetscSubcomm, PetscSubcommType);
220465d9b8f1SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetTypeGeneral(PetscSubcomm, PetscMPIInt, PetscMPIInt);
2205053d1c95SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommView(PetscSubcomm, PetscViewer);
2206f68be91cSHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetFromOptions(PetscSubcomm);
2207e5acf8a4SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetOptionsPrefix(PetscSubcomm, const char[]);
2208a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetParent(PetscSubcomm, MPI_Comm *);
2209a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetContiguousParent(PetscSubcomm, MPI_Comm *);
2210a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetChild(PetscSubcomm, MPI_Comm *);
2211d382aafbSBarry Smith 
22129962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapCreate(PetscInt, PetscHeap *);
22139962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapAdd(PetscHeap, PetscInt, PetscInt);
22149962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapPop(PetscHeap, PetscInt *, PetscInt *);
22159962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapPeek(PetscHeap, PetscInt *, PetscInt *);
22169962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapStash(PetscHeap, PetscInt, PetscInt);
22179962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapUnstash(PetscHeap);
22189962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapDestroy(PetscHeap *);
22199962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapView(PetscHeap, PetscViewer);
22209962606eSBarry Smith 
22215e71baefSBarry Smith PETSC_EXTERN PetscErrorCode PetscProcessPlacementView(PetscViewer);
22225f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGet(MPI_Comm, PetscShmComm *);
22235f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGlobalToLocal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
22245f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommLocalToGlobal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
22255f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGetMpiShmComm(PetscShmComm, MPI_Comm *);
22265e71baefSBarry Smith 
2227a32e93adSJunchao Zhang /* routines to better support OpenMP multithreading needs of some PETSc third party libraries */
2228a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlCreate(MPI_Comm, PetscInt, PetscOmpCtrl *);
2229a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlGetOmpComms(PetscOmpCtrl, MPI_Comm *, MPI_Comm *, PetscBool *);
2230a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlDestroy(PetscOmpCtrl *);
2231a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlBarrier(PetscOmpCtrl);
2232a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterBegin(PetscOmpCtrl);
2233a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterEnd(PetscOmpCtrl);
2234a32e93adSJunchao Zhang 
223513e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferCreate(size_t, size_t, PetscSegBuffer *);
22360f453b92SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferDestroy(PetscSegBuffer *);
223713e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferGet(PetscSegBuffer, size_t, void *);
2238137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractAlloc(PetscSegBuffer, void *);
2239137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractTo(PetscSegBuffer, void *);
2240137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractInPlace(PetscSegBuffer, void *);
224113e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferGetSize(PetscSegBuffer, size_t *);
224213e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferUnuse(PetscSegBuffer, size_t);
22430f453b92SJed Brown 
2244d598313aSBarry Smith /*MC
2245d598313aSBarry Smith   PetscSegBufferGetInts - access an array of `PetscInt` from a `PetscSegBuffer`
2246d598313aSBarry Smith 
2247d598313aSBarry Smith   Synopsis:
2248d598313aSBarry Smith   #include <petscsys.h>
2249d598313aSBarry Smith   PetscErrorCode PetscSegBufferGetInts(PetscSegBuffer seg, size_t count, PetscInt *PETSC_RESTRICT *slot);
2250d598313aSBarry Smith 
2251d598313aSBarry Smith   No Fortran Support
2252d598313aSBarry Smith 
2253d598313aSBarry Smith   Input Parameters:
2254d598313aSBarry Smith + seg   - `PetscSegBuffer` buffer
2255d598313aSBarry Smith - count - number of entries needed
2256d598313aSBarry Smith 
2257d598313aSBarry Smith   Output Parameter:
2258d598313aSBarry Smith . buf - address of new buffer for contiguous data
2259d598313aSBarry Smith 
2260d598313aSBarry Smith   Level: intermediate
2261d598313aSBarry Smith 
2262d598313aSBarry Smith   Developer Note:
2263d598313aSBarry Smith   Type-safe wrapper to encourage use of PETSC_RESTRICT. Does not use PetscFunctionBegin because the error handling
2264d598313aSBarry Smith   prevents the compiler from completely erasing the stub. This is called in inner loops so it has to be as fast as
2265d598313aSBarry Smith   possible.
2266d598313aSBarry Smith 
2267d598313aSBarry Smith .seealso: `PetscSegBuffer`, `PetscSegBufferGet()`, `PetscInitialize()`, `PetscFinalize()`, `PetscInitializeCalled`
2268d598313aSBarry Smith M*/
2269d598313aSBarry Smith /* */
2270d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscSegBufferGetInts(PetscSegBuffer seg, size_t count, PetscInt *PETSC_RESTRICT *slot)
2271d71ae5a4SJacob Faibussowitsch {
22729371c9d4SSatish Balay   return PetscSegBufferGet(seg, count, (void **)slot);
22739371c9d4SSatish Balay }
2274d382aafbSBarry Smith 
22759de0f6ecSBarry Smith extern PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton;
22769de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *);
22779de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *);
22789de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted, const char *, const char *, PetscBool *);
22799de0f6ecSBarry Smith 
2280ace2fbaeSBarry Smith #include <stdarg.h>
2281ace2fbaeSBarry Smith PETSC_EXTERN PetscErrorCode PetscVSNPrintf(char *, size_t, const char[], size_t *, va_list);
2282ace2fbaeSBarry Smith PETSC_EXTERN                PetscErrorCode (*PetscVFPrintf)(FILE *, const char[], va_list);
2283ace2fbaeSBarry Smith 
2284268b0dfdSSatish Balay PETSC_EXTERN PetscSegBuffer PetscCitationsList;
2285fbfcfee5SBarry Smith 
22861f817a21SBarry Smith /*@C
22871f817a21SBarry Smith      PetscCitationsRegister - Register a bibtex item to obtain credit for an implemented algorithm used in the code.
22881f817a21SBarry Smith 
2289667f096bSBarry Smith      Not Collective; No Fortran Support
22901f817a21SBarry Smith 
22911f817a21SBarry Smith      Input Parameters:
229235cb6cd3SPierre Jolivet +    cite - the bibtex item, formatted to displayed on multiple lines nicely
229387497f52SBarry Smith -    set - a boolean variable initially set to `PETSC_FALSE`; this is used to insure only a single registration of the citation
22941f817a21SBarry Smith 
2295667f096bSBarry Smith      Options Database Key:
22963c7db156SBarry Smith .     -citations [filename]   - print out the bibtex entries for the given computation
22973c7db156SBarry Smith 
2298850545d0SSatish Balay      Level: intermediate
22991f817a21SBarry Smith @*/
2300d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscCitationsRegister(const char cit[], PetscBool *set)
2301d71ae5a4SJacob Faibussowitsch {
2302dff31646SBarry Smith   size_t len;
2303dff31646SBarry Smith   char  *vstring;
2304dff31646SBarry Smith 
23051f817a21SBarry Smith   PetscFunctionBegin;
23063ba16761SJacob Faibussowitsch   if (set && *set) PetscFunctionReturn(PETSC_SUCCESS);
23079566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(cit, &len));
23089566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferGet(PetscCitationsList, len, &vstring));
23099566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(vstring, cit, len));
2310dff31646SBarry Smith   if (set) *set = PETSC_TRUE;
23113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2312dff31646SBarry Smith }
2313dff31646SBarry Smith 
2314fe278a28SBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm, char[], char[], size_t);
2315fe278a28SBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm, const char[], char[], size_t);
2316f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveUpload(MPI_Comm, const char[], const char[]);
2317fe278a28SBarry Smith 
2318f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscBoxAuthorize(MPI_Comm, char[], char[], size_t);
2319f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscBoxRefresh(MPI_Comm, const char[], char[], char[], size_t);
23204bf303faSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscBoxUpload(MPI_Comm, const char[], const char[]);
2321b967cddfSBarry Smith 
23229f4d3c52SBarry Smith PETSC_EXTERN PetscErrorCode PetscGlobusGetTransfers(MPI_Comm, const char[], char[], size_t);
23234bf303faSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscGlobusAuthorize(MPI_Comm, char[], size_t);
23244bf303faSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscGlobusUpload(MPI_Comm, const char[], const char[]);
23259f4d3c52SBarry Smith 
232668e69593SBarry Smith PETSC_EXTERN PetscErrorCode PetscPullJSONValue(const char[], const char[], char[], size_t, PetscBool *);
23275dc0f0a4SBarry Smith PETSC_EXTERN PetscErrorCode PetscPushJSONValue(char[], const char[], const char[], size_t);
2328dff31646SBarry Smith 
2329b2566f29SBarry Smith #if defined(PETSC_USE_DEBUG)
2330d71ae5a4SJacob Faibussowitsch static inline unsigned int PetscStrHash(const char *str)
2331d71ae5a4SJacob Faibussowitsch {
2332df05ca09SBarry Smith   unsigned int c, hash = 5381;
2333df05ca09SBarry Smith 
2334503e1a2cSLisandro Dalcin   while ((c = (unsigned int)*str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
2335df05ca09SBarry Smith   return hash;
2336df05ca09SBarry Smith }
233777a5e07dSBarry Smith 
233877a5e07dSBarry Smith   /*MC
233987497f52SBarry Smith    MPIU_Allreduce - a PETSc replacement for `MPI_Allreduce()` that tries to determine if the call from all the MPI ranks occur from the
234087497f52SBarry Smith                     same place in the PETSc code. This helps to detect bugs where different MPI ranks follow different code paths
234187497f52SBarry Smith                     resulting in inconsistent and incorrect calls to `MPI_Allreduce()`.
234277a5e07dSBarry Smith 
234377a5e07dSBarry Smith    Synopsis:
234477a5e07dSBarry Smith      #include <petscsys.h>
234577a5e07dSBarry Smith      PetscErrorCode MPIU_Allreduce(void *indata,void *outdata,PetscMPIInt count,MPI_Datatype datatype, MPI_Op op, MPI_Comm comm);
234677a5e07dSBarry Smith 
23477cdbe19fSJose E. Roman    Collective
23487cdbe19fSJose E. Roman 
234977a5e07dSBarry Smith    Input Parameters:
2350df05ca09SBarry Smith +  a - pointer to the input data to be reduced
2351df05ca09SBarry Smith .  c - the number of MPI data items in a and b
235287497f52SBarry Smith .  d - the MPI datatype, for example `MPI_INT`
235387497f52SBarry Smith .  e - the MPI operation, for example `MPI_SUM`
2354df05ca09SBarry Smith -  fcomm - the MPI communicator on which the operation occurs
235577a5e07dSBarry Smith 
235677a5e07dSBarry Smith    Output Parameter:
2357df05ca09SBarry Smith .  b - the reduced values
235877a5e07dSBarry Smith 
2359667f096bSBarry Smith    Level: developer
2360667f096bSBarry Smith 
2361e28ce29aSPatrick Sanan    Notes:
236287497f52SBarry Smith    In optimized mode this directly calls `MPI_Allreduce()`
236377a5e07dSBarry Smith 
2364df05ca09SBarry Smith    This is defined as a macro that can return error codes internally so it cannot be used in a subroutine that returns void.
2365df05ca09SBarry Smith 
236687497f52SBarry 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
2367df05ca09SBarry Smith 
2368db781477SPatrick Sanan .seealso: `MPI_Allreduce()`
236977a5e07dSBarry Smith M*/
23709371c9d4SSatish Balay   #define MPIU_Allreduce(a, b, c, d, e, fcomm) \
23713ba16761SJacob Faibussowitsch     PetscMacroReturnStandard( \
23723ba16761SJacob Faibussowitsch     PetscMPIInt a_b1[6], a_b2[6]; \
23733ba16761SJacob Faibussowitsch     int _mpiu_allreduce_c_int = (int)(c); \
23743ba16761SJacob Faibussowitsch     a_b1[0] = -(PetscMPIInt)__LINE__; \
23753ba16761SJacob Faibussowitsch     a_b1[1] = -a_b1[0]; \
23763ba16761SJacob Faibussowitsch     a_b1[2] = -(PetscMPIInt)PetscStrHash(PETSC_FUNCTION_NAME); \
23773ba16761SJacob Faibussowitsch     a_b1[3] = -a_b1[2]; \
23783ba16761SJacob Faibussowitsch     a_b1[4] = -(PetscMPIInt)(c); \
23793ba16761SJacob Faibussowitsch     a_b1[5] = -a_b1[4]; \
23803ba16761SJacob Faibussowitsch     \
23813ba16761SJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(a_b1, a_b2, 6, MPI_INT, MPI_MAX, fcomm)); \
23823ba16761SJacob Faibussowitsch     PetscCheck(-a_b2[0] == a_b2[1], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MPI_Allreduce() called in different locations (code lines) on different processors"); \
23833ba16761SJacob Faibussowitsch     PetscCheck(-a_b2[2] == a_b2[3], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MPI_Allreduce() called in different locations (functions) on different processors"); \
23843ba16761SJacob Faibussowitsch     PetscCheck(-a_b2[4] == a_b2[5], PETSC_COMM_SELF, PETSC_ERR_PLIB, "MPI_Allreduce() called with different counts %d on different processors", _mpiu_allreduce_c_int); \
23853ba16761SJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce((a), (b), (c), (d), (e), (fcomm)));)
2386b2566f29SBarry Smith #else
2387f3fa974cSJacob Faibussowitsch   #define MPIU_Allreduce(a, b, c, d, e, fcomm) PetscMacroReturnStandard(PetscCallMPI(MPI_Allreduce((a), (b), (c), (d), (e), (fcomm))))
2388b2566f29SBarry Smith #endif
2389b2566f29SBarry Smith 
2390b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
23918198064fSBarry Smith PETSC_EXTERN PetscErrorCode MPIU_Win_allocate_shared(MPI_Aint, PetscMPIInt, MPI_Info, MPI_Comm, void *, MPI_Win *);
23928198064fSBarry Smith PETSC_EXTERN PetscErrorCode MPIU_Win_shared_query(MPI_Win, PetscMPIInt, MPI_Aint *, PetscMPIInt *, void *);
23932f065d89SBarry Smith #endif
23948198064fSBarry Smith 
2395cd88fca5SStefano Zampini /* this is a vile hack */
2396cd88fca5SStefano Zampini #if defined(PETSC_HAVE_NECMPI)
23979371c9d4SSatish 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)
2398cd88fca5SStefano Zampini     #define MPI_Type_free(a) (*(a) = MPI_DATATYPE_NULL, 0);
2399cd88fca5SStefano Zampini   #endif
2400961fb248SStefano Zampini #endif
2401cd88fca5SStefano Zampini 
240212801b39SBarry Smith /*
240360fbe2beSVaclav Hapla     List of external packages and queries on it
240460fbe2beSVaclav Hapla */
240560fbe2beSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscHasExternalPackage(const char[], PetscBool *);
240660fbe2beSVaclav Hapla 
2407f1f2ae84SBarry Smith /* this cannot go here because it may be in a different shared library */
2408f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPIServerBegin(void);
2409f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPIServerEnd(void);
2410f1f2ae84SBarry Smith PETSC_EXTERN PetscErrorCode PCMPICommsDestroy(void);
24117a99bfcaSBarry Smith PETSC_EXTERN PetscBool      PCMPIServerActive;
2412fbf9dbe5SBarry Smith 
2413edd03b47SJacob Faibussowitsch #define PETSC_HAVE_FORTRAN PETSC_DEPRECATED_MACRO(3, 20, 0, "PETSC_USE_FORTRAN_BINDINGS", ) PETSC_USE_FORTRAN_BINDINGS
2414046ed546SBarry Smith 
2415046ed546SBarry Smith PETSC_EXTERN PetscErrorCode PetscBLASSetNumThreads(PetscInt);
2416046ed546SBarry Smith PETSC_EXTERN PetscErrorCode PetscBLASGetNumThreads(PetscInt *);
24178e3a54c0SPierre Jolivet 
24188e3a54c0SPierre Jolivet /*MC
24198e3a54c0SPierre Jolivet    PetscSafePointerPlusOffset - Checks that a pointer is not `NULL` before applying an offset
24208e3a54c0SPierre Jolivet 
24218e3a54c0SPierre Jolivet    Level: beginner
24228e3a54c0SPierre Jolivet 
24238e3a54c0SPierre Jolivet    Note:
24248e3a54c0SPierre Jolivet    This is needed to avoid errors with undefined-behavior sanitizers such as
24258e3a54c0SPierre Jolivet    UBSan, assuming PETSc has been configured with `-fsanitize=undefined` as part of the compiler flags
24268e3a54c0SPierre Jolivet M*/
24278e3a54c0SPierre Jolivet #define PetscSafePointerPlusOffset(ptr, offset) ((ptr) ? (ptr) + (offset) : NULL)
2428