Actual source code: petscsys.h

  1: /*
  2:    This is the main PETSc include file (for C and C++).  It is included by all
  3:    other PETSc include files, so it almost never has to be specifically included.
  4:    Portions of this code are under:
  5:    Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved.
  6: */
  7: #pragma once

  9: /*MC
 10:    PeOP - indicates an argument to a PETSc function is optional and one can pass `NULL` instead. This is used by the Fortran API generator

 12:    Level: developer

 14:    Example:
 15: .vb
 16:    PetscErrorCode XXXX(Vec v, PeOp PetscObject obj, PeOp PetscInt *idx, PeOp PetscInt *array[])
 17: .ve

 19:    Notes:
 20:    This is not part of the PETSc public API and should only be used in PETSc source code.

 22:    Put this in the function declaration in front of each variable that is optional

 24:    Developer Note:
 25:    Shortened form of PETSc optional

 27: .seealso: `PeNS`, `PeNSS`, `PetscCtxRt`, `PetscInitialize()`
 28: M*/
 29: #define PeOp

 31: /*MC
 32:    PeNS - indicates a function that does not use the PETSc standard arguments which make it easy to generate automatic language stubs for other languages

 34:    Level: developer

 36:    Notes:
 37:    This is not part of the PETSc public API and should only be used in PETSc source code.

 39:    Put this at the end of the function declaration closing parenthesis

 41:    Developer Note:
 42:    Shortened form of PETSc non-standard

 44: .seealso: `PeOp`, `PeNSS`, `PetscCtxRt`, `PetscInitialize()`
 45: M*/
 46: #define PeNS

 48: /*MC
 49:    PeNSS - indicates a function that needs a special treatment in the C-side stub when generating the binding for other languages

 51:    Level: developer

 53:    Notes:
 54:    This is not part of the PETSc public API and should only be used in PETSc source code.

 56:    Put this at the end of the function declaration closing parenthesis

 58:    It is similar to PeNS; in Fortran it will generate the Fortran interface definition automatically but not the C stub, which should be added manually under the appropriate `ftn-custom` directory

 60:    Developer Note:
 61:    Shortened form of PETSc non-standard stub

 63: .seealso: `PeOp`, `PeNS`, `PetscCtxRt`, `PetscInitialize()`
 64: M*/
 65: #define PeNSS

 67: /* ========================================================================== */
 68: /*
 69:    petscconf.h is contained in ${PETSC_ARCH}/include/petscconf.h it is
 70:    found automatically by the compiler due to the -I${PETSC_DIR}/${PETSC_ARCH}/include that
 71:    PETSc's makefiles add to the compiler rules.
 72:    For --prefix installs the directory ${PETSC_ARCH} does not exist and petscconf.h is in the same
 73:    directory as the other PETSc include files.
 74: */
 75: #include <petscconf.h>
 76: #include <petscpkg_version.h>
 77: #include <petscconf_poison.h>
 78: #include <petscfix.h>
 79: #include <petscmacros.h>

 81: /* SUBMANSEC = Sys */

 83: #if PetscDefined(DESIRE_FEATURE_TEST_MACROS)
 84:   /*
 85:    Feature test macros must be included before headers defined by IEEE Std 1003.1-2001
 86:    We only turn these in PETSc source files that require them by setting PETSC_DESIRE_FEATURE_TEST_MACROS
 87: */
 88:   #if PetscDefined(_POSIX_C_SOURCE_200112L) && !defined(_POSIX_C_SOURCE)
 89:     #define _POSIX_C_SOURCE 200112L
 90:   #endif
 91:   #if PetscDefined(_BSD_SOURCE) && !defined(_BSD_SOURCE)
 92:     #define _BSD_SOURCE
 93:   #endif
 94:   #if PetscDefined(_DEFAULT_SOURCE) && !defined(_DEFAULT_SOURCE)
 95:     #define _DEFAULT_SOURCE
 96:   #endif
 97:   #if PetscDefined(_GNU_SOURCE) && !defined(_GNU_SOURCE)
 98:     #define _GNU_SOURCE
 99:   #endif
100: #endif

102: #include <petscsystypes.h>

104: /* ========================================================================== */

106: /*
107:     Defines the interface to MPI allowing the use of all MPI functions.

109:     PETSc does not use the C++ binding of MPI at ALL. The following flag
110:     makes sure the C++ bindings are not included. The C++ bindings REQUIRE
111:     putting mpi.h before ANY C++ include files, we cannot control this
112:     with all PETSc users. Users who want to use the MPI C++ bindings can include
113:     mpicxx.h directly in their code
114: */
115: #if !defined(MPICH_SKIP_MPICXX)
116:   #define MPICH_SKIP_MPICXX 1
117: #endif
118: #if !defined(OMPI_SKIP_MPICXX)
119:   #define OMPI_SKIP_MPICXX 1
120: #endif
121: #if PetscDefined(HAVE_MPIUNI)
122: #include <petsc/mpiuni/mpi.h>
123: #else
124:   #include <mpi.h>
125: #endif

127: /*
128:    Perform various sanity checks that the correct mpi.h is being included at compile time.
129:    This usually happens because
130:       * either an unexpected mpi.h is in the default compiler path (i.e. in /usr/include) or
131:       * an extra include path -I/something (which contains the unexpected mpi.h) is being passed to the compiler
132:    Note: with MPICH and OpenMPI, accept versions [x.y.z, x+1.0.0) as compatible
133: */
134: #if PetscDefined(HAVE_MPIUNI)
135:   #if !defined(MPIUNI_H)
136:     #error "PETSc was configured with --with-mpi=0 but now appears to be compiling using a different mpi.h"
137:   #endif
138: #elif PetscDefined(HAVE_I_MPI)
139:   #if !defined(I_MPI_NUMVERSION)
140:     #error "PETSc was configured with I_MPI but now appears to be compiling using a non-I_MPI mpi.h"
141:   #elif I_MPI_NUMVERSION != PETSC_PKG_I_MPI_NUMVERSION
142:     #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"
143:   #endif
144: #elif PetscDefined(HAVE_MVAPICH2)
145:   #if !defined(MVAPICH2_NUMVERSION)
146:     #error "PETSc was configured with MVAPICH2 but now appears to be compiling using a non-MVAPICH2 mpi.h"
147:   #elif MVAPICH2_NUMVERSION != PETSC_PKG_MVAPICH2_NUMVERSION
148:     #error "PETSc was configured with one MVAPICH2 mpi.h version but now appears to be compiling using a different MVAPICH2 mpi.h version"
149:   #endif
150: #elif PetscDefined(HAVE_MPICH)
151:   #if !defined(MPICH_NUMVERSION) || defined(MVAPICH2_NUMVERSION) || defined(I_MPI_NUMVERSION)
152:     #error "PETSc was configured with MPICH but now appears to be compiling using a non-MPICH mpi.h"
153:   #elif PETSC_PKG_MPICH_VERSION_GT(MPICH_NUMVERSION / 10000000, MPICH_NUMVERSION / 100000 % 100, MPICH_NUMVERSION / 1000 % 100)
154:     #error "PETSc was configured with one MPICH mpi.h version but now appears to be compiling using an older MPICH mpi.h version"
155:   #elif PETSC_PKG_MPICH_VERSION_LT(MPICH_NUMVERSION / 10000000, 0, 0)
156:     #error "PETSc was configured with one MPICH mpi.h version but now appears to be compiling using a newer major MPICH mpi.h version"
157:   #endif
158: #elif PetscDefined(HAVE_OPENMPI)
159:   #if !defined(OMPI_MAJOR_VERSION)
160:     #error "PETSc was configured with Open MPI but now appears to be compiling using a non-Open MPI mpi.h"
161:   #elif PETSC_PKG_OPENMPI_VERSION_GT(OMPI_MAJOR_VERSION, OMPI_MINOR_VERSION, OMPI_RELEASE_VERSION)
162:     #error "PETSc was configured with one Open MPI mpi.h version but now appears to be compiling using an older Open MPI mpi.h version"
163:   #elif PETSC_PKG_OPENMPI_VERSION_LT(OMPI_MAJOR_VERSION, 0, 0)
164:     #error "PETSc was configured with one Open MPI mpi.h version but now appears to be compiling using a newer major Open MPI mpi.h version"
165:   #endif
166: #elif defined(PETSC_HAVE_MSMPI_VERSION)
167:   #if !defined(MSMPI_VER)
168:     #error "PETSc was configured with MSMPI but now appears to be compiling using a non-MSMPI mpi.h"
169:   #elif (MSMPI_VER != PETSC_HAVE_MSMPI_VERSION)
170:     #error "PETSc was configured with one MSMPI mpi.h version but now appears to be compiling using a different MSMPI mpi.h version"
171:   #endif
172: #elif defined(OMPI_MAJOR_VERSION) || defined(MPICH_NUMVERSION) || defined(MSMPI_VER)
173:   #error "PETSc was configured with undetermined MPI - but now appears to be compiling using any of Open MPI, MS-MPI or a MPICH variant"
174: #endif

176: /*
177:     Need to put stdio.h AFTER mpi.h for MPICH2 with C++ compiler
178:     see the top of mpicxx.h in the MPICH2 distribution.
179: */
180: #include <stdio.h>

182: /* MSMPI on 32-bit Microsoft Windows requires this yukky hack - that breaks MPI standard compliance */
183: #if !defined(MPIAPI)
184:   #define MPIAPI
185: #endif

187: PETSC_EXTERN MPI_Datatype MPIU_ENUM PETSC_ATTRIBUTE_MPI_TYPE_TAG(PetscEnum);
188: #define MPIU_BOOL MPI_C_BOOL PETSC_DEPRECATED_MACRO(3, 24, 0, "MPI_C_BOOL", )

190: PETSC_EXTERN MPI_Datatype MPIU_FORTRANADDR;

192: /*MC
193:    MPIU_INT - Portable `MPI_Datatype` corresponding to `PetscInt` independent of the precision of `PetscInt`

195:    Level: beginner

197:    Note:
198:    In MPI calls that require an `MPI_Datatype` that matches a `PetscInt` or array of `PetscInt` values, pass this value.

200: .seealso: `MPI_Datatype`, `PetscReal`, `PetscScalar`, `PetscComplex`, `PetscInt`, `MPIU_COUNT`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`
201: M*/
202: #if PetscDefined(USE_64BIT_INDICES)
203:   #define MPIU_INT MPIU_INT64
204: #else
205:   #define MPIU_INT MPI_INT
206: #endif

208: /*MC
209:    MPIU_COUNT - Portable `MPI_Datatype` corresponding to `PetscCount` independent of the precision of `PetscCount`

211:    Level: beginner

213:    Note:
214:    In MPI calls that require an `MPI_Datatype` that matches a `PetscCount` or array of `PetscCount` values, pass this value.

216:   Developer Note:
217:   It seems `MPI_AINT` is unsigned so this may be the wrong choice here since `PetscCount` is signed

219: .seealso: `MPI_Datatype`, `PetscReal`, `PetscScalar`, `PetscComplex`, `PetscInt`, `MPIU_INT`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`,
220:           `MPI_Count`
221: M*/
222: #define MPIU_COUNT MPI_AINT

224: /*
225:     For the rare cases when one needs to send a size_t object with MPI
226: */
227: PETSC_EXTERN MPI_Datatype MPIU_SIZE_T PETSC_ATTRIBUTE_MPI_TYPE_TAG(size_t);

229: /*
230:       You can use PETSC_STDOUT as a replacement of stdout. You can also change
231:     the value of PETSC_STDOUT to redirect all standard output elsewhere
232: */
233: PETSC_EXTERN FILE *PETSC_STDOUT;

235: /*
236:       You can use PETSC_STDERR as a replacement of stderr. You can also change
237:     the value of PETSC_STDERR to redirect all standard error elsewhere
238: */
239: PETSC_EXTERN FILE *PETSC_STDERR;

241: /*
242:   Handle inclusion when using clang compiler with CUDA support
243:   __float128 is not available for the device
244: */
245: #if defined(__clang__) && (defined(__CUDA_ARCH__) || defined(__HIPCC__))
246:   #define PETSC_SKIP_REAL___FLOAT128
247: #endif

249: /*
250:     Declare extern C stuff after including external header files
251: */

253: PETSC_EXTERN PetscBool PETSC_RUNNING_ON_VALGRIND;
254: /*
255:     Defines elementary mathematics functions and constants.
256: */
257: #include <petscmath.h>

259: /*MC
260:    PETSC_IGNORE - same as `NULL`, means PETSc will ignore this argument

262:    Level: beginner

264:    Note:
265:    Accepted by many PETSc functions to not set a parameter and instead use a default value

267:    Fortran Note:
268:    Use `PETSC_NULL_INTEGER`, `PETSC_NULL_SCALAR` etc

270: .seealso: `PETSC_DECIDE`, `PETSC_DEFAULT`, `PETSC_DETERMINE`
271: M*/
272: #define PETSC_IGNORE PETSC_NULLPTR
273: #define PETSC_NULL   PETSC_DEPRECATED_MACRO(3, 19, 0, "PETSC_NULLPTR", ) PETSC_NULLPTR

275: /*MC
276:    PETSC_UNLIMITED - standard way of passing an integer or floating point parameter to indicate PETSc there is no bound on the value allowed

278:    Level: beginner

280:    Example Usage:
281: .vb
282:    KSPSetTolerances(ksp, PETSC_CURRENT, PETSC_CURRENT, PETSC_UNLIMITED, PETSC_UNLIMITED);
283: .ve
284:   indicates that the solver is allowed to take any number of iterations and will not stop early no matter how the residual gets.

286:    Fortran Note:
287:    Use `PETSC_UNLIMITED_INTEGER` or `PETSC_UNLIMITED_REAL`.

289: .seealso: `PETSC_DEFAULT`, `PETSC_IGNORE`, `PETSC_DETERMINE`, `PETSC_DECIDE`
290: M*/

292: /*MC
293:    PETSC_DECIDE - standard way of passing an integer or floating point parameter to indicate PETSc should determine an appropriate value

295:    Level: beginner

297:    Example Usage:
298: .vb
299:    VecSetSizes(ksp, PETSC_DECIDE, 10);
300: .ve
301:   indicates that the global size of the vector is 10 and the local size will be automatically determined so that the sum of the
302:   local sizes is the global size, see `PetscSplitOwnership()`.

304:    Fortran Note:
305:    Use `PETSC_DECIDE_INTEGER` or `PETSC_DECIDE_REAL`.

307: .seealso: `PETSC_DEFAULT`, `PETSC_IGNORE`, `PETSC_DETERMINE`, `PETSC_UNLIMITED`
308: M*/

310: /*MC
311:    PETSC_DETERMINE - standard way of passing an integer or floating point parameter to indicate PETSc should determine an appropriate value

313:    Level: beginner

315:     Example Usage:
316: .vb
317:    VecSetSizes(ksp, 10, PETSC_DETERMINE);
318: .ve
319:   indicates that the local size of the vector is 10 and the global size will be automatically summing up all the local sizes.

321:    Note:
322:    Same as `PETSC_DECIDE`

324:    Fortran Note:
325:    Use `PETSC_DETERMINE_INTEGER` or `PETSC_DETERMINE_REAL`.

327:    Developer Note:
328:    I would like to use const `PetscInt` `PETSC_DETERMINE` = `PETSC_DECIDE`; but for
329:    some reason this is not allowed by the standard even though `PETSC_DECIDE` is a constant value.

331: .seealso: `PETSC_DECIDE`, `PETSC_DEFAULT`, `PETSC_IGNORE`, `VecSetSizes()`, `PETSC_UNLIMITED`
332: M*/

334: /*MC
335:    PETSC_CURRENT - standard way of indicating to an object not to change the current value of the parameter in the object

337:    Level: beginner

339:    Note:
340:    Use `PETSC_DECIDE` to use the value that was set by PETSc when the object's type was set

342:    Fortran Note:
343:    Use `PETSC_CURRENT_INTEGER` or `PETSC_CURRENT_REAL`.

345: .seealso: `PETSC_DECIDE`, `PETSC_IGNORE`, `PETSC_DETERMINE`, `PETSC_DEFAULT`, `PETSC_UNLIMITED`
346: M*/

348: /*MC
349:    PETSC_DEFAULT - deprecated, see `PETSC_CURRENT` and `PETSC_DETERMINE`

351:    Level: beginner

353:    Note:
354:    The name is confusing since it tells the object to continue to use the value it is using, not the default value when the object's type was set.

356:    Developer Note:
357:    Unfortunately this was used for two different purposes in the past, to actually trigger the use of a default value or to continue the
358:    use of currently set value (in, for example, `KSPSetTolerances()`.

360: .seealso: `PETSC_DECIDE`, `PETSC_IGNORE`, `PETSC_DETERMINE`, `PETSC_CURRENT`, `PETSC_UNLIMITED`
361: M*/

363: /* These MUST be preprocessor defines! see https://gitlab.com/petsc/petsc/-/issues/1370 */
364: #define PETSC_DECIDE    (-1)
365: #define PETSC_DETERMINE PETSC_DECIDE
366: #define PETSC_CURRENT   (-2)
367: #define PETSC_UNLIMITED (-3)
368: /*  PETSC_DEFAULT is deprecated in favor of PETSC_CURRENT for use in KSPSetTolerances() and similar functions */
369: #define PETSC_DEFAULT PETSC_CURRENT

371: /*MC
372:    PETSC_COMM_WORLD - the equivalent of the `MPI_COMM_WORLD` communicator which represents all the processes that PETSc knows about.

374:    Level: beginner

376:    Notes:
377:    By default `PETSC_COMM_WORLD` and `MPI_COMM_WORLD` are identical unless you wish to
378:    run PETSc on ONLY a subset of `MPI_COMM_WORLD`. In that case create your new (smaller)
379:    communicator, call it, say comm, and set `PETSC_COMM_WORLD` = comm BEFORE calling
380:    `PetscInitialize()`, but after `MPI_Init()` has been called.

382:    The value of `PETSC_COMM_WORLD` should never be used or accessed before `PetscInitialize()`
383:    is called because it may not have a valid value yet.

385: .seealso: `PETSC_COMM_SELF`
386: M*/
387: PETSC_EXTERN MPI_Comm PETSC_COMM_WORLD;

389: /*MC
390:    PETSC_COMM_SELF - This is always `MPI_COMM_SELF`

392:    Level: beginner

394:    Note:
395:    Do not USE/access or set this variable before `PetscInitialize()` has been called.

397: .seealso: `PETSC_COMM_WORLD`
398: M*/
399: #define PETSC_COMM_SELF MPI_COMM_SELF

401: /*MC
402:    PETSC_MPI_THREAD_REQUIRED - the required threading support used if PETSc initializes MPI with `MPI_Init_thread()`.

404:    Deprecated, use `PetscSetMPIThreadRequiredType()`

406:    Level: beginner

408:    Note:
409:    By default `PETSC_MPI_THREAD_REQUIRED` equals `MPI_THREAD_FUNNELED` when the MPI implementation provides `MPI_Init_thread()`, otherwise it equals `MPI_THREAD_SINGLE`

411: .seealso: `PetscInitialize()`, `PetscSetMPIThreadRequiredType()`
412: M*/
413: PETSC_EXTERN PetscMPIInt PETSC_MPI_THREAD_REQUIRED;

415: /*MC
416:    PetscBeganMPI - indicates if PETSc initialized MPI using `MPI_Init()` during `PetscInitialize()` or if MPI was already initialized with `MPI_Init()`

418:    Synopsis:
419: #include <petscsys.h>
420:    PetscBool PetscBeganMPI;

422:    No Fortran Support

424:    Level: developer

426:    Note:
427:    `MPI_Init()` can never be called after `PetscInitialize()`

429: .seealso: `PetscInitialize()`, `PetscInitializeCalled`
430: M*/
431: PETSC_EXTERN PetscBool PetscBeganMPI;

