Actual source code: petscimpl.h

petsc-master 2020-01-21
Report Typos and Errors

  2: /*
  3:     Defines the basic header of all PETSc objects.
  4: */

  6: #if !defined(PETSCIMPL_H)
  7: #define PETSCIMPL_H
  8:  #include <petscsys.h>

 10: /* These are used internally by PETSc ASCII IO routines*/
 11: #include <stdarg.h>
 12: PETSC_EXTERN PetscErrorCode PetscVFPrintfDefault(FILE*,const char[],va_list);

 14: #if defined(PETSC_HAVE_CLOSURE)
 15: PETSC_EXTERN PetscErrorCode PetscVFPrintfSetClosure(int (^)(const char*));
 16: #endif


 19: #if defined(PETSC_HAVE_CUDA)
 20: #include <cuda.h>
 21: #include <cublas_v2.h>
 22: #endif

 24: /*
 25:    All major PETSc data structures have a common core; this is defined
 26:    below by PETSCHEADER.

 28:    PetscHeaderCreate() should be used whenever creating a PETSc structure.
 29: */

 31: /*
 32:    PetscOps: structure of core operations that all PETSc objects support.

 34:       getcomm()         - Gets the object's communicator.
 35:       view()            - Is the routine for viewing the entire PETSc object; for
 36:                           example, MatView() is the general matrix viewing routine.
 37:                           This is used by PetscObjectView((PetscObject)obj) to allow
 38:                           viewing any PETSc object.
 39:       destroy()         - Is the routine for destroying the entire PETSc object;
 40:                           for example,MatDestroy() is the general matrix
 41:                           destruction routine.
 42:                           This is used by PetscObjectDestroy((PetscObject*)&obj) to allow
 43:                           destroying any PETSc object.
 44:       compose()         - Associates a PETSc object with another PETSc object with a name
 45:       query()           - Returns a different PETSc object that has been associated
 46:                           with the first object using a name.
 47:       composefunction() - Attaches an a function to a PETSc object with a name.
 48:       queryfunction()   - Requests a registered function that has been attached to a PETSc object.
 49: */

 51: typedef struct {
 52:    PetscErrorCode (*getcomm)(PetscObject,MPI_Comm *);
 53:    PetscErrorCode (*view)(PetscObject,PetscViewer);
 54:    PetscErrorCode (*destroy)(PetscObject*);
 55:    PetscErrorCode (*compose)(PetscObject,const char[],PetscObject);
 56:    PetscErrorCode (*query)(PetscObject,const char[],PetscObject *);
 57:    PetscErrorCode (*composefunction)(PetscObject,const char[],void (*)(void));
 58:    PetscErrorCode (*queryfunction)(PetscObject,const char[],void (**)(void));
 59: } PetscOps;

 61: typedef enum {PETSC_FORTRAN_CALLBACK_CLASS,PETSC_FORTRAN_CALLBACK_SUBTYPE,PETSC_FORTRAN_CALLBACK_MAXTYPE} PetscFortranCallbackType;
 62: typedef int PetscFortranCallbackId;
 63: #define PETSC_SMALLEST_FORTRAN_CALLBACK ((PetscFortranCallbackId)1000)
 64: PETSC_EXTERN PetscErrorCode PetscFortranCallbackRegister(PetscClassId,const char*,PetscFortranCallbackId*);
 65: PETSC_EXTERN PetscErrorCode PetscFortranCallbackGetSizes(PetscClassId,PetscInt*,PetscInt*);

 67: typedef struct {
 68:   void (*func)(void);
 69:   void *ctx;
 70: } PetscFortranCallback;

 72: /*
 73:    All PETSc objects begin with the fields defined in PETSCHEADER.
 74:    The PetscObject is a way of examining these fields regardless of
 75:    the specific object. In C++ this could be a base abstract class
 76:    from which all objects are derived.
 77: */
 78: #define PETSC_MAX_OPTIONS_HANDLER 5
 79: typedef struct _p_PetscObject {
 80:   PetscClassId         classid;
 81:   PetscOps             bops[1];
 82:   MPI_Comm             comm;
 83:   PetscInt             type;
 84:   PetscLogDouble       flops,time,mem,memchildren;
 85:   PetscObjectId        id;
 86:   PetscInt             refct;
 87:   PetscMPIInt          tag;
 88:   PetscFunctionList    qlist;
 89:   PetscObjectList      olist;
 90:   char                 *class_name;    /*  for example, "Vec" */
 91:   char                 *description;
 92:   char                 *mansec;
 93:   char                 *type_name;     /*  this is the subclass, for example VECSEQ which equals "seq" */
 94:   PetscObject          parent;
 95:   PetscObjectId        parentid;
 96:   char*                name;
 97:   char                 *prefix;
 98:   PetscInt             tablevel;
 99:   void                 *cpp;
100:   PetscObjectState     state;
101:   PetscInt             int_idmax,        intstar_idmax;
102:   PetscObjectState     *intcomposedstate,*intstarcomposedstate;
103:   PetscInt             *intcomposeddata, **intstarcomposeddata;
104:   PetscInt             real_idmax,        realstar_idmax;
105:   PetscObjectState     *realcomposedstate,*realstarcomposedstate;
106:   PetscReal            *realcomposeddata, **realstarcomposeddata;
107:   PetscInt             scalar_idmax,        scalarstar_idmax;
108:   PetscObjectState     *scalarcomposedstate,*scalarstarcomposedstate;
109:   PetscScalar          *scalarcomposeddata, **scalarstarcomposeddata;
110:   void                 (**fortran_func_pointers)(void);                  /* used by Fortran interface functions to stash user provided Fortran functions */
111:   PetscInt             num_fortran_func_pointers;                        /* number of Fortran function pointers allocated */
112:   PetscFortranCallback *fortrancallback[PETSC_FORTRAN_CALLBACK_MAXTYPE];
113:   PetscInt             num_fortrancallback[PETSC_FORTRAN_CALLBACK_MAXTYPE];
114:   void                 *python_context;
115:   PetscErrorCode       (*python_destroy)(void*);

117:   PetscInt             noptionhandler;
118:   PetscErrorCode       (*optionhandler[PETSC_MAX_OPTIONS_HANDLER])(PetscOptionItems*,PetscObject,void*);
119:   PetscErrorCode       (*optiondestroy[PETSC_MAX_OPTIONS_HANDLER])(PetscObject,void*);
120:   void                 *optionctx[PETSC_MAX_OPTIONS_HANDLER];
121:   PetscBool            optionsprinted;
122: #if defined(PETSC_HAVE_SAWS)
123:   PetscBool            amsmem;          /* if PETSC_TRUE then this object is registered with SAWs and visible to clients */
124:   PetscBool            amspublishblock; /* if PETSC_TRUE and publishing objects then will block at PetscObjectSAWsBlock() */
125: #endif
126:   PetscOptions         options;         /* options database used, NULL means default */
127:   PetscBool            donotPetscObjectPrintClassNamePrefixType;
128: } _p_PetscObject;

