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 *);
443: PETSC_EXTERN PetscErrorCode PetscCommCreateNonempty(MPI_Comm, const PetscInt[], MPI_Comm *);
444: PETSC_EXTERN PetscErrorCode PetscCommDestroyNonempty(MPI_Comm, MPI_Comm *);
446: #if PetscDefined(HAVE_KOKKOS)
447: PETSC_EXTERN PetscErrorCode PetscKokkosInitializeCheck(void); /* Initialize Kokkos if not yet. */
448: #endif
450: #if PetscDefined(HAVE_NVSHMEM)
451: PETSC_EXTERN PetscBool PetscBeganNvshmem;
452: PETSC_EXTERN PetscBool PetscNvshmemInitialized;
453: PETSC_EXTERN PetscErrorCode PetscNvshmemFinalize(void);
454: #endif
456: #if PetscDefined(HAVE_ELEMENTAL)
457: PETSC_EXTERN PetscErrorCode PetscElementalInitializePackage(void);
458: PETSC_EXTERN PetscErrorCode PetscElementalInitialized(PetscBool *);
459: PETSC_EXTERN PetscErrorCode PetscElementalFinalizePackage(void);
460: #endif
462: /*MC
463: PetscMalloc - Allocates memory for use with PETSc. One should use `PetscNew()`, `PetscMalloc1()` or `PetscCalloc1()` usually instead of `PetscMalloc()`
465: Synopsis:
466: #include <petscsys.h>
467: PetscErrorCode PetscMalloc(size_t m, void **result)
469: Not Collective
471: Input Parameter:
472: . m - number of bytes to allocate
474: Output Parameter:
475: . result - memory allocated
477: Level: beginner
479: Notes:
480: Memory is always allocated at least double aligned
482: It is safe to allocate with an m of 0 and pass the resulting pointer to `PetscFree()`.
483: However, the pointer should never be dereferenced or the program will crash.
485: Developer Note:
486: All the `PetscMallocN()` routines actually call `PetscMalloc()` behind the scenes.
488: Except for data structures that store information about the PETSc options database all memory allocated by PETSc is
489: obtained with `PetscMalloc()` or `PetscCalloc()`
491: .seealso: `PetscFree()`, `PetscNew()`, `PetscCalloc()`
492: M*/
493: #define PetscMalloc(a, b) ((*PetscTrMalloc)((a), PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))
495: /*MC
496: PetscRealloc - Reallocates memory
498: Synopsis:
499: #include <petscsys.h>
500: PetscErrorCode PetscRealloc(size_t m, void **result)
502: Not Collective
504: Input Parameters:
505: + m - number of bytes to allocate
506: - result - previous memory
508: Output Parameter:
509: . result - new memory allocated
511: Level: developer
513: Notes:
514: `results` must have already been obtained with `PetscMalloc()`
516: Memory is always allocated at least double aligned
518: .seealso: `PetscMalloc()`, `PetscFree()`, `PetscNew()`
519: M*/
520: #define PetscRealloc(a, b) ((*PetscTrRealloc)((a), __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void **)(b)))
522: /*MC
523: PetscAddrAlign - Rounds up an address to `PETSC_MEMALIGN` alignment
525: Synopsis:
526: #include <petscsys.h>
527: void *PetscAddrAlign(void *addr)
529: Not Collective
531: Input Parameter:
532: . addr - address to align (any pointer type)
534: Level: developer
536: .seealso: `PetscMallocAlign()`
537: M*/
538: #define PetscAddrAlign(a) ((void *)((((PETSC_UINTPTR_T)(a)) + (PETSC_MEMALIGN - 1)) & ~(PETSC_MEMALIGN - 1)))
540: /*MC
541: PetscCalloc - Allocates a cleared (zeroed) memory region aligned to `PETSC_MEMALIGN`, similar to `PetscMalloc()`
543: Synopsis:
544: #include <petscsys.h>
545: PetscErrorCode PetscCalloc(size_t m, void **result)
547: Not Collective
549: Input Parameter:
550: . m - number of bytes to allocate
552: Output Parameter:
553: . result - memory allocated
555: Level: beginner
557: Notes:
558: Memory is always allocated at least double aligned. This macro is useful in allocating memory pointed by void pointers
560: It is safe to allocate with an m of 0 and pass the resulting pointer to `PetscFree()`.
562: However, the pointer should never be dereferenced or the program will crash.
564: Developer Note:
565: All `PetscCallocN()` routines call `PetscCalloc()` behind the scenes.
567: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`
568: M*/
569: #define PetscCalloc(m, result) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)m), (void *)(result))
571: /*MC
572: PetscMalloc1 - Allocates an array of memory aligned to `PETSC_MEMALIGN`
574: Synopsis:
575: #include <petscsys.h>
576: PetscErrorCode PetscMalloc1(size_t m1, type **r1)
578: Not Collective
580: Input Parameter:
581: . m1 - number of elements to allocate (may be zero)
583: Output Parameter:
584: . r1 - memory allocated
586: Level: beginner
588: Note:
589: This uses `sizeof()` of the memory type requested to determine the total memory to be allocated; therefore, you should not
590: multiply the number of elements requested by the `sizeof()` the type. For example, use
591: .vb
592: PetscInt *id;
593: PetscMalloc1(10,&id);
594: .ve
595: not
596: .vb
597: PetscInt *id;
598: PetscMalloc1(10*sizeof(PetscInt),&id);
599: .ve
601: Does not zero the memory allocated, use `PetscCalloc1()` to obtain memory that has been zeroed.
603: 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
604: 32bit `int` computation - while computation in 64bit `size_t` would not overflow!
605: It's best if any arithmetic that is done for size computations is done with `size_t` type - avoiding arithmetic overflow!
607: `PetscMalloc[N]()` and `PetscCalloc[N]()` attempt to work-around this by casting the first variable to `size_t`.
608: This works for most expressions, but not all, such as
609: .vb
610: PetscInt *id, a, b;
611: 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
612: 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
613: .ve
615: These expressions should either be avoided, or appropriately cast variables to `size_t`:
616: .vb
617: PetscInt *id, a, b;
618: PetscMalloc1(use_a_squared ? (size_t)a * a * b : (size_t)a * b, &id); // Cast a to size_t before multiplication
619: PetscMalloc1(b * b + a, &id); // b is automatically cast to size_t and order-of-operations ensures size_t precision is maintained
620: .ve
622: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
623: M*/
624: #define PetscMalloc1(m1, r1) PetscMallocA(1, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(r1))
626: /*MC
627: PetscCalloc1 - Allocates a cleared (zeroed) array of memory aligned to `PETSC_MEMALIGN`
629: Synopsis:
630: #include <petscsys.h>
631: PetscErrorCode PetscCalloc1(size_t m1, type **r1)
633: Not Collective
635: Input Parameter:
636: . m1 - number of elements to allocate in 1st chunk (may be zero)
638: Output Parameter:
639: . r1 - memory allocated
641: Level: beginner
643: Note:
644: See `PetscMalloc1()` for more details on usage.
646: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
647: M*/
648: #define PetscCalloc1(m1, r1) PetscMallocA(1, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(r1))
650: /*MC
651: PetscMalloc2 - Allocates 2 arrays of memory both aligned to `PETSC_MEMALIGN`
653: Synopsis:
654: #include <petscsys.h>
655: PetscErrorCode PetscMalloc2(size_t m1, type **r1, size_t m2, type **r2)
657: Not Collective
659: Input Parameters:
660: + m1 - number of elements to allocate in 1st chunk (may be zero)
661: - m2 - number of elements to allocate in 2nd chunk (may be zero)
663: Output Parameters:
664: + r1 - memory allocated in first chunk
665: - r2 - memory allocated in second chunk
667: Level: developer
669: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc2()`
670: M*/
671: #define PetscMalloc2(m1, r1, m2, r2) PetscMallocA(2, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2))
673: /*MC
674: PetscCalloc2 - Allocates 2 cleared (zeroed) arrays of memory both aligned to `PETSC_MEMALIGN`
676: Synopsis:
677: #include <petscsys.h>
678: PetscErrorCode PetscCalloc2(size_t m1, type **r1, size_t m2, type **r2)
680: Not Collective
682: Input Parameters:
683: + m1 - number of elements to allocate in 1st chunk (may be zero)
684: - m2 - number of elements to allocate in 2nd chunk (may be zero)
686: Output Parameters:
687: + r1 - memory allocated in first chunk
688: - r2 - memory allocated in second chunk
690: Level: developer
692: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscMalloc2()`
693: M*/
694: #define PetscCalloc2(m1, r1, m2, r2) PetscMallocA(2, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2))
696: /*MC
697: PetscMalloc3 - Allocates 3 arrays of memory, all aligned to `PETSC_MEMALIGN`
699: Synopsis:
700: #include <petscsys.h>
701: PetscErrorCode PetscMalloc3(size_t m1, type **r1, size_t m2, type **r2, size_t m3, type **r3)
703: Not Collective
705: Input Parameters:
706: + m1 - number of elements to allocate in 1st chunk (may be zero)
707: . m2 - number of elements to allocate in 2nd chunk (may be zero)
708: - m3 - number of elements to allocate in 3rd chunk (may be zero)
710: Output Parameters:
711: + r1 - memory allocated in first chunk
712: . r2 - memory allocated in second chunk
713: - r3 - memory allocated in third chunk
715: Level: developer
717: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc3()`, `PetscFree3()`
718: M*/
719: #define PetscMalloc3(m1, r1, m2, r2, m3, r3) \
720: PetscMallocA(3, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3))
722: /*MC
723: PetscCalloc3 - Allocates 3 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
725: Synopsis:
726: #include <petscsys.h>
727: PetscErrorCode PetscCalloc3(size_t m1, type **r1, size_t m2, type **r2, size_t m3, type **r3)
729: Not Collective
731: Input Parameters:
732: + m1 - number of elements to allocate in 1st chunk (may be zero)
733: . m2 - number of elements to allocate in 2nd chunk (may be zero)
734: - m3 - number of elements to allocate in 3rd chunk (may be zero)
736: Output Parameters:
737: + r1 - memory allocated in first chunk
738: . r2 - memory allocated in second chunk
739: - r3 - memory allocated in third chunk
741: Level: developer
743: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscCalloc2()`, `PetscMalloc3()`, `PetscFree3()`
744: M*/
745: #define PetscCalloc3(m1, r1, m2, r2, m3, r3) \
746: PetscMallocA(3, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(r1), ((size_t)((size_t)m2) * sizeof(**(r2))), (r2), ((size_t)((size_t)m3) * sizeof(**(r3))), (r3))
748: /*MC
749: PetscMalloc4 - Allocates 4 arrays of memory, all aligned to `PETSC_MEMALIGN`
751: Synopsis:
752: #include <petscsys.h>
753: PetscErrorCode PetscMalloc4(size_t m1, type **r1, size_t m2, type **r2, size_t m3, type **r3, size_t m4, type **r4)
755: Not Collective
757: Input Parameters:
758: + m1 - number of elements to allocate in 1st chunk (may be zero)
759: . m2 - number of elements to allocate in 2nd chunk (may be zero)
760: . m3 - number of elements to allocate in 3rd chunk (may be zero)
761: - m4 - number of elements to allocate in 4th chunk (may be zero)
763: Output Parameters:
764: + r1 - memory allocated in first chunk
765: . r2 - memory allocated in second chunk
766: . r3 - memory allocated in third chunk
767: - r4 - memory allocated in fourth chunk
769: Level: developer
771: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
772: M*/
773: #define PetscMalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
774: PetscMallocA(4, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(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))
776: /*MC
777: PetscCalloc4 - Allocates 4 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
779: Synopsis:
780: #include <petscsys.h>
781: PetscErrorCode PetscCalloc4(size_t m1, type **r1, size_t m2, type **r2, size_t m3, type **r3, size_t m4, type **r4)
783: Not Collective
785: Input Parameters:
786: + m1 - number of elements to allocate in 1st chunk (may be zero)
787: . m2 - number of elements to allocate in 2nd chunk (may be zero)
788: . m3 - number of elements to allocate in 3rd chunk (may be zero)
789: - m4 - number of elements to allocate in 4th chunk (may be zero)
791: Output Parameters:
792: + r1 - memory allocated in first chunk
793: . r2 - memory allocated in second chunk
794: . r3 - memory allocated in third chunk
795: - r4 - memory allocated in fourth chunk
797: Level: developer
799: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc4()`, `PetscFree4()`
800: M*/
801: #define PetscCalloc4(m1, r1, m2, r2, m3, r3, m4, r4) \
802: PetscMallocA(4, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(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))
804: /*MC
805: PetscMalloc5 - Allocates 5 arrays of memory, all aligned to `PETSC_MEMALIGN`
807: Synopsis:
808: #include <petscsys.h>
809: 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)
811: Not Collective
813: Input Parameters:
814: + m1 - number of elements to allocate in 1st chunk (may be zero)
815: . m2 - number of elements to allocate in 2nd chunk (may be zero)
816: . m3 - number of elements to allocate in 3rd chunk (may be zero)
817: . m4 - number of elements to allocate in 4th chunk (may be zero)
818: - m5 - number of elements to allocate in 5th chunk (may be zero)
820: Output Parameters:
821: + r1 - memory allocated in first chunk
822: . r2 - memory allocated in second chunk
823: . r3 - memory allocated in third chunk
824: . r4 - memory allocated in fourth chunk
825: - r5 - memory allocated in fifth chunk
827: Level: developer
829: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc5()`, `PetscFree5()`
830: M*/
831: #define PetscMalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
832: PetscMallocA(5, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(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))
834: /*MC
835: PetscCalloc5 - Allocates 5 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
837: Synopsis:
838: #include <petscsys.h>
839: 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)
841: Not Collective
843: Input Parameters:
844: + m1 - number of elements to allocate in 1st chunk (may be zero)
845: . m2 - number of elements to allocate in 2nd chunk (may be zero)
846: . m3 - number of elements to allocate in 3rd chunk (may be zero)
847: . m4 - number of elements to allocate in 4th chunk (may be zero)
848: - m5 - number of elements to allocate in 5th chunk (may be zero)
850: Output Parameters:
851: + r1 - memory allocated in first chunk
852: . r2 - memory allocated in second chunk
853: . r3 - memory allocated in third chunk
854: . r4 - memory allocated in fourth chunk
855: - r5 - memory allocated in fifth chunk
857: Level: developer
859: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc5()`, `PetscFree5()`
860: M*/
861: #define PetscCalloc5(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5) \
862: PetscMallocA(5, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(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))
864: /*MC
865: PetscMalloc6 - Allocates 6 arrays of memory, all aligned to `PETSC_MEMALIGN`
867: Synopsis:
868: #include <petscsys.h>
869: 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)
871: Not Collective
873: Input Parameters:
874: + m1 - number of elements to allocate in 1st chunk (may be zero)
875: . m2 - number of elements to allocate in 2nd chunk (may be zero)
876: . m3 - number of elements to allocate in 3rd chunk (may be zero)
877: . m4 - number of elements to allocate in 4th chunk (may be zero)
878: . m5 - number of elements to allocate in 5th chunk (may be zero)
879: - m6 - number of elements to allocate in 6th chunk (may be zero)
881: Output Parameteasr:
882: + r1 - memory allocated in first chunk
883: . r2 - memory allocated in second chunk
884: . r3 - memory allocated in third chunk
885: . r4 - memory allocated in fourth chunk
886: . r5 - memory allocated in fifth chunk
887: - r6 - memory allocated in sixth chunk
889: Level: developer
891: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc6()`, `PetscFree3()`, `PetscFree4()`, `PetscFree5()`, `PetscFree6()`
892: M*/
893: #define PetscMalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
894: PetscMallocA(6, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(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))
896: /*MC
897: PetscCalloc6 - Allocates 6 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
899: Synopsis:
900: #include <petscsys.h>
901: 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)
903: Not Collective
905: Input Parameters:
906: + m1 - number of elements to allocate in 1st chunk (may be zero)
907: . m2 - number of elements to allocate in 2nd chunk (may be zero)
908: . m3 - number of elements to allocate in 3rd chunk (may be zero)
909: . m4 - number of elements to allocate in 4th chunk (may be zero)
910: . m5 - number of elements to allocate in 5th chunk (may be zero)
911: - m6 - number of elements to allocate in 6th chunk (may be zero)
913: Output Parameters:
914: + r1 - memory allocated in first chunk
915: . r2 - memory allocated in second chunk
916: . r3 - memory allocated in third chunk
917: . r4 - memory allocated in fourth chunk
918: . r5 - memory allocated in fifth chunk
919: - r6 - memory allocated in sixth chunk
921: Level: developer
923: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc6()`, `PetscFree6()`
924: M*/
925: #define PetscCalloc6(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6) \
926: PetscMallocA(6, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(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))
928: /*MC
929: PetscMalloc7 - Allocates 7 arrays of memory, all aligned to `PETSC_MEMALIGN`
931: Synopsis:
932: #include <petscsys.h>
933: 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)
935: Not Collective
937: Input Parameters:
938: + m1 - number of elements to allocate in 1st chunk (may be zero)
939: . m2 - number of elements to allocate in 2nd chunk (may be zero)
940: . m3 - number of elements to allocate in 3rd chunk (may be zero)
941: . m4 - number of elements to allocate in 4th chunk (may be zero)
942: . m5 - number of elements to allocate in 5th chunk (may be zero)
943: . m6 - number of elements to allocate in 6th chunk (may be zero)
944: - m7 - number of elements to allocate in 7th chunk (may be zero)
946: Output Parameters:
947: + r1 - memory allocated in first chunk
948: . r2 - memory allocated in second chunk
949: . r3 - memory allocated in third chunk
950: . r4 - memory allocated in fourth chunk
951: . r5 - memory allocated in fifth chunk
952: . r6 - memory allocated in sixth chunk
953: - r7 - memory allocated in seventh chunk
955: Level: developer
957: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscCalloc7()`, `PetscFree7()`
958: M*/
959: #define PetscMalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
960: PetscMallocA(7, PETSC_FALSE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(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))
962: /*MC
963: PetscCalloc7 - Allocates 7 cleared (zeroed) arrays of memory, all aligned to `PETSC_MEMALIGN`
965: Synopsis:
966: #include <petscsys.h>
967: 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)
969: Not Collective
971: Input Parameters:
972: + m1 - number of elements to allocate in 1st chunk (may be zero)
973: . m2 - number of elements to allocate in 2nd chunk (may be zero)
974: . m3 - number of elements to allocate in 3rd chunk (may be zero)
975: . m4 - number of elements to allocate in 4th chunk (may be zero)
976: . m5 - number of elements to allocate in 5th chunk (may be zero)
977: . m6 - number of elements to allocate in 6th chunk (may be zero)
978: - m7 - number of elements to allocate in 7th chunk (may be zero)
980: Output Parameters:
981: + r1 - memory allocated in first chunk
982: . r2 - memory allocated in second chunk
983: . r3 - memory allocated in third chunk
984: . r4 - memory allocated in fourth chunk
985: . r5 - memory allocated in fifth chunk
986: . r6 - memory allocated in sixth chunk
987: - r7 - memory allocated in seventh chunk
989: Level: developer
991: .seealso: `PetscFree()`, `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscMalloc7()`, `PetscFree7()`
992: M*/
993: #define PetscCalloc7(m1, r1, m2, r2, m3, r3, m4, r4, m5, r5, m6, r6, m7, r7) \
994: PetscMallocA(7, PETSC_TRUE, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ((size_t)((size_t)m1) * sizeof(**(r1))), (void *)(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))
996: /*MC
997: PetscNew - Allocates memory of a particular type, zeros the memory! Aligned to `PETSC_MEMALIGN`
999: Synopsis:
1000: #include <petscsys.h>
1001: PetscErrorCode PetscNew(type **result)
1003: Not Collective
1005: Output Parameter:
1006: . result - memory allocated, sized to match pointer `type`
1008: Level: beginner
1010: Developer Note:
1011: Calls `PetscCalloc()` with the appropriate memory size obtained from `type`
1013: .seealso: `PetscFree()`, `PetscMalloc()`, `PetscCall()`, `PetscCalloc1()`, `PetscMalloc1()`
1014: M*/
1015: #define PetscNew(b) PetscCalloc1(1, (b))
1017: /*MC
1018: PetscNewLog - Deprecated alias for `PetscNew()`; previously logged the allocation against a `PetscObject`
1020: Synopsis:
1021: #include <petscsys.h>
1022: PetscErrorCode PetscNewLog(PetscObject obj, type **result)
1024: Not Collective
1026: Input Parameter:
1027: . obj - the `PetscObject` to which the allocation used to be charged (now ignored)
1029: Output Parameter:
1030: . result - the newly allocated memory, sized to match `type`
1032: Level: deprecated
1034: Note:
1035: This macro is retained only for source compatibility. Use `PetscNew()` instead; PETSc no longer associates allocations with a specific `PetscObject`.
1037: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscCalloc1()`, `PetscFree()`
1038: M*/
1039: #define PetscNewLog(o, b) PETSC_DEPRECATED_MACRO(3, 18, 0, "PetscNew()", ) PetscNew(b)
1041: /*MC
1042: PetscFree - Frees memory
1044: Synopsis:
1045: #include <petscsys.h>
1046: PetscErrorCode PetscFree(void *memory)
1048: Not Collective
1050: Input Parameter:
1051: . memory - memory to free (the pointer is ALWAYS set to `NULL` upon success)
1053: Level: beginner
1055: Notes:
1056: Do not free memory obtained with `PetscMalloc2()`, `PetscCalloc2()` etc, they must be freed with `PetscFree2()` etc.
1058: It is safe to call `PetscFree()` on a `NULL` pointer.
1060: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc1()`
1061: M*/
1062: #define PetscFree(a) ((PetscErrorCode)((*PetscTrFree)((void *)(a), __LINE__, PETSC_FUNCTION_NAME, __FILE__) || ((a) = PETSC_NULLPTR, PETSC_SUCCESS)))
1064: /*MC
1065: PetscFree2 - Frees 2 chunks of memory obtained with `PetscMalloc2()`
1067: Synopsis:
1068: #include <petscsys.h>
1069: PetscErrorCode PetscFree2(void *memory1, void *memory2)
1071: Not Collective
1073: Input Parameters:
1074: + memory1 - memory to free
1075: - memory2 - 2nd memory to free
1077: Level: developer
1079: Notes:
1080: Memory must have been obtained with `PetscMalloc2()`
1082: The arguments need to be in the same order as they were in the call to `PetscMalloc2()`
1084: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`
1085: M*/
1086: #define PetscFree2(m1, m2) PetscFreeA(2, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void *)&(m1), &(m2))
1088: /*MC
1089: PetscFree3 - Frees 3 chunks of memory obtained with `PetscMalloc3()`
1091: Synopsis:
1092: #include <petscsys.h>
1093: PetscErrorCode PetscFree3(void *memory1, void *memory2, void *memory3)
1095: Not Collective
1097: Input Parameters:
1098: + memory1 - memory to free
1099: . memory2 - 2nd memory to free
1100: - memory3 - 3rd memory to free
1102: Level: developer
1104: Notes:
1105: Memory must have been obtained with `PetscMalloc3()`
1107: The arguments need to be in the same order as they were in the call to `PetscMalloc3()`
1109: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`
1110: M*/
1111: #define PetscFree3(m1, m2, m3) PetscFreeA(3, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void *)&(m1), &(m2), &(m3))
1113: /*MC
1114: PetscFree4 - Frees 4 chunks of memory obtained with `PetscMalloc4()`
1116: Synopsis:
1117: #include <petscsys.h>
1118: PetscErrorCode PetscFree4(void *m1, void *m2, void *m3, void *m4)
1120: Not Collective
1122: Input Parameters:
1123: + m1 - memory to free
1124: . m2 - 2nd memory to free
1125: . m3 - 3rd memory to free
1126: - m4 - 4th memory to free
1128: Level: developer
1130: Notes:
1131: Memory must have been obtained with `PetscMalloc4()`
1133: The arguments need to be in the same order as they were in the call to `PetscMalloc4()`
1135: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`
1136: M*/
1137: #define PetscFree4(m1, m2, m3, m4) PetscFreeA(4, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void *)&(m1), &(m2), &(m3), &(m4))
1139: /*MC
1140: PetscFree5 - Frees 5 chunks of memory obtained with `PetscMalloc5()`
1142: Synopsis:
1143: #include <petscsys.h>
1144: PetscErrorCode PetscFree5(void *m1, void *m2, void *m3, void *m4, void *m5)
1146: Not Collective
1148: Input Parameters:
1149: + m1 - memory to free
1150: . m2 - 2nd memory to free
1151: . m3 - 3rd memory to free
1152: . m4 - 4th memory to free
1153: - m5 - 5th memory to free
1155: Level: developer
1157: Notes:
1158: Memory must have been obtained with `PetscMalloc5()`
1160: The arguments need to be in the same order as they were in the call to `PetscMalloc5()`
1162: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`
1163: M*/
1164: #define PetscFree5(m1, m2, m3, m4, m5) PetscFreeA(5, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void *)&(m1), &(m2), &(m3), &(m4), &(m5))
1166: /*MC
1167: PetscFree6 - Frees 6 chunks of memory obtained with `PetscMalloc6()`
1169: Synopsis:
1170: #include <petscsys.h>
1171: PetscErrorCode PetscFree6(void *m1, void *m2, void *m3, void *m4, void *m5, void *m6)
1173: Not Collective
1175: Input Parameters:
1176: + m1 - memory to free
1177: . m2 - 2nd memory to free
1178: . m3 - 3rd memory to free
1179: . m4 - 4th memory to free
1180: . m5 - 5th memory to free
1181: - m6 - 6th memory to free
1183: Level: developer
1185: Notes:
1186: Memory must have been obtained with `PetscMalloc6()`
1188: The arguments need to be in the same order as they were in the call to `PetscMalloc6()`
1190: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`
1191: M*/
1192: #define PetscFree6(m1, m2, m3, m4, m5, m6) PetscFreeA(6, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void *)&(m1), &(m2), &(m3), &(m4), &(m5), &(m6))
1194: /*MC
1195: PetscFree7 - Frees 7 chunks of memory obtained with `PetscMalloc7()`
1197: Synopsis:
1198: #include <petscsys.h>
1199: PetscErrorCode PetscFree7(void *m1, void *m2, void *m3, void *m4, void *m5, void *m6, void *m7)
1201: Not Collective
1203: Input Parameters:
1204: + m1 - memory to free
1205: . m2 - 2nd memory to free
1206: . m3 - 3rd memory to free
1207: . m4 - 4th memory to free
1208: . m5 - 5th memory to free
1209: . m6 - 6th memory to free
1210: - m7 - 7th memory to free
1212: Level: developer
1214: Notes:
1215: Memory must have been obtained with `PetscMalloc7()`
1217: The arguments need to be in the same order as they were in the call to `PetscMalloc7()`
1219: .seealso: `PetscNew()`, `PetscMalloc()`, `PetscMalloc2()`, `PetscFree()`, `PetscMalloc3()`, `PetscMalloc4()`, `PetscMalloc5()`, `PetscMalloc6()`,
1220: `PetscMalloc7()`
1221: M*/
1222: #define PetscFree7(m1, m2, m3, m4, m5, m6, m7) PetscFreeA(7, __LINE__, PETSC_FUNCTION_NAME, __FILE__, (void *)&(m1), &(m2), &(m3), &(m4), &(m5), &(m6), &(m7))
1224: PETSC_EXTERN PetscErrorCode PetscMallocA(int, PetscBool, int, const char *, const char *, size_t, void *, ...);
1225: PETSC_EXTERN PetscErrorCode PetscFreeA(int, int, const char *, const char *, void *, ...);
1226: PETSC_EXTERN PetscErrorCode (*PetscTrMalloc)(size_t, PetscBool, int, const char[], const char[], void **);
1227: PETSC_EXTERN PetscErrorCode (*PetscTrFree)(void *, int, const char[], const char[]);
1228: PETSC_EXTERN PetscErrorCode (*PetscTrRealloc)(size_t, int, const char[], const char[], void **);
1229: PETSC_EXTERN PetscErrorCode PetscMallocSetCoalesce(PetscBool);
1230: 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 **));
1231: PETSC_EXTERN PetscErrorCode PetscMallocClear(void);
1233: /*
1234: Unlike PetscMallocSet and PetscMallocClear which overwrite the existing settings, these two functions save the previous choice of allocator, and should be used in pair.
1235: */
1236: PETSC_EXTERN PetscErrorCode PetscMallocSetDRAM(void);
1237: PETSC_EXTERN PetscErrorCode PetscMallocResetDRAM(void);
1238: #if PetscDefined(HAVE_CUDA)
1239: PETSC_EXTERN PetscErrorCode PetscMallocSetCUDAHost(void);
1240: PETSC_EXTERN PetscErrorCode PetscMallocResetCUDAHost(void);
1241: #endif
1242: #if PetscDefined(HAVE_HIP)
1243: PETSC_EXTERN PetscErrorCode PetscMallocSetHIPHost(void);
1244: PETSC_EXTERN PetscErrorCode PetscMallocResetHIPHost(void);
1245: #endif
1247: #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
1248: #define MPIU_2PETSCLOGDOUBLE MPI_2DOUBLE_PRECISION
1250: /*
1251: Routines for tracing memory corruption/bleeding with default PETSc memory allocation
1252: */
1253: PETSC_EXTERN PetscErrorCode PetscMallocDump(FILE *);
1254: PETSC_EXTERN PetscErrorCode PetscMallocView(FILE *);
1255: PETSC_EXTERN PetscErrorCode PetscMallocGetCurrentUsage(PetscLogDouble *);
1256: PETSC_EXTERN PetscErrorCode PetscMallocGetMaximumUsage(PetscLogDouble *);
1257: PETSC_EXTERN PetscErrorCode PetscMallocPushMaximumUsage(int);
1258: PETSC_EXTERN PetscErrorCode PetscMallocPopMaximumUsage(int, PetscLogDouble *);
1259: PETSC_EXTERN PetscErrorCode PetscMallocSetDebug(PetscBool, PetscBool);
1260: PETSC_EXTERN PetscErrorCode PetscMallocGetDebug(PetscBool *, PetscBool *, PetscBool *);
1261: PETSC_EXTERN PetscErrorCode PetscMallocValidate(int, const char[], const char[]);
1262: PETSC_EXTERN PetscErrorCode PetscMallocViewSet(PetscLogDouble);
1263: PETSC_EXTERN PetscErrorCode PetscMallocViewGet(PetscBool *);
1264: PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeSet(PetscBool);
1265: PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeGet(PetscBool *);
1267: PETSC_EXTERN PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType, MPI_Datatype *);
1268: PETSC_EXTERN PetscErrorCode PetscMPIDataTypeToPetscDataType(MPI_Datatype, PetscDataType *);
1269: PETSC_EXTERN PetscErrorCode PetscDataTypeGetSize(PetscDataType, size_t *);
1270: PETSC_EXTERN PetscErrorCode PetscDataTypeFromString(const char *, PetscDataType *, PetscBool *);
1272: /*
1273: These are MPI operations for MPI_Allreduce() etc
1274: */
1275: PETSC_EXTERN MPI_Op MPIU_MAXSUM_OP;
1276: #if PetscDefined(USE_REAL___FLOAT128) || PetscDefined(USE_REAL___FP16)
1277: PETSC_EXTERN MPI_Op MPIU_SUM;
1278: PETSC_EXTERN MPI_Op MPIU_MAX;
1279: PETSC_EXTERN MPI_Op MPIU_MIN;
1280: #else
1281: #define MPIU_SUM MPI_SUM
1282: #define MPIU_MAX MPI_MAX
1283: #define MPIU_MIN MPI_MIN
1284: #endif
1285: PETSC_EXTERN MPI_Op Petsc_Garbage_SetIntersectOp;
1286: PETSC_EXTERN PetscErrorCode PetscMaxSum(MPI_Comm, const PetscInt[], PetscInt *, PetscInt *);
1288: #if (PetscDefined(HAVE_REAL___FLOAT128) && !PetscDefined(SKIP_REAL___FLOAT128)) || (PetscDefined(HAVE_REAL___FP16) && !PetscDefined(SKIP_REAL___FP16))
1289: /*MC
1290: MPIU_SUM___FP16___FLOAT128 - `MPI_Op` that acts as a replacement for `MPI_SUM` with
1291: custom `MPI_Datatype` `MPIU___FLOAT128`, `MPIU___COMPLEX128`, and `MPIU___FP16`.
1293: Level: advanced
1295: Developer Note:
1296: This should be unified with `MPIU_SUM`
1298: .seealso: `MPI_Op`, `MPI_Datatype`, `MPIU_REAL`, `MPIU_SCALAR`, `MPIU_COMPLEX`
1299: M*/
1300: PETSC_EXTERN MPI_Op MPIU_SUM___FP16___FLOAT128;
1301: #endif
1303: /*
1304: These are so that in extern C code we can cast function pointers to non-extern C
1305: function pointers. Since the regular C++ code expects its function pointers to be C++
1306: */
1308: /*S
1309: PetscVoidFn - A prototype of a `void fn(void)` function
1311: Level: advanced
1313: Notes:
1314: `PetscVoidFn *` plays the role of `void *` for function pointers in the PETSc API that do not return an error code.
1315: It is used where a function pointer is needed but it is not possible to use the full prototype of the function.
1317: `PetscErrorCodeFn` is similar to `PetscVoidFn` but should be used when the function returns a `PetscErrorCode`
1319: .seealso: `PetscErrorCodeFn`, `PetscObject`, `PetscObjectDestroy()`
1320: S*/
1321: PETSC_EXTERN_TYPEDEF typedef void PetscVoidFn(void);
1323: PETSC_EXTERN_TYPEDEF typedef PetscVoidFn *PetscVoidFunction PETSC_DEPRECATED_TYPEDEF(3, 26, 0, "PetscVoidFn*", );
1324: PETSC_EXTERN_TYPEDEF typedef PetscVoidFn **PetscVoidStarFunction PETSC_DEPRECATED_TYPEDEF(3, 26, 0, "PetscVoidFn**", );
1326: /*S
1327: PetscErrorCodeFn - a function typedef that represents abstractly a function that returns a PETSc error code
1328: and takes any number of arguments. Since C/C++ has no way to express this concept, it is implemented as `void (fn)(void)`.
1330: Level: advanced
1332: Notes:
1333: `PetscErrorCodeFn *` plays the role of `void *` for function pointers in the PETSc API that return an error code.
1334: It is used where a function pointer is needed but it is not possible to use the full prototype of the function,
1335: for example `VecSetOperation()`.
1337: `PetscVoidFn` is similar to `PetscErrorCodeFn` but should be used when the function does not return a `PetscErrorCode`.
1339: Developer Notes:
1340: This function type is equivalent to `PetscVoidFn`*.
1342: At the C/C++ syntax level this construct adds nothing of value to the PETSc source code. It provides a way, at the abstract
1343: PETSc API level, to indicate specifically functions that return PETSc error codes as opposed to any C/C++ function.
1345: .seealso: `PetscVoidFn`, `PetscObject`, `PetscObjectDestroy()`, `VecSetOperation()`
1346: S*/
1347: PETSC_EXTERN_TYPEDEF typedef void PetscErrorCodeFn(void);
1349: /*
1350: Defines PETSc error handling.
1351: */
1352: #include <petscerror.h>
1354: PETSC_EXTERN PetscBool PetscCIEnabled; /* code is running in the PETSc test harness CI */
1355: PETSC_EXTERN PetscBool PetscCIEnabledPortableErrorOutput; /* error output is stripped to ensure portability of error messages across systems */
1356: PETSC_EXTERN const char *PetscCIFilename(const char *);
1357: PETSC_EXTERN int PetscCILinenumber(int);
1359: #define PETSC_SMALLEST_CLASSID 1211211
1360: PETSC_EXTERN PetscClassId PETSC_LARGEST_CLASSID;
1361: PETSC_EXTERN PetscClassId PETSC_OBJECT_CLASSID;
1362: PETSC_EXTERN PetscErrorCode PetscClassIdRegister(const char[], PetscClassId *);
1364: /*
1365: Routines that get memory usage information from the OS
1366: */
1367: PETSC_EXTERN PetscErrorCode PetscMemoryGetCurrentUsage(PetscLogDouble *);
1368: PETSC_EXTERN PetscErrorCode PetscMemoryGetMaximumUsage(PetscLogDouble *);
1369: PETSC_EXTERN PetscErrorCode PetscMemorySetGetMaximumUsage(void);
1370: PETSC_EXTERN PetscErrorCode PetscMemoryTrace(const char[]);
1372: PETSC_EXTERN PetscErrorCode PetscSleep(PetscReal);
1374: /*
1375: Initialization of PETSc
1376: */
1377: PETSC_EXTERN PetscErrorCode PetscInitialize(int *, char ***, const char[], const char[]);
1378: PETSC_EXTERN PetscErrorCode PetscInitializeNoPointers(int, char *[], const char[], const char[]);
1379: PETSC_EXTERN PetscErrorCode PetscInitializeNoArguments(void);
1380: PETSC_EXTERN PetscErrorCode PetscInitialized(PetscBool *);
1381: PETSC_EXTERN PetscErrorCode PetscFinalized(PetscBool *);
1382: PETSC_EXTERN PetscErrorCode PetscFinalize(void);
1383: PETSC_EXTERN PetscErrorCode PetscInitializeFortran(void);
1384: PETSC_EXTERN PetscErrorCode PetscGetArgs(int *, char ***);
1385: PETSC_EXTERN PetscErrorCode PetscGetArguments(char ***);
1386: PETSC_EXTERN PetscErrorCode PetscFreeArguments(char **);
1387: PETSC_EXTERN PetscErrorCode PetscSetMPIThreadRequiredType(PetscMPIInt);
1389: PETSC_EXTERN PetscErrorCode PetscEnd(void);
1390: PETSC_EXTERN PetscErrorCode PetscSysInitializePackage(void);
1391: PETSC_EXTERN PetscErrorCode PetscSysFinalizePackage(void);
1393: PETSC_EXTERN PetscErrorCode PetscPythonInitialize(const char[], const char[]);
1394: PETSC_EXTERN PetscErrorCode PetscPythonFinalize(void);
1395: PETSC_EXTERN PetscErrorCode PetscPythonPrintError(void);
1396: PETSC_EXTERN PetscErrorCode PetscPythonMonitorSet(PetscObject, const char[]);
1398: /*
1399: Functions that can act on any PETSc object.
1400: */
1401: PETSC_EXTERN PetscErrorCode PetscObjectDestroy(PetscObject *);
1402: PETSC_EXTERN PetscErrorCode PetscObjectGetComm(PetscObject, MPI_Comm *);
1403: PETSC_EXTERN PetscErrorCode PetscObjectGetClassId(PetscObject, PetscClassId *);
1404: PETSC_EXTERN PetscErrorCode PetscObjectGetClassName(PetscObject, const char *[]);
1405: PETSC_EXTERN PetscErrorCode PetscObjectGetType(PetscObject, const char *[]);
1406: PETSC_EXTERN PetscErrorCode PetscObjectSetName(PetscObject, const char[]);
1407: PETSC_EXTERN PetscErrorCode PetscObjectGetName(PetscObject, const char *[]);
1408: PETSC_EXTERN PetscErrorCode PetscObjectSetTabLevel(PetscObject, PetscInt);
1409: PETSC_EXTERN PetscErrorCode PetscObjectGetTabLevel(PetscObject, PetscInt *);
1410: PETSC_EXTERN PetscErrorCode PetscObjectIncrementTabLevel(PetscObject, PetscObject, PetscInt);
1411: PETSC_EXTERN PetscErrorCode PetscObjectReference(PetscObject);
1412: PETSC_EXTERN PetscErrorCode PetscObjectGetReference(PetscObject, PetscInt *);
1413: PETSC_EXTERN PetscErrorCode PetscObjectDereference(PetscObject);
1414: PETSC_EXTERN PetscErrorCode PetscObjectGetNewTag(PetscObject, PetscMPIInt *);
1415: PETSC_EXTERN PetscErrorCode PetscObjectCompose(PetscObject, const char[], PetscObject);
1416: PETSC_EXTERN PetscErrorCode PetscObjectRemoveReference(PetscObject, const char[]);
1417: PETSC_EXTERN PetscErrorCode PetscObjectQuery(PetscObject, const char[], PetscObject *);
1418: PETSC_EXTERN PetscErrorCode PetscObjectComposeFunction_Private(PetscObject, const char[], PetscErrorCodeFn *);
1419: #define PetscObjectComposeFunction(a, b, ...) PetscObjectComposeFunction_Private((a), (b), (PetscErrorCodeFn *)(__VA_ARGS__))
1420: PETSC_EXTERN PetscErrorCode PetscObjectSetFromOptions(PetscObject);
1421: PETSC_EXTERN PetscErrorCode PetscObjectSetUp(PetscObject);
1422: PETSC_EXTERN PetscErrorCode PetscObjectSetPrintedOptions(PetscObject);
1423: PETSC_EXTERN PetscErrorCode PetscObjectInheritPrintedOptions(PetscObject, PetscObject);
1424: PETSC_EXTERN PetscErrorCode PetscCommGetNewTag(MPI_Comm, PetscMPIInt *);
1425: PETSC_EXTERN PetscErrorCode PetscObjectGetId(PetscObject, PetscObjectId *);
1426: PETSC_EXTERN PetscErrorCode PetscObjectCompareId(PetscObject, PetscObjectId, PetscBool *);
1428: /*MC
1429: PetscObjectParameterSetDefault - sets a parameter default value in a `PetscObject` to a new default value.
1430: If the current value matches the old default value, then the current value is also set to the new value.
1432: No Fortran Support
1434: Synopsis:
1435: #include <petscsys.h>
1436: PetscBool PetscObjectParameterSetDefault(PetscObject obj, char* NAME, PetscReal value);
1438: Input Parameters:
1439: + obj - the `PetscObject`
1440: . NAME - the name of the parameter, unquoted
1441: - value - the new value
1443: Level: developer
1445: Notes:
1446: The defaults for an object are the values set when the object's type is set.
1448: This should only be used in object constructors, such as, `SNESCreate_NGS()`.
1450: This only works for parameters that are declared in the struct with `PetscObjectParameterDeclare()`
1452: .seealso: `PetscObjectParameterDeclare()`, `PetscInitialize()`, `PetscFinalize()`, `PetscObject`, `SNESParametersInitialize()`
1453: M*/
1454: #define PetscObjectParameterSetDefault(obj, NAME, value) \
1455: do { \
1456: if ((obj)->NAME == (obj)->default_##NAME) (obj)->NAME = value; \
1457: (obj)->default_##NAME = value; \
1458: } while (0)
1460: /*MC
1461: PetscObjectParameterDeclare - declares a parameter in a `PetscObject` and a location to store its default
1463: No Fortran Support
1465: Synopsis:
1466: #include <petscsys.h>
1467: PetscObjectParameterDeclare(type, NAME)
1469: Input Parameters:
1470: + type - the type of the parameter, for example `PetscInt`
1471: - NAME - the name of the parameter, unquoted
1473: Level: developer.
1475: .seealso: `PetscObjectParameterSetDefault()`, `PetscInitialize()`, `PetscFinalize()`, `PetscObject`, `SNESParametersInitialize()`
1476: M*/
1477: #define PetscObjectParameterDeclare(type, NAME) type NAME, default_##NAME
1479: /*MC
1480: PetscObjectParameterDeclarePtr - declares a pointer-valued parameter inside a `PetscObject` together with a slot for its default
1482: No Fortran Support
1484: Synopsis:
1485: #include <petscsys.h>
1486: PetscObjectParameterDeclarePtr(type, NAME)
1488: Input Parameters:
1489: + type - the pointee type of the parameter, for example `PetscReal`
1490: - NAME - the name of the parameter, unquoted
1492: Level: developer
1494: Note:
1495: 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.
1497: .seealso: `PetscObjectParameterDeclare()`, `PetscObjectParameterSetDefault()`, `PetscInitialize()`, `PetscFinalize()`, `PetscObject`, `SNESParametersInitialize()`
1498: M*/
1499: #define PetscObjectParameterDeclarePtr(type, NAME) type *NAME, *default_##NAME
1501: /*MC
1502: PetscCtx - indicates an argument that can be a pointer to any C struct (or Fortran derived type).
1504: Level: developer
1506: Notes:
1507: This should not be used for arrays of unknown type.
1509: Fortran Notes:
1510: A Fortran code that calls a function with a `PetscCtx` argument would declare the variable `ctx` with
1511: .vb
1512: type(AppType) :: ctx
1513: .ve
1514: where `AppType` is a Fortran derived type. Or the argument can be a `PetscObject`.
1516: Developer Note:
1517: `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.
1518: The getAPI() code processor also uses the variable type to generate correct bindings for other languages.
1520: .seealso: [](sec_fortran_context), `PetscCtxRt`, `PetscCtxDestroyFn()`, `PeOp`, `PeNS`, `PetscInitialize()`, `DMGetApplicationContext()`,
1521: `DMSetApplicationContextDestroy()`
1522: M*/
1523: typedef void *PetscCtx;
1525: /*MC
1526: PetscCtxRt - indicates an argument that returns a pointer to a C struct (or Fortran derived type) which is generally an application context
1528: Level: developer
1530: Notes:
1531: A PETSc object (in C or Fortran) can be used as a PETSc context
1533: This should not be used for functions that return pointers to arrays of unknown type. Thus it is used for, for example,
1534: `KSPGetApplicationContext()` but not used for `DMNetworkGetComponent()`
1536: A PETSc object (in C or Fortran) can be used as a PETSc context
1538: It is also used for functions that destroy an application context. For example, the destroy function passed to `DMSetApplicationContextDestroy()`
1539: which has a prototype of `PetscCtxDestroyFn()`
1541: This typedef is not part of the PETSc public API and should only be used in PETSc source code.
1543: For pointers to arrays of unknown type and for functions that return PETSc internal objects that are opaque to users, such
1544: as `KSPMonitorDynamicToleranceCreate()` a `void **` should be used.
1546: Fortran Notes:
1547: A Fortran code that calls a function with a `PetscCtxRt` argument must declare the variable `ctx` with
1548: .vb
1549: type(AppType), pointer :: ctx
1550: .ve
1551: where `AppType` is a Fortran derived type.
1553: If one passes a PETSc function with a `PetscCtxRt` argument as an argument in Fortran one must use the function named suffixed with `Cptr`,
1554: for example `KSPConvergedDefaultDestroyCptr`, see src/ksp/ksp/tutorials/ex1f.F90.
1556: Developer Notes:
1557: C++ compilers generate a warning or error if one passes a pointer to a pointer to a specific type (instead of `void`), for example,
1558: .vb
1559: extern calledfunction(void **);
1560: SomeCtx *ctx;
1561: calledfunction(&ctx); << warning that it is passing a pointer to a pointer to a SomeCtx instead of a void **
1562: .ve
1563: By using the common practice of prototyping the function as
1564: .vb
1565: extern calledfunction(void *);
1566: .ve
1567: the warning message is averted.
1569: `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.
1570: The getAPI() code processor also uses the variable type to generate correct bindings for other languages.
1572: The Fortran C stub and Fortran interface definition generated for functions with a `PetscCtxRt` argument are the C function name suffixed with
1573: `Cptr`, for example `KSPConvergedDefaultDestroyCptr`. The Fortran user API is a macro with the original C function name, for example,
1574: `KSPConvergedDefaultDestroy` that calls the `KSPConvergedDefaultDestroyCptr` version and then calls `c_f_pointer()` to handle the equivalent of a `void**` cast
1575: to the users Fortran derived type argument.
1577: .seealso: [](sec_fortran_context), `PetscCtx`, `PetscCtxDestroyFn()`, `PeOp`, `PeNS`, `PetscInitialize()`, `DMGetApplicationContext()`,
1578: `DMSetApplicationContextDestroy()`
1579: M*/
1580: typedef void *PetscCtxRt;
1582: /*S
1583: PetscCtxDestroyFn - A prototype of a `PetscErrorCode (*)(PetscCtxRt)` function that is used to free application contexts
1585: Level: intermediate
1587: Notes:
1588: Used in the prototype of functions such as `DMSetApplicationContextDestroy()`
1590: The function argument is a `PetscCtxRt` which is psychologically equivalent to a `void **` meaning that this function is called with a pointer to
1591: the application context (which is itself a pointer) thus the destroy implementation must first reference the context via, for example,
1592: `*(AppCtx **)arg`. Note that syntactically `PetscCtxRt` is defined as a `void *`, this is because C++ does
1593: not accept passing a pointer to a pointer to a `void**` but it does accept passing a pointer to a pointer to `void *`.
1595: 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
1596: appropriate, preventing accidental later use of a dangling pointer.
1598: .seealso: `PetscObject`, `PetscCtxDestroyDefault()`, `PetscObjectDestroy()`, `DMSetApplicationContextDestroy()`
1599: S*/
1600: PETSC_EXTERN_TYPEDEF typedef PetscErrorCode PetscCtxDestroyFn(PetscCtxRt ctx);
1602: PETSC_EXTERN PetscCtxDestroyFn PetscCtxDestroyDefault;
1603: PETSC_DEPRECATED_FUNCTION(3, 23, 0, "PetscCtxDestroyDefault()", ) static inline PetscErrorCode PetscContainerCtxDestroyDefault(PetscCtxRt a)
1604: {
1605: return PetscCtxDestroyDefault(a);
1606: }
1608: PETSC_EXTERN PetscErrorCode PetscMonitorCompare(PetscErrorCode (*)(void), void *, PetscCtxDestroyFn *, PetscErrorCode (*)(void), void *, PetscCtxDestroyFn *, PetscBool *);
1610: #include <petscviewertypes.h>
1611: #include <petscoptions.h>
1613: PETSC_EXTERN PetscErrorCode PetscMallocTraceSet(PetscViewer, PetscBool, PetscLogDouble);
1614: PETSC_EXTERN PetscErrorCode PetscMallocTraceGet(PetscBool *);
1616: PETSC_EXTERN PetscErrorCode PetscObjectsListGetGlobalNumbering(MPI_Comm, PetscInt, PetscObject[], PetscInt *, PetscInt *);
1618: PETSC_EXTERN PetscErrorCode PetscMemoryView(PetscViewer, const char[]);
1619: PETSC_EXTERN PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject, PetscViewer);
1620: PETSC_EXTERN PetscErrorCode PetscObjectView(PetscObject, PetscViewer);
1621: /*MC
1622: PetscObjectQueryFunction - Retrieve a function pointer that was previously composed onto a `PetscObject` with `PetscObjectComposeFunction()`
1624: Synopsis:
1625: #include <petscsys.h>
1626: PetscErrorCode PetscObjectQueryFunction(PetscObject obj, const char name[], void (**fptr)(void))
1628: Logically Collective
1630: Input Parameters:
1631: + obj - the `PetscObject`
1632: - name - the string under which the function was composed
1634: Output Parameter:
1635: . fptr - the address into which the function pointer is written; set to `NULL` if no function of this name is composed onto `obj`
1637: Level: advanced
1639: Note:
1640: The macro casts `fptr` to the internal `PetscErrorCodeFn **` representation before calling `PetscObjectQueryFunction_Private()`
1641: so that any function-pointer type can be supplied without an explicit cast.
1643: .seealso: `PetscObjectComposeFunction()`, `PetscObjectHasFunction()`, `PetscObject`
1644: M*/
1645: #define PetscObjectQueryFunction(obj, name, fptr) PetscObjectQueryFunction_Private((obj), (name), (PetscErrorCodeFn **)(fptr))
1646: PETSC_EXTERN PetscErrorCode PetscObjectHasFunction(PetscObject, const char[], PetscBool *);
1647: PETSC_EXTERN PetscErrorCode PetscObjectQueryFunction_Private(PetscObject, const char[], PetscErrorCodeFn **);
1648: PETSC_EXTERN PetscErrorCode PetscObjectSetOptionsPrefix(PetscObject, const char[]);
1649: PETSC_EXTERN PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject, const char[]);
1650: PETSC_EXTERN PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject, const char[]);
1651: PETSC_EXTERN PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject, const char *[]);
1652: PETSC_EXTERN PetscErrorCode PetscObjectChangeTypeName(PetscObject, const char[]);
1653: PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroy(PetscObject);
1654: PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroyAll(void);
1655: PETSC_EXTERN PetscErrorCode PetscObjectViewFromOptions(PetscObject, PetscObject, const char[]);
1656: PETSC_EXTERN PetscErrorCode PetscObjectViewSynchronizedFromOptions(PetscObject, PetscObject, const char[]);
1657: PETSC_EXTERN PetscErrorCode PetscObjectName(PetscObject);
1658: PETSC_EXTERN PetscErrorCode PetscObjectTypeCompare(PetscObject, const char[], PetscBool *);
1659: PETSC_EXTERN PetscErrorCode PetscObjectObjectTypeCompare(PetscObject, PetscObject, PetscBool *);
1660: PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompare(PetscObject, const char[], PetscBool *);
1661: PETSC_EXTERN PetscErrorCode PetscObjectTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1662: PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject, PetscBool *, const char[], ...);
1663: PETSC_EXTERN PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*)(void));
1664: PETSC_EXTERN PetscErrorCode PetscRegisterFinalizeAll(void);
1666: #if PetscDefined(HAVE_SAWS)
1667: PETSC_EXTERN PetscErrorCode PetscSAWsBlock(void);
1668: PETSC_EXTERN PetscErrorCode PetscObjectSAWsViewOff(PetscObject);
1669: PETSC_EXTERN PetscErrorCode PetscObjectSAWsSetBlock(PetscObject, PetscBool);
1670: PETSC_EXTERN PetscErrorCode PetscObjectSAWsBlock(PetscObject);
1671: PETSC_EXTERN PetscErrorCode PetscObjectSAWsGrantAccess(PetscObject);
1672: PETSC_EXTERN PetscErrorCode PetscObjectSAWsTakeAccess(PetscObject);
1673: PETSC_EXTERN void PetscStackSAWsGrantAccess(void);
1674: PETSC_EXTERN void PetscStackSAWsTakeAccess(void);
1675: PETSC_EXTERN PetscErrorCode PetscStackViewSAWs(void);
1676: PETSC_EXTERN PetscErrorCode PetscStackSAWsViewOff(void);
1678: #else
1679: #define PetscSAWsBlock() PETSC_SUCCESS
1680: #define PetscObjectSAWsViewOff(obj) PETSC_SUCCESS
1681: #define PetscObjectSAWsSetBlock(obj, flg) PETSC_SUCCESS
1682: #define PetscObjectSAWsBlock(obj) PETSC_SUCCESS
1683: #define PetscObjectSAWsGrantAccess(obj) PETSC_SUCCESS
1684: #define PetscObjectSAWsTakeAccess(obj) PETSC_SUCCESS
1685: #define PetscStackViewSAWs() PETSC_SUCCESS
1686: #define PetscStackSAWsViewOff() PETSC_SUCCESS
1687: #define PetscStackSAWsTakeAccess()
1688: #define PetscStackSAWsGrantAccess()
1690: #endif
1692: PETSC_EXTERN PetscErrorCode PetscDLOpen(const char[], PetscDLMode, PetscDLHandle *);
1693: PETSC_EXTERN PetscErrorCode PetscDLClose(PetscDLHandle *);
1694: PETSC_EXTERN PetscErrorCode PetscDLSym(PetscDLHandle, const char[], void **);
1695: PETSC_EXTERN PetscErrorCode PetscDLAddr(PetscVoidFn *, char *[]);
1696: PETSC_EXTERN PetscErrorCode PetscDemangleSymbol(const char *, char *[]);
1698: PETSC_EXTERN PetscErrorCode PetscMallocGetStack(void *, PetscStack **);
1700: PETSC_EXTERN PetscErrorCode PetscObjectsDump(FILE *, PetscBool);
1701: PETSC_EXTERN PetscErrorCode PetscObjectsView(PetscViewer);
1702: PETSC_EXTERN PetscErrorCode PetscObjectsGetObject(const char *, PetscObject *, const char *[]);
1703: PETSC_EXTERN PetscErrorCode PetscObjectListDestroy(PetscObjectList *);
1704: PETSC_EXTERN PetscErrorCode PetscObjectListFind(PetscObjectList, const char[], PetscObject *);
1705: PETSC_EXTERN PetscErrorCode PetscObjectListReverseFind(PetscObjectList, PetscObject, const char *[], PetscBool *);
1706: PETSC_EXTERN PetscErrorCode PetscObjectListAdd(PetscObjectList *, const char[], PetscObject);
1707: PETSC_EXTERN PetscErrorCode PetscObjectListRemoveReference(PetscObjectList *, const char[]);
1708: PETSC_EXTERN PetscErrorCode PetscObjectListDuplicate(PetscObjectList, PetscObjectList *);
1710: /*
1711: Dynamic library lists. Lists of names of routines in objects or in dynamic
1712: link libraries that will be loaded as needed.
1713: */
1715: /*MC
1716: PetscFunctionListAdd - Add a function pointer to a `PetscFunctionList`, under a string name
1718: Synopsis:
1719: #include <petscsys.h>
1720: PetscErrorCode PetscFunctionListAdd(PetscFunctionList *list, const char name[], void (*fptr)(void))
1722: Not Collective
1724: Input Parameters:
1725: + list - the function list; if `*list` is `NULL`, a new list is created
1726: . name - string under which to register the function
1727: - fptr - the function pointer; if `NULL`, any previous registration under `name` is removed
1729: Level: developer
1731: Notes:
1732: 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.
1734: 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.
1736: .seealso: `PetscFunctionList`, `PetscFunctionListFind()`, `PetscFunctionListDestroy()`, `PetscFunctionListDuplicate()`, `PetscFunctionListView()`, `PetscObjectComposeFunction()`,
1737: `SNESRegister()`, `KSPRegister()`, `PCRegister()`, `TSRegister()`
1738: M*/
1739: #define PetscFunctionListAdd(list, name, fptr) PetscFunctionListAdd_Private((list), (name), (PetscErrorCodeFn *)(fptr))
1740: PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList *, const char[], PetscErrorCodeFn *);
1741: PETSC_EXTERN PetscErrorCode PetscFunctionListDestroy(PetscFunctionList *);
1742: PETSC_EXTERN PetscErrorCode PetscFunctionListClear(PetscFunctionList);
1743: /*MC
1744: PetscFunctionListFind - Look up a function pointer in a `PetscFunctionList` by its registered string name
1746: Synopsis:
1747: #include <petscsys.h>
1748: PetscErrorCode PetscFunctionListFind(PetscFunctionList list, const char name[], void (**fptr)(void))
1750: Not Collective; No Fortran Support
1752: Input Parameters:
1753: + list - the function list
1754: - name - string under which the function was registered with `PetscFunctionListAdd()`
1756: Output Parameter:
1757: . fptr - the address into which the function pointer is written; set to `NULL` if no function of this name is registered
1759: Level: developer
1761: Note:
1762: 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.
1764: .seealso: `PetscFunctionList`, `PetscFunctionListAdd()`, `PetscFunctionListDestroy()`, `PetscObjectQueryFunction()`
1765: M*/
1766: #define PetscFunctionListFind(list, name, fptr) PetscFunctionListFind_Private((list), (name), (PetscErrorCodeFn **)(fptr))
1767: PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList, const char[], PetscErrorCodeFn **);
1768: PETSC_EXTERN PetscErrorCode PetscFunctionListPrintTypes(MPI_Comm, FILE *, const char[], const char[], const char[], const char[], PetscFunctionList, const char[], const char[]);
1769: PETSC_EXTERN PetscErrorCode PetscFunctionListDuplicate(PetscFunctionList, PetscFunctionList *);
1770: PETSC_EXTERN PetscErrorCode PetscFunctionListView(PetscFunctionList, PetscViewer);
1771: PETSC_EXTERN PetscErrorCode PetscFunctionListGet(PetscFunctionList, const char ***, int *);
1772: PETSC_EXTERN PetscErrorCode PetscFunctionListPrintNonEmpty(PetscFunctionList);
1773: PETSC_EXTERN PetscErrorCode PetscFunctionListPrintAll(void);
1775: PETSC_EXTERN PetscDLLibrary PetscDLLibrariesLoaded;
1776: PETSC_EXTERN PetscErrorCode PetscDLLibraryAppend(MPI_Comm, PetscDLLibrary *, const char[]);
1777: PETSC_EXTERN PetscErrorCode PetscDLLibraryPrepend(MPI_Comm, PetscDLLibrary *, const char[]);
1778: PETSC_EXTERN PetscErrorCode PetscDLLibrarySym(MPI_Comm, PetscDLLibrary *, const char[], const char[], void **);
1779: PETSC_EXTERN PetscErrorCode PetscDLLibraryPrintPath(PetscDLLibrary);
1780: PETSC_EXTERN PetscErrorCode PetscDLLibraryRetrieve(MPI_Comm, const char[], char *, size_t, PetscBool *);
1781: PETSC_EXTERN PetscErrorCode PetscDLLibraryOpen(MPI_Comm, const char[], PetscDLLibrary *);
1782: PETSC_EXTERN PetscErrorCode PetscDLLibraryClose(PetscDLLibrary);
1784: /*
1785: Useful utility routines
1786: */
1787: PETSC_EXTERN PetscErrorCode PetscSplitOwnership(MPI_Comm, PetscInt *, PetscInt *);
1788: PETSC_EXTERN PetscErrorCode PetscSplitOwnershipBlock(MPI_Comm, PetscInt, PetscInt *, PetscInt *);
1789: PETSC_EXTERN PetscErrorCode PetscSplitOwnershipEqual(MPI_Comm, PetscInt *, PetscInt *);
1790: PETSC_EXTERN PetscErrorCode PetscSequentialPhaseBegin(MPI_Comm, PetscMPIInt);
1791: PETSC_EXTERN PetscErrorCode PetscSequentialPhaseEnd(MPI_Comm, PetscMPIInt);
1792: PETSC_EXTERN PetscErrorCode PetscBarrier(PetscObject);
1793: PETSC_EXTERN PetscErrorCode PetscMPIDump(FILE *);
1794: PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxInt(MPI_Comm, const PetscInt[2], PetscInt[2]);
1795: PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxReal(MPI_Comm, const PetscReal[2], PetscReal[2]);
1797: /*MC
1798: PetscNot - negates a logical type value and returns result as a `PetscBool`
1800: Level: beginner
1802: Note:
1803: This is useful in cases like
1804: .vb
1805: int *a;
1806: PetscBool flag = PetscNot(a)
1807: .ve
1808: where !a would not return a `PetscBool` because we cannot provide a cast from int to `PetscBool` in C.
1810: .seealso: `PetscBool`, `PETSC_TRUE`, `PETSC_FALSE`
1811: M*/
1812: #define PetscNot(a) ((a) ? PETSC_FALSE : PETSC_TRUE)
1814: /*MC
1815: PetscHelpPrintf - Prints help messages.
1817: Synopsis:
1818: #include <petscsys.h>
1819: PetscErrorCode (*PetscHelpPrintf)(MPI_Comm comm, const char format[], ...);
1821: Not Collective, only applies on MPI rank 0; No Fortran Support
1823: Input Parameters:
1824: + comm - the MPI communicator over which the help message is printed
1825: . format - the usual printf() format string
1826: - args - arguments to be printed
1828: Level: developer
1830: Notes:
1831: You can change how help messages are printed by replacing the function pointer with a function that does not simply write to stdout.
1833: To use, write your own function, for example,
1834: .vb
1835: PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....)
1836: {
1837: PetscFunctionReturn(PETSC_SUCCESS);
1838: }
1839: .ve
1840: then do the assignment
1841: .vb
1842: PetscHelpPrintf = mypetschelpprintf;
1843: .ve
1845: You can do the assignment before `PetscInitialize()`.
1847: The default routine used is called `PetscHelpPrintfDefault()`.
1849: .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscErrorPrintf()`, `PetscHelpPrintfDefault()`
1850: M*/
1851: PETSC_EXTERN PetscErrorCode (*PetscHelpPrintf)(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1853: /*
1854: Defines PETSc profiling.
1855: */
1856: #include <petsclog.h>
1858: /*
1859: Simple PETSc parallel IO for ASCII printing
1860: */
1861: PETSC_EXTERN PetscErrorCode PetscFixFilename(const char[], char[]);
1862: PETSC_EXTERN PetscErrorCode PetscFOpen(MPI_Comm, const char[], const char[], FILE **);
1863: PETSC_EXTERN PetscErrorCode PetscFClose(MPI_Comm, FILE *);
1864: PETSC_EXTERN PetscErrorCode PetscFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
1865: PETSC_EXTERN PetscErrorCode PetscFFlush(FILE *);
1866: PETSC_EXTERN PetscErrorCode PetscPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1867: PETSC_EXTERN PetscErrorCode PetscSNPrintf(char *, size_t, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
1868: PETSC_EXTERN PetscErrorCode PetscSNPrintfCount(char *, size_t, const char[], size_t *, ...) PETSC_ATTRIBUTE_FORMAT(3, 5);
1869: PETSC_EXTERN PetscErrorCode PetscFormatRealArray(char[], size_t, const char *, PetscInt, const PetscReal[]);
1871: PETSC_EXTERN PetscErrorCode PetscErrorPrintfDefault(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
1872: PETSC_EXTERN PetscErrorCode PetscErrorPrintfNone(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
1873: PETSC_EXTERN PetscErrorCode PetscHelpPrintfDefault(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1875: PETSC_EXTERN PetscErrorCode PetscFormatConvertGetSize(const char *, size_t *);
1876: PETSC_EXTERN PetscErrorCode PetscFormatConvert(const char *, char *);
1878: PETSC_EXTERN PetscErrorCode PetscPOpen(MPI_Comm, const char[], const char[], const char[], FILE **);
1879: PETSC_EXTERN PetscErrorCode PetscPClose(MPI_Comm, FILE *);
1880: PETSC_EXTERN PetscErrorCode PetscPOpenSetMachine(const char[]);
1882: PETSC_EXTERN PetscErrorCode PetscSynchronizedPrintf(MPI_Comm, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
1883: PETSC_EXTERN PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm, FILE *, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
1884: PETSC_EXTERN PetscErrorCode PetscSynchronizedFlush(MPI_Comm, FILE *);
1885: PETSC_EXTERN PetscErrorCode PetscSynchronizedFGets(MPI_Comm, FILE *, size_t, char[]);
1886: PETSC_EXTERN PetscErrorCode PetscStartMatlab(MPI_Comm, const char[], const char[], FILE **);
1887: PETSC_EXTERN PetscErrorCode PetscGetPetscDir(const char *[]);
1889: PETSC_EXTERN PetscClassId PETSC_CONTAINER_CLASSID;
1890: PETSC_EXTERN PetscErrorCode PetscContainerGetPointer(PetscContainer, void *);
1891: PETSC_EXTERN PetscErrorCode PetscContainerSetPointer(PetscContainer, void *);
1892: PETSC_EXTERN PetscErrorCode PetscContainerDestroy(PetscContainer *);
1893: PETSC_EXTERN PetscErrorCode PetscContainerCreate(MPI_Comm, PetscContainer *);
1894: PETSC_EXTERN PetscErrorCode PetscContainerSetCtxDestroy(PetscContainer, PetscCtxDestroyFn *);
1895: PETSC_EXTERN PETSC_DEPRECATED_FUNCTION(3, 23, 0, "PetscContainerSetCtxDestroy()", ) PetscErrorCode PetscContainerSetUserDestroy(PetscContainer, PetscErrorCode (*)(void *));
1896: PETSC_EXTERN PetscErrorCode PetscObjectContainerCompose(PetscObject, const char *name, void *, PetscCtxDestroyFn *);
1897: PETSC_EXTERN PetscErrorCode PetscObjectContainerQuery(PetscObject, const char *, PetscCtxRt);
1899: /*
1900: For use in debuggers
1901: */
1902: PETSC_EXTERN PetscMPIInt PetscGlobalRank;
1903: PETSC_EXTERN PetscMPIInt PetscGlobalSize;
1904: PETSC_EXTERN PetscErrorCode PetscIntViewNumColumns(PetscInt, PetscInt, const PetscInt[], PetscViewer);
1905: PETSC_EXTERN PetscErrorCode PetscRealViewNumColumns(PetscInt, PetscInt, const PetscReal[], PetscViewer);
1906: PETSC_EXTERN PetscErrorCode PetscScalarViewNumColumns(PetscInt, PetscInt, const PetscScalar[], PetscViewer);
1907: PETSC_EXTERN PetscErrorCode PetscIntView(PetscInt, const PetscInt[], PetscViewer);
1908: PETSC_EXTERN PetscErrorCode PetscIntCSRView(PetscInt, const PetscInt[], const PetscInt[], PetscViewer);
1909: PETSC_EXTERN PetscErrorCode PetscRealView(PetscInt, const PetscReal[], PetscViewer);
1910: PETSC_EXTERN PetscErrorCode PetscScalarView(PetscInt, const PetscScalar[], PetscViewer);
1912: /*
1913: Basic memory and string operations. These are usually simple wrappers
1914: around the basic Unix system calls, but a few of them have additional
1915: functionality and/or error checking.
1916: */
1917: #include <petscstring.h>
1919: #include <stddef.h>
1920: #include <stdlib.h>
1922: #if PetscDefined(CLANG_STATIC_ANALYZER)
1923: #define PetscPrefetchBlock(a, b, c, d)
1924: #else
1925: /*MC
1926: PetscPrefetchBlock - Prefetches a block of memory
1928: Synopsis:
1929: #include <petscsys.h>
1930: void PetscPrefetchBlock(const anytype *a, size_t n, int rw, int t)
1932: Not Collective
1934: Input Parameters:
1935: + a - pointer to first element to fetch (any type but usually `PetscInt` or `PetscScalar`)
1936: . n - number of elements to fetch
1937: . rw - 1 if the memory will be written to, otherwise 0 (ignored by many processors)
1938: - t - temporal locality (PETSC_PREFETCH_HINT_{NTA,T0,T1,T2}), see note
1940: Level: developer
1942: Notes:
1943: The last two arguments (`rw` and `t`) must be compile-time constants.
1945: Adopting Intel's x86/x86-64 conventions, there are four levels of temporal locality. Not all architectures offer
1946: equivalent locality hints, but the following macros are always defined to their closest analogue.
1947: + `PETSC_PREFETCH_HINT_NTA` - Non-temporal. Prefetches directly to L1, evicts to memory (skips higher level cache unless it was already there when prefetched).
1948: . `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.
1949: . `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.
1950: - `PETSC_PREFETCH_HINT_T2` - Fetch to high-level cache only.
1952: This function does nothing on architectures that do not support prefetch and never errors (even if passed an invalid
1953: address).
1955: .seealso: `PETSC_PREFETCH_HINT_NTA`, `PETSC_PREFETCH_HINT_T0`, `PETSC_PREFETCH_HINT_T1`, `PETSC_PREFETCH_HINT_T2`
1956: M*/
1957: #define PetscPrefetchBlock(a, n, rw, t) \
1958: do { \
1959: const char *_p = (const char *)(a), *_end = (const char *)((a) + (n)); \
1960: for (; _p < _end; _p += PETSC_LEVEL1_DCACHE_LINESIZE) PETSC_Prefetch(_p, (rw), (t)); \
1961: } while (0)
1962: #endif
1963: /*
1964: Determine if some of the kernel computation routines use
1965: Fortran (rather than C) for the numerical calculations. On some machines
1966: and compilers (like complex numbers) the Fortran version of the routines
1967: is faster than the C/C++ versions. The flag --with-fortran-kernels
1968: should be used with ./configure to turn these on.
1969: */
1970: #if PetscDefined(USE_FORTRAN_KERNELS)
1972: #if !PetscDefined(USE_FORTRAN_KERNEL_MULTCRL)
1973: #define PETSC_USE_FORTRAN_KERNEL_MULTCRL
1974: #endif
1976: #if !PetscDefined(USE_FORTRAN_KERNEL_MULTAIJ)
1977: #define PETSC_USE_FORTRAN_KERNEL_MULTAIJ
1978: #endif
1980: #if !PetscDefined(USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1981: #define PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ
1982: #endif
1984: #if !PetscDefined(USE_FORTRAN_KERNEL_MAXPY)
1985: #define PETSC_USE_FORTRAN_KERNEL_MAXPY
1986: #endif
1988: #if !PetscDefined(USE_FORTRAN_KERNEL_SOLVEAIJ)
1989: #define PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ
1990: #endif
1992: #if !PetscDefined(USE_FORTRAN_KERNEL_SOLVEBAIJ)
1993: #define PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ
1994: #endif
1996: #if !PetscDefined(USE_FORTRAN_KERNEL_MULTADDAIJ)
1997: #define PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ
1998: #endif
2000: #if !PetscDefined(USE_FORTRAN_KERNEL_MDOT)
2001: #define PETSC_USE_FORTRAN_KERNEL_MDOT
2002: #endif
2004: #if !PetscDefined(USE_FORTRAN_KERNEL_XTIMESY)
2005: #define PETSC_USE_FORTRAN_KERNEL_XTIMESY
2006: #endif
2008: #if !PetscDefined(USE_FORTRAN_KERNEL_AYPX)
2009: #define PETSC_USE_FORTRAN_KERNEL_AYPX
2010: #endif
2012: #if !PetscDefined(USE_FORTRAN_KERNEL_WAXPY)
2013: #define PETSC_USE_FORTRAN_KERNEL_WAXPY
2014: #endif
2016: #endif
2018: /*
2019: Macros for indicating code that should be compiled with a C interface,
2020: rather than a C++ interface. Any routines that are dynamically loaded
2021: (such as the PCCreate_XXX() routines) must be wrapped so that the name
2022: mangler does not change the functions symbol name. This just hides the
2023: ugly extern "C" {} wrappers.
2024: */
2025: #if defined(__cplusplus)
2026: #define EXTERN_C_BEGIN extern "C" {
2027: #define EXTERN_C_END }
2028: #else
2029: #define EXTERN_C_BEGIN
2030: #define EXTERN_C_END
2031: #endif
2033: /*MC
2034: MPI_Comm - the basic object used by MPI to determine which processes are involved in a
2035: communication
2037: Level: beginner
2039: Note:
2040: This manual page is a place-holder because MPICH does not have a manual page for `MPI_Comm`
2042: .seealso: `PETSC_COMM_WORLD`, `PETSC_COMM_SELF`
2043: M*/
2045: #if PetscDefined(HAVE_MPIIO)
2046: PETSC_EXTERN PetscErrorCode MPIU_File_write_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(2, 4);
2047: PETSC_EXTERN PetscErrorCode MPIU_File_read_all(MPI_File, void *, PetscMPIInt, MPI_Datatype, MPI_Status *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(2, 4);
2048: 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);
2049: 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);
2050: 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);
2051: 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);
2052: #endif
2054: /*MC
2055: MPIU_Count - PETSc datatype used to hold message-size counts for large MPI messages
2057: Level: intermediate
2059: Note:
2060: 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.
2062: .seealso: `MPI_Count`, `MPIU_COUNT`, `MPIU_Send()`, `MPIU_Recv()`, `MPIU_Reduce()`, `PetscInt64`
2063: M*/
2064: #if PetscDefined(HAVE_MPI_COUNT)
2065: typedef MPI_Count MPIU_Count;
2066: #else
2067: typedef PetscInt64 MPIU_Count;
2068: #endif
2070: /*@
2071: PetscIntCast - casts a `MPI_Count`, `PetscInt64`, `PetscCount`, or `size_t` to a `PetscInt` (which may be 32-bits in size), generates an
2072: error if the `PetscInt` is not large enough to hold the number.
2074: Not Collective; No Fortran Support
2076: Input Parameter:
2077: . a - the `PetscInt64` value
2079: Output Parameter:
2080: . b - the resulting `PetscInt` value, or `NULL` if the result is not needed
2082: Level: advanced
2084: Note:
2085: 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
2087: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscCIntCast()`, `PetscIntMultError()`, `PetscIntSumError()`
2088: @*/
2089: static inline PetscErrorCode PetscIntCast(MPIU_Count a, PetscInt *b)
2090: {
2091: PetscFunctionBegin;
2092: if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2093: 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);
2094: if (b) *b = (PetscInt)a;
2095: PetscFunctionReturn(PETSC_SUCCESS);
2096: }
2098: /*@
2099: PetscBLASIntCast - casts a `MPI_Count`, `PetscInt`, `PetscCount` or `PetscInt64` to a `PetscBLASInt` (which may be 32-bits in size), generates an
2100: error if the `PetscBLASInt` is not large enough to hold the number.
2102: Not Collective; No Fortran Support
2104: Input Parameter:
2105: . a - the `PetscInt` value
2107: Output Parameter:
2108: . b - the resulting `PetscBLASInt` value, or `NULL` if the result is not needed
2110: Level: advanced
2112: Note:
2113: Errors if the integer is negative since PETSc calls to BLAS/LAPACK never need to cast negative integer inputs
2115: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscCIntCast()`, `PetscIntCast()`
2116: @*/
2117: static inline PetscErrorCode PetscBLASIntCast(MPIU_Count a, PetscBLASInt *b)
2118: {
2119: PetscFunctionBegin;
2120: if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2121: 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);
2122: PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer to BLAS/LAPACK routine");
2123: if (b) *b = (PetscBLASInt)a;
2124: PetscFunctionReturn(PETSC_SUCCESS);
2125: }
2127: /*@
2128: PetscCuBLASIntCast - like `PetscBLASIntCast()`, but for `PetscCuBLASInt`.
2130: Not Collective; No Fortran Support
2132: Input Parameter:
2133: . a - the `PetscInt` value
2135: Output Parameter:
2136: . b - the resulting `PetscCuBLASInt` value, or `NULL` if the result is not needed
2138: Level: advanced
2140: Note:
2141: Errors if the integer is negative since PETSc calls to cuBLAS and friends never need to cast negative integer inputs
2143: .seealso: `PetscCuBLASInt`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscMPIIntCast()`, `PetscCIntCast()`, `PetscIntCast()`
2144: @*/
2145: static inline PetscErrorCode PetscCuBLASIntCast(MPIU_Count a, PetscCuBLASInt *b)
2146: {
2147: PetscFunctionBegin;
2148: if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2149: 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);
2150: PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer %" PetscInt64_FMT "to cuBLAS routine", (PetscInt64)a);
2151: if (b) *b = (PetscCuBLASInt)a;
2152: PetscFunctionReturn(PETSC_SUCCESS);
2153: }
2155: /*@
2156: PetscHipBLASIntCast - like `PetscBLASIntCast()`, but for `PetscHipBLASInt`.
2158: Not Collective; No Fortran Support
2160: Input Parameter:
2161: . a - the `PetscInt` value
2163: Output Parameter:
2164: . b - the resulting `PetscHipBLASInt` value, or `NULL` if the result is not needed
2166: Level: advanced
2168: Note:
2169: Errors if the integer is negative since PETSc calls to hipBLAS and friends never need to cast negative integer inputs
2171: .seealso: `PetscHipBLASInt`, `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscMPIIntCast()`, `PetscCIntCast()`, `PetscIntCast()`
2172: @*/
2173: static inline PetscErrorCode PetscHipBLASIntCast(MPIU_Count a, PetscHipBLASInt *b)
2174: {
2175: PetscFunctionBegin;
2176: if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2177: 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);
2178: PetscCheck(a >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Passing negative integer %" PetscInt64_FMT "to hipBLAS routine", (PetscInt64)a);
2179: if (b) *b = (PetscHipBLASInt)a;
2180: PetscFunctionReturn(PETSC_SUCCESS);
2181: }
2183: /*@
2184: PetscMPIIntCast - casts a `MPI_Count`, `PetscInt`, `PetscCount`, or `PetscInt64` to a `PetscMPIInt` (which is always 32-bits in size), generates an
2185: error if the `PetscMPIInt` is not large enough to hold the number.
2187: Not Collective; No Fortran Support
2189: Input Parameter:
2190: . a - the `PetscInt` value
2192: Output Parameter:
2193: . b - the resulting `PetscMPIInt` value, or `NULL` if the result is not needed
2195: Level: advanced
2197: .seealso: [](stylePetscCount), `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscIntCast()`
2198: @*/
2199: static inline PetscErrorCode PetscMPIIntCast(MPIU_Count a, PetscMPIInt *b)
2200: {
2201: PetscFunctionBegin;
2202: if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2203: 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);
2204: if (b) *b = (PetscMPIInt)a;
2205: PetscFunctionReturn(PETSC_SUCCESS);
2206: }
2208: /*@
2209: 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.
2211: Not Collective; No Fortran Support
2213: Input Parameter:
2214: . a - the `PetscInt` value
2216: Output Parameter:
2217: . b - the resulting `int` value, or `NULL` if the result is not needed
2219: Level: advanced
2221: .seealso: [](stylePetscCount), `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscMPIIntCast()`, `PetscBLASIntCast()`, `PetscIntCast()`
2222: @*/
2223: static inline PetscErrorCode PetscCIntCast(MPIU_Count a, int *b)
2224: {
2225: PetscFunctionBegin;
2226: if (b) *b = 0; /* to prevent compilers erroneously suggesting uninitialized variable */
2227: 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);
2228: if (b) *b = (int)a;
2229: PetscFunctionReturn(PETSC_SUCCESS);
2230: }
2232: /*MC
2233: PetscInt64Mult - Computes the product of two variables after casting them to `PetscInt64`.
2235: Not Collective; No Fortran Support
2237: Input Parameters:
2238: + a - the first variable
2239: - b - the second variable
2241: Level: advanced
2243: .seealso: [](stylePetscCount), `PetscIntMultError()`, `PetscIntMultTruncate()`
2244: M*/
2245: #if PetscDefined(USE_64BIT_INDICES)
2246: #define PetscInt64Mult(a, b) ((a) * (b))
2247: #else
2248: #define PetscInt64Mult(a, b) (((PetscInt64)(a)) * ((PetscInt64)(b)))
2249: #endif
2251: /*@
2252: PetscRealIntMultTruncate - Computes the product of a positive `PetscReal` and a positive
2253: `PetscInt` and truncates the value to slightly less than the maximal possible value.
2255: Not Collective; No Fortran Support
2257: Input Parameters:
2258: + a - The `PetscReal` value
2259: - b - The `PetscInt` value
2261: Level: advanced
2263: Notes:
2264: Returns the result as a `PetscInt` value.
2266: Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`.
2268: Use `PetscIntMultTruncate()` to compute the product of two positive `PetscInt` and truncate
2269: to fit a `PetscInt`.
2271: Use `PetscIntMultError()` to compute the product of two `PetscInt` if you wish to generate an
2272: error if the result will not fit in a `PetscInt`.
2274: Developer Notes:
2275: We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but
2276: requires many more checks.
2278: This is used where we compute approximate sizes for workspace and need to insure the
2279: workspace is index-able.
2281: .seealso: `PetscReal`, `PetscInt`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`
2282: @*/
2283: static inline PetscInt PetscRealIntMultTruncate(PetscReal a, PetscInt b)
2284: {
2285: PetscInt64 r = (PetscInt64)(a * (PetscReal)b);
2286: if (r > PETSC_INT_MAX - 100) r = PETSC_INT_MAX - 100;
2287: #if PetscDefined(USE_64BIT_INDICES)
2288: return r;
2289: #else
2290: return (PetscInt)r;
2291: #endif
2292: }
2294: /*@
2295: PetscIntMultTruncate - Computes the product of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value
2297: Not Collective; No Fortran Support
2299: Input Parameters:
2300: + a - the `PetscInt` value
2301: - b - the second value
2303: Returns:
2304: The result as a `PetscInt` value
2306: Level: advanced
2308: Notes:
2309: Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`
2311: Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`
2313: 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`
2315: Developer Notes:
2316: We currently assume that `PetscInt` addition can never overflow, this is obviously wrong but requires many more checks.
2318: This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
2320: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`, `PetscIntSumError()`,
2321: `PetscIntSumTruncate()`
2322: @*/
2323: static inline PetscInt PetscIntMultTruncate(PetscInt a, PetscInt b)
2324: {
2325: PetscInt64 r = PetscInt64Mult(a, b);
2326: if (r > PETSC_INT_MAX - 100) r = PETSC_INT_MAX - 100;
2327: #if PetscDefined(USE_64BIT_INDICES)
2328: return r;
2329: #else
2330: return (PetscInt)r;
2331: #endif
2332: }
2334: /*@
2335: PetscIntSumTruncate - Computes the sum of two positive `PetscInt` and truncates the value to slightly less than the maximal possible value
2337: Not Collective; No Fortran Support
2339: Input Parameters:
2340: + a - the `PetscInt` value
2341: - b - the second value
2343: Returns:
2344: The result as a `PetscInt` value
2346: Level: advanced
2348: Notes:
2349: Use `PetscInt64Mult()` to compute the product of two `PetscInt` as a `PetscInt64`
2351: Use `PetscRealIntMultTruncate()` to compute the product of a `PetscReal` and a `PetscInt` and truncate to fit a `PetscInt`
2353: 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`
2355: Developer Note:
2356: This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
2358: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
2359: @*/
2360: static inline PetscInt PetscIntSumTruncate(PetscInt a, PetscInt b)
2361: {
2362: PetscInt64 r = a;
2364: r += b;
2365: if (r > PETSC_INT_MAX - 100) r = PETSC_INT_MAX - 100;
2366: #if PetscDefined(USE_64BIT_INDICES)
2367: return r;
2368: #else
2369: return (PetscInt)r;
2370: #endif
2371: }
2373: /*@
2374: PetscIntMultError - Computes the product of two positive `PetscInt` and generates an error with overflow.
2376: Not Collective; No Fortran Support
2378: Input Parameters:
2379: + a - the `PetscInt` value
2380: - b - the second value
2382: Output Parameter:
2383: . result - the result as a `PetscInt` value, or `NULL` if you do not want the result, you just want to check if it overflows
2385: Level: advanced
2387: Notes:
2388: Use `PetscInt64Mult()` to compute the product of two `PetscInt` and store in a `PetscInt64`
2390: Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`
2392: Developer Note:
2393: 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.
2394: `PetscIntSumError()` can be used to check for this situation.
2396: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntSumError()`
2397: @*/
2398: static inline PetscErrorCode PetscIntMultError(PetscInt a, PetscInt b, PetscInt *result)
2399: {
2400: PetscInt64 r = PetscInt64Mult(a, b);
2402: PetscFunctionBegin;
2403: #if PetscDefined(USE_64BIT_INDICES)
2404: if (result) *result = r;
2405: #else
2406: if (result) *result = (PetscInt)r;
2407: #endif
2408: if (!PetscDefined(USE_64BIT_INDICES)) {
2409: 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);
2410: }
2411: PetscFunctionReturn(PETSC_SUCCESS);
2412: }
2414: /*@
2415: PetscIntSumError - Computes the sum of two positive `PetscInt` and generates an error with overflow.
2417: Not Collective; No Fortran Support
2419: Input Parameters:
2420: + a - the `PetscInt` value
2421: - b - the second value
2423: Output Parameter:
2424: . c - the result as a `PetscInt` value, or `NULL` if you do not want the result, you just want to check if it overflows
2426: Level: advanced
2428: Notes:
2429: Use `PetscInt64Mult()` to compute the product of two 32-bit `PetscInt` and store in a `PetscInt64`
2431: Use `PetscIntMultTruncate()` to compute the product of two `PetscInt` and truncate it to fit in a `PetscInt`
2433: .seealso: `PetscBLASInt`, `PetscMPIInt`, `PetscInt`, `PetscBLASIntCast()`, `PetscInt64Mult()`, `PetscIntMultError()`
2434: @*/
2435: static inline PetscErrorCode PetscIntSumError(PetscInt a, PetscInt b, PetscInt *result)
2436: {
2437: PetscInt64 r = a;
2439: PetscFunctionBegin;
2440: r += b;
2441: #if PetscDefined(USE_64BIT_INDICES)
2442: if (result) *result = r;
2443: #else
2444: if (result) *result = (PetscInt)r;
2445: #endif
2446: if (!PetscDefined(USE_64BIT_INDICES)) {
2447: 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);
2448: }
2449: PetscFunctionReturn(PETSC_SUCCESS);
2450: }
2452: /*
2453: The IBM include files define hz, here we hide it so that it may be used as a regular user variable.
2454: */
2455: #if defined(hz)
2456: #undef hz
2457: #endif
2459: #if PetscDefined(HAVE_SYS_TYPES_H)
2460: #include <sys/types.h>
2461: #endif
2463: /*MC
2465: PETSC_VERSION - This manual page provides information about how PETSc documents and uses its version information. This information is available to both C/C++
2466: and Fortran compilers when `petscsys.h` is included.
2468: The current PETSc version and the API for accessing it are defined in <A HREF="../include/petscversion.h.html">include/petscversion.html</A>
2470: The complete version number is given as the triple PETSC_VERSION_MAJOR.PETSC_VERSION_MINOR.PETSC_VERSION_SUBMINOR (in short hand x.y.z)
2472: 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
2473: where only a change in the major version number (x) indicates a change in the API.
2475: 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
2477: 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),
2478: 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
2479: version number (x.y.z).
2481: `PETSC_RELEASE_DATE` is the date the x.y version was released (i.e. the version before any patch releases)
2483: `PETSC_VERSION_DATE` is the date the x.y.z version was released
2485: `PETSC_VERSION_GIT` is the last git commit to the repository given in the form vx.y.z-wwwww
2487: `PETSC_VERSION_DATE_GIT` is the date of the last git commit to the repository
2489: `PETSC_VERSION_()` is deprecated and will eventually be removed.
2491: Level: intermediate
2492: M*/
2494: PETSC_EXTERN PetscErrorCode PetscGetArchType(char[], size_t);
2495: PETSC_EXTERN PetscErrorCode PetscGetHostName(char[], size_t);
2496: PETSC_EXTERN PetscErrorCode PetscGetUserName(char[], size_t);
2497: PETSC_EXTERN PetscErrorCode PetscGetProgramName(char[], size_t);
2498: PETSC_EXTERN PetscErrorCode PetscSetProgramName(const char[]);
2499: PETSC_EXTERN PetscErrorCode PetscGetDate(char[], size_t);
2500: PETSC_EXTERN PetscErrorCode PetscGetVersion(char[], size_t);
2501: PETSC_EXTERN PetscErrorCode PetscGetVersionNumber(PetscInt *, PetscInt *, PetscInt *, PetscInt *);
2502: PETSC_EXTERN PetscErrorCode PetscGetConfiguration(const char *[]);
2504: PETSC_EXTERN PetscErrorCode PetscSortedInt(PetscCount, const PetscInt[], PetscBool *);
2505: PETSC_EXTERN PetscErrorCode PetscSortedInt64(PetscCount, const PetscInt64[], PetscBool *);
2506: PETSC_EXTERN PetscErrorCode PetscSortedMPIInt(PetscCount, const PetscMPIInt[], PetscBool *);
2507: PETSC_EXTERN PetscErrorCode PetscSortedReal(PetscCount, const PetscReal[], PetscBool *);
2508: PETSC_EXTERN PetscErrorCode PetscSortInt(PetscCount, PetscInt[]);
2509: PETSC_EXTERN PetscErrorCode PetscSortInt64(PetscCount, PetscInt64[]);
2510: PETSC_EXTERN PetscErrorCode PetscSortCount(PetscCount, PetscCount[]);
2511: PETSC_EXTERN PetscErrorCode PetscSortReverseInt(PetscCount, PetscInt[]);
2512: PETSC_EXTERN PetscErrorCode PetscSortedRemoveDupsInt(PetscInt *, PetscInt[]);
2513: PETSC_EXTERN PetscErrorCode PetscSortedCheckDupsInt(PetscCount, const PetscInt[], PetscBool *);
2514: PETSC_EXTERN PetscErrorCode PetscSortedCheckDupsCount(PetscCount, const PetscCount[], PetscBool *);
2515: PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsInt(PetscInt *, PetscInt[]);
2516: PETSC_EXTERN PetscErrorCode PetscCheckDupsInt(PetscInt, const PetscInt[], PetscBool *);
2517: PETSC_EXTERN PetscErrorCode PetscFindInt(PetscInt, PetscCount, const PetscInt[], PetscInt *);
2518: PETSC_EXTERN PetscErrorCode PetscFindMPIInt(PetscMPIInt, PetscCount, const PetscMPIInt[], PetscInt *);
2519: PETSC_EXTERN PetscErrorCode PetscFindCount(PetscCount, PetscCount, const PetscCount[], PetscCount *);
2520: PETSC_EXTERN PetscErrorCode PetscSortIntWithPermutation(PetscInt, const PetscInt[], PetscInt[]);
2521: PETSC_EXTERN PetscErrorCode PetscSortStrWithPermutation(PetscInt, const char *[], PetscInt[]);
2522: PETSC_EXTERN PetscErrorCode PetscSortIntWithArray(PetscCount, PetscInt[], PetscInt[]);
2523: PETSC_EXTERN PetscErrorCode PetscSortIntWithCountArray(PetscCount, PetscInt[], PetscCount[]);
2524: PETSC_EXTERN PetscErrorCode PetscSortIntWithMPIIntArray(PetscCount, PetscInt[], PetscMPIInt[]);
2525: PETSC_EXTERN PetscErrorCode PetscSortIntWithArrayPair(PetscCount, PetscInt[], PetscInt[], PetscInt[]);
2526: PETSC_EXTERN PetscErrorCode PetscSortIntWithIntCountArrayPair(PetscCount, PetscInt[], PetscInt[], PetscCount[]);
2527: PETSC_EXTERN PetscErrorCode PetscSortMPIInt(PetscCount, PetscMPIInt[]);
2528: PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsMPIInt(PetscInt *, PetscMPIInt[]);
2529: PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithArray(PetscCount, PetscMPIInt[], PetscMPIInt[]);
2530: PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithIntArray(PetscCount, PetscMPIInt[], PetscInt[]);
2531: PETSC_EXTERN PetscErrorCode PetscSortIntWithScalarArray(PetscCount, PetscInt[], PetscScalar[]);
2532: PETSC_EXTERN PetscErrorCode PetscSortIntWithDataArray(PetscCount, PetscInt[], void *, size_t, void *);
2533: PETSC_EXTERN PetscErrorCode PetscSortReal(PetscCount, PetscReal[]);
2534: PETSC_EXTERN PetscErrorCode PetscSortRealWithArrayInt(PetscCount, PetscReal[], PetscInt[]);
2535: PETSC_EXTERN PetscErrorCode PetscSortRealWithPermutation(PetscInt, const PetscReal[], PetscInt[]);
2536: PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsReal(PetscInt *, PetscReal[]);
2537: PETSC_EXTERN PetscErrorCode PetscFindReal(PetscReal, PetscCount, const PetscReal[], PetscReal, PetscInt *);
2538: PETSC_EXTERN PetscErrorCode PetscSortSplit(PetscInt, PetscInt, PetscScalar[], PetscInt[]);
2539: PETSC_EXTERN PetscErrorCode PetscSortSplitReal(PetscInt, PetscInt, PetscReal[], PetscInt[]);
2540: PETSC_EXTERN PetscErrorCode PetscProcessTree(PetscInt, const PetscBool[], const PetscInt[], PetscInt *, PetscInt *[], PetscInt *[], PetscInt *[], PetscInt *[]);
2541: PETSC_EXTERN PetscErrorCode PetscMergeIntArrayPair(PetscInt, const PetscInt[], const PetscInt[], PetscInt, const PetscInt[], const PetscInt[], PetscInt *, PetscInt *[], PetscInt *[]);
2542: PETSC_EXTERN PetscErrorCode PetscMergeIntArray(PetscInt, const PetscInt[], PetscInt, const PetscInt[], PetscInt *, PetscInt *[]);
2543: PETSC_EXTERN PetscErrorCode PetscMergeMPIIntArray(PetscInt, const PetscMPIInt[], PetscInt, const PetscMPIInt[], PetscInt *, PetscMPIInt *[]);
2544: PETSC_EXTERN PetscErrorCode PetscParallelSortedInt(MPI_Comm, PetscInt, const PetscInt[], PetscBool *);
2546: PETSC_EXTERN PetscErrorCode PetscTimSort(PetscInt, void *, size_t, int (*)(const void *, const void *, void *), void *);
2547: PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrdered(PetscInt, PetscInt[]);
2548: PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrdered(PetscInt, PetscMPIInt[]);
2549: PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrdered(PetscInt, PetscReal[]);
2550: PETSC_EXTERN PetscErrorCode PetscTimSortWithArray(PetscInt, void *, size_t, void *, size_t, int (*)(const void *, const void *, void *), void *);
2551: PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrderedWithArray(PetscInt, PetscInt[], PetscInt[]);
2552: PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrderedWithArray(PetscInt, PetscMPIInt[], PetscMPIInt[]);
2553: PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrderedWithArrayInt(PetscInt, PetscReal[], PetscInt[]);
2555: PETSC_EXTERN PetscErrorCode PetscSetDisplay(void);
2556: PETSC_EXTERN PetscErrorCode PetscGetDisplay(char[], size_t);
2558: /*J
2559: PetscRandomType - String with the name of a PETSc randomizer
2561: Level: beginner
2563: Note:
2564: To use `PETSCSPRNG` or `PETSCRANDOM123` you must have ./configure PETSc
2565: with the option `--download-sprng` or `--download-random123`. We recommend the default provided with PETSc.
2567: .seealso: `PetscRandomSetType()`, `PetscRandom`, `PetscRandomCreate()`
2568: J*/
2569: typedef const char *PetscRandomType;
2570: #define PETSCRAND "rand"
2571: #define PETSCRAND48 "rand48"
2572: #define PETSCSPRNG "sprng"
2573: #define PETSCRANDER48 "rander48"
2574: #define PETSCRANDOM123 "random123"
2575: #define PETSCCURAND "curand"
2577: /* Logging support */
2578: PETSC_EXTERN PetscClassId PETSC_RANDOM_CLASSID;
2580: PETSC_EXTERN PetscErrorCode PetscRandomInitializePackage(void);
2581: PETSC_EXTERN PetscErrorCode PetscRandomFinalizePackage(void);
2583: /* Dynamic creation and loading functions */
2584: PETSC_EXTERN PetscFunctionList PetscRandomList;
2586: PETSC_EXTERN PetscErrorCode PetscRandomRegister(const char[], PetscErrorCode (*)(PetscRandom));
2587: PETSC_EXTERN PetscErrorCode PetscRandomSetType(PetscRandom, PetscRandomType);
2588: PETSC_EXTERN PetscErrorCode PetscRandomSetOptionsPrefix(PetscRandom, const char[]);
2589: PETSC_EXTERN PetscErrorCode PetscRandomAppendOptionsPrefix(PetscRandom, const char[]);
2590: PETSC_EXTERN PetscErrorCode PetscRandomGetOptionsPrefix(PetscRandom, const char *[]);
2591: PETSC_EXTERN PetscErrorCode PetscRandomSetFromOptions(PetscRandom);
2592: PETSC_EXTERN PetscErrorCode PetscRandomGetType(PetscRandom, PetscRandomType *);
2593: PETSC_EXTERN PetscErrorCode PetscRandomViewFromOptions(PetscRandom, PetscObject, const char[]);
2594: PETSC_EXTERN PetscErrorCode PetscRandomView(PetscRandom, PetscViewer);
2596: PETSC_EXTERN PetscErrorCode PetscRandomCreate(MPI_Comm, PetscRandom *);
2597: PETSC_EXTERN PetscErrorCode PetscRandomGetValue(PetscRandom, PetscScalar *);
2598: PETSC_EXTERN PetscErrorCode PetscRandomGetValueReal(PetscRandom, PetscReal *);
2599: PETSC_EXTERN PetscErrorCode PetscRandomGetValues(PetscRandom, PetscInt, PetscScalar *);
2600: PETSC_EXTERN PetscErrorCode PetscRandomGetValuesReal(PetscRandom, PetscInt, PetscReal *);
2601: PETSC_EXTERN PetscErrorCode PetscRandomGetInterval(PetscRandom, PetscScalar *, PetscScalar *);
2602: PETSC_EXTERN PetscErrorCode PetscRandomSetInterval(PetscRandom, PetscScalar, PetscScalar);
2603: PETSC_EXTERN PetscErrorCode PetscRandomSetSeed(PetscRandom, PetscInt64);
2604: PETSC_EXTERN PetscErrorCode PetscRandomGetSeed(PetscRandom, PetscInt64 *);
2605: PETSC_EXTERN PetscErrorCode PetscRandomSeed(PetscRandom);
2606: PETSC_EXTERN PetscErrorCode PetscRandomDestroy(PetscRandom *);
2608: PETSC_EXTERN PetscErrorCode PetscGetFullPath(const char[], char[], size_t);
2609: PETSC_EXTERN PetscErrorCode PetscGetRelativePath(const char[], char[], size_t);
2610: PETSC_EXTERN PetscErrorCode PetscGetWorkingDirectory(char[], size_t);
2611: PETSC_EXTERN PetscErrorCode PetscGetRealPath(const char[], char[]);
2612: PETSC_EXTERN PetscErrorCode PetscGetHomeDirectory(char[], size_t);
2613: PETSC_EXTERN PetscErrorCode PetscTestFile(const char[], char, PetscBool *);
2614: PETSC_EXTERN PetscErrorCode PetscTestDirectory(const char[], char, PetscBool *);
2615: PETSC_EXTERN PetscErrorCode PetscMkdir(const char[]);
2616: PETSC_EXTERN PetscErrorCode PetscMkdtemp(char[]);
2617: PETSC_EXTERN PetscErrorCode PetscRMTree(const char[]);
2619: /*MC
2620: PetscBinaryBigEndian - indicates if values in memory are stored with big endian format
2622: Synopsis:
2623: #include <petscsys.h>
2624: PetscBool PetscBinaryBigEndian(void);
2626: Not Collective; No Fortran Support
2628: Level: developer
2630: .seealso: `PetscInitialize()`, `PetscFinalize()`, `PetscInitializeCalled`
2631: M*/
2632: static inline PetscBool PetscBinaryBigEndian(void)
2633: {
2634: long _petsc_v = 1;
2635: return ((char *)&_petsc_v)[0] ? PETSC_FALSE : PETSC_TRUE;
2636: }
2638: PETSC_EXTERN PetscErrorCode PetscBinaryRead(int, void *, PetscCount, PetscInt *, PetscDataType);
2639: PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedRead(MPI_Comm, int, void *, PetscInt, PetscInt *, PetscDataType);
2640: PETSC_EXTERN PetscErrorCode PetscBinaryWrite(int, const void *, PetscCount, PetscDataType);
2641: PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedWrite(MPI_Comm, int, const void *, PetscInt, PetscDataType);
2642: PETSC_EXTERN PetscErrorCode PetscBinaryOpen(const char[], PetscFileMode, int *);
2643: PETSC_EXTERN PetscErrorCode PetscBinaryClose(int);
2644: PETSC_EXTERN PetscErrorCode PetscSharedTmp(MPI_Comm, PetscBool *);
2645: PETSC_EXTERN PetscErrorCode PetscSharedWorkingDirectory(MPI_Comm, PetscBool *);
2646: PETSC_EXTERN PetscErrorCode PetscGetTmp(MPI_Comm, char[], size_t);
2647: PETSC_EXTERN PetscErrorCode PetscFileRetrieve(MPI_Comm, const char[], char[], size_t, PetscBool *);
2648: PETSC_EXTERN PetscErrorCode PetscLs(MPI_Comm, const char[], char[], size_t, PetscBool *);
2649: #if PetscDefined(USE_SOCKET_VIEWER)
2650: PETSC_EXTERN PetscErrorCode PetscOpenSocket(const char[], int, int *);
2651: #endif
2653: PETSC_EXTERN PetscErrorCode PetscBinarySeek(int, off_t, PetscBinarySeekType, off_t *);
2654: PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedSeek(MPI_Comm, int, off_t, PetscBinarySeekType, off_t *);
2655: PETSC_EXTERN PetscErrorCode PetscByteSwap(void *, PetscDataType, PetscCount);
2657: PETSC_EXTERN PetscErrorCode PetscSetDebugTerminal(const char[]);
2658: PETSC_EXTERN PetscErrorCode PetscSetDebugger(const char[], PetscBool);
2659: PETSC_EXTERN PetscErrorCode PetscSetDefaultDebugger(void);
2660: PETSC_EXTERN PetscErrorCode PetscSetDebuggerFromString(const char *);
2661: PETSC_EXTERN PetscErrorCode PetscAttachDebugger(void);
2662: PETSC_EXTERN PetscErrorCode PetscStopForDebugger(void);
2663: PETSC_EXTERN PetscErrorCode PetscWaitOnError(void);
2665: PETSC_EXTERN PetscErrorCode PetscGatherNumberOfMessages(MPI_Comm, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt *);
2666: PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], PetscMPIInt *[], PetscMPIInt *[]);
2667: PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths2(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscMPIInt *[], PetscMPIInt *[], PetscMPIInt *[]);
2668: PETSC_EXTERN PetscErrorCode PetscPostIrecvInt(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscInt ***, MPI_Request **);
2669: PETSC_EXTERN PetscErrorCode PetscPostIrecvScalar(MPI_Comm, PetscMPIInt, PetscMPIInt, const PetscMPIInt[], const PetscMPIInt[], PetscScalar ***, MPI_Request **);
2670: 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);
2671: 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);
2672: 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);
2674: PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedSetType(MPI_Comm, PetscBuildTwoSidedType);
2675: PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedGetType(MPI_Comm, PetscBuildTwoSidedType *);
2677: PETSC_DEPRECATED_FUNCTION(3, 24, 0, "PetscSSEIsEnabled()", ) static inline PetscErrorCode PetscSSEIsEnabled(PETSC_UNUSED MPI_Comm comm, PetscBool *lflag, PetscBool *gflag)
2678: {
2679: if (lflag) *lflag = PETSC_FALSE;
2680: if (gflag) *gflag = PETSC_FALSE;
2681: return PETSC_SUCCESS;
2682: }
2684: PETSC_EXTERN MPI_Comm PetscObjectComm(PetscObject);
2686: struct _n_PetscSubcomm {
2687: MPI_Comm parent; /* parent communicator */
2688: MPI_Comm dupparent; /* duplicate parent communicator, under which the processors of this subcomm have contiguous rank */
2689: MPI_Comm child; /* the sub-communicator */
2690: PetscMPIInt n; /* num of subcommunicators under the parent communicator */
2691: PetscMPIInt color; /* color of processors belong to this communicator */
2692: PetscMPIInt *subsize; /* size of subcommunicator[color] */
2693: PetscSubcommType type;
2694: char *subcommprefix;
2695: };
2697: static inline MPI_Comm PetscSubcommParent(PetscSubcomm scomm)
2698: {
2699: return scomm->parent;
2700: }
2701: static inline MPI_Comm PetscSubcommChild(PetscSubcomm scomm)
2702: {
2703: return scomm->child;
2704: }
2705: static inline MPI_Comm PetscSubcommContiguousParent(PetscSubcomm scomm)
2706: {
2707: return scomm->dupparent;
2708: }
2709: PETSC_EXTERN PetscErrorCode PetscSubcommCreate(MPI_Comm, PetscSubcomm *);
2710: PETSC_EXTERN PetscErrorCode PetscSubcommDestroy(PetscSubcomm *);
2711: PETSC_EXTERN PetscErrorCode PetscSubcommSetNumber(PetscSubcomm, PetscInt);
2712: PETSC_EXTERN PetscErrorCode PetscSubcommSetType(PetscSubcomm, PetscSubcommType);
2713: PETSC_EXTERN PetscErrorCode PetscSubcommSetTypeGeneral(PetscSubcomm, PetscMPIInt, PetscMPIInt);
2714: PETSC_EXTERN PetscErrorCode PetscSubcommView(PetscSubcomm, PetscViewer);
2715: PETSC_EXTERN PetscErrorCode PetscSubcommSetFromOptions(PetscSubcomm);
2716: PETSC_EXTERN PetscErrorCode PetscSubcommSetOptionsPrefix(PetscSubcomm, const char[]);
2717: PETSC_EXTERN PetscErrorCode PetscSubcommGetParent(PetscSubcomm, MPI_Comm *);
2718: PETSC_EXTERN PetscErrorCode PetscSubcommGetContiguousParent(PetscSubcomm, MPI_Comm *);
2719: PETSC_EXTERN PetscErrorCode PetscSubcommGetChild(PetscSubcomm, MPI_Comm *);
2721: PETSC_EXTERN PetscErrorCode PetscHeapCreate(PetscInt, PetscHeap *);
2722: PETSC_EXTERN PetscErrorCode PetscHeapAdd(PetscHeap, PetscInt, PetscInt);
2723: PETSC_EXTERN PetscErrorCode PetscHeapPop(PetscHeap, PetscInt *, PetscInt *);
2724: PETSC_EXTERN PetscErrorCode PetscHeapPeek(PetscHeap, PetscInt *, PetscInt *);
2725: PETSC_EXTERN PetscErrorCode PetscHeapStash(PetscHeap, PetscInt, PetscInt);
2726: PETSC_EXTERN PetscErrorCode PetscHeapUnstash(PetscHeap);
2727: PETSC_EXTERN PetscErrorCode PetscHeapDestroy(PetscHeap *);
2728: PETSC_EXTERN PetscErrorCode PetscHeapView(PetscHeap, PetscViewer);
2730: PETSC_EXTERN PetscErrorCode PetscProcessPlacementView(PetscViewer);
2731: PETSC_EXTERN PetscErrorCode PetscShmCommGet(MPI_Comm, PetscShmComm *);
2732: PETSC_EXTERN PetscErrorCode PetscShmCommGlobalToLocal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
2733: PETSC_EXTERN PetscErrorCode PetscShmCommLocalToGlobal(PetscShmComm, PetscMPIInt, PetscMPIInt *);
2734: PETSC_EXTERN PetscErrorCode PetscShmCommGetMpiShmComm(PetscShmComm, MPI_Comm *);
2736: /* routines to better support OpenMP multithreading needs of some PETSc third party libraries */
2737: PETSC_EXTERN PetscErrorCode PetscOmpCtrlCreate(MPI_Comm, PetscInt, PetscOmpCtrl *);
2738: PETSC_EXTERN PetscErrorCode PetscOmpCtrlGetOmpComms(PetscOmpCtrl, MPI_Comm *, MPI_Comm *, PetscBool *);
2739: PETSC_EXTERN PetscErrorCode PetscOmpCtrlDestroy(PetscOmpCtrl *);
2740: PETSC_EXTERN PetscErrorCode PetscOmpCtrlBarrier(PetscOmpCtrl);
2741: PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterBegin(PetscOmpCtrl);
2742: PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterEnd(PetscOmpCtrl);
2744: PETSC_EXTERN PetscErrorCode PetscSegBufferCreate(size_t, PetscCount, PetscSegBuffer *);
2745: PETSC_EXTERN PetscErrorCode PetscSegBufferDestroy(PetscSegBuffer *);
2746: PETSC_EXTERN PetscErrorCode PetscSegBufferGet(PetscSegBuffer, PetscCount, void *);
2747: PETSC_EXTERN PetscErrorCode PetscSegBufferExtractAlloc(PetscSegBuffer, void *);
2748: PETSC_EXTERN PetscErrorCode PetscSegBufferExtractTo(PetscSegBuffer, void *);
2749: PETSC_EXTERN PetscErrorCode PetscSegBufferExtractInPlace(PetscSegBuffer, void *);
2750: PETSC_EXTERN PetscErrorCode PetscSegBufferGetSize(PetscSegBuffer, PetscCount *);
2751: PETSC_EXTERN PetscErrorCode PetscSegBufferUnuse(PetscSegBuffer, PetscCount);
2753: /*MC
2754: PetscSegBufferGetInts - access an array of `PetscInt` from a `PetscSegBuffer`
2756: Synopsis:
2757: #include <petscsys.h>
2758: PetscErrorCode PetscSegBufferGetInts(PetscSegBuffer seg, size_t count, PetscInt *PETSC_RESTRICT *slot);
2760: No Fortran Support
2762: Input Parameters:
2763: + seg - `PetscSegBuffer` buffer
2764: - count - number of entries needed
2766: Output Parameter:
2767: . buf - address of new buffer for contiguous data
2769: Level: intermediate
2771: Developer Note:
2772: Type-safe wrapper to encourage use of PETSC_RESTRICT. Does not use PetscFunctionBegin because the error handling
2773: prevents the compiler from completely erasing the stub. This is called in inner loops so it has to be as fast as
2774: possible.
2776: .seealso: `PetscSegBuffer`, `PetscSegBufferGet()`, `PetscInitialize()`, `PetscFinalize()`, `PetscInitializeCalled`
2777: M*/
2778: static inline PetscErrorCode PetscSegBufferGetInts(PetscSegBuffer seg, PetscCount count, PetscInt *PETSC_RESTRICT *slot)
2779: {
2780: return PetscSegBufferGet(seg, count, (void **)slot);
2781: }
2783: extern PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton;
2784: PETSC_EXTERN PetscErrorCode PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *);
2785: PETSC_EXTERN PetscErrorCode PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *);
2786: PETSC_EXTERN PetscErrorCode PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted, const char *, const char *, PetscBool *);
2788: #include <stdarg.h>
2789: PETSC_EXTERN PetscErrorCode PetscVSNPrintf(char *, size_t, const char[], size_t *, va_list);
2790: PETSC_EXTERN PetscErrorCode (*PetscVFPrintf)(FILE *, const char[], va_list);
2792: PETSC_EXTERN PetscSegBuffer PetscCitationsList;
2794: /*@
2795: PetscCitationsRegister - Register a bibtex item to obtain credit for an implemented algorithm used in the code.
2797: Not Collective; No Fortran Support
2799: Input Parameters:
2800: + cite - the bibtex item, formatted to displayed on multiple lines nicely
2801: - set - a boolean variable initially set to `PETSC_FALSE`; this is used to insure only a single registration of the citation
2803: Options Database Key:
2804: . -citations [filename] - print out the bibtex entries for the given computation
2806: Level: intermediate
2808: .seealso: `PetscFinalize()`
2809: @*/
2810: static inline PetscErrorCode PetscCitationsRegister(const char cit[], PetscBool *set)
2811: {
2812: size_t len;
2813: char *vstring;
2815: PetscFunctionBegin;
2816: if (set && *set) PetscFunctionReturn(PETSC_SUCCESS);
2817: PetscCall(PetscStrlen(cit, &len));
2818: PetscCall(PetscSegBufferGet(PetscCitationsList, (PetscCount)len, &vstring));
2819: PetscCall(PetscArraycpy(vstring, cit, len));
2820: if (set) *set = PETSC_TRUE;
2821: PetscFunctionReturn(PETSC_SUCCESS);
2822: }
2824: PETSC_EXTERN PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm, char[], char[], size_t);
2825: PETSC_EXTERN PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm, const char[], char[], size_t);
2826: PETSC_EXTERN PetscErrorCode PetscGoogleDriveUpload(MPI_Comm, const char[], const char[]);
2828: PETSC_EXTERN PetscErrorCode PetscBoxAuthorize(MPI_Comm, char[], char[], size_t);
2829: PETSC_EXTERN PetscErrorCode PetscBoxRefresh(MPI_Comm, const char[], char[], char[], size_t);
2830: PETSC_EXTERN PetscErrorCode PetscBoxUpload(MPI_Comm, const char[], const char[]);
2832: PETSC_EXTERN PetscErrorCode PetscGlobusGetTransfers(MPI_Comm, const char[], char[], size_t);
2833: PETSC_EXTERN PetscErrorCode PetscGlobusAuthorize(MPI_Comm, char[], size_t);
2834: PETSC_EXTERN PetscErrorCode PetscGlobusUpload(MPI_Comm, const char[], const char[]);
2836: PETSC_EXTERN PetscErrorCode PetscPullJSONValue(const char[], const char[], char[], size_t, PetscBool *);
2837: PETSC_EXTERN PetscErrorCode PetscPushJSONValue(char[], const char[], const char[], size_t);
2839: #if !PetscDefined(HAVE_MPI_LARGE_COUNT)
2840: /*
2841: Cast PetscCount <a> to PetscMPIInt <b>, where <a> is likely used for the 'count' argument in MPI routines.
2842: It is similar to PetscMPIIntCast() except that here it returns an MPI error code.
2843: */
2844: #define PetscMPIIntCast_Internal(a, b) \
2845: do { \
2846: *(b) = 0; \
2847: if (PetscUnlikely((a) > (MPIU_Count)PETSC_MPI_INT_MAX)) return MPI_ERR_COUNT; \
2848: *(b) = (PetscMPIInt)(a); \
2849: } while (0)
2851: static inline PetscMPIInt MPIU_Get_count(MPI_Status *status, MPI_Datatype dtype, PetscCount *count)
2852: {
2853: PetscMPIInt count2, err;
2855: *count = 0; /* to prevent incorrect warnings of uninitialized variables */
2856: err = MPI_Get_count(status, dtype, &count2);
2857: *count = count2;
2858: return err;
2859: }
2861: static inline PetscMPIInt MPIU_Send(const void *buf, MPIU_Count count, MPI_Datatype dtype, PetscMPIInt dest, PetscMPIInt tag, MPI_Comm comm)
2862: {
2863: PetscMPIInt count2, err;
2865: PetscMPIIntCast_Internal(count, &count2);
2866: err = MPI_Send((void *)buf, count2, dtype, dest, tag, comm);
2867: return err;
2868: }
2870: 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)
2871: {
2872: PetscMPIInt count2, err;
2874: PetscMPIIntCast_Internal(count, &count2);
2875: err = MPI_Send_init((void *)buf, count2, dtype, dest, tag, comm, request);
2876: return err;
2877: }
2879: static inline PetscMPIInt MPIU_Isend(const void *buf, MPIU_Count count, MPI_Datatype dtype, PetscMPIInt dest, PetscMPIInt tag, MPI_Comm comm, MPI_Request *request)
2880: {
2881: PetscMPIInt count2, err;
2883: PetscMPIIntCast_Internal(count, &count2);
2884: err = MPI_Isend((void *)buf, count2, dtype, dest, tag, comm, request);
2885: return err;
2886: }
2888: static inline PetscMPIInt MPIU_Recv(const void *buf, MPIU_Count count, MPI_Datatype dtype, PetscMPIInt source, PetscMPIInt tag, MPI_Comm comm, MPI_Status *status)
2889: {
2890: PetscMPIInt count2, err;
2892: PetscMPIIntCast_Internal(count, &count2);
2893: err = MPI_Recv((void *)buf, count2, dtype, source, tag, comm, status);
2894: return err;
2895: }
2897: 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)
2898: {
2899: PetscMPIInt count2, err;
2901: PetscMPIIntCast_Internal(count, &count2);
2902: err = MPI_Recv_init((void *)buf, count2, dtype, source, tag, comm, request);
2903: return err;
2904: }
2906: static inline PetscMPIInt MPIU_Irecv(const void *buf, MPIU_Count count, MPI_Datatype dtype, PetscMPIInt source, PetscMPIInt tag, MPI_Comm comm, MPI_Request *request)
2907: {
2908: PetscMPIInt count2, err;
2910: PetscMPIIntCast_Internal(count, &count2);
2911: err = MPI_Irecv((void *)buf, count2, dtype, source, tag, comm, request);
2912: return err;
2913: }
2915: static inline PetscMPIInt MPIU_Reduce(const void *inbuf, void *outbuf, MPIU_Count count, MPI_Datatype dtype, MPI_Op op, PetscMPIInt root, MPI_Comm comm)
2916: {
2917: PetscMPIInt count2, err;
2919: PetscMPIIntCast_Internal(count, &count2);
2920: err = MPI_Reduce((void *)inbuf, outbuf, count2, dtype, op, root, comm);
2921: return err;
2922: }
2924: #if PetscDefined(HAVE_MPI_REDUCE_LOCAL)
2925: static inline PetscMPIInt MPIU_Reduce_local(const void *inbuf, void *inoutbuf, MPIU_Count count, MPI_Datatype dtype, MPI_Op op)
2926: {
2927: PetscMPIInt count2, err;
2929: PetscMPIIntCast_Internal(count, &count2);
2930: err = MPI_Reduce_local((void *)inbuf, inoutbuf, count2, dtype, op);
2931: return err;
2932: }
2933: #endif
2935: #if !PetscDefined(USE_64BIT_INDICES)
2936: #define MPIU_Scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm) MPI_Scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm)
2937: #define MPIU_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm) MPI_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm)
2938: #else
2939: #define MPIU_Scatterv(sendbuf, sendcount, displs, sendtype, recvbuf, recvcount, recvtype, root, comm) \
2940: ((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)
2941: #define MPIU_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, 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: #endif
2945: #else
2947: /* on 32 bit systems MPI_Count maybe 64-bit while PetscCount is 32-bit */
2948: #define PetscCountCast_Internal(a, b) \
2949: do { \
2950: *(b) = 0; \
2951: if (PetscUnlikely((a) > (MPI_Count)PETSC_COUNT_MAX)) return MPI_ERR_COUNT; \
2952: *(b) = (PetscMPIInt)(a); \
2953: } while (0)
2955: static inline PetscMPIInt MPIU_Get_count(MPI_Status *status, MPI_Datatype dtype, PetscCount *count)
2956: {
2957: MPI_Count count2;
2958: PetscMPIInt err;
2960: *count = 0; /* to prevent incorrect warnings of uninitialized variables */
2961: err = MPI_Get_count_c(status, dtype, &count2);
2962: if (err) return err;
2963: PetscCountCast_Internal(count2, count);
2964: return MPI_SUCCESS;
2965: }
2967: #define MPIU_Reduce(inbuf, outbuf, count, dtype, op, root, comm) MPI_Reduce_c(inbuf, outbuf, (MPI_Count)(count), dtype, op, root, comm)
2968: #define MPIU_Send(buf, count, dtype, dest, tag, comm) MPI_Send_c(buf, (MPI_Count)(count), dtype, dest, tag, comm)
2969: #define MPIU_Send_init(buf, count, dtype, dest, tag, comm, request) MPI_Send_init_c(buf, (MPI_Count)(count), dtype, dest, tag, comm, request)
2970: #define MPIU_Isend(buf, count, dtype, dest, tag, comm, request) MPI_Isend_c(buf, (MPI_Count)(count), dtype, dest, tag, comm, request)
2971: #define MPIU_Recv(buf, count, dtype, source, tag, comm, status) MPI_Recv_c(buf, (MPI_Count)(count), dtype, source, tag, comm, status)
2972: #define MPIU_Recv_init(buf, count, dtype, source, tag, comm, request) MPI_Recv_init_c(buf, (MPI_Count)(count), dtype, source, tag, comm, request)
2973: #define MPIU_Irecv(buf, count, dtype, source, tag, comm, request) MPI_Irecv_c(buf, (MPI_Count)(count), dtype, source, tag, comm, request)
2974: #if PetscDefined(HAVE_MPI_REDUCE_LOCAL)
2975: #define MPIU_Reduce_local(inbuf, inoutbuf, count, dtype, op) MPI_Reduce_local_c(inbuf, inoutbuf, (MPI_Count)(count), dtype, op)
2976: #endif
2978: /*MC
2979: 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.
2981: Synopsis:
2982: #include <petscsys.h>
2983: 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)
2985: Collective
2987: Input Parameters:
2988: + sendbuf - address of send buffer
2989: . sendcounts - non-negative `PetscInt` array (of length `comm` group size) specifying the number of elements to send to each MPI process
2990: . 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
2991: . sendtype - data type of `sendbuf` elements
2992: . recvcount - number of elements in `recvbuf` (non-negative integer)
2993: . recvtype - data type of `recvbuf` elements
2994: . root - Rank of the MPI root process, which will dispatch the data to scatter
2995: - comm - `MPI_Comm` communicator
2997: Output Parameter:
2998: . recvbuf - the resulting scattered values on this MPI process
3000: Level: developer
3002: Notes:
3003: Should be wrapped with `PetscCallMPI()` for error checking
3005: This is different than most of the `MPIU_` wrappers in that all the count arguments are in `PetscInt`
3007: .seealso: [](stylePetscCount), `MPI_Allreduce()`, `MPIU_Gatherv()`
3008: M*/
3010: #if !PetscDefined(USE_64BIT_INDICES)
3011: #define MPIU_Scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm) MPI_Scatterv(sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, root, comm)
3012: #define MPIU_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm) MPI_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, root, comm)
3013: #else
3014: #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)
3015: #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)
3016: #endif
3018: #endif
3020: PETSC_EXTERN PetscMPIInt MPIU_Allreduce_Private(const void *, void *, MPIU_Count, MPI_Datatype, MPI_Op, MPI_Comm);
3021: PETSC_EXTERN PetscErrorCode PetscCheckAllreduceSameLineAndCount_Private(MPI_Comm, const char *, PetscMPIInt, PetscMPIInt);
3023: static inline unsigned int PetscStrHash(const char *str)
3024: {
3025: unsigned int c, hash = 5381;
3027: while ((c = (unsigned int)*str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
3028: return hash;
3029: }
3031: /*MC
3032: MPIU_Allreduce - A replacement for `MPI_Allreduce()` that (1) performs single-count `MPIU_INT` operations in `PetscInt64` to detect
3033: integer overflows and (2) tries to determine if the call from all the MPI ranks occur in the
3034: same place in the PETSc code. This helps to detect bugs where different MPI ranks follow different code paths
3035: resulting in inconsistent and incorrect calls to `MPI_Allreduce()`.
3037: Synopsis:
3038: #include <petscsys.h>
3039: PetscMPIInt MPIU_Allreduce(void *indata, void *outdata, PetscCount count, MPI_Datatype dtype, MPI_Op op, MPI_Comm comm);
3041: Collective
3043: Input Parameters:
3044: + a - pointer to the input data to be reduced
3045: . count - the number of MPI data items in `a` and `b`
3046: . dtype - the `MPI_Datatype`, for example `MPI_INT`
3047: . op - the `MPI_Op`, for example `MPI_SUM`
3048: - comm - the `MPI_Comm` on which the operation occurs
3050: Output Parameter:
3051: . b - the reduced values
3053: Level: developer
3055: Note:
3056: Should be wrapped with `PetscCallMPI()` for error checking
3058: .seealso: [](stylePetscCount), `MPI_Allreduce()`
3059: M*/
3060: #if PetscDefined(USE_DEBUG)
3061: #define MPIU_Allreduce(a, b, count, dtype, op, comm) \
3062: PetscMacroReturnStandard( \
3063: PetscCall(PetscCheckAllreduceSameLineAndCount_Private((comm), __FILE__, (PetscMPIInt)__LINE__, (PetscMPIInt)(count))); \
3064: PetscCallMPI(MPIU_Allreduce_Private((a), (b), (count), (dtype), (op), (comm)));)
3065: #else
3066: #define MPIU_Allreduce(a, b, count, dtype, op, comm) MPIU_Allreduce_Private((a), (b), (count), (dtype), (op), (comm))
3067: #endif
3069: /* this is a vile hack */
3070: #if PetscDefined(HAVE_NECMPI)
3071: #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)
3072: #define MPI_Type_free(a) (*(a) = MPI_DATATYPE_NULL, 0);
3073: #endif
3074: #endif
3076: /*
3077: List of external packages and queries on it
3078: */
3079: PETSC_EXTERN PetscErrorCode PetscHasExternalPackage(const char[], PetscBool *);
3081: /* this cannot go here because it may be in a different shared library */
3082: PETSC_EXTERN PetscErrorCode PCMPIServerBegin(void);
3083: PETSC_EXTERN PetscErrorCode PCMPIServerEnd(void);
3084: PETSC_EXTERN PetscBool PCMPIServerActive;
3085: PETSC_EXTERN PetscBool PCMPIServerInSolve;
3086: PETSC_EXTERN PetscBool PCMPIServerUseShmget;
3087: PETSC_EXTERN PetscErrorCode PetscShmgetAllocateArray(size_t, size_t, void **);
3088: PETSC_EXTERN PetscErrorCode PetscShmgetDeallocateArray(void **);
3089: PETSC_EXTERN PetscErrorCode PetscShmgetMapAddresses(MPI_Comm, PetscInt, const void **, void **);
3090: PETSC_EXTERN PetscErrorCode PetscShmgetUnmapAddresses(PetscInt, void **);
3091: PETSC_EXTERN PetscErrorCode PetscShmgetAddressesFinalize(void);
3093: /*S
3094: PCMPIServerAddresses - Small bookkeeping record used by the `PCMPI` server to track shared-memory address mappings that have been distributed across the participating MPI ranks
3096: Level: developer
3098: Note:
3099: 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.
3101: .seealso: `PCMPI`, `PCMPIServerBegin()`, `PCMPIServerEnd()`, `PCMPIServerAddressesDestroy()`, `PCMPIServerUseShmget`
3102: S*/
3103: typedef struct {
3104: PetscInt n;
3105: void *addr[3];
3106: } PCMPIServerAddresses;
3107: PETSC_EXTERN PetscErrorCode PCMPIServerAddressesDestroy(PetscCtxRt);
3109: #define PETSC_HAVE_FORTRAN PETSC_DEPRECATED_MACRO(3, 20, 0, "PETSC_USE_FORTRAN_BINDINGS", ) PETSC_USE_FORTRAN_BINDINGS
3111: PETSC_EXTERN PetscErrorCode PetscBLASSetNumThreads(PetscInt);
3112: PETSC_EXTERN PetscErrorCode PetscBLASGetNumThreads(PetscInt *);
3114: /*MC
3115: PetscSafePointerPlusOffset - Checks that a pointer is not `NULL` before applying an offset
3117: Level: beginner
3119: Note:
3120: This is needed to avoid errors with undefined-behavior sanitizers such as
3121: UBSan, assuming PETSc has been configured with `-fsanitize=undefined` as part of the compiler flags
3123: .seealso: `PetscInitialize()`
3124: M*/
3125: #define PetscSafePointerPlusOffset(ptr, offset) ((ptr) ? (ptr) + (offset) : NULL)
3127: /* this is required to force PetscDevice to be visible at the system level for the Fortran interface */
3128: #include <petscdevicetypes.h>
3130: #if PetscDefined(USE_DEBUG)
3131: PETSC_EXTERN PetscErrorCode PetscStackView(FILE *);
3132: #else
3133: #define PetscStackView(file) PETSC_SUCCESS
3134: #endif
3136: /*MC
3137: PetscOverrideIntelMKLCPUVendorDetection - a macro to override Intel MKL's CPU vendor detection routines
3139: Level: developer
3141: Note:
3142: Intel MKL may detect the CPU vendor at runtime and select less optimized code paths on non-Intel CPUs, such as AMD processors.
3143: A common workaround is to intercept MKL's CPU detection functions (`mkl_serv_intel_cpu_true()` or `mkl_serv_get_cpu_true()`)
3144: and force them to report that the processor is an Intel CPU, thus enabling the optimized code paths of MKL.
3146: Put `PetscOverrideIntelMKLCPUVendorDetection();` before
3147: `main()`, and then compile and link the main object file before the MKL libraries (which is typically already the case).
3148: See <https://danieldk.eu/software/misc/intel-mkl-on-amd-zen>.
3150: .seealso: `PetscInitialize()`
3151: M*/
3152: #define PetscOverrideIntelMKLCPUVendorDetection() \
3153: PETSC_EXTERN int mkl_serv_intel_cpu_true(void) \
3154: { \
3155: return 1; \
3156: } \
3157: typedef int (*fakeintel_fptr)(void); \
3158: PETSC_EXTERN fakeintel_fptr mkl_serv_get_cpu_true(void) \
3159: { \
3160: return &mkl_serv_intel_cpu_true; \
3161: }