433: PETSC_EXTERN PetscBool PetscErrorHandlingInitialized;
434: PETSC_EXTERN PetscBool PetscInitializeCalled;
435: PETSC_EXTERN PetscBool PetscFinalizeCalled;
436: PETSC_EXTERN PetscBool PetscViennaCLSynchronize;

438: PETSC_EXTERN PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*)(MPI_Comm), PetscErrorCode (*)(MPI_Comm));
439: PETSC_EXTERN PetscErrorCode PetscCommDuplicate(MPI_Comm, MPI_Comm *, int *);
440: PETSC_EXTERN PetscErrorCode PetscCommDestroy(MPI_Comm *);
441: PETSC_EXTERN PetscErrorCode PetscCommGetComm(MPI_Comm, MPI_Comm *);
442: PETSC_EXTERN PetscErrorCode PetscCommRestoreComm(MPI_Comm, MPI_Comm *);

444: #if PetscDefined(HAVE_KOKKOS)
445: PETSC_EXTERN PetscErrorCode PetscKokkosInitializeCheck(void); /* Initialize Kokkos if not yet. */
446: #endif

448: #if PetscDefined(HAVE_NVSHMEM)
449: PETSC_EXTERN PetscBool      PetscBeganNvshmem;
450: PETSC_EXTERN PetscBool      PetscNvshmemInitialized;
451: PETSC_EXTERN PetscErrorCode PetscNvshmemFinalize(void);
452: #endif

454: #if PetscDefined(HAVE_ELEMENTAL)
455: PETSC_EXTERN PetscErrorCode PetscElementalInitializePackage(void);
456: PETSC_EXTERN PetscErrorCode PetscElementalInitialized(PetscBool *);
457: PETSC_EXTERN PetscErrorCode PetscElementalFinalizePackage(void);
458: #endif

460: /*MC
461:    PetscMalloc - Allocates memory for use with PETSc. One should use `PetscNew()`, `PetscMalloc1()` or `PetscCalloc1()` usually instead of `PetscMalloc()`

463:    Synopsis:
464: #include <petscsys.h>
465:    PetscErrorCode PetscMalloc(size_t m, void **result)

467:    Not Collective

469:    Input Parameter:
470: .  m - number of bytes to allocate

472:    Output Parameter:
473: .  result - memory allocated

475:    Level: beginner

477:    Notes:
478:    Memory is always allocated at least double aligned

480:    It is safe to allocate with an m of 0 and pass the resulting pointer to `PetscFree()`.
481:    However, the pointer should never be dereferenced or the program will crash.

483:    Developer Note:
484:    All the `PetscMallocN()` routines actually call `PetscMalloc()` behind the scenes.

486:    Except for data structures that store information about the PETSc options database all memory allocated by PETSc is
487:    obtained with `PetscMalloc()` or `PetscCalloc()`

489: .seealso: `PetscFree()`, `PetscNew()`, `PetscCalloc()`
490: M*/
491: #define PetscMalloc(a, b) ((*PetscTrMalloc)((a), PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))

493: /*MC
494:    PetscRealloc - Reallocates memory

496:    Synopsis:
497: #include <petscsys.h>
498:    PetscErrorCode PetscRealloc(size_t m, void **result)

500:    Not Collective

502:    Input Parameters:
503: +  m      - number of bytes to allocate
504: -  result - previous memory

506:    Output Parameter:
507: .  result - new memory allocated

509:    Level: developer

511:    Notes:
512:    `results` must have already been obtained with `PetscMalloc()`

514:    Memory is always allocated at least double aligned

516: .seealso: `PetscMalloc()`, `PetscFree()`, `PetscNew()`
517: M*/
518: #define PetscRealloc(a, b) ((*PetscTrRealloc)((a), __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))

520: /*MC
521:    PetscAddrAlign - Rounds up an address to `PETSC_MEMALIGN` alignment

523:    Synopsis:
524: #include <petscsys.h>
525:    void *PetscAddrAlign(void *addr)

527:    Not Collective

529:    Input Parameter:
530: .  addr - address to align (any pointer type)

532:    Level: developer

534: .seealso: `PetscMallocAlign()`
535: M*/
536: #define PetscAddrAlign(a) ((void *)((((PETSC_UINTPTR_T)(a)) + (PETSC_MEMALIGN - 1)) & ~(PETSC_MEMALIGN - 1)))

538: /*MC
539:    PetscCalloc - Allocates a cleared (zeroed) memory region aligned to `PETSC_MEMALIGN`, similar to `PetscMalloc()`

541:    Synopsis:
542: #include <petscsys.h>
543:    PetscErrorCode PetscCalloc(size_t m, void **result)

545:    Not Collective

547:    Input Parameter:
548: .  m - number of bytes to allocate

550:    Output Parameter:
551: .  result - memory allocated

553:    Level: beginner

555:    Notes:
556:    Memory is always allocated at least double aligned. This macro is useful in allocating memory pointed by void pointers

558:    It is safe to allocate with an m of 0 and pass the resulting pointer to `PetscFree()`.

560:    However, the pointer should never be dereferenced or the program will crash.

562:    Developer Note:
563:    All `PetscCallocN()` routines call `PetscCalloc()` behind the scenes.

565: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`
566: M*/
567: #define PetscCalloc(m, result) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)m), (result))

569: /*MC
570:    PetscMalloc1 - Allocates an array of memory aligned to `PETSC_MEMALIGN`

572:    Synopsis:
573: #include <petscsys.h>
574:    PetscErrorCode PetscMalloc1(size_t m1, type **r1)

576:    Not Collective

578:    Input Parameter:
579: .  m1 - number of elements to allocate  (may be zero)

581:    Output Parameter:
582: .  r1 - memory allocated

584:    Level: beginner

586:    Note:
587:    This uses `sizeof()` of the memory type requested to determine the total memory to be allocated; therefore, you should not
588:    multiply the number of elements requested by the `sizeof()` the type. For example, use
589: .vb
590:   PetscInt *id;
591:   PetscMalloc1(10,&id);
592: .ve
593:        not
594: .vb
595:   PetscInt *id;
596:   PetscMalloc1(10*sizeof(PetscInt),&id);
597: .ve

599:   Does not zero the memory allocated, use `PetscCalloc1()` to obtain memory that has been zeroed.

601:   The `PetscMalloc[N]()` and `PetscCalloc[N]()` take an argument of type `size_t`! However, most codes use `value`, computed via `int` or `PetscInt` variables. This can overflow in
602:   32bit `int` computation - while computation in 64bit `size_t` would not overflow!
603:   It's best if any arithmetic that is done for size computations is done with `size_t` type - avoiding arithmetic overflow!

605:   `PetscMalloc[N]()` and `PetscCalloc[N]()` attempt to work-around this by casting the first variable to `size_t`.
606:   This works for most expressions, but not all, such as
607: .vb
608:   PetscInt *id, a, b;
609:   PetscMalloc1(use_a_squared ? a * a * b : a * b, &id); // use_a_squared is cast to size_t, but a and b are still PetscInt
610:   PetscMalloc1(a + b * b, &id); // a is cast to size_t, but b * b is performed at PetscInt precision first due to order-of-operations
611: .ve

613:   These expressions should either be avoided, or appropriately cast variables to `size_t`:
614: .vb
615:   PetscInt *id, a, b;
616:   PetscMalloc1(use_a_squared ? (size_t)a * a * b : (size_t)a * b, &id); // Cast a to size_t before multiplication
617:   PetscMalloc1(b * b + a, &id); // b is automatically cast to size_t and order-of-operations ensures size_t precision is maintained
618: .ve

620: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
621: M*/
622: #define PetscMalloc1(m1, r1) PetscMallocA(1, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1))

624: /*MC
625:    PetscCalloc1 - Allocates a cleared (zeroed) array of memory aligned to `PETSC_MEMALIGN`

627:    Synopsis:
628: #include <petscsys.h>
629:    PetscErrorCode PetscCalloc1(size_t m1, type **r1)

631:    Not Collective

633:    Input Parameter:
634: .  m1 - number of elements to allocate in 1st chunk  (may be zero)

636:    Output Parameter:
637: .  r1 - memory allocated

639:    Level: beginner

641:    Note:
642:    See `PetscMalloc1()` for more details on usage.

644: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
645: M*/
646: #define PetscCalloc1(m1, r1) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (r1))

648: /*MC
649:    PetscMalloc2 - Allocates 2 arrays of memory both aligned to `PETSC_MEMALIGN`

651:    Synopsis:
652: #include <petscsys.h>
653:    PetscErrorCode PetscMalloc2(size_t m1, type **r1, size_t m2, type **r2)

655:    Not Collective

657:    Input Parameters:
658: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
659: -  m2 - number of elements to allocate in 2nd chunk  (may be zero)

661:    Output Parameters:
662: +  r1 - memory allocated in first chunk
663: -  r2 - memory allocated in second chunk

665:    Level: developer

667: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
668: M*/
669: #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))

671: /*MC
672:    PetscCalloc2 - Allocates 2 cleared (zeroed) arrays of memory both aligned to `PETSC_MEMALIGN`

674:    Synopsis:
675: #include <petscsys.h>
676:    PetscErrorCode PetscCalloc2(size_t m1, type **r1, size_t m2, type **r2)

678:    Not Collective

680:    Input Parameters:
681: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
682: -  m2 - number of elements to allocate in 2nd chunk  (may be zero)

684:    Output Parameters:
685: +  r1 - memory allocated in first chunk
686: -  r2 - memory allocated in second chunk

688:    Level: developer

690: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
691: M*/
692: #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))

694: /*MC
695:    PetscMalloc3 - Allocates 3 arrays of memory, all aligned to `PETSC_MEMALIGN`

697:    Synopsis:
698: #include <petscsys.h>
699:    PetscErrorCode PetscMalloc3(size_t m1, type **r1, size_t m2, type **r2, size_t m3, type **r3)

701:    Not Collective

703:    Input Parameters:
704: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
705: .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
706: -  m3 - number of elements to allocate in 3rd chunk  (may be zero)

708:    Output Parameters:
709: +  r1 - memory allocated in first chunk
710: .  r2 - memory allocated in second chunk
711: -  r3 - memory allocated in third chunk

713:    Level: developer

715: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc3()`, `PetscFree3()`
716: M*/
717: #define PetscMalloc3(m1, r1, m2, r2, m3, r3) \
718:   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))

720: /*MC
721:    PetscCalloc3 - Allocates 3 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`

723:    Synopsis:
724: #include <petscsys.h>
725:    PetscErrorCode PetscCalloc3(size_t m1, type **r1, size_t m2, type **r2, size_t m3, type **r3)

727:    Not Collective

729:    Input Parameters:
730: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
731: .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
732: -  m3 - number of elements to allocate in 3rd chunk  (may be zero)

734:    Output Parameters:
735: +  r1 - memory allocated in first chunk
736: .  r2 - memory allocated in second chunk
737: -  r3 - memory allocated in third chunk

739:    Level: developer

741: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc2()`, `PetscMalloc3()`, `PetscFree3()`
742: M*/
743: #define PetscCalloc3(m1, r1, m2, r2, m3, r3) \
744:   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))

746: /*MC
747:    PetscMalloc4 - Allocates 4 arrays of memory, all aligned to `PETSC_MEMALIGN`

749:    Synopsis:
750: #include <petscsys.h>
751:    PetscErrorCode PetscMalloc4(size_t m1, type **r1, size_t m2, type **r2, size_t m3, type **r3, size_t m4, type **r4)

753:    Not Collective

755:    Input Parameters:
756: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
757: .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
758: .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
759: -  m4 - number of elements to allocate in 4th chunk  (may be zero)

761:    Output Parameters:
762: +  r1 - memory allocated in first chunk
763: .  r2 - memory allocated in second chunk
764: .  r3 - memory allocated in third chunk
765: -  r4 - memory allocated in fourth chunk

767:    Level: developer

769: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
770: M*/
771: #define PetscMalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
772:   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))

774: /*MC
775:    PetscCalloc4 - Allocates 4 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`

777:    Synopsis:
778: #include <petscsys.h>
779:    PetscErrorCode PetscCalloc4(size_t m1, type **r1, size_t m2, type **r2, size_t m3, type **r3, size_t m4, type **r4)

781:    Not Collective

783:    Input Parameters:
784: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
785: .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
786: .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
787: -  m4 - number of elements to allocate in 4th chunk  (may be zero)

789:    Output Parameters:
790: +  r1 - memory allocated in first chunk
791: .  r2 - memory allocated in second chunk
792: .  r3 - memory allocated in third chunk
793: -  r4 - memory allocated in fourth chunk

795:    Level: developer

797: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
798: M*/
799: #define PetscCalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
800:   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))

802: /*MC
803:    PetscMalloc5 - Allocates 5 arrays of memory, all aligned to `PETSC_MEMALIGN`

805:    Synopsis:
806: #include <petscsys.h>
807:    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)

809:    Not Collective

811:    Input Parameters:
812: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
813: .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
814: .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
815: .  m4 - number of elements to allocate in 4th chunk  (may be zero)
816: -  m5 - number of elements to allocate in 5th chunk  (may be zero)

818:    Output Parameters:
819: +  r1 - memory allocated in first chunk
820: .  r2 - memory allocated in second chunk
821: .  r3 - memory allocated in third chunk
822: .  r4 - memory allocated in fourth chunk
823: -  r5 - memory allocated in fifth chunk

825:    Level: developer

827: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc5()`, `PetscFree5()`
828: M*/
829: #define PetscMalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
830:   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))

832: /*MC
833:    PetscCalloc5 - Allocates 5 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`

835:    Synopsis:
836: #include <petscsys.h>
837:    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)

839:    Not Collective

841:    Input Parameters:
842: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
843: .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
844: .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
845: .  m4 - number of elements to allocate in 4th chunk  (may be zero)
846: -  m5 - number of elements to allocate in 5th chunk  (may be zero)

848:    Output Parameters:
849: +  r1 - memory allocated in first chunk
850: .  r2 - memory allocated in second chunk
851: .  r3 - memory allocated in third chunk
852: .  r4 - memory allocated in fourth chunk
853: -  r5 - memory allocated in fifth chunk

855:    Level: developer

857: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc5()`, `PetscFree5()`
858: M*/
859: #define PetscCalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
860:   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))

862: /*MC
863:    PetscMalloc6 - Allocates 6 arrays of memory, all aligned to `PETSC_MEMALIGN`

865:    Synopsis:
866: #include <petscsys.h>
867:    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)

869:    Not Collective

871:    Input Parameters:
872: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
873: .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
874: .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
875: .  m4 - number of elements to allocate in 4th chunk  (may be zero)
876: .  m5 - number of elements to allocate in 5th chunk  (may be zero)
877: -  m6 - number of elements to allocate in 6th chunk  (may be zero)

879:    Output Parameteasr:
880: +  r1 - memory allocated in first chunk
881: .  r2 - memory allocated in second chunk
882: .  r3 - memory allocated in third chunk
883: .  r4 - memory allocated in fourth chunk
884: .  r5 - memory allocated in fifth chunk
885: -  r6 - memory allocated in sixth chunk

887:    Level: developer

889: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc6()`, `PetscFree3()`, `PetscFree4()`, `PetscFree5()`, `PetscFree6()`
890: M*/
891: #define PetscMalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
892:   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))

894: /*MC
895:    PetscCalloc6 - Allocates 6 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`

897:    Synopsis:
898: #include <petscsys.h>
899:    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)

901:    Not Collective

903:    Input Parameters:
904: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
905: .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
906: .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
907: .  m4 - number of elements to allocate in 4th chunk  (may be zero)
908: .  m5 - number of elements to allocate in 5th chunk  (may be zero)
909: -  m6 - number of elements to allocate in 6th chunk  (may be zero)

911:    Output Parameters:
912: +  r1 - memory allocated in first chunk
913: .  r2 - memory allocated in second chunk
914: .  r3 - memory allocated in third chunk
915: .  r4 - memory allocated in fourth chunk
916: .  r5 - memory allocated in fifth chunk
917: -  r6 - memory allocated in sixth chunk

919:    Level: developer

921: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc6()`, `PetscFree6()`
922: M*/
923: #define PetscCalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
924:   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))

926: /*MC
927:    PetscMalloc7 - Allocates 7 arrays of memory, all aligned to `PETSC_MEMALIGN`

929:    Synopsis:
930: #include <petscsys.h>
931:    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)

933:    Not Collective

935:    Input Parameters:
936: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
937: .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
938: .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
939: .  m4 - number of elements to allocate in 4th chunk  (may be zero)
940: .  m5 - number of elements to allocate in 5th chunk  (may be zero)
941: .  m6 - number of elements to allocate in 6th chunk  (may be zero)
942: -  m7 - number of elements to allocate in 7th chunk  (may be zero)

944:    Output Parameters:
945: +  r1 - memory allocated in first chunk
946: .  r2 - memory allocated in second chunk
947: .  r3 - memory allocated in third chunk
948: .  r4 - memory allocated in fourth chunk
949: .  r5 - memory allocated in fifth chunk
950: .  r6 - memory allocated in sixth chunk
951: -  r7 - memory allocated in seventh chunk

953:    Level: developer

955: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc7()`, `PetscFree7()`
956: M*/
957: #define PetscMalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
958:   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))

960: /*MC
961:    PetscCalloc7 - Allocates 7 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`

963:    Synopsis:
964: #include <petscsys.h>
965:    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)

967:    Not Collective

969:    Input Parameters:
970: +  m1 - number of elements to allocate in 1st chunk  (may be zero)
971: .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
972: .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
973: .  m4 - number of elements to allocate in 4th chunk  (may be zero)
974: .  m5 - number of elements to allocate in 5th chunk  (may be zero)
975: .  m6 - number of elements to allocate in 6th chunk  (may be zero)
976: -  m7 - number of elements to allocate in 7th chunk  (may be zero)

978:    Output Parameters:
979: +  r1 - memory allocated in first chunk
980: .  r2 - memory allocated in second chunk
981: .  r3 - memory allocated in third chunk
982: .  r4 - memory allocated in fourth chunk
983: .  r5 - memory allocated in fifth chunk
984: .  r6 - memory allocated in sixth chunk
985: -  r7 - memory allocated in seventh chunk

987:    Level: developer

989: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc7()`, `PetscFree7()`
990: M*/
991: #define PetscCalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
992:   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))

994: /*MC
995:    PetscNew - Allocates memory of a particular type, zeros the memory! Aligned to `PETSC_MEMALIGN`

997:    Synopsis:
998: #include <petscsys.h>
999:    PetscErrorCode PetscNew(type **result)

1001:    Not Collective

1003:    Output Parameter:
1004: .  result - memory allocated, sized to match pointer `type`

1006:    Level: beginner

1008:    Developer Note:
1009:    Calls `PetscCalloc()` with the appropriate memory size obtained from `type`

1011: .seealso: `PetscFree()`, `PetscMalloc()`, `PetscCall()`, `PetscCalloc1()`, `PetscMalloc1()`
1012: M*/
1013: #define PetscNew(b) PetscCalloc1(1, (b))

1015: /*MC
1016:    PetscNewLog - Deprecated alias for `PetscNew()`; previously logged the allocation against a `PetscObject`

1018:    Synopsis:
1019: #include <petscsys.h>
1020:    PetscErrorCode PetscNewLog(PetscObject obj, type **result)

1022:    Not Collective

1024:    Input Parameter:
1025: .  obj - the `PetscObject` to which the allocation used to be charged (now ignored)

1027:    Output Parameter:
1028: .  result - the newly allocated memory, sized to match `type`

1030:    Level: deprecated

1032:    Note:
1033:    This macro is retained only for source compatibility. Use `PetscNew()` instead; PETSc no longer associates allocations with a specific `PetscObject`.

1035: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscFree()`
1036: M*/
1037: #define PetscNewLog(o, b) PETSC_DEPRECATED_MACRO(3, 18, 0, "PetscNew()", ) PetscNew(b)