130: #define PETSCHEADER(ObjectOps) \
131:   _p_PetscObject hdr;          \
132:   ObjectOps      ops[1]

134: #define  PETSCFREEDHEADER -1

136: PETSC_EXTERN_TYPEDEF typedef PetscErrorCode (*PetscObjectDestroyFunction)(PetscObject*); /* force cast in next macro to NEVER use extern "C" style */
137: PETSC_EXTERN_TYPEDEF typedef PetscErrorCode (*PetscObjectViewFunction)(PetscObject,PetscViewer);

139: /*@C
140:     PetscHeaderCreate - Creates a PETSc object of a particular class

142:     Input Parameters:
143: +   classid - the classid associated with this object (for example VEC_CLASSID)
144: .   class_name - string name of class; should be static (for example "Vec")
145: .   descr - string containing short description; should be static (for example "Vector")
146: .   mansec - string indicating section in manual pages; should be static (for example "Vec")
147: .   comm - the MPI Communicator
148: .   destroy - the destroy routine for this object (for example VecDestroy())
149: -   view - the view routine for this object (for example VecView())

151:     Output Parameter:
152: .   h - the newly created object

154:     Level: developer

156: .seealso: PetscHeaderDestroy(), PetscClassIdRegister()

158: @*/
159: #define PetscHeaderCreate(h,classid,class_name,descr,mansec,comm,destroy,view) \
160:   (PetscNew(&(h)) || \
161:    PetscHeaderCreate_Private((PetscObject)(h),classid,class_name,descr,mansec,comm,(PetscObjectDestroyFunction)(destroy),(PetscObjectViewFunction)(view)) || \
162:    PetscLogObjectCreate(h) || \
163:    PetscLogObjectMemory((PetscObject)(h),sizeof(*(h))))

165: PETSC_EXTERN PetscErrorCode PetscComposedQuantitiesDestroy(PetscObject obj);
166: PETSC_EXTERN PetscErrorCode PetscHeaderCreate_Private(PetscObject,PetscClassId,const char[],const char[],const char[],MPI_Comm,PetscObjectDestroyFunction,PetscObjectViewFunction);

168: /*@C
169:     PetscHeaderDestroy - Final step in destroying a PetscObject

171:     Input Parameters:
172: .   h - the header created with PetscHeaderCreate()

174:     Level: developer

176: .seealso: PetscHeaderCreate()
177: @*/
178: #define PetscHeaderDestroy(h) (PetscHeaderDestroy_Private((PetscObject)(*(h))) || PetscFree(*(h)))

180: PETSC_EXTERN PetscErrorCode PetscHeaderDestroy_Private(PetscObject);
181: PETSC_EXTERN PetscErrorCode PetscObjectCopyFortranFunctionPointers(PetscObject,PetscObject);
182: PETSC_EXTERN PetscErrorCode PetscObjectSetFortranCallback(PetscObject,PetscFortranCallbackType,PetscFortranCallbackId*,void(*)(void),void *ctx);
183: PETSC_EXTERN PetscErrorCode PetscObjectGetFortranCallback(PetscObject,PetscFortranCallbackType,PetscFortranCallbackId,void(**)(void),void **ctx);

185: PETSC_INTERN PetscErrorCode PetscCitationsInitialize(void);
186: PETSC_INTERN PetscErrorCode PetscFreeMPIResources(void);




