xref: /petsc/include/petscsys.h (revision 57f21012ae1765de385971e4fb54ab783992739c)
1d382aafbSBarry Smith /*
2d382aafbSBarry Smith    This is the main PETSc include file (for C and C++).  It is included by all
3d382aafbSBarry Smith    other PETSc include files, so it almost never has to be specifically included.
4d382aafbSBarry Smith */
526bd1501SBarry Smith #if !defined(PETSCSYS_H)
626bd1501SBarry Smith #define PETSCSYS_H
73ca90d2dSJacob Faibussowitsch 
8d382aafbSBarry Smith /* ========================================================================== */
9d382aafbSBarry Smith /*
10d382aafbSBarry Smith    petscconf.h is contained in ${PETSC_ARCH}/include/petscconf.h it is
111c77a0c5SBarry Smith    found automatically by the compiler due to the -I${PETSC_DIR}/${PETSC_ARCH}/include that
121c77a0c5SBarry Smith    PETSc's makefiles add to the compiler rules.
131c77a0c5SBarry Smith    For --prefix installs the directory ${PETSC_ARCH} does not exist and petscconf.h is in the same
141cc8b206SBarry Smith    directory as the other PETSc include files.
15d382aafbSBarry Smith */
162c8e378dSBarry Smith #include <petscconf.h>
171c77a0c5SBarry Smith #include <petscconf_poison.h>
182c8e378dSBarry Smith #include <petscfix.h>
19d382aafbSBarry Smith 
2033bb201bSJacob Faibussowitsch /*MC
2133bb201bSJacob Faibussowitsch   PetscHasAttribute - determine whether a particular __attribute__ is supported by the compiler
2233bb201bSJacob Faibussowitsch 
2333bb201bSJacob Faibussowitsch   Synopsis:
2433bb201bSJacob Faibussowitsch   #include <petscsys.h>
2533bb201bSJacob Faibussowitsch   boolean PetscHasAttribute(name)
2633bb201bSJacob Faibussowitsch 
2733bb201bSJacob Faibussowitsch   Input Parameter:
2833bb201bSJacob Faibussowitsch . name - the name of the attribute to test
2933bb201bSJacob Faibussowitsch 
3033bb201bSJacob Faibussowitsch   Notes:
3133bb201bSJacob Faibussowitsch   name should be identical to what you might pass to the __attribute__ declaration itself --
3233bb201bSJacob Faibussowitsch   plain, unbroken text.
3333bb201bSJacob Faibussowitsch 
3433bb201bSJacob Faibussowitsch   As PetscHasAttribute() is wrapper over the function-like macro __has_attribute(), the exact
3533bb201bSJacob Faibussowitsch   type and value returned is implementation defined. In practice however, it usually returns
3633bb201bSJacob Faibussowitsch   the integer literal 1 if the attribute is supported, and integer literal 0 if the attribute
3733bb201bSJacob Faibussowitsch   is not supported.
3833bb201bSJacob Faibussowitsch 
3933bb201bSJacob Faibussowitsch   Sample usage:
4033bb201bSJacob Faibussowitsch   Typical usage is usually using the preprocessor
4133bb201bSJacob Faibussowitsch 
4233bb201bSJacob Faibussowitsch .vb
4333bb201bSJacob Faibussowitsch   #if PetscHasAttribute(always_inline)
4433bb201bSJacob Faibussowitsch   #  define MY_ALWAYS_INLINE __attribute__((always_inline))
4533bb201bSJacob Faibussowitsch   #else
4633bb201bSJacob Faibussowitsch   #  define MY_ALWAYS_INLINE
4733bb201bSJacob Faibussowitsch   #endif
4833bb201bSJacob Faibussowitsch 
4933bb201bSJacob Faibussowitsch   void foo(void) MY_ALWAYS_INLINE;
5033bb201bSJacob Faibussowitsch .ve
5133bb201bSJacob Faibussowitsch 
5233bb201bSJacob Faibussowitsch   but can also be used in regular code
5333bb201bSJacob Faibussowitsch 
5433bb201bSJacob Faibussowitsch .vb
5533bb201bSJacob Faibussowitsch   if (PetscHasAttribute(some_attribute)) {
5633bb201bSJacob Faibussowitsch     foo();
5733bb201bSJacob Faibussowitsch   } else {
5833bb201bSJacob Faibussowitsch     bar();
5933bb201bSJacob Faibussowitsch   }
6033bb201bSJacob Faibussowitsch .ve
6133bb201bSJacob Faibussowitsch 
6233bb201bSJacob Faibussowitsch   Level: advanced
6333bb201bSJacob Faibussowitsch 
6433bb201bSJacob Faibussowitsch .seealso: PetscDefined(), PetscLikely(), PetscUnlikely()
6533bb201bSJacob Faibussowitsch M*/
6633bb201bSJacob Faibussowitsch #if defined(__has_attribute)
6733bb201bSJacob Faibussowitsch #  define PetscHasAttribute(x) __has_attribute(x)
6833bb201bSJacob Faibussowitsch #else
6933bb201bSJacob Faibussowitsch #  define PetscHasAttribute(x) 0
7033bb201bSJacob Faibussowitsch #endif
7133bb201bSJacob Faibussowitsch 
7233bb201bSJacob Faibussowitsch /* placeholder defines */
7333bb201bSJacob Faibussowitsch #if PetscHasAttribute(format)
7433bb201bSJacob Faibussowitsch #  define PETSC_ATTRIBUTE_FORMAT(strIdx,vaArgIdx)
7533bb201bSJacob Faibussowitsch #else
7633bb201bSJacob Faibussowitsch #  define PETSC_ATTRIBUTE_FORMAT(strIdx,vaArgIdx)
7733bb201bSJacob Faibussowitsch #endif
7833bb201bSJacob Faibussowitsch 
7973fca5a0SBarry Smith #if defined(PETSC_DESIRE_FEATURE_TEST_MACROS)
8073fca5a0SBarry Smith /*
8173fca5a0SBarry Smith    Feature test macros must be included before headers defined by IEEE Std 1003.1-2001
8273fca5a0SBarry Smith    We only turn these in PETSc source files that require them by setting PETSC_DESIRE_FEATURE_TEST_MACROS
8373fca5a0SBarry Smith */
84216f1ae6SBarry Smith #  if defined(PETSC__POSIX_C_SOURCE_200112L) && !defined(_POSIX_C_SOURCE)
8573fca5a0SBarry Smith #    define _POSIX_C_SOURCE 200112L
8673fca5a0SBarry Smith #  endif
87216f1ae6SBarry Smith #  if defined(PETSC__BSD_SOURCE) && !defined(_BSD_SOURCE)
8873fca5a0SBarry Smith #    define _BSD_SOURCE
8973fca5a0SBarry Smith #  endif
9007f00807SSatish Balay #  if defined(PETSC__DEFAULT_SOURCE) && !defined(_DEFAULT_SOURCE)
9107f00807SSatish Balay #    define _DEFAULT_SOURCE
9207f00807SSatish Balay #  endif
93ea0fecefSShri Abhyankar #  if defined(PETSC__GNU_SOURCE) && !defined(_GNU_SOURCE)
94ea0fecefSShri Abhyankar #    define _GNU_SOURCE
95ea0fecefSShri Abhyankar #  endif
9673fca5a0SBarry Smith #endif
9773fca5a0SBarry Smith 
98df4397b0SStefano Zampini #include <petscsystypes.h>
99df4397b0SStefano Zampini 
100d382aafbSBarry Smith /* ========================================================================== */
101d382aafbSBarry Smith /*
1022c280183SJed Brown    This facilitates using the C version of PETSc from C++ and the C++ version from C.
103d382aafbSBarry Smith */
1041ec50b02SJed Brown #if defined(__cplusplus)
1051ec50b02SJed Brown #  define PETSC_FUNCTION_NAME PETSC_FUNCTION_NAME_CXX
1061ec50b02SJed Brown #else
1071ec50b02SJed Brown #  define PETSC_FUNCTION_NAME PETSC_FUNCTION_NAME_C
1081ec50b02SJed Brown #endif
109d382aafbSBarry Smith 
110c5e4d11fSDmitry Karpeev /* ========================================================================== */
111c5e4d11fSDmitry Karpeev /*
112c5e4d11fSDmitry Karpeev    Since PETSc manages its own extern "C" handling users should never include PETSc include
1131cc8b206SBarry Smith    files within extern "C". This will generate a compiler error if a user does put the include
1141cc8b206SBarry Smith    file within an extern "C".
115c5e4d11fSDmitry Karpeev */
116c5e4d11fSDmitry Karpeev #if defined(__cplusplus)
117c5e4d11fSDmitry Karpeev void assert_never_put_petsc_headers_inside_an_extern_c(int); void assert_never_put_petsc_headers_inside_an_extern_c(double);
118c5e4d11fSDmitry Karpeev #endif
119c5e4d11fSDmitry Karpeev 
120ed938b00SJed Brown #if defined(__cplusplus)
121ed938b00SJed Brown #  define PETSC_RESTRICT PETSC_CXX_RESTRICT
122ed938b00SJed Brown #else
123ed938b00SJed Brown #  define PETSC_RESTRICT PETSC_C_RESTRICT
124ed938b00SJed Brown #endif
125ed938b00SJed Brown 
126ed938b00SJed Brown #if defined(__cplusplus)
12758c433abSLisandro Dalcin #  define PETSC_INLINE PETSC_CXX_INLINE
128ed938b00SJed Brown #else
12958c433abSLisandro Dalcin #  define PETSC_INLINE PETSC_C_INLINE
130ed938b00SJed Brown #endif
131ed938b00SJed Brown 
13258c433abSLisandro Dalcin #define PETSC_STATIC_INLINE static PETSC_INLINE
13358c433abSLisandro Dalcin 
134ab699e31SSatish Balay #if defined(_WIN32) && defined(PETSC_USE_SHARED_LIBRARIES) /* For Win32 shared libraries */
135014dd563SJed Brown #  define PETSC_DLLEXPORT __declspec(dllexport)
136014dd563SJed Brown #  define PETSC_DLLIMPORT __declspec(dllimport)
13721f66b81SJed Brown #  define PETSC_VISIBILITY_INTERNAL
13826703958SBarry Smith #elif defined(PETSC_USE_VISIBILITY_CXX) && defined(__cplusplus)
13926703958SBarry Smith #  define PETSC_DLLEXPORT __attribute__((visibility ("default")))
14026703958SBarry Smith #  define PETSC_DLLIMPORT __attribute__((visibility ("default")))
14126703958SBarry Smith #  define PETSC_VISIBILITY_INTERNAL __attribute__((visibility ("hidden")))
14226703958SBarry Smith #elif defined(PETSC_USE_VISIBILITY_C) && !defined(__cplusplus)
143014dd563SJed Brown #  define PETSC_DLLEXPORT __attribute__((visibility ("default")))
144014dd563SJed Brown #  define PETSC_DLLIMPORT __attribute__((visibility ("default")))
14521f66b81SJed Brown #  define PETSC_VISIBILITY_INTERNAL __attribute__((visibility ("hidden")))
146d382aafbSBarry Smith #else
147014dd563SJed Brown #  define PETSC_DLLEXPORT
148014dd563SJed Brown #  define PETSC_DLLIMPORT
14921f66b81SJed Brown #  define PETSC_VISIBILITY_INTERNAL
150014dd563SJed Brown #endif
151014dd563SJed Brown 
152014dd563SJed Brown #if defined(petsc_EXPORTS)      /* CMake defines this when building the shared library */
153014dd563SJed Brown #  define PETSC_VISIBILITY_PUBLIC PETSC_DLLEXPORT
154014dd563SJed Brown #else  /* Win32 users need this to import symbols from petsc.dll */
155014dd563SJed Brown #  define PETSC_VISIBILITY_PUBLIC PETSC_DLLIMPORT
156014dd563SJed Brown #endif
157014dd563SJed Brown 
1581cc8b206SBarry Smith /*
1591cc8b206SBarry Smith     Functions tagged with PETSC_EXTERN in the header files are
1601cc8b206SBarry Smith   always defined as extern "C" when compiled with C++ so they may be
1611cc8b206SBarry Smith   used from C and are always visible in the shared libraries
1621cc8b206SBarry Smith */
1632c280183SJed Brown #if defined(__cplusplus)
164014dd563SJed Brown #  define PETSC_EXTERN extern "C" PETSC_VISIBILITY_PUBLIC
165638cfed1SJed Brown #  define PETSC_EXTERN_TYPEDEF extern "C"
16621f66b81SJed Brown #  define PETSC_INTERN extern "C" PETSC_VISIBILITY_INTERNAL
167014dd563SJed Brown #else
168014dd563SJed Brown #  define PETSC_EXTERN extern PETSC_VISIBILITY_PUBLIC
169638cfed1SJed Brown #  define PETSC_EXTERN_TYPEDEF
1706258c452SJed Brown #  define PETSC_INTERN extern PETSC_VISIBILITY_INTERNAL
171d382aafbSBarry Smith #endif
17227710113SBarry Smith 
173193d546dSJacob Faibussowitsch #if defined(PETSC_USE_SINGLE_LIBRARY)
174193d546dSJacob Faibussowitsch #  define PETSC_SINGLE_LIBRARY_INTERN PETSC_INTERN
175193d546dSJacob Faibussowitsch #else
176193d546dSJacob Faibussowitsch #  define PETSC_SINGLE_LIBRARY_INTERN PETSC_EXTERN
177193d546dSJacob Faibussowitsch #endif
178193d546dSJacob Faibussowitsch 
1790609f53bSJacob Faibussowitsch /* C++11 features */
180030f984aSJacob Faibussowitsch #if defined(__cplusplus) && defined(PETSC_HAVE_CXX_DIALECT_CXX11)
181030f984aSJacob Faibussowitsch #  define PETSC_NULLPTR             nullptr
182030f984aSJacob Faibussowitsch #  define PETSC_CONSTEXPR           constexpr
183030f984aSJacob Faibussowitsch #  define PETSC_NOEXCEPT            noexcept
184030f984aSJacob Faibussowitsch #  define PETSC_NOEXCEPT_ARG(cond_) noexcept(cond_)
185030f984aSJacob Faibussowitsch #else
186030f984aSJacob Faibussowitsch #  define PETSC_NULLPTR             NULL
187030f984aSJacob Faibussowitsch #  define PETSC_CONSTEXPR
188030f984aSJacob Faibussowitsch #  define PETSC_NOEXCEPT
189030f984aSJacob Faibussowitsch #  define PETSC_NOEXCEPT_ARG(cond_)
1900609f53bSJacob Faibussowitsch #endif /* __cplusplus && PETSC_HAVE_CXX_DIALECT_CXX11 */
1910609f53bSJacob Faibussowitsch 
1920609f53bSJacob Faibussowitsch /* C++14 features */
1930609f53bSJacob Faibussowitsch #if defined(PETSC_HAVE_CXX_DIALECT_CXX14)
1940609f53bSJacob Faibussowitsch #  define PETSC_CONSTEXPR_14 PETSC_CONSTEXPR
1950609f53bSJacob Faibussowitsch #else
1960609f53bSJacob Faibussowitsch #  define PETSC_CONSTEXPR_14
197030f984aSJacob Faibussowitsch #endif
198030f984aSJacob Faibussowitsch 
1990609f53bSJacob Faibussowitsch /* C++17 features */
2000a996781SJunchao Zhang /* We met cases that the host CXX compiler (say mpicxx) supports C++17, but nvcc does not agree, even with -ccbin mpicxx! */
2010a996781SJunchao Zhang #if defined(__cplusplus) && defined(PETSC_HAVE_CXX_DIALECT_CXX17) && (!defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_CUDA_DIALECT_CXX17))
202030f984aSJacob Faibussowitsch #  define PETSC_NODISCARD    [[nodiscard]]
2030609f53bSJacob Faibussowitsch #  define PETSC_CONSTEXPR_17 PETSC_CONSTEXPR
204030f984aSJacob Faibussowitsch #else
205030f984aSJacob Faibussowitsch #  define PETSC_NODISCARD
2060609f53bSJacob Faibussowitsch #  define PETSC_CONSTEXPR_17
207030f984aSJacob Faibussowitsch #endif
208030f984aSJacob Faibussowitsch 
2092c8e378dSBarry Smith #include <petscversion.h>
210a17b96a8SKyle Gerard Felker #define PETSC_AUTHOR_INFO  "       The PETSc Team\n    petsc-maint@mcs.anl.gov\n https://petsc.org/\n"
211d382aafbSBarry Smith 
212d382aafbSBarry Smith /* ========================================================================== */
213d382aafbSBarry Smith 
214d382aafbSBarry Smith /*
215d382aafbSBarry Smith     Defines the interface to MPI allowing the use of all MPI functions.
216d382aafbSBarry Smith 
217d382aafbSBarry Smith     PETSc does not use the C++ binding of MPI at ALL. The following flag
218d382aafbSBarry Smith     makes sure the C++ bindings are not included. The C++ bindings REQUIRE
219d382aafbSBarry Smith     putting mpi.h before ANY C++ include files, we cannot control this
220d382aafbSBarry Smith     with all PETSc users. Users who want to use the MPI C++ bindings can include
221d382aafbSBarry Smith     mpicxx.h directly in their code
222d382aafbSBarry Smith */
223fa9e63e2SJed Brown #if !defined(MPICH_SKIP_MPICXX)
224d382aafbSBarry Smith #  define MPICH_SKIP_MPICXX 1
225fa9e63e2SJed Brown #endif
226fa9e63e2SJed Brown #if !defined(OMPI_SKIP_MPICXX)
227d382aafbSBarry Smith #  define OMPI_SKIP_MPICXX 1
228fa9e63e2SJed Brown #endif
229e771154cSJed Brown #if defined(PETSC_HAVE_MPIUNI)
230e771154cSJed Brown #  include <petsc/mpiuni/mpi.h>
231e771154cSJed Brown #else
2322c8e378dSBarry Smith #  include <mpi.h>
233e771154cSJed Brown #endif
23465013866SBarry Smith 
2354e1ad211SJed Brown /*MC
2364e1ad211SJed Brown   PetscDefined - determine whether a boolean macro is defined
2374e1ad211SJed Brown 
2384e1ad211SJed Brown   Notes:
2391393a3abSPatrick Sanan   The prefix "PETSC_" is added to the argument.
2401393a3abSPatrick Sanan 
2414e1ad211SJed Brown   Typical usage is within normal code,
2424e1ad211SJed Brown 
2434e1ad211SJed Brown $   if (PetscDefined(USE_DEBUG)) { ... }
2444e1ad211SJed Brown 
2454e1ad211SJed Brown   but can also be used in the preprocessor,
2464e1ad211SJed Brown 
2474e1ad211SJed Brown $   #if PetscDefined(USE_DEBUG)
2484e1ad211SJed Brown $     ...
2494e1ad211SJed Brown $   #else
2504e1ad211SJed Brown 
2511393a3abSPatrick Sanan   Either way, it evaluates true if PETSC_USE_DEBUG is defined (merely defined or defined to 1), and false if PETSC_USE_DEBUG is undefined.  This macro
2524e1ad211SJed Brown   should not be used if its argument may be defined to a non-empty value other than 1.
2534e1ad211SJed Brown 
2541393a3abSPatrick Sanan   To avoid prepending "PETSC_", say to add custom checks in user code, one can use e.g.
2551393a3abSPatrick Sanan 
2561393a3abSPatrick Sanan $  #define FooDefined(d) PetscDefined_(FOO_ ## d)
2571393a3abSPatrick Sanan 
2584e1ad211SJed Brown   Developer Notes:
2594e1ad211SJed Brown   Getting something that works in C and CPP for an arg that may or may not be defined is tricky.  Here, if we have
2604e1ad211SJed Brown   "#define PETSC_HAVE_BOOGER 1" we match on the placeholder define, insert the "0," for arg1 and generate the triplet
2614e1ad211SJed Brown   (0, 1, 0).  Then the last step cherry picks the 2nd arg (a one).  When PETSC_HAVE_BOOGER is not defined, we generate
2624e1ad211SJed Brown   a (... 1, 0) pair, and when the last step cherry picks the 2nd arg, we get a zero.
2634e1ad211SJed Brown 
2644e1ad211SJed Brown   Our extra expansion via PetscDefined__take_second_expand() is needed with MSVC, which has a nonconforming
2654e1ad211SJed Brown   implementation of variadic macros.
2664e1ad211SJed Brown 
2674e1ad211SJed Brown   Level: developer
26833bb201bSJacob Faibussowitsch .seealso: PetscHasAttribute(), PetscUnlikely(), PetscLikely()
2694e1ad211SJed Brown M*/
2704e1ad211SJed Brown #if !defined(PETSC_SKIP_VARIADIC_MACROS)
2714e1ad211SJed Brown #  define PetscDefined_arg_1    shift,
2724e1ad211SJed Brown #  define PetscDefined_arg_     shift,
2734e1ad211SJed Brown #  define PetscDefined__take_second_expanded(ignored, val, ...) val
2744e1ad211SJed Brown #  define PetscDefined__take_second_expand(args) PetscDefined__take_second_expanded args
2754e1ad211SJed Brown #  define PetscDefined__take_second(...) PetscDefined__take_second_expand((__VA_ARGS__))
276c7faf32eSJacob Faibussowitsch #  define PetscDefined____(arg1_or_junk) PetscDefined__take_second(arg1_or_junk 1, 0, at_)
277c7faf32eSJacob Faibussowitsch #  define PetscDefined___(value) PetscDefined____(PetscDefined_arg_ ## value)
278c7faf32eSJacob Faibussowitsch #  define PetscDefined__(d)      PetscDefined___(d)
279c7faf32eSJacob Faibussowitsch #  define PetscDefined_(d)       PetscDefined__(PETSC_ ## d)
280c7faf32eSJacob Faibussowitsch #  define PetscDefined(d)        PetscDefined_(d)
2814e1ad211SJed Brown #endif
2824e1ad211SJed Brown 
283d382aafbSBarry Smith /*
284a7886beaSBarry Smith    Perform various sanity checks that the correct mpi.h is being included at compile time.
285a7886beaSBarry Smith    This usually happens because
286a7886beaSBarry Smith       * either an unexpected mpi.h is in the default compiler path (i.e. in /usr/include) or
287a7886beaSBarry Smith       * an extra include path -I/something (which contains the unexpected mpi.h) is being passed to the compiler
288a7886beaSBarry Smith */
289a7886beaSBarry Smith #if defined(PETSC_HAVE_MPIUNI)
290c9b973beSBarry Smith #  if !defined(MPIUNI_H)
291a7886beaSBarry Smith #    error "PETSc was configured with --with-mpi=0 but now appears to be compiling using a different mpi.h"
292a7886beaSBarry Smith #  endif
29341f4af4cSSatish Balay #elif defined(PETSC_HAVE_I_MPI_NUMVERSION)
29441f4af4cSSatish Balay #  if !defined(I_MPI_NUMVERSION)
29541f4af4cSSatish Balay #    error "PETSc was configured with I_MPI but now appears to be compiling using a non-I_MPI mpi.h"
29641f4af4cSSatish Balay #  elif I_MPI_NUMVERSION != PETSC_HAVE_I_MPI_NUMVERSION
29741f4af4cSSatish Balay #    error "PETSc was configured with one I_MPI mpi.h version but now appears to be compiling using a different I_MPI mpi.h version"
29841f4af4cSSatish Balay #  endif
29941f4af4cSSatish Balay #elif defined(PETSC_HAVE_MVAPICH2_NUMVERSION)
30041f4af4cSSatish Balay #  if !defined(MVAPICH2_NUMVERSION)
30141f4af4cSSatish Balay #    error "PETSc was configured with MVAPICH2 but now appears to be compiling using a non-MVAPICH2 mpi.h"
30241f4af4cSSatish Balay #  elif MVAPICH2_NUMVERSION != PETSC_HAVE_MVAPICH2_NUMVERSION
30341f4af4cSSatish Balay #    error "PETSc was configured with one MVAPICH2 mpi.h version but now appears to be compiling using a different MVAPICH2 mpi.h version"
30441f4af4cSSatish Balay #  endif
305a7886beaSBarry Smith #elif defined(PETSC_HAVE_MPICH_NUMVERSION)
30641f4af4cSSatish Balay #  if !defined(MPICH_NUMVERSION) || defined(MVAPICH2_NUMVERSION) || defined(I_MPI_NUMVERSION)
307a7886beaSBarry Smith #    error "PETSc was configured with MPICH but now appears to be compiling using a non-MPICH mpi.h"
308ca70f86eSJed Brown #  elif (MPICH_NUMVERSION/100000 != PETSC_HAVE_MPICH_NUMVERSION/100000) || (MPICH_NUMVERSION%100000/1000 < PETSC_HAVE_MPICH_NUMVERSION%100000/1000)
309a7886beaSBarry Smith #    error "PETSc was configured with one MPICH mpi.h version but now appears to be compiling using a different MPICH mpi.h version"
310a7886beaSBarry Smith #  endif
311a7886beaSBarry Smith #elif defined(PETSC_HAVE_OMPI_MAJOR_VERSION)
312a7886beaSBarry Smith #  if !defined(OMPI_MAJOR_VERSION)
313a7886beaSBarry Smith #    error "PETSc was configured with OpenMPI but now appears to be compiling using a non-OpenMPI mpi.h"
314ca70f86eSJed Brown #  elif (OMPI_MAJOR_VERSION != PETSC_HAVE_OMPI_MAJOR_VERSION) || (OMPI_MINOR_VERSION != PETSC_HAVE_OMPI_MINOR_VERSION) || (OMPI_RELEASE_VERSION < PETSC_HAVE_OMPI_RELEASE_VERSION)
315c3ac57cfSSatish Balay #    error "PETSc was configured with one OpenMPI mpi.h version but now appears to be compiling using a different OpenMPI mpi.h version"
316a7886beaSBarry Smith #  endif
317c93fae50SJacob Faibussowitsch #  define PETSC_MPI_COMM_FMT "p"
318c93fae50SJacob Faibussowitsch #  define PETSC_MPI_WIN_FMT  "p"
319d97fd468SStefano Zampini #elif defined(PETSC_HAVE_MSMPI_VERSION)
320d97fd468SStefano Zampini #  if !defined(MSMPI_VER)
321d97fd468SStefano Zampini #    error "PETSc was configured with MSMPI but now appears to be compiling using a non-MSMPI mpi.h"
322d97fd468SStefano Zampini #  elif (MSMPI_VER != PETSC_HAVE_MSMPI_VERSION)
323d97fd468SStefano Zampini #    error "PETSc was configured with one MSMPI mpi.h version but now appears to be compiling using a different MSMPI mpi.h version"
324d97fd468SStefano Zampini #  endif
325d97fd468SStefano Zampini #elif defined(OMPI_MAJOR_VERSION) || defined(MPICH_NUMVERSION) || defined(MSMPI_VER)
326d97fd468SStefano Zampini #  error "PETSc was configured with undetermined MPI - but now appears to be compiling using any of OpenMPI, MS-MPI or a MPICH variant"
327a7886beaSBarry Smith #endif
328a7886beaSBarry Smith 
329c93fae50SJacob Faibussowitsch /* Format specifier for printing MPI_Comm (most implementations use 'int' as type) */
330c93fae50SJacob Faibussowitsch #if !defined(PETSC_MPI_COMM_FMT)
331c93fae50SJacob Faibussowitsch #  define PETSC_MPI_COMM_FMT "d"
332c93fae50SJacob Faibussowitsch #endif
333c93fae50SJacob Faibussowitsch 
334c93fae50SJacob Faibussowitsch #if !defined(PETSC_MPI_WIN_FMT)
335c93fae50SJacob Faibussowitsch #  define PETSC_MPI_WIN_FMT "d"
336c93fae50SJacob Faibussowitsch #endif
337c93fae50SJacob Faibussowitsch 
338a7886beaSBarry Smith /*
3392981ebdbSBarry Smith     Need to put stdio.h AFTER mpi.h for MPICH2 with C++ compiler
340d382aafbSBarry Smith     see the top of mpicxx.h in the MPICH2 distribution.
341d382aafbSBarry Smith */
342d382aafbSBarry Smith #include <stdio.h>
343d382aafbSBarry Smith 
344d382aafbSBarry Smith /* MSMPI on 32bit windows requires this yukky hack - that breaks MPI standard compliance */
345d382aafbSBarry Smith #if !defined(MPIAPI)
346d382aafbSBarry Smith #define MPIAPI
347d382aafbSBarry Smith #endif
348d382aafbSBarry Smith 
3491cc8b206SBarry Smith /*
3501cc8b206SBarry Smith    Support for Clang (>=3.2) matching type tag arguments with void* buffer types.
3511cc8b206SBarry Smith    This allows the compiler to detect cases where the MPI datatype argument passed to a MPI routine
3521cc8b206SBarry Smith    does not match the actual type of the argument being passed in
3531cc8b206SBarry Smith */
354c5e4d11fSDmitry Karpeev #if defined(__has_attribute) && defined(works_with_const_which_is_not_true)
355894dd566SJed Brown #  if __has_attribute(argument_with_type_tag) && __has_attribute(pointer_with_type_tag) && __has_attribute(type_tag_for_datatype)
356894dd566SJed Brown #    define PetscAttrMPIPointerWithType(bufno,typeno) __attribute__((pointer_with_type_tag(MPI,bufno,typeno)))
3578ad47952SJed Brown #    define PetscAttrMPITypeTag(type)                 __attribute__((type_tag_for_datatype(MPI,type)))
3588ad47952SJed Brown #    define PetscAttrMPITypeTagLayoutCompatible(type) __attribute__((type_tag_for_datatype(MPI,type,layout_compatible)))
359894dd566SJed Brown #  endif
360894dd566SJed Brown #endif
361894dd566SJed Brown #if !defined(PetscAttrMPIPointerWithType)
362894dd566SJed Brown #  define PetscAttrMPIPointerWithType(bufno,typeno)
3638ad47952SJed Brown #  define PetscAttrMPITypeTag(type)
3648ad47952SJed Brown #  define PetscAttrMPITypeTagLayoutCompatible(type)
365894dd566SJed Brown #endif
366b3506946SBarry Smith 
3675a576424SJed Brown PETSC_EXTERN MPI_Datatype MPIU_ENUM PetscAttrMPITypeTag(PetscEnum);
368df4397b0SStefano Zampini PETSC_EXTERN MPI_Datatype MPIU_BOOL PetscAttrMPITypeTag(PetscBool);
369e28ce29aSPatrick Sanan 
370e28ce29aSPatrick Sanan /*MC
371e28ce29aSPatrick Sanan    MPIU_INT - MPI datatype corresponding to PetscInt
372e28ce29aSPatrick Sanan 
373e28ce29aSPatrick Sanan    Notes:
374e28ce29aSPatrick Sanan    In MPI calls that require an MPI datatype that matches a PetscInt or array of PetscInt values, pass this value.
375e28ce29aSPatrick Sanan 
376e28ce29aSPatrick Sanan    Level: beginner
377e28ce29aSPatrick Sanan 
378e28ce29aSPatrick Sanan .seealso: PetscReal, PetscScalar, PetscComplex, PetscInt, MPIU_REAL, MPIU_SCALAR, MPIU_COMPLEX
379e28ce29aSPatrick Sanan M*/
380e28ce29aSPatrick Sanan 
3817cdaf61dSJed Brown PETSC_EXTERN MPI_Datatype MPIU_FORTRANADDR;
382b7b8f77aSBarry Smith 
383d382aafbSBarry Smith #if defined(PETSC_USE_64BIT_INDICES)
384bd84f1cfSSatish Balay #  define MPIU_INT MPIU_INT64
385a05e1a72SSatish Balay #  define PetscInt_FMT PetscInt64_FMT
386d382aafbSBarry Smith #else
387d382aafbSBarry Smith #  define MPIU_INT MPI_INT
388a05e1a72SSatish Balay #  define PetscInt_FMT "d"
389d382aafbSBarry Smith #endif
390d382aafbSBarry Smith 
391503cfb0cSBarry Smith /*
392503cfb0cSBarry Smith     For the rare cases when one needs to send a size_t object with MPI
393503cfb0cSBarry Smith */
394e316c87fSJed Brown PETSC_EXTERN MPI_Datatype MPIU_SIZE_T;
395d382aafbSBarry Smith 
396d382aafbSBarry Smith /*
397d382aafbSBarry Smith       You can use PETSC_STDOUT as a replacement of stdout. You can also change
398d382aafbSBarry Smith     the value of PETSC_STDOUT to redirect all standard output elsewhere
399d382aafbSBarry Smith */
400014dd563SJed Brown PETSC_EXTERN FILE* PETSC_STDOUT;
401d382aafbSBarry Smith 
402d382aafbSBarry Smith /*
403d382aafbSBarry Smith       You can use PETSC_STDERR as a replacement of stderr. You can also change
404d382aafbSBarry Smith     the value of PETSC_STDERR to redirect all standard error elsewhere
405d382aafbSBarry Smith */
406014dd563SJed Brown PETSC_EXTERN FILE* PETSC_STDERR;
407d382aafbSBarry Smith 
408d382aafbSBarry Smith /*MC
409d382aafbSBarry Smith     PetscUnlikely - hints the compiler that the given condition is usually FALSE
410d382aafbSBarry Smith 
411d382aafbSBarry Smith     Synopsis:
412aaa7dc30SBarry Smith     #include <petscsys.h>
413ace3abfcSBarry Smith     PetscBool  PetscUnlikely(PetscBool  cond)
414d382aafbSBarry Smith 
415eca87e8dSBarry Smith     Not Collective
416eca87e8dSBarry Smith 
417d382aafbSBarry Smith     Input Parameters:
418d382aafbSBarry Smith .   cond - condition or expression
419d382aafbSBarry Smith 
420e28ce29aSPatrick Sanan     Notes:
421e28ce29aSPatrick Sanan     This returns the same truth value, it is only a hint to compilers that the resulting
422d382aafbSBarry Smith     branch is unlikely.
423d382aafbSBarry Smith 
424d382aafbSBarry Smith     Level: advanced
425d382aafbSBarry Smith 
42633bb201bSJacob Faibussowitsch .seealso: PetscUnlikelyDebug(), PetscLikely(), CHKERRQ, PetscDefined(), PetscHasAttribute()
427d382aafbSBarry Smith M*/
428d382aafbSBarry Smith 
429d382aafbSBarry Smith /*MC
430d382aafbSBarry Smith     PetscLikely - hints the compiler that the given condition is usually TRUE
431d382aafbSBarry Smith 
432d382aafbSBarry Smith     Synopsis:
433aaa7dc30SBarry Smith     #include <petscsys.h>
43423072422SBarry Smith     PetscBool  PetscLikely(PetscBool  cond)
435d382aafbSBarry Smith 
436eca87e8dSBarry Smith     Not Collective
437eca87e8dSBarry Smith 
438d382aafbSBarry Smith     Input Parameters:
439d382aafbSBarry Smith .   cond - condition or expression
440d382aafbSBarry Smith 
441e28ce29aSPatrick Sanan     Notes:
442e28ce29aSPatrick Sanan     This returns the same truth value, it is only a hint to compilers that the resulting
443d382aafbSBarry Smith     branch is likely.
444d382aafbSBarry Smith 
445d382aafbSBarry Smith     Level: advanced
446d382aafbSBarry Smith 
44733bb201bSJacob Faibussowitsch .seealso: PetscUnlikely(), PetscDefined(), PetscHasAttribute()
448d382aafbSBarry Smith M*/
449d382aafbSBarry Smith #if defined(PETSC_HAVE_BUILTIN_EXPECT)
450d382aafbSBarry Smith #  define PetscUnlikely(cond)   __builtin_expect(!!(cond),0)
451d382aafbSBarry Smith #  define PetscLikely(cond)     __builtin_expect(!!(cond),1)
452d382aafbSBarry Smith #else
453cd3f4ce9SJed Brown #  define PetscUnlikely(cond)   (cond)
454cd3f4ce9SJed Brown #  define PetscLikely(cond)     (cond)
455d382aafbSBarry Smith #endif
456d382aafbSBarry Smith 
457cf9c20a2SJed Brown /*MC
458cf9c20a2SJed Brown     PetscUnlikelyDebug - hints the compiler that the given condition is usually FALSE, eliding the check in optimized mode
459cf9c20a2SJed Brown 
460cf9c20a2SJed Brown     Synopsis:
461cf9c20a2SJed Brown     #include <petscsys.h>
462cf9c20a2SJed Brown     PetscBool  PetscUnlikelyDebug(PetscBool  cond)
463cf9c20a2SJed Brown 
464cf9c20a2SJed Brown     Not Collective
465cf9c20a2SJed Brown 
466cf9c20a2SJed Brown     Input Parameters:
467cf9c20a2SJed Brown .   cond - condition or expression
468cf9c20a2SJed Brown 
469cf9c20a2SJed Brown     Notes:
470cf9c20a2SJed Brown     This returns the same truth value, it is only a hint to compilers that the resulting
471cf9c20a2SJed Brown     branch is unlikely.  When compiled in optimized mode, it always returns false.
472cf9c20a2SJed Brown 
473cf9c20a2SJed Brown     Level: advanced
474cf9c20a2SJed Brown 
475cf9c20a2SJed Brown .seealso: PetscUnlikely(), CHKERRQ, SETERRQ
476cf9c20a2SJed Brown M*/
477cf9c20a2SJed Brown #define PetscUnlikelyDebug(cond) (PetscDefined(USE_DEBUG) && PetscUnlikely(cond))
478cf9c20a2SJed Brown 
479c8b9133aSSatish Balay /* PetscPragmaSIMD - from CeedPragmaSIMD */
480c8b9133aSSatish Balay 
4815c993f7fSStefano Zampini #if defined(__NEC__)
4825c993f7fSStefano Zampini #  define PetscPragmaSIMD _Pragma("_NEC ivdep")
4835c993f7fSStefano Zampini #elif defined(__INTEL_COMPILER) && !defined(_WIN32)
484c8b9133aSSatish Balay #  define PetscPragmaSIMD _Pragma("vector")
4851dba8834SJose E. Roman #elif defined(__GNUC__) && __GNUC__ >= 5 && !defined(__PGI)
486c8b9133aSSatish Balay #  define PetscPragmaSIMD _Pragma("GCC ivdep")
487e5518150SSatish Balay #elif defined(_OPENMP) && _OPENMP >= 201307
488e5518150SSatish Balay #  if defined(_MSC_VER)
489160a5775SAli Reza Khaz'ali #    define PetscPragmaSIMD __pragma(omp simd)
490e5518150SSatish Balay #  else
491e5518150SSatish Balay #    define PetscPragmaSIMD _Pragma("omp simd")
492e5518150SSatish Balay #  endif
493c8b9133aSSatish Balay #elif defined(PETSC_HAVE_CRAY_VECTOR)
494c8b9133aSSatish Balay #  define PetscPragmaSIMD _Pragma("_CRI ivdep")
495c8b9133aSSatish Balay #else
496c8b9133aSSatish Balay #  define PetscPragmaSIMD
497c8b9133aSSatish Balay #endif
498c8b9133aSSatish Balay 
499d382aafbSBarry Smith /*
500d382aafbSBarry Smith     Declare extern C stuff after including external header files
501d382aafbSBarry Smith */
502d382aafbSBarry Smith 
5036a6fc655SJed Brown PETSC_EXTERN const char *const PetscBools[];
50470b3c8c7SBarry Smith 
5056edef35eSSatish Balay PETSC_EXTERN PetscBool PETSC_RUNNING_ON_VALGRIND;
5068b49ba18SBarry Smith /*
507390e1bf2SBarry Smith     Defines elementary mathematics functions and constants.
5088b49ba18SBarry Smith */
5098b49ba18SBarry Smith #include <petscmath.h>
5108b49ba18SBarry Smith 
5116a6fc655SJed Brown PETSC_EXTERN const char *const PetscCopyModes[];
512d382aafbSBarry Smith 
513d382aafbSBarry Smith /*MC
5140298fd71SBarry Smith     PETSC_IGNORE - same as NULL, means PETSc will ignore this argument
515d382aafbSBarry Smith 
516d382aafbSBarry Smith    Level: beginner
517d382aafbSBarry Smith 
518e28ce29aSPatrick Sanan    Note:
519e28ce29aSPatrick Sanan    Accepted by many PETSc functions to not set a parameter and instead use
520d382aafbSBarry Smith           some default
521d382aafbSBarry Smith 
522e28ce29aSPatrick Sanan    Fortran Notes:
523e28ce29aSPatrick Sanan    This macro does not exist in Fortran; you must use PETSC_NULL_INTEGER,
524d382aafbSBarry Smith           PETSC_NULL_DOUBLE_PRECISION etc
525d382aafbSBarry Smith 
526e28ce29aSPatrick Sanan .seealso: PETSC_DECIDE, PETSC_DEFAULT, PETSC_DETERMINE
527d382aafbSBarry Smith 
528d382aafbSBarry Smith M*/
5290298fd71SBarry Smith #define PETSC_IGNORE NULL
530d382aafbSBarry Smith 
531c528d872SBarry Smith /* This is deprecated */
532c528d872SBarry Smith #define PETSC_NULL NULL
533c528d872SBarry Smith 
534d382aafbSBarry Smith /*MC
535557d4da8SBarry Smith     PETSC_DECIDE - standard way of passing in integer or floating point parameter
536557d4da8SBarry Smith        where you wish PETSc to use the default.
537557d4da8SBarry Smith 
538557d4da8SBarry Smith    Level: beginner
539557d4da8SBarry Smith 
540e28ce29aSPatrick Sanan .seealso: PETSC_DEFAULT, PETSC_IGNORE, PETSC_DETERMINE
541557d4da8SBarry Smith 
542557d4da8SBarry Smith M*/
543557d4da8SBarry Smith #define PETSC_DECIDE -1
544557d4da8SBarry Smith 
545557d4da8SBarry Smith /*MC
546d382aafbSBarry Smith     PETSC_DETERMINE - standard way of passing in integer or floating point parameter
547d382aafbSBarry Smith        where you wish PETSc to compute the required value.
548d382aafbSBarry Smith 
549d382aafbSBarry Smith    Level: beginner
550d382aafbSBarry Smith 
551e28ce29aSPatrick Sanan    Developer Note:
552e28ce29aSPatrick Sanan    I would like to use const PetscInt PETSC_DETERMINE = PETSC_DECIDE; but for
553557d4da8SBarry Smith      some reason this is not allowed by the standard even though PETSC_DECIDE is a constant value.
554557d4da8SBarry Smith 
5550298fd71SBarry Smith .seealso: PETSC_DECIDE, PETSC_DEFAULT, PETSC_IGNORE, VecSetSizes()
556d382aafbSBarry Smith 
557d382aafbSBarry Smith M*/
558d382aafbSBarry Smith #define PETSC_DETERMINE PETSC_DECIDE
559d382aafbSBarry Smith 
560d382aafbSBarry Smith /*MC
561557d4da8SBarry Smith     PETSC_DEFAULT - standard way of passing in integer or floating point parameter
562557d4da8SBarry Smith        where you wish PETSc to use the default.
563557d4da8SBarry Smith 
564557d4da8SBarry Smith    Level: beginner
565557d4da8SBarry Smith 
566e28ce29aSPatrick Sanan    Fortran Notes:
567e28ce29aSPatrick Sanan    You need to use PETSC_DEFAULT_INTEGER or PETSC_DEFAULT_REAL.
568557d4da8SBarry Smith 
5690298fd71SBarry Smith .seealso: PETSC_DECIDE, PETSC_IGNORE, PETSC_DETERMINE
570557d4da8SBarry Smith 
571557d4da8SBarry Smith M*/
572557d4da8SBarry Smith #define PETSC_DEFAULT -2
573557d4da8SBarry Smith 
574557d4da8SBarry Smith /*MC
575d382aafbSBarry Smith     PETSC_COMM_WORLD - the equivalent of the MPI_COMM_WORLD communicator which represents
576ea8fe74dSBarry Smith            all the processes that PETSc knows about.
577d382aafbSBarry Smith 
578d382aafbSBarry Smith    Level: beginner
579d382aafbSBarry Smith 
580e28ce29aSPatrick Sanan    Notes:
581e28ce29aSPatrick Sanan    By default PETSC_COMM_WORLD and MPI_COMM_WORLD are identical unless you wish to
582d382aafbSBarry Smith           run PETSc on ONLY a subset of MPI_COMM_WORLD. In that case create your new (smaller)
583d382aafbSBarry Smith           communicator, call it, say comm, and set PETSC_COMM_WORLD = comm BEFORE calling
584a990b902SBarry Smith           PetscInitialize(), but after MPI_Init() has been called.
585a990b902SBarry Smith 
586a990b902SBarry Smith           The value of PETSC_COMM_WORLD should never be USED/accessed before PetscInitialize()
587a990b902SBarry Smith           is called because it may not have a valid value yet.
588d382aafbSBarry Smith 
589d382aafbSBarry Smith .seealso: PETSC_COMM_SELF
590d382aafbSBarry Smith 
591d382aafbSBarry Smith M*/
592014dd563SJed Brown PETSC_EXTERN MPI_Comm PETSC_COMM_WORLD;
593d382aafbSBarry Smith 
594d382aafbSBarry Smith /*MC
595503cfb0cSBarry Smith     PETSC_COMM_SELF - This is always MPI_COMM_SELF
596d382aafbSBarry Smith 
597d382aafbSBarry Smith    Level: beginner
598d382aafbSBarry Smith 
599e28ce29aSPatrick Sanan    Notes:
600e28ce29aSPatrick Sanan    Do not USE/access or set this variable before PetscInitialize() has been called.
601a990b902SBarry Smith 
602d382aafbSBarry Smith .seealso: PETSC_COMM_WORLD
603d382aafbSBarry Smith 
604d382aafbSBarry Smith M*/
605d382aafbSBarry Smith #define PETSC_COMM_SELF MPI_COMM_SELF
606d382aafbSBarry Smith 
6076de5d289SStefano Zampini /*MC
6086de5d289SStefano Zampini     PETSC_MPI_THREAD_REQUIRED - the required threading support used if PETSc initializes
6096de5d289SStefano Zampini            MPI with MPI_Init_thread.
6106de5d289SStefano Zampini 
6116de5d289SStefano Zampini    Level: beginner
6126de5d289SStefano Zampini 
6136de5d289SStefano Zampini    Notes:
6146de5d289SStefano Zampini    By default PETSC_MPI_THREAD_REQUIRED equals MPI_THREAD_FUNNELED.
6156de5d289SStefano Zampini 
6166de5d289SStefano Zampini .seealso: PetscInitialize()
6176de5d289SStefano Zampini 
6186de5d289SStefano Zampini M*/
6196de5d289SStefano Zampini PETSC_EXTERN PetscMPIInt PETSC_MPI_THREAD_REQUIRED;
6206de5d289SStefano Zampini 
621d6f2c3cbSBarry Smith PETSC_EXTERN PetscBool PetscBeganMPI;
6228ad20175SVaclav Hapla PETSC_EXTERN PetscBool PetscErrorHandlingInitialized;
623014dd563SJed Brown PETSC_EXTERN PetscBool PetscInitializeCalled;
624014dd563SJed Brown PETSC_EXTERN PetscBool PetscFinalizeCalled;
6254cf1874eSKarl Rupp PETSC_EXTERN PetscBool PetscViennaCLSynchronize;
626d382aafbSBarry Smith 
627014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*)(MPI_Comm),PetscErrorCode (*)(MPI_Comm));
628014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommDuplicate(MPI_Comm,MPI_Comm*,int*);
629014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommDestroy(MPI_Comm*);
630*57f21012SBarry Smith PETSC_EXTERN PetscErrorCode PetscCommGetComm(MPI_Comm,MPI_Comm*);
631*57f21012SBarry Smith PETSC_EXTERN PetscErrorCode PetscCommRestoreComm(MPI_Comm,MPI_Comm*);
632d382aafbSBarry Smith 
63359e55d94SJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
63459e55d94SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscKokkosInitializeCheck(void);  /* Initialize Kokkos if not yet. */
63559e55d94SJunchao Zhang #endif
63659e55d94SJunchao Zhang 
63771438e86SJunchao Zhang #if defined(PETSC_HAVE_NVSHMEM)
63871438e86SJunchao Zhang PETSC_EXTERN PetscBool      PetscBeganNvshmem;
63971438e86SJunchao Zhang PETSC_EXTERN PetscBool      PetscNvshmemInitialized;
64071438e86SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscNvshmemFinalize(void);
64171438e86SJunchao Zhang #endif
64271438e86SJunchao Zhang 
643540e20f2SPierre Jolivet #if defined(PETSC_HAVE_ELEMENTAL)
644540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalInitializePackage(void);
645540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalInitialized(PetscBool*);
646540e20f2SPierre Jolivet PETSC_EXTERN PetscErrorCode PetscElementalFinalizePackage(void);
647540e20f2SPierre Jolivet #endif
648540e20f2SPierre Jolivet 
649d382aafbSBarry Smith /*MC
65095dccacaSBarry Smith    PetscMalloc - Allocates memory, One should use PetscNew(), PetscMalloc1() or PetscCalloc1() usually instead of this
651d382aafbSBarry Smith 
652eca87e8dSBarry Smith    Synopsis:
653aaa7dc30SBarry Smith     #include <petscsys.h>
654eca87e8dSBarry Smith    PetscErrorCode PetscMalloc(size_t m,void **result)
655eca87e8dSBarry Smith 
656eca87e8dSBarry Smith    Not Collective
657eca87e8dSBarry Smith 
658d382aafbSBarry Smith    Input Parameter:
659d382aafbSBarry Smith .  m - number of bytes to allocate
660d382aafbSBarry Smith 
661d382aafbSBarry Smith    Output Parameter:
662d382aafbSBarry Smith .  result - memory allocated
663d382aafbSBarry Smith 
664d382aafbSBarry Smith    Level: beginner
665d382aafbSBarry Smith 
66649d7da52SJed Brown    Notes:
66749d7da52SJed Brown    Memory is always allocated at least double aligned
668d382aafbSBarry Smith 
66949d7da52SJed Brown    It is safe to allocate size 0 and pass the resulting pointer (which may or may not be NULL) to PetscFree().
670d382aafbSBarry Smith 
671d382aafbSBarry Smith .seealso: PetscFree(), PetscNew()
672d382aafbSBarry Smith 
673d382aafbSBarry Smith M*/
674071fcb05SBarry Smith #define PetscMalloc(a,b)  ((*PetscTrMalloc)((a),PETSC_FALSE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(void**)(b)))
675d382aafbSBarry Smith 
676d382aafbSBarry Smith /*MC
6773221ece2SMatthew G. Knepley    PetscRealloc - Rellocates memory
6783221ece2SMatthew G. Knepley 
6793221ece2SMatthew G. Knepley    Synopsis:
6803221ece2SMatthew G. Knepley     #include <petscsys.h>
6813221ece2SMatthew G. Knepley    PetscErrorCode PetscRealloc(size_t m,void **result)
6823221ece2SMatthew G. Knepley 
6833221ece2SMatthew G. Knepley    Not Collective
6843221ece2SMatthew G. Knepley 
6853221ece2SMatthew G. Knepley    Input Parameters:
6863221ece2SMatthew G. Knepley +  m - number of bytes to allocate
68792f119d6SBarry Smith -  result - previous memory
6883221ece2SMatthew G. Knepley 
6893221ece2SMatthew G. Knepley    Output Parameter:
6903221ece2SMatthew G. Knepley .  result - new memory allocated
6913221ece2SMatthew G. Knepley 
6923221ece2SMatthew G. Knepley    Level: developer
6933221ece2SMatthew G. Knepley 
6943221ece2SMatthew G. Knepley    Notes:
6953221ece2SMatthew G. Knepley    Memory is always allocated at least double aligned
6963221ece2SMatthew G. Knepley 
6973221ece2SMatthew G. Knepley .seealso: PetscMalloc(), PetscFree(), PetscNew()
6983221ece2SMatthew G. Knepley 
6993221ece2SMatthew G. Knepley M*/
7003221ece2SMatthew G. Knepley #define PetscRealloc(a,b)  ((*PetscTrRealloc)((a),__LINE__,PETSC_FUNCTION_NAME,__FILE__,(void**)(b)))
7013221ece2SMatthew G. Knepley 
7023221ece2SMatthew G. Knepley /*MC
703503cfb0cSBarry Smith    PetscAddrAlign - Rounds up an address to PETSC_MEMALIGN alignment
704d382aafbSBarry Smith 
705d382aafbSBarry Smith    Synopsis:
706aaa7dc30SBarry Smith     #include <petscsys.h>
707d382aafbSBarry Smith    void *PetscAddrAlign(void *addr)
708d382aafbSBarry Smith 
709eca87e8dSBarry Smith    Not Collective
710eca87e8dSBarry Smith 
711eca87e8dSBarry Smith    Input Parameters:
712eca87e8dSBarry Smith .  addr - address to align (any pointer type)
713eca87e8dSBarry Smith 
714d382aafbSBarry Smith    Level: developer
715d382aafbSBarry Smith 
716d382aafbSBarry Smith .seealso: PetscMallocAlign()
717d382aafbSBarry Smith 
718d382aafbSBarry Smith M*/
719d382aafbSBarry Smith #define PetscAddrAlign(a) (void*)((((PETSC_UINTPTR_T)(a))+(PETSC_MEMALIGN-1)) & ~(PETSC_MEMALIGN-1))
720d382aafbSBarry Smith 
721d382aafbSBarry Smith /*MC
7228f51dc47SJunchao Zhang    PetscCalloc - Allocates a cleared (zeroed) memory region aligned to PETSC_MEMALIGN
7238f51dc47SJunchao Zhang 
7248f51dc47SJunchao Zhang    Synopsis:
7258f51dc47SJunchao Zhang     #include <petscsys.h>
7268f51dc47SJunchao Zhang    PetscErrorCode PetscCalloc(size_t m,void **result)
7278f51dc47SJunchao Zhang 
7288f51dc47SJunchao Zhang    Not Collective
7298f51dc47SJunchao Zhang 
7308f51dc47SJunchao Zhang    Input Parameter:
7318f51dc47SJunchao Zhang .  m - number of bytes to allocate
7328f51dc47SJunchao Zhang 
7338f51dc47SJunchao Zhang    Output Parameter:
7348f51dc47SJunchao Zhang .  result - memory allocated
7358f51dc47SJunchao Zhang 
7368f51dc47SJunchao Zhang    Level: beginner
7378f51dc47SJunchao Zhang 
7388f51dc47SJunchao Zhang    Notes:
7398f51dc47SJunchao Zhang    Memory is always allocated at least double aligned. This macro is useful in allocating memory pointed by void pointers
7408f51dc47SJunchao Zhang 
7418f51dc47SJunchao Zhang    It is safe to allocate size 0 and pass the resulting pointer (which may or may not be NULL) to PetscFree().
7428f51dc47SJunchao Zhang 
7438f51dc47SJunchao Zhang .seealso: PetscFree(), PetscNew()
7448f51dc47SJunchao Zhang 
7458f51dc47SJunchao Zhang M*/
7468f51dc47SJunchao Zhang #define PetscCalloc(m,result)  PetscMallocA(1,PETSC_TRUE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m),(result))
7478f51dc47SJunchao Zhang 
7488f51dc47SJunchao Zhang /*MC
749785e854fSJed Brown    PetscMalloc1 - Allocates an array of memory aligned to PETSC_MEMALIGN
750d382aafbSBarry Smith 
751eca87e8dSBarry Smith    Synopsis:
752aaa7dc30SBarry Smith     #include <petscsys.h>
753785e854fSJed Brown    PetscErrorCode PetscMalloc1(size_t m1,type **r1)
754785e854fSJed Brown 
755785e854fSJed Brown    Not Collective
756785e854fSJed Brown 
757785e854fSJed Brown    Input Parameter:
758390e1bf2SBarry Smith .  m1 - number of elements to allocate  (may be zero)
759785e854fSJed Brown 
760785e854fSJed Brown    Output Parameter:
7611fe1b817SJunchao Zhang .  r1 - memory allocated
762785e854fSJed Brown 
763e28ce29aSPatrick Sanan    Note:
764e28ce29aSPatrick Sanan    This uses the sizeof() of the memory type requested to determine the total memory to be allocated, therefore you should not
765390e1bf2SBarry Smith          multiply the number of elements requested by the sizeof() the type. For example use
766390e1bf2SBarry Smith $  PetscInt *id;
767390e1bf2SBarry Smith $  PetscMalloc1(10,&id);
768390e1bf2SBarry Smith         not
769390e1bf2SBarry Smith $  PetscInt *id;
770390e1bf2SBarry Smith $  PetscMalloc1(10*sizeof(PetscInt),&id);
771390e1bf2SBarry Smith 
7721fe1b817SJunchao Zhang         Does not zero the memory allocated, use PetscCalloc1() to obtain memory that has been zeroed.
773390e1bf2SBarry Smith 
774390e1bf2SBarry Smith    Level: beginner
775785e854fSJed Brown 
7761795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscCalloc1(), PetscMalloc2()
777785e854fSJed Brown 
778785e854fSJed Brown M*/
7792553b7ecSJed Brown #define PetscMalloc1(m1,r1) PetscMallocA(1,PETSC_FALSE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1))
780785e854fSJed Brown 
781785e854fSJed Brown /*MC
7821795a4d1SJed Brown    PetscCalloc1 - Allocates a cleared (zeroed) array of memory aligned to PETSC_MEMALIGN
7831795a4d1SJed Brown 
7841795a4d1SJed Brown    Synopsis:
785aaa7dc30SBarry Smith     #include <petscsys.h>
7861795a4d1SJed Brown    PetscErrorCode PetscCalloc1(size_t m1,type **r1)
7871795a4d1SJed Brown 
7881795a4d1SJed Brown    Not Collective
7891795a4d1SJed Brown 
7901795a4d1SJed Brown    Input Parameter:
7911795a4d1SJed Brown .  m1 - number of elements to allocate in 1st chunk  (may be zero)
7921795a4d1SJed Brown 
7931795a4d1SJed Brown    Output Parameter:
7941fe1b817SJunchao Zhang .  r1 - memory allocated
7951795a4d1SJed Brown 
796e28ce29aSPatrick Sanan    Notes:
797e28ce29aSPatrick Sanan    See PetsMalloc1() for more details on usage.
798390e1bf2SBarry Smith 
799390e1bf2SBarry Smith    Level: beginner
8001795a4d1SJed Brown 
8011795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc1(), PetscCalloc2()
8021795a4d1SJed Brown 
8031795a4d1SJed Brown M*/
8042553b7ecSJed Brown #define PetscCalloc1(m1,r1) PetscMallocA(1,PETSC_TRUE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1))
8051795a4d1SJed Brown 
8061795a4d1SJed Brown /*MC
807dcca6d9dSJed Brown    PetscMalloc2 - Allocates 2 arrays of memory both aligned to PETSC_MEMALIGN
808d382aafbSBarry Smith 
809eca87e8dSBarry Smith    Synopsis:
810aaa7dc30SBarry Smith     #include <petscsys.h>
811dcca6d9dSJed Brown    PetscErrorCode PetscMalloc2(size_t m1,type **r1,size_t m2,type **r2)
812eca87e8dSBarry Smith 
813eca87e8dSBarry Smith    Not Collective
814eca87e8dSBarry Smith 
815d8d19677SJose E. Roman    Input Parameters:
816d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
817dcca6d9dSJed Brown -  m2 - number of elements to allocate in 2nd chunk  (may be zero)
818d382aafbSBarry Smith 
819d8d19677SJose E. Roman    Output Parameters:
820d382aafbSBarry Smith +  r1 - memory allocated in first chunk
821d382aafbSBarry Smith -  r2 - memory allocated in second chunk
822d382aafbSBarry Smith 
823d382aafbSBarry Smith    Level: developer
824d382aafbSBarry Smith 
8251795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc1(), PetscCalloc2()
826d382aafbSBarry Smith 
827d382aafbSBarry Smith M*/
8282553b7ecSJed Brown #define PetscMalloc2(m1,r1,m2,r2) PetscMallocA(2,PETSC_FALSE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2))
829d382aafbSBarry Smith 
830d382aafbSBarry Smith /*MC
8311795a4d1SJed Brown    PetscCalloc2 - Allocates 2 cleared (zeroed) arrays of memory both aligned to PETSC_MEMALIGN
832d382aafbSBarry Smith 
833eca87e8dSBarry Smith    Synopsis:
834aaa7dc30SBarry Smith     #include <petscsys.h>
8351795a4d1SJed Brown    PetscErrorCode PetscCalloc2(size_t m1,type **r1,size_t m2,type **r2)
836eca87e8dSBarry Smith 
837eca87e8dSBarry Smith    Not Collective
838eca87e8dSBarry Smith 
839d8d19677SJose E. Roman    Input Parameters:
840d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
8411795a4d1SJed Brown -  m2 - number of elements to allocate in 2nd chunk  (may be zero)
8421795a4d1SJed Brown 
843d8d19677SJose E. Roman    Output Parameters:
8441795a4d1SJed Brown +  r1 - memory allocated in first chunk
8451795a4d1SJed Brown -  r2 - memory allocated in second chunk
8461795a4d1SJed Brown 
8471795a4d1SJed Brown    Level: developer
8481795a4d1SJed Brown 
8491795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscCalloc1(), PetscMalloc2()
8501795a4d1SJed Brown 
8511795a4d1SJed Brown M*/
8522553b7ecSJed Brown #define PetscCalloc2(m1,r1,m2,r2) PetscMallocA(2,PETSC_TRUE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2))
8531795a4d1SJed Brown 
8541795a4d1SJed Brown /*MC
855dcca6d9dSJed Brown    PetscMalloc3 - Allocates 3 arrays of memory, all aligned to PETSC_MEMALIGN
856d382aafbSBarry Smith 
857d382aafbSBarry Smith    Synopsis:
858aaa7dc30SBarry Smith     #include <petscsys.h>
859dcca6d9dSJed Brown    PetscErrorCode PetscMalloc3(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3)
860d382aafbSBarry Smith 
861d382aafbSBarry Smith    Not Collective
862d382aafbSBarry Smith 
863d8d19677SJose E. Roman    Input Parameters:
864d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
865d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
866dcca6d9dSJed Brown -  m3 - number of elements to allocate in 3rd chunk  (may be zero)
867d382aafbSBarry Smith 
868d8d19677SJose E. Roman    Output Parameters:
869d382aafbSBarry Smith +  r1 - memory allocated in first chunk
870d382aafbSBarry Smith .  r2 - memory allocated in second chunk
871d382aafbSBarry Smith -  r3 - memory allocated in third chunk
872d382aafbSBarry Smith 
873d382aafbSBarry Smith    Level: developer
874d382aafbSBarry Smith 
8751795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscCalloc3(), PetscFree3()
876d382aafbSBarry Smith 
877d382aafbSBarry Smith M*/
8782553b7ecSJed Brown #define PetscMalloc3(m1,r1,m2,r2,m3,r3) PetscMallocA(3,PETSC_FALSE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2),(size_t)(m3)*sizeof(**(r3)),(r3))
879d382aafbSBarry Smith 
880d382aafbSBarry Smith /*MC
8811795a4d1SJed Brown    PetscCalloc3 - Allocates 3 cleared (zeroed) arrays of memory, all aligned to PETSC_MEMALIGN
882d382aafbSBarry Smith 
883eca87e8dSBarry Smith    Synopsis:
884aaa7dc30SBarry Smith     #include <petscsys.h>
8851795a4d1SJed Brown    PetscErrorCode PetscCalloc3(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3)
886eca87e8dSBarry Smith 
887eca87e8dSBarry Smith    Not Collective
888eca87e8dSBarry Smith 
889d8d19677SJose E. Roman    Input Parameters:
890d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
891d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
8921795a4d1SJed Brown -  m3 - number of elements to allocate in 3rd chunk  (may be zero)
8931795a4d1SJed Brown 
894d8d19677SJose E. Roman    Output Parameters:
8951795a4d1SJed Brown +  r1 - memory allocated in first chunk
8961795a4d1SJed Brown .  r2 - memory allocated in second chunk
8971795a4d1SJed Brown -  r3 - memory allocated in third chunk
8981795a4d1SJed Brown 
8991795a4d1SJed Brown    Level: developer
9001795a4d1SJed Brown 
9011795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscCalloc2(), PetscMalloc3(), PetscFree3()
9021795a4d1SJed Brown 
9031795a4d1SJed Brown M*/
9042553b7ecSJed Brown #define PetscCalloc3(m1,r1,m2,r2,m3,r3) PetscMallocA(3,PETSC_TRUE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2),(size_t)(m3)*sizeof(**(r3)),(r3))
9051795a4d1SJed Brown 
9061795a4d1SJed Brown /*MC
907dcca6d9dSJed Brown    PetscMalloc4 - Allocates 4 arrays of memory, all aligned to PETSC_MEMALIGN
908d382aafbSBarry Smith 
909d382aafbSBarry Smith    Synopsis:
910aaa7dc30SBarry Smith     #include <petscsys.h>
911dcca6d9dSJed Brown    PetscErrorCode PetscMalloc4(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4)
912d382aafbSBarry Smith 
913d382aafbSBarry Smith    Not Collective
914d382aafbSBarry Smith 
915d8d19677SJose E. Roman    Input Parameters:
916d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
917d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
918d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
919dcca6d9dSJed Brown -  m4 - number of elements to allocate in 4th chunk  (may be zero)
920d382aafbSBarry Smith 
921d8d19677SJose E. Roman    Output Parameters:
922d382aafbSBarry Smith +  r1 - memory allocated in first chunk
923d382aafbSBarry Smith .  r2 - memory allocated in second chunk
924d382aafbSBarry Smith .  r3 - memory allocated in third chunk
925d382aafbSBarry Smith -  r4 - memory allocated in fourth chunk
926d382aafbSBarry Smith 
927d382aafbSBarry Smith    Level: developer
928d382aafbSBarry Smith 
9291795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscCalloc4(), PetscFree4()
930d382aafbSBarry Smith 
931d382aafbSBarry Smith M*/
9322553b7ecSJed Brown #define PetscMalloc4(m1,r1,m2,r2,m3,r3,m4,r4) PetscMallocA(4,PETSC_FALSE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2),(size_t)(m3)*sizeof(**(r3)),(r3),(size_t)(m4)*sizeof(**(r4)),(r4))
933d382aafbSBarry Smith 
934d382aafbSBarry Smith /*MC
9351795a4d1SJed Brown    PetscCalloc4 - Allocates 4 cleared (zeroed) arrays of memory, all aligned to PETSC_MEMALIGN
936d382aafbSBarry Smith 
937eca87e8dSBarry Smith    Synopsis:
938aaa7dc30SBarry Smith     #include <petscsys.h>
9391795a4d1SJed Brown    PetscErrorCode PetscCalloc4(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4)
940eca87e8dSBarry Smith 
941eca87e8dSBarry Smith    Not Collective
942eca87e8dSBarry Smith 
943e28ce29aSPatrick Sanan    Input Parameters:
944d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
945d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
946d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
9471795a4d1SJed Brown -  m4 - number of elements to allocate in 4th chunk  (may be zero)
9481795a4d1SJed Brown 
949e28ce29aSPatrick Sanan    Output Parameters:
9501795a4d1SJed Brown +  r1 - memory allocated in first chunk
9511795a4d1SJed Brown .  r2 - memory allocated in second chunk
9521795a4d1SJed Brown .  r3 - memory allocated in third chunk
9531795a4d1SJed Brown -  r4 - memory allocated in fourth chunk
9541795a4d1SJed Brown 
9551795a4d1SJed Brown    Level: developer
9561795a4d1SJed Brown 
9571795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscCalloc4(), PetscFree4()
9581795a4d1SJed Brown 
9591795a4d1SJed Brown M*/
9602553b7ecSJed Brown #define PetscCalloc4(m1,r1,m2,r2,m3,r3,m4,r4) PetscMallocA(4,PETSC_TRUE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2),(size_t)(m3)*sizeof(**(r3)),(r3),(size_t)(m4)*sizeof(**(r4)),(r4))
9611795a4d1SJed Brown 
9621795a4d1SJed Brown /*MC
963dcca6d9dSJed Brown    PetscMalloc5 - Allocates 5 arrays of memory, all aligned to PETSC_MEMALIGN
964d382aafbSBarry Smith 
965d382aafbSBarry Smith    Synopsis:
966aaa7dc30SBarry Smith     #include <petscsys.h>
967dcca6d9dSJed Brown    PetscErrorCode PetscMalloc5(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5)
968d382aafbSBarry Smith 
969d382aafbSBarry Smith    Not Collective
970d382aafbSBarry Smith 
971e28ce29aSPatrick Sanan    Input Parameters:
972d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
973d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
974d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
975d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
976dcca6d9dSJed Brown -  m5 - number of elements to allocate in 5th chunk  (may be zero)
977d382aafbSBarry Smith 
978e28ce29aSPatrick Sanan    Output Parameters:
979d382aafbSBarry Smith +  r1 - memory allocated in first chunk
980d382aafbSBarry Smith .  r2 - memory allocated in second chunk
981d382aafbSBarry Smith .  r3 - memory allocated in third chunk
982d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
983d382aafbSBarry Smith -  r5 - memory allocated in fifth chunk
984d382aafbSBarry Smith 
985d382aafbSBarry Smith    Level: developer
986d382aafbSBarry Smith 
9871795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscCalloc5(), PetscFree5()
988d382aafbSBarry Smith 
989d382aafbSBarry Smith M*/
9902553b7ecSJed Brown #define PetscMalloc5(m1,r1,m2,r2,m3,r3,m4,r4,m5,r5) PetscMallocA(5,PETSC_FALSE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2),(size_t)(m3)*sizeof(**(r3)),(r3),(size_t)(m4)*sizeof(**(r4)),(r4),(size_t)(m5)*sizeof(**(r5)),(r5))
991d382aafbSBarry Smith 
992d382aafbSBarry Smith /*MC
9931795a4d1SJed Brown    PetscCalloc5 - Allocates 5 cleared (zeroed) arrays of memory, all aligned to PETSC_MEMALIGN
994d382aafbSBarry Smith 
995eca87e8dSBarry Smith    Synopsis:
996aaa7dc30SBarry Smith     #include <petscsys.h>
9971795a4d1SJed Brown    PetscErrorCode PetscCalloc5(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5)
998eca87e8dSBarry Smith 
999eca87e8dSBarry Smith    Not Collective
1000eca87e8dSBarry Smith 
1001e28ce29aSPatrick Sanan    Input Parameters:
1002d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
1003d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
1004d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
1005d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
10061795a4d1SJed Brown -  m5 - number of elements to allocate in 5th chunk  (may be zero)
10071795a4d1SJed Brown 
1008e28ce29aSPatrick Sanan    Output Parameters:
10091795a4d1SJed Brown +  r1 - memory allocated in first chunk
10101795a4d1SJed Brown .  r2 - memory allocated in second chunk
10111795a4d1SJed Brown .  r3 - memory allocated in third chunk
10121795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
10131795a4d1SJed Brown -  r5 - memory allocated in fifth chunk
10141795a4d1SJed Brown 
10151795a4d1SJed Brown    Level: developer
10161795a4d1SJed Brown 
10171795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc5(), PetscFree5()
10181795a4d1SJed Brown 
10191795a4d1SJed Brown M*/
10202553b7ecSJed Brown #define PetscCalloc5(m1,r1,m2,r2,m3,r3,m4,r4,m5,r5) PetscMallocA(5,PETSC_TRUE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2),(size_t)(m3)*sizeof(**(r3)),(r3),(size_t)(m4)*sizeof(**(r4)),(r4),(size_t)(m5)*sizeof(**(r5)),(r5))
1021d382aafbSBarry Smith 
1022d382aafbSBarry Smith /*MC
1023dcca6d9dSJed Brown    PetscMalloc6 - Allocates 6 arrays of memory, all aligned to PETSC_MEMALIGN
1024d382aafbSBarry Smith 
1025d382aafbSBarry Smith    Synopsis:
1026aaa7dc30SBarry Smith     #include <petscsys.h>
1027dcca6d9dSJed Brown    PetscErrorCode PetscMalloc6(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5,size_t m6,type **r6)
1028d382aafbSBarry Smith 
1029d382aafbSBarry Smith    Not Collective
1030d382aafbSBarry Smith 
1031e28ce29aSPatrick Sanan    Input Parameters:
1032d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
1033d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
1034d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
1035d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
1036d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
1037dcca6d9dSJed Brown -  m6 - number of elements to allocate in 6th chunk  (may be zero)
1038d382aafbSBarry Smith 
1039e28ce29aSPatrick Sanan    Output Parameteasr:
1040d382aafbSBarry Smith +  r1 - memory allocated in first chunk
1041d382aafbSBarry Smith .  r2 - memory allocated in second chunk
1042d382aafbSBarry Smith .  r3 - memory allocated in third chunk
1043d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
1044d382aafbSBarry Smith .  r5 - memory allocated in fifth chunk
1045d382aafbSBarry Smith -  r6 - memory allocated in sixth chunk
1046d382aafbSBarry Smith 
1047d382aafbSBarry Smith    Level: developer
1048d382aafbSBarry Smith 
10491795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscCalloc6(), PetscFree3(), PetscFree4(), PetscFree5(), PetscFree6()
1050d382aafbSBarry Smith 
1051d382aafbSBarry Smith M*/
10522553b7ecSJed Brown #define PetscMalloc6(m1,r1,m2,r2,m3,r3,m4,r4,m5,r5,m6,r6) PetscMallocA(6,PETSC_FALSE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2),(size_t)(m3)*sizeof(**(r3)),(r3),(size_t)(m4)*sizeof(**(r4)),(r4),(size_t)(m5)*sizeof(**(r5)),(r5),(size_t)(m6)*sizeof(**(r6)),(r6))
1053d382aafbSBarry Smith 
1054d382aafbSBarry Smith /*MC
10551795a4d1SJed Brown    PetscCalloc6 - Allocates 6 cleared (zeroed) arrays of memory, all aligned to PETSC_MEMALIGN
1056d382aafbSBarry Smith 
1057eca87e8dSBarry Smith    Synopsis:
1058aaa7dc30SBarry Smith     #include <petscsys.h>
10591795a4d1SJed Brown    PetscErrorCode PetscCalloc6(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5,size_t m6,type **r6)
1060eca87e8dSBarry Smith 
1061eca87e8dSBarry Smith    Not Collective
1062eca87e8dSBarry Smith 
1063e28ce29aSPatrick Sanan    Input Parameters:
1064d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
1065d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
1066d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
1067d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
1068d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
10691795a4d1SJed Brown -  m6 - number of elements to allocate in 6th chunk  (may be zero)
10701795a4d1SJed Brown 
1071e28ce29aSPatrick Sanan    Output Parameters:
10721795a4d1SJed Brown +  r1 - memory allocated in first chunk
10731795a4d1SJed Brown .  r2 - memory allocated in second chunk
10741795a4d1SJed Brown .  r3 - memory allocated in third chunk
10751795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
10761795a4d1SJed Brown .  r5 - memory allocated in fifth chunk
10771795a4d1SJed Brown -  r6 - memory allocated in sixth chunk
10781795a4d1SJed Brown 
10791795a4d1SJed Brown    Level: developer
10801795a4d1SJed Brown 
10811795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscMalloc6(), PetscFree6()
10821795a4d1SJed Brown 
10831795a4d1SJed Brown M*/
10842553b7ecSJed Brown #define PetscCalloc6(m1,r1,m2,r2,m3,r3,m4,r4,m5,r5,m6,r6) PetscMallocA(6,PETSC_TRUE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2),(size_t)(m3)*sizeof(**(r3)),(r3),(size_t)(m4)*sizeof(**(r4)),(r4),(size_t)(m5)*sizeof(**(r5)),(r5),(size_t)(m6)*sizeof(**(r6)),(r6))
10851795a4d1SJed Brown 
10861795a4d1SJed Brown /*MC
1087dcca6d9dSJed Brown    PetscMalloc7 - Allocates 7 arrays of memory, all aligned to PETSC_MEMALIGN
1088d382aafbSBarry Smith 
1089d382aafbSBarry Smith    Synopsis:
1090aaa7dc30SBarry Smith     #include <petscsys.h>
1091dcca6d9dSJed Brown    PetscErrorCode PetscMalloc7(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5,size_t m6,type **r6,size_t m7,type **r7)
1092d382aafbSBarry Smith 
1093d382aafbSBarry Smith    Not Collective
1094d382aafbSBarry Smith 
1095e28ce29aSPatrick Sanan    Input Parameters:
1096d382aafbSBarry Smith +  m1 - number of elements to allocate in 1st chunk  (may be zero)
1097d382aafbSBarry Smith .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
1098d382aafbSBarry Smith .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
1099d382aafbSBarry Smith .  m4 - number of elements to allocate in 4th chunk  (may be zero)
1100d382aafbSBarry Smith .  m5 - number of elements to allocate in 5th chunk  (may be zero)
1101d382aafbSBarry Smith .  m6 - number of elements to allocate in 6th chunk  (may be zero)
1102dcca6d9dSJed Brown -  m7 - number of elements to allocate in 7th chunk  (may be zero)
1103d382aafbSBarry Smith 
1104e28ce29aSPatrick Sanan    Output Parameters:
1105d382aafbSBarry Smith +  r1 - memory allocated in first chunk
1106d382aafbSBarry Smith .  r2 - memory allocated in second chunk
1107d382aafbSBarry Smith .  r3 - memory allocated in third chunk
1108d382aafbSBarry Smith .  r4 - memory allocated in fourth chunk
1109d382aafbSBarry Smith .  r5 - memory allocated in fifth chunk
1110d382aafbSBarry Smith .  r6 - memory allocated in sixth chunk
1111eca87e8dSBarry Smith -  r7 - memory allocated in seventh chunk
1112d382aafbSBarry Smith 
1113d382aafbSBarry Smith    Level: developer
1114d382aafbSBarry Smith 
11151795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscCalloc7(), PetscFree7()
1116d382aafbSBarry Smith 
1117d382aafbSBarry Smith M*/
11182553b7ecSJed Brown #define PetscMalloc7(m1,r1,m2,r2,m3,r3,m4,r4,m5,r5,m6,r6,m7,r7) PetscMallocA(7,PETSC_FALSE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2),(size_t)(m3)*sizeof(**(r3)),(r3),(size_t)(m4)*sizeof(**(r4)),(r4),(size_t)(m5)*sizeof(**(r5)),(r5),(size_t)(m6)*sizeof(**(r6)),(r6),(size_t)(m7)*sizeof(**(r7)),(r7))
1119d382aafbSBarry Smith 
1120d382aafbSBarry Smith /*MC
11211795a4d1SJed Brown    PetscCalloc7 - Allocates 7 cleared (zeroed) arrays of memory, all aligned to PETSC_MEMALIGN
11221795a4d1SJed Brown 
11231795a4d1SJed Brown    Synopsis:
1124aaa7dc30SBarry Smith     #include <petscsys.h>
11251795a4d1SJed Brown    PetscErrorCode PetscCalloc7(size_t m1,type **r1,size_t m2,type **r2,size_t m3,type **r3,size_t m4,type **r4,size_t m5,type **r5,size_t m6,type **r6,size_t m7,type **r7)
11261795a4d1SJed Brown 
11271795a4d1SJed Brown    Not Collective
11281795a4d1SJed Brown 
1129e28ce29aSPatrick Sanan    Input Parameters:
11301795a4d1SJed Brown +  m1 - number of elements to allocate in 1st chunk  (may be zero)
11311795a4d1SJed Brown .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
11321795a4d1SJed Brown .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
11331795a4d1SJed Brown .  m4 - number of elements to allocate in 4th chunk  (may be zero)
11341795a4d1SJed Brown .  m5 - number of elements to allocate in 5th chunk  (may be zero)
11351795a4d1SJed Brown .  m6 - number of elements to allocate in 6th chunk  (may be zero)
11361795a4d1SJed Brown -  m7 - number of elements to allocate in 7th chunk  (may be zero)
11371795a4d1SJed Brown 
1138e28ce29aSPatrick Sanan    Output Parameters:
11391795a4d1SJed Brown +  r1 - memory allocated in first chunk
11401795a4d1SJed Brown .  r2 - memory allocated in second chunk
11411795a4d1SJed Brown .  r3 - memory allocated in third chunk
11421795a4d1SJed Brown .  r4 - memory allocated in fourth chunk
11431795a4d1SJed Brown .  r5 - memory allocated in fifth chunk
11441795a4d1SJed Brown .  r6 - memory allocated in sixth chunk
11451795a4d1SJed Brown -  r7 - memory allocated in seventh chunk
11461795a4d1SJed Brown 
11471795a4d1SJed Brown    Level: developer
11481795a4d1SJed Brown 
11491795a4d1SJed Brown .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscMalloc7(), PetscFree7()
11501795a4d1SJed Brown 
11511795a4d1SJed Brown M*/
11522553b7ecSJed Brown #define PetscCalloc7(m1,r1,m2,r2,m3,r3,m4,r4,m5,r5,m6,r6,m7,r7) PetscMallocA(7,PETSC_TRUE,__LINE__,PETSC_FUNCTION_NAME,__FILE__,(size_t)(m1)*sizeof(**(r1)),(r1),(size_t)(m2)*sizeof(**(r2)),(r2),(size_t)(m3)*sizeof(**(r3)),(r3),(size_t)(m4)*sizeof(**(r4)),(r4),(size_t)(m5)*sizeof(**(r5)),(r5),(size_t)(m6)*sizeof(**(r6)),(r6),(size_t)(m7)*sizeof(**(r7)),(r7))
11531795a4d1SJed Brown 
11541795a4d1SJed Brown /*MC
1155503cfb0cSBarry Smith    PetscNew - Allocates memory of a particular type, zeros the memory! Aligned to PETSC_MEMALIGN
1156d382aafbSBarry Smith 
1157eca87e8dSBarry Smith    Synopsis:
1158aaa7dc30SBarry Smith     #include <petscsys.h>
1159b00a9115SJed Brown    PetscErrorCode PetscNew(type **result)
1160eca87e8dSBarry Smith 
1161eca87e8dSBarry Smith    Not Collective
1162eca87e8dSBarry Smith 
1163d382aafbSBarry Smith    Output Parameter:
1164b00a9115SJed Brown .  result - memory allocated, sized to match pointer type
1165d382aafbSBarry Smith 
1166d382aafbSBarry Smith    Level: beginner
1167d382aafbSBarry Smith 
1168390e1bf2SBarry Smith .seealso: PetscFree(), PetscMalloc(), PetscNewLog(), PetscCalloc1(), PetscMalloc1()
1169d382aafbSBarry Smith 
1170d382aafbSBarry Smith M*/
1171b00a9115SJed Brown #define PetscNew(b)      PetscCalloc1(1,(b))
1172ad0619ddSBarry Smith 
1173ad0619ddSBarry Smith /*MC
1174b00a9115SJed Brown    PetscNewLog - Allocates memory of a type matching pointer, zeros the memory! Aligned to PETSC_MEMALIGN. Associates the memory allocated
1175ad0619ddSBarry Smith          with the given object using PetscLogObjectMemory().
1176ad0619ddSBarry Smith 
1177ad0619ddSBarry Smith    Synopsis:
1178aaa7dc30SBarry Smith     #include <petscsys.h>
1179b00a9115SJed Brown    PetscErrorCode PetscNewLog(PetscObject obj,type **result)
1180ad0619ddSBarry Smith 
1181ad0619ddSBarry Smith    Not Collective
1182ad0619ddSBarry Smith 
1183ad0619ddSBarry Smith    Input Parameter:
1184b00a9115SJed Brown .  obj - object memory is logged to
1185ad0619ddSBarry Smith 
1186ad0619ddSBarry Smith    Output Parameter:
1187b00a9115SJed Brown .  result - memory allocated, sized to match pointer type
1188ad0619ddSBarry Smith 
1189ad0619ddSBarry Smith    Level: developer
1190ad0619ddSBarry Smith 
1191390e1bf2SBarry Smith .seealso: PetscFree(), PetscMalloc(), PetscNew(), PetscLogObjectMemory(), PetscCalloc1(), PetscMalloc1()
1192ad0619ddSBarry Smith 
1193ad0619ddSBarry Smith M*/
1194d12f57a0SLisandro Dalcin #define PetscNewLog(o,b) (PetscNew((b)) || PetscLogObjectMemory((PetscObject)o,sizeof(**(b))))
1195d382aafbSBarry Smith 
1196d382aafbSBarry Smith /*MC
1197d382aafbSBarry Smith    PetscFree - Frees memory
1198d382aafbSBarry Smith 
1199eca87e8dSBarry Smith    Synopsis:
1200aaa7dc30SBarry Smith     #include <petscsys.h>
1201eca87e8dSBarry Smith    PetscErrorCode PetscFree(void *memory)
1202eca87e8dSBarry Smith 
1203eca87e8dSBarry Smith    Not Collective
1204eca87e8dSBarry Smith 
1205d382aafbSBarry Smith    Input Parameter:
12067f4976b7SJacob Faibussowitsch .   memory - memory to free (the pointer is ALWAYS set to NULL upon success)
1207d382aafbSBarry Smith 
1208d382aafbSBarry Smith    Level: beginner
1209d382aafbSBarry Smith 
121049d7da52SJed Brown    Notes:
1211390e1bf2SBarry Smith    Do not free memory obtained with PetscMalloc2(), PetscCalloc2() etc, they must be freed with PetscFree2() etc.
1212390e1bf2SBarry Smith 
121349d7da52SJed Brown    It is safe to call PetscFree() on a NULL pointer.
1214d382aafbSBarry Smith 
1215390e1bf2SBarry Smith .seealso: PetscNew(), PetscMalloc(), PetscNewLog(), PetscMalloc1(), PetscCalloc1()
1216d382aafbSBarry Smith 
1217d382aafbSBarry Smith M*/
1218ffc31a0eSLisandro Dalcin #define PetscFree(a)   ((*PetscTrFree)((void*)(a),__LINE__,PETSC_FUNCTION_NAME,__FILE__) || ((a) = NULL,0))
1219d382aafbSBarry Smith 
1220d382aafbSBarry Smith /*MC
1221d382aafbSBarry Smith    PetscFree2 - Frees 2 chunks of memory obtained with PetscMalloc2()
1222d382aafbSBarry Smith 
1223eca87e8dSBarry Smith    Synopsis:
1224aaa7dc30SBarry Smith     #include <petscsys.h>
1225eca87e8dSBarry Smith    PetscErrorCode PetscFree2(void *memory1,void *memory2)
1226eca87e8dSBarry Smith 
1227eca87e8dSBarry Smith    Not Collective
1228eca87e8dSBarry Smith 
1229e28ce29aSPatrick Sanan    Input Parameters:
1230d382aafbSBarry Smith +   memory1 - memory to free
1231d382aafbSBarry Smith -   memory2 - 2nd memory to free
1232d382aafbSBarry Smith 
1233d382aafbSBarry Smith    Level: developer
1234d382aafbSBarry Smith 
123595452b02SPatrick Sanan    Notes:
123695452b02SPatrick Sanan     Memory must have been obtained with PetscMalloc2()
1237d382aafbSBarry Smith 
1238d382aafbSBarry Smith .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree()
1239d382aafbSBarry Smith 
1240d382aafbSBarry Smith M*/
1241ba282f50SJed Brown #define PetscFree2(m1,m2)   PetscFreeA(2,__LINE__,PETSC_FUNCTION_NAME,__FILE__,&(m1),&(m2))
1242d382aafbSBarry Smith 
1243d382aafbSBarry Smith /*MC
1244d382aafbSBarry Smith    PetscFree3 - Frees 3 chunks of memory obtained with PetscMalloc3()
1245d382aafbSBarry Smith 
1246eca87e8dSBarry Smith    Synopsis:
1247aaa7dc30SBarry Smith     #include <petscsys.h>
1248eca87e8dSBarry Smith    PetscErrorCode PetscFree3(void *memory1,void *memory2,void *memory3)
1249eca87e8dSBarry Smith 
1250eca87e8dSBarry Smith    Not Collective
1251eca87e8dSBarry Smith 
1252e28ce29aSPatrick Sanan    Input Parameters:
1253d382aafbSBarry Smith +   memory1 - memory to free
1254d382aafbSBarry Smith .   memory2 - 2nd memory to free
1255d382aafbSBarry Smith -   memory3 - 3rd memory to free
1256d382aafbSBarry Smith 
1257d382aafbSBarry Smith    Level: developer
1258d382aafbSBarry Smith 
125995452b02SPatrick Sanan    Notes:
126095452b02SPatrick Sanan     Memory must have been obtained with PetscMalloc3()
1261d382aafbSBarry Smith 
1262d382aafbSBarry Smith .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree(), PetscMalloc3()
1263d382aafbSBarry Smith 
1264d382aafbSBarry Smith M*/
1265ba282f50SJed Brown #define PetscFree3(m1,m2,m3)   PetscFreeA(3,__LINE__,PETSC_FUNCTION_NAME,__FILE__,&(m1),&(m2),&(m3))
1266d382aafbSBarry Smith 
1267d382aafbSBarry Smith /*MC
1268d382aafbSBarry Smith    PetscFree4 - Frees 4 chunks of memory obtained with PetscMalloc4()
1269d382aafbSBarry Smith 
1270eca87e8dSBarry Smith    Synopsis:
1271aaa7dc30SBarry Smith     #include <petscsys.h>
1272eca87e8dSBarry Smith    PetscErrorCode PetscFree4(void *m1,void *m2,void *m3,void *m4)
1273eca87e8dSBarry Smith 
1274eca87e8dSBarry Smith    Not Collective
1275eca87e8dSBarry Smith 
1276e28ce29aSPatrick Sanan    Input Parameters:
1277d382aafbSBarry Smith +   m1 - memory to free
1278d382aafbSBarry Smith .   m2 - 2nd memory to free
1279d382aafbSBarry Smith .   m3 - 3rd memory to free
1280d382aafbSBarry Smith -   m4 - 4th memory to free
1281d382aafbSBarry Smith 
1282d382aafbSBarry Smith    Level: developer
1283d382aafbSBarry Smith 
128495452b02SPatrick Sanan    Notes:
128595452b02SPatrick Sanan     Memory must have been obtained with PetscMalloc4()
1286d382aafbSBarry Smith 
1287d382aafbSBarry Smith .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree(), PetscMalloc3(), PetscMalloc4()
1288d382aafbSBarry Smith 
1289d382aafbSBarry Smith M*/
1290ba282f50SJed Brown #define PetscFree4(m1,m2,m3,m4)   PetscFreeA(4,__LINE__,PETSC_FUNCTION_NAME,__FILE__,&(m1),&(m2),&(m3),&(m4))
1291d382aafbSBarry Smith 
1292d382aafbSBarry Smith /*MC
1293d382aafbSBarry Smith    PetscFree5 - Frees 5 chunks of memory obtained with PetscMalloc5()
1294d382aafbSBarry Smith 
1295eca87e8dSBarry Smith    Synopsis:
1296aaa7dc30SBarry Smith     #include <petscsys.h>
1297eca87e8dSBarry Smith    PetscErrorCode PetscFree5(void *m1,void *m2,void *m3,void *m4,void *m5)
1298eca87e8dSBarry Smith 
1299eca87e8dSBarry Smith    Not Collective
1300eca87e8dSBarry Smith 
1301e28ce29aSPatrick Sanan    Input Parameters:
1302d382aafbSBarry Smith +   m1 - memory to free
1303d382aafbSBarry Smith .   m2 - 2nd memory to free
1304d382aafbSBarry Smith .   m3 - 3rd memory to free
1305d382aafbSBarry Smith .   m4 - 4th memory to free
1306d382aafbSBarry Smith -   m5 - 5th memory to free
1307d382aafbSBarry Smith 
1308d382aafbSBarry Smith    Level: developer
1309d382aafbSBarry Smith 
131095452b02SPatrick Sanan    Notes:
131195452b02SPatrick Sanan     Memory must have been obtained with PetscMalloc5()
1312d382aafbSBarry Smith 
1313d382aafbSBarry Smith .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree(), PetscMalloc3(), PetscMalloc4(), PetscMalloc5()
1314d382aafbSBarry Smith 
1315d382aafbSBarry Smith M*/
1316ba282f50SJed Brown #define PetscFree5(m1,m2,m3,m4,m5)   PetscFreeA(5,__LINE__,PETSC_FUNCTION_NAME,__FILE__,&(m1),&(m2),&(m3),&(m4),&(m5))
1317d382aafbSBarry Smith 
1318d382aafbSBarry Smith /*MC
1319d382aafbSBarry Smith    PetscFree6 - Frees 6 chunks of memory obtained with PetscMalloc6()
1320d382aafbSBarry Smith 
1321eca87e8dSBarry Smith    Synopsis:
1322aaa7dc30SBarry Smith     #include <petscsys.h>
1323eca87e8dSBarry Smith    PetscErrorCode PetscFree6(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6)
1324eca87e8dSBarry Smith 
1325eca87e8dSBarry Smith    Not Collective
1326eca87e8dSBarry Smith 
1327e28ce29aSPatrick Sanan    Input Parameters:
1328d382aafbSBarry Smith +   m1 - memory to free
1329d382aafbSBarry Smith .   m2 - 2nd memory to free
1330d382aafbSBarry Smith .   m3 - 3rd memory to free
1331d382aafbSBarry Smith .   m4 - 4th memory to free
1332d382aafbSBarry Smith .   m5 - 5th memory to free
1333d382aafbSBarry Smith -   m6 - 6th memory to free
1334d382aafbSBarry Smith 
1335d382aafbSBarry Smith    Level: developer
1336d382aafbSBarry Smith 
133795452b02SPatrick Sanan    Notes:
133895452b02SPatrick Sanan     Memory must have been obtained with PetscMalloc6()
1339d382aafbSBarry Smith 
1340d382aafbSBarry Smith .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree(), PetscMalloc3(), PetscMalloc4(), PetscMalloc5(), PetscMalloc6()
1341d382aafbSBarry Smith 
1342d382aafbSBarry Smith M*/
1343ba282f50SJed Brown #define PetscFree6(m1,m2,m3,m4,m5,m6)   PetscFreeA(6,__LINE__,PETSC_FUNCTION_NAME,__FILE__,&(m1),&(m2),&(m3),&(m4),&(m5),&(m6))
1344d382aafbSBarry Smith 
1345d382aafbSBarry Smith /*MC
1346d382aafbSBarry Smith    PetscFree7 - Frees 7 chunks of memory obtained with PetscMalloc7()
1347d382aafbSBarry Smith 
1348eca87e8dSBarry Smith    Synopsis:
1349aaa7dc30SBarry Smith     #include <petscsys.h>
1350eca87e8dSBarry Smith    PetscErrorCode PetscFree7(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6,void *m7)
1351eca87e8dSBarry Smith 
1352eca87e8dSBarry Smith    Not Collective
1353eca87e8dSBarry Smith 
1354e28ce29aSPatrick Sanan    Input Parameters:
1355d382aafbSBarry Smith +   m1 - memory to free
1356d382aafbSBarry Smith .   m2 - 2nd memory to free
1357d382aafbSBarry Smith .   m3 - 3rd memory to free
1358d382aafbSBarry Smith .   m4 - 4th memory to free
1359d382aafbSBarry Smith .   m5 - 5th memory to free
1360d382aafbSBarry Smith .   m6 - 6th memory to free
1361d382aafbSBarry Smith -   m7 - 7th memory to free
1362d382aafbSBarry Smith 
1363d382aafbSBarry Smith    Level: developer
1364d382aafbSBarry Smith 
136595452b02SPatrick Sanan    Notes:
136695452b02SPatrick Sanan     Memory must have been obtained with PetscMalloc7()
1367d382aafbSBarry Smith 
1368d382aafbSBarry Smith .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree(), PetscMalloc3(), PetscMalloc4(), PetscMalloc5(), PetscMalloc6(),
1369d382aafbSBarry Smith           PetscMalloc7()
1370d382aafbSBarry Smith 
1371d382aafbSBarry Smith M*/
1372ba282f50SJed Brown #define PetscFree7(m1,m2,m3,m4,m5,m6,m7)   PetscFreeA(7,__LINE__,PETSC_FUNCTION_NAME,__FILE__,&(m1),&(m2),&(m3),&(m4),&(m5),&(m6),&(m7))
1373d382aafbSBarry Smith 
1374ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocA(int,PetscBool,int,const char *,const char *,size_t,void *,...);
1375ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscFreeA(int,int,const char *,const char *,void *,...);
1376071fcb05SBarry Smith PETSC_EXTERN PetscErrorCode (*PetscTrMalloc)(size_t,PetscBool,int,const char[],const char[],void**);
1377efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode (*PetscTrFree)(void*,int,const char[],const char[]);
13783221ece2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode (*PetscTrRealloc)(size_t,int,const char[],const char[],void**);
1379ba282f50SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocSetCoalesce(PetscBool);
138092f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocSet(PetscErrorCode (*)(size_t,PetscBool,int,const char[],const char[],void**),PetscErrorCode (*)(void*,int,const char[],const char[]),PetscErrorCode (*)(size_t,int,const char[],const char[], void **));
1381014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocClear(void);
1382d382aafbSBarry Smith 
1383d382aafbSBarry Smith /*
1384c1706be6SHong Zhang   Unlike PetscMallocSet and PetscMallocClear which overwrite the existing settings, these two functions save the previous choice of allocator, and should be used in pair.
1385c1706be6SHong Zhang */
1386c1706be6SHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocSetDRAM(void);
1387c1706be6SHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocResetDRAM(void);
138831f06eaaSHong Zhang #if defined(PETSC_HAVE_CUDA)
138931f06eaaSHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocSetCUDAHost(void);
139031f06eaaSHong Zhang PETSC_EXTERN PetscErrorCode PetscMallocResetCUDAHost(void);
139131f06eaaSHong Zhang #endif
139259af0bd3SScott Kruger #if defined(PETSC_HAVE_HIP)
139359af0bd3SScott Kruger PETSC_EXTERN PetscErrorCode PetscMallocSetHIPHost(void);
139459af0bd3SScott Kruger PETSC_EXTERN PetscErrorCode PetscMallocResetHIPHost(void);
139559af0bd3SScott Kruger #endif
139659af0bd3SScott Kruger 
1397a5057860SBarry Smith #define MPIU_PETSCLOGDOUBLE  MPI_DOUBLE
139836763ca0SBas van 't Hof #define MPIU_2PETSCLOGDOUBLE MPI_2DOUBLE_PRECISION
1399a5057860SBarry Smith 
1400a5057860SBarry Smith /*
140166d669d6SBarry Smith    Routines for tracing memory corruption/bleeding with default PETSc memory allocation
1402d382aafbSBarry Smith */
1403014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocDump(FILE *);
140492f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocView(FILE *);
1405014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocGetCurrentUsage(PetscLogDouble *);
1406014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMallocGetMaximumUsage(PetscLogDouble *);
1407e3ed9ee7SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocPushMaximumUsage(int);
1408e3ed9ee7SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocPopMaximumUsage(int,PetscLogDouble*);
140992f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocSetDebug(PetscBool,PetscBool);
141092f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocGetDebug(PetscBool*,PetscBool*,PetscBool*);
1411efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscMallocValidate(int,const char[],const char[]);
141292f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocViewSet(PetscLogDouble);
141392f119d6SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocViewGet(PetscBool*);
1414608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeSet(PetscBool);
1415608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocLogRequestedSizeGet(PetscBool*);
1416d382aafbSBarry Smith 
14176a6fc655SJed Brown PETSC_EXTERN const char *const PetscDataTypes[];
1418014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDataTypeToMPIDataType(PetscDataType,MPI_Datatype*);
1419014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIDataTypeToPetscDataType(MPI_Datatype,PetscDataType*);
1420014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDataTypeGetSize(PetscDataType,size_t*);
14218e4b2d1dSBarry Smith PETSC_EXTERN PetscErrorCode PetscDataTypeFromString(const char*,PetscDataType*,PetscBool*);
1422d382aafbSBarry Smith 
1423d382aafbSBarry Smith /*
1424d382aafbSBarry Smith     Basic memory and string operations. These are usually simple wrappers
1425d382aafbSBarry Smith    around the basic Unix system calls, but a few of them have additional
1426d382aafbSBarry Smith    functionality and/or error checking.
1427d382aafbSBarry Smith */
1428014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemcmp(const void*,const void*,size_t,PetscBool  *);
1429014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrlen(const char[],size_t*);
1430d67fe73bSBarry Smith PETSC_EXTERN PetscErrorCode PetscStrToArray(const char[],char,int*,char ***);
1431014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrToArrayDestroy(int,char **);
1432014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrcmp(const char[],const char[],PetscBool  *);
1433014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrgrt(const char[],const char[],PetscBool  *);
1434014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrcasecmp(const char[],const char[],PetscBool *);
1435014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrncmp(const char[],const char[],size_t,PetscBool *);
1436014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrcpy(char[],const char[]);
1437014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrcat(char[],const char[]);
1438a126751eSBarry Smith PETSC_EXTERN PetscErrorCode PetscStrlcat(char[],const char[],size_t);
1439014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrncpy(char[],const char[],size_t);
1440014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrchr(const char[],char,char *[]);
1441014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrtolower(char[]);
14422f234a98SBarry Smith PETSC_EXTERN PetscErrorCode PetscStrtoupper(char[]);
1443014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrrchr(const char[],char,char *[]);
1444014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrstr(const char[],const char[],char *[]);
1445014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrrstr(const char[],const char[],char *[]);
1446014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrendswith(const char[],const char[],PetscBool*);
14472c9581d2SBarry Smith PETSC_EXTERN PetscErrorCode PetscStrbeginswith(const char[],const char[],PetscBool*);
1448014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrendswithwhich(const char[],const char *const*,PetscInt*);
1449014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrallocpy(const char[],char *[]);
145047340559SBarry Smith PETSC_EXTERN PetscErrorCode PetscStrArrayallocpy(const char *const*,char***);
14516fed8037SJed Brown PETSC_EXTERN PetscErrorCode PetscStrArrayDestroy(char***);
14526991f827SBarry Smith PETSC_EXTERN PetscErrorCode PetscStrNArrayallocpy(PetscInt,const char *const*,char***);
14536991f827SBarry Smith PETSC_EXTERN PetscErrorCode PetscStrNArrayDestroy(PetscInt,char***);
1454014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStrreplace(MPI_Comm,const char[],char[],size_t);
1455d382aafbSBarry Smith 
1456573b0fb4SBarry Smith PETSC_EXTERN void PetscStrcmpNoError(const char[],const char[],PetscBool  *);
1457573b0fb4SBarry Smith 
1458014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTokenCreate(const char[],const char,PetscToken*);
1459014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTokenFind(PetscToken,char *[]);
1460014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTokenDestroy(PetscToken*);
1461d382aafbSBarry Smith 
1462e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscStrInList(const char[],const char[],char,PetscBool*);
1463a53986e1SJed Brown PETSC_EXTERN PetscErrorCode PetscEListFind(PetscInt,const char *const*,const char*,PetscInt*,PetscBool*);
1464a53986e1SJed Brown PETSC_EXTERN PetscErrorCode PetscEnumFind(const char *const*,const char*,PetscEnum*,PetscBool*);
1465a53986e1SJed Brown 
1466d382aafbSBarry Smith /*
1467d382aafbSBarry Smith    These are MPI operations for MPI_Allreduce() etc
1468d382aafbSBarry Smith */
1469367daffbSBarry Smith PETSC_EXTERN MPI_Op MPIU_MAXSUM_OP;
1470570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
1471de272c7aSSatish Balay PETSC_EXTERN MPI_Op MPIU_SUM;
1472014dd563SJed Brown PETSC_EXTERN MPI_Op MPIU_MAX;
1473014dd563SJed Brown PETSC_EXTERN MPI_Op MPIU_MIN;
1474d9822059SBarry Smith #else
1475de272c7aSSatish Balay #define MPIU_SUM MPI_SUM
1476d9822059SBarry Smith #define MPIU_MAX MPI_MAX
1477d9822059SBarry Smith #define MPIU_MIN MPI_MIN
1478d9822059SBarry Smith #endif
1479014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMaxSum(MPI_Comm,const PetscInt[],PetscInt*,PetscInt*);
1480d382aafbSBarry Smith 
1481014dd563SJed Brown PETSC_EXTERN PetscErrorCode MPIULong_Send(void*,PetscInt,MPI_Datatype,PetscMPIInt,PetscMPIInt,MPI_Comm);
1482014dd563SJed Brown PETSC_EXTERN PetscErrorCode MPIULong_Recv(void*,PetscInt,MPI_Datatype,PetscMPIInt,PetscMPIInt,MPI_Comm);
1483eb3f98d2SBarry Smith 
148495c0884eSLisandro Dalcin PETSC_EXTERN const char *const PetscFileModes[];
1485d6a4318aSJed Brown 
1486639ff905SBarry Smith /*
1487639ff905SBarry Smith     Defines PETSc error handling.
1488639ff905SBarry Smith */
1489639ff905SBarry Smith #include <petscerror.h>
1490d382aafbSBarry Smith 
14910700a824SBarry Smith #define PETSC_SMALLEST_CLASSID  1211211
1492014dd563SJed Brown PETSC_EXTERN PetscClassId PETSC_LARGEST_CLASSID;
1493014dd563SJed Brown PETSC_EXTERN PetscClassId PETSC_OBJECT_CLASSID;
1494014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscClassIdRegister(const char[],PetscClassId *);
149581256985SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscObjectGetId(PetscObject,PetscObjectId*);
149681256985SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscObjectCompareId(PetscObject,PetscObjectId,PetscBool*);
149781256985SMatthew G. Knepley 
1498d382aafbSBarry Smith /*
1499d382aafbSBarry Smith    Routines that get memory usage information from the OS
1500d382aafbSBarry Smith */
1501014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemoryGetCurrentUsage(PetscLogDouble *);
1502014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemoryGetMaximumUsage(PetscLogDouble *);
1503014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMemorySetGetMaximumUsage(void);
1504b44d5720SBarry Smith PETSC_EXTERN PetscErrorCode PetscMemoryTrace(const char[]);
1505d382aafbSBarry Smith 
1506014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSleep(PetscReal);
1507d382aafbSBarry Smith 
1508d382aafbSBarry Smith /*
1509d382aafbSBarry Smith    Initialization of PETSc
1510d382aafbSBarry Smith */
1511014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitialize(int*,char***,const char[],const char[]);
1512014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeNoPointers(int,char**,const char[],const char[]);
1513014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeNoArguments(void);
1514014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitialized(PetscBool *);
1515014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFinalized(PetscBool *);
1516014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFinalize(void);
1517014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInitializeFortran(void);
1518014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArgs(int*,char ***);
1519014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArguments(char ***);
1520014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFreeArguments(char **);
1521d382aafbSBarry Smith 
1522014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscEnd(void);
1523607a6623SBarry Smith PETSC_EXTERN PetscErrorCode PetscSysInitializePackage(void);
1524d382aafbSBarry Smith 
1525014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonInitialize(const char[],const char[]);
1526014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonFinalize(void);
1527014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonPrintError(void);
1528014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPythonMonitorSet(PetscObject,const char[]);
1529d382aafbSBarry Smith 
1530a50e088cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMonitorCompare(PetscErrorCode (*)(void),void *,PetscErrorCode (*)(void**),PetscErrorCode (*)(void),void *,PetscErrorCode (*)(void**),PetscBool *);
1531a50e088cSMatthew G. Knepley 
1532d382aafbSBarry Smith /*
1533d382aafbSBarry Smith      These are so that in extern C code we can caste function pointers to non-extern C
15342981ebdbSBarry Smith    function pointers. Since the regular C++ code expects its function pointers to be C++
1535d382aafbSBarry Smith */
1536638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef void (**PetscVoidStarFunction)(void);
1537638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef void (*PetscVoidFunction)(void);
1538638cfed1SJed Brown PETSC_EXTERN_TYPEDEF typedef PetscErrorCode (*PetscErrorCodeFunction)(void);
1539d382aafbSBarry Smith 
1540d382aafbSBarry Smith /*
1541d382aafbSBarry Smith     Functions that can act on any PETSc object.
1542d382aafbSBarry Smith */
1543014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectDestroy(PetscObject*);
1544014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetComm(PetscObject,MPI_Comm *);
1545014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetClassId(PetscObject,PetscClassId *);
1546014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetClassName(PetscObject,const char *[]);
1547014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetType(PetscObject,const char []);
1548014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetType(PetscObject,const char *[]);
1549014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetName(PetscObject,const char[]);
1550014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetName(PetscObject,const char*[]);
1551014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetTabLevel(PetscObject,PetscInt);
1552014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetTabLevel(PetscObject,PetscInt*);
1553014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectIncrementTabLevel(PetscObject,PetscObject,PetscInt);
1554014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectReference(PetscObject);
1555014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetReference(PetscObject,PetscInt*);
1556014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectDereference(PetscObject);
1557014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetNewTag(PetscObject,PetscMPIInt*);
1558014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectCompose(PetscObject,const char[],PetscObject);
1559014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRemoveReference(PetscObject,const char[]);
1560014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectQuery(PetscObject,const char[],PetscObject*);
1561bdf89e91SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectComposeFunction_Private(PetscObject,const char[],void (*)(void));
1562bdf89e91SBarry Smith #define PetscObjectComposeFunction(a,b,d) PetscObjectComposeFunction_Private(a,b,(PetscVoidFunction)(d))
1563014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetFromOptions(PetscObject);
1564014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetUp(PetscObject);
15650eb63584SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSetPrintedOptions(PetscObject);
15660eb63584SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectInheritPrintedOptions(PetscObject,PetscObject);
1567014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscCommGetNewTag(MPI_Comm,PetscMPIInt*);
1568d382aafbSBarry Smith 
1569665c2dedSJed Brown #include <petscviewertypes.h>
1570639ff905SBarry Smith #include <petscoptions.h>
1571639ff905SBarry Smith 
1572608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocTraceSet(PetscViewer,PetscBool,PetscLogDouble);
1573608c71bfSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscMallocTraceGet(PetscBool*);
1574608c71bfSMatthew G. Knepley 
15750633abcbSJed Brown PETSC_EXTERN PetscErrorCode PetscObjectsListGetGlobalNumbering(MPI_Comm,PetscInt,PetscObject*,PetscInt*,PetscInt*);
15760633abcbSJed Brown 
1577c5e4d11fSDmitry Karpeev PETSC_EXTERN PetscErrorCode PetscMemoryView(PetscViewer,const char[]);
1578dae58748SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectPrintClassNamePrefixType(PetscObject,PetscViewer);
1579639ff905SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectView(PetscObject,PetscViewer);
15800005d66cSJed Brown #define PetscObjectQueryFunction(obj,name,fptr) PetscObjectQueryFunction_Private((obj),(name),(PetscVoidFunction*)(fptr))
15810005d66cSJed Brown PETSC_EXTERN PetscErrorCode PetscObjectQueryFunction_Private(PetscObject,const char[],void (**)(void));
1582014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectSetOptionsPrefix(PetscObject,const char[]);
1583014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectAppendOptionsPrefix(PetscObject,const char[]);
1584014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectPrependOptionsPrefix(PetscObject,const char[]);
1585014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectGetOptionsPrefix(PetscObject,const char*[]);
1586014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectChangeTypeName(PetscObject,const char[]);
1587014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroy(PetscObject);
1588014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectRegisterDestroyAll(void);
1589685405a1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectViewFromOptions(PetscObject,PetscObject,const char[]);
1590014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectName(PetscObject);
1591014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectTypeCompare(PetscObject,const char[],PetscBool *);
15924099cc6bSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompare(PetscObject,const char[],PetscBool *);
1593014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscObjectTypeCompareAny(PetscObject,PetscBool*,const char[],...);
1594b9e7e5c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject,PetscBool*,const char[],...);
1595014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*)(void));
1596014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRegisterFinalizeAll(void);
1597d382aafbSBarry Smith 
1598e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
15997aab2a10SBarry Smith PETSC_EXTERN PetscErrorCode PetscSAWsBlock(void);
1600e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsViewOff(PetscObject);
1601e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsSetBlock(PetscObject,PetscBool);
1602e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsBlock(PetscObject);
1603e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsGrantAccess(PetscObject);
1604e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectSAWsTakeAccess(PetscObject);
1605e04113cfSBarry Smith PETSC_EXTERN void           PetscStackSAWsGrantAccess(void);
1606e04113cfSBarry Smith PETSC_EXTERN void           PetscStackSAWsTakeAccess(void);
1607e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscStackViewSAWs(void);
1608e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscStackSAWsViewOff(void);
160915681b3cSBarry Smith 
1610ec7429eaSBarry Smith #else
16117aab2a10SBarry Smith #define PetscSAWsBlock()                        0
1612e04113cfSBarry Smith #define PetscObjectSAWsViewOff(obj)             0
1613e04113cfSBarry Smith #define PetscObjectSAWsSetBlock(obj,flg)        0
1614e04113cfSBarry Smith #define PetscObjectSAWsBlock(obj)               0
1615e04113cfSBarry Smith #define PetscObjectSAWsGrantAccess(obj)         0
1616e04113cfSBarry Smith #define PetscObjectSAWsTakeAccess(obj)          0
1617e04113cfSBarry Smith #define PetscStackViewSAWs()                    0
1618e04113cfSBarry Smith #define PetscStackSAWsViewOff()                 0
1619e04113cfSBarry Smith #define PetscStackSAWsTakeAccess()
1620e04113cfSBarry Smith #define PetscStackSAWsGrantAccess()
162115681b3cSBarry Smith 
1622ec7429eaSBarry Smith #endif
1623b90c6cbeSBarry Smith 
162482b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLOpen(const char[],PetscDLMode,PetscDLHandle *);
162582b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLClose(PetscDLHandle *);
162682b97d80SJed Brown PETSC_EXTERN PetscErrorCode PetscDLSym(PetscDLHandle,const char[],void **);
1627258ec3d2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscDLAddr(void (*)(void), char **);
1628258ec3d2SMatthew G. Knepley #ifdef PETSC_HAVE_CXX
1629258ec3d2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscDemangleSymbol(const char *, char **);
1630258ec3d2SMatthew G. Knepley #endif
16317d5d4d99SBarry Smith 
16325fe58b6fSBarry Smith #if defined(PETSC_USE_DEBUG)
1633a64a8e02SBarry Smith PETSC_EXTERN PetscErrorCode PetscMallocGetStack(void*,PetscStack**);
16345fe58b6fSBarry Smith #endif
16357eb1d149SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectsDump(FILE*,PetscBool);
1636a64a8e02SBarry Smith 
1637140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListDestroy(PetscObjectList*);
1638140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListFind(PetscObjectList,const char[],PetscObject*);
1639140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListReverseFind(PetscObjectList,PetscObject,char**,PetscBool*);
1640140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListAdd(PetscObjectList *,const char[],PetscObject);
1641140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListRemoveReference(PetscObjectList *,const char[]);
1642140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscObjectListDuplicate(PetscObjectList,PetscObjectList *);
1643d382aafbSBarry Smith 
1644d382aafbSBarry Smith /*
1645503cfb0cSBarry Smith     Dynamic library lists. Lists of names of routines in objects or in dynamic
1646d382aafbSBarry Smith   link libraries that will be loaded as needed.
1647d382aafbSBarry Smith */
1648a240a19fSJed Brown 
1649a240a19fSJed Brown #define PetscFunctionListAdd(list,name,fptr) PetscFunctionListAdd_Private((list),(name),(PetscVoidFunction)(fptr))
1650a240a19fSJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListAdd_Private(PetscFunctionList*,const char[],void (*)(void));
1651140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListDestroy(PetscFunctionList*);
16521c9cd337SJed Brown #define PetscFunctionListFind(list,name,fptr) PetscFunctionListFind_Private((list),(name),(PetscVoidFunction*)(fptr))
16531c9cd337SJed Brown PETSC_EXTERN PetscErrorCode PetscFunctionListFind_Private(PetscFunctionList,const char[],void (**)(void));
165444ef3d73SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListPrintTypes(MPI_Comm,FILE*,const char[],const char[],const char[],const char[],PetscFunctionList,const char[],const char[]);
1655140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListDuplicate(PetscFunctionList,PetscFunctionList *);
1656140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListView(PetscFunctionList,PetscViewer);
1657140e18c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFunctionListGet(PetscFunctionList,const char ***,int*);
1658d382aafbSBarry Smith 
1659014dd563SJed Brown PETSC_EXTERN PetscDLLibrary  PetscDLLibrariesLoaded;
1660014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryAppend(MPI_Comm,PetscDLLibrary *,const char[]);
1661014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryPrepend(MPI_Comm,PetscDLLibrary *,const char[]);
1662014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibrarySym(MPI_Comm,PetscDLLibrary *,const char[],const char[],void **);
1663014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryPrintPath(PetscDLLibrary);
1664014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryRetrieve(MPI_Comm,const char[],char *,size_t,PetscBool  *);
1665014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryOpen(MPI_Comm,const char[],PetscDLLibrary *);
1666014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDLLibraryClose(PetscDLLibrary);
1667d382aafbSBarry Smith 
1668d382aafbSBarry Smith /*
1669d382aafbSBarry Smith      Useful utility routines
1670d382aafbSBarry Smith */
1671014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSplitOwnership(MPI_Comm,PetscInt*,PetscInt*);
1672014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSplitOwnershipBlock(MPI_Comm,PetscInt,PetscInt*,PetscInt*);
1673d24d4204SJose E. Roman PETSC_EXTERN PetscErrorCode PetscSplitOwnershipEqual(MPI_Comm,PetscInt*,PetscInt*);
1674014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSequentialPhaseBegin(MPI_Comm,PetscMPIInt);
1675014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSequentialPhaseEnd(MPI_Comm,PetscMPIInt);
1676014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBarrier(PetscObject);
1677014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIDump(FILE*);
1678dd438433SSatish Balay PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxInt(MPI_Comm,PetscInt[2],PetscInt[2]);
1679dd438433SSatish Balay PETSC_EXTERN PetscErrorCode PetscGlobalMinMaxReal(MPI_Comm,PetscReal[2],PetscReal[2]);
1680d382aafbSBarry Smith 
168164ac3b0dSPatrick Sanan /*MC
1682ace3abfcSBarry Smith     PetscNot - negates a logical type value and returns result as a PetscBool
1683503cfb0cSBarry Smith 
1684e28ce29aSPatrick Sanan     Notes:
1685e28ce29aSPatrick Sanan     This is useful in cases like
1686503cfb0cSBarry Smith $     int        *a;
1687ace3abfcSBarry Smith $     PetscBool  flag = PetscNot(a)
16881cc8b206SBarry Smith      where !a would not return a PetscBool because we cannot provide a cast from int to PetscBool in C.
168964ac3b0dSPatrick Sanan 
169064ac3b0dSPatrick Sanan     Level: beginner
169164ac3b0dSPatrick Sanan 
169264ac3b0dSPatrick Sanan     .seealso : PetscBool, PETSC_TRUE, PETSC_FALSE
169364ac3b0dSPatrick Sanan M*/
1694d382aafbSBarry Smith #define PetscNot(a) ((a) ? PETSC_FALSE : PETSC_TRUE)
1695503cfb0cSBarry Smith 
1696d382aafbSBarry Smith /*MC
1697d382aafbSBarry Smith     PetscHelpPrintf - Prints help messages.
1698d382aafbSBarry Smith 
1699d382aafbSBarry Smith    Synopsis:
1700aaa7dc30SBarry Smith     #include <petscsys.h>
1701659f7ba0SBarry Smith      PetscErrorCode (*PetscHelpPrintf)(MPI_Comm comm, const char format[],args);
1702d382aafbSBarry Smith 
1703659f7ba0SBarry Smith     Collective on comm
1704eca87e8dSBarry Smith 
1705d382aafbSBarry Smith     Input Parameters:
1706659f7ba0SBarry Smith +  comm - the MPI communicator over which the help message is printed
1707d382aafbSBarry Smith .  format - the usual printf() format string
1708659f7ba0SBarry Smith -  args - arguments to be printed
1709d382aafbSBarry Smith 
1710d382aafbSBarry Smith    Level: developer
1711d382aafbSBarry Smith 
1712d382aafbSBarry Smith    Fortran Note:
1713d382aafbSBarry Smith      This routine is not supported in Fortran.
1714d382aafbSBarry Smith 
1715659f7ba0SBarry Smith    Note:
1716659f7ba0SBarry Smith      You can change how help messages are printed by replacing the function pointer with a function that does not simply write to stdout.
1717659f7ba0SBarry Smith 
1718659f7ba0SBarry Smith       To use, write your own function, for example,
1719659f7ba0SBarry Smith $PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....)
1720659f7ba0SBarry Smith ${
1721659f7ba0SBarry Smith $ PetscFunctionReturn(0);
1722659f7ba0SBarry Smith $}
1723659f7ba0SBarry Smith then do the assigment
1724659f7ba0SBarry Smith $    PetscHelpPrintf = mypetschelpprintf;
1725659f7ba0SBarry Smith    You can do the assignment before PetscInitialize().
1726659f7ba0SBarry Smith 
1727659f7ba0SBarry Smith   The default routine used is called PetscHelpPrintfDefault().
1728d382aafbSBarry Smith 
1729d382aafbSBarry Smith .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscErrorPrintf()
1730d382aafbSBarry Smith M*/
17313ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode (*PetscHelpPrintf)(MPI_Comm,const char[],...) PETSC_ATTRIBUTE_FORMAT(2,3);
1732d382aafbSBarry Smith 
1733fcfd50ebSBarry Smith /*
1734fcfd50ebSBarry Smith      Defines PETSc profiling.
1735fcfd50ebSBarry Smith */
17362c8e378dSBarry Smith #include <petsclog.h>
1737fcfd50ebSBarry Smith 
1738fcfd50ebSBarry Smith /*
1739fcfd50ebSBarry Smith       Simple PETSc parallel IO for ASCII printing
1740fcfd50ebSBarry Smith */
1741014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFixFilename(const char[],char[]);
1742014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFOpen(MPI_Comm,const char[],const char[],FILE**);
1743014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFClose(MPI_Comm,FILE*);
17443ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscFPrintf(MPI_Comm,FILE*,const char[],...) PETSC_ATTRIBUTE_FORMAT(3,4);
17453ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscPrintf(MPI_Comm,const char[],...) PETSC_ATTRIBUTE_FORMAT(2,3);
17463ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSNPrintf(char*,size_t,const char [],...) PETSC_ATTRIBUTE_FORMAT(3,4);
17473ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSNPrintfCount(char*,size_t,const char [],size_t*,...) PETSC_ATTRIBUTE_FORMAT(3,5);
17481b5687a1SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatRealArray(char[],size_t,const char*,PetscInt,const PetscReal[]);
1749fcfd50ebSBarry Smith 
17503ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscErrorPrintfDefault(const char [],...) PETSC_ATTRIBUTE_FORMAT(1,2);
17513ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscErrorPrintfNone(const char [],...) PETSC_ATTRIBUTE_FORMAT(1,2);
17523ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscHelpPrintfDefault(MPI_Comm,const char [],...) PETSC_ATTRIBUTE_FORMAT(2,3);
1753d382aafbSBarry Smith 
1754d781fa04SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatConvertGetSize(const char*,size_t*);
1755d781fa04SBarry Smith PETSC_EXTERN PetscErrorCode PetscFormatConvert(const char*,char *);
1756d781fa04SBarry Smith 
1757d382aafbSBarry Smith #if defined(PETSC_HAVE_POPEN)
1758014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPOpen(MPI_Comm,const char[],const char[],const char[],FILE **);
1759016831caSBarry Smith PETSC_EXTERN PetscErrorCode PetscPClose(MPI_Comm,FILE*);
176074ba8654SBarry Smith PETSC_EXTERN PetscErrorCode PetscPOpenSetMachine(const char[]);
1761d382aafbSBarry Smith #endif
1762d382aafbSBarry Smith 
17633ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSynchronizedPrintf(MPI_Comm,const char[],...) PETSC_ATTRIBUTE_FORMAT(2,3);
17643ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm,FILE*,const char[],...) PETSC_ATTRIBUTE_FORMAT(3,4);
17650ec8b6e3SBarry Smith PETSC_EXTERN PetscErrorCode PetscSynchronizedFlush(MPI_Comm,FILE*);
1766014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSynchronizedFGets(MPI_Comm,FILE*,size_t,char[]);
1767014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStartMatlab(MPI_Comm,const char[],const char[],FILE**);
1768014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStartJava(MPI_Comm,const char[],const char[],FILE**);
1769014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetPetscDir(const char*[]);
1770d382aafbSBarry Smith 
1771014dd563SJed Brown PETSC_EXTERN PetscClassId PETSC_CONTAINER_CLASSID;
1772014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerGetPointer(PetscContainer,void**);
1773014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerSetPointer(PetscContainer,void*);
1774014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerDestroy(PetscContainer*);
1775014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerCreate(MPI_Comm,PetscContainer*);
1776014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscContainerSetUserDestroy(PetscContainer,PetscErrorCode (*)(void*));
1777b61ba218SStefano Zampini PETSC_EXTERN PetscErrorCode PetscContainerUserDestroyDefault(void*);
1778d382aafbSBarry Smith 
1779d382aafbSBarry Smith /*
1780d382aafbSBarry Smith    For use in debuggers
1781d382aafbSBarry Smith */
1782014dd563SJed Brown PETSC_EXTERN PetscMPIInt PetscGlobalRank;
1783014dd563SJed Brown PETSC_EXTERN PetscMPIInt PetscGlobalSize;
1784014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIntView(PetscInt,const PetscInt[],PetscViewer);
1785014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRealView(PetscInt,const PetscReal[],PetscViewer);
1786014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscScalarView(PetscInt,const PetscScalar[],PetscViewer);
1787d382aafbSBarry Smith 
1788a663daf8SBarry Smith #include <stddef.h>
1789a3aaec0aSJed Brown #include <string.h>             /* for memcpy, memset */
1790d382aafbSBarry Smith #include <stdlib.h>
17916c7e564aSBarry Smith 
17924a92a979SJed Brown #if defined(PETSC_HAVE_XMMINTRIN_H) && !defined(__CUDACC__)
1793d382aafbSBarry Smith #include <xmmintrin.h>
1794d382aafbSBarry Smith #endif
1795d382aafbSBarry Smith 
1796d382aafbSBarry Smith /*@C
1797580bdb30SBarry Smith    PetscMemmove - Copies n bytes, beginning at location b, to the space
1798580bdb30SBarry Smith    beginning at location a. Copying  between regions that overlap will
1799580bdb30SBarry Smith    take place correctly. Use PetscMemcpy() if the locations do not overlap
1800580bdb30SBarry Smith 
1801580bdb30SBarry Smith    Not Collective
1802580bdb30SBarry Smith 
1803580bdb30SBarry Smith    Input Parameters:
1804580bdb30SBarry Smith +  b - pointer to initial memory space
1805580bdb30SBarry Smith .  a - pointer to copy space
1806580bdb30SBarry Smith -  n - length (in bytes) of space to copy
1807580bdb30SBarry Smith 
1808580bdb30SBarry Smith    Level: intermediate
1809580bdb30SBarry Smith 
1810580bdb30SBarry Smith    Note:
1811857a15f1SBarry Smith    PetscArraymove() is preferred
1812580bdb30SBarry Smith    This routine is analogous to memmove().
1813580bdb30SBarry Smith 
1814580bdb30SBarry Smith    Developers Note: This is inlined for performance
1815580bdb30SBarry Smith 
1816580bdb30SBarry Smith .seealso: PetscMemcpy(), PetscMemcmp(), PetscArrayzero(), PetscMemzero(), PetscArraycmp(), PetscArraycpy(), PetscStrallocpy(),
1817580bdb30SBarry Smith           PetscArraymove()
1818580bdb30SBarry Smith @*/
1819580bdb30SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscMemmove(void *a,const void *b,size_t n)
1820580bdb30SBarry Smith {
1821580bdb30SBarry Smith   PetscFunctionBegin;
1822580bdb30SBarry Smith   if (n > 0 && !a) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy to null pointer");
1823580bdb30SBarry Smith   if (n > 0 && !b) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy from a null pointer");
1824580bdb30SBarry Smith #if !defined(PETSC_HAVE_MEMMOVE)
1825580bdb30SBarry Smith   if (a < b) {
1826580bdb30SBarry Smith     if (a <= b - n) memcpy(a,b,n);
1827580bdb30SBarry Smith     else {
1828580bdb30SBarry Smith       memcpy(a,b,(int)(b - a));
1829580bdb30SBarry Smith       PetscMemmove(b,b + (int)(b - a),n - (int)(b - a));
1830580bdb30SBarry Smith     }
1831580bdb30SBarry Smith   } else {
1832580bdb30SBarry Smith     if (b <= a - n) memcpy(a,b,n);
1833580bdb30SBarry Smith     else {
1834580bdb30SBarry Smith       memcpy(b + n,b + (n - (int)(a - b)),(int)(a - b));
1835580bdb30SBarry Smith       PetscMemmove(a,b,n - (int)(a - b));
1836580bdb30SBarry Smith     }
1837580bdb30SBarry Smith   }
1838580bdb30SBarry Smith #else
1839580bdb30SBarry Smith   memmove((char*)(a),(char*)(b),n);
1840580bdb30SBarry Smith #endif
1841580bdb30SBarry Smith   PetscFunctionReturn(0);
1842580bdb30SBarry Smith }
1843580bdb30SBarry Smith 
1844d382aafbSBarry Smith /*@C
1845d382aafbSBarry Smith    PetscMemcpy - Copies n bytes, beginning at location b, to the space
1846d382aafbSBarry Smith    beginning at location a. The two memory regions CANNOT overlap, use
1847d382aafbSBarry Smith    PetscMemmove() in that case.
1848d382aafbSBarry Smith 
1849d382aafbSBarry Smith    Not Collective
1850d382aafbSBarry Smith 
1851d382aafbSBarry Smith    Input Parameters:
1852d382aafbSBarry Smith +  b - pointer to initial memory space
1853d382aafbSBarry Smith -  n - length (in bytes) of space to copy
1854d382aafbSBarry Smith 
1855d382aafbSBarry Smith    Output Parameter:
1856d382aafbSBarry Smith .  a - pointer to copy space
1857d382aafbSBarry Smith 
1858d382aafbSBarry Smith    Level: intermediate
1859d382aafbSBarry Smith 
1860d382aafbSBarry Smith    Compile Option:
1861d382aafbSBarry Smith     PETSC_PREFER_DCOPY_FOR_MEMCPY will cause the BLAS dcopy() routine to be used
1862d382aafbSBarry Smith                                   for memory copies on double precision values.
1863d382aafbSBarry Smith     PETSC_PREFER_COPY_FOR_MEMCPY will cause C code to be used
1864d382aafbSBarry Smith                                   for memory copies on double precision values.
1865d382aafbSBarry Smith     PETSC_PREFER_FORTRAN_FORMEMCPY will cause Fortran code to be used
1866d382aafbSBarry Smith                                   for memory copies on double precision values.
1867d382aafbSBarry Smith 
1868d382aafbSBarry Smith    Note:
1869580bdb30SBarry Smith    Prefer PetscArraycpy()
1870d382aafbSBarry Smith    This routine is analogous to memcpy().
18711fd49c25SBarry Smith    Not available from Fortran
18721fd49c25SBarry Smith 
1873f5f57ec0SBarry Smith    Developer Note: this is inlined for fastest performance
1874f5f57ec0SBarry Smith 
1875580bdb30SBarry Smith .seealso: PetscMemzero(), PetscMemcmp(), PetscArrayzero(), PetscArraycmp(), PetscArraycpy(), PetscMemmove(), PetscStrallocpy()
1876d382aafbSBarry Smith 
1877d382aafbSBarry Smith @*/
18787087cfbeSBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscMemcpy(void *a,const void *b,size_t n)
1879d382aafbSBarry Smith {
1880d382aafbSBarry Smith #if defined(PETSC_USE_DEBUG)
1881c5e4d11fSDmitry Karpeev   size_t al = (size_t) a,bl = (size_t) b;
1882c5e4d11fSDmitry Karpeev   size_t nl = (size_t) n;
1883a7e36092SMatthew G Knepley   PetscFunctionBegin;
1884e32f2f54SBarry Smith   if (n > 0 && !b) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy from a null pointer");
1885e32f2f54SBarry Smith   if (n > 0 && !a) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy to a null pointer");
1886a7e36092SMatthew G Knepley #else
1887d382aafbSBarry Smith   PetscFunctionBegin;
1888a7e36092SMatthew G Knepley #endif
1889a47a537aSLisandro Dalcin   if (a != b && n > 0) {
1890d382aafbSBarry Smith #if defined(PETSC_USE_DEBUG)
1891e47d2de2SBarry Smith     if ((al > bl && (al - bl) < nl) || (bl - al) < nl)  SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Memory regions overlap: either use PetscMemmov()\n\
1892d382aafbSBarry Smith               or make sure your copy regions and lengths are correct. \n\
1893d382aafbSBarry Smith               Length (bytes) %ld first address %ld second address %ld",nl,al,bl);
1894d382aafbSBarry Smith #endif
1895d382aafbSBarry Smith #if (defined(PETSC_PREFER_DCOPY_FOR_MEMCPY) || defined(PETSC_PREFER_COPY_FOR_MEMCPY) || defined(PETSC_PREFER_FORTRAN_FORMEMCPY))
1896c5e4d11fSDmitry Karpeev    if (!(a % sizeof(PetscScalar)) && !(n % sizeof(PetscScalar))) {
1897d382aafbSBarry Smith       size_t len = n/sizeof(PetscScalar);
1898d382aafbSBarry Smith #if defined(PETSC_PREFER_DCOPY_FOR_MEMCPY)
1899c5df96a5SBarry Smith       PetscBLASInt   one = 1,blen;
1900c5df96a5SBarry Smith       PetscErrorCode ierr;
1901c5df96a5SBarry Smith       ierr = PetscBLASIntCast(len,&blen);CHKERRQ(ierr);
19028b83055fSJed Brown       PetscStackCallBLAS("BLAScopy",BLAScopy_(&blen,(PetscScalar *)b,&one,(PetscScalar *)a,&one));
1903d382aafbSBarry Smith #elif defined(PETSC_PREFER_FORTRAN_FORMEMCPY)
1904d382aafbSBarry Smith       fortrancopy_(&len,(PetscScalar*)b,(PetscScalar*)a);
1905d382aafbSBarry Smith #else
1906d382aafbSBarry Smith       size_t      i;
1907d382aafbSBarry Smith       PetscScalar *x = (PetscScalar*)b, *y = (PetscScalar*)a;
1908d382aafbSBarry Smith       for (i=0; i<len; i++) y[i] = x[i];
1909d382aafbSBarry Smith #endif
1910d382aafbSBarry Smith     } else {
1911d382aafbSBarry Smith       memcpy((char*)(a),(char*)(b),n);
1912d382aafbSBarry Smith     }
1913d382aafbSBarry Smith #else
1914d382aafbSBarry Smith     memcpy((char*)(a),(char*)(b),n);
1915d382aafbSBarry Smith #endif
1916d382aafbSBarry Smith   }
1917d382aafbSBarry Smith   PetscFunctionReturn(0);
1918d382aafbSBarry Smith }
1919d382aafbSBarry Smith 
1920d382aafbSBarry Smith /*@C
1921d382aafbSBarry Smith    PetscMemzero - Zeros the specified memory.
1922d382aafbSBarry Smith 
1923d382aafbSBarry Smith    Not Collective
1924d382aafbSBarry Smith 
1925d382aafbSBarry Smith    Input Parameters:
1926d382aafbSBarry Smith +  a - pointer to beginning memory location
1927d382aafbSBarry Smith -  n - length (in bytes) of memory to initialize
1928d382aafbSBarry Smith 
1929d382aafbSBarry Smith    Level: intermediate
1930d382aafbSBarry Smith 
1931d382aafbSBarry Smith    Compile Option:
1932d382aafbSBarry Smith    PETSC_PREFER_BZERO - on certain machines (the IBM RS6000) the bzero() routine happens
1933d382aafbSBarry Smith   to be faster than the memset() routine. This flag causes the bzero() routine to be used.
1934d382aafbSBarry Smith 
19351fd49c25SBarry Smith    Not available from Fortran
1936857a15f1SBarry Smith    Prefer PetscArrayzero()
19371fd49c25SBarry Smith 
1938503cfb0cSBarry Smith    Developer Note: this is inlined for fastest performance
1939503cfb0cSBarry Smith 
1940580bdb30SBarry Smith .seealso: PetscMemcpy(), PetscMemcmp(), PetscArrayzero(), PetscArraycmp(), PetscArraycpy(), PetscMemmove(), PetscStrallocpy()
1941d382aafbSBarry Smith @*/
19427087cfbeSBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscMemzero(void *a,size_t n)
1943d382aafbSBarry Smith {
1944d382aafbSBarry Smith   if (n > 0) {
1945d382aafbSBarry Smith #if defined(PETSC_USE_DEBUG)
194618703db9SJunchao Zhang     if (!a) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to zero at a null pointer with %zu bytes",n);
1947d382aafbSBarry Smith #endif
1948d382aafbSBarry Smith #if defined(PETSC_PREFER_ZERO_FOR_MEMZERO)
1949d382aafbSBarry Smith     if (!(((long) a) % sizeof(PetscScalar)) && !(n % sizeof(PetscScalar))) {
1950d382aafbSBarry Smith       size_t      i,len = n/sizeof(PetscScalar);
1951d382aafbSBarry Smith       PetscScalar *x = (PetscScalar*)a;
1952d382aafbSBarry Smith       for (i=0; i<len; i++) x[i] = 0.0;
1953d382aafbSBarry Smith     } else {
1954d382aafbSBarry Smith #elif defined(PETSC_PREFER_FORTRAN_FOR_MEMZERO)
1955d382aafbSBarry Smith     if (!(((long) a) % sizeof(PetscScalar)) && !(n % sizeof(PetscScalar))) {
1956d382aafbSBarry Smith       PetscInt len = n/sizeof(PetscScalar);
1957d382aafbSBarry Smith       fortranzero_(&len,(PetscScalar*)a);
1958d382aafbSBarry Smith     } else {
1959d382aafbSBarry Smith #endif
1960d382aafbSBarry Smith #if defined(PETSC_PREFER_BZERO)
1961d382aafbSBarry Smith       bzero((char *)a,n);
1962d382aafbSBarry Smith #else
1963d382aafbSBarry Smith       memset((char*)a,0,n);
1964d382aafbSBarry Smith #endif
1965d382aafbSBarry Smith #if defined(PETSC_PREFER_ZERO_FOR_MEMZERO) || defined(PETSC_PREFER_FORTRAN_FOR_MEMZERO)
1966d382aafbSBarry Smith     }
1967d382aafbSBarry Smith #endif
1968d382aafbSBarry Smith   }
1969d382aafbSBarry Smith   return 0;
1970d382aafbSBarry Smith }
1971d382aafbSBarry Smith 
1972d382aafbSBarry Smith /*MC
1973580bdb30SBarry Smith    PetscArraycmp - Compares two arrays in memory.
1974580bdb30SBarry Smith 
1975580bdb30SBarry Smith    Synopsis:
1976580bdb30SBarry Smith     #include <petscsys.h>
1977580bdb30SBarry Smith     PetscErrorCode PetscArraycmp(const anytype *str1,const anytype *str2,size_t cnt,PetscBool *e)
1978580bdb30SBarry Smith 
1979580bdb30SBarry Smith    Not Collective
1980580bdb30SBarry Smith 
1981580bdb30SBarry Smith    Input Parameters:
1982580bdb30SBarry Smith +  str1 - First array
1983580bdb30SBarry Smith .  str2 - Second array
1984580bdb30SBarry Smith -  cnt  - Count of the array, not in bytes, but number of entries in the arrays
1985580bdb30SBarry Smith 
1986580bdb30SBarry Smith    Output Parameters:
1987580bdb30SBarry Smith .   e - PETSC_TRUE if equal else PETSC_FALSE.
1988580bdb30SBarry Smith 
1989580bdb30SBarry Smith    Level: intermediate
1990580bdb30SBarry Smith 
1991580bdb30SBarry Smith    Note:
1992857a15f1SBarry Smith    This routine is a preferred replacement to PetscMemcmp()
1993580bdb30SBarry Smith    The arrays must be of the same type
1994580bdb30SBarry Smith 
1995580bdb30SBarry Smith .seealso: PetscMemcpy(), PetscMemcmp(), PetscArrayzero(), PetscMemzero(), PetscArraycpy(), PetscMemmove(), PetscStrallocpy(),
1996580bdb30SBarry Smith           PetscArraymove()
1997580bdb30SBarry Smith M*/
19989e2232c7SLisandro Dalcin #define  PetscArraycmp(str1,str2,cnt,e) ((sizeof(*(str1)) != sizeof(*(str2))) || PetscMemcmp(str1,str2,(size_t)(cnt)*sizeof(*(str1)),e))
1999580bdb30SBarry Smith 
20008f8f2f0dSBarry Smith /*MC
2001580bdb30SBarry Smith    PetscArraymove - Copies from one array in memory to another, the arrays may overlap. Use PetscArraycpy() when the arrays
2002580bdb30SBarry Smith                     do not overlap
2003580bdb30SBarry Smith 
2004580bdb30SBarry Smith    Synopsis:
2005580bdb30SBarry Smith     #include <petscsys.h>
2006580bdb30SBarry Smith     PetscErrorCode PetscArraymove(anytype *str1,const anytype *str2,size_t cnt)
2007580bdb30SBarry Smith 
2008580bdb30SBarry Smith    Not Collective
2009580bdb30SBarry Smith 
2010580bdb30SBarry Smith    Input Parameters:
2011580bdb30SBarry Smith +  str1 - First array
2012580bdb30SBarry Smith .  str2 - Second array
2013580bdb30SBarry Smith -  cnt  - Count of the array, not in bytes, but number of entries in the arrays
2014580bdb30SBarry Smith 
2015580bdb30SBarry Smith    Level: intermediate
2016580bdb30SBarry Smith 
2017580bdb30SBarry Smith    Note:
2018857a15f1SBarry Smith    This routine is a preferred replacement to PetscMemmove()
2019580bdb30SBarry Smith    The arrays must be of the same type
2020580bdb30SBarry Smith 
2021580bdb30SBarry Smith .seealso: PetscMemcpy(), PetscMemcmp(), PetscArrayzero(), PetscMemzero(), PetscArraycpy(), PetscMemmove(), PetscArraycmp(), PetscStrallocpy()
2022580bdb30SBarry Smith M*/
20239e2232c7SLisandro Dalcin #define  PetscArraymove(str1,str2,cnt) ((sizeof(*(str1)) != sizeof(*(str2))) || PetscMemmove(str1,str2,(size_t)(cnt)*sizeof(*(str1))))
2024580bdb30SBarry Smith 
20258f8f2f0dSBarry Smith /*MC
2026580bdb30SBarry Smith    PetscArraycpy - Copies from one array in memory to another
2027580bdb30SBarry Smith 
2028580bdb30SBarry Smith    Synopsis:
2029580bdb30SBarry Smith     #include <petscsys.h>
2030580bdb30SBarry Smith     PetscErrorCode PetscArraycpy(anytype *str1,const anytype *str2,size_t cnt)
2031580bdb30SBarry Smith 
2032580bdb30SBarry Smith    Not Collective
2033580bdb30SBarry Smith 
2034580bdb30SBarry Smith    Input Parameters:
2035de25709eSStefano Zampini +  str1 - First array (destination)
2036de25709eSStefano Zampini .  str2 - Second array (source)
2037580bdb30SBarry Smith -  cnt  - Count of the array, not in bytes, but number of entries in the arrays
2038580bdb30SBarry Smith 
2039580bdb30SBarry Smith    Level: intermediate
2040580bdb30SBarry Smith 
2041580bdb30SBarry Smith    Note:
2042857a15f1SBarry Smith    This routine is a preferred replacement to PetscMemcpy()
2043580bdb30SBarry Smith    The arrays must be of the same type
2044580bdb30SBarry Smith 
2045580bdb30SBarry Smith .seealso: PetscMemcpy(), PetscMemcmp(), PetscArrayzero(), PetscMemzero(), PetscArraymove(), PetscMemmove(), PetscArraycmp(), PetscStrallocpy()
2046580bdb30SBarry Smith M*/
20479e2232c7SLisandro Dalcin #define  PetscArraycpy(str1,str2,cnt) ((sizeof(*(str1)) != sizeof(*(str2))) || PetscMemcpy(str1,str2,(size_t)(cnt)*sizeof(*(str1))))
2048580bdb30SBarry Smith 
20498f8f2f0dSBarry Smith /*MC
2050580bdb30SBarry Smith    PetscArrayzero - Zeros an array in memory.
2051580bdb30SBarry Smith 
2052580bdb30SBarry Smith    Synopsis:
2053580bdb30SBarry Smith     #include <petscsys.h>
2054580bdb30SBarry Smith     PetscErrorCode PetscArrayzero(anytype *str1,size_t cnt)
2055580bdb30SBarry Smith 
2056580bdb30SBarry Smith    Not Collective
2057580bdb30SBarry Smith 
2058580bdb30SBarry Smith    Input Parameters:
2059580bdb30SBarry Smith +  str1 - array
2060580bdb30SBarry Smith -  cnt  - Count of the array, not in bytes, but number of entries in the array
2061580bdb30SBarry Smith 
2062580bdb30SBarry Smith    Level: intermediate
2063580bdb30SBarry Smith 
2064580bdb30SBarry Smith    Note:
2065857a15f1SBarry Smith    This routine is a preferred replacement to PetscMemzero()
2066580bdb30SBarry Smith 
2067580bdb30SBarry Smith .seealso: PetscMemcpy(), PetscMemcmp(), PetscMemzero(), PetscArraycmp(), PetscArraycpy(), PetscMemmove(), PetscStrallocpy(), PetscArraymove()
2068580bdb30SBarry Smith M*/
20699e2232c7SLisandro Dalcin #define  PetscArrayzero(str1,cnt) PetscMemzero(str1,(size_t)(cnt)*sizeof(*(str1)))
2070580bdb30SBarry Smith 
2071d382aafbSBarry Smith /*MC
2072d382aafbSBarry Smith    PetscPrefetchBlock - Prefetches a block of memory
2073d382aafbSBarry Smith 
2074eca87e8dSBarry Smith    Synopsis:
2075aaa7dc30SBarry Smith     #include <petscsys.h>
2076eca87e8dSBarry Smith     void PetscPrefetchBlock(const anytype *a,size_t n,int rw,int t)
2077eca87e8dSBarry Smith 
2078d382aafbSBarry Smith    Not Collective
2079d382aafbSBarry Smith 
2080d382aafbSBarry Smith    Input Parameters:
2081d382aafbSBarry Smith +  a - pointer to first element to fetch (any type but usually PetscInt or PetscScalar)
2082d382aafbSBarry Smith .  n - number of elements to fetch
2083d382aafbSBarry Smith .  rw - 1 if the memory will be written to, otherwise 0 (ignored by many processors)
208450d8bf02SJed Brown -  t - temporal locality (PETSC_PREFETCH_HINT_{NTA,T0,T1,T2}), see note
2085d382aafbSBarry Smith 
2086d382aafbSBarry Smith    Level: developer
2087d382aafbSBarry Smith 
2088d382aafbSBarry Smith    Notes:
2089d382aafbSBarry Smith    The last two arguments (rw and t) must be compile-time constants.
2090d382aafbSBarry Smith 
209150d8bf02SJed Brown    Adopting Intel's x86/x86-64 conventions, there are four levels of temporal locality.  Not all architectures offer
209250d8bf02SJed Brown    equivalent locality hints, but the following macros are always defined to their closest analogue.
209350d8bf02SJed Brown +  PETSC_PREFETCH_HINT_NTA - Non-temporal.  Prefetches directly to L1, evicts to memory (skips higher level cache unless it was already there when prefetched).
209463cf1ef0SJed Brown .  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.
209563cf1ef0SJed Brown .  PETSC_PREFETCH_HINT_T1 - Fetch to level 2 and higher (not L1).
209663cf1ef0SJed Brown -  PETSC_PREFETCH_HINT_T2 - Fetch to high-level cache only.  (On many systems, T0 and T1 are equivalent.)
2097d382aafbSBarry Smith 
2098d382aafbSBarry Smith    This function does nothing on architectures that do not support prefetch and never errors (even if passed an invalid
2099d382aafbSBarry Smith    address).
2100d382aafbSBarry Smith 
2101d382aafbSBarry Smith M*/
2102d382aafbSBarry Smith #define PetscPrefetchBlock(a,n,rw,t) do {                               \
2103d382aafbSBarry Smith     const char *_p = (const char*)(a),*_end = (const char*)((a)+(n));   \
2104d382aafbSBarry Smith     for (; _p < _end; _p += PETSC_LEVEL1_DCACHE_LINESIZE) PETSC_Prefetch(_p,(rw),(t)); \
2105d382aafbSBarry Smith   } while (0)
2106d382aafbSBarry Smith 
2107d382aafbSBarry Smith /*
2108d382aafbSBarry Smith       Determine if some of the kernel computation routines use
2109d382aafbSBarry Smith    Fortran (rather than C) for the numerical calculations. On some machines
2110d382aafbSBarry Smith    and compilers (like complex numbers) the Fortran version of the routines
2111d382aafbSBarry Smith    is faster than the C/C++ versions. The flag --with-fortran-kernels
2112e2e64c6bSBarry Smith    should be used with ./configure to turn these on.
2113d382aafbSBarry Smith */
2114d382aafbSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNELS)
2115d382aafbSBarry Smith 
2116d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTCRL)
2117d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MULTCRL
2118d382aafbSBarry Smith #endif
2119d382aafbSBarry Smith 
21205a11e1b2SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJPERM)
21215a11e1b2SBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MULTAIJPERM
2122d382aafbSBarry Smith #endif
2123d382aafbSBarry Smith 
2124d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
2125d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MULTAIJ
2126d382aafbSBarry Smith #endif
2127d382aafbSBarry Smith 
2128d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
2129d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ
2130d382aafbSBarry Smith #endif
2131d382aafbSBarry Smith 
2132d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_NORM)
2133d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_NORM
2134d382aafbSBarry Smith #endif
2135d382aafbSBarry Smith 
2136d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MAXPY)
2137d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MAXPY
2138d382aafbSBarry Smith #endif
2139d382aafbSBarry Smith 
2140d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
2141d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ
2142d382aafbSBarry Smith #endif
2143d382aafbSBarry Smith 
2144d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
2145d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_RELAXAIJ
2146d382aafbSBarry Smith #endif
2147d382aafbSBarry Smith 
2148d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ)
2149d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ
2150d382aafbSBarry Smith #endif
2151d382aafbSBarry Smith 
2152d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
2153d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ
2154d382aafbSBarry Smith #endif
2155d382aafbSBarry Smith 
2156d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MDOT)
2157d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_MDOT
2158d382aafbSBarry Smith #endif
2159d382aafbSBarry Smith 
2160d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_XTIMESY)
2161d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_XTIMESY
2162d382aafbSBarry Smith #endif
2163d382aafbSBarry Smith 
2164d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_AYPX)
2165d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_AYPX
2166d382aafbSBarry Smith #endif
2167d382aafbSBarry Smith 
2168d382aafbSBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_WAXPY)
2169d382aafbSBarry Smith #define PETSC_USE_FORTRAN_KERNEL_WAXPY
2170d382aafbSBarry Smith #endif
2171d382aafbSBarry Smith 
2172d382aafbSBarry Smith #endif
2173d382aafbSBarry Smith 
2174d382aafbSBarry Smith /*
2175d382aafbSBarry Smith     Macros for indicating code that should be compiled with a C interface,
2176d382aafbSBarry Smith    rather than a C++ interface. Any routines that are dynamically loaded
2177d382aafbSBarry Smith    (such as the PCCreate_XXX() routines) must be wrapped so that the name
2178d382aafbSBarry Smith    mangler does not change the functions symbol name. This just hides the
2179d382aafbSBarry Smith    ugly extern "C" {} wrappers.
2180d382aafbSBarry Smith */
2181d382aafbSBarry Smith #if defined(__cplusplus)
2182d382aafbSBarry Smith #  define EXTERN_C_BEGIN extern "C" {
2183d382aafbSBarry Smith #  define EXTERN_C_END }
2184d382aafbSBarry Smith #else
2185d382aafbSBarry Smith #  define EXTERN_C_BEGIN
2186d382aafbSBarry Smith #  define EXTERN_C_END
2187d382aafbSBarry Smith #endif
2188d382aafbSBarry Smith 
2189d382aafbSBarry Smith /* --------------------------------------------------------------------*/
2190d382aafbSBarry Smith 
2191d382aafbSBarry Smith /*MC
2192d382aafbSBarry Smith     MPI_Comm - the basic object used by MPI to determine which processes are involved in a
2193d382aafbSBarry Smith         communication
2194d382aafbSBarry Smith 
2195d382aafbSBarry Smith    Level: beginner
2196d382aafbSBarry Smith 
2197d382aafbSBarry Smith    Note: This manual page is a place-holder because MPICH does not have a manual page for MPI_Comm
2198d382aafbSBarry Smith 
2199d382aafbSBarry Smith .seealso: PETSC_COMM_WORLD, PETSC_COMM_SELF
2200d382aafbSBarry Smith M*/
2201d382aafbSBarry Smith 
2202d382aafbSBarry Smith #if defined(PETSC_HAVE_MPIIO)
2203014dd563SJed Brown PETSC_EXTERN PetscErrorCode MPIU_File_write_all(MPI_File,void*,PetscMPIInt,MPI_Datatype,MPI_Status*);
2204014dd563SJed Brown PETSC_EXTERN PetscErrorCode MPIU_File_read_all(MPI_File,void*,PetscMPIInt,MPI_Datatype,MPI_Status*);
2205c4e82887SLisandro Dalcin PETSC_EXTERN PetscErrorCode MPIU_File_write_at(MPI_File,MPI_Offset,void*,PetscMPIInt,MPI_Datatype,MPI_Status*);
2206c4e82887SLisandro Dalcin PETSC_EXTERN PetscErrorCode MPIU_File_read_at(MPI_File,MPI_Offset,void*,PetscMPIInt,MPI_Datatype,MPI_Status*);
2207c4e82887SLisandro Dalcin PETSC_EXTERN PetscErrorCode MPIU_File_write_at_all(MPI_File,MPI_Offset,void*,PetscMPIInt,MPI_Datatype,MPI_Status*);
2208c4e82887SLisandro Dalcin PETSC_EXTERN PetscErrorCode MPIU_File_read_at_all(MPI_File,MPI_Offset,void*,PetscMPIInt,MPI_Datatype,MPI_Status*);
2209d382aafbSBarry Smith #endif
2210d382aafbSBarry Smith 
2211d382aafbSBarry Smith /* the following petsc_static_inline require petscerror.h */
2212d382aafbSBarry Smith 
2213d382aafbSBarry Smith /* Limit MPI to 32-bits */
2214d382aafbSBarry Smith #define PETSC_MPI_INT_MAX  2147483647
2215d382aafbSBarry Smith #define PETSC_MPI_INT_MIN -2147483647
2216d382aafbSBarry Smith /* Limit BLAS to 32-bits */
2217d382aafbSBarry Smith #define PETSC_BLAS_INT_MAX  2147483647
2218d382aafbSBarry Smith #define PETSC_BLAS_INT_MIN -2147483647
2219eee0c0a6SToby Isaac #define PETSC_CUBLAS_INT_MAX  2147483647
2220d382aafbSBarry Smith 
222161b0d812SBarry Smith /*@C
2222c73702f5SBarry Smith     PetscIntCast - casts a PetscInt64 (which is 64 bits in size) to a PetscInt (which may be 32 bits in size), generates an
2223c73702f5SBarry Smith          error if the PetscInt is not large enough to hold the number.
2224c73702f5SBarry Smith 
2225c73702f5SBarry Smith    Not Collective
2226c73702f5SBarry Smith 
2227c73702f5SBarry Smith    Input Parameter:
2228c73702f5SBarry Smith .     a - the PetscInt64 value
2229c73702f5SBarry Smith 
2230c73702f5SBarry Smith    Output Parameter:
2231c73702f5SBarry Smith .     b - the resulting PetscInt value
2232c73702f5SBarry Smith 
2233c73702f5SBarry Smith    Level: advanced
2234c73702f5SBarry Smith 
2235c73702f5SBarry Smith    Notes: If integers needed for the applications are too large to fit in 32 bit ints you can ./configure using --with-64-bit-indices to make PetscInt use 64 bit ints
2236c73702f5SBarry Smith 
2237c73702f5SBarry Smith    Not available from Fortran
2238c73702f5SBarry Smith 
223961778c46SBarry Smith .seealso: PetscBLASInt, PetscMPIInt, PetscInt, PetscMPIIntCast(), PetscBLASIntCast(), PetscIntMultError(), PetscIntSumError()
2240c73702f5SBarry Smith @*/
2241c73702f5SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIntCast(PetscInt64 a,PetscInt *b)
2242c73702f5SBarry Smith {
2243c73702f5SBarry Smith   PetscFunctionBegin;
2244c73702f5SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
22453ca90d2dSJacob Faibussowitsch   if (a > PETSC_MAX_INT) {
22463ca90d2dSJacob Faibussowitsch     *b = 0;
22473ca90d2dSJacob Faibussowitsch     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"%" PetscInt64_FMT " is too big for PetscInt, you may need to ./configure using --with-64-bit-indices",a);
22483ca90d2dSJacob Faibussowitsch   }
2249c73702f5SBarry Smith #endif
2250c73702f5SBarry Smith   *b = (PetscInt)(a);
2251c73702f5SBarry Smith   PetscFunctionReturn(0);
2252c73702f5SBarry Smith }
2253c73702f5SBarry Smith 
2254c73702f5SBarry Smith /*@C
225561b0d812SBarry Smith     PetscBLASIntCast - casts a PetscInt (which may be 64 bits in size) to a PetscBLASInt (which may be 32 bits in size), generates an
225661b0d812SBarry Smith          error if the PetscBLASInt is not large enough to hold the number.
225761b0d812SBarry Smith 
225861b0d812SBarry Smith    Not Collective
225961b0d812SBarry Smith 
226061b0d812SBarry Smith    Input Parameter:
226161b0d812SBarry Smith .     a - the PetscInt value
226261b0d812SBarry Smith 
226361b0d812SBarry Smith    Output Parameter:
226461b0d812SBarry Smith .     b - the resulting PetscBLASInt value
226561b0d812SBarry Smith 
226661b0d812SBarry Smith    Level: advanced
226761b0d812SBarry Smith 
22680bc7d38cSBarry Smith    Notes:
22691fd49c25SBarry Smith       Not available from Fortran
2270f0b7f91aSBarry Smith       Errors if the integer is negative since PETSc calls to BLAS/LAPACK never need to cast negative integer inputs
22711fd49c25SBarry Smith 
2272c73702f5SBarry Smith .seealso: PetscBLASInt, PetscMPIInt, PetscInt, PetscMPIIntCast(), PetscIntCast()
227361b0d812SBarry Smith @*/
2274c5df96a5SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscBLASIntCast(PetscInt a,PetscBLASInt *b)
2275c5df96a5SBarry Smith {
2276c5df96a5SBarry Smith   PetscFunctionBegin;
2277c5df96a5SBarry Smith #if defined(PETSC_USE_64BIT_INDICES) && !defined(PETSC_HAVE_64BIT_BLAS_INDICES)
22783ca90d2dSJacob Faibussowitsch   if (a > PETSC_BLAS_INT_MAX) { *b = 0; SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"%" PetscInt_FMT " is too big for BLAS/LAPACK, which is restricted to 32 bit integers. Either you have an invalidly large integer error in your code or you must ./configure PETSc with -with-64-bit-blas-indices for the case you are running",a); }
2279c5df96a5SBarry Smith #endif
2280e9e21dffSJose E. Roman   if (a < 0) { *b = 0; SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Passing negative integer to BLAS/LAPACK routine"); }
2281c73702f5SBarry Smith   *b = (PetscBLASInt)(a);
2282c5df96a5SBarry Smith   PetscFunctionReturn(0);
2283c5df96a5SBarry Smith }
2284c5df96a5SBarry Smith 
228561b0d812SBarry Smith /*@C
2286eee0c0a6SToby Isaac     PetscCuBLASIntCast - like PetscBLASIntCast(), but for PetscCuBLASInt.
2287eee0c0a6SToby Isaac 
2288eee0c0a6SToby Isaac    Not Collective
2289eee0c0a6SToby Isaac 
2290eee0c0a6SToby Isaac    Input Parameter:
2291eee0c0a6SToby Isaac .     a - the PetscInt value
2292eee0c0a6SToby Isaac 
2293eee0c0a6SToby Isaac    Output Parameter:
2294eee0c0a6SToby Isaac .     b - the resulting PetscCuBLASInt value
2295eee0c0a6SToby Isaac 
2296eee0c0a6SToby Isaac    Level: advanced
2297eee0c0a6SToby Isaac 
2298eee0c0a6SToby Isaac    Notes:
2299eee0c0a6SToby Isaac       Errors if the integer is negative since PETSc calls to cuBLAS and friends never need to cast negative integer inputs
2300eee0c0a6SToby Isaac 
2301eee0c0a6SToby Isaac .seealso: PetscCuBLASInt, PetscBLASInt, PetscMPIInt, PetscInt, PetscBLASIntCast(), PetscMPIIntCast(), PetscIntCast()
2302eee0c0a6SToby Isaac @*/
2303ae77d22fSJunchao Zhang PETSC_STATIC_INLINE PetscErrorCode PetscCuBLASIntCast(PetscInt a,PetscCuBLASInt *b)
2304eee0c0a6SToby Isaac {
2305eee0c0a6SToby Isaac   PetscFunctionBegin;
2306eee0c0a6SToby Isaac #if defined(PETSC_USE_64BIT_INDICES)
23073ca90d2dSJacob Faibussowitsch   if (a > PETSC_CUBLAS_INT_MAX) { *b = 0; SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"%" PetscInt_FMT " is too big for cuBLAS, which is restricted to 32 bit integers.",a); }
2308eee0c0a6SToby Isaac #endif
2309e9e21dffSJose E. Roman   if (a < 0) { *b = 0; SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Passing negative integer to cuBLAS routine"); }
2310eee0c0a6SToby Isaac   *b = (PetscCuBLASInt)(a);
2311eee0c0a6SToby Isaac   PetscFunctionReturn(0);
2312eee0c0a6SToby Isaac }
2313eee0c0a6SToby Isaac 
2314eee0c0a6SToby Isaac /*@C
231561b0d812SBarry Smith     PetscMPIIntCast - casts a PetscInt (which may be 64 bits in size) to a PetscMPIInt (which may be 32 bits in size), generates an
231661b0d812SBarry Smith          error if the PetscMPIInt is not large enough to hold the number.
231761b0d812SBarry Smith 
231861b0d812SBarry Smith    Not Collective
231961b0d812SBarry Smith 
232061b0d812SBarry Smith    Input Parameter:
232161b0d812SBarry Smith .     a - the PetscInt value
232261b0d812SBarry Smith 
232361b0d812SBarry Smith    Output Parameter:
232461b0d812SBarry Smith .     b - the resulting PetscMPIInt value
232561b0d812SBarry Smith 
232661b0d812SBarry Smith    Level: advanced
232761b0d812SBarry Smith 
23281fd49c25SBarry Smith    Not available from Fortran
23291fd49c25SBarry Smith 
2330c73702f5SBarry Smith .seealso: PetscBLASInt, PetscMPIInt, PetscInt, PetscBLASIntCast(), PetscIntCast()
233161b0d812SBarry Smith @*/
23324dc2109aSBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscMPIIntCast(PetscInt a,PetscMPIInt *b)
23334dc2109aSBarry Smith {
23344dc2109aSBarry Smith   PetscFunctionBegin;
23354dc2109aSBarry Smith #if defined(PETSC_USE_64BIT_INDICES)
23363ca90d2dSJacob Faibussowitsch   if (a > PETSC_MPI_INT_MAX) { *b = 0; SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"%" PetscInt_FMT " is too big for MPI buffer length. We currently only support 32 bit integers",a); }
23374dc2109aSBarry Smith #endif
2338c73702f5SBarry Smith   *b = (PetscMPIInt)(a);
23394dc2109aSBarry Smith   PetscFunctionReturn(0);
23404dc2109aSBarry Smith }
23414dc2109aSBarry Smith 
2342bafee8b4SSatish Balay #define PetscInt64Mult(a,b)   ((PetscInt64)(a))*((PetscInt64)(b))
23432f18eb33SBarry Smith 
23442f18eb33SBarry Smith /*@C
23452f18eb33SBarry Smith 
23462f18eb33SBarry Smith    PetscRealIntMultTruncate - Computes the product of a positive PetscReal and a positive PetscInt and truncates the value to slightly less than the maximal possible value
23472f18eb33SBarry Smith 
23482f18eb33SBarry Smith    Not Collective
23492f18eb33SBarry Smith 
2350d8d19677SJose E. Roman    Input Parameters:
23512f18eb33SBarry Smith +     a - the PetscReal value
23522f18eb33SBarry Smith -     b - the second value
23532f18eb33SBarry Smith 
2354390e1bf2SBarry Smith    Returns:
2355390e1bf2SBarry Smith       the result as a PetscInt value
23562f18eb33SBarry Smith 
2357bafee8b4SSatish Balay    Use PetscInt64Mult() to compute the product of two PetscInt as a PetscInt64
23582f18eb33SBarry Smith    Use PetscIntMultTruncate() to compute the product of two positive PetscInt and truncate to fit a PetscInt
23592f18eb33SBarry Smith    Use PetscIntMultError() to compute the product of two PetscInt if you wish to generate an error if the result will not fit in a PetscInt
23602f18eb33SBarry Smith 
2361e28ce29aSPatrick Sanan    Developers Note:
2362e28ce29aSPatrick Sanan    We currently assume that PetscInt addition can never overflow, this is obviously wrong but requires many more checks.
23632f18eb33SBarry Smith 
23642f18eb33SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
23652f18eb33SBarry Smith 
23661fd49c25SBarry Smith    Not available from Fortran
23671fd49c25SBarry Smith 
23682f18eb33SBarry Smith    Level: advanced
23692f18eb33SBarry Smith 
237061778c46SBarry Smith .seealso: PetscBLASInt, PetscMPIInt, PetscInt, PetscBLASIntCast(), PetscInt64Mult(), PetscIntMultError(), PetscIntSumError()
23712f18eb33SBarry Smith @*/
23722f18eb33SBarry Smith PETSC_STATIC_INLINE PetscInt PetscRealIntMultTruncate(PetscReal a,PetscInt b)
23732f18eb33SBarry Smith {
2374bafee8b4SSatish Balay   PetscInt64 r;
23752f18eb33SBarry Smith 
2376bafee8b4SSatish Balay   r  =  (PetscInt64) (a*(PetscReal)b);
23772f18eb33SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
23782f18eb33SBarry Smith   return (PetscInt) r;
23792f18eb33SBarry Smith }
23802f18eb33SBarry Smith 
23812f18eb33SBarry Smith /*@C
23822f18eb33SBarry Smith 
23832f18eb33SBarry Smith    PetscIntMultTruncate - Computes the product of two positive PetscInt and truncates the value to slightly less than the maximal possible value
23842f18eb33SBarry Smith 
23852f18eb33SBarry Smith    Not Collective
23862f18eb33SBarry Smith 
2387d8d19677SJose E. Roman    Input Parameters:
23882f18eb33SBarry Smith +     a - the PetscInt value
23892f18eb33SBarry Smith -     b - the second value
23902f18eb33SBarry Smith 
2391390e1bf2SBarry Smith    Returns:
2392390e1bf2SBarry Smith       the result as a PetscInt value
23932f18eb33SBarry Smith 
2394bafee8b4SSatish Balay    Use PetscInt64Mult() to compute the product of two PetscInt as a PetscInt64
23952f18eb33SBarry Smith    Use PetscRealIntMultTruncate() to compute the product of a PetscReal and a PetscInt and truncate to fit a PetscInt
23962f18eb33SBarry Smith    Use PetscIntMultError() to compute the product of two PetscInt if you wish to generate an error if the result will not fit in a PetscInt
23972f18eb33SBarry Smith 
23981fd49c25SBarry Smith    Not available from Fortran
23991fd49c25SBarry Smith 
24002f18eb33SBarry Smith    Developers Note: We currently assume that PetscInt addition can never overflow, this is obviously wrong but requires many more checks.
24012f18eb33SBarry Smith 
24022f18eb33SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
24032f18eb33SBarry Smith 
24042f18eb33SBarry Smith    Level: advanced
24052f18eb33SBarry Smith 
240661778c46SBarry Smith .seealso: PetscBLASInt, PetscMPIInt, PetscInt, PetscBLASIntCast(), PetscInt64Mult(), PetscIntMultError(), PetscIntSumError()
24072f18eb33SBarry Smith @*/
24082f18eb33SBarry Smith PETSC_STATIC_INLINE PetscInt PetscIntMultTruncate(PetscInt a,PetscInt b)
24092f18eb33SBarry Smith {
2410bafee8b4SSatish Balay   PetscInt64 r;
24112f18eb33SBarry Smith 
2412bafee8b4SSatish Balay   r  =  PetscInt64Mult(a,b);
24132f18eb33SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
24142f18eb33SBarry Smith   return (PetscInt) r;
24152f18eb33SBarry Smith }
24162f18eb33SBarry Smith 
2417f91af8c7SBarry Smith /*@C
2418f91af8c7SBarry Smith 
2419f91af8c7SBarry Smith    PetscIntSumTruncate - Computes the sum of two positive PetscInt and truncates the value to slightly less than the maximal possible value
2420f91af8c7SBarry Smith 
2421f91af8c7SBarry Smith    Not Collective
2422f91af8c7SBarry Smith 
2423d8d19677SJose E. Roman    Input Parameters:
2424f91af8c7SBarry Smith +     a - the PetscInt value
2425f91af8c7SBarry Smith -     b - the second value
2426f91af8c7SBarry Smith 
2427390e1bf2SBarry Smith    Returns:
2428390e1bf2SBarry Smith      the result as a PetscInt value
2429f91af8c7SBarry Smith 
2430bafee8b4SSatish Balay    Use PetscInt64Mult() to compute the product of two PetscInt as a PetscInt64
2431f91af8c7SBarry Smith    Use PetscRealIntMultTruncate() to compute the product of a PetscReal and a PetscInt and truncate to fit a PetscInt
2432f91af8c7SBarry Smith    Use PetscIntMultError() to compute the product of two PetscInt if you wish to generate an error if the result will not fit in a PetscInt
2433f91af8c7SBarry Smith 
2434f91af8c7SBarry Smith    This is used where we compute approximate sizes for workspace and need to insure the workspace is index-able.
2435f91af8c7SBarry Smith 
2436f5f57ec0SBarry Smith    Not available from Fortran
2437f5f57ec0SBarry Smith 
2438f91af8c7SBarry Smith    Level: advanced
2439f91af8c7SBarry Smith 
244061778c46SBarry Smith .seealso: PetscBLASInt, PetscMPIInt, PetscInt, PetscBLASIntCast(), PetscInt64Mult(), PetscIntMultError()
2441f91af8c7SBarry Smith @*/
2442f91af8c7SBarry Smith PETSC_STATIC_INLINE PetscInt PetscIntSumTruncate(PetscInt a,PetscInt b)
2443f91af8c7SBarry Smith {
2444bafee8b4SSatish Balay   PetscInt64 r;
2445f91af8c7SBarry Smith 
2446bafee8b4SSatish Balay   r  =  ((PetscInt64)a) + ((PetscInt64)b);
2447f91af8c7SBarry Smith   if (r > PETSC_MAX_INT - 100) r = PETSC_MAX_INT - 100;
2448f91af8c7SBarry Smith   return (PetscInt) r;
2449f91af8c7SBarry Smith }
2450f91af8c7SBarry Smith 
24512f18eb33SBarry Smith /*@C
24522f18eb33SBarry Smith 
24532f18eb33SBarry Smith    PetscIntMultError - Computes the product of two positive PetscInt and generates an error with overflow.
24542f18eb33SBarry Smith 
24552f18eb33SBarry Smith    Not Collective
24562f18eb33SBarry Smith 
2457d8d19677SJose E. Roman    Input Parameters:
24582f18eb33SBarry Smith +     a - the PetscInt value
24592f18eb33SBarry Smith -     b - the second value
24602f18eb33SBarry Smith 
2461d8d19677SJose E. Roman    Output Parameter:
2462390e1bf2SBarry Smith .     result - the result as a PetscInt value, or NULL if you do not want the result, you just want to check if it overflows
24632f18eb33SBarry Smith 
2464bafee8b4SSatish Balay    Use PetscInt64Mult() to compute the product of two 32 bit PetscInt and store in a PetscInt64
24652f18eb33SBarry Smith    Use PetscIntMultTruncate() to compute the product of two PetscInt and truncate it to fit in a PetscInt
24662f18eb33SBarry Smith 
2467f5f57ec0SBarry Smith    Not available from Fortran
2468f5f57ec0SBarry Smith 
2469220afb94SBarry Smith    Developers Note: We currently assume that PetscInt addition does not overflow, this is obviously wrong but requires many more checks.
24702f18eb33SBarry Smith 
24712f18eb33SBarry Smith    Level: advanced
24722f18eb33SBarry Smith 
2473220afb94SBarry Smith .seealso: PetscBLASInt, PetscMPIInt, PetscInt, PetscBLASIntCast(), PetscIntMult64(), PetscIntSumError()
24742f18eb33SBarry Smith @*/
24752f18eb33SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIntMultError(PetscInt a,PetscInt b,PetscInt *result)
24762f18eb33SBarry Smith {
2477bafee8b4SSatish Balay   PetscInt64 r;
24782f18eb33SBarry Smith 
24792f18eb33SBarry Smith   PetscFunctionBegin;
2480bafee8b4SSatish Balay   r  =  PetscInt64Mult(a,b);
24812f18eb33SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
248261778c46SBarry Smith   if (r > PETSC_MAX_INT) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Product of two integers %d %d overflow, either you have an invalidly large integer error in your code or you must ./configure PETSc with --with-64-bit-indices for the case you are running",a,b);
24832f18eb33SBarry Smith #endif
2484f91af8c7SBarry Smith   if (result) *result = (PetscInt) r;
2485f91af8c7SBarry Smith   PetscFunctionReturn(0);
2486f91af8c7SBarry Smith }
2487f91af8c7SBarry Smith 
2488f91af8c7SBarry Smith /*@C
2489f91af8c7SBarry Smith 
2490fbfcfee5SBarry Smith    PetscIntSumError - Computes the sum of two positive PetscInt and generates an error with overflow.
2491f91af8c7SBarry Smith 
2492f91af8c7SBarry Smith    Not Collective
2493f91af8c7SBarry Smith 
2494d8d19677SJose E. Roman    Input Parameters:
2495f91af8c7SBarry Smith +     a - the PetscInt value
2496f91af8c7SBarry Smith -     b - the second value
2497f91af8c7SBarry Smith 
2498d8d19677SJose E. Roman    Output Parameter:
2499390e1bf2SBarry Smith .     c - the result as a PetscInt value,  or NULL if you do not want the result, you just want to check if it overflows
2500f91af8c7SBarry Smith 
2501bafee8b4SSatish Balay    Use PetscInt64Mult() to compute the product of two 32 bit PetscInt and store in a PetscInt64
2502f91af8c7SBarry Smith    Use PetscIntMultTruncate() to compute the product of two PetscInt and truncate it to fit in a PetscInt
2503f91af8c7SBarry Smith 
2504f5f57ec0SBarry Smith    Not available from Fortran
2505f5f57ec0SBarry Smith 
2506f91af8c7SBarry Smith    Level: advanced
2507f91af8c7SBarry Smith 
250861778c46SBarry Smith .seealso: PetscBLASInt, PetscMPIInt, PetscInt, PetscBLASIntCast(), PetscInt64Mult(), PetscIntMultError()
2509f91af8c7SBarry Smith @*/
2510f91af8c7SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIntSumError(PetscInt a,PetscInt b,PetscInt *result)
2511f91af8c7SBarry Smith {
2512bafee8b4SSatish Balay   PetscInt64 r;
2513f91af8c7SBarry Smith 
2514f91af8c7SBarry Smith   PetscFunctionBegin;
2515bafee8b4SSatish Balay   r  =  ((PetscInt64)a) + ((PetscInt64)b);
2516f91af8c7SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
251761778c46SBarry Smith   if (r > PETSC_MAX_INT) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Sum of two integers %d %d overflow, either you have an invalidly large integer error in your code or you must ./configure PETSc with --with-64-bit-indices for the case you are running",a,b);
2518f91af8c7SBarry Smith #endif
2519f91af8c7SBarry Smith   if (result) *result = (PetscInt) r;
25202f18eb33SBarry Smith   PetscFunctionReturn(0);
25212f18eb33SBarry Smith }
2522c5df96a5SBarry Smith 
2523d382aafbSBarry Smith /*
25242981ebdbSBarry Smith      The IBM include files define hz, here we hide it so that it may be used as a regular user variable.
2525d382aafbSBarry Smith */
2526d382aafbSBarry Smith #if defined(hz)
2527d382aafbSBarry Smith #  undef hz
2528d382aafbSBarry Smith #endif
2529d382aafbSBarry Smith 
253015a5570dSLisandro Dalcin #include <limits.h>
253115a5570dSLisandro Dalcin 
253215a5570dSLisandro Dalcin /* The number of bits in a byte */
253315a5570dSLisandro Dalcin 
253415a5570dSLisandro Dalcin #define PETSC_BITS_PER_BYTE CHAR_BIT
253515a5570dSLisandro Dalcin 
2536d382aafbSBarry Smith #if defined(PETSC_HAVE_SYS_TYPES_H)
2537d382aafbSBarry Smith #  include <sys/types.h>
2538d382aafbSBarry Smith #endif
2539d382aafbSBarry Smith 
2540d382aafbSBarry Smith /*MC
2541d382aafbSBarry Smith 
254266d79e26SBarry Smith     PETSC_VERSION - This manual page provides information about how PETSc documents and uses its version information. This information is available to both C/C++
254366d79e26SBarry Smith                     and Fortran compilers when petscsys.h is included.
254466d79e26SBarry Smith 
254566d79e26SBarry Smith     The current PETSc version and the API for accessing it are defined in petscversion.h
254666d79e26SBarry Smith 
254766d79e26SBarry Smith     The complete version number is given as the triple  PETSC_VERSION_MAJOR.PETSC_VERSION_MINOR.PETSC_VERSION_SUBMINOR (in short hand x.y.z)
254866d79e26SBarry Smith 
254966d79e26SBarry Smith     A change in the minor version number (y) indicates possible/likely changes in the PETSc API. Note this is different than with the semantic versioning convention
255066d79e26SBarry Smith     where only a change in the major version number (x) indicates a change in the API.
255166d79e26SBarry Smith 
255266d79e26SBarry Smith     A subminor greater than zero indicates a patch release. Version x.y.z maintains source and binary compatibility with version x.y.w for all z and w
255366d79e26SBarry Smith 
255466d79e26SBarry Smith     Use the macros PETSC_VERSION_EQ(x,y,z), PETSC_VERSION_LT(x,y,z), PETSC_VERSION_LE(x,y,z), PETSC_VERSION_GT(x,y,z),
255566d79e26SBarry Smith     PETSC_VERSION_GE(x,y,z) to determine if the current version is equal to, less than, less than or equal to, greater than or greater than or equal to a given
255666d79e26SBarry Smith     version number (x.y.z).
255766d79e26SBarry Smith 
255866d79e26SBarry Smith     PETSC_RELEASE_DATE is the date the x.y version was released (i.e. the version before any patch releases)
255966d79e26SBarry Smith 
256066d79e26SBarry Smith     PETSC_VERSION_DATE is the date the x.y.z version was released
256166d79e26SBarry Smith 
256266d79e26SBarry Smith     PETSC_VERSION_GIT is the last git commit to the repository given in the form vx.y.z-wwwww
256366d79e26SBarry Smith 
256466d79e26SBarry Smith     PETSC_VERSION_DATE_GIT is the date of the last git commit to the repository
256566d79e26SBarry Smith 
2566cbc12506SBarry Smith     PETSC_VERSION_() is deprecated and will eventually be removed.
256766d79e26SBarry Smith 
2568cbc12506SBarry Smith     Level: intermediate
256966d79e26SBarry Smith 
257066d79e26SBarry Smith M*/
257166d79e26SBarry Smith 
2572014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetArchType(char[],size_t);
2573014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetHostName(char[],size_t);
2574014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetUserName(char[],size_t);
2575014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetProgramName(char[],size_t);
2576014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetProgramName(const char[]);
2577014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetDate(char[],size_t);
257827710113SBarry Smith PETSC_EXTERN PetscErrorCode PetscGetVersion(char[], size_t);
25795f309d01SBarry Smith PETSC_EXTERN PetscErrorCode PetscGetVersionNumber(PetscInt*,PetscInt*,PetscInt*,PetscInt*);
2580d382aafbSBarry Smith 
25816a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedInt(PetscInt,const PetscInt[],PetscBool*);
25826a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedMPIInt(PetscInt,const PetscMPIInt[],PetscBool*);
25836a7c706bSVaclav Hapla PETSC_EXTERN PetscErrorCode PetscSortedReal(PetscInt,const PetscReal[],PetscBool*);
2584014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortInt(PetscInt,PetscInt[]);
2585ce605777SToby Isaac PETSC_EXTERN PetscErrorCode PetscSortReverseInt(PetscInt,PetscInt[]);
258622ab5688SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscSortedRemoveDupsInt(PetscInt*,PetscInt[]);
2587014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsInt(PetscInt*,PetscInt[]);
2588f1cab4e1SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscCheckDupsInt(PetscInt,const PetscInt[],PetscBool*);
258960e03357SMatthew G Knepley PETSC_EXTERN PetscErrorCode PetscFindInt(PetscInt, PetscInt, const PetscInt[], PetscInt*);
2590d2aeb606SJed Brown PETSC_EXTERN PetscErrorCode PetscFindMPIInt(PetscMPIInt, PetscInt, const PetscMPIInt[], PetscInt*);
2591014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithPermutation(PetscInt,const PetscInt[],PetscInt[]);
2592014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortStrWithPermutation(PetscInt,const char*[],PetscInt[]);
2593014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithArray(PetscInt,PetscInt[],PetscInt[]);
25946c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscSortIntWithArrayPair(PetscInt,PetscInt[],PetscInt[],PetscInt[]);
259517d7d925SStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortMPIInt(PetscInt,PetscMPIInt[]);
259617d7d925SStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsMPIInt(PetscInt*,PetscMPIInt[]);
2597014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithArray(PetscMPIInt,PetscMPIInt[],PetscMPIInt[]);
25985569a785SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscSortMPIIntWithIntArray(PetscMPIInt,PetscMPIInt[],PetscInt[]);
2599014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortIntWithScalarArray(PetscInt,PetscInt[],PetscScalar[]);
260017df18f2SToby Isaac PETSC_EXTERN PetscErrorCode PetscSortIntWithDataArray(PetscInt,PetscInt[],void*,size_t,void*);
2601014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortReal(PetscInt,PetscReal[]);
260239f41f7fSStefano Zampini PETSC_EXTERN PetscErrorCode PetscSortRealWithArrayInt(PetscInt,PetscReal[],PetscInt[]);
2603014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortRealWithPermutation(PetscInt,const PetscReal[],PetscInt[]);
2604745b41b2SMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscSortRemoveDupsReal(PetscInt*,PetscReal[]);
260539f41f7fSStefano Zampini PETSC_EXTERN PetscErrorCode PetscFindReal(PetscReal,PetscInt,const PetscReal[],PetscReal,PetscInt*);
2606014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortSplit(PetscInt,PetscInt,PetscScalar[],PetscInt[]);
2607014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSortSplitReal(PetscInt,PetscInt,PetscReal[],PetscInt[]);
2608014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscProcessTree(PetscInt,const PetscBool [],const PetscInt[],PetscInt*,PetscInt**,PetscInt**,PetscInt**,PetscInt**);
26096c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscMergeIntArrayPair(PetscInt,const PetscInt[],const PetscInt[],PetscInt,const PetscInt[],const PetscInt[],PetscInt*,PetscInt**,PetscInt**);
26106c2863d0SBarry Smith PETSC_EXTERN PetscErrorCode PetscMergeIntArray(PetscInt,const PetscInt[],PetscInt,const PetscInt[],PetscInt*,PetscInt**);
2611e498c390SJed Brown PETSC_EXTERN PetscErrorCode PetscMergeMPIIntArray(PetscInt,const PetscMPIInt[],PetscInt,const PetscMPIInt[],PetscInt*,PetscMPIInt**);
2612ce605777SToby Isaac PETSC_EXTERN PetscErrorCode PetscParallelSortedInt(MPI_Comm, PetscInt, const PetscInt[], PetscBool *);
2613ce605777SToby Isaac 
26144d3610e3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscTimSort(PetscInt,void*,size_t,int(*)(const void*,const void*,void*),void*);
2615676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrdered(PetscInt,PetscInt[]);
2616676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrdered(PetscInt,PetscMPIInt[]);
2617676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrdered(PetscInt,PetscReal[]);
26184d3610e3SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscTimSortWithArray(PetscInt,void*,size_t,void*,size_t,int(*)(const void*,const void*,void*),void*);
2619676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscIntSortSemiOrderedWithArray(PetscInt,PetscInt[],PetscInt[]);
2620676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscMPIIntSortSemiOrderedWithArray(PetscInt,PetscMPIInt[],PetscMPIInt[]);
2621676f2a66SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscRealSortSemiOrderedWithArrayInt(PetscInt,PetscReal[],PetscInt[]);
2622676f2a66SJacob Faibussowitsch 
2623014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDisplay(void);
2624014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetDisplay(char[],size_t);
2625d382aafbSBarry Smith 
262676bdecfbSBarry Smith /*J
2627d382aafbSBarry Smith     PetscRandomType - String with the name of a PETSc randomizer
2628d382aafbSBarry Smith 
2629d382aafbSBarry Smith    Level: beginner
2630d382aafbSBarry Smith 
2631e28ce29aSPatrick Sanan    Notes:
2632e28ce29aSPatrick Sanan    To use SPRNG or RANDOM123 you must have ./configure PETSc
263325ccb61fSToby Isaac    with the option --download-sprng or --download-random123
2634d382aafbSBarry Smith 
26358f6c3df8SBarry Smith .seealso: PetscRandomSetType(), PetscRandom, PetscRandomCreate()
263676bdecfbSBarry Smith J*/
263719fd82e9SBarry Smith typedef const char* PetscRandomType;
2638d382aafbSBarry Smith #define PETSCRAND       "rand"
2639d382aafbSBarry Smith #define PETSCRAND48     "rand48"
2640d382aafbSBarry Smith #define PETSCSPRNG      "sprng"
2641c5e4d11fSDmitry Karpeev #define PETSCRANDER48   "rander48"
264225ccb61fSToby Isaac #define PETSCRANDOM123  "random123"
2643808ba619SStefano Zampini #define PETSCCURAND     "curand"
2644d382aafbSBarry Smith 
2645d382aafbSBarry Smith /* Logging support */
2646014dd563SJed Brown PETSC_EXTERN PetscClassId PETSC_RANDOM_CLASSID;
2647d382aafbSBarry Smith 
2648607a6623SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomInitializePackage(void);
2649d382aafbSBarry Smith 
2650d382aafbSBarry Smith /* Dynamic creation and loading functions */
2651140e18c1SBarry Smith PETSC_EXTERN PetscFunctionList PetscRandomList;
2652d382aafbSBarry Smith 
2653bdf89e91SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomRegister(const char[],PetscErrorCode (*)(PetscRandom));
265419fd82e9SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomSetType(PetscRandom,PetscRandomType);
2655014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetFromOptions(PetscRandom);
265619fd82e9SBarry Smith PETSC_EXTERN PetscErrorCode PetscRandomGetType(PetscRandom,PetscRandomType*);
2657fe2efc57SMark PETSC_EXTERN PetscErrorCode PetscRandomViewFromOptions(PetscRandom,PetscObject,const char[]);
2658014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomView(PetscRandom,PetscViewer);
2659d382aafbSBarry Smith 
2660014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomCreate(MPI_Comm,PetscRandom*);
2661014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetValue(PetscRandom,PetscScalar*);
2662014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetValueReal(PetscRandom,PetscReal*);
2663808ba619SStefano Zampini PETSC_EXTERN PetscErrorCode PetscRandomGetValues(PetscRandom,PetscInt,PetscScalar*);
2664808ba619SStefano Zampini PETSC_EXTERN PetscErrorCode PetscRandomGetValuesReal(PetscRandom,PetscInt,PetscReal*);
2665014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetInterval(PetscRandom,PetscScalar*,PetscScalar*);
2666014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetInterval(PetscRandom,PetscScalar,PetscScalar);
2667014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSetSeed(PetscRandom,unsigned long);
2668014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomGetSeed(PetscRandom,unsigned long *);
2669014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomSeed(PetscRandom);
2670014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscRandomDestroy(PetscRandom*);
2671d382aafbSBarry Smith 
2672014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetFullPath(const char[],char[],size_t);
2673014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetRelativePath(const char[],char[],size_t);
2674014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetWorkingDirectory(char[],size_t);
2675014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetRealPath(const char[],char[]);
2676014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetHomeDirectory(char[],size_t);
2677014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTestFile(const char[],char,PetscBool *);
2678014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTestDirectory(const char[],char,PetscBool *);
267997540f07SSatish Balay PETSC_EXTERN PetscErrorCode PetscMkdir(const char[]);
26808a10d460SHong Zhang PETSC_EXTERN PetscErrorCode PetscMkdtemp(char[]);
268197540f07SSatish Balay PETSC_EXTERN PetscErrorCode PetscRMTree(const char[]);
2682d382aafbSBarry Smith 
268330815ce0SLisandro Dalcin PETSC_STATIC_INLINE PetscBool PetscBinaryBigEndian(void) {long _petsc_v = 1; return ((char*)&_petsc_v)[0] ? PETSC_FALSE : PETSC_TRUE;}
268430815ce0SLisandro Dalcin 
26859860990eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinaryRead(int,void*,PetscInt,PetscInt*,PetscDataType);
26869860990eSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedRead(MPI_Comm,int,void*,PetscInt,PetscInt*,PetscDataType);
2687f253e43cSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinaryWrite(int,const void*,PetscInt,PetscDataType);
2688f253e43cSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedWrite(MPI_Comm,int,const void*,PetscInt,PetscDataType);
2689014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinaryOpen(const char[],PetscFileMode,int *);
2690014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinaryClose(int);
2691014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSharedTmp(MPI_Comm,PetscBool  *);
2692014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSharedWorkingDirectory(MPI_Comm,PetscBool  *);
2693014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetTmp(MPI_Comm,char[],size_t);
2694014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFileRetrieve(MPI_Comm,const char[],char[],size_t,PetscBool *);
2695014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLs(MPI_Comm,const char[],char[],size_t,PetscBool *);
2696c891a504SStefano Zampini #if defined(PETSC_USE_SOCKET_VIEWER)
2697e2fc02c1SBarry Smith PETSC_EXTERN PetscErrorCode PetscOpenSocket(const char[],int,int*);
2698c891a504SStefano Zampini #endif
2699d382aafbSBarry Smith 
2700014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinarySeek(int,off_t,PetscBinarySeekType,off_t*);
2701014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscBinarySynchronizedSeek(MPI_Comm,int,off_t,PetscBinarySeekType,off_t*);
2702014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscByteSwap(void *,PetscDataType,PetscInt);
2703d382aafbSBarry Smith 
2704014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebugTerminal(const char[]);
2705014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebugger(const char[],PetscBool);
2706014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDefaultDebugger(void);
2707014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetDebuggerFromString(const char*);
2708014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAttachDebugger(void);
2709014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStopForDebugger(void);
27102a2a2941SBarry Smith PETSC_EXTERN PetscErrorCode PetscWaitOnError(void);
2711d382aafbSBarry Smith 
2712014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherNumberOfMessages(MPI_Comm,const PetscMPIInt[],const PetscMPIInt[],PetscMPIInt*);
2713014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths(MPI_Comm,PetscMPIInt,PetscMPIInt,const PetscMPIInt[],PetscMPIInt**,PetscMPIInt**);
2714014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGatherMessageLengths2(MPI_Comm,PetscMPIInt,PetscMPIInt,const PetscMPIInt[],const PetscMPIInt[],PetscMPIInt**,PetscMPIInt**,PetscMPIInt**);
2715014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPostIrecvInt(MPI_Comm,PetscMPIInt,PetscMPIInt,const PetscMPIInt[],const PetscMPIInt[],PetscInt***,MPI_Request**);
2716014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPostIrecvScalar(MPI_Comm,PetscMPIInt,PetscMPIInt,const PetscMPIInt[],const PetscMPIInt[],PetscScalar***,MPI_Request**);
2717cf4b5b4fSJed Brown PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSided(MPI_Comm,PetscMPIInt,MPI_Datatype,PetscMPIInt,const PetscMPIInt*,const void*,PetscMPIInt*,PetscMPIInt**,void*)
2718cf4b5b4fSJed Brown   PetscAttrMPIPointerWithType(6,3);
2719d815da10SJed Brown PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedF(MPI_Comm,PetscMPIInt,MPI_Datatype,PetscMPIInt,const PetscMPIInt[],const void*,PetscMPIInt*,PetscMPIInt**,void*,PetscMPIInt,
2720d815da10SJed Brown                                                     PetscErrorCode (*send)(MPI_Comm,const PetscMPIInt[],PetscMPIInt,PetscMPIInt,void*,MPI_Request[],void*),
2721d815da10SJed Brown                                                     PetscErrorCode (*recv)(MPI_Comm,const PetscMPIInt[],PetscMPIInt,void*,MPI_Request[],void*),void *ctx)
2722d815da10SJed Brown   PetscAttrMPIPointerWithType(6,3);
27236afbe7ddSJed Brown PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedFReq(MPI_Comm,PetscMPIInt,MPI_Datatype,PetscMPIInt,const PetscMPIInt[],const void*,PetscMPIInt*,PetscMPIInt**,void*,PetscMPIInt,
27246afbe7ddSJed Brown                                                        MPI_Request**,MPI_Request**,
27256afbe7ddSJed Brown                                                        PetscErrorCode (*send)(MPI_Comm,const PetscMPIInt[],PetscMPIInt,PetscMPIInt,void*,MPI_Request[],void*),
27266afbe7ddSJed Brown                                                        PetscErrorCode (*recv)(MPI_Comm,const PetscMPIInt[],PetscMPIInt,void*,MPI_Request[],void*),void *ctx)
27276afbe7ddSJed Brown   PetscAttrMPIPointerWithType(6,3);
2728d382aafbSBarry Smith 
27296145cd65SJed Brown PETSC_EXTERN const char *const PetscBuildTwoSidedTypes[];
27306145cd65SJed Brown PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedSetType(MPI_Comm,PetscBuildTwoSidedType);
27316145cd65SJed Brown PETSC_EXTERN PetscErrorCode PetscCommBuildTwoSidedGetType(MPI_Comm,PetscBuildTwoSidedType*);
27326145cd65SJed Brown 
2733014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSSEIsEnabled(MPI_Comm,PetscBool*,PetscBool*);
2734d382aafbSBarry Smith 
2735ce94432eSBarry Smith PETSC_EXTERN MPI_Comm PetscObjectComm(PetscObject);
2736ce94432eSBarry Smith 
273745487dadSJed Brown PETSC_EXTERN const char *const PetscSubcommTypes[];
273845487dadSJed Brown 
2739d382aafbSBarry Smith struct _n_PetscSubcomm {
2740d382aafbSBarry Smith   MPI_Comm         parent;           /* parent communicator */
2741d382aafbSBarry Smith   MPI_Comm         dupparent;        /* duplicate parent communicator, under which the processors of this subcomm have contiguous rank */
2742306c2d5bSBarry Smith   MPI_Comm         child;            /* the sub-communicator */
274345487dadSJed Brown   PetscMPIInt      n;                /* num of subcommunicators under the parent communicator */
274445487dadSJed Brown   PetscMPIInt      color;            /* color of processors belong to this communicator */
2745708d824cSJed Brown   PetscMPIInt      *subsize;         /* size of subcommunicator[color] */
274645487dadSJed Brown   PetscSubcommType type;
2747e5acf8a4SHong Zhang   char             *subcommprefix;
2748d382aafbSBarry Smith };
2749d382aafbSBarry Smith 
2750c5e4d11fSDmitry Karpeev PETSC_STATIC_INLINE MPI_Comm PetscSubcommParent(PetscSubcomm scomm) {return scomm->parent;}
2751306c2d5bSBarry Smith PETSC_STATIC_INLINE MPI_Comm PetscSubcommChild(PetscSubcomm scomm) {return scomm->child;}
275236be1a5eSBarry Smith PETSC_STATIC_INLINE MPI_Comm PetscSubcommContiguousParent(PetscSubcomm scomm) {return scomm->dupparent;}
2753014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommCreate(MPI_Comm,PetscSubcomm*);
2754014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommDestroy(PetscSubcomm*);
2755014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommSetNumber(PetscSubcomm,PetscInt);
2756014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSubcommSetType(PetscSubcomm,PetscSubcommType);
275765d9b8f1SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetTypeGeneral(PetscSubcomm,PetscMPIInt,PetscMPIInt);
2758053d1c95SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommView(PetscSubcomm,PetscViewer);
2759f68be91cSHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetFromOptions(PetscSubcomm);
2760e5acf8a4SHong Zhang PETSC_EXTERN PetscErrorCode PetscSubcommSetOptionsPrefix(PetscSubcomm,const char[]);
2761a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetParent(PetscSubcomm,MPI_Comm*);
2762a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetContiguousParent(PetscSubcomm,MPI_Comm*);
2763a530d236SBarry Smith PETSC_EXTERN PetscErrorCode PetscSubcommGetChild(PetscSubcomm,MPI_Comm*);
2764d382aafbSBarry Smith 
27659962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapCreate(PetscInt,PetscHeap*);
27669962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapAdd(PetscHeap,PetscInt,PetscInt);
27679962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapPop(PetscHeap,PetscInt*,PetscInt*);
27689962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapPeek(PetscHeap,PetscInt*,PetscInt*);
27699962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapStash(PetscHeap,PetscInt,PetscInt);
27709962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapUnstash(PetscHeap);
27719962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapDestroy(PetscHeap*);
27729962606eSBarry Smith PETSC_EXTERN PetscErrorCode PetscHeapView(PetscHeap,PetscViewer);
27739962606eSBarry Smith 
27745e71baefSBarry Smith PETSC_EXTERN PetscErrorCode PetscProcessPlacementView(PetscViewer);
27755f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGet(MPI_Comm,PetscShmComm*);
27765f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGlobalToLocal(PetscShmComm,PetscMPIInt,PetscMPIInt*);
27775f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommLocalToGlobal(PetscShmComm,PetscMPIInt,PetscMPIInt*);
27785f7487a0SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscShmCommGetMpiShmComm(PetscShmComm,MPI_Comm*);
27795e71baefSBarry Smith 
2780a32e93adSJunchao Zhang /* routines to better support OpenMP multithreading needs of some PETSc third party libraries */
2781a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlCreate(MPI_Comm,PetscInt,PetscOmpCtrl*);
2782a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlGetOmpComms(PetscOmpCtrl,MPI_Comm*,MPI_Comm*,PetscBool*);
2783a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlDestroy(PetscOmpCtrl*);
2784a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlBarrier(PetscOmpCtrl);
2785a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterBegin(PetscOmpCtrl);
2786a32e93adSJunchao Zhang PETSC_EXTERN PetscErrorCode PetscOmpCtrlOmpRegionOnMasterEnd(PetscOmpCtrl);
2787a32e93adSJunchao Zhang 
278813e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferCreate(size_t,size_t,PetscSegBuffer*);
27890f453b92SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferDestroy(PetscSegBuffer*);
279013e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferGet(PetscSegBuffer,size_t,void*);
2791137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractAlloc(PetscSegBuffer,void*);
2792137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractTo(PetscSegBuffer,void*);
2793137cf7b6SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferExtractInPlace(PetscSegBuffer,void*);
279413e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferGetSize(PetscSegBuffer,size_t*);
279513e3f751SJed Brown PETSC_EXTERN PetscErrorCode PetscSegBufferUnuse(PetscSegBuffer,size_t);
27960f453b92SJed Brown 
2797471ecdacSJed Brown /* Type-safe wrapper to encourage use of PETSC_RESTRICT. Does not use PetscFunctionBegin because the error handling
2798471ecdacSJed Brown  * prevents the compiler from completely erasing the stub. This is called in inner loops so it has to be as fast as
2799471ecdacSJed Brown  * possible. */
28006a11cab8SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscSegBufferGetInts(PetscSegBuffer seg,size_t count,PetscInt *PETSC_RESTRICT *slot) {return PetscSegBufferGet(seg,count,(void**)slot);}
2801d382aafbSBarry Smith 
28029de0f6ecSBarry Smith extern PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton;
28039de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted*);
28049de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted*);
28059de0f6ecSBarry Smith PETSC_EXTERN PetscErrorCode PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted,const char*,const char*,PetscBool*);
28069de0f6ecSBarry Smith 
2807ace2fbaeSBarry Smith #include <stdarg.h>
2808ace2fbaeSBarry Smith PETSC_EXTERN PetscErrorCode PetscVSNPrintf(char*,size_t,const char[],size_t*,va_list);
2809ace2fbaeSBarry Smith PETSC_EXTERN PetscErrorCode (*PetscVFPrintf)(FILE*,const char[],va_list);
2810ace2fbaeSBarry Smith 
2811268b0dfdSSatish Balay PETSC_EXTERN PetscSegBuffer PetscCitationsList;
2812fbfcfee5SBarry Smith 
28131f817a21SBarry Smith /*@C
28141f817a21SBarry Smith       PetscCitationsRegister - Register a bibtex item to obtain credit for an implemented algorithm used in the code.
28151f817a21SBarry Smith 
28161f817a21SBarry Smith      Not Collective - only what is registered on rank 0 of PETSC_COMM_WORLD will be printed
28171f817a21SBarry Smith 
28181f817a21SBarry Smith      Input Parameters:
28191f817a21SBarry Smith +      cite - the bibtex item, formated to displayed on multiple lines nicely
28201f817a21SBarry Smith -      set - a boolean variable initially set to PETSC_FALSE; this is used to insure only a single registration of the citation
28211f817a21SBarry Smith 
2822850545d0SSatish Balay    Level: intermediate
2823850545d0SSatish Balay 
2824f5f57ec0SBarry Smith    Not available from Fortran
2825f5f57ec0SBarry Smith 
28261f817a21SBarry Smith      Options Database:
2827063414e0SPatrick Sanan .     -citations [filename]   - print out the bibtex entries for the given computation
28281f817a21SBarry Smith @*/
2829dff31646SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscCitationsRegister(const char cit[],PetscBool *set)
2830dff31646SBarry Smith {
2831dff31646SBarry Smith   size_t         len;
2832dff31646SBarry Smith   char           *vstring;
2833dff31646SBarry Smith   PetscErrorCode ierr;
2834dff31646SBarry Smith 
28351f817a21SBarry Smith   PetscFunctionBegin;
28368d3683fcSBarry Smith   if (set && *set) PetscFunctionReturn(0);
2837dff31646SBarry Smith   ierr = PetscStrlen(cit,&len);CHKERRQ(ierr);
283835005c2bSLisandro Dalcin   ierr = PetscSegBufferGet(PetscCitationsList,len,&vstring);CHKERRQ(ierr);
2839580bdb30SBarry Smith   ierr = PetscArraycpy(vstring,cit,len);CHKERRQ(ierr);
2840dff31646SBarry Smith   if (set) *set = PETSC_TRUE;
28411f817a21SBarry Smith   PetscFunctionReturn(0);
2842dff31646SBarry Smith }
2843dff31646SBarry Smith 
2844b967cddfSBarry Smith PETSC_EXTERN PetscErrorCode PetscURLShorten(const char[],char[],size_t);
2845fe278a28SBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveAuthorize(MPI_Comm,char[],char[],size_t);
2846fe278a28SBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveRefresh(MPI_Comm,const char[],char[],size_t);
2847f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscGoogleDriveUpload(MPI_Comm,const char[],const char []);
2848fe278a28SBarry Smith 
2849f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscBoxAuthorize(MPI_Comm,char[],char[],size_t);
2850f044a08eSBarry Smith PETSC_EXTERN PetscErrorCode PetscBoxRefresh(MPI_Comm,const char[],char[],char[],size_t);
2851b967cddfSBarry Smith 
28529f4d3c52SBarry Smith PETSC_EXTERN PetscErrorCode PetscGlobusGetTransfers(MPI_Comm,const char[],char[],size_t);
28539f4d3c52SBarry Smith 
285404102261SBarry Smith PETSC_EXTERN PetscErrorCode PetscTextBelt(MPI_Comm,const char[],const char[],PetscBool*);
2855763ec7b1SBarry Smith PETSC_EXTERN PetscErrorCode PetscTellMyCell(MPI_Comm,const char[],const char[],PetscBool*);
2856dff31646SBarry Smith 
285768e69593SBarry Smith PETSC_EXTERN PetscErrorCode PetscPullJSONValue(const char[],const char[],char[],size_t,PetscBool*);
28585dc0f0a4SBarry Smith PETSC_EXTERN PetscErrorCode PetscPushJSONValue(char[],const char[],const char[],size_t);
2859d382aafbSBarry Smith 
2860b2566f29SBarry Smith #if defined(PETSC_USE_DEBUG)
2861503e1a2cSLisandro Dalcin PETSC_STATIC_INLINE unsigned int PetscStrHash(const char *str)
2862df05ca09SBarry Smith {
2863df05ca09SBarry Smith   unsigned int c,hash = 5381;
2864df05ca09SBarry Smith 
2865503e1a2cSLisandro Dalcin   while ((c = (unsigned int)*str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
2866df05ca09SBarry Smith   return hash;
2867df05ca09SBarry Smith }
286877a5e07dSBarry Smith 
286977a5e07dSBarry Smith /*MC
287077a5e07dSBarry Smith    MPIU_Allreduce - a PETSc replacement for MPI_Allreduce() that tries to determine if the call from all the MPI processes occur from the
287177a5e07dSBarry Smith                     same place in the PETSc code. This helps to detect bugs where different MPI processes follow different code paths
287277a5e07dSBarry Smith                     resulting in inconsistent and incorrect calls to MPI_Allreduce().
287377a5e07dSBarry Smith 
2874d083f849SBarry Smith    Collective
287577a5e07dSBarry Smith 
287677a5e07dSBarry Smith    Synopsis:
287777a5e07dSBarry Smith      #include <petscsys.h>
287877a5e07dSBarry Smith      PetscErrorCode MPIU_Allreduce(void *indata,void *outdata,PetscMPIInt count,MPI_Datatype datatype, MPI_Op op, MPI_Comm comm);
287977a5e07dSBarry Smith 
288077a5e07dSBarry Smith    Input Parameters:
2881df05ca09SBarry Smith +  a - pointer to the input data to be reduced
2882df05ca09SBarry Smith .  c - the number of MPI data items in a and b
2883df05ca09SBarry Smith .  d - the MPI datatype, for example MPI_INT
2884df05ca09SBarry Smith .  e - the MPI operation, for example MPI_SUM
2885df05ca09SBarry Smith -  fcomm - the MPI communicator on which the operation occurs
288677a5e07dSBarry Smith 
288777a5e07dSBarry Smith    Output Parameter:
2888df05ca09SBarry Smith .  b - the reduced values
288977a5e07dSBarry Smith 
2890e28ce29aSPatrick Sanan    Notes:
2891e28ce29aSPatrick Sanan      In optimized mode this directly calls MPI_Allreduce()
289277a5e07dSBarry Smith 
2893df05ca09SBarry Smith      This is defined as a macro that can return error codes internally so it cannot be used in a subroutine that returns void.
2894df05ca09SBarry Smith 
2895df05ca09SBarry Smith      The error code this returns should be checked with CHKERRMPI()
2896df05ca09SBarry Smith 
289777a5e07dSBarry Smith    Level: developer
289877a5e07dSBarry Smith 
289977a5e07dSBarry Smith .seealso: MPI_Allreduce()
290077a5e07dSBarry Smith M*/
2901df05ca09SBarry Smith #define MPIU_Allreduce(a,b,c,d,e,fcomm) MPI_SUCCESS; do {               \
2902df05ca09SBarry Smith   PetscErrorCode _4_ierr;                                               \
2903df05ca09SBarry Smith   PetscMPIInt    a_b1[6],a_b2[6];                                       \
29043ca90d2dSJacob Faibussowitsch   int            _mpiu_allreduce_c_int = (int)c;                        \
2905df05ca09SBarry Smith   a_b1[0] = -(PetscMPIInt)__LINE__;                          a_b1[1] = -a_b1[0];\
2906df05ca09SBarry Smith   a_b1[2] = -(PetscMPIInt)PetscStrHash(PETSC_FUNCTION_NAME); a_b1[3] = -a_b1[2];\
2907df05ca09SBarry Smith   a_b1[4] = -(PetscMPIInt)(c);                               a_b1[5] = -a_b1[4];\
2908df05ca09SBarry Smith   _4_ierr = MPI_Allreduce(a_b1,a_b2,6,MPI_INT,MPI_MAX,fcomm);CHKERRMPI(_4_ierr);\
2909df05ca09SBarry Smith   if (-a_b2[0] != a_b2[1]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPI_Allreduce() called in different locations (code lines) on different processors");\
2910df05ca09SBarry Smith   if (-a_b2[2] != a_b2[3]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPI_Allreduce() called in different locations (functions) on different processors");\
29113ca90d2dSJacob Faibussowitsch   if (-a_b2[4] != a_b2[5]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"MPI_Allreduce() called with different counts %d on different processors",_mpiu_allreduce_c_int); \
2912df05ca09SBarry Smith   _4_ierr = MPI_Allreduce((a),(b),(c),d,e,(fcomm));CHKERRMPI(_4_ierr);\
2913df05ca09SBarry Smith   } while (0)
2914b2566f29SBarry Smith #else
2915df05ca09SBarry Smith #define MPIU_Allreduce(a,b,c,d,e,fcomm) MPI_Allreduce((a),(b),(c),d,e,(fcomm))
2916b2566f29SBarry Smith #endif
2917b2566f29SBarry Smith 
2918b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
29198198064fSBarry Smith PETSC_EXTERN PetscErrorCode MPIU_Win_allocate_shared(MPI_Aint,PetscMPIInt,MPI_Info,MPI_Comm,void*,MPI_Win*);
29208198064fSBarry Smith PETSC_EXTERN PetscErrorCode MPIU_Win_shared_query(MPI_Win,PetscMPIInt,MPI_Aint*,PetscMPIInt*,void*);
29212f065d89SBarry Smith #endif
29228198064fSBarry Smith 
2923cd88fca5SStefano Zampini /* this is a vile hack */
2924cd88fca5SStefano Zampini #if defined(PETSC_HAVE_NECMPI)
2925961fb248SStefano Zampini #if !defined(PETSC_NECMPI_VERSION_MAJOR) ||                              \
2926961fb248SStefano Zampini     !defined(PETSC_NECMPI_VERSION_MINOR) ||                              \
2927961fb248SStefano Zampini     PETSC_NECMPI_VERSION_MAJOR < 2       ||                              \
2928961fb248SStefano Zampini     (PETSC_NECMPI_VERSION_MAJOR == 2 && PETSC_NECMPI_VERSION_MINOR < 18)
2929cd88fca5SStefano Zampini #define MPI_Type_free(a) (*(a) = MPI_DATATYPE_NULL,0);
2930cd88fca5SStefano Zampini #endif
2931961fb248SStefano Zampini #endif
2932cd88fca5SStefano Zampini 
293312801b39SBarry Smith /*
293460fbe2beSVaclav Hapla     List of external packages and queries on it
293560fbe2beSVaclav Hapla */
293660fbe2beSVaclav Hapla PETSC_EXTERN PetscErrorCode  PetscHasExternalPackage(const char[],PetscBool*);
293760fbe2beSVaclav Hapla 
2938a52b435bSMark Adams /*
2939a52b435bSMark Adams  OpenMP support
2940a52b435bSMark Adams */
2941a52b435bSMark Adams #if defined(_OPENMP)
2942a52b435bSMark Adams #define PetscPragmaOMP(...) _Pragma(PetscStringize(omp __VA_ARGS__))
2943a52b435bSMark Adams #else // no OpenMP so no threads
2944a52b435bSMark Adams #define PetscPragmaOMP(...)
2945a52b435bSMark Adams #endif
2946a52b435bSMark Adams 
2947d382aafbSBarry Smith #endif
2948