1039: /*MC
1040:    PetscFree - Frees memory

1042:    Synopsis:
1043: #include <petscsys.h>
1044:    PetscErrorCode PetscFree(void *memory)

1046:    Not Collective

1048:    Input Parameter:
1049: .   memory - memory to free (the pointer is ALWAYS set to `NULL` upon success)

1051:    Level: beginner

1053:    Notes:
1054:    Do not free memory obtained with `PetscMalloc2()`, `PetscCalloc2()` etc, they must be freed with `PetscFree2()` etc.

1056:    It is safe to call `PetscFree()` on a `NULL` pointer.

1058: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc1()`
1059: M*/
1060: #define PetscFree(a) ((PetscErrorCode)((*PetscTrFree)((void *)(a), __LINE__, PETSC_FUNCTION_NAME, __FILE__) || ((a) = PETSC_NULLPTR, PETSC_SUCCESS)))

1062: /*MC
1063:    PetscFree2 - Frees 2 chunks of memory obtained with `PetscMalloc2()`

1065:    Synopsis:
1066: #include <petscsys.h>
1067:    PetscErrorCode PetscFree2(void *memory1, void *memory2)

1069:    Not Collective

1071:    Input Parameters:
1072: +   memory1 - memory to free
1073: -   memory2 - 2nd memory to free

1075:    Level: developer

1077:    Notes:
1078:     Memory must have been obtained with `PetscMalloc2()`

1080:     The arguments need to be in the same order as they were in the call to `PetscMalloc2()`

1082: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`
1083: M*/
1084: #define PetscFree2(m1, m2) PetscFreeA(2, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2))

1086: /*MC
1087:    PetscFree3 - Frees 3 chunks of memory obtained with `PetscMalloc3()`

1089:    Synopsis:
1090: #include <petscsys.h>
1091:    PetscErrorCode PetscFree3(void *memory1, void *memory2, void *memory3)

1093:    Not Collective

1095:    Input Parameters:
1096: +   memory1 - memory to free
1097: .   memory2 - 2nd memory to free
1098: -   memory3 - 3rd memory to free

1100:    Level: developer

1102:    Notes:
1103:     Memory must have been obtained with `PetscMalloc3()`

1105:     The arguments need to be in the same order as they were in the call to `PetscMalloc3()`

1107: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`
1108: M*/
1109: #define PetscFree3(m1, m2, m3) PetscFreeA(3, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3))

1111: /*MC
1112:    PetscFree4 - Frees 4 chunks of memory obtained with `PetscMalloc4()`

1114:    Synopsis:
1115: #include <petscsys.h>
1116:    PetscErrorCode PetscFree4(void *m1, void *m2, void *m3, void *m4)

1118:    Not Collective

1120:    Input Parameters:
1121: +   m1 - memory to free
1122: .   m2 - 2nd memory to free
1123: .   m3 - 3rd memory to free
1124: -   m4 - 4th memory to free

1126:    Level: developer

1128:    Notes:
1129:     Memory must have been obtained with `PetscMalloc4()`

1131:     The arguments need to be in the same order as they were in the call to `PetscMalloc4()`

1133: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`
1134: M*/
1135: #define PetscFree4(m1, m2, m3, m4) PetscFreeA(4, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4))

1137: /*MC
1138:    PetscFree5 - Frees 5 chunks of memory obtained with `PetscMalloc5()`

1140:    Synopsis:
1141: #include <petscsys.h>
1142:    PetscErrorCode PetscFree5(void *m1, void *m2, void *m3, void *m4, void *m5)

1144:    Not Collective

1146:    Input Parameters:
1147: +   m1 - memory to free
1148: .   m2 - 2nd memory to free
1149: .   m3 - 3rd memory to free
1150: .   m4 - 4th memory to free
1151: -   m5 - 5th memory to free

1153:    Level: developer

1155:    Notes:
1156:     Memory must have been obtained with `PetscMalloc5()`

1158:     The arguments need to be in the same order as they were in the call to `PetscMalloc5()`

1160: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`
1161: M*/
1162: #define PetscFree5(m1, m2, m3, m4, m5) PetscFreeA(5, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5))

1164: /*MC
1165:    PetscFree6 - Frees 6 chunks of memory obtained with `PetscMalloc6()`

1167:    Synopsis:
1168: #include <petscsys.h>
1169:    PetscErrorCode PetscFree6(void *m1, void *m2, void *m3, void *m4, void *m5, void *m6)

1171:    Not Collective

1173:    Input Parameters:
1174: +   m1 - memory to free
1175: .   m2 - 2nd memory to free
1176: .   m3 - 3rd memory to free
1177: .   m4 - 4th memory to free
1178: .   m5 - 5th memory to free
1179: -   m6 - 6th memory to free

1181:    Level: developer

1183:    Notes:
1184:     Memory must have been obtained with `PetscMalloc6()`

1186:     The arguments need to be in the same order as they were in the call to `PetscMalloc6()`

1188: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`
1189: M*/
1190: #define PetscFree6(m1, m2, m3, m4, m5, m6) PetscFreeA(6, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5), &(m6))

1192: /*MC
1193:    PetscFree7 - Frees 7 chunks of memory obtained with `PetscMalloc7()`

1195:    Synopsis:
1196: #include <petscsys.h>
1197:    PetscErrorCode PetscFree7(void *m1, void *m2, void *m3, void *m4, void *m5, void *m6, void *m7)

1199:    Not Collective

1201:    Input Parameters:
1202: +   m1 - memory to free
1203: .   m2 - 2nd memory to free
1204: .   m3 - 3rd memory to free
1205: .   m4 - 4th memory to free
1206: .   m5 - 5th memory to free
1207: .   m6 - 6th memory to free
1208: -   m7 - 7th memory to free

1210:    Level: developer

1212:    Notes:
1213:     Memory must have been obtained with `PetscMalloc7()`

1215:     The arguments need to be in the same order as they were in the call to `PetscMalloc7()`

1217: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`,
1218:           `PetscMalloc7()`
1219: M*/
1220: #define PetscFree7(m1, m2, m3, m4, m5, m6, m7) PetscFreeA(7, __LINE__, PETSC_FUNCTION_NAME, __FILE__, &(m1), &(m2), &(m3), &(m4), &(m5), &(m6), &(m7))

1222: PETSC_EXTERN PetscErrorCode PetscMallocA(int, PetscBool, int, const char *, const char *, size_t, void *, ...);
1223: PETSC_EXTERN PetscErrorCode PetscFreeA(int, int, const char *, const char *, void *, ...);
1224: PETSC_EXTERN PetscErrorCode (*PetscTrMalloc)(size_t, PetscBool, int, const char[], const char[], void **);
1225: PETSC_EXTERN PetscErrorCode (*PetscTrFree)(void *, int, const char[], const char[]);
1226: PETSC_EXTERN PetscErrorCode (*PetscTrRealloc)(size_t, int, const char[], const char[], void **);
1227: PETSC_EXTERN PetscErrorCode PetscMallocSetCoalesce(PetscBool);
1228: 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 **));
1229: PETSC_EXTERN PetscErrorCode PetscMallocClear(void);

1231: /*
1232:   Unlike PetscMallocSet and PetscMallocClear which overwrite the existing settings, these two functions save the previous choice of allocator, and should be used in pair.
1233: */
1234: PETSC_EXTERN PetscErrorCode PetscMallocSetDRAM(void);
1235: PETSC_EXTERN PetscErrorCode PetscMallocResetDRAM(void);
1236: #if PetscDefined(HAVE_CUDA)
1237: PETSC_EXTERN PetscErrorCode PetscMallocSetCUDAHost(void);
1238: PETSC_EXTERN PetscErrorCode PetscMallocResetCUDAHost(void);
1239: #endif
1240: #if PetscDefined(HAVE_HIP)
1241: PETSC_EXTERN PetscErrorCode PetscMallocSetHIPHost(void);
1242: PETSC_EXTERN PetscErrorCode PetscMallocResetHIPHost(void);
1243: #endif

1245: #define MPIU_PETSCLOGDOUBLE  MPI_DOUBLE
1246: #define MPIU_2PETSCLOGDOUBLE MPI_2DOUBLE_PRECISION

1248: /*
1249:    Routines for tracing memory corruption/bleeding with default PETSc memory allocation
1250: */
1251: PETSC_EXTERN PetscErrorCode PetscMallocDump(FILE *);
1252: PETSC_EXTERN PetscErrorCode PetscMallocView(FILE *);
1253: PETSC_EXTERN PetscErrorCode PetscMallocGetCurrentUsage(PetscLogDouble *);
1254: PETSC_EXTERN PetscErrorCode PetscMallocGetMaximumUsage(PetscLogDouble *);
1255: PETSC_EXTERN PetscErrorCode PetscMallocPushMaximumUsage(int);
1256: PETSC_EXTERN PetscErrorCode PetscMallocPopMaximumUsage(int, PetscLogDouble *);
1257: PETSC_EXTERN PetscErrorCode PetscMallocSetDebug(PetscBool, PetscBool);
1258: PETSC_EXTERN PetscErrorCode PetscMallocGetDebug(PetscBool *, PetscBool *, PetscBool *);
1259: PETSC_EXTERN PetscErrorCode PetscMallocValidate(int, const char[], const char[]);
1260: PETSC_EXTERN PetscErrorCode PetscMallocViewSet(PetscLogDouble);
1261: PETSC_EXTERN PetscErrorCode PetscMallocViewGet(PetscBool *);
1262: PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeSet(PetscBool);
1263: PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeGet(PetscBool *);

1265: PETSC_EXTERN PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType, MPI_Datatype *);
1266: PETSC_EXTERN PetscErrorCode PetscMPIDataTypeToPetscDataType(MPI_Datatype, PetscDataType *);
1267: PETSC_EXTERN PetscErrorCode PetscDataTypeGetSize(PetscDataType, size_t *);
1268: PETSC_EXTERN PetscErrorCode PetscDataTypeFromString(const char *, PetscDataType *, PetscBool *);

1270: /*
1271:    These are MPI operations for MPI_Allreduce() etc
1272: */
1273: PETSC_EXTERN MPI_Op MPIU_MAXSUM_OP;
1274: #if PetscDefined(USE_REAL___FLOAT128) || PetscDefined(USE_REAL___FP16)
1275: PETSC_EXTERN MPI_Op MPIU_SUM;
1276: PETSC_EXTERN MPI_Op MPIU_MAX;
1277: PETSC_EXTERN MPI_Op MPIU_MIN;
1278: #else
1279:   #define MPIU_SUM MPI_SUM
1280:   #define MPIU_MAX MPI_MAX
1281:   #define MPIU_MIN MPI_MIN
1282: #endif
1283: PETSC_EXTERN MPI_Op         Petsc_Garbage_SetIntersectOp;
1284: PETSC_EXTERN PetscErrorCode PetscMaxSum(MPI_Comm, const PetscInt[], PetscInt *, PetscInt *);

1286: #if (PetscDefined(HAVE_REAL___FLOAT128) && !PetscDefined(SKIP_REAL___FLOAT128)) || (PetscDefined(HAVE_REAL___FP16) && !PetscDefined(SKIP_REAL___FP16))
1287: /*MC
1288:    MPIU_SUM___FP16___FLOAT128 - `MPI_Op` that acts as a replacement for `MPI_SUM` with
1289:    custom `MPI_Datatype` `MPIU___FLOAT128`, `MPIU___COMPLEX128`, and `MPIU___FP16`.

1291:    Level: advanced

1293:    Developer Note:
1294:    This should be unified with `MPIU_SUM`

1296: .seealso: `MPI_Op`, `MPI_Datatype`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`
1297: M*/
1298: PETSC_EXTERN MPI_Op MPIU_SUM___FP16___FLOAT128;
1299: #endif

1301: /*
1302:      These are so that in extern C code we can cast function pointers to non-extern C
1303:    function pointers. Since the regular C++ code expects its function pointers to be C++
1304: */

1306: /*S
1307:   PetscVoidFn - A prototype of a `void fn(void)` function

1309:   Level: advanced

1311:   Notes:
1312:   `PetscVoidFn *` plays the role of `void *` for function pointers in the PETSc API that do not return an error code.
1313:   It is used where a function pointer is needed but it is not possible to use the full prototype of the function.

1315:   `PetscErrorCodeFn` is similar to `PetscVoidFn` but should be used when the function returns a `PetscErrorCode`

1317:   The deprecated `PetscVoidFunction` works as a replacement for `PetscVoidFn` *.

1319:   The deprecated `PetscVoidStarFunction` works as a replacement for `PetscVoidFn` **.

1321: .seealso: `PetscErrorCodeFn`, `PetscObject`, `PetscObjectDestroy()`
1322: S*/
1323: PETSC_EXTERN_TYPEDEF typedef void PetscVoidFn(void);

1325: PETSC_EXTERN_TYPEDEF typedef PetscVoidFn  *PetscVoidFunction;
1326: PETSC_EXTERN_TYPEDEF typedef PetscVoidFn **PetscVoidStarFunction;

1328: /*S
1329:   PetscErrorCodeFn - a function typedef that represents abstractly a function that returns a PETSc error code
1330:   and takes any number of arguments. Since C/C++ has no way to express this concept, it is implemented as `void (fn)(void)`.

1332:   Level: advanced

1334:   Notes:
1335:   `PetscErrorCodeFn *` plays the role of `void *` for function pointers in the PETSc API that return an error code.
1336:   It is used where a function pointer is needed but it is not possible to use the full prototype of the function,
1337:   for example `VecSetOperation()`.

1339:   `PetscVoidFn` is similar to `PetscErrorCodeFn` but should be used when the function does not return a `PetscErrorCode`.

1341:   Developer Notes:
1342:   This function type is equivalent to `PetscVoidFn`*.

1344:   At the C/C++ syntax level this construct adds nothing of value to the PETSc source code. It provides a way, at the abstract
1345:   PETSc API level, to indicate specifically functions that return PETSc error codes as opposed to any C/C++ function.

1347: .seealso: `PetscVoidFn`, `PetscObject`, `PetscObjectDestroy()`, `VecSetOperation()`
1348: S*/
1349: PETSC_EXTERN_TYPEDEF typedef void PetscErrorCodeFn(void);

1351: /*
1352:     Defines PETSc error handling.
1353: */
1354: #include <petscerror.h>

1356: PETSC_EXTERN PetscBool   PetscCIEnabled;                    /* code is running in the PETSc test harness CI */
1357: PETSC_EXTERN PetscBool   PetscCIEnabledPortableErrorOutput; /* error output is stripped to ensure portability of error messages across systems */
1358: PETSC_EXTERN const char *PetscCIFilename(const char *);
1359: PETSC_EXTERN int         PetscCILinenumber(int);

1361: #define PETSC_SMALLEST_CLASSID 1211211
1362: PETSC_EXTERN PetscClassId   PETSC_LARGEST_CLASSID;
1363: PETSC_EXTERN PetscClassId   PETSC_OBJECT_CLASSID;
1364: PETSC_EXTERN PetscErrorCode PetscClassIdRegister(const char[], PetscClassId *);

1366: /*
1367:    Routines that get memory usage information from the OS
1368: */
1369: PETSC_EXTERN PetscErrorCode PetscMemoryGetCurrentUsage(PetscLogDouble *);
1370: PETSC_EXTERN PetscErrorCode PetscMemoryGetMaximumUsage(PetscLogDouble *);
1371: PETSC_EXTERN PetscErrorCode PetscMemorySetGetMaximumUsage(void);
1372: PETSC_EXTERN PetscErrorCode PetscMemoryTrace(const char[]);

1374: PETSC_EXTERN PetscErrorCode PetscSleep(PetscReal);

1376: /*
1377:    Initialization of PETSc
1378: */
1379: PETSC_EXTERN PetscErrorCode PetscInitialize(int *, char ***, const char[], const char[]);
1380: PETSC_EXTERN PetscErrorCode PetscInitializeNoPointers(int, char *[], const char[], const char[]);
1381: PETSC_EXTERN PetscErrorCode PetscInitializeNoArguments(void);
1382: PETSC_EXTERN PetscErrorCode PetscInitialized(PetscBool *);
1383: PETSC_EXTERN PetscErrorCode PetscFinalized(PetscBool *);
1384: PETSC_EXTERN PetscErrorCode PetscFinalize(void);
1385: PETSC_EXTERN PetscErrorCode PetscInitializeFortran(void);
1386: PETSC_EXTERN PetscErrorCode PetscGetArgs(int *, char ***);
1387: PETSC_EXTERN PetscErrorCode PetscGetArguments(char ***);
1388: PETSC_EXTERN PetscErrorCode PetscFreeArguments(char **);
1389: PETSC_EXTERN PetscErrorCode PetscSetMPIThreadRequiredType(PetscMPIInt);

1391: PETSC_EXTERN PetscErrorCode PetscEnd(void);
1392: PETSC_EXTERN PetscErrorCode PetscSysInitializePackage(void);
1393: PETSC_EXTERN PetscErrorCode PetscSysFinalizePackage(void);

1395: PETSC_EXTERN PetscErrorCode PetscPythonInitialize(const char[], const char[]);
1396: PETSC_EXTERN PetscErrorCode PetscPythonFinalize(void);
1397: PETSC_EXTERN PetscErrorCode PetscPythonPrintError(void);
1398: PETSC_EXTERN PetscErrorCode PetscPythonMonitorSet(PetscObject, const char[]);

1400: /*
1401:     Functions that can act on any PETSc object.
1402: */
1403: PETSC_EXTERN PetscErrorCode PetscObjectDestroy(PetscObject *);
1404: PETSC_EXTERN PetscErrorCode PetscObjectGetComm(PetscObject, MPI_Comm *);
1405: PETSC_EXTERN PetscErrorCode PetscObjectGetClassId(PetscObject, PetscClassId *);
1406: PETSC_EXTERN PetscErrorCode PetscObjectGetClassName(PetscObject, const char *[]);
1407: PETSC_EXTERN PetscErrorCode PetscObjectGetType(PetscObject, const char *[]);
1408: PETSC_EXTERN PetscErrorCode PetscObjectSetName(PetscObject, const char[]);
1409: PETSC_EXTERN PetscErrorCode PetscObjectGetName(PetscObject, const char *[]);
1410: PETSC_EXTERN PetscErrorCode PetscObjectSetTabLevel(PetscObject, PetscInt);
1411: PETSC_EXTERN PetscErrorCode PetscObjectGetTabLevel(PetscObject, PetscInt *);
1412: PETSC_EXTERN PetscErrorCode PetscObjectIncrementTabLevel(PetscObject, PetscObject, PetscInt);
1413: PETSC_EXTERN PetscErrorCode PetscObjectReference(PetscObject);
1414: PETSC_EXTERN PetscErrorCode PetscObjectGetReference(PetscObject, PetscInt *);
1415: PETSC_EXTERN PetscErrorCode PetscObjectDereference(PetscObject);
1416: PETSC_EXTERN PetscErrorCode PetscObjectGetNewTag(PetscObject, PetscMPIInt *);
1417: PETSC_EXTERN PetscErrorCode PetscObjectCompose(PetscObject, const char[], PetscObject);
1418: PETSC_EXTERN PetscErrorCode PetscObjectRemoveReference(PetscObject, const char[]);
1419: PETSC_EXTERN PetscErrorCode PetscObjectQuery(PetscObject, const char[], PetscObject *);
1420: PETSC_EXTERN PetscErrorCode PetscObjectComposeFunction_Private(PetscObject, const char[], PetscErrorCodeFn *);
1421: #define PetscObjectComposeFunction(a, b, ...) PetscObjectComposeFunction_Private((a), (b), (PetscErrorCodeFn *)(__VA_ARGS__))
1422: PETSC_EXTERN PetscErrorCode PetscObjectSetFromOptions(PetscObject);
1423: PETSC_EXTERN PetscErrorCode PetscObjectSetUp(PetscObject);
1424: PETSC_EXTERN PetscErrorCode PetscObjectSetPrintedOptions(PetscObject);
1425: PETSC_EXTERN PetscErrorCode PetscObjectInheritPrintedOptions(PetscObject, PetscObject);
1426: PETSC_EXTERN PetscErrorCode PetscCommGetNewTag(MPI_Comm, PetscMPIInt *);
1427: PETSC_EXTERN PetscErrorCode PetscObjectGetId(PetscObject, PetscObjectId *);
1428: PETSC_EXTERN PetscErrorCode PetscObjectCompareId(PetscObject, PetscObjectId, PetscBool *);

