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