192: /*
193:     Macros to test if a PETSc object is valid and if pointers are valid
194: */
195: #if !defined(PETSC_USE_DEBUG)


208: #else

210: /*  This check is for subtype methods such as DMDAGetCorners() that do not use the PetscTryMethod() or PetscUseMethod() paradigm */
212:   do {   \
213:     PetscErrorCode _7_ierr; \
214:     PetscBool      _7_same; \
216:     _7_PetscObjectTypeCompare((PetscObject)(h),t,&_7_same);CHKERRQ(_7_ierr); \
217:     if (!_7_same) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Wrong subtype object:Parameter # %d must have implementation %s it is %s",arg,t,((PetscObject)(h))->type_name); \
218:   } while (0)

221:   do {                                                                  \
222:     if (!(h)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Null Object: Parameter # %d",arg); \
224:     if (((PetscObject)(h))->classid != ck) {                            \
225:       if (((PetscObject)(h))->classid == PETSCFREEDHEADER) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Object already free: Parameter # %d",arg); \
226:       else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Wrong type of object: Parameter # %d",arg); \
227:     }                                                                   \
228:   } while (0)

231:   do {                                                                  \
232:     if (!(h)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Null Object: Parameter # %d",arg); \
234:     if (((PetscObject)(h))->classid == PETSCFREEDHEADER) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Object already free: Parameter # %d",arg); \
235:     else if (((PetscObject)(h))->classid < PETSC_SMALLEST_CLASSID || ((PetscObject)(h))->classid > PETSC_LARGEST_CLASSID) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Invalid type of object: Parameter # %d",arg); \
236:   } while (0)

239:   do {                                                                  \
240:     if (!(h)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Null Pointer: Parameter # %d",arg); \
242:   } while (0)

245:   do {                                                                  \
246:     if (!(h)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Null Pointer: Parameter # %d",arg);\
248:   } while (0)

251:   do {                                                                  \
252:     if (!(h)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Null Pointer: Parameter # %d",arg); \
254:   } while (0)

257:   do {                                                                  \
258:     if (!(h)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Null Pointer: Parameter # %d",arg); \
260:   } while (0)

263:   do {                                                                  \
264:     if (!(h)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Null Pointer: Parameter # %d",arg); \
266:   } while (0)

269:   do {                                                                  \
270:     if (!(h)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Null Pointer: Parameter # %d",arg); \
272:   } while (0)

275:   do {                                                                  \
276:     if (!(f)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Null Function Pointer: Parameter # %d",arg); \
277:   } while (0)

279: #endif

281: #define PetscSorted(n,idx,sorted)           \
282:   do {                                      \
283:     PetscInt _i_;                           \
284:     (sorted) = PETSC_TRUE;                  \
285:     for (_i_ = 1; _i_ < (n); _i_++)         \
286:       if ((idx)[_i_] < (idx)[_i_ - 1])      \
287:         { (sorted) = PETSC_FALSE; break; }  \
288:   } while(0)

290: #if !defined(PETSC_USE_DEBUG)


306: #else

308: /*
309:     For example, in the dot product between two vectors,
310:   both vectors must be either Seq or MPI, not one of each
311: */
313:   do {                                                                  \
314:     if (((PetscObject)(a))->type != ((PetscObject)(b))->type) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMETYPE,"Objects not of same type: Argument # %d and %d",arga,argb); \
315:   } while (0)
316: /*
317:     Check type_name
318: */
320:   do {                                                                  \
321:     PetscBool      _7_match;                                            \
322:     PetscErrorCode _7_ierr;                                             \
323:     _7_PetscObjectTypeCompare(((PetscObject)(a)),(type),&_7_match);CHKERRQ(_7_ierr); \
324:     if (!_7_match) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Object (%s) is not %s",(char*)(((PetscObject)(a))->type_name),type); \
325:   } while (0)

328:   do {                                                                  \
329:     PetscBool      _7_match;                                            \
330:     PetscErrorCode _7_ierr;                                             \
331:     _7_PetscObjectTypeCompareAny(((PetscObject)(a)),&_7_match,(type1),(type2),"");CHKERRQ(_7_ierr); \
332:     if (!_7_match) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Object (%s) is not %s or %s",(char*)(((PetscObject)(a))->type_name),type1,type2); \
333:   } while (0)
334: /*
335:    Use this macro to check if the type is set
336: */
338:   do {                                                                  \
339:     if (!((PetscObject)(a))->type_name) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"%s object's type is not set: Argument # %d",((PetscObject)(a))->class_name,arg); \
340:   } while (0)
341: /*
342:    Sometimes object must live on same communicator to inter-operate
343: */
345:   do {                                                                  \
346:     PetscErrorCode _7_ierr;                                             \
347:     PetscMPIInt    _7_flag;                                             \
348:     _7_MPI_Comm_compare(PetscObjectComm((PetscObject)(a)),PetscObjectComm((PetscObject)(b)),&_7_flag);CHKERRQ(_7_ierr); \
349:     if (_7_flag != MPI_CONGRUENT && _7_flag != MPI_IDENT) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"Different communicators in the two objects: Argument # %d and %d flag %d",arga,argb,_7_flag); \
350:   } while (0)

353:   do {                                                  \
356:   } while (0)

359:   do {                                                                  \
360:     PetscErrorCode _7_ierr;                                             \
361:     PetscScalar b0=(b);                                                 \
362:     PetscReal b1[5],b2[5];                                              \
363:     if (PetscIsNanScalar(b0)) {b1[4] = 1;} else {b1[4] = 0;};           \
364:     b1[0] = -PetscRealPart(b0); b1[1] = PetscRealPart(b0); b1[2] = -PetscImaginaryPart(b0); b1[3] = PetscImaginaryPart(b0); \
365:     _7_MPI_Allreduce(b1,b2,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)(a)));CHKERRQ(_7_ierr); \
366:     if (!(b2[4] > 0) && !(PetscEqualReal(-b2[0],b2[1]) && PetscEqualReal(-b2[2],b2[3]))) SETERRQ1(PetscObjectComm((PetscObject)(a)),PETSC_ERR_ARG_WRONG,"Scalar value must be same on all processes, argument # %d",arg); \
367:   } while (0)

370:   do {                                                                  \
371:     PetscErrorCode _7_ierr;                                             \
372:     PetscReal b0=(b),b1[3],b2[3];                                       \
373:     if (PetscIsNanReal(b0)) {b1[2] = 1;} else {b1[2] = 0;};             \
374:     b1[0] = -b0; b1[1] = b0;                                            \
375:     _7_MPI_Allreduce(b1,b2,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)(a)));CHKERRQ(_7_ierr); \
376:     if (!(b2[2] > 0) && !PetscEqualReal(-b2[0],b2[1])) SETERRQ1(PetscObjectComm((PetscObject)(a)),PETSC_ERR_ARG_WRONG,"Real value must be same on all processes, argument # %d",arg); \
377:   } while (0)

380:   do {                                                                  \
381:     PetscErrorCode _7_ierr;                                             \
382:     PetscInt b0=(b),b1[2],b2[2];                                        \
383:     b1[0] = -b0; b1[1] = b0;                                            \
384:     _7_MPIU_Allreduce(b1,b2,2,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)(a)));CHKERRQ(_7_ierr); \
385:     if (-b2[0] != b2[1]) SETERRQ1(PetscObjectComm((PetscObject)(a)),PETSC_ERR_ARG_WRONG,"Int value must be same on all processes, argument # %d",arg); \
386:   } while (0)

389:   do {                                                                  \
390:     PetscErrorCode _7_ierr;                                             \
391:     PetscMPIInt b0=(b),b1[2],b2[2];                                     \
392:     b1[0] = -b0; b1[1] = b0;                                            \
393:     _7_MPIU_Allreduce(b1,b2,2,MPI_INT,MPI_MAX,PetscObjectComm((PetscObject)(a)));CHKERRQ(_7_ierr); \
394:     if (-b2[0] != b2[1]) SETERRQ1(PetscObjectComm((PetscObject)(a)),PETSC_ERR_ARG_WRONG,"PetscMPIInt value must be same on all processes, argument # %d",arg); \
395:   } while (0)

398:   do {                                                                  \
399:     PetscErrorCode _7_ierr;                                             \
400:     PetscMPIInt b0=(PetscMPIInt)(b),b1[2],b2[2];                        \
401:     b1[0] = -b0; b1[1] = b0;                                            \
402:     _7_MPIU_Allreduce(b1,b2,2,MPI_INT,MPI_MAX,PetscObjectComm((PetscObject)(a)));CHKERRQ(_7_ierr); \
403:     if (-b2[0] != b2[1]) SETERRQ1(PetscObjectComm((PetscObject)(a)),PETSC_ERR_ARG_WRONG,"Bool value must be same on all processes, argument # %d",arg); \
404:   } while (0)

407:   do {                                                                  \
408:     PetscErrorCode _7_ierr;                                             \
409:     PetscMPIInt b0=(PetscMPIInt)(b),b1[2],b2[2];                        \
410:     b1[0] = -b0; b1[1] = b0;                                            \
411:     _7_MPIU_Allreduce(b1,b2,2,MPI_INT,MPI_MAX,PetscObjectComm((PetscObject)(a)));CHKERRQ(_7_ierr); \
412:     if (-b2[0] != b2[1]) SETERRQ1(PetscObjectComm((PetscObject)(a)),PETSC_ERR_ARG_WRONG,"Enum value must be same on all processes, argument # %d",arg); \
413:   } while (0)

416:   do {                                                                                            \
417:     PetscBool _1_flg;                                                                             \
418:     PetscSorted(n,idx,_1_flg);                                                                    \
419:     if (!_1_flg) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input array needs to be sorted"); \
420:   } while (0)

422: #endif

424: /*
425:    PetscTryMethod - Queries an object for a method, if it exists then calls it.
426:               These are intended to be used only inside PETSc functions.

428:    Level: developer

430: .seealso: PetscUseMethod()
431: */
432: #define  PetscTryMethod(obj,A,B,C) \
433:   0; do { PetscErrorCode (*_7_f)B, _7_ierr; \
434:     _7_PetscObjectQueryFunction((PetscObject)(obj),A,&_7_f);CHKERRQ(_7_ierr); \
435:     if (_7_f) {_7_(*_7_f)C;CHKERRQ(_7_ierr);} \
436:   } while(0)

438: /*
439:    PetscUseMethod - Queries an object for a method, if it exists then calls it, otherwise generates an error.
440:               These are intended to be used only inside PETSc functions.

442:    Level: developer

444: .seealso: PetscTryMethod()
445: */
446: #define  PetscUseMethod(obj,A,B,C) \
447:   0; do { PetscErrorCode (*_7_f)B, _7_ierr; \
448:     _7_PetscObjectQueryFunction((PetscObject)(obj),A,&_7_f);CHKERRQ(_7_ierr); \
449:     if (_7_f) {_7_(*_7_f)C;CHKERRQ(_7_ierr);} \
450:     else SETERRQ1(PetscObjectComm((PetscObject)(obj)),PETSC_ERR_SUP,"Cannot locate function %s in object",A); \
451:   } while(0)

453: /*MC
454:    PetscObjectStateIncrease - Increases the state of any PetscObject

456:    Synopsis:
457:    #include "petsc/private/petscimpl.h"
458:    PetscErrorCode PetscObjectStateIncrease(PetscObject obj)

460:    Logically Collective

462:    Input Parameter:
463: .  obj - any PETSc object, for example a Vec, Mat or KSP. This must be
464:          cast with a (PetscObject), for example,
465:          PetscObjectStateIncrease((PetscObject)mat);

467:    Notes:
468:     object state is an integer which gets increased every time
469:    the object is changed internally. By saving and later querying the object state
470:    one can determine whether information about the object is still current.
471:    Currently, state is maintained for Vec and Mat objects.

473:    This routine is mostly for internal use by PETSc; a developer need only
474:    call it after explicit access to an object's internals. Routines such
475:    as VecSet() or MatScale() already call this routine. It is also called, as a
476:    precaution, in VecRestoreArray(), MatRestoreRow(), MatDenseRestoreArray().

478:    This routine is logically collective because state equality comparison needs to be possible without communication.

480:    Level: developer

482:    seealso: PetscObjectStateGet()

484: M*/
485: #define PetscObjectStateIncrease(obj) ((obj)->state++,0)

487: PETSC_EXTERN PetscErrorCode PetscObjectStateGet(PetscObject,PetscObjectState*);
488: PETSC_EXTERN PetscErrorCode PetscObjectStateSet(PetscObject,PetscObjectState);
489: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataRegister(PetscInt*);
490: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseInt(PetscObject);
491: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseIntstar(PetscObject);
492: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseReal(PetscObject);
493: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseRealstar(PetscObject);
494: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseScalar(PetscObject);
495: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseScalarstar(PetscObject);
496: PETSC_EXTERN PetscInt       PetscObjectComposedDataMax;
497: /*MC
498:    PetscObjectComposedDataSetInt - attach integer data to a PetscObject

500:    Synopsis:
501:    #include "petsc/private/petscimpl.h"
502:    PetscErrorCode PetscObjectComposedDataSetInt(PetscObject obj,int id,int data)

504:    Not collective

506:    Input parameters:
507: +  obj - the object to which data is to be attached
508: .  id - the identifier for the data
509: -  data - the data to  be attached

511:    Notes
512:    The data identifier can best be created through a call to  PetscObjectComposedDataRegister()

514:    Level: developer
515: M*/
516: #define PetscObjectComposedDataSetInt(obj,id,data)                                      \
517:   ((((obj)->int_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseInt(obj)) ||  \
518:    ((obj)->intcomposeddata[id] = data,(obj)->intcomposedstate[id] = (obj)->state, 0))

520: /*MC
521:    PetscObjectComposedDataGetInt - retrieve integer data attached to an object

523:    Synopsis:
524:    #include "petsc/private/petscimpl.h"
525:    PetscErrorCode PetscObjectComposedDataGetInt(PetscObject obj,int id,int data,PetscBool  flag)

527:    Not collective

529:    Input parameters:
530: +  obj - the object from which data is to be retrieved
531: -  id - the identifier for the data

533:    Output parameters
534: +  data - the data to be retrieved
535: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

537:    The 'data' and 'flag' variables are inlined, so they are not pointers.

539:    Level: developer
540: M*/
541: #define PetscObjectComposedDataGetInt(obj,id,data,flag)                            \
542:   ((((obj)->intcomposedstate && ((obj)->intcomposedstate[id] == (obj)->state)) ?   \
543:    (data = (obj)->intcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

545: /*MC
546:    PetscObjectComposedDataSetIntstar - attach integer array data to a PetscObject

548:    Synopsis:
549:    #include "petsc/private/petscimpl.h"
550:    PetscErrorCode PetscObjectComposedDataSetIntstar(PetscObject obj,int id,int *data)

552:    Not collective

554:    Input parameters:
555: +  obj - the object to which data is to be attached
556: .  id - the identifier for the data
557: -  data - the data to  be attached

559:    Notes
560:    The data identifier can best be determined through a call to
561:    PetscObjectComposedDataRegister()

563:    Level: developer
564: M*/
565: #define PetscObjectComposedDataSetIntstar(obj,id,data)                                          \
566:   ((((obj)->intstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseIntstar(obj)) ||  \
567:    ((obj)->intstarcomposeddata[id] = data,(obj)->intstarcomposedstate[id] = (obj)->state, 0))

569: /*MC
570:    PetscObjectComposedDataGetIntstar - retrieve integer array data
571:    attached to an object

573:    Synopsis:
574:    #include "petsc/private/petscimpl.h"
575:    PetscErrorCode PetscObjectComposedDataGetIntstar(PetscObject obj,int id,int *data,PetscBool  flag)

577:    Not collective

579:    Input parameters:
580: +  obj - the object from which data is to be retrieved
581: -  id - the identifier for the data

583:    Output parameters
584: +  data - the data to be retrieved
585: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

587:    The 'data' and 'flag' variables are inlined, so they are not pointers.

589:    Level: developer
590: M*/
591: #define PetscObjectComposedDataGetIntstar(obj,id,data,flag)                               \
592:   ((((obj)->intstarcomposedstate && ((obj)->intstarcomposedstate[id] == (obj)->state)) ?  \
593:    (data = (obj)->intstarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

595: /*MC
596:    PetscObjectComposedDataSetReal - attach real data to a PetscObject

598:    Synopsis:
599:    #include "petsc/private/petscimpl.h"
600:    PetscErrorCode PetscObjectComposedDataSetReal(PetscObject obj,int id,PetscReal data)

602:    Not collective

604:    Input parameters:
605: +  obj - the object to which data is to be attached
606: .  id - the identifier for the data
607: -  data - the data to  be attached

609:    Notes
610:    The data identifier can best be determined through a call to
611:    PetscObjectComposedDataRegister()

613:    Level: developer
614: M*/
615: #define PetscObjectComposedDataSetReal(obj,id,data)                                       \
616:   ((((obj)->real_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseReal(obj)) ||  \
617:    ((obj)->realcomposeddata[id] = data,(obj)->realcomposedstate[id] = (obj)->state, 0))

619: /*MC
620:    PetscObjectComposedDataGetReal - retrieve real data attached to an object

622:    Synopsis:
623:    #include "petsc/private/petscimpl.h"
624:    PetscErrorCode PetscObjectComposedDataGetReal(PetscObject obj,int id,PetscReal data,PetscBool  flag)

626:    Not collective

628:    Input parameters:
629: +  obj - the object from which data is to be retrieved
630: -  id - the identifier for the data

632:    Output parameters
633: +  data - the data to be retrieved
634: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

636:    The 'data' and 'flag' variables are inlined, so they are not pointers.

638:    Level: developer
639: M*/
640: #define PetscObjectComposedDataGetReal(obj,id,data,flag)                            \
641:   ((((obj)->realcomposedstate && ((obj)->realcomposedstate[id] == (obj)->state)) ?  \
642:    (data = (obj)->realcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

644: /*MC
645:    PetscObjectComposedDataSetRealstar - attach real array data to a PetscObject

647:    Synopsis:
648:    #include "petsc/private/petscimpl.h"
649:    PetscErrorCode PetscObjectComposedDataSetRealstar(PetscObject obj,int id,PetscReal *data)

651:    Not collective

653:    Input parameters:
654: +  obj - the object to which data is to be attached
655: .  id - the identifier for the data
656: -  data - the data to  be attached

658:    Notes
659:    The data identifier can best be determined through a call to
660:    PetscObjectComposedDataRegister()

662:    Level: developer
663: M*/
664: #define PetscObjectComposedDataSetRealstar(obj,id,data)                                           \
665:   ((((obj)->realstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseRealstar(obj)) ||  \
666:    ((obj)->realstarcomposeddata[id] = data, (obj)->realstarcomposedstate[id] = (obj)->state, 0))

668: /*MC
669:    PetscObjectComposedDataGetRealstar - retrieve real array data
670:    attached to an object

672:    Synopsis:
673:    #include "petsc/private/petscimpl.h"
674:    PetscErrorCode PetscObjectComposedDataGetRealstar(PetscObject obj,int id,PetscReal *data,PetscBool  flag)

676:    Not collective

678:    Input parameters:
679: +  obj - the object from which data is to be retrieved
680: -  id - the identifier for the data

682:    Output parameters
683: +  data - the data to be retrieved
684: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

686:    The 'data' and 'flag' variables are inlined, so they are not pointers.

688:    Level: developer
689: M*/
690: #define PetscObjectComposedDataGetRealstar(obj,id,data,flag)                                \
691:   ((((obj)->realstarcomposedstate && ((obj)->realstarcomposedstate[id] == (obj)->state)) ?  \
692:    (data = (obj)->realstarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

694: /*MC
695:    PetscObjectComposedDataSetScalar - attach scalar data to a PetscObject

697:    Synopsis:
698:    #include "petsc/private/petscimpl.h"
699:    PetscErrorCode PetscObjectComposedDataSetScalar(PetscObject obj,int id,PetscScalar data)

701:    Not collective

703:    Input parameters:
704: +  obj - the object to which data is to be attached
705: .  id - the identifier for the data
706: -  data - the data to  be attached

708:    Notes
709:    The data identifier can best be determined through a call to
710:    PetscObjectComposedDataRegister()

712:    Level: developer
713: M*/
714: #if defined(PETSC_USE_COMPLEX)
715: #define PetscObjectComposedDataSetScalar(obj,id,data)                                        \
716:   ((((obj)->scalar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseScalar(obj)) || \
717:    ((obj)->scalarcomposeddata[id] = data,(obj)->scalarcomposedstate[id] = (obj)->state, 0))
718: #else
719: #define PetscObjectComposedDataSetScalar(obj,id,data) \
720:         PetscObjectComposedDataSetReal(obj,id,data)
721: #endif
722: /*MC
723:    PetscObjectComposedDataGetScalar - retrieve scalar data attached to an object

725:    Synopsis:
726:    #include "petsc/private/petscimpl.h"
727:    PetscErrorCode PetscObjectComposedDataGetScalar(PetscObject obj,int id,PetscScalar data,PetscBool  flag)

729:    Not collective

731:    Input parameters:
732: +  obj - the object from which data is to be retrieved
733: -  id - the identifier for the data

735:    Output parameters
736: +  data - the data to be retrieved
737: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

739:    The 'data' and 'flag' variables are inlined, so they are not pointers.

741:    Level: developer
742: M*/
743: #if defined(PETSC_USE_COMPLEX)
744: #define PetscObjectComposedDataGetScalar(obj,id,data,flag)                              \
745:   ((((obj)->scalarcomposedstate && ((obj)->scalarcomposedstate[id] == (obj)->state) ) ? \
746:    (data = (obj)->scalarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)
747: #else
748: #define PetscObjectComposedDataGetScalar(obj,id,data,flag)                             \
749:         PetscObjectComposedDataGetReal(obj,id,data,flag)
750: #endif

752: /*MC
753:    PetscObjectComposedDataSetScalarstar - attach scalar array data to a PetscObject

755:    Synopsis:
756:    #include "petsc/private/petscimpl.h"
757:    PetscErrorCode PetscObjectComposedDataSetScalarstar(PetscObject obj,int id,PetscScalar *data)

759:    Not collective

761:    Input parameters:
762: +  obj - the object to which data is to be attached
763: .  id - the identifier for the data
764: -  data - the data to  be attached

766:    Notes
767:    The data identifier can best be determined through a call to
768:    PetscObjectComposedDataRegister()

770:    Level: developer
771: M*/
772: #if defined(PETSC_USE_COMPLEX)
773: #define PetscObjectComposedDataSetScalarstar(obj,id,data)                                             \
774:   ((((obj)->scalarstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseScalarstar(obj)) ||  \
775:    ((obj)->scalarstarcomposeddata[id] = data,(obj)->scalarstarcomposedstate[id] = (obj)->state, 0))
776: #else
777: #define PetscObjectComposedDataSetScalarstar(obj,id,data) \
778:         PetscObjectComposedDataSetRealstar(obj,id,data)
779: #endif
780: /*MC
781:    PetscObjectComposedDataGetScalarstar - retrieve scalar array data
782:    attached to an object

784:    Synopsis:
785:    #include "petsc/private/petscimpl.h"
786:    PetscErrorCode PetscObjectComposedDataGetScalarstar(PetscObject obj,int id,PetscScalar *data,PetscBool  flag)

788:    Not collective

790:    Input parameters:
791: +  obj - the object from which data is to be retrieved
792: -  id - the identifier for the data

794:    Output parameters
795: +  data - the data to be retrieved
796: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

798:    The 'data' and 'flag' variables are inlined, so they are not pointers.

800:    Level: developer
801: M*/
802: #if defined(PETSC_USE_COMPLEX)
803: #define PetscObjectComposedDataGetScalarstar(obj,id,data,flag)                                 \
804:   ((((obj)->scalarstarcomposedstate && ((obj)->scalarstarcomposedstate[id] == (obj)->state)) ? \
805:        (data = (obj)->scalarstarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)
806: #else
807: #define PetscObjectComposedDataGetScalarstar(obj,id,data,flag)         \
808:         PetscObjectComposedDataGetRealstar(obj,id,data,flag)
809: #endif

811: PETSC_EXTERN PetscMPIInt Petsc_Counter_keyval;
812: PETSC_EXTERN PetscMPIInt Petsc_InnerComm_keyval;
813: PETSC_EXTERN PetscMPIInt Petsc_OuterComm_keyval;
814: PETSC_EXTERN PetscMPIInt Petsc_Seq_keyval;
815: PETSC_EXTERN PetscMPIInt Petsc_ShmComm_keyval;

817: /*
818:   PETSc communicators have this attribute, see
819:   PetscCommDuplicate(), PetscCommDestroy(), PetscCommGetNewTag(), PetscObjectGetName()
820: */
821: typedef struct {
822:   PetscMPIInt tag;              /* next free tag value */
823:   PetscInt    refcount;         /* number of references, communicator can be freed when this reaches 0 */
824:   PetscInt    namecount;        /* used to generate the next name, as in Vec_0, Mat_1, ... */
825: } PetscCommCounter;

827: /*E
828:     PetscOffloadMask - indicates which memory (CPU, GPU, or none) contains valid data

830:    PETSC_OFFLOAD_UNALLOCATED  - no memory contains valid matrix entries; NEVER used for vectors
831:    PETSC_OFFLOAD_GPU - GPU has valid vector/matrix entries
832:    PETSC_OFFLOAD_CPU - CPU has valid vector/matrix entries
833:    PETSC_OFFLOAD_BOTH - Both GPU and CPU have valid vector/matrix entries and they match

835:    Level: developer
836: E*/
837: typedef enum {PETSC_OFFLOAD_UNALLOCATED=0x0,PETSC_OFFLOAD_CPU=0x1,PETSC_OFFLOAD_GPU=0x2,PETSC_OFFLOAD_BOTH=0x3} PetscOffloadMask;

839: typedef enum {STATE_BEGIN, STATE_PENDING, STATE_END} SRState;

841: typedef enum {PETSC_SR_REDUCE_SUM=0,PETSC_SR_REDUCE_MAX=1,PETSC_SR_REDUCE_MIN=2} PetscSRReductionType;

843: typedef struct {
844:   MPI_Comm    comm;
845:   MPI_Request request;
846:   PetscBool   async;
847:   PetscScalar *lvalues;     /* this are the reduced values before call to MPI_Allreduce() */
848:   PetscScalar *gvalues;     /* values after call to MPI_Allreduce() */
849:   void        **invecs;     /* for debugging only, vector/memory used with each op */
850:   PetscInt    *reducetype;  /* is particular value to be summed or maxed? */
851:   SRState     state;        /* are we calling xxxBegin() or xxxEnd()? */
852:   PetscInt    maxops;       /* total amount of space we have for requests */
853:   PetscInt    numopsbegin;  /* number of requests that have been queued in */
854:   PetscInt    numopsend;    /* number of requests that have been gotten by user */
855: } PetscSplitReduction;

857: PETSC_EXTERN PetscErrorCode PetscSplitReductionGet(MPI_Comm,PetscSplitReduction**);
858: PETSC_EXTERN PetscErrorCode PetscSplitReductionEnd(PetscSplitReduction*);
859: PETSC_EXTERN PetscErrorCode PetscSplitReductionExtend(PetscSplitReduction*);

861: #if !defined(PETSC_SKIP_SPINLOCK)
862: #if defined(PETSC_HAVE_THREADSAFETY)
863: #  if defined(PETSC_HAVE_CONCURRENCYKIT)
864: #if defined(__cplusplus)
865: /*  CK does not have extern "C" protection in their include files */
866: extern "C" {
867: #endif
868: #include <ck_spinlock.h>
869: #if defined(__cplusplus)
870: }
871: #endif
872: typedef ck_spinlock_t PetscSpinlock;
873: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockCreate(PetscSpinlock *ck_spinlock)
874: {
875:   ck_spinlock_init(ck_spinlock);
876:   return 0;
877: }
878: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockLock(PetscSpinlock *ck_spinlock)
879: {
880:   ck_spinlock_lock(ck_spinlock);
881:   return 0;
882: }
883: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockUnlock(PetscSpinlock *ck_spinlock)
884: {
885:   ck_spinlock_unlock(ck_spinlock);
886:   return 0;
887: }
888: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockDestroy(PetscSpinlock *ck_spinlock)
889: {
890:   return 0;
891: }
892: #  elif defined(PETSC_HAVE_OPENMP)

894: #include <omp.h>
895: typedef omp_lock_t PetscSpinlock;
896: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockCreate(PetscSpinlock *omp_lock)
897: {
898:   omp_init_lock(omp_lock);
899:   return 0;
900: }
901: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockLock(PetscSpinlock *omp_lock)
902: {
903:   omp_set_lock(omp_lock);
904:   return 0;
905: }
906: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockUnlock(PetscSpinlock *omp_lock)
907: {
908:   omp_unset_lock(omp_lock);
909:   return 0;
910: }
911: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockDestroy(PetscSpinlock *omp_lock)
912: {
913:   omp_destroy_lock(omp_lock);
914:   return 0;
915: }
916: #else
917: Thread safety requires either --with-openmp or --download-concurrencykit
918: #endif

920: #else
921: typedef int PetscSpinlock;
922: #define PetscSpinlockCreate(a)  0
923: #define PetscSpinlockLock(a)    0
924: #define PetscSpinlockUnlock(a)  0
925: #define PetscSpinlockDestroy(a) 0
926: #endif

928: #if defined(PETSC_HAVE_THREADSAFETY)
929: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockOpen;
930: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockStdout;
931: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockStderr;
932: PETSC_INTERN PetscSpinlock PetscCommSpinLock;
933: #endif
934: #endif

936: PETSC_EXTERN PetscLogEvent PETSC_Barrier;
937: PETSC_EXTERN PetscLogEvent PETSC_BuildTwoSided;
938: PETSC_EXTERN PetscLogEvent PETSC_BuildTwoSidedF;

940: #if defined(PETSC_HAVE_ADIOS)
941: PETSC_EXTERN int64_t Petsc_adios_group;
942: #endif

944: PETSC_EXTERN PetscBool use_gpu_aware_mpi;
945: #if defined(PETSC_HAVE_CUDA)
946: PETSC_EXTERN PetscBool sf_use_default_cuda_stream;
947: #endif

949: #endif /* PETSCIMPL_H */