1430: /*MC
1431:    PetscObjectParameterSetDefault - sets a parameter default value in a `PetscObject` to a new default value.
1432:    If the current value matches the old default value, then the current value is also set to the new value.

1434:    No Fortran Support

1436:    Synopsis:
1437: #include <petscsys.h>
1438:    PetscBool PetscObjectParameterSetDefault(PetscObject obj, char* NAME, PetscReal value);

1440:    Input Parameters:
1441: +  obj - the `PetscObject`
1442: .  NAME - the name of the parameter, unquoted
1443: -  value - the new value

1445:    Level: developer

1447:    Notes:
1448:    The defaults for an object are the values set when the object's type is set.

1450:    This should only be used in object constructors, such as, `SNESCreate_NGS()`.

1452:    This only works for parameters that are declared in the struct with `PetscObjectParameterDeclare()`

1454: .seealso: `PetscObjectParameterDeclare()`, `PetscInitialize()`, `PetscFinalize()`, `PetscObject`, `SNESParametersInitialize()`
1455: M*/
1456: #define PetscObjectParameterSetDefault(obj, NAME, value) \
1457:   do { \
1458:     if (obj->NAME == obj->default_##NAME) obj->NAME = value; \
1459:     obj->default_##NAME = value; \
1460:   } while (0)

1462: /*MC
1463:    PetscObjectParameterDeclare - declares a parameter in a `PetscObject` and a location to store its default

1465:    No Fortran Support

1467:    Synopsis:
1468: #include <petscsys.h>
1469:    PetscObjectParameterDeclare(type, NAME)

1471:    Input Parameters:
1472: +  type - the type of the parameter, for example `PetscInt`
1473: -  NAME - the name of the parameter, unquoted

1475:    Level: developer.

1477: .seealso: `PetscObjectParameterSetDefault()`, `PetscInitialize()`, `PetscFinalize()`, `PetscObject`, `SNESParametersInitialize()`
1478: M*/
1479: #define PetscObjectParameterDeclare(type, NAME) type NAME, default_##NAME

1481: /*MC
1482:    PetscObjectParameterDeclarePtr - declares a pointer-valued parameter inside a `PetscObject` together with a slot for its default

1484:    No Fortran Support

1486:    Synopsis:
1487: #include <petscsys.h>
1488:    PetscObjectParameterDeclarePtr(type, NAME)

1490:    Input Parameters:
1491: +  type - the pointee type of the parameter, for example `PetscReal`
1492: -  NAME - the name of the parameter, unquoted

1494:    Level: developer

1496:    Note:
1497:    Equivalent to `PetscObjectParameterDeclare()` but for pointer-valued parameters: declares `type *NAME` and `type *default_##NAME` so that `PetscObjectParameterSetDefault()` can record and propagate the default value.

1499: .seealso: `PetscObjectParameterDeclare()`, `PetscObjectParameterSetDefault()`, `PetscInitialize()`, `PetscFinalize()`, `PetscObject`, `SNESParametersInitialize()`
1500: M*/
1501: #define PetscObjectParameterDeclarePtr(type, NAME) type *NAME, *default_##NAME

1503: /*MC
1504:    PetscCtx - indicates an argument that can be a pointer to any C struct (or Fortran derived type).

1506:    Level: developer

1508:    Notes:
1509:    This should not be used for arrays of unknown type.

1511:    Fortran Notes:
1512:    A Fortran code that calls a function with a `PetscCtx` argument would declare the variable `ctx` with
1513: .vb
1514:    type(AppType) :: ctx
1515: .ve
1516:    where `AppType` is a Fortran derived type. Or the argument can be a `PetscObject`.

1518:    Developer Note:
1519:    `PetscCtx` is used instead of `void *` in PETSc code to enhance the clarity of the PETSc source code since `void *` serves so many different roles.
1520:    The getAPI() code processor also uses the variable type to generate correct bindings for other languages.

1522: .seealso: [](sec_fortran_context), `PetscCtxRt`, PetscCtxDestroyFn()`, `PeOp`, `PeNS`, `PetscInitialize()`, `DMGetApplicationContext()`,
1523:           `DMSetApplicationContextDestroy()`
1524: M*/
1525: typedef void *PetscCtx;

1527: /*MC
1528:    PetscCtxRt - indicates an argument that returns a pointer to a C struct (or Fortran derived type) which is generally an application context

1530:    Level: developer

1532:    Notes:
1533:    A PETSc object (in C or Fortran) can be used as a PETSc context

1535:    This should not be used for functions that return pointers to arrays of unknown type. Thus it is used for, for example,
1536:    `KSPGetApplicationContext()` but not used for `DMNetworkGetComponent()`

1538:    A PETSc object (in C or Fortran) can be used as a PETSc context

1540:    It is also used for functions that destroy an application context. For example, the destroy function passed to `DMSetApplicationContextDestroy()`
1541:    which has a prototype of `PetscCtxDestroyFn()`

1543:    This typedef is not part of the PETSc public API and should only be used in PETSc source code.

1545:    For pointers to arrays of unknown type and for functions that return PETSc internal objects that are opaque to users, such
1546:    as `KSPMonitorDynamicToleranceCreate()` a `void **` should be used.

1548:    Fortran Notes:
1549:    A Fortran code that calls a function with a `PetscCtxRt` argument must declare the variable `ctx` with
1550: .vb
1551:    type(AppType), pointer :: ctx
1552: .ve
1553:    where `AppType` is a Fortran derived type.

1555:    If one passes a PETSc function with a `PetscCtxRt` argument as an argument in Fortran one must use the function named suffixed with `Cptr`,
1556:    for example `KSPConvergedDefaultDestroyCptr`, see src/ksp/ksp/tutorials/ex1f.F90.

1558:    Developer Notes:
1559:    C++ compilers generate a warning or error if one passes a pointer to a pointer to a specific type (instead of `void`), for example,
1560: .vb
1561:    extern calledfunction(void **);
1562:    SomeCtx *ctx;
1563:    calledfunction(&ctx);   << warning that it is passing a pointer to a pointer to a SomeCtx instead of a void **
1564: .ve
1565:    By using the common practice of prototyping the function as
1566: .vb
1567:    extern calledfunction(void *);
1568: .ve
1569:    the warning message is averted.

1571:    `PetscCtxRt` is used instead of `void *` in PETSc code to enhance the clarity of the PETSc source code since `void *` serves so many different roles.
1572:    The getAPI() code processor also uses the variable type to generate correct bindings for other languages.

1574:    The Fortran C stub and Fortran interface definition generated for functions with a `PetscCtxRt` argument are the C function name suffixed with
1575:    `Cptr`, for example `KSPConvergedDefaultDestroyCptr`. The Fortran user API is a macro with the original C function name, for example,
1576:    `KSPConvergedDefaultDestroy` that calls the  `KSPConvergedDefaultDestroyCptr` version and then calls `c_f_pointer()` to handle the equivalent of a `void**` cast
1577:    to the users Fortran derived type argument.

1579: .seealso: [](sec_fortran_context), `PetscCtx`, `PetscCtxDestroyFn()`, `PeOp`, `PeNS`, `PetscInitialize()`, `DMGetApplicationContext()`,
1580:           `DMSetApplicationContextDestroy()`
1581: M*/
1582: typedef void *PetscCtxRt;

1584: /*S
1585:   PetscCtxDestroyFn - A prototype of a `PetscErrorCode (*)(PetscCtxRt)` function that is used to free application contexts

1587:   Level: intermediate

1589:   Notes:
1590:   Used in the prototype of functions such as `DMSetApplicationContextDestroy()`

1592:   The function argument is a `PetscCtxRt` which is psychologically equivalent to a `void **` meaning that this function is called with a pointer to
1593:   the application context (which is itself a pointer) thus the destroy implementation must first reference the context via, for example,
1594:   `*(AppCtx **)arg`. Note that syntactically `PetscCtxRt` is defined as a `void *`, this is because C++ does
1595:   not accept passing a pointer to a pointer to a `void**` but it does accept passing a pointer to a pointer to `void *`.

1597:   PETSc destroy functions take the address of the context (rather than just the context) so that that the destroy function can "zero the pointer" when
1598:   appropriate, preventing accidental later use of a dangling pointer.

1600: .seealso: `PetscObject`, `PetscCtxDestroyDefault()`, `PetscObjectDestroy()`, `DMSetApplicationContextDestroy()`
1601: S*/
1602: PETSC_EXTERN_TYPEDEF typedef PetscErrorCode PetscCtxDestroyFn(PetscCtxRt ctx);

1604: PETSC_EXTERN PetscCtxDestroyFn PetscCtxDestroyDefault;
1605: PETSC_DEPRECATED_FUNCTION(3, 23, 0, "PetscCtxDestroyDefault()", ) static inline PetscErrorCode PetscContainerCtxDestroyDefault(PetscCtxRt a)
1606: {
1607:   return PetscCtxDestroyDefault(a);
1608: }

1610: PETSC_EXTERN PetscErrorCode PetscMonitorCompare(PetscErrorCode (*)(void), void *, PetscCtxDestroyFn *, PetscErrorCode (*)(void), void *, PetscCtxDestroyFn *, PetscBool *);

1612: #include <petscviewertypes.h>
1613: #include <petscoptions.h>

1615: PETSC_EXTERN PetscErrorCode PetscMallocTraceSet(PetscViewer, PetscBool, PetscLogDouble);
1616: PETSC_EXTERN PetscErrorCode PetscMallocTraceGet(PetscBool *);

1618: PETSC_EXTERN PetscErrorCode PetscObjectsListGetGlobalNumbering(MPI_Comm, PetscInt, PetscObject[], PetscInt *, PetscInt *);

1620: PETSC_EXTERN PetscErrorCode PetscMemoryView(PetscViewer, const char[]);
1621: PETSC_EXTERN PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject, PetscViewer);
1622: PETSC_EXTERN PetscErrorCode PetscObjectView(PetscObject, PetscViewer);
1623: /*MC
1624:    PetscObjectQueryFunction - Retrieve a function pointer that was previously composed onto a `PetscObject` with `PetscObjectComposeFunction()`

1626:    Synopsis:
1627: #include <petscsys.h>
1628:    PetscErrorCode PetscObjectQueryFunction(PetscObject obj, const char name[], void (**fptr)(void))

1630:    Logically Collective

1632:    Input Parameters:
1633: +  obj  - the `PetscObject`
1634: -  name - the string under which the function was composed

1636:    Output Parameter:
1637: .  fptr - the address into which the function pointer is written; set to `NULL` if no function of this name is composed onto `obj`

1639:    Level: advanced

1641:    Note:
1642:    The macro casts `fptr` to the internal `PetscErrorCodeFn **` representation before calling `PetscObjectQueryFunction_Private()`
1643:    so that any function-pointer type can be supplied without an explicit cast.

1645: .seealso: `PetscObjectComposeFunction()`, `PetscObjectHasFunction()`, `PetscObject`
1646: M*/
1647: #define PetscObjectQueryFunction(obj, name, fptr) PetscObjectQueryFunction_Private((obj), (name), (PetscErrorCodeFn **)(fptr))
1648: PETSC_EXTERN PetscErrorCode PetscObjectHasFunction(PetscObject, const char[], PetscBool *);
1649: PETSC_EXTERN PetscErrorCode PetscObjectQueryFunction_Private(PetscObject, const char[], PetscErrorCodeFn **);
1650: PETSC_EXTERN PetscErrorCode PetscObjectSetOptionsPrefix(PetscObject, const char[]);
1651: PETSC_EXTERN PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject, const char[]);
1652: PETSC_EXTERN PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject, const char[]);
1653: PETSC_EXTERN PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject, const char *[]);
1654: PETSC_EXTERN PetscErrorCode PetscObjectChangeTypeName(PetscObject, const char[]);
1655: PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroy(PetscObject);
1656: PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroyAll(void);
1657: PETSC_EXTERN PetscErrorCode PetscObjectViewFromOptions(PetscObject, PetscObject, const char[]);
1658: PETSC_EXTERN PetscErrorCode PetscObjectViewSynchronizedFromOptions(PetscObject, PetscObject, const char[]);
1659: PETSC_EXTERN PetscErrorCode PetscObjectName(PetscObject);
1660: PETSC_EXTERN PetscErrorCode PetscObjectTypeCompare(PetscObject, const char[], PetscBool *);
1661: PETSC_EXTERN PetscErrorCode PetscObjectObjectTypeCompare(PetscObject, PetscObject, PetscBool *);
1662: PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompare(PetscObject, const char[], PetscBool *);
1663: PETSC_EXTERN PetscErrorCode PetscObjectTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1664: PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1665: PETSC_EXTERN PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*)(void));
1666: PETSC_EXTERN PetscErrorCode PetscRegisterFinalizeAll(void);

1668: #if PetscDefined(HAVE_SAWS)
1669: PETSC_EXTERN PetscErrorCode PetscSAWsBlock(void);
1670: PETSC_EXTERN PetscErrorCode PetscObjectSAWsViewOff(PetscObject);
1671: PETSC_EXTERN PetscErrorCode PetscObjectSAWsSetBlock(PetscObject, PetscBool);
1672: PETSC_EXTERN PetscErrorCode PetscObjectSAWsBlock(PetscObject);
1673: PETSC_EXTERN PetscErrorCode PetscObjectSAWsGrantAccess(PetscObject);
1674: PETSC_EXTERN PetscErrorCode PetscObjectSAWsTakeAccess(PetscObject);
1675: PETSC_EXTERN void           PetscStackSAWsGrantAccess(void);
1676: PETSC_EXTERN void           PetscStackSAWsTakeAccess(void);
1677: PETSC_EXTERN PetscErrorCode PetscStackViewSAWs(void);
1678: PETSC_EXTERN PetscErrorCode PetscStackSAWsViewOff(void);

1680: #else
1681:   #define PetscSAWsBlock()                  PETSC_SUCCESS
1682:   #define PetscObjectSAWsViewOff(obj)       PETSC_SUCCESS
1683:   #define PetscObjectSAWsSetBlock(obj, flg) PETSC_SUCCESS
1684:   #define PetscObjectSAWsBlock(obj)         PETSC_SUCCESS
1685:   #define PetscObjectSAWsGrantAccess(obj)   PETSC_SUCCESS
1686:   #define PetscObjectSAWsTakeAccess(obj)    PETSC_SUCCESS
1687:   #define PetscStackViewSAWs()              PETSC_SUCCESS
1688:   #define PetscStackSAWsViewOff()           PETSC_SUCCESS
1689:   #define PetscStackSAWsTakeAccess()
1690:   #define PetscStackSAWsGrantAccess()

1692: #endif

1694: PETSC_EXTERN PetscErrorCode PetscDLOpen(const char[], PetscDLMode, PetscDLHandle *);
1695: PETSC_EXTERN PetscErrorCode PetscDLClose(PetscDLHandle *);
1696: PETSC_EXTERN PetscErrorCode PetscDLSym(PetscDLHandle, const char[], void **);
1697: PETSC_EXTERN PetscErrorCode PetscDLAddr(PetscVoidFn *, char *[]);
1698: PETSC_EXTERN PetscErrorCode PetscDemangleSymbol(const char *, char *[]);

1700: PETSC_EXTERN PetscErrorCode PetscMallocGetStack(void *, PetscStack **);

1702: PETSC_EXTERN PetscErrorCode PetscObjectsDump(FILE *, PetscBool);
1703: PETSC_EXTERN PetscErrorCode PetscObjectsView(PetscViewer);
1704: PETSC_EXTERN PetscErrorCode PetscObjectsGetObject(const char *, PetscObject *, const char *[]);
1705: PETSC_EXTERN PetscErrorCode PetscObjectListDestroy(PetscObjectList *);
1706: PETSC_EXTERN PetscErrorCode PetscObjectListFind(PetscObjectList, const char[], PetscObject *);
1707: PETSC_EXTERN PetscErrorCode PetscObjectListReverseFind(PetscObjectList, PetscObject, const char *[], PetscBool *);
1708: PETSC_EXTERN PetscErrorCode PetscObjectListAdd(PetscObjectList *, const char[], PetscObject);
1709: PETSC_EXTERN PetscErrorCode PetscObjectListRemoveReference(PetscObjectList *, const char[]);
1710: PETSC_EXTERN PetscErrorCode PetscObjectListDuplicate(PetscObjectList, PetscObjectList *);

1712: /*
1713:     Dynamic library lists. Lists of names of routines in objects or in dynamic
1714:   link libraries that will be loaded as needed.
1715: */

1717: /*MC
1718:    PetscFunctionListAdd - Add a function pointer to a `PetscFunctionList`, under a string name

1720:    Synopsis:
1721: #include <petscsys.h>
1722:    PetscErrorCode PetscFunctionListAdd(PetscFunctionList *list, const char name[], void (*fptr)(void))

1724:    Not Collective

1726:    Input Parameters:
1727: +  list - the function list; if `*list` is `NULL`, a new list is created
1728: .  name - string under which to register the function
1729: -  fptr - the function pointer; if `NULL`, any previous registration under `name` is removed

1731:    Level: developer

1733:    Notes:
1734:    Users who wish to register new classes for use by a particular PETSc component (e.g., `SNES`) should generally call the registration routine for that particular component (e.g., `SNESRegister()`) instead of calling `PetscFunctionListAdd()` directly.

1736:    The macro casts `fptr` to the internal `PetscErrorCodeFn *` representation before calling `PetscFunctionListAdd_Private()` so that any error-code-returning function-pointer type may be supplied without an explicit cast.

1738: .seealso: `PetscFunctionList`, `PetscFunctionListFind()`, `PetscFunctionListDestroy()`, `PetscFunctionListDuplicate()`, `PetscFunctionListView()`, `PetscObjectComposeFunction()`,
1739:           `SNESRegister()`, `KSPRegister()`, `PCRegister()`, `TSRegister()`
1740: M*/
1741: #define PetscFunctionListAdd(list, name, fptr) PetscFunctionListAdd_Private((list), (name), (PetscErrorCodeFn *)(fptr))
1742: PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *, const char[], PetscErrorCodeFn *);
1743: PETSC_EXTERN PetscErrorCode PetscFunctionListDestroy(PetscFunctionList *);
1744: PETSC_EXTERN PetscErrorCode PetscFunctionListClear(PetscFunctionList);
1745: /*MC
1746:    PetscFunctionListFind - Look up a function pointer in a `PetscFunctionList` by its registered string name

1748:    Synopsis:
1749: #include <petscsys.h>
1750:    PetscErrorCode PetscFunctionListFind(PetscFunctionList list, const char name[], void (**fptr)(void))

1752:    Not Collective; No Fortran Support

1754:    Input Parameters:
1755: +  list - the function list
1756: -  name - string under which the function was registered with `PetscFunctionListAdd()`

1758:    Output Parameter:
1759: .  fptr - the address into which the function pointer is written; set to `NULL` if no function of this name is registered

1761:    Level: developer

1763:    Note:
1764:    The macro casts `fptr` to the internal `PetscErrorCodeFn **` representation before calling `PetscFunctionListFind_Private()` so that any function-pointer type can be supplied without an explicit cast.

1766: .seealso: `PetscFunctionList`, `PetscFunctionListAdd()`, `PetscFunctionListDestroy()`, `PetscObjectQueryFunction()`
1767: M*/
1768: #define PetscFunctionListFind(list, name, fptr) PetscFunctionListFind_Private((list), (name), (PetscErrorCodeFn **)(fptr))
1769: PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList, const char[], PetscErrorCodeFn **);
1770: PETSC_EXTERN PetscErrorCode PetscFunctionListPrintTypes(MPI_Comm, FILE *, const char[], const char[], const char[], const char[], PetscFunctionList, const char[], const char[]);
1771: PETSC_EXTERN PetscErrorCode PetscFunctionListDuplicate(PetscFunctionList, PetscFunctionList *);
1772: PETSC_EXTERN PetscErrorCode PetscFunctionListView(PetscFunctionList, PetscViewer);
1773: PETSC_EXTERN PetscErrorCode PetscFunctionListGet(PetscFunctionList, const char ***, int *);
1774: PETSC_EXTERN PetscErrorCode PetscFunctionListPrintNonEmpty(PetscFunctionList);
1775: PETSC_EXTERN PetscErrorCode PetscFunctionListPrintAll(void);

