xref: /petsc/include/petscsys.h (revision 7087cfbefd1a42b179f217f9994fb6cb0d0c1824)
1 /*
2    This is the main PETSc include file (for C and C++).  It is included by all
3    other PETSc include files, so it almost never has to be specifically included.
4 */
5 #if !defined(__PETSCSYS_H)
6 #define __PETSCSYS_H
7 /* ========================================================================== */
8 /*
9    petscconf.h is contained in ${PETSC_ARCH}/include/petscconf.h it is
10    found automatically by the compiler due to the -I${PETSC_DIR}/${PETSC_ARCH}/include
11    in the conf/variables definition of PETSC_INCLUDE
12 */
13 #include "petscconf.h"
14 #include "petscfix.h"
15 
16 /* ========================================================================== */
17 /*
18    This facilitates using C version of PETSc from C++ and
19    C++ version from C. Use --with-c-support --with-clanguage=c++ with ./configure for the latter)
20 */
21 #if defined(PETSC_CLANGUAGE_CXX) && !defined(PETSC_USE_EXTERN_CXX) && !defined(__cplusplus)
22 #error "PETSc configured with --with-clanguage=c++ and NOT --with-c-support - it can be used only with a C++ compiler"
23 #endif
24 
25 #if defined(__cplusplus)
26 #  define PETSC_FUNCTION_NAME PETSC_FUNCTION_NAME_CXX
27 #else
28 #  define PETSC_FUNCTION_NAME PETSC_FUNCTION_NAME_C
29 #endif
30 
31 #if defined(PETSC_USE_EXTERN_CXX) && defined(__cplusplus)
32 #define PETSC_EXTERN_CXX_BEGIN extern "C" {
33 #define PETSC_EXTERN_CXX_END  }
34 #else
35 #define PETSC_EXTERN_CXX_BEGIN
36 #define PETSC_EXTERN_CXX_END
37 #endif
38 /* ========================================================================== */
39 /*
40    Current PETSc version number and release date. Also listed in
41     Web page
42     src/docs/tex/manual/intro.tex,
43     src/docs/tex/manual/manual.tex.
44     src/docs/website/index.html.
45 */
46 #include "petscversion.h"
47 #define PETSC_AUTHOR_INFO        "       The PETSc Team\n    petsc-maint@mcs.anl.gov\n http://www.mcs.anl.gov/petsc/\n"
48 #if (PETSC_VERSION_RELEASE == 1)
49 #define PetscGetVersion(version,len) PetscSNPrintf(version,len,"Petsc Release Version %d.%d.%d, Patch %d, %s ", \
50                                          PETSC_VERSION_MAJOR,PETSC_VERSION_MINOR, PETSC_VERSION_SUBMINOR, \
51 					 PETSC_VERSION_PATCH,PETSC_VERSION_PATCH_DATE)
52 #else
53 #define PetscGetVersion(version,len) PetscSNPrintf(version,len,"Petsc Development HG revision: %s  HG Date: %s", \
54                                         PETSC_VERSION_HG, PETSC_VERSION_DATE_HG)
55 #endif
56 
57 /*MC
58     PetscGetVersion - Gets the PETSc version information in a string.
59 
60     Input Parameter:
61 .   len - length of the string
62 
63     Output Parameter:
64 .   version - version string
65 
66     Level: developer
67 
68     Usage:
69     char version[256];
70     ierr = PetscGetVersion(version,256);CHKERRQ(ierr)
71 
72     Fortran Note:
73     This routine is not supported in Fortran.
74 
75 .seealso: PetscGetProgramName()
76 
77 M*/
78 
79 /* ========================================================================== */
80 
81 /*
82     Defines the interface to MPI allowing the use of all MPI functions.
83 
84     PETSc does not use the C++ binding of MPI at ALL. The following flag
85     makes sure the C++ bindings are not included. The C++ bindings REQUIRE
86     putting mpi.h before ANY C++ include files, we cannot control this
87     with all PETSc users. Users who want to use the MPI C++ bindings can include
88     mpicxx.h directly in their code
89 */
90 #define MPICH_SKIP_MPICXX 1
91 #define OMPI_SKIP_MPICXX 1
92 #include "mpi.h"
93 
94 /*
95     Yuck, we need to put stdio.h AFTER mpi.h for MPICH2 with C++ compiler
96     see the top of mpicxx.h in the MPICH2 distribution.
97 
98     The MPI STANDARD HAS TO BE CHANGED to prevent this nonsense.
99 */
100 #include <stdio.h>
101 
102 /* MSMPI on 32bit windows requires this yukky hack - that breaks MPI standard compliance */
103 #if !defined(MPIAPI)
104 #define MPIAPI
105 #endif
106 
107 
108 /*MC
109     PetscErrorCode - datatype used for return error code from all PETSc functions
110 
111     Level: beginner
112 
113 .seealso: CHKERRQ, SETERRQ
114 M*/
115 typedef int PetscErrorCode;
116 
117 /*MC
118 
119     PetscClassId - A unique id used to identify each PETSc class.
120          (internal integer in the data structure used for error
121          checking). These are all defined by an offset from the lowest
122          one, PETSC_SMALLEST_CLASSID.
123 
124     Level: advanced
125 
126 .seealso: PetscClassIdRegister(), PetscLogEventRegister(), PetscHeaderCreate()
127 M*/
128 typedef int PetscClassId;
129 
130 
131 /*MC
132     PetscBLASInt - datatype used to represent 'int' parameters to BLAS/LAPACK functions.
133 
134     Level: intermediate
135 
136     Notes: usually this is the same as PetscInt, but if PETSc was built with --with-64-bit-indices but
137            standard C/Fortran integers are 32 bit then this is NOT the same as PetscInt it remains 32 bit
138            (except on very rare BLAS/LAPACK implementations that support 64 bit integers).
139 
140     PetscBLASIntCheck(a) checks if the given PetscInt a will fit in a PetscBLASInt, if not it generates a
141       PETSC_ERR_ARG_OUTOFRANGE.
142 
143     PetscBLASInt b = PetscBLASIntCast(a) checks if the given PetscInt a will fit in a PetscBLASInt, if not it
144       generates a PETSC_ERR_ARG_OUTOFRANGE
145 
146 .seealso: PetscMPIInt, PetscInt
147 
148 M*/
149 typedef int PetscBLASInt;
150 
151 /*MC
152     PetscMPIInt - datatype used to represent 'int' parameters to MPI functions.
153 
154     Level: intermediate
155 
156     Notes: usually this is the same as PetscInt, but if PETSc was built with --with-64-bit-indices but
157            standard C/Fortran integers are 32 bit then this is NOT the same as PetscInt it remains 32 bit
158 
159     PetscMPIIntCheck(a) checks if the given PetscInt a will fit in a PetscMPIInt, if not it generates a
160       PETSC_ERR_ARG_OUTOFRANGE.
161 
162     PetscMPIInt b = PetscMPIIntCast(a) checks if the given PetscInt a will fit in a PetscMPIInt, if not it
163       generates a PETSC_ERR_ARG_OUTOFRANGE
164 
165 .seealso: PetscBLASInt, PetscInt
166 
167 M*/
168 typedef int PetscMPIInt;
169 
170 /*MC
171     PetscEnum - datatype used to pass enum types within PETSc functions.
172 
173     Level: intermediate
174 
175     PetscMPIIntCheck(a) checks if the given PetscInt a will fit in a PetscMPIInt, if not it generates a
176       PETSC_ERR_ARG_OUTOFRANGE.
177 
178     PetscMPIInt b = PetscMPIIntCast(a) checks if the given PetscInt a will fit in a PetscMPIInt, if not it
179       generates a PETSC_ERR_ARG_OUTOFRANGE
180 
181 .seealso: PetscOptionsGetEnum(), PetscOptionsEnum(), PetscBagRegisterEnum()
182 M*/
183 typedef enum { ENUM_DUMMY } PetscEnum;
184 
185 /*MC
186     PetscInt - PETSc type that represents integer - used primarily to
187       represent size of arrays and indexing into arrays. Its size can be configured with the option
188       --with-64-bit-indices - to be either 32bit or 64bit [default 32 bit ints]
189 
190    Level: intermediate
191 
192 .seealso: PetscScalar, PetscBLASInt, PetscMPIInt
193 M*/
194 #if defined(PETSC_USE_64BIT_INDICES)
195 typedef long long PetscInt;
196 #define MPIU_INT MPI_LONG_LONG_INT
197 #else
198 typedef int PetscInt;
199 #define MPIU_INT MPI_INT
200 #endif
201 
202 /*EC
203 
204     PetscPrecision - indicates what precision the object is using
205 
206     Level: advanced
207 
208 .seealso: PetscObjectSetPrecision()
209 E*/
210 typedef enum { PETSC_PRECISION_SINGLE=4,PETSC_PRECISION_DOUBLE=8 } PetscPrecision;
211 extern const char *PetscPrecisions[];
212 
213 
214 /*
215     For the rare cases when one needs to send a size_t object with MPI
216 */
217 #if (PETSC_SIZEOF_SIZE_T) == (PETSC_SIZEOF_INT)
218 #define MPIU_SIZE_T MPI_INT
219 #elif  (PETSC_SIZEOF_SIZE_T) == (PETSC_SIZEOF_LONG)
220 #define MPIU_SIZE_T MPI_LONG
221 #elif  (PETSC_SIZEOF_SIZE_T) == (PETSC_SIZEOF_LONG_LONG)
222 #define MPIU_SIZE_T MPI_LONG_LONG_INT
223 #else
224 #error "Unknown size for size_t! Send us a bugreport at petsc-maint@mcs.anl.gov"
225 #endif
226 
227 
228 /*
229       You can use PETSC_STDOUT as a replacement of stdout. You can also change
230     the value of PETSC_STDOUT to redirect all standard output elsewhere
231 */
232 
233 extern FILE* PETSC_STDOUT;
234 
235 /*
236       You can use PETSC_STDERR as a replacement of stderr. You can also change
237     the value of PETSC_STDERR to redirect all standard error elsewhere
238 */
239 extern FILE* PETSC_STDERR;
240 
241 /*
242       PETSC_ZOPEFD is used to send data to the PETSc webpage.  It can be used
243     in conjunction with PETSC_STDOUT, or by itself.
244 */
245 extern FILE* PETSC_ZOPEFD;
246 
247 #if !defined(PETSC_USE_EXTERN_CXX) && defined(__cplusplus)
248 /*MC
249       PetscPolymorphicSubroutine - allows defining a C++ polymorphic version of
250             a PETSc function that remove certain optional arguments for a simplier user interface
251 
252    Synopsis:
253    PetscPolymorphicSubroutine(Functionname,(arguments of C++ function),(arguments of C function))
254 
255      Not collective
256 
257    Level: developer
258 
259     Example:
260       PetscPolymorphicSubroutine(VecNorm,(Vec x,PetscReal *r),(x,NORM_2,r)) generates the new routine
261            PetscErrorCode VecNorm(Vec x,PetscReal *r) = VecNorm(x,NORM_2,r)
262 
263 .seealso: PetscPolymorphicFunction()
264 
265 M*/
266 #define PetscPolymorphicSubroutine(A,B,C) PETSC_STATIC_INLINE PetscErrorCode A B {return A C;}
267 
268 /*MC
269       PetscPolymorphicScalar - allows defining a C++ polymorphic version of
270             a PETSc function that replaces a PetscScalar * argument with a PetscScalar argument
271 
272    Synopsis:
273    PetscPolymorphicScalar(Functionname,(arguments of C++ function),(arguments of C function))
274 
275    Not collective
276 
277    Level: developer
278 
279     Example:
280       PetscPolymorphicScalar(VecAXPY,(PetscScalar _val,Vec x,Vec y),(&_Val,x,y)) generates the new routine
281            PetscErrorCode VecAXPY(PetscScalar _val,Vec x,Vec y) = {PetscScalar _Val = _val; return VecAXPY(&_Val,x,y);}
282 
283 .seealso: PetscPolymorphicFunction(),PetscPolymorphicSubroutine()
284 
285 M*/
286 #define PetscPolymorphicScalar(A,B,C) PETSC_STATIC_INLINE PetscErrorCode A B {PetscScalar _Val = _val; return A C;}
287 
288 /*MC
289       PetscPolymorphicFunction - allows defining a C++ polymorphic version of
290             a PETSc function that remove certain optional arguments for a simplier user interface
291             and returns the computed value (istead of an error code)
292 
293    Synopsis:
294    PetscPolymorphicFunction(Functionname,(arguments of C++ function),(arguments of C function),return type,return variable name)
295 
296      Not collective
297 
298    Level: developer
299 
300     Example:
301       PetscPolymorphicFunction(VecNorm,(Vec x,NormType t),(x,t,&r),PetscReal,r) generates the new routine
302          PetscReal VecNorm(Vec x,NormType t) = {PetscReal r; VecNorm(x,t,&r); return r;}
303 
304 .seealso: PetscPolymorphicSubroutine()
305 
306 M*/
307 #define PetscPolymorphicFunction(A,B,C,D,E) PETSC_STATIC_INLINE D A B {D E; A C;return E;}
308 
309 #else
310 #define PetscPolymorphicSubroutine(A,B,C)
311 #define PetscPolymorphicScalar(A,B,C)
312 #define PetscPolymorphicFunction(A,B,C,D,E)
313 #endif
314 
315 /*MC
316     PetscUnlikely - hints the compiler that the given condition is usually FALSE
317 
318     Synopsis:
319     PetscBool  PetscUnlikely(PetscBool  cond)
320 
321     Not Collective
322 
323     Input Parameters:
324 .   cond - condition or expression
325 
326     Note: This returns the same truth value, it is only a hint to compilers that the resulting
327     branch is unlikely.
328 
329     Level: advanced
330 
331 .seealso: PetscLikely(), CHKERRQ
332 M*/
333 
334 /*MC
335     PetscLikely - hints the compiler that the given condition is usually TRUE
336 
337     Synopsis:
338     PetscBool  PetscUnlikely(PetscBool  cond)
339 
340     Not Collective
341 
342     Input Parameters:
343 .   cond - condition or expression
344 
345     Note: This returns the same truth value, it is only a hint to compilers that the resulting
346     branch is likely.
347 
348     Level: advanced
349 
350 .seealso: PetscUnlikely()
351 M*/
352 #if defined(PETSC_HAVE_BUILTIN_EXPECT)
353 #  define PetscUnlikely(cond)   __builtin_expect(!!(cond),0)
354 #  define PetscLikely(cond)     __builtin_expect(!!(cond),1)
355 #else
356 #  define PetscUnlikely(cond)   (cond)
357 #  define PetscLikely(cond)     (cond)
358 #endif
359 
360 /*
361     Extern indicates a PETSc function defined elsewhere
362 */
363 #if !defined(EXTERN)
364 #define extern extern
365 #endif
366 
367 /*
368     Defines some elementary mathematics functions and constants.
369 */
370 #include "petscmath.h"
371 
372 /*
373     Declare extern C stuff after including external header files
374 */
375 
376 PETSC_EXTERN_CXX_BEGIN
377 
378 /*
379        Basic PETSc constants
380 */
381 
382 /*E
383     PetscBool  - Logical variable. Actually an int in C and a logical in Fortran.
384 
385    Level: beginner
386 
387    Developer Note: Why have PetscBool , why not use bool in C? The problem is that K and R C, C99 and C++ all have different mechanisms for
388       boolean values. It is not easy to have a simple macro that that will work properly in all circumstances with all three mechanisms.
389 
390 E*/
391 typedef enum { PETSC_FALSE,PETSC_TRUE } PetscBool;
392 extern const char *PetscBools[];
393 
394 /*E
395     PetscCopyMode  - Determines how an array passed to certain functions is copied or retained
396 
397    Level: beginner
398 
399 $   PETSC_COPY_VALUES - the array values are copied into new space, the user is free to reuse or delete the passed in array
400 $   PETSC_OWN_POINTER - the array values are NOT copied, the object takes ownership of the array and will free it later, the user cannot change or
401 $                       delete the array. The array MUST have been obtained with PetscMalloc(). Hence this mode cannot be used in Fortran.
402 $   PETSC_USE_POINTER - the array values are NOT copied, the object uses the array but does NOT take ownership of the array. The user cannot use
403                         the array but the user must delete the array after the object is destroyed.
404 
405 E*/
406 typedef enum { PETSC_COPY_VALUES, PETSC_OWN_POINTER, PETSC_USE_POINTER} PetscCopyMode;
407 extern const char *PetscCopyModes[];
408 
409 /*MC
410     PETSC_FALSE - False value of PetscBool
411 
412     Level: beginner
413 
414     Note: Zero integer
415 
416 .seealso: PetscBool , PETSC_TRUE
417 M*/
418 
419 /*MC
420     PETSC_TRUE - True value of PetscBool
421 
422     Level: beginner
423 
424     Note: Nonzero integer
425 
426 .seealso: PetscBool , PETSC_FALSE
427 M*/
428 
429 /*MC
430     PETSC_NULL - standard way of passing in a null or array or pointer
431 
432    Level: beginner
433 
434    Notes: accepted by many PETSc functions to not set a parameter and instead use
435           some default
436 
437           This macro does not exist in Fortran; you must use PETSC_NULL_INTEGER,
438           PETSC_NULL_DOUBLE_PRECISION, PETSC_NULL_FUNCTION, PETSC_NULL_OBJECT etc
439 
440   Developer Note: Why have PETSC_NULL, why not just use NULL? The problem is that NULL is defined in different include files under
441       different versions of Unix. It is tricky to insure the correct include file is always included.
442 
443 .seealso: PETSC_DECIDE, PETSC_DEFAULT, PETSC_IGNORE, PETSC_DETERMINE
444 
445 M*/
446 #define PETSC_NULL           0
447 
448 /*MC
449     PETSC_DECIDE - standard way of passing in integer or floating point parameter
450        where you wish PETSc to use the default.
451 
452    Level: beginner
453 
454 .seealso: PETSC_NULL, PETSC_DEFAULT, PETSC_IGNORE, PETSC_DETERMINE
455 
456 M*/
457 #define PETSC_DECIDE         -1
458 
459 /*MC
460     PETSC_DEFAULT - standard way of passing in integer or floating point parameter
461        where you wish PETSc to use the default.
462 
463    Level: beginner
464 
465    Fortran Notes: You need to use PETSC_DEFAULT_INTEGER or PETSC_DEFAULT_DOUBLE_PRECISION.
466 
467 .seealso: PETSC_DECIDE, PETSC_NULL, PETSC_IGNORE, PETSC_DETERMINE
468 
469 M*/
470 #define PETSC_DEFAULT        -2
471 
472 
473 /*MC
474     PETSC_IGNORE - same as PETSC_NULL, means PETSc will ignore this argument
475 
476    Level: beginner
477 
478    Note: accepted by many PETSc functions to not set a parameter and instead use
479           some default
480 
481    Fortran Notes: This macro does not exist in Fortran; you must use PETSC_NULL_INTEGER,
482           PETSC_NULL_DOUBLE_PRECISION etc
483 
484 .seealso: PETSC_DECIDE, PETSC_DEFAULT, PETSC_NULL, PETSC_DETERMINE
485 
486 M*/
487 #define PETSC_IGNORE         PETSC_NULL
488 
489 /*MC
490     PETSC_DETERMINE - standard way of passing in integer or floating point parameter
491        where you wish PETSc to compute the required value.
492 
493    Level: beginner
494 
495 .seealso: PETSC_DECIDE, PETSC_DEFAULT, PETSC_IGNORE, PETSC_NULL, VecSetSizes()
496 
497 M*/
498 #define PETSC_DETERMINE      PETSC_DECIDE
499 
500 /*MC
501     PETSC_COMM_WORLD - the equivalent of the MPI_COMM_WORLD communicator which represents
502            all the processs that PETSc knows about.
503 
504    Level: beginner
505 
506    Notes: By default PETSC_COMM_WORLD and MPI_COMM_WORLD are identical unless you wish to
507           run PETSc on ONLY a subset of MPI_COMM_WORLD. In that case create your new (smaller)
508           communicator, call it, say comm, and set PETSC_COMM_WORLD = comm BEFORE calling
509           PetscInitialize()
510 
511 .seealso: PETSC_COMM_SELF
512 
513 M*/
514 extern MPI_Comm PETSC_COMM_WORLD;
515 
516 /*MC
517     PETSC_COMM_SELF - This is always MPI_COMM_SELF
518 
519    Level: beginner
520 
521 .seealso: PETSC_COMM_WORLD
522 
523 M*/
524 #define PETSC_COMM_SELF MPI_COMM_SELF
525 
526 extern  PetscBool  PetscInitializeCalled;
527 extern  PetscBool  PetscFinalizeCalled;
528 
529 extern PetscErrorCode  PetscSetHelpVersionFunctions(PetscErrorCode (*)(MPI_Comm),PetscErrorCode (*)(MPI_Comm));
530 extern PetscErrorCode  PetscCommDuplicate(MPI_Comm,MPI_Comm*,int*);
531 extern PetscErrorCode  PetscCommDestroy(MPI_Comm*);
532 
533 /*MC
534    PetscMalloc - Allocates memory
535 
536    Synopsis:
537    PetscErrorCode PetscMalloc(size_t m,void **result)
538 
539    Not Collective
540 
541    Input Parameter:
542 .  m - number of bytes to allocate
543 
544    Output Parameter:
545 .  result - memory allocated
546 
547    Level: beginner
548 
549    Notes: Memory is always allocated at least double aligned
550 
551           If you request memory of zero size it will allocate no space and assign the pointer to 0; PetscFree() will
552           properly handle not freeing the null pointer.
553 
554 .seealso: PetscFree(), PetscNew()
555 
556   Concepts: memory allocation
557 
558 M*/
559 #define PetscMalloc(a,b)  ((a != 0) ? (*PetscTrMalloc)((a),__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,(void**)(b)) : (*(b) = 0,0) )
560 
561 /*MC
562    PetscAddrAlign - Rounds up an address to PETSC_MEMALIGN alignment
563 
564    Synopsis:
565    void *PetscAddrAlign(void *addr)
566 
567    Not Collective
568 
569    Input Parameters:
570 .  addr - address to align (any pointer type)
571 
572    Level: developer
573 
574 .seealso: PetscMallocAlign()
575 
576   Concepts: memory allocation
577 M*/
578 #define PetscAddrAlign(a) (void*)((((PETSC_UINTPTR_T)(a))+(PETSC_MEMALIGN-1)) & ~(PETSC_MEMALIGN-1))
579 
580 /*MC
581    PetscMalloc2 - Allocates 2 chunks of  memory both aligned to PETSC_MEMALIGN
582 
583    Synopsis:
584    PetscErrorCode PetscMalloc2(size_t m1,type, t1,void **r1,size_t m2,type t2,void **r2)
585 
586    Not Collective
587 
588    Input Parameter:
589 +  m1 - number of elements to allocate in 1st chunk  (may be zero)
590 .  t1 - type of first memory elements
591 .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
592 -  t2 - type of second memory elements
593 
594    Output Parameter:
595 +  r1 - memory allocated in first chunk
596 -  r2 - memory allocated in second chunk
597 
598    Level: developer
599 
600 .seealso: PetscFree(), PetscNew(), PetscMalloc()
601 
602   Concepts: memory allocation
603 
604 M*/
605 #if defined(PETSC_USE_DEBUG)
606 #define PetscMalloc2(m1,t1,r1,m2,t2,r2) (PetscMalloc((m1)*sizeof(t1),r1) || PetscMalloc((m2)*sizeof(t2),r2))
607 #else
608 #define PetscMalloc2(m1,t1,r1,m2,t2,r2) ((*(r2) = 0,PetscMalloc((m1)*sizeof(t1)+(m2)*sizeof(t2)+(PETSC_MEMALIGN-1),r1)) \
609                                          || (*(r2) = (t2*)PetscAddrAlign(*(r1)+m1),0))
610 #endif
611 
612 /*MC
613    PetscMalloc3 - Allocates 3 chunks of  memory  all aligned to PETSC_MEMALIGN
614 
615    Synopsis:
616    PetscErrorCode PetscMalloc3(size_t m1,type, t1,void **r1,size_t m2,type t2,void **r2,size_t m3,type t3,void **r3)
617 
618    Not Collective
619 
620    Input Parameter:
621 +  m1 - number of elements to allocate in 1st chunk  (may be zero)
622 .  t1 - type of first memory elements
623 .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
624 .  t2 - type of second memory elements
625 .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
626 -  t3 - type of third memory elements
627 
628    Output Parameter:
629 +  r1 - memory allocated in first chunk
630 .  r2 - memory allocated in second chunk
631 -  r3 - memory allocated in third chunk
632 
633    Level: developer
634 
635 .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree3()
636 
637   Concepts: memory allocation
638 
639 M*/
640 #if defined(PETSC_USE_DEBUG)
641 #define PetscMalloc3(m1,t1,r1,m2,t2,r2,m3,t3,r3) (PetscMalloc((m1)*sizeof(t1),r1) || PetscMalloc((m2)*sizeof(t2),r2) || PetscMalloc((m3)*sizeof(t3),r3))
642 #else
643 #define PetscMalloc3(m1,t1,r1,m2,t2,r2,m3,t3,r3) ((*(r2) = 0,*(r3) = 0,PetscMalloc((m1)*sizeof(t1)+(m2)*sizeof(t2)+(m3)*sizeof(t3)+2*(PETSC_MEMALIGN-1),r1)) \
644                                                   || (*(r2) = (t2*)PetscAddrAlign(*(r1)+m1),*(r3) = (t3*)PetscAddrAlign(*(r2)+m2),0))
645 #endif
646 
647 /*MC
648    PetscMalloc4 - Allocates 4 chunks of  memory  all aligned to PETSC_MEMALIGN
649 
650    Synopsis:
651    PetscErrorCode PetscMalloc4(size_t m1,type, t1,void **r1,size_t m2,type t2,void **r2,size_t m3,type t3,void **r3,size_t m4,type t4,void **r4)
652 
653    Not Collective
654 
655    Input Parameter:
656 +  m1 - number of elements to allocate in 1st chunk  (may be zero)
657 .  t1 - type of first memory elements
658 .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
659 .  t2 - type of second memory elements
660 .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
661 .  t3 - type of third memory elements
662 .  m4 - number of elements to allocate in 4th chunk  (may be zero)
663 -  t4 - type of fourth memory elements
664 
665    Output Parameter:
666 +  r1 - memory allocated in first chunk
667 .  r2 - memory allocated in second chunk
668 .  r3 - memory allocated in third chunk
669 -  r4 - memory allocated in fourth chunk
670 
671    Level: developer
672 
673 .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree3(), PetscFree4()
674 
675   Concepts: memory allocation
676 
677 M*/
678 #if defined(PETSC_USE_DEBUG)
679 #define PetscMalloc4(m1,t1,r1,m2,t2,r2,m3,t3,r3,m4,t4,r4) (PetscMalloc((m1)*sizeof(t1),r1) || PetscMalloc((m2)*sizeof(t2),r2) || PetscMalloc((m3)*sizeof(t3),r3) || PetscMalloc((m4)*sizeof(t4),r4))
680 #else
681 #define PetscMalloc4(m1,t1,r1,m2,t2,r2,m3,t3,r3,m4,t4,r4)               \
682   ((*(r2) = 0, *(r3) = 0, *(r4) = 0,PetscMalloc((m1)*sizeof(t1)+(m2)*sizeof(t2)+(m3)*sizeof(t3)+(m4)*sizeof(t4)+3*(PETSC_MEMALIGN-1),r1)) \
683    || (*(r2) = (t2*)PetscAddrAlign(*(r1)+m1),*(r3) = (t3*)PetscAddrAlign(*(r2)+m2),*(r4) = (t4*)PetscAddrAlign(*(r3)+m3),0))
684 #endif
685 
686 /*MC
687    PetscMalloc5 - Allocates 5 chunks of  memory all aligned to PETSC_MEMALIGN
688 
689    Synopsis:
690    PetscErrorCode PetscMalloc5(size_t m1,type, t1,void **r1,size_t m2,type t2,void **r2,size_t m3,type t3,void **r3,size_t m4,type t4,void **r4,size_t m5,type t5,void **r5)
691 
692    Not Collective
693 
694    Input Parameter:
695 +  m1 - number of elements to allocate in 1st chunk  (may be zero)
696 .  t1 - type of first memory elements
697 .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
698 .  t2 - type of second memory elements
699 .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
700 .  t3 - type of third memory elements
701 .  m4 - number of elements to allocate in 4th chunk  (may be zero)
702 .  t4 - type of fourth memory elements
703 .  m5 - number of elements to allocate in 5th chunk  (may be zero)
704 -  t5 - type of fifth memory elements
705 
706    Output Parameter:
707 +  r1 - memory allocated in first chunk
708 .  r2 - memory allocated in second chunk
709 .  r3 - memory allocated in third chunk
710 .  r4 - memory allocated in fourth chunk
711 -  r5 - memory allocated in fifth chunk
712 
713    Level: developer
714 
715 .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree3(), PetscFree4(), PetscFree5()
716 
717   Concepts: memory allocation
718 
719 M*/
720 #if defined(PETSC_USE_DEBUG)
721 #define PetscMalloc5(m1,t1,r1,m2,t2,r2,m3,t3,r3,m4,t4,r4,m5,t5,r5) (PetscMalloc((m1)*sizeof(t1),r1) || PetscMalloc((m2)*sizeof(t2),r2) || PetscMalloc((m3)*sizeof(t3),r3) || PetscMalloc((m4)*sizeof(t4),r4) || PetscMalloc((m5)*sizeof(t5),r5))
722 #else
723 #define PetscMalloc5(m1,t1,r1,m2,t2,r2,m3,t3,r3,m4,t4,r4,m5,t5,r5)      \
724   ((*(r2) = 0, *(r3) = 0, *(r4) = 0,*(r5) = 0,PetscMalloc((m1)*sizeof(t1)+(m2)*sizeof(t2)+(m3)*sizeof(t3)+(m4)*sizeof(t4)+(m5)*sizeof(t5)+4*(PETSC_MEMALIGN-1),r1)) \
725    || (*(r2) = (t2*)PetscAddrAlign(*(r1)+m1),*(r3) = (t3*)PetscAddrAlign(*(r2)+m2),*(r4) = (t4*)PetscAddrAlign(*(r3)+m3),*(r5) = (t5*)PetscAddrAlign(*(r4)+m4),0))
726 #endif
727 
728 
729 /*MC
730    PetscMalloc6 - Allocates 6 chunks of  memory all aligned to PETSC_MEMALIGN
731 
732    Synopsis:
733    PetscErrorCode PetscMalloc6(size_t m1,type, t1,void **r1,size_t m2,type t2,void **r2,size_t m3,type t3,void **r3,size_t m4,type t4,void **r4,size_t m5,type t5,void **r5,size_t m6,type t6,void **r6)
734 
735    Not Collective
736 
737    Input Parameter:
738 +  m1 - number of elements to allocate in 1st chunk  (may be zero)
739 .  t1 - type of first memory elements
740 .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
741 .  t2 - type of second memory elements
742 .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
743 .  t3 - type of third memory elements
744 .  m4 - number of elements to allocate in 4th chunk  (may be zero)
745 .  t4 - type of fourth memory elements
746 .  m5 - number of elements to allocate in 5th chunk  (may be zero)
747 .  t5 - type of fifth memory elements
748 .  m6 - number of elements to allocate in 6th chunk  (may be zero)
749 -  t6 - type of sixth memory elements
750 
751    Output Parameter:
752 +  r1 - memory allocated in first chunk
753 .  r2 - memory allocated in second chunk
754 .  r3 - memory allocated in third chunk
755 .  r4 - memory allocated in fourth chunk
756 .  r5 - memory allocated in fifth chunk
757 -  r6 - memory allocated in sixth chunk
758 
759    Level: developer
760 
761 .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree3(), PetscFree4(), PetscFree5(), PetscFree6()
762 
763   Concepts: memory allocation
764 
765 M*/
766 #if defined(PETSC_USE_DEBUG)
767 #define PetscMalloc6(m1,t1,r1,m2,t2,r2,m3,t3,r3,m4,t4,r4,m5,t5,r5,m6,t6,r6) (PetscMalloc((m1)*sizeof(t1),r1) || PetscMalloc((m2)*sizeof(t2),r2) || PetscMalloc((m3)*sizeof(t3),r3) || PetscMalloc((m4)*sizeof(t4),r4) || PetscMalloc((m5)*sizeof(t5),r5) || PetscMalloc((m6)*sizeof(t6),r6))
768 #else
769 #define PetscMalloc6(m1,t1,r1,m2,t2,r2,m3,t3,r3,m4,t4,r4,m5,t5,r5,m6,t6,r6) \
770   ((*(r2) = 0, *(r3) = 0, *(r4) = 0,*(r5) = 0,*(r6) = 0,PetscMalloc((m1)*sizeof(t1)+(m2)*sizeof(t2)+(m3)*sizeof(t3)+(m4)*sizeof(t4)+(m5)*sizeof(t5)+(m6)*sizeof(t6)+5*(PETSC_MEMALIGN-1),r1)) \
771    || (*(r2) = (t2*)PetscAddrAlign(*(r1)+m1),*(r3) = (t3*)PetscAddrAlign(*(r2)+m2),*(r4) = (t4*)PetscAddrAlign(*(r3)+m3),*(r5) = (t5*)PetscAddrAlign(*(r4)+m4),*(r6) = (t6*)PetscAddrAlign(*(r5)+m5),0))
772 #endif
773 
774 /*MC
775    PetscMalloc7 - Allocates 7 chunks of  memory all aligned to PETSC_MEMALIGN
776 
777    Synopsis:
778    PetscErrorCode PetscMalloc7(size_t m1,type, t1,void **r1,size_t m2,type t2,void **r2,size_t m3,type t3,void **r3,size_t m4,type t4,void **r4,size_t m5,type t5,void **r5,size_t m6,type t6,void **r6,size_t m7,type t7,void **r7)
779 
780    Not Collective
781 
782    Input Parameter:
783 +  m1 - number of elements to allocate in 1st chunk  (may be zero)
784 .  t1 - type of first memory elements
785 .  m2 - number of elements to allocate in 2nd chunk  (may be zero)
786 .  t2 - type of second memory elements
787 .  m3 - number of elements to allocate in 3rd chunk  (may be zero)
788 .  t3 - type of third memory elements
789 .  m4 - number of elements to allocate in 4th chunk  (may be zero)
790 .  t4 - type of fourth memory elements
791 .  m5 - number of elements to allocate in 5th chunk  (may be zero)
792 .  t5 - type of fifth memory elements
793 .  m6 - number of elements to allocate in 6th chunk  (may be zero)
794 .  t6 - type of sixth memory elements
795 .  m7 - number of elements to allocate in 7th chunk  (may be zero)
796 -  t7 - type of sixth memory elements
797 
798    Output Parameter:
799 +  r1 - memory allocated in first chunk
800 .  r2 - memory allocated in second chunk
801 .  r3 - memory allocated in third chunk
802 .  r4 - memory allocated in fourth chunk
803 .  r5 - memory allocated in fifth chunk
804 .  r6 - memory allocated in sixth chunk
805 -  r7 - memory allocated in seventh chunk
806 
807    Level: developer
808 
809 .seealso: PetscFree(), PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree3(), PetscFree4(), PetscFree5(), PetscFree6(), PetscFree7()
810 
811   Concepts: memory allocation
812 
813 M*/
814 #if defined(PETSC_USE_DEBUG)
815 #define PetscMalloc7(m1,t1,r1,m2,t2,r2,m3,t3,r3,m4,t4,r4,m5,t5,r5,m6,t6,r6,m7,t7,r7) (PetscMalloc((m1)*sizeof(t1),r1) || PetscMalloc((m2)*sizeof(t2),r2) || PetscMalloc((m3)*sizeof(t3),r3) || PetscMalloc((m4)*sizeof(t4),r4) || PetscMalloc((m5)*sizeof(t5),r5) || PetscMalloc((m6)*sizeof(t6),r6) || PetscMalloc((m7)*sizeof(t7),r7))
816 #else
817 #define PetscMalloc7(m1,t1,r1,m2,t2,r2,m3,t3,r3,m4,t4,r4,m5,t5,r5,m6,t6,r6,m7,t7,r7) \
818   ((*(r2) = 0, *(r3) = 0, *(r4) = 0,*(r5) = 0,*(r6) = 0,*(r7) = 0,PetscMalloc((m1)*sizeof(t1)+(m2)*sizeof(t2)+(m3)*sizeof(t3)+(m4)*sizeof(t4)+(m5)*sizeof(t5)+(m6)*sizeof(t6)+(m7)*sizeof(t7)+6*(PETSC_MEMALIGN-1),r1)) \
819    || (*(r2) = (t2*)PetscAddrAlign(*(r1)+m1),*(r3) = (t3*)PetscAddrAlign(*(r2)+m2),*(r4) = (t4*)PetscAddrAlign(*(r3)+m3),*(r5) = (t5*)PetscAddrAlign(*(r4)+m4),*(r6) = (t6*)PetscAddrAlign(*(r5)+m5),*(r7) = (t7*)PetscAddrAlign(*(r6)+m6),0))
820 #endif
821 
822 /*MC
823    PetscNew - Allocates memory of a particular type, zeros the memory! Aligned to PETSC_MEMALIGN
824 
825    Synopsis:
826    PetscErrorCode PetscNew(struct type,((type *))result)
827 
828    Not Collective
829 
830    Input Parameter:
831 .  type - structure name of space to be allocated. Memory of size sizeof(type) is allocated
832 
833    Output Parameter:
834 .  result - memory allocated
835 
836    Level: beginner
837 
838 .seealso: PetscFree(), PetscMalloc(), PetscNewLog()
839 
840   Concepts: memory allocation
841 
842 M*/
843 #define PetscNew(A,b)      (PetscMalloc(sizeof(A),(b)) || PetscMemzero(*(b),sizeof(A)))
844 
845 /*MC
846    PetscNewLog - Allocates memory of a particular type, zeros the memory! Aligned to PETSC_MEMALIGN. Associates the memory allocated
847          with the given object using PetscLogObjectMemory().
848 
849    Synopsis:
850    PetscErrorCode PetscNewLog(PetscObject obj,struct type,((type *))result)
851 
852    Not Collective
853 
854    Input Parameter:
855 +  obj - object memory is logged to
856 -  type - structure name of space to be allocated. Memory of size sizeof(type) is allocated
857 
858    Output Parameter:
859 .  result - memory allocated
860 
861    Level: developer
862 
863 .seealso: PetscFree(), PetscMalloc(), PetscNew(), PetscLogObjectMemory()
864 
865   Concepts: memory allocation
866 
867 M*/
868 #define PetscNewLog(o,A,b) (PetscNew(A,b) || ((o) ? PetscLogObjectMemory(o,sizeof(A)) : 0))
869 
870 /*MC
871    PetscFree - Frees memory
872 
873    Synopsis:
874    PetscErrorCode PetscFree(void *memory)
875 
876    Not Collective
877 
878    Input Parameter:
879 .   memory - memory to free (the pointer is ALWAYS set to 0 upon sucess)
880 
881    Level: beginner
882 
883    Notes: Memory must have been obtained with PetscNew() or PetscMalloc()
884 
885 .seealso: PetscNew(), PetscMalloc(), PetscFreeVoid()
886 
887   Concepts: memory allocation
888 
889 M*/
890 #define PetscFree(a)   ((a) ? ((*PetscTrFree)((void*)(a),__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__) || (((a) = 0),0)) : 0)
891 
892 /*MC
893    PetscFreeVoid - Frees memory
894 
895    Synopsis:
896    void PetscFreeVoid(void *memory)
897 
898    Not Collective
899 
900    Input Parameter:
901 .   memory - memory to free
902 
903    Level: beginner
904 
905    Notes: This is different from PetscFree() in that no error code is returned
906 
907 .seealso: PetscFree(), PetscNew(), PetscMalloc()
908 
909   Concepts: memory allocation
910 
911 M*/
912 #define PetscFreeVoid(a) ((*PetscTrFree)((a),__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__),(a) = 0)
913 
914 
915 /*MC
916    PetscFree2 - Frees 2 chunks of memory obtained with PetscMalloc2()
917 
918    Synopsis:
919    PetscErrorCode PetscFree2(void *memory1,void *memory2)
920 
921    Not Collective
922 
923    Input Parameter:
924 +   memory1 - memory to free
925 -   memory2 - 2nd memory to free
926 
927    Level: developer
928 
929    Notes: Memory must have been obtained with PetscMalloc2()
930 
931 .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree()
932 
933   Concepts: memory allocation
934 
935 M*/
936 #if defined(PETSC_USE_DEBUG)
937 #define PetscFree2(m1,m2)   (PetscFree(m2) || PetscFree(m1))
938 #else
939 #define PetscFree2(m1,m2)   ((m2)=0, PetscFree(m1))
940 #endif
941 
942 /*MC
943    PetscFree3 - Frees 3 chunks of memory obtained with PetscMalloc3()
944 
945    Synopsis:
946    PetscErrorCode PetscFree3(void *memory1,void *memory2,void *memory3)
947 
948    Not Collective
949 
950    Input Parameter:
951 +   memory1 - memory to free
952 .   memory2 - 2nd memory to free
953 -   memory3 - 3rd memory to free
954 
955    Level: developer
956 
957    Notes: Memory must have been obtained with PetscMalloc3()
958 
959 .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree(), PetscMalloc3()
960 
961   Concepts: memory allocation
962 
963 M*/
964 #if defined(PETSC_USE_DEBUG)
965 #define PetscFree3(m1,m2,m3)   (PetscFree(m3) || PetscFree(m2) || PetscFree(m1))
966 #else
967 #define PetscFree3(m1,m2,m3)   ((m3)=0,(m2)=0,PetscFree(m1))
968 #endif
969 
970 /*MC
971    PetscFree4 - Frees 4 chunks of memory obtained with PetscMalloc4()
972 
973    Synopsis:
974    PetscErrorCode PetscFree4(void *m1,void *m2,void *m3,void *m4)
975 
976    Not Collective
977 
978    Input Parameter:
979 +   m1 - memory to free
980 .   m2 - 2nd memory to free
981 .   m3 - 3rd memory to free
982 -   m4 - 4th memory to free
983 
984    Level: developer
985 
986    Notes: Memory must have been obtained with PetscMalloc4()
987 
988 .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree(), PetscMalloc3(), PetscMalloc4()
989 
990   Concepts: memory allocation
991 
992 M*/
993 #if defined(PETSC_USE_DEBUG)
994 #define PetscFree4(m1,m2,m3,m4)   (PetscFree(m4) || PetscFree(m3) || PetscFree(m2) || PetscFree(m1))
995 #else
996 #define PetscFree4(m1,m2,m3,m4)   ((m4)=0,(m3)=0,(m2)=0,PetscFree(m1))
997 #endif
998 
999 /*MC
1000    PetscFree5 - Frees 5 chunks of memory obtained with PetscMalloc5()
1001 
1002    Synopsis:
1003    PetscErrorCode PetscFree5(void *m1,void *m2,void *m3,void *m4,void *m5)
1004 
1005    Not Collective
1006 
1007    Input Parameter:
1008 +   m1 - memory to free
1009 .   m2 - 2nd memory to free
1010 .   m3 - 3rd memory to free
1011 .   m4 - 4th memory to free
1012 -   m5 - 5th memory to free
1013 
1014    Level: developer
1015 
1016    Notes: Memory must have been obtained with PetscMalloc5()
1017 
1018 .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree(), PetscMalloc3(), PetscMalloc4(), PetscMalloc5()
1019 
1020   Concepts: memory allocation
1021 
1022 M*/
1023 #if defined(PETSC_USE_DEBUG)
1024 #define PetscFree5(m1,m2,m3,m4,m5)   (PetscFree(m5) || PetscFree(m4) || PetscFree(m3) || PetscFree(m2) || PetscFree(m1))
1025 #else
1026 #define PetscFree5(m1,m2,m3,m4,m5)   ((m5)=0,(m4)=0,(m3)=0,(m2)=0,PetscFree(m1))
1027 #endif
1028 
1029 
1030 /*MC
1031    PetscFree6 - Frees 6 chunks of memory obtained with PetscMalloc6()
1032 
1033    Synopsis:
1034    PetscErrorCode PetscFree6(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6)
1035 
1036    Not Collective
1037 
1038    Input Parameter:
1039 +   m1 - memory to free
1040 .   m2 - 2nd memory to free
1041 .   m3 - 3rd memory to free
1042 .   m4 - 4th memory to free
1043 .   m5 - 5th memory to free
1044 -   m6 - 6th memory to free
1045 
1046 
1047    Level: developer
1048 
1049    Notes: Memory must have been obtained with PetscMalloc6()
1050 
1051 .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree(), PetscMalloc3(), PetscMalloc4(), PetscMalloc5(), PetscMalloc6()
1052 
1053   Concepts: memory allocation
1054 
1055 M*/
1056 #if defined(PETSC_USE_DEBUG)
1057 #define PetscFree6(m1,m2,m3,m4,m5,m6)   (PetscFree(m6) || PetscFree(m5) || PetscFree(m4) || PetscFree(m3) || PetscFree(m2) || PetscFree(m1))
1058 #else
1059 #define PetscFree6(m1,m2,m3,m4,m5,m6)   ((m6)=0,(m5)=0,(m4)=0,(m3)=0,(m2)=0,PetscFree(m1))
1060 #endif
1061 
1062 /*MC
1063    PetscFree7 - Frees 7 chunks of memory obtained with PetscMalloc7()
1064 
1065    Synopsis:
1066    PetscErrorCode PetscFree7(void *m1,void *m2,void *m3,void *m4,void *m5,void *m6,void *m7)
1067 
1068    Not Collective
1069 
1070    Input Parameter:
1071 +   m1 - memory to free
1072 .   m2 - 2nd memory to free
1073 .   m3 - 3rd memory to free
1074 .   m4 - 4th memory to free
1075 .   m5 - 5th memory to free
1076 .   m6 - 6th memory to free
1077 -   m7 - 7th memory to free
1078 
1079 
1080    Level: developer
1081 
1082    Notes: Memory must have been obtained with PetscMalloc7()
1083 
1084 .seealso: PetscNew(), PetscMalloc(), PetscMalloc2(), PetscFree(), PetscMalloc3(), PetscMalloc4(), PetscMalloc5(), PetscMalloc6(),
1085           PetscMalloc7()
1086 
1087   Concepts: memory allocation
1088 
1089 M*/
1090 #if defined(PETSC_USE_DEBUG)
1091 #define PetscFree7(m1,m2,m3,m4,m5,m6,m7)   (PetscFree(m7) || PetscFree(m6) || PetscFree(m5) || PetscFree(m4) || PetscFree(m3) || PetscFree(m2) || PetscFree(m1))
1092 #else
1093 #define PetscFree7(m1,m2,m3,m4,m5,m6,m7)   ((m7)=0,(m6)=0,(m5)=0,(m4)=0,(m3)=0,(m2)=0,PetscFree(m1))
1094 #endif
1095 
1096 extern  PetscErrorCode (*PetscTrMalloc)(size_t,int,const char[],const char[],const char[],void**);
1097 extern  PetscErrorCode (*PetscTrFree)(void*,int,const char[],const char[],const char[]);
1098 extern PetscErrorCode   PetscMallocSet(PetscErrorCode (*)(size_t,int,const char[],const char[],const char[],void**),PetscErrorCode (*)(void*,int,const char[],const char[],const char[]));
1099 extern PetscErrorCode   PetscMallocClear(void);
1100 
1101 /*
1102    Routines for tracing memory corruption/bleeding with default PETSc  memory allocation
1103 */
1104 extern PetscErrorCode    PetscMallocDump(FILE *);
1105 extern PetscErrorCode    PetscMallocDumpLog(FILE *);
1106 extern PetscErrorCode    PetscMallocGetCurrentUsage(PetscLogDouble *);
1107 extern PetscErrorCode    PetscMallocGetMaximumUsage(PetscLogDouble *);
1108 extern PetscErrorCode    PetscMallocDebug(PetscBool);
1109 extern PetscErrorCode    PetscMallocValidate(int,const char[],const char[],const char[]);
1110 extern PetscErrorCode    PetscMallocSetDumpLog(void);
1111 
1112 
1113 /*E
1114     PetscDataType - Used for handling different basic data types.
1115 
1116    Level: beginner
1117 
1118    Developer comment: It would be nice if we could always just use MPI Datatypes, why can we not?
1119 
1120 .seealso: PetscBinaryRead(), PetscBinaryWrite(), PetscDataTypeToMPIDataType(),
1121           PetscDataTypeGetSize()
1122 
1123 E*/
1124 typedef enum {PETSC_INT = 0,PETSC_DOUBLE = 1,PETSC_COMPLEX = 2, PETSC_LONG = 3 ,PETSC_SHORT = 4,PETSC_FLOAT = 5,
1125               PETSC_CHAR = 6,PETSC_BIT_LOGICAL = 7,PETSC_ENUM = 8,PETSC_BOOL=9, PETSC_LONG_DOUBLE = 10, PETSC_QD_DD = 11} PetscDataType;
1126 extern const char *PetscDataTypes[];
1127 
1128 #if defined(PETSC_USE_COMPLEX)
1129 #define PETSC_SCALAR PETSC_COMPLEX
1130 #else
1131 #if defined(PETSC_USE_SCALAR_SINGLE)
1132 #define PETSC_SCALAR PETSC_FLOAT
1133 #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
1134 #define PETSC_SCALAR PETSC_LONG_DOUBLE
1135 #elif defined(PETSC_USE_SCALAR_INT)
1136 #define PETSC_SCALAR PETSC_INT
1137 #elif defined(PETSC_USE_SCALAR_QD_DD)
1138 #define PETSC_SCALAR PETSC_QD_DD
1139 #else
1140 #define PETSC_SCALAR PETSC_DOUBLE
1141 #endif
1142 #endif
1143 #if defined(PETSC_USE_SCALAR_SINGLE)
1144 #define PETSC_REAL PETSC_FLOAT
1145 #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
1146 #define PETSC_REAL PETSC_LONG_DOUBLE
1147 #elif defined(PETSC_USE_SCALAR_INT)
1148 #define PETSC_REAL PETSC_INT
1149 #elif defined(PETSC_USE_SCALAR_QD_DD)
1150 #define PETSC_REAL PETSC_QD_DD
1151 #else
1152 #define PETSC_REAL PETSC_DOUBLE
1153 #endif
1154 #define PETSC_FORTRANADDR PETSC_LONG
1155 
1156 extern PetscErrorCode  PetscDataTypeToMPIDataType(PetscDataType,MPI_Datatype*);
1157 extern PetscErrorCode  PetscMPIDataTypeToPetscDataType(MPI_Datatype,PetscDataType*);
1158 extern PetscErrorCode  PetscDataTypeGetSize(PetscDataType,size_t*);
1159 
1160 /*
1161     Basic memory and string operations. These are usually simple wrappers
1162    around the basic Unix system calls, but a few of them have additional
1163    functionality and/or error checking.
1164 */
1165 extern PetscErrorCode    PetscBitMemcpy(void*,PetscInt,const void*,PetscInt,PetscInt,PetscDataType);
1166 extern PetscErrorCode    PetscMemmove(void*,void *,size_t);
1167 extern PetscErrorCode    PetscMemcmp(const void*,const void*,size_t,PetscBool  *);
1168 extern PetscErrorCode    PetscStrlen(const char[],size_t*);
1169 extern PetscErrorCode    PetscStrToArray(const char[],int*,char ***);
1170 extern PetscErrorCode    PetscStrToArrayDestroy(int,char **);
1171 extern PetscErrorCode    PetscStrcmp(const char[],const char[],PetscBool  *);
1172 extern PetscErrorCode    PetscStrgrt(const char[],const char[],PetscBool  *);
1173 extern PetscErrorCode    PetscStrcasecmp(const char[],const char[],PetscBool *);
1174 extern PetscErrorCode    PetscStrncmp(const char[],const char[],size_t,PetscBool *);
1175 extern PetscErrorCode    PetscStrcpy(char[],const char[]);
1176 extern PetscErrorCode    PetscStrcat(char[],const char[]);
1177 extern PetscErrorCode    PetscStrncat(char[],const char[],size_t);
1178 extern PetscErrorCode    PetscStrncpy(char[],const char[],size_t);
1179 extern PetscErrorCode    PetscStrchr(const char[],char,char *[]);
1180 extern PetscErrorCode    PetscStrtolower(char[]);
1181 extern PetscErrorCode    PetscStrrchr(const char[],char,char *[]);
1182 extern PetscErrorCode    PetscStrstr(const char[],const char[],char *[]);
1183 extern PetscErrorCode    PetscStrrstr(const char[],const char[],char *[]);
1184 extern PetscErrorCode    PetscStrallocpy(const char[],char *[]);
1185 extern PetscErrorCode    PetscStrreplace(MPI_Comm,const char[],char[],size_t);
1186 
1187 /*S
1188     PetscToken - 'Token' used for managing tokenizing strings
1189 
1190   Level: intermediate
1191 
1192 .seealso: PetscTokenCreate(), PetscTokenFind(), PetscTokenDestroy()
1193 S*/
1194 typedef struct _p_PetscToken* PetscToken;
1195 
1196 extern PetscErrorCode    PetscTokenCreate(const char[],const char,PetscToken*);
1197 extern PetscErrorCode    PetscTokenFind(PetscToken,char *[]);
1198 extern PetscErrorCode    PetscTokenDestroy(PetscToken);
1199 
1200 /*
1201    These are  MPI operations for MPI_Allreduce() etc
1202 */
1203 extern  MPI_Op PetscMaxSum_Op;
1204 #if defined(PETSC_USE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
1205 extern  MPI_Op MPIU_SUM;
1206 #else
1207 #define MPIU_SUM MPI_SUM
1208 #endif
1209 extern PetscErrorCode  PetscMaxSum(MPI_Comm,const PetscInt[],PetscInt*,PetscInt*);
1210 
1211 /*S
1212      PetscObject - any PETSc object, PetscViewer, Mat, Vec, KSP etc
1213 
1214    Level: beginner
1215 
1216    Note: This is the base class from which all objects appear.
1217 
1218 .seealso:  PetscObjectDestroy(), PetscObjectView(), PetscObjectGetName(), PetscObjectSetName(), PetscObjectReference(), PetscObjectDereferenc()
1219 S*/
1220 typedef struct _p_PetscObject* PetscObject;
1221 
1222 /*S
1223      PetscFList - Linked list of functions, possibly stored in dynamic libraries, accessed
1224       by string name
1225 
1226    Level: advanced
1227 
1228 .seealso:  PetscFListAdd(), PetscFListDestroy()
1229 S*/
1230 typedef struct _n_PetscFList *PetscFList;
1231 
1232 /*E
1233   PetscFileMode - Access mode for a file.
1234 
1235   Level: beginner
1236 
1237   FILE_MODE_READ - open a file at its beginning for reading
1238 
1239   FILE_MODE_WRITE - open a file at its beginning for writing (will create if the file does not exist)
1240 
1241   FILE_MODE_APPEND - open a file at end for writing
1242 
1243   FILE_MODE_UPDATE - open a file for updating, meaning for reading and writing
1244 
1245   FILE_MODE_APPEND_UPDATE - open a file for updating, meaning for reading and writing, at the end
1246 
1247 .seealso: PetscViewerFileSetMode()
1248 E*/
1249 typedef enum {FILE_MODE_READ, FILE_MODE_WRITE, FILE_MODE_APPEND, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE} PetscFileMode;
1250 
1251 #include "petscviewer.h"
1252 #include "petscoptions.h"
1253 
1254 #define PETSC_SMALLEST_CLASSID 1211211
1255 extern  PetscClassId PETSC_LARGEST_CLASSID;
1256 extern  PetscClassId PETSC_OBJECT_CLASSID;
1257 extern PetscErrorCode  PetscClassIdRegister(const char[],PetscClassId *);
1258 
1259 /*
1260    Routines that get memory usage information from the OS
1261 */
1262 extern PetscErrorCode  PetscMemoryGetCurrentUsage(PetscLogDouble *);
1263 extern PetscErrorCode  PetscMemoryGetMaximumUsage(PetscLogDouble *);
1264 extern PetscErrorCode  PetscMemorySetGetMaximumUsage(void);
1265 extern PetscErrorCode  PetscMemoryShowUsage(PetscViewer,const char[]);
1266 
1267 extern PetscErrorCode  PetscInfoAllow(PetscBool ,const char []);
1268 extern PetscErrorCode  PetscGetTime(PetscLogDouble*);
1269 extern PetscErrorCode  PetscGetCPUTime(PetscLogDouble*);
1270 extern PetscErrorCode  PetscSleep(PetscReal);
1271 
1272 /*
1273    Initialization of PETSc
1274 */
1275 extern PetscErrorCode  PetscInitialize(int*,char***,const char[],const char[]);
1276 PetscPolymorphicSubroutine(PetscInitialize,(int *argc,char ***args),(argc,args,PETSC_NULL,PETSC_NULL))
1277 extern PetscErrorCode  PetscInitializeNoArguments(void);
1278 extern PetscErrorCode  PetscInitialized(PetscBool  *);
1279 extern PetscErrorCode  PetscFinalized(PetscBool  *);
1280 extern PetscErrorCode  PetscFinalize(void);
1281 extern PetscErrorCode PetscInitializeFortran(void);
1282 extern PetscErrorCode  PetscGetArgs(int*,char ***);
1283 extern PetscErrorCode  PetscGetArguments(char ***);
1284 extern PetscErrorCode  PetscFreeArguments(char **);
1285 
1286 extern PetscErrorCode  PetscEnd(void);
1287 extern PetscErrorCode  PetscSysInitializePackage(const char[]);
1288 
1289 extern MPI_Comm PETSC_COMM_LOCAL_WORLD;
1290 extern PetscErrorCode  PetscOpenMPMerge(PetscMPIInt,PetscErrorCode (*)(void*),void*);
1291 extern PetscErrorCode  PetscOpenMPSpawn(PetscMPIInt);
1292 extern PetscErrorCode  PetscOpenMPFinalize(void);
1293 extern PetscErrorCode  PetscOpenMPRun(MPI_Comm,PetscErrorCode (*)(MPI_Comm,void *),void*);
1294 extern PetscErrorCode  PetscOpenMPRunCtx(MPI_Comm,PetscErrorCode (*)(MPI_Comm,void*,void *),void*);
1295 extern PetscErrorCode  PetscOpenMPFree(MPI_Comm,void*);
1296 extern PetscErrorCode  PetscOpenMPMalloc(MPI_Comm,size_t,void**);
1297 
1298 extern PetscErrorCode  PetscPythonInitialize(const char[],const char[]);
1299 extern PetscErrorCode  PetscPythonFinalize(void);
1300 extern PetscErrorCode  PetscPythonPrintError(void);
1301 
1302 /*
1303      These are so that in extern C code we can caste function pointers to non-extern C
1304    function pointers. Since the regular C++ code expects its function pointers to be
1305    C++.
1306 */
1307 typedef void (**PetscVoidStarFunction)(void);
1308 typedef void (*PetscVoidFunction)(void);
1309 typedef PetscErrorCode (*PetscErrorCodeFunction)(void);
1310 
1311 /*
1312    PetscTryMethod - Queries an object for a method, if it exists then calls it.
1313               These are intended to be used only inside PETSc functions.
1314 
1315    Level: developer
1316 
1317 .seealso: PetscUseMethod()
1318 */
1319 #define  PetscTryMethod(obj,A,B,C) \
1320   0;{ PetscErrorCode (*f)B, __ierr; \
1321     __ierr = PetscObjectQueryFunction((PetscObject)obj,A,(PetscVoidStarFunction)&f);CHKERRQ(__ierr); \
1322     if (f) {__ierr = (*f)C;CHKERRQ(__ierr);}\
1323   }
1324 
1325 /*
1326    PetscUseMethod - Queries an object for a method, if it exists then calls it, otherwise generates an error.
1327               These are intended to be used only inside PETSc functions.
1328 
1329    Level: developer
1330 
1331 .seealso: PetscTryMethod()
1332 */
1333 #define  PetscUseMethod(obj,A,B,C) \
1334   0;{ PetscErrorCode (*f)B, __ierr; \
1335     __ierr = PetscObjectQueryFunction((PetscObject)obj,A,(PetscVoidStarFunction)&f);CHKERRQ(__ierr); \
1336     if (f) {__ierr = (*f)C;CHKERRQ(__ierr);}\
1337     else SETERRQ1(((PetscObject)obj)->comm,PETSC_ERR_SUP,"Cannot locate function %s in object",A); \
1338   }
1339 
1340 /*
1341     Functions that can act on any PETSc object.
1342 */
1343 extern PetscErrorCode  PetscObjectCreate(MPI_Comm,PetscObject*);
1344 extern PetscErrorCode  PetscObjectCreateGeneric(MPI_Comm, PetscClassId, const char [], PetscObject *);
1345 extern PetscErrorCode  PetscObjectDestroy(PetscObject);
1346 extern PetscErrorCode  PetscObjectGetComm(PetscObject,MPI_Comm *);
1347 extern PetscErrorCode  PetscObjectGetClassId(PetscObject,PetscClassId *);
1348 extern PetscErrorCode  PetscObjectSetType(PetscObject,const char []);
1349 extern PetscErrorCode  PetscObjectSetPrecision(PetscObject,PetscPrecision);
1350 extern PetscErrorCode  PetscObjectGetType(PetscObject,const char *[]);
1351 extern PetscErrorCode  PetscObjectSetName(PetscObject,const char[]);
1352 extern PetscErrorCode  PetscObjectGetName(PetscObject,const char*[]);
1353 extern PetscErrorCode  PetscObjectPrintClassNamePrefixType(PetscObject,PetscViewer,const char[]);
1354 extern PetscErrorCode  PetscObjectSetTabLevel(PetscObject,PetscInt);
1355 extern PetscErrorCode  PetscObjectGetTabLevel(PetscObject,PetscInt*);
1356 extern PetscErrorCode  PetscObjectIncrementTabLevel(PetscObject,PetscObject,PetscInt);
1357 extern PetscErrorCode  PetscObjectReference(PetscObject);
1358 extern PetscErrorCode  PetscObjectGetReference(PetscObject,PetscInt*);
1359 extern PetscErrorCode  PetscObjectDereference(PetscObject);
1360 extern PetscErrorCode  PetscObjectGetNewTag(PetscObject,PetscMPIInt *);
1361 extern PetscErrorCode  PetscObjectView(PetscObject,PetscViewer);
1362 extern PetscErrorCode  PetscObjectCompose(PetscObject,const char[],PetscObject);
1363 extern PetscErrorCode  PetscObjectQuery(PetscObject,const char[],PetscObject *);
1364 extern PetscErrorCode  PetscObjectComposeFunction(PetscObject,const char[],const char[],void (*)(void));
1365 extern PetscErrorCode  PetscObjectSetFromOptions(PetscObject);
1366 extern PetscErrorCode  PetscObjectSetUp(PetscObject);
1367 extern PetscErrorCode  PetscCommGetNewTag(MPI_Comm,PetscMPIInt *);
1368 extern PetscErrorCode  PetscObjectAddOptionsHandler(PetscObject,PetscErrorCode (*)(PetscObject,void*),PetscErrorCode (*)(PetscObject,void*),void*);
1369 extern PetscErrorCode  PetscObjectProcessOptionsHandlers(PetscObject);
1370 extern PetscErrorCode  PetscObjectDestroyOptionsHandlers(PetscObject);
1371 
1372 /*MC
1373    PetscObjectComposeFunctionDynamic - Associates a function with a given PETSc object.
1374 
1375     Synopsis:
1376     PetscErrorCode PetscObjectComposeFunctionDynamic(PetscObject obj,const char name[],const char fname[],void *ptr)
1377 
1378    Logically Collective on PetscObject
1379 
1380    Input Parameters:
1381 +  obj - the PETSc object; this must be cast with a (PetscObject), for example,
1382          PetscObjectCompose((PetscObject)mat,...);
1383 .  name - name associated with the child function
1384 .  fname - name of the function
1385 -  ptr - function pointer (or PETSC_NULL if using dynamic libraries)
1386 
1387    Level: advanced
1388 
1389 
1390    Notes:
1391    To remove a registered routine, pass in a PETSC_NULL rname and fnc().
1392 
1393    PetscObjectComposeFunctionDynamic() can be used with any PETSc object (such as
1394    Mat, Vec, KSP, SNES, etc.) or any user-provided object.
1395 
1396    The composed function must be wrapped in a EXTERN_C_BEGIN/END for this to
1397    work in C++/complex with dynamic link libraries (./configure options --with-shared-libraries --with-dynamic-loading)
1398    enabled.
1399 
1400    Concepts: objects^composing functions
1401    Concepts: composing functions
1402    Concepts: functions^querying
1403    Concepts: objects^querying
1404    Concepts: querying objects
1405 
1406 .seealso: PetscObjectQueryFunction()
1407 M*/
1408 #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
1409 #define PetscObjectComposeFunctionDynamic(a,b,c,d) PetscObjectComposeFunction(a,b,c,0)
1410 #else
1411 #define PetscObjectComposeFunctionDynamic(a,b,c,d) PetscObjectComposeFunction(a,b,c,(PetscVoidFunction)(d))
1412 #endif
1413 
1414 extern PetscErrorCode  PetscObjectQueryFunction(PetscObject,const char[],void (**)(void));
1415 extern PetscErrorCode  PetscObjectSetOptionsPrefix(PetscObject,const char[]);
1416 extern PetscErrorCode  PetscObjectAppendOptionsPrefix(PetscObject,const char[]);
1417 extern PetscErrorCode  PetscObjectPrependOptionsPrefix(PetscObject,const char[]);
1418 extern PetscErrorCode  PetscObjectGetOptionsPrefix(PetscObject,const char*[]);
1419 extern PetscErrorCode  PetscObjectAMSPublish(PetscObject);
1420 extern PetscErrorCode  PetscObjectUnPublish(PetscObject);
1421 extern PetscErrorCode  PetscObjectChangeTypeName(PetscObject,const char[]);
1422 extern PetscErrorCode  PetscObjectRegisterDestroy(PetscObject);
1423 extern PetscErrorCode  PetscObjectRegisterDestroyAll(void);
1424 extern PetscErrorCode  PetscObjectName(PetscObject);
1425 extern PetscErrorCode  PetscTypeCompare(PetscObject,const char[],PetscBool *);
1426 extern PetscErrorCode  PetscRegisterFinalize(PetscErrorCode (*)(void));
1427 extern PetscErrorCode  PetscRegisterFinalizeAll(void);
1428 
1429 /*
1430     Defines PETSc error handling.
1431 */
1432 #include "petscerror.h"
1433 
1434 /*S
1435      PetscOList - Linked list of PETSc objects, each accessable by string name
1436 
1437    Level: developer
1438 
1439    Notes: Used by PetscObjectCompose() and PetscObjectQuery()
1440 
1441 .seealso:  PetscOListAdd(), PetscOListDestroy(), PetscOListFind(), PetscObjectCompose(), PetscObjectQuery()
1442 S*/
1443 typedef struct _n_PetscOList *PetscOList;
1444 
1445 extern PetscErrorCode  PetscOListDestroy(PetscOList);
1446 extern PetscErrorCode  PetscOListFind(PetscOList,const char[],PetscObject*);
1447 extern PetscErrorCode  PetscOListReverseFind(PetscOList,PetscObject,char**);
1448 extern PetscErrorCode  PetscOListAdd(PetscOList *,const char[],PetscObject);
1449 extern PetscErrorCode  PetscOListDuplicate(PetscOList,PetscOList *);
1450 
1451 /*
1452     Dynamic library lists. Lists of names of routines in objects or in dynamic
1453   link libraries that will be loaded as needed.
1454 */
1455 extern PetscErrorCode  PetscFListAdd(PetscFList*,const char[],const char[],void (*)(void));
1456 extern PetscErrorCode  PetscFListDestroy(PetscFList*);
1457 extern PetscErrorCode  PetscFListFind(PetscFList,MPI_Comm,const char[],void (**)(void));
1458 extern PetscErrorCode  PetscFListPrintTypes(MPI_Comm,FILE*,const char[],const char[],const char[],const char[],PetscFList,const char[]);
1459 #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
1460 #define    PetscFListAddDynamic(a,b,p,c) PetscFListAdd(a,b,p,0)
1461 #else
1462 #define    PetscFListAddDynamic(a,b,p,c) PetscFListAdd(a,b,p,(void (*)(void))c)
1463 #endif
1464 extern PetscErrorCode  PetscFListDuplicate(PetscFList,PetscFList *);
1465 extern PetscErrorCode  PetscFListView(PetscFList,PetscViewer);
1466 extern PetscErrorCode  PetscFListConcat(const char [],const char [],char []);
1467 extern PetscErrorCode  PetscFListGet(PetscFList,char ***,int*);
1468 
1469 /*S
1470      PetscDLLibrary - Linked list of dynamics libraries to search for functions
1471 
1472    Level: advanced
1473 
1474    --with-shared-libraries --with-dynamic-loading must be used with ./configure to use dynamic libraries
1475 
1476 .seealso:  PetscDLLibraryOpen()
1477 S*/
1478 typedef struct _n_PetscDLLibrary *PetscDLLibrary;
1479 extern  PetscDLLibrary DLLibrariesLoaded;
1480 extern PetscErrorCode  PetscDLLibraryAppend(MPI_Comm,PetscDLLibrary *,const char[]);
1481 extern PetscErrorCode  PetscDLLibraryPrepend(MPI_Comm,PetscDLLibrary *,const char[]);
1482 extern PetscErrorCode  PetscDLLibrarySym(MPI_Comm,PetscDLLibrary *,const char[],const char[],void **);
1483 extern PetscErrorCode  PetscDLLibraryPrintPath(PetscDLLibrary);
1484 extern PetscErrorCode  PetscDLLibraryRetrieve(MPI_Comm,const char[],char *,size_t,PetscBool  *);
1485 extern PetscErrorCode  PetscDLLibraryOpen(MPI_Comm,const char[],PetscDLLibrary *);
1486 extern PetscErrorCode  PetscDLLibraryClose(PetscDLLibrary);
1487 extern PetscErrorCode  PetscDLLibraryCCAAppend(MPI_Comm,PetscDLLibrary *,const char[]);
1488 
1489 /*
1490   PetscFwk support.  Needs to be documented.
1491   Logically it is an extension of PetscDLLXXX, PetscObjectCompose, etc.
1492 */
1493 #include "petscfwk.h"
1494 
1495 /*
1496      Useful utility routines
1497 */
1498 extern PetscErrorCode  PetscSplitOwnership(MPI_Comm,PetscInt*,PetscInt*);
1499 extern PetscErrorCode  PetscSplitOwnershipBlock(MPI_Comm,PetscInt,PetscInt*,PetscInt*);
1500 extern PetscErrorCode  PetscSequentialPhaseBegin(MPI_Comm,PetscMPIInt);
1501 PetscPolymorphicSubroutine(PetscSequentialPhaseBegin,(MPI_Comm comm),(comm,1))
1502 PetscPolymorphicSubroutine(PetscSequentialPhaseBegin,(void),(PETSC_COMM_WORLD,1))
1503 extern PetscErrorCode  PetscSequentialPhaseEnd(MPI_Comm,PetscMPIInt);
1504 PetscPolymorphicSubroutine(PetscSequentialPhaseEnd,(MPI_Comm comm),(comm,1))
1505 PetscPolymorphicSubroutine(PetscSequentialPhaseEnd,(void),(PETSC_COMM_WORLD,1))
1506 extern PetscErrorCode  PetscBarrier(PetscObject);
1507 extern PetscErrorCode  PetscMPIDump(FILE*);
1508 
1509 /*
1510     PetscNot - negates a logical type value and returns result as a PetscBool
1511 
1512     Notes: This is useful in cases like
1513 $     int        *a;
1514 $     PetscBool  flag = PetscNot(a)
1515      where !a does not return a PetscBool  because we cannot provide a cast from int to PetscBool  in C.
1516 */
1517  #define PetscNot(a) ((a) ? PETSC_FALSE : PETSC_TRUE)
1518 
1519 /*
1520     Defines basic graphics available from PETSc.
1521 */
1522 #include "petscdraw.h"
1523 
1524 /*
1525     Defines the base data structures for all PETSc objects
1526 */
1527 #include "private/petscimpl.h"
1528 
1529 /*
1530      Defines PETSc profiling.
1531 */
1532 #include "petsclog.h"
1533 
1534 /*
1535           For locking, unlocking and destroying AMS memories associated with  PETSc objects. ams.h is included in petscviewer.h
1536 */
1537 #if defined(PETSC_HAVE_AMS)
1538 extern PetscBool  PetscAMSPublishAll;
1539 #define PetscObjectTakeAccess(obj)  ((((PetscObject)(obj))->amem == -1) ? 0 : AMS_Memory_take_access(((PetscObject)(obj))->amem))
1540 #define PetscObjectGrantAccess(obj) ((((PetscObject)(obj))->amem == -1) ? 0 : AMS_Memory_grant_access(((PetscObject)(obj))->amem))
1541 #define PetscObjectDepublish(obj)   ((((PetscObject)(obj))->amem == -1) ? 0 : AMS_Memory_destroy(((PetscObject)(obj))->amem));((PetscObject)(obj))->amem = -1;
1542 #else
1543 #define PetscObjectTakeAccess(obj)   0
1544 #define PetscObjectGrantAccess(obj)  0
1545 #define PetscObjectDepublish(obj)      0
1546 #endif
1547 
1548 /*
1549       Simple PETSc parallel IO for ASCII printing
1550 */
1551 extern PetscErrorCode   PetscFixFilename(const char[],char[]);
1552 extern PetscErrorCode   PetscFOpen(MPI_Comm,const char[],const char[],FILE**);
1553 extern PetscErrorCode   PetscFClose(MPI_Comm,FILE*);
1554 extern PetscErrorCode   PetscFPrintf(MPI_Comm,FILE*,const char[],...);
1555 extern PetscErrorCode   PetscPrintf(MPI_Comm,const char[],...);
1556 extern PetscErrorCode   PetscSNPrintf(char*,size_t,const char [],...);
1557 
1558 
1559 
1560 /* These are used internally by PETSc ASCII IO routines*/
1561 #include <stdarg.h>
1562 extern PetscErrorCode   PetscVSNPrintf(char*,size_t,const char[],size_t*,va_list);
1563 extern PetscErrorCode   (*PetscVFPrintf)(FILE*,const char[],va_list);
1564 extern PetscErrorCode   PetscVFPrintfDefault(FILE*,const char[],va_list);
1565 
1566 #if defined(PETSC_HAVE_MATLAB_ENGINE)
1567 extern PetscErrorCode  PetscVFPrintf_Matlab(FILE*,const char[],va_list);
1568 #endif
1569 
1570 /*MC
1571     PetscErrorPrintf - Prints error messages.
1572 
1573    Synopsis:
1574      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
1575 
1576     Not Collective
1577 
1578     Input Parameters:
1579 .   format - the usual printf() format string
1580 
1581    Options Database Keys:
1582 +    -error_output_stdout - cause error messages to be printed to stdout instead of the
1583          (default) stderr
1584 -    -error_output_none to turn off all printing of error messages (does not change the way the
1585           error is handled.)
1586 
1587    Notes: Use
1588 $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
1589 $                        error is handled.) and
1590 $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on
1591 $        of you can use your own function
1592 
1593           Use
1594      PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
1595      PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.
1596 
1597           Use
1598       PetscPushErrorHandler() to provide your own error handler that determines what kind of messages to print
1599 
1600    Level: developer
1601 
1602     Fortran Note:
1603     This routine is not supported in Fortran.
1604 
1605     Concepts: error messages^printing
1606     Concepts: printing^error messages
1607 
1608 .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscHelpPrintf(), PetscPrintf(), PetscErrorHandlerPush(), PetscVFPrintf(), PetscHelpPrintf()
1609 M*/
1610 extern  PetscErrorCode (*PetscErrorPrintf)(const char[],...);
1611 
1612 /*MC
1613     PetscHelpPrintf - Prints help messages.
1614 
1615    Synopsis:
1616      PetscErrorCode (*PetscHelpPrintf)(const char format[],...);
1617 
1618     Not Collective
1619 
1620     Input Parameters:
1621 .   format - the usual printf() format string
1622 
1623    Level: developer
1624 
1625     Fortran Note:
1626     This routine is not supported in Fortran.
1627 
1628     Concepts: help messages^printing
1629     Concepts: printing^help messages
1630 
1631 .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscErrorPrintf()
1632 M*/
1633 extern  PetscErrorCode  (*PetscHelpPrintf)(MPI_Comm,const char[],...);
1634 
1635 extern PetscErrorCode  PetscErrorPrintfDefault(const char [],...);
1636 extern PetscErrorCode  PetscErrorPrintfNone(const char [],...);
1637 extern PetscErrorCode  PetscHelpPrintfDefault(MPI_Comm,const char [],...);
1638 
1639 #if defined(PETSC_HAVE_POPEN)
1640 extern PetscErrorCode   PetscPOpen(MPI_Comm,const char[],const char[],const char[],FILE **);
1641 extern PetscErrorCode   PetscPClose(MPI_Comm,FILE*);
1642 #endif
1643 
1644 extern PetscErrorCode   PetscSynchronizedPrintf(MPI_Comm,const char[],...);
1645 extern PetscErrorCode   PetscSynchronizedFPrintf(MPI_Comm,FILE*,const char[],...);
1646 extern PetscErrorCode   PetscSynchronizedFlush(MPI_Comm);
1647 extern PetscErrorCode   PetscSynchronizedFGets(MPI_Comm,FILE*,size_t,char[]);
1648 extern PetscErrorCode   PetscStartMatlab(MPI_Comm,const char[],const char[],FILE**);
1649 extern PetscErrorCode   PetscStartJava(MPI_Comm,const char[],const char[],FILE**);
1650 extern PetscErrorCode   PetscGetPetscDir(const char*[]);
1651 
1652 extern PetscErrorCode   PetscPopUpSelect(MPI_Comm,const char*,const char*,int,const char**,int*);
1653 
1654 /*S
1655      PetscContainer - Simple PETSc object that contains a pointer to any required data
1656 
1657    Level: advanced
1658 
1659 .seealso:  PetscObject, PetscContainerCreate()
1660 S*/
1661 extern PetscClassId  PETSC_CONTAINER_CLASSID;
1662 typedef struct _p_PetscContainer*  PetscContainer;
1663 extern PetscErrorCode  PetscContainerGetPointer(PetscContainer,void **);
1664 extern PetscErrorCode  PetscContainerSetPointer(PetscContainer,void *);
1665 extern PetscErrorCode  PetscContainerDestroy(PetscContainer);
1666 extern PetscErrorCode  PetscContainerCreate(MPI_Comm,PetscContainer *);
1667 extern PetscErrorCode  PetscContainerSetUserDestroy(PetscContainer, PetscErrorCode (*)(void*));
1668 
1669 /*
1670    For use in debuggers
1671 */
1672 extern  PetscMPIInt PetscGlobalRank;
1673 extern  PetscMPIInt PetscGlobalSize;
1674 extern PetscErrorCode  PetscIntView(PetscInt,const PetscInt[],PetscViewer);
1675 extern PetscErrorCode  PetscRealView(PetscInt,const PetscReal[],PetscViewer);
1676 extern PetscErrorCode  PetscScalarView(PetscInt,const PetscScalar[],PetscViewer);
1677 
1678 #if defined(PETSC_HAVE_MEMORY_H)
1679 #include <memory.h>
1680 #endif
1681 #if defined(PETSC_HAVE_STDLIB_H)
1682 #include <stdlib.h>
1683 #endif
1684 #if defined(PETSC_HAVE_STRINGS_H)
1685 #include <strings.h>
1686 #endif
1687 #if defined(PETSC_HAVE_STRING_H)
1688 #include <string.h>
1689 #endif
1690 
1691 
1692 #if defined(PETSC_HAVE_XMMINTRIN_H)
1693 #include <xmmintrin.h>
1694 #endif
1695 #if defined(PETSC_HAVE_STDINT_H)
1696 #include <stdint.h>
1697 #endif
1698 
1699 /*@C
1700    PetscMemcpy - Copies n bytes, beginning at location b, to the space
1701    beginning at location a. The two memory regions CANNOT overlap, use
1702    PetscMemmove() in that case.
1703 
1704    Not Collective
1705 
1706    Input Parameters:
1707 +  b - pointer to initial memory space
1708 -  n - length (in bytes) of space to copy
1709 
1710    Output Parameter:
1711 .  a - pointer to copy space
1712 
1713    Level: intermediate
1714 
1715    Compile Option:
1716     PETSC_PREFER_DCOPY_FOR_MEMCPY will cause the BLAS dcopy() routine to be used
1717                                   for memory copies on double precision values.
1718     PETSC_PREFER_COPY_FOR_MEMCPY will cause C code to be used
1719                                   for memory copies on double precision values.
1720     PETSC_PREFER_FORTRAN_FORMEMCPY will cause Fortran code to be used
1721                                   for memory copies on double precision values.
1722 
1723    Note:
1724    This routine is analogous to memcpy().
1725 
1726    Developer Note: this is inlined for fastest performance
1727 
1728   Concepts: memory^copying
1729   Concepts: copying^memory
1730 
1731 .seealso: PetscMemmove()
1732 
1733 @*/
1734 PETSC_STATIC_INLINE PetscErrorCode  PetscMemcpy(void *a,const void *b,size_t n)
1735 {
1736 #if defined(PETSC_USE_DEBUG)
1737   unsigned long al = (unsigned long) a,bl = (unsigned long) b;
1738   unsigned long nl = (unsigned long) n;
1739   if (n > 0 && !b) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy from a null pointer");
1740   if (n > 0 && !a) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy to a null pointer");
1741 #endif
1742   PetscFunctionBegin;
1743   if (a != b) {
1744 #if defined(PETSC_USE_DEBUG)
1745     if ((al > bl && (al - bl) < nl) || (bl - al) < nl) {
1746       SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Memory regions overlap: either use PetscMemmov()\n\
1747               or make sure your copy regions and lengths are correct. \n\
1748               Length (bytes) %ld first address %ld second address %ld",nl,al,bl);
1749     }
1750 #endif
1751 #if (defined(PETSC_PREFER_DCOPY_FOR_MEMCPY) || defined(PETSC_PREFER_COPY_FOR_MEMCPY) || defined(PETSC_PREFER_FORTRAN_FORMEMCPY))
1752    if (!(((long) a) % sizeof(PetscScalar)) && !(n % sizeof(PetscScalar))) {
1753       size_t len = n/sizeof(PetscScalar);
1754 #if defined(PETSC_PREFER_DCOPY_FOR_MEMCPY)
1755       PetscBLASInt one = 1,blen = PetscBLASIntCast(len);
1756       BLAScopy_(&blen,(PetscScalar *)b,&one,(PetscScalar *)a,&one);
1757 #elif defined(PETSC_PREFER_FORTRAN_FORMEMCPY)
1758       fortrancopy_(&len,(PetscScalar*)b,(PetscScalar*)a);
1759 #else
1760       size_t      i;
1761       PetscScalar *x = (PetscScalar*)b, *y = (PetscScalar*)a;
1762       for (i=0; i<len; i++) y[i] = x[i];
1763 #endif
1764     } else {
1765       memcpy((char*)(a),(char*)(b),n);
1766     }
1767 #elif defined(PETSC_HAVE__INTEL_FAST_MEMCPY)
1768     _intel_fast_memcpy((char*)(a),(char*)(b),n);
1769 #else
1770     memcpy((char*)(a),(char*)(b),n);
1771 #endif
1772   }
1773   PetscFunctionReturn(0);
1774 }
1775 
1776 /*@C
1777    PetscMemzero - Zeros the specified memory.
1778 
1779    Not Collective
1780 
1781    Input Parameters:
1782 +  a - pointer to beginning memory location
1783 -  n - length (in bytes) of memory to initialize
1784 
1785    Level: intermediate
1786 
1787    Compile Option:
1788    PETSC_PREFER_BZERO - on certain machines (the IBM RS6000) the bzero() routine happens
1789   to be faster than the memset() routine. This flag causes the bzero() routine to be used.
1790 
1791    Developer Note: this is inlined for fastest performance
1792 
1793    Concepts: memory^zeroing
1794    Concepts: zeroing^memory
1795 
1796 .seealso: PetscMemcpy()
1797 @*/
1798 PETSC_STATIC_INLINE PetscErrorCode  PetscMemzero(void *a,size_t n)
1799 {
1800   if (n > 0) {
1801 #if defined(PETSC_USE_DEBUG)
1802     if (!a) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to zero at a null pointer");
1803 #endif
1804 #if defined(PETSC_PREFER_ZERO_FOR_MEMZERO)
1805     if (!(((long) a) % sizeof(PetscScalar)) && !(n % sizeof(PetscScalar))) {
1806       size_t      i,len = n/sizeof(PetscScalar);
1807       PetscScalar *x = (PetscScalar*)a;
1808       for (i=0; i<len; i++) x[i] = 0.0;
1809     } else {
1810 #elif defined(PETSC_PREFER_FORTRAN_FOR_MEMZERO)
1811     if (!(((long) a) % sizeof(PetscScalar)) && !(n % sizeof(PetscScalar))) {
1812       PetscInt len = n/sizeof(PetscScalar);
1813       fortranzero_(&len,(PetscScalar*)a);
1814     } else {
1815 #endif
1816 #if defined(PETSC_PREFER_BZERO)
1817       bzero((char *)a,n);
1818 #elif defined (PETSC_HAVE__INTEL_FAST_MEMSET)
1819       _intel_fast_memset((char*)a,0,n);
1820 #else
1821       memset((char*)a,0,n);
1822 #endif
1823 #if defined(PETSC_PREFER_ZERO_FOR_MEMZERO) || defined(PETSC_PREFER_FORTRAN_FOR_MEMZERO)
1824     }
1825 #endif
1826   }
1827   return 0;
1828 }
1829 
1830 /*MC
1831    PetscPrefetchBlock - Prefetches a block of memory
1832 
1833    Synopsis:
1834     void PetscPrefetchBlock(const anytype *a,size_t n,int rw,int t)
1835 
1836    Not Collective
1837 
1838    Input Parameters:
1839 +  a - pointer to first element to fetch (any type but usually PetscInt or PetscScalar)
1840 .  n - number of elements to fetch
1841 .  rw - 1 if the memory will be written to, otherwise 0 (ignored by many processors)
1842 -  t - temporal locality (PETSC_PREFETCH_HINT_{NTA,T0,T1,T2}), see note
1843 
1844    Level: developer
1845 
1846    Notes:
1847    The last two arguments (rw and t) must be compile-time constants.
1848 
1849    Adopting Intel's x86/x86-64 conventions, there are four levels of temporal locality.  Not all architectures offer
1850    equivalent locality hints, but the following macros are always defined to their closest analogue.
1851 +  PETSC_PREFETCH_HINT_NTA - Non-temporal.  Prefetches directly to L1, evicts to memory (skips higher level cache unless it was already there when prefetched).
1852 .  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.
1853 .  PETSC_PREFETCH_HINT_T1 - Fetch to level 2 and higher (not L1).
1854 -  PETSC_PREFETCH_HINT_T2 - Fetch to high-level cache only.  (On many systems, T0 and T1 are equivalent.)
1855 
1856    This function does nothing on architectures that do not support prefetch and never errors (even if passed an invalid
1857    address).
1858 
1859    Concepts: memory
1860 M*/
1861 #define PetscPrefetchBlock(a,n,rw,t) do {                               \
1862     const char *_p = (const char*)(a),*_end = (const char*)((a)+(n));   \
1863     for ( ; _p < _end; _p += PETSC_LEVEL1_DCACHE_LINESIZE) PETSC_Prefetch(_p,(rw),(t)); \
1864   } while (0)
1865 
1866 /*
1867     Allows accessing Matlab Engine
1868 */
1869 #include "petscmatlab.h"
1870 
1871 /*
1872       Determine if some of the kernel computation routines use
1873    Fortran (rather than C) for the numerical calculations. On some machines
1874    and compilers (like complex numbers) the Fortran version of the routines
1875    is faster than the C/C++ versions. The flag --with-fortran-kernels
1876    should be used with ./configure to turn these on.
1877 */
1878 #if defined(PETSC_USE_FORTRAN_KERNELS)
1879 
1880 #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTCRL)
1881 #define PETSC_USE_FORTRAN_KERNEL_MULTCRL
1882 #endif
1883 
1884 #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJPERM)
1885 #define PETSC_USE_FORTRAN_KERNEL_MULTAIJPERM
1886 #endif
1887 
1888 #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1889 #define PETSC_USE_FORTRAN_KERNEL_MULTAIJ
1890 #endif
1891 
1892 #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1893 #define PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ
1894 #endif
1895 
1896 #if !defined(PETSC_USE_FORTRAN_KERNEL_NORM)
1897 #define PETSC_USE_FORTRAN_KERNEL_NORM
1898 #endif
1899 
1900 #if !defined(PETSC_USE_FORTRAN_KERNEL_MAXPY)
1901 #define PETSC_USE_FORTRAN_KERNEL_MAXPY
1902 #endif
1903 
1904 #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ)
1905 #define PETSC_USE_FORTRAN_KERNEL_SOLVEAIJ
1906 #endif
1907 
1908 #if !defined(PETSC_USE_FORTRAN_KERNEL_RELAXAIJ)
1909 #define PETSC_USE_FORTRAN_KERNEL_RELAXAIJ
1910 #endif
1911 
1912 #if !defined(PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ)
1913 #define PETSC_USE_FORTRAN_KERNEL_SOLVEBAIJ
1914 #endif
1915 
1916 #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1917 #define PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ
1918 #endif
1919 
1920 #if !defined(PETSC_USE_FORTRAN_KERNEL_MDOT)
1921 #define PETSC_USE_FORTRAN_KERNEL_MDOT
1922 #endif
1923 
1924 #if !defined(PETSC_USE_FORTRAN_KERNEL_XTIMESY)
1925 #define PETSC_USE_FORTRAN_KERNEL_XTIMESY
1926 #endif
1927 
1928 #if !defined(PETSC_USE_FORTRAN_KERNEL_AYPX)
1929 #define PETSC_USE_FORTRAN_KERNEL_AYPX
1930 #endif
1931 
1932 #if !defined(PETSC_USE_FORTRAN_KERNEL_WAXPY)
1933 #define PETSC_USE_FORTRAN_KERNEL_WAXPY
1934 #endif
1935 
1936 #endif
1937 
1938 /*
1939     Macros for indicating code that should be compiled with a C interface,
1940    rather than a C++ interface. Any routines that are dynamically loaded
1941    (such as the PCCreate_XXX() routines) must be wrapped so that the name
1942    mangler does not change the functions symbol name. This just hides the
1943    ugly extern "C" {} wrappers.
1944 */
1945 #if defined(__cplusplus)
1946 #define EXTERN_C_BEGIN extern "C" {
1947 #define EXTERN_C_END }
1948 #else
1949 #define EXTERN_C_BEGIN
1950 #define EXTERN_C_END
1951 #endif
1952 
1953 /* --------------------------------------------------------------------*/
1954 
1955 /*MC
1956     MPI_Comm - the basic object used by MPI to determine which processes are involved in a
1957         communication
1958 
1959    Level: beginner
1960 
1961    Note: This manual page is a place-holder because MPICH does not have a manual page for MPI_Comm
1962 
1963 .seealso: PETSC_COMM_WORLD, PETSC_COMM_SELF
1964 M*/
1965 
1966 /*MC
1967     PetscScalar - PETSc type that represents either a double precision real number, a double precision
1968        complex number, a single precision real number, a long double or an int - if the code is configured
1969        with --with-scalar-type=real,complex --with-precision=single,double,longdouble,int,matsingle
1970 
1971 
1972    Level: beginner
1973 
1974 .seealso: PetscReal, PassiveReal, PassiveScalar, MPIU_SCALAR, PetscInt
1975 M*/
1976 
1977 /*MC
1978     PetscReal - PETSc type that represents a real number version of PetscScalar
1979 
1980    Level: beginner
1981 
1982 .seealso: PetscScalar, PassiveReal, PassiveScalar
1983 M*/
1984 
1985 /*MC
1986     PassiveScalar - PETSc type that represents a PetscScalar
1987    Level: beginner
1988 
1989     This is the same as a PetscScalar except in code that is automatically differentiated it is
1990    treated as a constant (not an indendent or dependent variable)
1991 
1992 .seealso: PetscReal, PassiveReal, PetscScalar
1993 M*/
1994 
1995 /*MC
1996     PassiveReal - PETSc type that represents a PetscReal
1997 
1998    Level: beginner
1999 
2000     This is the same as a PetscReal except in code that is automatically differentiated it is
2001    treated as a constant (not an indendent or dependent variable)
2002 
2003 .seealso: PetscScalar, PetscReal, PassiveScalar
2004 M*/
2005 
2006 /*MC
2007     MPIU_SCALAR - MPI datatype corresponding to PetscScalar
2008 
2009    Level: beginner
2010 
2011     Note: In MPI calls that require an MPI datatype that matches a PetscScalar or array of PetscScalars
2012           pass this value
2013 
2014 .seealso: PetscReal, PassiveReal, PassiveScalar, PetscScalar, MPIU_INT
2015 M*/
2016 
2017 #if defined(PETSC_HAVE_MPIIO)
2018 #if !defined(PETSC_WORDS_BIGENDIAN)
2019 extern PetscErrorCode MPIU_File_write_all(MPI_File,void*,PetscMPIInt,MPI_Datatype,MPI_Status*);
2020 extern PetscErrorCode MPIU_File_read_all(MPI_File,void*,PetscMPIInt,MPI_Datatype,MPI_Status*);
2021 #else
2022 #define MPIU_File_write_all(a,b,c,d,e) MPI_File_write_all(a,b,c,d,e)
2023 #define MPIU_File_read_all(a,b,c,d,e) MPI_File_read_all(a,b,c,d,e)
2024 #endif
2025 #endif
2026 
2027 /* the following petsc_static_inline require petscerror.h */
2028 
2029 /* Limit MPI to 32-bits */
2030 #define PETSC_MPI_INT_MAX  2147483647
2031 #define PETSC_MPI_INT_MIN -2147483647
2032 /* Limit BLAS to 32-bits */
2033 #define PETSC_BLAS_INT_MAX  2147483647
2034 #define PETSC_BLAS_INT_MIN -2147483647
2035 /* On 32 bit systems HDF5 is limited by size of integer, because hsize_t is defined as size_t */
2036 #define PETSC_HDF5_INT_MAX  2147483647
2037 #define PETSC_HDF5_INT_MIN -2147483647
2038 
2039 #if defined(PETSC_USE_64BIT_INDICES)
2040 #define PetscMPIIntCheck(a)  if ((a) > PETSC_MPI_INT_MAX) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Message too long for MPI")
2041 #define PetscBLASIntCheck(a)  if ((a) > PETSC_BLAS_INT_MAX) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Array too long for BLAS/LAPACK")
2042 #define PetscMPIIntCast(a) (a);PetscMPIIntCheck(a)
2043 #define PetscBLASIntCast(a) (a);PetscBLASIntCheck(a)
2044 
2045 #if (PETSC_SIZEOF_SIZE_T == 4)
2046 #define PetscHDF5IntCheck(a)  if ((a) > PETSC_HDF5_INT_MAX) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Array too long for HDF5")
2047 #define PetscHDF5IntCast(a) (a);PetscHDF5IntCheck(a)
2048 #else
2049 #define PetscHDF5IntCheck(a)
2050 #define PetscHDF5IntCast(a) a
2051 #endif
2052 
2053 #else
2054 #define PetscMPIIntCheck(a)
2055 #define PetscBLASIntCheck(a)
2056 #define PetscHDF5IntCheck(a)
2057 #define PetscMPIIntCast(a) a
2058 #define PetscBLASIntCast(a) a
2059 #define PetscHDF5IntCast(a) a
2060 #endif
2061 
2062 
2063 /*
2064      The IBM include files define hz, here we hide it so that it may be used
2065    as a regular user variable.
2066 */
2067 #if defined(hz)
2068 #undef hz
2069 #endif
2070 
2071 /*  For arrays that contain filenames or paths */
2072 
2073 
2074 #if defined(PETSC_HAVE_LIMITS_H)
2075 #include <limits.h>
2076 #endif
2077 #if defined(PETSC_HAVE_SYS_PARAM_H)
2078 #include <sys/param.h>
2079 #endif
2080 #if defined(PETSC_HAVE_SYS_TYPES_H)
2081 #include <sys/types.h>
2082 #endif
2083 #if defined(MAXPATHLEN)
2084 #  define PETSC_MAX_PATH_LEN     MAXPATHLEN
2085 #elif defined(MAX_PATH)
2086 #  define PETSC_MAX_PATH_LEN     MAX_PATH
2087 #elif defined(_MAX_PATH)
2088 #  define PETSC_MAX_PATH_LEN     _MAX_PATH
2089 #else
2090 #  define PETSC_MAX_PATH_LEN     4096
2091 #endif
2092 
2093 /* Special support for C++ */
2094 #include "petscsys.hh"
2095 
2096 
2097 /*MC
2098 
2099     UsingFortran - Fortran can be used with PETSc in four distinct approaches
2100 
2101 $    1) classic Fortran 77 style
2102 $#include "finclude/petscXXX.h" to work with material from the XXX component of PETSc
2103 $       XXX variablename
2104 $      You cannot use this approach if you wish to use the Fortran 90 specific PETSc routines
2105 $      which end in F90; such as VecGetArrayF90()
2106 $
2107 $    2) classic Fortran 90 style
2108 $#include "finclude/petscXXX.h"
2109 $#include "finclude/petscXXX.h90" to work with material from the XXX component of PETSc
2110 $       XXX variablename
2111 $
2112 $    3) Using Fortran modules
2113 $#include "finclude/petscXXXdef.h"
2114 $         use petscXXXX
2115 $       XXX variablename
2116 $
2117 $    4) Use Fortran modules and Fortran data types for PETSc types
2118 $#include "finclude/petscXXXdef.h"
2119 $         use petscXXXX
2120 $       type(XXX) variablename
2121 $      To use this approach you must ./configure PETSc with the additional
2122 $      option --with-fortran-datatypes You cannot use the type(XXX) declaration approach without using Fortran modules
2123 
2124     Finally if you absolutely do not want to use any #include you can use either
2125 
2126 $    3a) skip the #include BUT you cannot use any PETSc data type names like Vec, Mat, PetscInt, PetscErrorCode etc
2127 $        and you must declare the variables as integer, for example
2128 $        integer variablename
2129 $
2130 $    4a) skip the #include, you use the object types like type(Vec) type(Mat) but cannot use the data type
2131 $        names like PetscErrorCode, PetscInt etc. again for those you must use integer
2132 
2133    We recommend either 2 or 3. Approaches 2 and 3 provide type checking for most PETSc function calls; 4 has type checking
2134 for only a few PETSc functions.
2135 
2136    Fortran type checking with interfaces is strick, this means you cannot pass a scalar value when an array value
2137 is expected (even though it is legal Fortran). For example when setting a single value in a matrix with MatSetValues()
2138 you cannot have something like
2139 $      PetscInt row,col
2140 $      PetscScalar val
2141 $        ...
2142 $      call MatSetValues(mat,1,row,1,col,val,INSERT_VALUES,ierr)
2143 You must instead have
2144 $      PetscInt row(1),col(1)
2145 $      PetscScalar val(1)
2146 $        ...
2147 $      call MatSetValues(mat,1,row,1,col,val,INSERT_VALUES,ierr)
2148 
2149 
2150     See the example src/vec/vec/examples/tutorials/ex20f90.F90 for an example that can use all four approaches
2151 
2152     Developer Notes: The finclude/petscXXXdef.h contain all the #defines (would be typedefs in C code) these
2153      automatically include their predecessors; for example finclude/petscvecdef.h includes finclude/petscisdef.h
2154 
2155      The finclude/petscXXXX.h contain all the parameter statements for that package. These automatically include
2156      their finclude/petscXXXdef.h file but DO NOT automatically include their predecessors;  for example
2157      finclude/petscvec.h does NOT automatically include finclude/petscis.h
2158 
2159      The finclude/ftn-custom/petscXXXdef.h90 are not intended to be used directly in code, they define the
2160      Fortran data type type(XXX) (for example type(Vec)) when PETSc is ./configure with the --with-fortran-datatypes option.
2161 
2162      The finclude/ftn-custom/petscXXX.h90 (not included directly by code) contain interface definitions for
2163      the PETSc Fortran stubs that have different bindings then their C version (for example VecGetArrayF90).
2164 
2165      The finclude/ftn-auto/petscXXX.h90 (not included directly by code) contain interface definitions generated
2166      automatically by "make allfortranstubs".
2167 
2168      The finclude/petscXXX.h90 includes the custom finclude/ftn-custom/petscXXX.h90 and if ./configure
2169      was run with --with-fortran-interfaces it also includes the finclude/ftn-auto/petscXXX.h90 These DO NOT automatically
2170      include their predecessors
2171 
2172     Level: beginner
2173 
2174 M*/
2175 
2176 extern PetscErrorCode  PetscGetArchType(char[],size_t);
2177 extern PetscErrorCode  PetscGetHostName(char[],size_t);
2178 extern PetscErrorCode  PetscGetUserName(char[],size_t);
2179 extern PetscErrorCode  PetscGetProgramName(char[],size_t);
2180 extern PetscErrorCode  PetscSetProgramName(const char[]);
2181 extern PetscErrorCode  PetscGetDate(char[],size_t);
2182 
2183 extern PetscErrorCode  PetscSortInt(PetscInt,PetscInt[]);
2184 extern PetscErrorCode  PetscSortRemoveDupsInt(PetscInt*,PetscInt[]);
2185 extern PetscErrorCode  PetscSortIntWithPermutation(PetscInt,const PetscInt[],PetscInt[]);
2186 extern PetscErrorCode  PetscSortStrWithPermutation(PetscInt,const char*[],PetscInt[]);
2187 extern PetscErrorCode  PetscSortIntWithArray(PetscInt,PetscInt[],PetscInt[]);
2188 extern PetscErrorCode  PetscSortMPIIntWithArray(PetscMPIInt,PetscMPIInt[],PetscMPIInt[]);
2189 extern PetscErrorCode  PetscSortIntWithScalarArray(PetscInt,PetscInt[],PetscScalar[]);
2190 extern PetscErrorCode  PetscSortReal(PetscInt,PetscReal[]);
2191 extern PetscErrorCode  PetscSortRealWithPermutation(PetscInt,const PetscReal[],PetscInt[]);
2192 extern PetscErrorCode  PetscSortSplit(PetscInt,PetscInt,PetscScalar[],PetscInt[]);
2193 extern PetscErrorCode  PetscSortSplitReal(PetscInt,PetscInt,PetscReal[],PetscInt[]);
2194 extern PetscErrorCode  PetscProcessTree(PetscInt,const PetscBool [],const PetscInt[],PetscInt*,PetscInt**,PetscInt**,PetscInt**,PetscInt**);
2195 
2196 extern PetscErrorCode  PetscSetDisplay(void);
2197 extern PetscErrorCode  PetscGetDisplay(char[],size_t);
2198 
2199 /*E
2200     PetscRandomType - String with the name of a PETSc randomizer
2201        with an optional dynamic library name, for example
2202        http://www.mcs.anl.gov/petsc/lib.a:myrandcreate()
2203 
2204    Level: beginner
2205 
2206    Notes: to use the SPRNG you must have ./configure PETSc
2207    with the option --download-sprng
2208 
2209 .seealso: PetscRandomSetType(), PetscRandom
2210 E*/
2211 #define PetscRandomType char*
2212 #define PETSCRAND       "rand"
2213 #define PETSCRAND48     "rand48"
2214 #define PETSCSPRNG      "sprng"
2215 
2216 /* Logging support */
2217 extern  PetscClassId PETSC_RANDOM_CLASSID;
2218 
2219 extern PetscErrorCode  PetscRandomInitializePackage(const char[]);
2220 
2221 /*S
2222      PetscRandom - Abstract PETSc object that manages generating random numbers
2223 
2224    Level: intermediate
2225 
2226   Concepts: random numbers
2227 
2228 .seealso:  PetscRandomCreate(), PetscRandomGetValue(), PetscRandomType
2229 S*/
2230 typedef struct _p_PetscRandom*   PetscRandom;
2231 
2232 /* Dynamic creation and loading functions */
2233 extern PetscFList PetscRandomList;
2234 extern PetscBool  PetscRandomRegisterAllCalled;
2235 
2236 extern PetscErrorCode  PetscRandomRegisterAll(const char []);
2237 extern PetscErrorCode  PetscRandomRegister(const char[],const char[],const char[],PetscErrorCode (*)(PetscRandom));
2238 extern PetscErrorCode  PetscRandomRegisterDestroy(void);
2239 extern PetscErrorCode  PetscRandomSetType(PetscRandom, const PetscRandomType);
2240 extern PetscErrorCode  PetscRandomSetFromOptions(PetscRandom);
2241 extern PetscErrorCode  PetscRandomGetType(PetscRandom, const PetscRandomType*);
2242 extern PetscErrorCode  PetscRandomViewFromOptions(PetscRandom,char*);
2243 extern PetscErrorCode  PetscRandomView(PetscRandom,PetscViewer);
2244 
2245 /*MC
2246   PetscRandomRegisterDynamic - Adds a new PetscRandom component implementation
2247 
2248   Synopsis:
2249   PetscErrorCode PetscRandomRegisterDynamic(const char *name, const char *path, const char *func_name, PetscErrorCode (*create_func)(PetscRandom))
2250 
2251   Not Collective
2252 
2253   Input Parameters:
2254 + name        - The name of a new user-defined creation routine
2255 . path        - The path (either absolute or relative) of the library containing this routine
2256 . func_name   - The name of routine to create method context
2257 - create_func - The creation routine itself
2258 
2259   Notes:
2260   PetscRandomRegisterDynamic() may be called multiple times to add several user-defined randome number generators
2261 
2262   If dynamic libraries are used, then the fourth input argument (routine_create) is ignored.
2263 
2264   Sample usage:
2265 .vb
2266     PetscRandomRegisterDynamic("my_rand","/home/username/my_lib/lib/libO/solaris/libmy.a", "MyPetscRandomtorCreate", MyPetscRandomtorCreate);
2267 .ve
2268 
2269   Then, your random type can be chosen with the procedural interface via
2270 .vb
2271     PetscRandomCreate(MPI_Comm, PetscRandom *);
2272     PetscRandomSetType(PetscRandom,"my_random_name");
2273 .ve
2274    or at runtime via the option
2275 .vb
2276     -random_type my_random_name
2277 .ve
2278 
2279   Notes: $PETSC_ARCH occuring in pathname will be replaced with appropriate values.
2280 
2281          For an example of the code needed to interface your own random number generator see
2282          src/sys/random/impls/rand/rand.c
2283 
2284   Level: advanced
2285 
2286 .keywords: PetscRandom, register
2287 .seealso: PetscRandomRegisterAll(), PetscRandomRegisterDestroy(), PetscRandomRegister()
2288 M*/
2289 #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
2290 #define PetscRandomRegisterDynamic(a,b,c,d) PetscRandomRegister(a,b,c,0)
2291 #else
2292 #define PetscRandomRegisterDynamic(a,b,c,d) PetscRandomRegister(a,b,c,d)
2293 #endif
2294 
2295 extern PetscErrorCode  PetscRandomCreate(MPI_Comm,PetscRandom*);
2296 extern PetscErrorCode  PetscRandomGetValue(PetscRandom,PetscScalar*);
2297 extern PetscErrorCode  PetscRandomGetValueReal(PetscRandom,PetscReal*);
2298 extern PetscErrorCode  PetscRandomGetInterval(PetscRandom,PetscScalar*,PetscScalar*);
2299 extern PetscErrorCode  PetscRandomSetInterval(PetscRandom,PetscScalar,PetscScalar);
2300 extern PetscErrorCode  PetscRandomSetSeed(PetscRandom,unsigned long);
2301 extern PetscErrorCode  PetscRandomGetSeed(PetscRandom,unsigned long *);
2302 extern PetscErrorCode  PetscRandomSeed(PetscRandom);
2303 extern PetscErrorCode  PetscRandomDestroy(PetscRandom);
2304 
2305 extern PetscErrorCode  PetscGetFullPath(const char[],char[],size_t);
2306 extern PetscErrorCode  PetscGetRelativePath(const char[],char[],size_t);
2307 extern PetscErrorCode  PetscGetWorkingDirectory(char[],size_t);
2308 extern PetscErrorCode  PetscGetRealPath(const char[],char[]);
2309 extern PetscErrorCode  PetscGetHomeDirectory(char[],size_t);
2310 extern PetscErrorCode  PetscTestFile(const char[],char,PetscBool *);
2311 extern PetscErrorCode  PetscTestDirectory(const char[],char,PetscBool *);
2312 
2313 extern PetscErrorCode  PetscBinaryRead(int,void*,PetscInt,PetscDataType);
2314 extern PetscErrorCode  PetscBinarySynchronizedRead(MPI_Comm,int,void*,PetscInt,PetscDataType);
2315 extern PetscErrorCode  PetscBinarySynchronizedWrite(MPI_Comm,int,void*,PetscInt,PetscDataType,PetscBool );
2316 extern PetscErrorCode  PetscBinaryWrite(int,void*,PetscInt,PetscDataType,PetscBool );
2317 extern PetscErrorCode  PetscBinaryOpen(const char[],PetscFileMode,int *);
2318 extern PetscErrorCode  PetscBinaryClose(int);
2319 extern PetscErrorCode  PetscSharedTmp(MPI_Comm,PetscBool  *);
2320 extern PetscErrorCode  PetscSharedWorkingDirectory(MPI_Comm,PetscBool  *);
2321 extern PetscErrorCode  PetscGetTmp(MPI_Comm,char[],size_t);
2322 extern PetscErrorCode  PetscFileRetrieve(MPI_Comm,const char[],char[],size_t,PetscBool *);
2323 extern PetscErrorCode  PetscLs(MPI_Comm,const char[],char[],size_t,PetscBool *);
2324 extern PetscErrorCode  PetscOpenSocket(char*,int,int*);
2325 extern PetscErrorCode  PetscWebServe(MPI_Comm,int);
2326 
2327 /*
2328    In binary files variables are stored using the following lengths,
2329   regardless of how they are stored in memory on any one particular
2330   machine. Use these rather then sizeof() in computing sizes for
2331   PetscBinarySeek().
2332 */
2333 #define PETSC_BINARY_INT_SIZE    (32/8)
2334 #define PETSC_BINARY_FLOAT_SIZE  (32/8)
2335 #define PETSC_BINARY_CHAR_SIZE    (8/8)
2336 #define PETSC_BINARY_SHORT_SIZE  (16/8)
2337 #define PETSC_BINARY_DOUBLE_SIZE (64/8)
2338 #define PETSC_BINARY_SCALAR_SIZE sizeof(PetscScalar)
2339 
2340 /*E
2341   PetscBinarySeekType - argument to PetscBinarySeek()
2342 
2343   Level: advanced
2344 
2345 .seealso: PetscBinarySeek(), PetscBinarySynchronizedSeek()
2346 E*/
2347 typedef enum {PETSC_BINARY_SEEK_SET = 0,PETSC_BINARY_SEEK_CUR = 1,PETSC_BINARY_SEEK_END = 2} PetscBinarySeekType;
2348 extern PetscErrorCode  PetscBinarySeek(int,off_t,PetscBinarySeekType,off_t*);
2349 extern PetscErrorCode  PetscBinarySynchronizedSeek(MPI_Comm,int,off_t,PetscBinarySeekType,off_t*);
2350 
2351 extern PetscErrorCode  PetscSetDebugTerminal(const char[]);
2352 extern PetscErrorCode  PetscSetDebugger(const char[],PetscBool );
2353 extern PetscErrorCode  PetscSetDefaultDebugger(void);
2354 extern PetscErrorCode  PetscSetDebuggerFromString(char*);
2355 extern PetscErrorCode  PetscAttachDebugger(void);
2356 extern PetscErrorCode  PetscStopForDebugger(void);
2357 
2358 extern PetscErrorCode  PetscGatherNumberOfMessages(MPI_Comm,const PetscMPIInt[],const PetscMPIInt[],PetscMPIInt*);
2359 extern PetscErrorCode  PetscGatherMessageLengths(MPI_Comm,PetscMPIInt,PetscMPIInt,const PetscMPIInt[],PetscMPIInt**,PetscMPIInt**);
2360 extern PetscErrorCode  PetscGatherMessageLengths2(MPI_Comm,PetscMPIInt,PetscMPIInt,const PetscMPIInt[],const PetscMPIInt[],PetscMPIInt**,PetscMPIInt**,PetscMPIInt**);
2361 extern PetscErrorCode  PetscPostIrecvInt(MPI_Comm,PetscMPIInt,PetscMPIInt,const PetscMPIInt[],const PetscMPIInt[],PetscInt***,MPI_Request**);
2362 extern PetscErrorCode  PetscPostIrecvScalar(MPI_Comm,PetscMPIInt,PetscMPIInt,const PetscMPIInt[],const PetscMPIInt[],PetscScalar***,MPI_Request**);
2363 
2364 extern PetscErrorCode  PetscSSEIsEnabled(MPI_Comm,PetscBool  *,PetscBool  *);
2365 
2366 /*E
2367   InsertMode - Whether entries are inserted or added into vectors or matrices
2368 
2369   Level: beginner
2370 
2371 .seealso: VecSetValues(), MatSetValues(), VecSetValue(), VecSetValuesBlocked(),
2372           VecSetValuesLocal(), VecSetValuesBlockedLocal(), MatSetValuesBlocked(),
2373           MatSetValuesBlockedLocal(), MatSetValuesLocal(), VecScatterBegin(), VecScatterEnd()
2374 E*/
2375 typedef enum {NOT_SET_VALUES, INSERT_VALUES, ADD_VALUES, MAX_VALUES} InsertMode;
2376 
2377 /*MC
2378     INSERT_VALUES - Put a value into a vector or matrix, overwrites any previous value
2379 
2380     Level: beginner
2381 
2382 .seealso: InsertMode, VecSetValues(), MatSetValues(), VecSetValue(), VecSetValuesBlocked(),
2383           VecSetValuesLocal(), VecSetValuesBlockedLocal(), MatSetValuesBlocked(), ADD_VALUES,
2384           MatSetValuesBlockedLocal(), MatSetValuesLocal(), VecScatterBegin(), VecScatterEnd(), MAX_VALUES
2385 
2386 M*/
2387 
2388 /*MC
2389     ADD_VALUES - Adds a value into a vector or matrix, if there previously was no value, just puts the
2390                 value into that location
2391 
2392     Level: beginner
2393 
2394 .seealso: InsertMode, VecSetValues(), MatSetValues(), VecSetValue(), VecSetValuesBlocked(),
2395           VecSetValuesLocal(), VecSetValuesBlockedLocal(), MatSetValuesBlocked(), INSERT_VALUES,
2396           MatSetValuesBlockedLocal(), MatSetValuesLocal(), VecScatterBegin(), VecScatterEnd(), MAX_VALUES
2397 
2398 M*/
2399 
2400 /*MC
2401     MAX_VALUES - Puts the maximum of the scattered/gathered value and the current value into each location
2402 
2403     Level: beginner
2404 
2405 .seealso: InsertMode, VecScatterBegin(), VecScatterEnd(), ADD_VALUES, INSERT_VALUES
2406 
2407 M*/
2408 
2409 /*S
2410    PetscSubcomm - Context of MPI subcommunicators, used by PCREDUNDANT
2411 
2412    Level: advanced
2413 
2414    Concepts: communicator, create
2415 S*/
2416 typedef struct _n_PetscSubcomm* PetscSubcomm;
2417 
2418 struct _n_PetscSubcomm {
2419   MPI_Comm   parent;      /* parent communicator */
2420   MPI_Comm   dupparent;   /* duplicate parent communicator, under which the processors of this subcomm have contiguous rank */
2421   MPI_Comm   comm;        /* this communicator */
2422   PetscInt   n;           /* num of subcommunicators under the parent communicator */
2423   PetscInt   color;       /* color of processors belong to this communicator */
2424 };
2425 
2426 typedef enum {PETSC_SUBCOMM_GENERAL=0,PETSC_SUBCOMM_CONTIGUOUS=1,PETSC_SUBCOMM_INTERLACED=2} PetscSubcommType;
2427 extern const char *PetscSubcommTypes[];
2428 
2429 extern PetscErrorCode  PetscSubcommCreate(MPI_Comm,PetscSubcomm*);
2430 extern PetscErrorCode  PetscSubcommDestroy(PetscSubcomm);
2431 extern PetscErrorCode  PetscSubcommSetNumber(PetscSubcomm,PetscInt);
2432 extern PetscErrorCode  PetscSubcommSetType(PetscSubcomm,const PetscSubcommType);
2433 extern PetscErrorCode  PetscSubcommSetTypeGeneral(PetscSubcomm,PetscMPIInt,PetscMPIInt,PetscMPIInt);
2434 
2435 PETSC_EXTERN_CXX_END
2436 
2437 #endif
2438