1777: PETSC_EXTERN PetscDLLibrary PetscDLLibrariesLoaded;
1778: PETSC_EXTERN PetscErrorCode PetscDLLibraryAppend(MPI_Comm, PetscDLLibrary *, const char[]);
1779: PETSC_EXTERN PetscErrorCode PetscDLLibraryPrepend(MPI_Comm, PetscDLLibrary *, const char[]);
1780: PETSC_EXTERN PetscErrorCode PetscDLLibrarySym(MPI_Comm, PetscDLLibrary *, const char[], const char[], void **);
1781: PETSC_EXTERN PetscErrorCode PetscDLLibraryPrintPath(PetscDLLibrary);
1782: PETSC_EXTERN PetscErrorCode PetscDLLibraryRetrieve(MPI_Comm, const char[], char *, size_t, PetscBool *);
1783: PETSC_EXTERN PetscErrorCode PetscDLLibraryOpen(MPI_Comm, const char[], PetscDLLibrary *);
1784: PETSC_EXTERN PetscErrorCode PetscDLLibraryClose(PetscDLLibrary);

1786: /*
1787:      Useful utility routines
1788: */
1789: PETSC_EXTERN PetscErrorCode PetscSplitOwnership(MPI_Comm, PetscInt *, PetscInt *);
1790: PETSC_EXTERN PetscErrorCode PetscSplitOwnershipBlock(MPI_Comm, PetscInt, PetscInt *, PetscInt *);
1791: PETSC_EXTERN PetscErrorCode PetscSplitOwnershipEqual(MPI_Comm, PetscInt *, PetscInt *);
1792: PETSC_EXTERN PetscErrorCode PetscSequentialPhaseBegin(MPI_Comm, PetscMPIInt);
1793: PETSC_EXTERN PetscErrorCode PetscSequentialPhaseEnd(MPI_Comm, PetscMPIInt);
1794: PETSC_EXTERN PetscErrorCode PetscBarrier(PetscObject);
1795: PETSC_EXTERN PetscErrorCode PetscMPIDump(FILE *);
1796: PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxInt(MPI_Comm, const PetscInt[2], PetscInt[2]);
1797: PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxReal(MPI_Comm, const PetscReal[2], PetscReal[2]);

1799: /*MC
1800:     PetscNot - negates a logical type value and returns result as a `PetscBool`

1802:     Level: beginner

1804:     Note:
1805:     This is useful in cases like
1806: .vb
1807:      int        *a;
1808:      PetscBool  flag = PetscNot(a)
1809: .ve
1810:      where !a would not return a `PetscBool` because we cannot provide a cast from int to `PetscBool` in C.

1812: .seealso: `PetscBool`, `PETSC_TRUE`, `PETSC_FALSE`
1813: M*/
1814: #define PetscNot(a) ((a) ? PETSC_FALSE : PETSC_TRUE)

1816: /*MC
1817:    PetscHelpPrintf - Prints help messages.

1819:    Synopsis:
1820: #include <petscsys.h>
1821:    PetscErrorCode (*PetscHelpPrintf)(MPI_Comm comm, const char format[], args);

1823:    Not Collective, only applies on MPI rank 0; No Fortran Support

1825:    Input Parameters:
1826: +  comm - the MPI communicator over which the help message is printed
1827: .  format - the usual printf() format string
1828: -  args - arguments to be printed

1830:    Level: developer

1832:    Notes:
1833:    You can change how help messages are printed by replacing the function pointer with a function that does not simply write to stdout.

1835:    To use, write your own function, for example,
1836: .vb
1837:    PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....)
1838:    {
1839:      PetscFunctionReturn(PETSC_SUCCESS);
1840:    }
1841: .ve
1842: then do the assignment
1843: .vb
1844:   PetscHelpPrintf = mypetschelpprintf;
1845: .ve

1847:   You can do the assignment before `PetscInitialize()`.

1849:   The default routine used is called `PetscHelpPrintfDefault()`.

1851: .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscErrorPrintf()`, `PetscHelpPrintfDefault()`
1852: M*/
1853: PETSC_EXTERN PetscErrorCode (*PetscHelpPrintf)(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);

1855: /*
1856:      Defines PETSc profiling.
1857: */
1858: #include <petsclog.h>

1860: /*
1861:       Simple PETSc parallel IO for ASCII printing
1862: */
1863: PETSC_EXTERN PetscErrorCode PetscFixFilename(const char[], char[]);
1864: PETSC_EXTERN PetscErrorCode PetscFOpen(MPI_Comm, const char[], const char[], FILE **);
1865: PETSC_EXTERN PetscErrorCode PetscFClose(MPI_Comm, FILE *);
1866: PETSC_EXTERN PetscErrorCode PetscFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
1867: PETSC_EXTERN PetscErrorCode PetscFFlush(FILE *);
1868: PETSC_EXTERN PetscErrorCode PetscPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1869: PETSC_EXTERN PetscErrorCode PetscSNPrintf(char *, size_t, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
1870: PETSC_EXTERN PetscErrorCode PetscSNPrintfCount(char *, size_t, const char[], size_t *, ...) PETSC_ATTRIBUTE_FORMAT(3, 5);
1871: PETSC_EXTERN PetscErrorCode PetscFormatRealArray(char[], size_t, const char *, PetscInt, const PetscReal[]);

1873: PETSC_EXTERN PetscErrorCode PetscErrorPrintfDefault(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
1874: PETSC_EXTERN PetscErrorCode PetscErrorPrintfNone(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
1875: PETSC_EXTERN PetscErrorCode PetscHelpPrintfDefault(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);

1877: PETSC_EXTERN PetscErrorCode PetscFormatConvertGetSize(const char *, size_t *);
1878: PETSC_EXTERN PetscErrorCode PetscFormatConvert(const char *, char *);

1880: PETSC_EXTERN PetscErrorCode PetscPOpen(MPI_Comm, const char[], const char[], const char[], FILE **);
1881: PETSC_EXTERN PetscErrorCode PetscPClose(MPI_Comm, FILE *);
1882: PETSC_EXTERN PetscErrorCode PetscPOpenSetMachine(const char[]);

1884: PETSC_EXTERN PetscErrorCode PetscSynchronizedPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1885: PETSC_EXTERN PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
1886: PETSC_EXTERN PetscErrorCode PetscSynchronizedFlush(MPI_Comm, FILE *);
1887: PETSC_EXTERN PetscErrorCode PetscSynchronizedFGets(MPI_Comm, FILE *, size_t, char[]);
1888: PETSC_EXTERN PetscErrorCode PetscStartMatlab(MPI_Comm, const char[], const char[], FILE **);
1889: PETSC_EXTERN PetscErrorCode PetscGetPetscDir(const char *[]);

1891: PETSC_EXTERN PetscClassId   PETSC_CONTAINER_CLASSID;
1892: PETSC_EXTERN PetscErrorCode PetscContainerGetPointer(PetscContainer, void *);
1893: PETSC_EXTERN PetscErrorCode PetscContainerSetPointer(PetscContainer, void *);
1894: PETSC_EXTERN PetscErrorCode PetscContainerDestroy(PetscContainer *);
1895: PETSC_EXTERN PetscErrorCode PetscContainerCreate(MPI_Comm, PetscContainer *);
1896: PETSC_EXTERN PetscErrorCode PetscContainerSetCtxDestroy(PetscContainer, PetscCtxDestroyFn *);
1897: PETSC_EXTERN PETSC_DEPRECATED_FUNCTION(3, 23, 0, "PetscContainerSetCtxDestroy()", ) PetscErrorCode PetscContainerSetUserDestroy(PetscContainer, PetscErrorCode (*)(void *));
1898: PETSC_EXTERN PetscErrorCode PetscObjectContainerCompose(PetscObject, const char *name, void *, PetscCtxDestroyFn *);
1899: PETSC_EXTERN PetscErrorCode PetscObjectContainerQuery(PetscObject, const char *, PetscCtxRt);

1901: /*
1902:    For use in debuggers
1903: */
1904: PETSC_EXTERN PetscMPIInt    PetscGlobalRank;
1905: PETSC_EXTERN PetscMPIInt    PetscGlobalSize;
1906: PETSC_EXTERN PetscErrorCode PetscIntViewNumColumns(PetscInt, PetscInt, const PetscInt[], PetscViewer);
1907: PETSC_EXTERN PetscErrorCode PetscRealViewNumColumns(PetscInt, PetscInt, const PetscReal[], PetscViewer);
1908: PETSC_EXTERN PetscErrorCode PetscScalarViewNumColumns(PetscInt, PetscInt, const PetscScalar[], PetscViewer);
1909: PETSC_EXTERN PetscErrorCode PetscIntView(PetscInt, const PetscInt[], PetscViewer);
1910: PETSC_EXTERN PetscErrorCode PetscIntCSRView(PetscInt, const PetscInt[], const PetscInt[], PetscViewer);
1911: PETSC_EXTERN PetscErrorCode PetscRealView(PetscInt, const PetscReal[], PetscViewer);
1912: PETSC_EXTERN PetscErrorCode PetscScalarView(PetscInt, const PetscScalar[], PetscViewer);

1914: /*
1915:     Basic memory and string operations. These are usually simple wrappers
1916:    around the basic Unix system calls, but a few of them have additional
1917:    functionality and/or error checking.
1918: */
1919: #include <petscstring.h>

1921: #include <stddef.h>
1922: #include <stdlib.h>

1924: #if PetscDefined(CLANG_STATIC_ANALYZER)
1925:   #define PetscPrefetchBlock(a, b, c, d)
1926: #else
1927: /*MC
1928:    PetscPrefetchBlock - Prefetches a block of memory

1930:    Synopsis:
1931: #include <petscsys.h>
1932:     void PetscPrefetchBlock(const anytype *a, size_t n, int rw, int t)

1934:    Not Collective

1936:    Input Parameters:
1937: +  a  - pointer to first element to fetch (any type but usually `PetscInt` or `PetscScalar`)
1938: .  n  - number of elements to fetch
1939: .  rw - 1 if the memory will be written to, otherwise 0 (ignored by many processors)
1940: -  t  - temporal locality (PETSC_PREFETCH_HINT_{NTA,T0,T1,T2}), see note

1942:    Level: developer

1944:    Notes:
1945:    The last two arguments (`rw` and `t`) must be compile-time constants.

1947:    Adopting Intel's x86/x86-64 conventions, there are four levels of temporal locality.  Not all architectures offer
1948:    equivalent locality hints, but the following macros are always defined to their closest analogue.
1949: +  `PETSC_PREFETCH_HINT_NTA` - Non-temporal.  Prefetches directly to L1, evicts to memory (skips higher level cache unless it was already there when prefetched).
1950: .  `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.
1951: .  `PETSC_PREFETCH_HINT_T1`  - Fetch to level 2 and higher (not L1). On many systems, `PETSC_PREFETCH_HINT_T0` and `PETSC_PREFETCH_HINT_T1` are equivalent.
1952: -  `PETSC_PREFETCH_HINT_T2`  - Fetch to high-level cache only.

1954:    This function does nothing on architectures that do not support prefetch and never errors (even if passed an invalid
1955:    address).

1957: .seealso: `PETSC_PREFETCH_HINT_NTA`, `PETSC_PREFETCH_HINT_T0`, `PETSC_PREFETCH_HINT_T1`, `PETSC_PREFETCH_HINT_T2`
1958: M*/
1959:   #define PetscPrefetchBlock(a, n, rw, t) \
1960:     do { \
1961:       const char *_p = (const char *)(a), *_end = (const char *)((a) + (n)); \
1962:       for (; _p < _end; _p += PETSC_LEVEL1_DCACHE_LINESIZE) PETSC_Prefetch(_p, (rw), (t)); \
1963:     } while (0)
1964: #endif
1965: /*
1966:       Determine if some of the kernel computation routines use
1967:    Fortran (rather than C) for the numerical calculations. On some machines
1968:    and compilers (like complex numbers) the Fortran version of the routines
1969:    is faster than the C/C++ versions. The flag --with-fortran-kernels
1970:    should be used with ./configure to turn these on.
1971: */
1972: #if PetscDefined(USE_FORTRAN_KERNELS)

1974:   #if !PetscDefined(USE_FORTRAN_KERNEL_MULTCRL)
1975:     #define PETSC_USE_FORTRAN_KERNEL_MULTCRL
1976:   #endif

1978:   #if !PetscDefined(USE_FORTRAN_KERNEL_MULTAIJ)
1979:     #define PETSC_USE_FORTRAN_KERNEL_MULTAIJ
1980:   #endif

1982:   #if !PetscDefined(USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1983:     #define PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ
1984:   #endif

1986:   #if !PetscDefined(USE_FORTRAN_KERNEL_MAXPY)
1987:     #define PETSC_USE_FORTRAN_KERNEL_MAXPY
1988:   #endif

1990:   #if !PetscDefined(USE_FORTRAN_KERNEL_SOLVEAIJ)
1991:     #define PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ
1992:   #endif

1994:   #if !PetscDefined(USE_FORTRAN_KERNEL_SOLVEBAIJ)
1995:     #define PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ
1996:   #endif

1998:   #if !PetscDefined(USE_FORTRAN_KERNEL_MULTADDAIJ)
1999:     #define PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ
2000:   #endif

2002:   #if !PetscDefined(USE_FORTRAN_KERNEL_MDOT)
2003:     #define PETSC_USE_FORTRAN_KERNEL_MDOT
2004:   #endif

2006:   #if !PetscDefined(USE_FORTRAN_KERNEL_XTIMESY)
2007:     #define PETSC_USE_FORTRAN_KERNEL_XTIMESY
2008:   #endif

2010:   #if !PetscDefined(USE_FORTRAN_KERNEL_AYPX)
2011:     #define PETSC_USE_FORTRAN_KERNEL_AYPX
2012:   #endif

2014:   #if !PetscDefined(USE_FORTRAN_KERNEL_WAXPY)
2015:     #define PETSC_USE_FORTRAN_KERNEL_WAXPY
2016:   #endif

2018: #endif

2020: /*
2021:     Macros for indicating code that should be compiled with a C interface,
2022:    rather than a C++ interface. Any routines that are dynamically loaded
2023:    (such as the PCCreate_XXX() routines) must be wrapped so that the name
2024:    mangler does not change the functions symbol name. This just hides the
2025:    ugly extern "C" {} wrappers.
2026: */
2027: #if defined(__cplusplus)
2028:   #define EXTERN_C_BEGIN extern "C" {
2029:   #define EXTERN_C_END   }
2030: #else
2031:   #define EXTERN_C_BEGIN
2032:   #define EXTERN_C_END
2033: #endif

2035: /*MC
2036:    MPI_Comm - the basic object used by MPI to determine which processes are involved in a
2037:    communication

2039:    Level: beginner

2041:    Note:
2042:    This manual page is a place-holder because MPICH does not have a manual page for `MPI_Comm`

2044: .seealso: `PETSC_COMM_WORLD`, `PETSC_COMM_SELF`
2045: M*/

2047: #if PetscDefined(HAVE_MPIIO)
2048: PETSC_EXTERN PetscErrorCode MPIU_File_write_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(2, 4);
2049: PETSC_EXTERN PetscErrorCode MPIU_File_read_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(2, 4);
2050: 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);
2051: 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);
2052: 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);
2053: 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);
2054: #endif

2056: /*MC
2057:    MPIU_Count - PETSc datatype used to hold message-size counts for large MPI messages

2059:    Level: intermediate

2061:    Note:
2062:    When the underlying MPI implementation supports `MPI_Count` (which is wide enough to express counts that do not fit in `int`), `MPIU_Count` is a typedef for `MPI_Count`; otherwise it is defined as `PetscInt64`. Use `MPIU_Count` together with the PETSc MPI wrappers such as `MPIU_Send()`, `MPIU_Recv()`, and `MPIU_Allreduce()` so that codes can be written portably regardless of the available MPI version.

2064: .seealso: `MPI_Count`, `MPIU_COUNT`, `MPIU_Send()`, `MPIU_Recv()`, `MPIU_Reduce()`, `PetscInt64`
2065: M*/
2066: #if PetscDefined(HAVE_MPI_COUNT)
2067: typedef MPI_Count MPIU_Count;
2068: #else
2069: typedef PetscInt64 MPIU_Count;
2070: #endif

2072: /*@
2073:   PetscIntCast - casts a `MPI_Count`, `PetscInt64`, `PetscCount`, or `size_t` to a `PetscInt` (which may be 32-bits in size), generates an
2074:   error if the `PetscInt` is not large enough to hold the number.

2076:   Not Collective; No Fortran Support

2078:   Input Parameter:
2079: . a - the `PetscInt64` value

2081:   Output Parameter:
2082: . b - the resulting `PetscInt` value, or `NULL` if the result is not needed

2084:   Level: advanced

2086:   Note:
2087:   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

2089: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscCIntCast()`, `PetscIntMultError()`, `PetscIntSumError()`
2090: @*/
2091: static inline PetscErrorCode PetscIntCast(MPIU_Count a, PetscInt *b)
2092: {
2093:   PetscFunctionBegin;
2094:   if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2095:   PetscCheck(sizeof(MPIU_Count) <= sizeof(PetscInt) || (a <= (MPIU_Count)PETSC_INT_MAX && a >= (MPIU_Count)PETSC_INT_MIN), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt64_FMT " is too big for PetscInt, you may need to ./configure using --with-64-bit-indices", (PetscInt64)a);
2096:   if (b) *b = (PetscInt)a;
2097:   PetscFunctionReturn(PETSC_SUCCESS);
2098: }

2100: /*@
2101:   PetscBLASIntCast - casts a `MPI_Count`, `PetscInt`, `PetscCount` or `PetscInt64` to a `PetscBLASInt` (which may be 32-bits in size), generates an
2102:   error if the `PetscBLASInt` is not large enough to hold the number.

2104:   Not Collective; No Fortran Support

2106:   Input Parameter:
2107: . a - the `PetscInt` value

2109:   Output Parameter:
2110: . b - the resulting `PetscBLASInt` value, or `NULL` if the result is not needed

2112:   Level: advanced

2114:   Note:
2115:   Errors if the integer is negative since PETSc calls to BLAS/LAPACK never need to cast negative integer inputs

2117: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscCIntCast()`, `PetscIntCast()`
2118: @*/
2119: static inline PetscErrorCode PetscBLASIntCast(MPIU_Count a, PetscBLASInt *b)
2120: {
2121:   PetscFunctionBegin;
2122:   if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2123:   PetscCheck(sizeof(MPIU_Count) <= sizeof(PetscBLASInt) || a <= (MPIU_Count)PETSC_BLAS_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt64_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", (PetscInt64)a);
2124:   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer to BLAS/LAPACK routine");
2125:   if (b) *b = (PetscBLASInt)a;
2126:   PetscFunctionReturn(PETSC_SUCCESS);
2127: }

2129: /*@
2130:   PetscCuBLASIntCast - like `PetscBLASIntCast()`, but for `PetscCuBLASInt`.

2132:   Not Collective; No Fortran Support

2134:   Input Parameter:
2135: . a - the `PetscInt` value

2137:   Output Parameter:
2138: . b - the resulting `PetscCuBLASInt` value, or `NULL` if the result is not needed

2140:   Level: advanced

2142:   Note:
2143:   Errors if the integer is negative since PETSc calls to cuBLAS and friends never need to cast negative integer inputs

2145: .seealso: `PetscCuBLASInt`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscMPIIntCast()`, `PetscCIntCast()`, `PetscIntCast()`
2146: @*/
2147: static inline PetscErrorCode PetscCuBLASIntCast(MPIU_Count a, PetscCuBLASInt *b)
2148: {
2149:   PetscFunctionBegin;
2150:   if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2151:   PetscCheck(sizeof(MPIU_Count) <= sizeof(PetscCuBLASInt) || a <= (MPIU_Count)PETSC_CUBLAS_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt64_FMT " is too big for cuBLAS, which is restricted to 32-bit integers.", (PetscInt64)a);
2152:   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer %" PetscInt64_FMT "to cuBLAS routine", (PetscInt64)a);
2153:   if (b) *b = (PetscCuBLASInt)a;
2154:   PetscFunctionReturn(PETSC_SUCCESS);
2155: }

2157: /*@
2158:   PetscHipBLASIntCast - like `PetscBLASIntCast()`, but for `PetscHipBLASInt`.

2160:   Not Collective; No Fortran Support

2162:   Input Parameter:
2163: . a - the `PetscInt` value

2165:   Output Parameter:
2166: . b - the resulting `PetscHipBLASInt` value, or `NULL` if the result is not needed

2168:   Level: advanced

2170:   Note:
2171:   Errors if the integer is negative since PETSc calls to hipBLAS and friends never need to cast negative integer inputs

2173: .seealso: `PetscHipBLASInt`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscMPIIntCast()`, `PetscCIntCast()`, `PetscIntCast()`
2174: @*/
2175: static inline PetscErrorCode PetscHipBLASIntCast(MPIU_Count a, PetscHipBLASInt *b)
2176: {
2177:   PetscFunctionBegin;
2178:   if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2179:   PetscCheck(sizeof(MPIU_Count) <= sizeof(PetscHipBLASInt) || a <= (MPIU_Count)PETSC_HIPBLAS_INT_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt64_FMT " is too big for hipBLAS, which is restricted to 32-bit integers.", (PetscInt64)a);
2180:   PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer %" PetscInt64_FMT "to hipBLAS routine", (PetscInt64)a);
2181:   if (b) *b = (PetscHipBLASInt)a;
2182:   PetscFunctionReturn(PETSC_SUCCESS);
2183: }

2185: /*@
2186:   PetscMPIIntCast - casts a `MPI_Count`, `PetscInt`, `PetscCount`, or `PetscInt64` to a `PetscMPIInt` (which is always 32-bits in size), generates an
2187:   error if the `PetscMPIInt` is not large enough to hold the number.

2189:   Not Collective; No Fortran Support

2191:   Input Parameter:
2192: . a - the `PetscInt` value

2194:   Output Parameter:
2195: . b - the resulting `PetscMPIInt` value, or `NULL` if the result is not needed

2197:   Level: advanced

2199: .seealso: [](stylePetscCount), `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscIntCast()`
2200: @*/
2201: static inline PetscErrorCode PetscMPIIntCast(MPIU_Count a, PetscMPIInt *b)
2202: {
2203:   PetscFunctionBegin;
2204:   if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2205:   PetscCheck(a <= (MPIU_Count)PETSC_MPI_INT_MAX && a >= (MPIU_Count)PETSC_MPI_INT_MIN, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt64_FMT " is too big for MPI buffer length. Maximum supported value is %d", (PetscInt64)a, PETSC_MPI_INT_MAX);
2206:   if (b) *b = (PetscMPIInt)a;
2207:   PetscFunctionReturn(PETSC_SUCCESS);
2208: }

2210: /*@
2211:   PetscCIntCast - casts a `MPI_Count`, `PetscInt`, `PetscCount`, or `PetscInt64` to a `int`, generates an error if the `int` is not large enough to hold the number.

2213:   Not Collective; No Fortran Support

2215:   Input Parameter:
2216: . a - the `PetscInt` value

2218:   Output Parameter:
2219: . b - the resulting `int` value, or `NULL` if the result is not needed

2221:   Level: advanced

2223: .seealso: [](stylePetscCount), `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscIntCast()`
2224: @*/
2225: static inline PetscErrorCode PetscCIntCast(MPIU_Count a, int *b)
2226: {
2227:   PetscFunctionBegin;
2228:   if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2229:   PetscCheck(a <= INT_MAX && a >= INT_MIN, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt64_FMT " is too big to be casted to an int. Maximum supported value is %d", (PetscInt64)a, INT_MAX);
2230:   if (b) *b = (int)a;
2231:   PetscFunctionReturn(PETSC_SUCCESS);
2232: }

2234: /*MC
2235:    PetscInt64Mult - Computes the product of two variables after casting them to `PetscInt64`.

2237:    Not Collective; No Fortran Support

2239:    Input Parameters:
2240: +  a - the first variable
2241: -  b - the second variable

2243:    Level: advanced

2245: .seealso: [](stylePetscCount), `PetscIntMultError()`, `PetscIntMultTruncate()`
2246: M*/
2247: #if PetscDefined(USE_64BIT_INDICES)
2248:   #define PetscInt64Mult(a, b) ((a) * (b))
2249: #else
2250:   #define PetscInt64Mult(a, b) (((PetscInt64)(a)) * ((PetscInt64)(b)))
2251: #endif

2253: /*@
2254:   PetscRealIntMultTruncate - Computes the product of a positive `PetscReal` and a positive
2255:   `PetscInt` and truncates the value to slightly less than the maximal possible value.

2257:   Not Collective; No Fortran Support

2259:   Input Parameters:
2260: + a - The `PetscReal` value
2261: - b - The `PetscInt` value

2263:   Level: advanced

2265:   Notes:
2266:   Returns the result as a `PetscInt` value.

2268:   Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`.

2270:   Use `PetscIntMultTruncate()` to compute the product of two positive `PetscInt` and truncate
2271:   to fit a `PetscInt`.

2273:   Use `PetscIntMultError()` to compute the product of two `PetscInt` if you wish to generate an
2274:   error if the result will not fit in a `PetscInt`.

2276:   Developer Notes:
2277:   We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but
2278:   requires many more checks.

2280:   This is used where we compute approximate sizes for workspace and need to insure the
2281:   workspace is index-able.

2283: .seealso: `PetscReal`, `PetscInt`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`
2284: @*/
2285: static inline PetscInt PetscRealIntMultTruncate(PetscReal a, PetscInt b)
2286: {
2287:   PetscInt64 r = (PetscInt64)(a * (PetscReal)b);
2288:   if (r > PETSC_INT_MAX - 100) r = PETSC_INT_MAX - 100;
2289: #if PetscDefined(USE_64BIT_INDICES)
2290:   return r;
2291: #else
2292:   return (PetscInt)r;
2293: #endif
2294: }

2296: /*@
2297:   PetscIntMultTruncate - Computes the product of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value

2299:   Not Collective; No Fortran Support

2301:   Input Parameters:
2302: + a - the `PetscInt` value
2303: - b - the second value

2305:   Returns:
2306:   The result as a `PetscInt` value

2308:   Level: advanced

2310:   Notes:
2311:   Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`

2313:   Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`

2315:   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`

2317:   Developer Notes:
2318:   We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but requires many more checks.

2320:   This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.

2322: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`,
2323:   `PetscIntSumTruncate()`
2324: @*/
2325: static inline PetscInt PetscIntMultTruncate(PetscInt a, PetscInt b)
2326: {
2327:   PetscInt64 r = PetscInt64Mult(a, b);
2328:   if (r > PETSC_INT_MAX - 100) r = PETSC_INT_MAX - 100;
2329: #if PetscDefined(USE_64BIT_INDICES)
2330:   return r;
2331: #else
2332:   return (PetscInt)r;
2333: #endif
2334: }

2336: /*@
2337:   PetscIntSumTruncate - Computes the sum of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value

2339:   Not Collective; No Fortran Support

2341:   Input Parameters:
2342: + a - the `PetscInt` value
2343: - b - the second value

2345:   Returns:
2346:   The result as a `PetscInt` value

2348:   Level: advanced

2350:   Notes:
2351:   Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`

2353:   Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`

2355:   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`

2357:   Developer Note:
2358:   This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.

2360: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
2361: @*/
2362: static inline PetscInt PetscIntSumTruncate(PetscInt a, PetscInt b)
2363: {
2364:   PetscInt64 r = a;

2366:   r += b;
2367:   if (r > PETSC_INT_MAX - 100) r = PETSC_INT_MAX - 100;
2368: #if PetscDefined(USE_64BIT_INDICES)
2369:   return r;
2370: #else
2371:   return (PetscInt)r;
2372: #endif
2373: }

2375: /*@
2376:   PetscIntMultError - Computes the product of two positive `PetscInt` and generates an error with overflow.

2378:   Not Collective; No Fortran Support

2380:   Input Parameters:
2381: + a - the `PetscInt` value
2382: - b - the second value

2384:   Output Parameter:
2385: . result - the result as a `PetscInt` value, or `NULL` if you do not want the result, you just want to check if it overflows

2387:   Level: advanced

2389:   Notes:
2390:   Use `PetscInt64Mult()` to compute the product of two `PetscInt` and store in a `PetscInt64`

2392:   Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`

2394:   Developer Note:
2395:   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.
2396:   `PetscIntSumError()` can be used to check for this situation.

2398: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntSumError()`
2399: @*/
2400: static inline PetscErrorCode PetscIntMultError(PetscInt a, PetscInt b, PetscInt *result)
2401: {
2402:   PetscInt64 r = PetscInt64Mult(a, b);

2404:   PetscFunctionBegin;
2405: #if PetscDefined(USE_64BIT_INDICES)
2406:   if (result) *result = r;
2407: #else
2408:   if (result) *result = (PetscInt)r;
2409: #endif
2410:   if (!PetscDefined(USE_64BIT_INDICES)) {
2411:     PetscCheck(r <= PETSC_INT_MAX, 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);
2412:   }
2413:   PetscFunctionReturn(PETSC_SUCCESS);
2414: }

2416: /*@
2417:   PetscIntSumError - Computes the sum of two positive `PetscInt` and generates an error with overflow.

2419:   Not Collective; No Fortran Support

2421:   Input Parameters:
2422: + a - the `PetscInt` value
2423: - b - the second value

2425:   Output Parameter:
2426: . c - the result as a `PetscInt` value,  or `NULL` if you do not want the result, you just want to check if it overflows

2428:   Level: advanced

2430:   Notes:
2431:   Use `PetscInt64Mult()` to compute the product of two 32-bit `PetscInt` and store in a `PetscInt64`

2433:   Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`

2435: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
2436: @*/
2437: static inline PetscErrorCode PetscIntSumError(PetscInt a, PetscInt b, PetscInt *result)
2438: {
2439:   PetscInt64 r = a;

2441:   PetscFunctionBegin;
2442:   r += b;
2443: #if PetscDefined(USE_64BIT_INDICES)
2444:   if (result) *result = r;
2445: #else
2446:   if (result) *result = (PetscInt)r;
2447: #endif
2448:   if (!PetscDefined(USE_64BIT_INDICES)) {
2449:     PetscCheck(r <= PETSC_INT_MAX, 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);
2450:   }
2451:   PetscFunctionReturn(PETSC_SUCCESS);
2452: }

2454: /*
2455:      The IBM include files define hz, here we hide it so that it may be used as a regular user variable.
2456: */
2457: #if defined(hz)
2458:   #undef hz
2459: #endif

2461: #if PetscDefined(HAVE_SYS_TYPES_H)
2462:   #include <sys/types.h>
2463: #endif

2465: /*MC

2467:     PETSC_VERSION - This manual page provides information about how PETSc documents and uses its version information. This information is available to both C/C++
2468:                     and Fortran compilers when `petscsys.h` is included.

2470:     The current PETSc version and the API for accessing it are defined in <A HREF="../include/petscversion.h.html">include/petscversion.html</A>

2472:     The complete version number is given as the triple  PETSC_VERSION_MAJOR.PETSC_VERSION_MINOR.PETSC_VERSION_SUBMINOR (in short hand x.y.z)

2474:     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
2475:     where only a change in the major version number (x) indicates a change in the API.

2477:     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

2479:     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),
2480:     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
2481:     version number (x.y.z).

2483:     `PETSC_RELEASE_DATE` is the date the x.y version was released (i.e. the version before any patch releases)

2485:     `PETSC_VERSION_DATE` is the date the x.y.z version was released

2487:     `PETSC_VERSION_GIT` is the last git commit to the repository given in the form vx.y.z-wwwww

2489:     `PETSC_VERSION_DATE_GIT` is the date of the last git commit to the repository

2491:     `PETSC_VERSION_()` is deprecated and will eventually be removed.

2493:     Level: intermediate
2494: M*/

2496: PETSC_EXTERN PetscErrorCode PetscGetArchType(char[], size_t);
2497: PETSC_EXTERN PetscErrorCode PetscGetHostName(char[], size_t);
2498: PETSC_EXTERN PetscErrorCode PetscGetUserName(char[], size_t);
2499: PETSC_EXTERN PetscErrorCode PetscGetProgramName(char[], size_t);
2500: PETSC_EXTERN PetscErrorCode PetscSetProgramName(const char[]);
2501: PETSC_EXTERN PetscErrorCode PetscGetDate(char[], size_t);
2502: PETSC_EXTERN PetscErrorCode PetscGetVersion(char[], size_t);
2503: PETSC_EXTERN PetscErrorCode PetscGetVersionNumber(PetscInt *, PetscInt *, PetscInt *, PetscInt *);
2504: PETSC_EXTERN PetscErrorCode PetscGetConfiguration(const char *[]);

2506: PETSC_EXTERN PetscErrorCode PetscSortedInt(PetscCount, const PetscInt[], PetscBool *);
2507: PETSC_EXTERN PetscErrorCode PetscSortedInt64(PetscCount, const PetscInt64[], PetscBool *);
2508: PETSC_EXTERN PetscErrorCode PetscSortedMPIInt(PetscCount, const PetscMPIInt[], PetscBool *);
2509: PETSC_EXTERN PetscErrorCode PetscSortedReal(PetscCount, const PetscReal[], PetscBool *);
2510: PETSC_EXTERN PetscErrorCode PetscSortInt(PetscCount, PetscInt[]);
2511: PETSC_EXTERN PetscErrorCode PetscSortInt64(PetscCount, PetscInt64[]);
2512: PETSC_EXTERN PetscErrorCode PetscSortCount(PetscCount, PetscCount[]);
2513: PETSC_EXTERN PetscErrorCode PetscSortReverseInt(PetscCount, PetscInt[]);
2514: PETSC_EXTERN PetscErrorCode PetscSortedRemoveDupsInt(PetscInt *, PetscInt[]);
2515: PETSC_EXTERN PetscErrorCode PetscSortedCheckDupsInt(PetscCount, const PetscInt[], PetscBool *);
2516: PETSC_EXTERN PetscErrorCode PetscSortedCheckDupsCount(PetscCount, const PetscCount[], PetscBool *);
2517: PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsInt(PetscInt *, PetscInt[]);
2518: PETSC_EXTERN PetscErrorCode PetscCheckDupsInt(PetscInt, const PetscInt[], PetscBool *);
2519: PETSC_EXTERN PetscErrorCode PetscFindInt(PetscInt, PetscCount, const PetscInt[], PetscInt *);
2520: PETSC_EXTERN PetscErrorCode PetscFindMPIInt(PetscMPIInt, PetscCount, const PetscMPIInt[], PetscInt *);
2521: PETSC_EXTERN PetscErrorCode PetscFindCount(PetscCount, PetscCount, const PetscCount[], PetscCount *);
2522: PETSC_EXTERN PetscErrorCode PetscSortIntWithPermutation(PetscInt, const PetscInt[], PetscInt[]);
2523: PETSC_EXTERN PetscErrorCode PetscSortStrWithPermutation(PetscInt, const char *[], PetscInt[]);
2524: PETSC_EXTERN PetscErrorCode PetscSortIntWithArray(PetscCount, PetscInt[], PetscInt[]);
2525: PETSC_EXTERN PetscErrorCode PetscSortIntWithCountArray(PetscCount, PetscInt[], PetscCount[]);
2526: PETSC_EXTERN PetscErrorCode PetscSortIntWithMPIIntArray(PetscCount, PetscInt[], PetscMPIInt[]);
2527: PETSC_EXTERN PetscErrorCode PetscSortIntWithArrayPair(PetscCount, PetscInt[], PetscInt[], PetscInt[]);
2528: PETSC_EXTERN PetscErrorCode PetscSortIntWithIntCountArrayPair(PetscCount, PetscInt[], PetscInt[], PetscCount[]);
2529: PETSC_EXTERN PetscErrorCode PetscSortMPIInt(PetscCount, PetscMPIInt[]);
2530: PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsMPIInt(PetscInt *, PetscMPIInt[]);
2531: PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithArray(PetscCount, PetscMPIInt[], PetscMPIInt[]);
2532: PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithIntArray(PetscCount, PetscMPIInt[], PetscInt[]);
2533: PETSC_EXTERN PetscErrorCode PetscSortIntWithScalarArray(PetscCount, PetscInt[], PetscScalar[]);
2534: PETSC_EXTERN PetscErrorCode PetscSortIntWithDataArray(PetscCount, PetscInt[], void *, size_t, void *);
2535: PETSC_EXTERN PetscErrorCode PetscSortReal(PetscCount, PetscReal[]);
2536: PETSC_EXTERN PetscErrorCode PetscSortRealWithArrayInt(PetscCount, PetscReal[], PetscInt[]);
2537: PETSC_EXTERN PetscErrorCode PetscSortRealWithPermutation(PetscInt, const PetscReal[], PetscInt[]);
2538: PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsReal(PetscInt *, PetscReal[]);
2539: PETSC_EXTERN PetscErrorCode PetscFindReal(PetscReal, PetscCount, const PetscReal[], PetscReal, PetscInt *);
2540: PETSC_EXTERN PetscErrorCode PetscSortSplit(PetscInt, PetscInt, PetscScalar[], PetscInt[]);
2541: PETSC_EXTERN PetscErrorCode PetscSortSplitReal(PetscInt, PetscInt, PetscReal[], PetscInt[]);
2542: PETSC_EXTERN PetscErrorCode PetscProcessTree(PetscInt, const PetscBool[], const PetscInt[], PetscInt *, PetscInt *[], PetscInt *[], PetscInt *[], PetscInt *[]);
2543: PETSC_EXTERN PetscErrorCode PetscMergeIntArrayPair(PetscInt, const PetscInt[], const PetscInt[], PetscInt, const PetscInt[], const PetscInt[], PetscInt *, PetscInt *[], PetscInt *[]);
2544: PETSC_EXTERN PetscErrorCode PetscMergeIntArray(PetscInt, const PetscInt[], PetscInt, const PetscInt[], PetscInt *, PetscInt *[]);
2545: PETSC_EXTERN PetscErrorCode PetscMergeMPIIntArray(PetscInt, const PetscMPIInt[], PetscInt, const PetscMPIInt[], PetscInt *, PetscMPIInt *[]);
2546: PETSC_EXTERN PetscErrorCode PetscParallelSortedInt(MPI_Comm, PetscInt, const PetscInt[], PetscBool *);

2548: PETSC_EXTERN PetscErrorCode PetscTimSort(PetscInt, void *, size_t, int (*)(const void *, const void *, void *), void *);
2549: PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrdered(PetscInt, PetscInt[]);
2550: PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrdered(PetscInt, PetscMPIInt[]);
2551: PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrdered(PetscInt, PetscReal[]);
2552: PETSC_EXTERN PetscErrorCode PetscTimSortWithArray(PetscInt, void *, size_t, void *, size_t, int (*)(const void *, const void *, void *), void *);
2553: PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrderedWithArray(PetscInt, PetscInt[], PetscInt[]);
2554: PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrderedWithArray(PetscInt, PetscMPIInt[], PetscMPIInt[]);
2555: PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrderedWithArrayInt(PetscInt, PetscReal[], PetscInt[]);

2557: PETSC_EXTERN PetscErrorCode PetscSetDisplay(void);
2558: PETSC_EXTERN PetscErrorCode PetscGetDisplay(char[], size_t);

2560: /*J
2561:     PetscRandomType - String with the name of a PETSc randomizer

2563:    Level: beginner

2565:    Note:
2566:    To use `PETSCSPRNG` or `PETSCRANDOM123` you must have ./configure PETSc
2567:    with the option `--download-sprng` or `--download-random123`. We recommend the default provided with PETSc.

2569: .seealso: `PetscRandomSetType()`, `PetscRandom`, `PetscRandomCreate()`
2570: J*/
2571: typedef const char *PetscRandomType;
2572: #define PETSCRAND      "rand"
2573: #define PETSCRAND48    "rand48"
2574: #define PETSCSPRNG     "sprng"
2575: #define PETSCRANDER48  "rander48"
2576: #define PETSCRANDOM123 "random123"
2577: #define PETSCCURAND    "curand"

2579: /* Logging support */
2580: PETSC_EXTERN PetscClassId PETSC_RANDOM_CLASSID;

2582: PETSC_EXTERN PetscErrorCode PetscRandomInitializePackage(void);
2583: PETSC_EXTERN PetscErrorCode PetscRandomFinalizePackage(void);

2585: /* Dynamic creation and loading functions */
2586: PETSC_EXTERN PetscFunctionList PetscRandomList;

2588: PETSC_EXTERN PetscErrorCode PetscRandomRegister(const char[], PetscErrorCode (*)(PetscRandom));
2589: PETSC_EXTERN PetscErrorCode PetscRandomSetType(PetscRandom, PetscRandomType);
2590: PETSC_EXTERN PetscErrorCode PetscRandomSetOptionsPrefix(PetscRandom, const char[]);
2591: PETSC_EXTERN PetscErrorCode PetscRandomAppendOptionsPrefix(PetscRandom, const char[]);
2592: PETSC_EXTERN PetscErrorCode PetscRandomGetOptionsPrefix(PetscRandom, const char *[]);
2593: PETSC_EXTERN PetscErrorCode PetscRandomSetFromOptions(PetscRandom);
2594: PETSC_EXTERN PetscErrorCode PetscRandomGetType(PetscRandom, PetscRandomType *);
2595: PETSC_EXTERN PetscErrorCode PetscRandomViewFromOptions(PetscRandom, PetscObject, const char[]);
2596: PETSC_EXTERN PetscErrorCode PetscRandomView(PetscRandom, PetscViewer);

2598: PETSC_EXTERN PetscErrorCode PetscRandomCreate(MPI_Comm, PetscRandom *);
2599: PETSC_EXTERN PetscErrorCode PetscRandomGetValue(PetscRandom, PetscScalar *);
2600: PETSC_EXTERN PetscErrorCode PetscRandomGetValueReal(PetscRandom, PetscReal *);
2601: PETSC_EXTERN PetscErrorCode PetscRandomGetValues(PetscRandom, PetscInt, PetscScalar *);
2602: PETSC_EXTERN PetscErrorCode PetscRandomGetValuesReal(PetscRandom, PetscInt, PetscReal *);
2603: PETSC_EXTERN PetscErrorCode PetscRandomGetInterval(PetscRandom, PetscScalar *, PetscScalar *);
2604: PETSC_EXTERN PetscErrorCode PetscRandomSetInterval(PetscRandom, PetscScalar, PetscScalar);
2605: PETSC_EXTERN PetscErrorCode PetscRandomSetSeed(PetscRandom, PetscInt64);
2606: PETSC_EXTERN PetscErrorCode PetscRandomGetSeed(PetscRandom, PetscInt64 *);
2607: PETSC_EXTERN PetscErrorCode PetscRandomSeed(PetscRandom);
2608: PETSC_EXTERN PetscErrorCode PetscRandomDestroy(PetscRandom *);

2610: PETSC_EXTERN PetscErrorCode PetscGetFullPath(const char[], char[], size_t);
2611: PETSC_EXTERN PetscErrorCode PetscGetRelativePath(const char[], char[], size_t);
2612: PETSC_EXTERN PetscErrorCode PetscGetWorkingDirectory(char[], size_t);
2613: PETSC_EXTERN PetscErrorCode PetscGetRealPath(const char[], char[]);
2614: PETSC_EXTERN PetscErrorCode PetscGetHomeDirectory(char[], size_t);
2615: PETSC_EXTERN PetscErrorCode PetscTestFile(const char[], char, PetscBool *);
2616: PETSC_EXTERN PetscErrorCode PetscTestDirectory(const char[], char, PetscBool *);
2617: PETSC_EXTERN PetscErrorCode PetscMkdir(const char[]);
2618: PETSC_EXTERN PetscErrorCode PetscMkdtemp(char[]);
2619: PETSC_EXTERN PetscErrorCode PetscRMTree(const char[]);

2621: /*MC
2622:   PetscBinaryBigEndian - indicates if values in memory are stored with big endian format

2624:   Synopsis:
2625: #include <petscsys.h>
2626:   PetscBool PetscBinaryBigEndian(void);

2628:   Not Collective; No Fortran Support

2630:   Level: developer

2632: .seealso: `PetscInitialize()`, `PetscFinalize()`, `PetscInitializeCalled`
2633: M*/
2634: static inline PetscBool PetscBinaryBigEndian(void)
2635: {
2636:   long _petsc_v = 1;
2637:   return ((char *)&_petsc_v)[0] ? PETSC_FALSE : PETSC_TRUE;
2638: }

2640: PETSC_EXTERN PetscErrorCode PetscBinaryRead(int, void *, PetscCount, PetscInt *, PetscDataType);
2641: PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedRead(MPI_Comm, int, void *, PetscInt, PetscInt *, PetscDataType);
2642: PETSC_EXTERN PetscErrorCode PetscBinaryWrite(int, const void *, PetscCount, PetscDataType);
2643: PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedWrite(MPI_Comm, int, const void *, PetscInt, PetscDataType);
2644: PETSC_EXTERN PetscErrorCode PetscBinaryOpen(const char[], PetscFileMode, int *);
2645: PETSC_EXTERN PetscErrorCode PetscBinaryClose(int);
2646: PETSC_EXTERN PetscErrorCode PetscSharedTmp(MPI_Comm, PetscBool *);
2647: PETSC_EXTERN PetscErrorCode PetscSharedWorkingDirectory(MPI_Comm, PetscBool *);
2648: PETSC_EXTERN PetscErrorCode PetscGetTmp(MPI_Comm, char[], size_t);
2649: PETSC_EXTERN PetscErrorCode PetscFileRetrieve(MPI_Comm, const char[], char[], size_t, PetscBool *);
2650: PETSC_EXTERN PetscErrorCode PetscLs(MPI_Comm, const char[], char[], size_t, PetscBool *);
2651: #if PetscDefined(USE_SOCKET_VIEWER)
2652: PETSC_EXTERN PetscErrorCode PetscOpenSocket(const char[], int, int *);
2653: #endif

2655: PETSC_EXTERN PetscErrorCode PetscBinarySeek(int, off_t, PetscBinarySeekType, off_t *);
2656: PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedSeek(MPI_Comm, int, off_t, PetscBinarySeekType, off_t *);
2657: PETSC_EXTERN PetscErrorCode PetscByteSwap(void *, PetscDataType, PetscCount);

2659: PETSC_EXTERN PetscErrorCode PetscSetDebugTerminal(const char[]);
2660: PETSC_EXTERN PetscErrorCode PetscSetDebugger(const char[], PetscBool);
2661: PETSC_EXTERN PetscErrorCode PetscSetDefaultDebugger(void);
2662: PETSC_EXTERN PetscErrorCode PetscSetDebuggerFromString(const char *);
2663: PETSC_EXTERN PetscErrorCode PetscAttachDebugger(void);
2664: PETSC_EXTERN PetscErrorCode PetscStopForDebugger(void);
2665: PETSC_EXTERN PetscErrorCode PetscWaitOnError(void);

2667: PETSC_EXTERN PetscErrorCode PetscGatherNumberOfMessages(MPI_Comm, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt *);
2668: PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], PetscMPIInt *[], PetscMPIInt *[]);
2669: PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths2(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt *[], PetscMPIInt *[], PetscMPIInt *[]);
2670: PETSC_EXTERN PetscErrorCode PetscPostIrecvInt(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscInt ***, MPI_Request **);
2671: PETSC_EXTERN PetscErrorCode PetscPostIrecvScalar(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscScalar ***, MPI_Request **);
2672: 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);
2673: 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 *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(6, 3);
2674: 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 *), PetscCtx ctx) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(6, 3);

2676: PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedSetType(MPI_Comm, PetscBuildTwoSidedType);
2677: PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedGetType(MPI_Comm, PetscBuildTwoSidedType *);

2679: PETSC_DEPRECATED_FUNCTION(3, 24, 0, "PetscSSEIsEnabled()", ) static inline PetscErrorCode PetscSSEIsEnabled(PETSC_UNUSED MPI_Comm comm, PetscBool *lflag, PetscBool *gflag)
2680: {
2681:   if (lflag) *lflag = PETSC_FALSE;
2682:   if (gflag) *gflag = PETSC_FALSE;
2683:   return PETSC_SUCCESS;
2684: }

2686: PETSC_EXTERN MPI_Comm PetscObjectComm(PetscObject);

2688: struct _n_PetscSubcomm {
2689:   MPI_Comm         parent;    /* parent communicator */
2690:   MPI_Comm         dupparent; /* duplicate parent communicator, under which the processors of this subcomm have contiguous rank */
2691:   MPI_Comm         child;     /* the sub-communicator */
2692:   PetscMPIInt      n;         /* num of subcommunicators under the parent communicator */
2693:   PetscMPIInt      color;     /* color of processors belong to this communicator */
2694:   PetscMPIInt     *subsize;   /* size of subcommunicator[color] */
2695:   PetscSubcommType type;
2696:   char            *subcommprefix;
2697: };

2699: static inline MPI_Comm PetscSubcommParent(PetscSubcomm scomm)
2700: {
2701:   return scomm->parent;
2702: }
2703: static inline MPI_Comm PetscSubcommChild(PetscSubcomm scomm)
2704: {
2705:   return scomm->child;
2706: }
2707: static inline MPI_Comm PetscSubcommContiguousParent(PetscSubcomm scomm)
2708: {
2709:   return scomm->dupparent;
2710: }
2711: PETSC_EXTERN PetscErrorCode PetscSubcommCreate(MPI_Comm, PetscSubcomm *);
2712: PETSC_EXTERN PetscErrorCode PetscSubcommDestroy(PetscSubcomm *);
2713: PETSC_EXTERN PetscErrorCode PetscSubcommSetNumber(PetscSubcomm, PetscInt);
2714: PETSC_EXTERN PetscErrorCode PetscSubcommSetType(PetscSubcomm, PetscSubcommType);
2715: PETSC_EXTERN PetscErrorCode PetscSubcommSetTypeGeneral(PetscSubcomm, PetscMPIInt, PetscMPIInt);
2716: PETSC_EXTERN PetscErrorCode PetscSubcommView(PetscSubcomm, PetscViewer);
2717: PETSC_EXTERN PetscErrorCode PetscSubcommSetFromOptions(PetscSubcomm);
2718: PETSC_EXTERN PetscErrorCode PetscSubcommSetOptionsPrefix(PetscSubcomm, const char[]);
2719: PETSC_EXTERN PetscErrorCode PetscSubcommGetParent(PetscSubcomm, MPI_Comm *);
2720: PETSC_EXTERN PetscErrorCode PetscSubcommGetContiguousParent(PetscSubcomm, MPI_Comm *);
2721: PETSC_EXTERN PetscErrorCode PetscSubcommGetChild(PetscSubcomm, MPI_Comm *);

2723: PETSC_EXTERN PetscErrorCode PetscHeapCreate(PetscInt, PetscHeap *);
2724: PETSC_EXTERN PetscErrorCode PetscHeapAdd(PetscHeap, PetscInt, PetscInt);
2725: PETSC_EXTERN PetscErrorCode PetscHeapPop(PetscHeap, PetscInt *, PetscInt *);
2726: PETSC_EXTERN PetscErrorCode PetscHeapPeek(PetscHeap, PetscInt *, PetscInt *);
2727: PETSC_EXTERN PetscErrorCode PetscHeapStash(PetscHeap, PetscInt, PetscInt);
2728: PETSC_EXTERN PetscErrorCode PetscHeapUnstash(PetscHeap);
2729: PETSC_EXTERN PetscErrorCode PetscHeapDestroy(PetscHeap *);
2730: PETSC_EXTERN PetscErrorCode PetscHeapView(PetscHeap, PetscViewer);

2732: PETSC_EXTERN PetscErrorCode PetscProcessPlacementView(PetscViewer);
2733: PETSC_EXTERN PetscErrorCode PetscShmCommGet(MPI_Comm, PetscShmComm *);
2734: PETSC_EXTERN PetscErrorCode PetscShmCommGlobalToLocal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
2735: PETSC_EXTERN PetscErrorCode PetscShmCommLocalToGlobal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
2736: PETSC_EXTERN PetscErrorCode PetscShmCommGetMpiShmComm(PetscShmComm, MPI_Comm *);

2738: /* routines to better support OpenMP multithreading needs of some PETSc third party libraries */
2739: PETSC_EXTERN PetscErrorCode PetscOmpCtrlCreate(MPI_Comm, PetscInt, PetscOmpCtrl *);
2740: PETSC_EXTERN PetscErrorCode PetscOmpCtrlGetOmpComms(PetscOmpCtrl, MPI_Comm *, MPI_Comm *, PetscBool *);
2741: PETSC_EXTERN PetscErrorCode PetscOmpCtrlDestroy(PetscOmpCtrl *);
2742: PETSC_EXTERN PetscErrorCode PetscOmpCtrlBarrier(PetscOmpCtrl);
2743: PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterBegin(PetscOmpCtrl);
2744: PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterEnd(PetscOmpCtrl);

2746: PETSC_EXTERN PetscErrorCode PetscSegBufferCreate(size_t, PetscCount, PetscSegBuffer *);
2747: PETSC_EXTERN PetscErrorCode PetscSegBufferDestroy(PetscSegBuffer *);
2748: PETSC_EXTERN PetscErrorCode PetscSegBufferGet(PetscSegBuffer, PetscCount, void *);
2749: PETSC_EXTERN PetscErrorCode PetscSegBufferExtractAlloc(PetscSegBuffer, void *);
2750: PETSC_EXTERN PetscErrorCode PetscSegBufferExtractTo(PetscSegBuffer, void *);
2751: PETSC_EXTERN PetscErrorCode PetscSegBufferExtractInPlace(PetscSegBuffer, void *);
2752: PETSC_EXTERN PetscErrorCode PetscSegBufferGetSize(PetscSegBuffer, PetscCount *);
2753: PETSC_EXTERN PetscErrorCode PetscSegBufferUnuse(PetscSegBuffer, PetscCount);

2755: /*MC
2756:   PetscSegBufferGetInts - access an array of `PetscInt` from a `PetscSegBuffer`

2758:   Synopsis:
2759: #include <petscsys.h>
2760:   PetscErrorCode PetscSegBufferGetInts(PetscSegBuffer seg, size_t count, PetscInt *PETSC_RESTRICT *slot);

2762:   No Fortran Support

2764:   Input Parameters:
2765: + seg   - `PetscSegBuffer` buffer
2766: - count - number of entries needed

2768:   Output Parameter:
2769: . buf - address of new buffer for contiguous data

2771:   Level: intermediate

2773:   Developer Note:
2774:   Type-safe wrapper to encourage use of PETSC_RESTRICT. Does not use PetscFunctionBegin because the error handling
2775:   prevents the compiler from completely erasing the stub. This is called in inner loops so it has to be as fast as
2776:   possible.

2778: .seealso: `PetscSegBuffer`, `PetscSegBufferGet()`, `PetscInitialize()`, `PetscFinalize()`, `PetscInitializeCalled`
2779: M*/
2780: static inline PetscErrorCode PetscSegBufferGetInts(PetscSegBuffer seg, PetscCount count, PetscInt *PETSC_RESTRICT *slot)
2781: {
2782:   return PetscSegBufferGet(seg, count, (void **)slot);
2783: }

2785: extern PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton;
2786: PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *);
2787: PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *);
2788: PETSC_EXTERN PetscErrorCode    PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted, const char *, const char *, PetscBool *);

2790: #include <stdarg.h>
2791: PETSC_EXTERN PetscErrorCode PetscVSNPrintf(char *, size_t, const char[], size_t *, va_list);
2792: PETSC_EXTERN PetscErrorCode (*PetscVFPrintf)(FILE *, const char[], va_list);

2794: PETSC_EXTERN PetscSegBuffer PetscCitationsList;

2796: /*@
2797:   PetscCitationsRegister - Register a bibtex item to obtain credit for an implemented algorithm used in the code.

2799:   Not Collective; No Fortran Support

2801:   Input Parameters:
2802: + cite - the bibtex item, formatted to displayed on multiple lines nicely
2803: - set  - a boolean variable initially set to `PETSC_FALSE`; this is used to insure only a single registration of the citation

2805:   Options Database Key:
2806: . -citations [filename] - print out the bibtex entries for the given computation

2808:   Level: intermediate

2810: .seealso: `PetscFinalize()`
2811: @*/
2812: static inline PetscErrorCode PetscCitationsRegister(const char cit[], PetscBool *set)
2813: {
2814:   size_t len;
2815:   char  *vstring;

2817:   PetscFunctionBegin;
2818:   if (set && *set) PetscFunctionReturn(PETSC_SUCCESS);
2819:   PetscCall(PetscStrlen(cit, &len));
2820:   PetscCall(PetscSegBufferGet(PetscCitationsList, (PetscCount)len, &vstring));
2821:   PetscCall(PetscArraycpy(vstring, cit, len));
2822:   if (set) *set = PETSC_TRUE;
2823:   PetscFunctionReturn(PETSC_SUCCESS);
2824: }

2826: PETSC_EXTERN PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm, char[], char[], size_t);
2827: PETSC_EXTERN PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm, const char[], char[], size_t);
2828: PETSC_EXTERN PetscErrorCode PetscGoogleDriveUpload(MPI_Comm, const char[], const char[]);

2830: PETSC_EXTERN PetscErrorCode PetscBoxAuthorize(MPI_Comm, char[], char[], size_t);
2831: PETSC_EXTERN PetscErrorCode PetscBoxRefresh(MPI_Comm, const char[], char[], char[], size_t);
2832: PETSC_EXTERN PetscErrorCode PetscBoxUpload(MPI_Comm, const char[], const char[]);

2834: PETSC_EXTERN PetscErrorCode PetscGlobusGetTransfers(MPI_Comm, const char[], char[], size_t);
2835: PETSC_EXTERN PetscErrorCode PetscGlobusAuthorize(MPI_Comm, char[], size_t);
2836: PETSC_EXTERN PetscErrorCode PetscGlobusUpload(MPI_Comm, const char[], const char[]);

2838: PETSC_EXTERN PetscErrorCode PetscPullJSONValue(const char[], const char[], char[], size_t, PetscBool *);
2839: PETSC_EXTERN PetscErrorCode PetscPushJSONValue(char[], const char[], const char[], size_t);

2841: #if !PetscDefined(HAVE_MPI_LARGE_COUNT)
2842:   /*
2843:    Cast PetscCount <a> to PetscMPIInt <b>, where <a> is likely used for the 'count' argument in MPI routines.
2844:    It is similar to PetscMPIIntCast() except that here it returns an MPI error code.
2845: */
2846:   #define PetscMPIIntCast_Internal(a, b) \
2847:     do { \
2848:       *b = 0; \
2849:       if (PetscUnlikely(a > (MPIU_Count)PETSC_MPI_INT_MAX)) return MPI_ERR_COUNT; \
2850:       *b = (PetscMPIInt)a; \
2851:     } while (0)

2853: static inline PetscMPIInt MPIU_Get_count(MPI_Status *status, MPI_Datatype dtype, PetscCount *count)
2854: {
2855:   PetscMPIInt count2, err;

2857:   *count = 0; /* to prevent incorrect warnings of uninitialized variables */
2858:   err    = MPI_Get_count(status, dtype, &count2);
2859:   *count = count2;
2860:   return err;
2861: }

2863: static inline PetscMPIInt MPIU_Send(const void *buf, MPIU_Count count, MPI_Datatype dtype, PetscMPIInt dest, PetscMPIInt tag, MPI_Comm comm)
2864: {
2865:   PetscMPIInt count2, err;

2867:   PetscMPIIntCast_Internal(count, &count2);
2868:   err = MPI_Send((void *)buf, count2, dtype, dest, tag, comm);
2869:   return err;
2870: }

2872: static inline PetscMPIInt MPIU_Send_init(const void *buf, MPIU_Count count, MPI_Datatype dtype, PetscMPIInt dest, PetscMPIInt tag, MPI_Comm comm, MPI_Request *request)
2873: {
2874:   PetscMPIInt count2, err;

2876:   PetscMPIIntCast_Internal(count, &count2);
2877:   err = MPI_Send_init((void *)buf, count2, dtype, dest, tag, comm, request);
2878:   return err;
2879: }

2881: static inline PetscMPIInt MPIU_Isend(const void *buf, MPIU_Count count, MPI_Datatype dtype, PetscMPIInt dest, PetscMPIInt tag, MPI_Comm comm, MPI_Request *request)
2882: {
2883:   PetscMPIInt count2, err;

2885:   PetscMPIIntCast_Internal(count, &count2);
2886:   err = MPI_Isend((void *)buf, count2, dtype, dest, tag, comm, request);
2887:   return err;
2888: }

2890: static inline PetscMPIInt MPIU_Recv(const void *buf, MPIU_Count count, MPI_Datatype dtype, PetscMPIInt source, PetscMPIInt tag, MPI_Comm comm, MPI_Status *status)
2891: {
2892:   PetscMPIInt count2, err;

2894:   PetscMPIIntCast_Internal(count, &count2);
2895:   err = MPI_Recv((void *)buf, count2, dtype, source, tag, comm, status);
2896:   return err;
2897: }

2899: static inline PetscMPIInt MPIU_Recv_init(const void *buf, MPIU_Count count, MPI_Datatype dtype, PetscMPIInt source, PetscMPIInt tag, MPI_Comm comm, MPI_Request *request)
2900: {
2901:   PetscMPIInt count2, err;

2903:   PetscMPIIntCast_Internal(count, &count2);
2904:   err = MPI_Recv_init((void *)buf, count2, dtype, source, tag, comm, request);
2905:   return err;
2906: }

2908: static inline PetscMPIInt MPIU_Irecv(const void *buf, MPIU_Count count, MPI_Datatype dtype, PetscMPIInt source, PetscMPIInt tag, MPI_Comm comm, MPI_Request *request)
2909: {
2910:   PetscMPIInt count2, err;

2912:   PetscMPIIntCast_Internal(count, &count2);
2913:   err = MPI_Irecv((void *)buf, count2, dtype, source, tag, comm, request);
2914:   return err;
2915: }

2917: static inline PetscMPIInt MPIU_Reduce(const void *inbuf, void *outbuf, MPIU_Count count, MPI_Datatype dtype, MPI_Op op, PetscMPIInt root, MPI_Comm comm)
2918: {
2919:   PetscMPIInt count2, err;

2921:   PetscMPIIntCast_Internal(count, &count2);
2922:   err = MPI_Reduce((void *)inbuf, outbuf, count2, dtype, op, root, comm);
2923:   return err;
2924: }

2926:   #if PetscDefined(HAVE_MPI_REDUCE_LOCAL)
2927: static inline PetscMPIInt MPIU_Reduce_local(const void *inbuf, void *inoutbuf, MPIU_Count count, MPI_Datatype dtype, MPI_Op op)
2928: {
2929:   PetscMPIInt count2, err;

2931:   PetscMPIIntCast_Internal(count, &count2);
2932:   err = MPI_Reduce_local((void *)inbuf, inoutbuf, count2, dtype, op);
2933:   return err;
2934: }
2935:   #endif

2937:   #if !PetscDefined(USE_64BIT_INDICES)
2938:     #define MPIU_Scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm) MPI_Scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm)
2939:     #define MPIU_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm)  MPI_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm)
2940:   #else
2941:     #define MPIU_Scatterv(sendbuf, sendcount, displs, sendtype, recvbuf, recvcount, recvtype, root, comm) \
2942:       ((void)PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, PETSC_ERR_SUP, PETSC_ERROR_INITIAL, "Must have MPI 4 support for MPI_Scatterv_c() for this functionality, upgrade your MPI"), MPI_ERR_COUNT)
2943:     #define MPIU_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm) \
2944:       ((void)PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, PETSC_ERR_SUP, PETSC_ERROR_INITIAL, "Must have MPI 4 support for MPI_Scatterv_c() for this functionality, upgrade your MPI"), MPI_ERR_COUNT)
2945:   #endif

2947: #else

2949:   /* on 32 bit systems MPI_Count maybe 64-bit while PetscCount is 32-bit */
2950:   #define PetscCountCast_Internal(a, b) \
2951:     do { \
2952:       *b = 0; \
2953:       if (PetscUnlikely(a > (MPI_Count)PETSC_COUNT_MAX)) return MPI_ERR_COUNT; \
2954:       *b = (PetscMPIInt)a; \
2955:     } while (0)

2957: static inline PetscMPIInt MPIU_Get_count(MPI_Status *status, MPI_Datatype dtype, PetscCount *count)
2958: {
2959:   MPI_Count   count2;
2960:   PetscMPIInt err;

2962:   *count = 0; /* to prevent incorrect warnings of uninitialized variables */
2963:   err    = MPI_Get_count_c(status, dtype, &count2);
2964:   if (err) return err;
2965:   PetscCountCast_Internal(count2, count);
2966:   return MPI_SUCCESS;
2967: }

2969:   #define MPIU_Reduce(inbuf, outbuf, count, dtype, op, root, comm)      MPI_Reduce_c(inbuf, outbuf, (MPI_Count)(count), dtype, op, root, comm)
2970:   #define MPIU_Send(buf, count, dtype, dest, tag, comm)                 MPI_Send_c(buf, (MPI_Count)(count), dtype, dest, tag, comm)
2971:   #define MPIU_Send_init(buf, count, dtype, dest, tag, comm, request)   MPI_Send_init_c(buf, (MPI_Count)(count), dtype, dest, tag, comm, request)
2972:   #define MPIU_Isend(buf, count, dtype, dest, tag, comm, request)       MPI_Isend_c(buf, (MPI_Count)(count), dtype, dest, tag, comm, request)
2973:   #define MPIU_Recv(buf, count, dtype, source, tag, comm, status)       MPI_Recv_c(buf, (MPI_Count)(count), dtype, source, tag, comm, status)
2974:   #define MPIU_Recv_init(buf, count, dtype, source, tag, comm, request) MPI_Recv_init_c(buf, (MPI_Count)(count), dtype, source, tag, comm, request)
2975:   #define MPIU_Irecv(buf, count, dtype, source, tag, comm, request)     MPI_Irecv_c(buf, (MPI_Count)(count), dtype, source, tag, comm, request)
2976:   #if PetscDefined(HAVE_MPI_REDUCE_LOCAL)
2977:     #define MPIU_Reduce_local(inbuf, inoutbuf, count, dtype, op) MPI_Reduce_local_c(inbuf, inoutbuf, (MPI_Count)(count), dtype, op)
2978:   #endif

2980: /*MC
2981:   MPIU_Scatterv - A replacement for `MPI_Scatterv()` that can be called with `PetscInt` types when PETSc is built for either 32-bit indices or 64-bit indices.

2983:   Synopsis:
2984: #include <petscsys.h>
2985:   PetscMPIInt MPIU_Scatterv(const void *sendbuf, const PetscInt sendcounts[], const PetscInt displs[], MPI_Datatype sendtype, void *recvbuf, PetscInt recvcount, MPI_Datatype recvtype, PetscMPIInt root, MPI_Comm comm)

2987:   Collective

2989:   Input Parameters:
2990: + sendbuf    - address of send buffer
2991: . sendcounts - non-negative `PetscInt` array (of length `comm` group size) specifying the number of elements to send to each MPI process
2992: . displs     - `PetscInt` array (of length `comm` group size). Entry i specifies the displacement (relative to `sendbuf`) from which to take the outgoing data to process i
2993: . sendtype   - data type of `sendbuf` elements
2994: . recvcount  - number of elements in `recvbuf` (non-negative integer)
2995: . recvtype   - data type of `recvbuf` elements
2996: . root       - Rank of the MPI root process, which will dispatch the data to scatter
2997: - comm       - `MPI_Comm` communicator

2999:   Output Parameter:
3000: . recvbuf - the resulting scattered values on this MPI process

3002:   Level: developer

3004:   Notes:
3005:   Should be wrapped with `PetscCallMPI()` for error checking

3007:   This is different than most of the `MPIU_` wrappers in that all the count arguments are in `PetscInt`

3009: .seealso: [](stylePetscCount), `MPI_Allreduce()`, `MPIU_Gatherv()`
3010: M*/

3012:   #if !PetscDefined(USE_64BIT_INDICES)
3013:     #define MPIU_Scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm) MPI_Scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm)
3014:     #define MPIU_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm)  MPI_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm)
3015:   #else
3016:     #define MPIU_Scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm) MPI_Scatterv_c(sendbuf, (const MPI_Count *)(sendcounts), (const MPI_Aint *)(displs), sendtype, recvbuf, recvcount, recvtype, root, comm)
3017:     #define MPIU_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm)  MPI_Gatherv_c(sendbuf, sendcount, sendtype, recvbuf, (const MPI_Count *)(recvcounts), (const MPI_Aint *)(displs), recvtype, root, comm)
3018:   #endif

3020: #endif

3022: PETSC_EXTERN PetscMPIInt    MPIU_Allreduce_Private(const void *, void *, MPIU_Count, MPI_Datatype, MPI_Op, MPI_Comm);
3023: PETSC_EXTERN PetscErrorCode PetscCheckAllreduceSameLineAndCount_Private(MPI_Comm, const char *, PetscMPIInt, PetscMPIInt);

3025: static inline unsigned int PetscStrHash(const char *str)
3026: {
3027:   unsigned int c, hash = 5381;

3029:   while ((c = (unsigned int)*str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
3030:   return hash;
3031: }

3033: /*MC
3034:   MPIU_Allreduce - A replacement for `MPI_Allreduce()` that (1) performs single-count `MPIU_INT` operations in `PetscInt64` to detect
3035:                    integer overflows and (2) tries to determine if the call from all the MPI ranks occur in the
3036:                    same place in the PETSc code. This helps to detect bugs where different MPI ranks follow different code paths
3037:                    resulting in inconsistent and incorrect calls to `MPI_Allreduce()`.

3039:   Synopsis:
3040: #include <petscsys.h>
3041:   PetscMPIInt MPIU_Allreduce(void *indata, void *outdata, PetscCount count, MPI_Datatype dtype, MPI_Op op, MPI_Comm comm);

3043:   Collective

3045:   Input Parameters:
3046: + a     - pointer to the input data to be reduced
3047: . count - the number of MPI data items in `a` and `b`
3048: . dtype - the `MPI_Datatype`, for example `MPI_INT`
3049: . op    - the `MPI_Op`, for example `MPI_SUM`
3050: - comm  - the `MPI_Comm` on which the operation occurs

3052:   Output Parameter:
3053: . b - the reduced values

3055:   Level: developer

3057:   Note:
3058:   Should be wrapped with `PetscCallMPI()` for error checking

3060: .seealso: [](stylePetscCount), `MPI_Allreduce()`
3061: M*/
3062: #if PetscDefined(USE_DEBUG)
3063:   #define MPIU_Allreduce(a, b, count, dtype, op, comm) \
3064:     PetscMacroReturnStandard( \
3065:     PetscCall(PetscCheckAllreduceSameLineAndCount_Private((comm), __FILE__, (PetscMPIInt)__LINE__, (PetscMPIInt)(count))); \
3066:     PetscCallMPI(MPIU_Allreduce_Private((a), (b), (count), (dtype), (op), (comm)));)
3067: #else
3068:   #define MPIU_Allreduce(a, b, count, dtype, op, comm) MPIU_Allreduce_Private((a), (b), (count), (dtype), (op), (comm))
3069: #endif

3071: /* this is a vile hack */
3072: #if PetscDefined(HAVE_NECMPI)
3073:   #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)
3074:     #define MPI_Type_free(a) (*(a) = MPI_DATATYPE_NULL, 0);
3075:   #endif
3076: #endif

3078: /*
3079:     List of external packages and queries on it
3080: */
3081: PETSC_EXTERN PetscErrorCode PetscHasExternalPackage(const char[], PetscBool *);

3083: /* this cannot go here because it may be in a different shared library */
3084: PETSC_EXTERN PetscErrorCode PCMPIServerBegin(void);
3085: PETSC_EXTERN PetscErrorCode PCMPIServerEnd(void);
3086: PETSC_EXTERN PetscBool      PCMPIServerActive;
3087: PETSC_EXTERN PetscBool      PCMPIServerInSolve;
3088: PETSC_EXTERN PetscBool      PCMPIServerUseShmget;
3089: PETSC_EXTERN PetscErrorCode PetscShmgetAllocateArray(size_t, size_t, void **);
3090: PETSC_EXTERN PetscErrorCode PetscShmgetDeallocateArray(void **);
3091: PETSC_EXTERN PetscErrorCode PetscShmgetMapAddresses(MPI_Comm, PetscInt, const void **, void **);
3092: PETSC_EXTERN PetscErrorCode PetscShmgetUnmapAddresses(PetscInt, void **);
3093: PETSC_EXTERN PetscErrorCode PetscShmgetAddressesFinalize(void);

3095: /*S
3096:    PCMPIServerAddresses - Small bookkeeping record used by the `PCMPI` server to track shared-memory address mappings that have been distributed across the participating MPI ranks

3098:    Level: developer

3100:    Note:
3101:    Allocated and managed by the `PCMPI` server when running with shared-memory transfers enabled (`PCMPIServerUseShmget`). Destroyed with `PCMPIServerAddressesDestroy()`. Not intended to be inspected or constructed by user code.

3103: .seealso: `PCMPI`, `PCMPIServerBegin()`, `PCMPIServerEnd()`, `PCMPIServerAddressesDestroy()`, `PCMPIServerUseShmget`
3104: S*/
3105: typedef struct {
3106:   PetscInt n;
3107:   void    *addr[3];
3108: } PCMPIServerAddresses;
3109: PETSC_EXTERN PetscErrorCode PCMPIServerAddressesDestroy(PetscCtxRt);

3111: #define PETSC_HAVE_FORTRAN PETSC_DEPRECATED_MACRO(3, 20, 0, "PETSC_USE_FORTRAN_BINDINGS", ) PETSC_USE_FORTRAN_BINDINGS

3113: PETSC_EXTERN PetscErrorCode PetscBLASSetNumThreads(PetscInt);
3114: PETSC_EXTERN PetscErrorCode PetscBLASGetNumThreads(PetscInt *);

3116: /*MC
3117:    PetscSafePointerPlusOffset - Checks that a pointer is not `NULL` before applying an offset

3119:    Level: beginner

3121:    Note:
3122:    This is needed to avoid errors with undefined-behavior sanitizers such as
3123:    UBSan, assuming PETSc has been configured with `-fsanitize=undefined` as part of the compiler flags

3125: .seealso: `PetscInitialize()`
3126: M*/
3127: #define PetscSafePointerPlusOffset(ptr, offset) ((ptr) ? (ptr) + (offset) : NULL)

3129: /* this is required to force PetscDevice to be visible at the system level for the Fortran interface */
3130: #include <petscdevicetypes.h>

3132: #if PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)
3133: PETSC_EXTERN PetscErrorCode PetscStackView(FILE *);
3134: #else
3135:   #define PetscStackView(file) PETSC_SUCCESS
3136: #endif

3138: /*MC
3139:   PetscOverrideIntelMKLCPUVendorDetection - a macro to override Intel MKL's CPU vendor detection routines

3141:   Level: developer

3143:   Note:
3144:   Intel MKL may detect the CPU vendor at runtime and select less optimized code paths on non-Intel CPUs, such as AMD processors.
3145:   A common workaround is to intercept MKL's CPU detection functions (`mkl_serv_intel_cpu_true()` or `mkl_serv_get_cpu_true()`)
3146:   and force them to report that the processor is an Intel CPU, thus enabling the optimized code paths of MKL.

3148:   Put `PetscOverrideIntelMKLCPUVendorDetection();` before
3149:   `main()`, and then compile and link the main object file before the MKL libraries (which is typically already the case).
3150:   See <https://danieldk.eu/software/misc/intel-mkl-on-amd-zen>.

3152: .seealso: `PetscInitialize()`
3153: M*/
3154: #define PetscOverrideIntelMKLCPUVendorDetection() \
3155:   PETSC_EXTERN int mkl_serv_intel_cpu_true(void) \
3156:   { \
3157:     return 1; \
3158:   } \
3159:   typedef int (*fakeintel_fptr)(void); \
3160:   PETSC_EXTERN fakeintel_fptr mkl_serv_get_cpu_true(void) \
3161:   { \
3162:     return &mkl_serv_intel_cpu_true; \
3163:   }