Actual source code: petscimpl.h

petsc-master 2019-11-16
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++) if (idx[i] < idx[i - 1]) break; \
286:   if (i < n) sorted = PETSC_FALSE;                        \
287: } while(0)

289: #if !defined(PETSC_USE_DEBUG)


305: #else

307: /*
308:     For example, in the dot product between two vectors,
309:   both vectors must be either Seq or MPI, not one of each
310: */
312:   do {                                                                  \
313:     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); \
314:   } while (0)
315: /*
316:     Check type_name
317: */
319:   do {                                                                  \
320:     PetscBool      _7_match;                                             \
321:     PetscErrorCode _7_ierr;                                             \
322:     _7_PetscObjectTypeCompare(((PetscObject)a),(type),&_7_match);CHKERRQ(_7_ierr); \
323:     if (!_7_match) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Object (%s) is not %s",(char*)(((PetscObject)a)->type_name),type); \
324:   } while (0)

327:   do {                                                                  \
328:     PetscBool      _7_match;                                            \
329:     PetscErrorCode _7_ierr;                                             \
330:     _7_PetscObjectTypeCompareAny(((PetscObject)a),&_7_match,(type1),(type2),"");CHKERRQ(_7_ierr); \
331:     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); \
332:   } while (0)
333: /*
334:    Use this macro to check if the type is set
335: */
337:   do {                                                                  \
338:     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); \
339:   } while (0)
340: /*
341:    Sometimes object must live on same communicator to inter-operate
342: */
344:   do {                                                                  \
345:     PetscErrorCode _7_ierr;                                             \
346:     PetscMPIInt    _7_flag;                                             \
347:     _7_MPI_Comm_compare(PetscObjectComm((PetscObject)a),PetscObjectComm((PetscObject)b),&_7_flag);CHKERRQ(_7_ierr);                                                   \
348:     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); \
349:   } while (0)

352:   do {                                                  \
355:   } while (0)

358:   do {                                                                  \
359:     PetscErrorCode _7_ierr;                                             \
360:     PetscReal b1[5],b2[5];                                              \
361:     if (PetscIsNanScalar(b)) {b1[4] = 1;} else {b1[4] = 0;};            \
362:     b1[0] = -PetscRealPart(b); b1[1] = PetscRealPart(b); b1[2] = -PetscImaginaryPart(b); b1[3] = PetscImaginaryPart(b); \
363:     _7_MPI_Allreduce(b1,b2,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)a));CHKERRQ(_7_ierr); \
364:     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",c); \
365:   } while (0)

368:   do {                                                                  \
369:     PetscErrorCode _7_ierr;                                             \
370:     PetscReal b1[3],b2[3];                                              \
371:     if (PetscIsNanReal(b)) {b1[2] = 1;} else {b1[2] = 0;};              \
372:     b1[0] = -b; b1[1] = b;                                              \
373:     _7_MPI_Allreduce(b1,b2,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)a));CHKERRQ(_7_ierr); \
374:     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",c); \
375:   } while (0)

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


389:   do {                                                                  \
390:     PetscErrorCode _7_ierr;                                             \
391:     PetscMPIInt b1[2],b2[2];                                            \
392:     b1[0] = -(PetscMPIInt)b; b1[1] = (PetscMPIInt)b;                    \
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,"Bool value must be same on all processes, argument # %d",c); \
395:   } while (0)

398:   do {                                                                  \
399:     PetscErrorCode _7_ierr;                                             \
400:     PetscMPIInt b1[2],b2[2];                                            \
401:     b1[0] = -(PetscMPIInt)b; b1[1] = (PetscMPIInt)b;                    \
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,"Enum value must be same on all processes, argument # %d",c); \
404:   } while (0)

407:   do {                                                                                         \
408:     PetscBool flg;                                                                             \
409:     PetscSorted(n,idx,flg);                                                                    \
410:     if (!flg) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input array needs to be sorted"); \
411:   } while (0)

413: #endif

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

419:    Level: developer

421: .seealso: PetscUseMethod()
422: */
423: #define  PetscTryMethod(obj,A,B,C) \
424:   0; do { PetscErrorCode (*_7_f)B, _7_ierr; \
425:     _7_PetscObjectQueryFunction((PetscObject)(obj),A,&_7_f);CHKERRQ(_7_ierr); \
426:     if (_7_f) {_7_(*_7_f)C;CHKERRQ(_7_ierr);} \
427:   } while(0)

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

433:    Level: developer

435: .seealso: PetscTryMethod()
436: */
437: #define  PetscUseMethod(obj,A,B,C) \
438:   0; do { PetscErrorCode (*_7_f)B, _7_ierr; \
439:     _7_PetscObjectQueryFunction((PetscObject)(obj),A,&_7_f);CHKERRQ(_7_ierr); \
440:     if (_7_f) {_7_(*_7_f)C;CHKERRQ(_7_ierr);} \
441:     else SETERRQ1(PetscObjectComm((PetscObject)(obj)),PETSC_ERR_SUP,"Cannot locate function %s in object",A); \
442:   } while(0)

444: /*MC
445:    PetscObjectStateIncrease - Increases the state of any PetscObject

447:    Synopsis:
448:    #include "petsc/private/petscimpl.h"
449:    PetscErrorCode PetscObjectStateIncrease(PetscObject obj)

451:    Logically Collective

453:    Input Parameter:
454: .  obj - any PETSc object, for example a Vec, Mat or KSP. This must be
455:          cast with a (PetscObject), for example,
456:          PetscObjectStateIncrease((PetscObject)mat);

458:    Notes:
459:     object state is an integer which gets increased every time
460:    the object is changed internally. By saving and later querying the object state
461:    one can determine whether information about the object is still current.
462:    Currently, state is maintained for Vec and Mat objects.

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

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

471:    Level: developer

473:    seealso: PetscObjectStateGet()

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

478: PETSC_EXTERN PetscErrorCode PetscObjectStateGet(PetscObject,PetscObjectState*);
479: PETSC_EXTERN PetscErrorCode PetscObjectStateSet(PetscObject,PetscObjectState);
480: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataRegister(PetscInt*);
481: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseInt(PetscObject);
482: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseIntstar(PetscObject);
483: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseReal(PetscObject);
484: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseRealstar(PetscObject);
485: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseScalar(PetscObject);
486: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseScalarstar(PetscObject);
487: PETSC_EXTERN PetscInt       PetscObjectComposedDataMax;
488: /*MC
489:    PetscObjectComposedDataSetInt - attach integer data to a PetscObject

491:    Synopsis:
492:    #include "petsc/private/petscimpl.h"
493:    PetscErrorCode PetscObjectComposedDataSetInt(PetscObject obj,int id,int data)

495:    Not collective

497:    Input parameters:
498: +  obj - the object to which data is to be attached
499: .  id - the identifier for the data
500: -  data - the data to  be attached

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

505:    Level: developer
506: M*/
507: #define PetscObjectComposedDataSetInt(obj,id,data)                                      \
508:   ((((obj)->int_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseInt(obj)) ||  \
509:    ((obj)->intcomposeddata[id] = data,(obj)->intcomposedstate[id] = (obj)->state, 0))

511: /*MC
512:    PetscObjectComposedDataGetInt - retrieve integer data attached to an object

514:    Synopsis:
515:    #include "petsc/private/petscimpl.h"
516:    PetscErrorCode PetscObjectComposedDataGetInt(PetscObject obj,int id,int data,PetscBool  flag)

518:    Not collective

520:    Input parameters:
521: +  obj - the object from which data is to be retrieved
522: -  id - the identifier for the data

524:    Output parameters
525: +  data - the data to be retrieved
526: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

530:    Level: developer
531: M*/
532: #define PetscObjectComposedDataGetInt(obj,id,data,flag)                            \
533:   ((((obj)->intcomposedstate && ((obj)->intcomposedstate[id] == (obj)->state)) ?   \
534:    (data = (obj)->intcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

536: /*MC
537:    PetscObjectComposedDataSetIntstar - attach integer array data to a PetscObject

539:    Synopsis:
540:    #include "petsc/private/petscimpl.h"
541:    PetscErrorCode PetscObjectComposedDataSetIntstar(PetscObject obj,int id,int *data)

543:    Not collective

545:    Input parameters:
546: +  obj - the object to which data is to be attached
547: .  id - the identifier for the data
548: -  data - the data to  be attached

550:    Notes
551:    The data identifier can best be determined through a call to
552:    PetscObjectComposedDataRegister()

554:    Level: developer
555: M*/
556: #define PetscObjectComposedDataSetIntstar(obj,id,data)                                          \
557:   ((((obj)->intstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseIntstar(obj)) ||  \
558:    ((obj)->intstarcomposeddata[id] = data,(obj)->intstarcomposedstate[id] = (obj)->state, 0))

560: /*MC
561:    PetscObjectComposedDataGetIntstar - retrieve integer array data
562:    attached to an object

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

568:    Not collective

570:    Input parameters:
571: +  obj - the object from which data is to be retrieved
572: -  id - the identifier for the data

574:    Output parameters
575: +  data - the data to be retrieved
576: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

580:    Level: developer
581: M*/
582: #define PetscObjectComposedDataGetIntstar(obj,id,data,flag)                               \
583:   ((((obj)->intstarcomposedstate && ((obj)->intstarcomposedstate[id] == (obj)->state)) ?  \
584:    (data = (obj)->intstarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

586: /*MC
587:    PetscObjectComposedDataSetReal - attach real data to a PetscObject

589:    Synopsis:
590:    #include "petsc/private/petscimpl.h"
591:    PetscErrorCode PetscObjectComposedDataSetReal(PetscObject obj,int id,PetscReal data)

593:    Not collective

595:    Input parameters:
596: +  obj - the object to which data is to be attached
597: .  id - the identifier for the data
598: -  data - the data to  be attached

600:    Notes
601:    The data identifier can best be determined through a call to
602:    PetscObjectComposedDataRegister()

604:    Level: developer
605: M*/
606: #define PetscObjectComposedDataSetReal(obj,id,data)                                       \
607:   ((((obj)->real_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseReal(obj)) ||  \
608:    ((obj)->realcomposeddata[id] = data,(obj)->realcomposedstate[id] = (obj)->state, 0))

610: /*MC
611:    PetscObjectComposedDataGetReal - retrieve real data attached to an object

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

617:    Not collective

619:    Input parameters:
620: +  obj - the object from which data is to be retrieved
621: -  id - the identifier for the data

623:    Output parameters
624: +  data - the data to be retrieved
625: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

629:    Level: developer
630: M*/
631: #define PetscObjectComposedDataGetReal(obj,id,data,flag)                            \
632:   ((((obj)->realcomposedstate && ((obj)->realcomposedstate[id] == (obj)->state)) ?  \
633:    (data = (obj)->realcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

635: /*MC
636:    PetscObjectComposedDataSetRealstar - attach real array data to a PetscObject

638:    Synopsis:
639:    #include "petsc/private/petscimpl.h"
640:    PetscErrorCode PetscObjectComposedDataSetRealstar(PetscObject obj,int id,PetscReal *data)

642:    Not collective

644:    Input parameters:
645: +  obj - the object to which data is to be attached
646: .  id - the identifier for the data
647: -  data - the data to  be attached

649:    Notes
650:    The data identifier can best be determined through a call to
651:    PetscObjectComposedDataRegister()

653:    Level: developer
654: M*/
655: #define PetscObjectComposedDataSetRealstar(obj,id,data)                                           \
656:   ((((obj)->realstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseRealstar(obj)) ||  \
657:    ((obj)->realstarcomposeddata[id] = data, (obj)->realstarcomposedstate[id] = (obj)->state, 0))

659: /*MC
660:    PetscObjectComposedDataGetRealstar - retrieve real array data
661:    attached to an object

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

667:    Not collective

669:    Input parameters:
670: +  obj - the object from which data is to be retrieved
671: -  id - the identifier for the data

673:    Output parameters
674: +  data - the data to be retrieved
675: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

679:    Level: developer
680: M*/
681: #define PetscObjectComposedDataGetRealstar(obj,id,data,flag)                                \
682:   ((((obj)->realstarcomposedstate && ((obj)->realstarcomposedstate[id] == (obj)->state)) ?  \
683:    (data = (obj)->realstarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

685: /*MC
686:    PetscObjectComposedDataSetScalar - attach scalar data to a PetscObject

688:    Synopsis:
689:    #include "petsc/private/petscimpl.h"
690:    PetscErrorCode PetscObjectComposedDataSetScalar(PetscObject obj,int id,PetscScalar data)

692:    Not collective

694:    Input parameters:
695: +  obj - the object to which data is to be attached
696: .  id - the identifier for the data
697: -  data - the data to  be attached

699:    Notes
700:    The data identifier can best be determined through a call to
701:    PetscObjectComposedDataRegister()

703:    Level: developer
704: M*/
705: #if defined(PETSC_USE_COMPLEX)
706: #define PetscObjectComposedDataSetScalar(obj,id,data)                                        \
707:   ((((obj)->scalar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseScalar(obj)) || \
708:    ((obj)->scalarcomposeddata[id] = data,(obj)->scalarcomposedstate[id] = (obj)->state, 0))
709: #else
710: #define PetscObjectComposedDataSetScalar(obj,id,data) \
711:         PetscObjectComposedDataSetReal(obj,id,data)
712: #endif
713: /*MC
714:    PetscObjectComposedDataGetScalar - retrieve scalar data attached to an object

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

720:    Not collective

722:    Input parameters:
723: +  obj - the object from which data is to be retrieved
724: -  id - the identifier for the data

726:    Output parameters
727: +  data - the data to be retrieved
728: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

732:    Level: developer
733: M*/
734: #if defined(PETSC_USE_COMPLEX)
735: #define PetscObjectComposedDataGetScalar(obj,id,data,flag)                              \
736:   ((((obj)->scalarcomposedstate && ((obj)->scalarcomposedstate[id] == (obj)->state) ) ? \
737:    (data = (obj)->scalarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)
738: #else
739: #define PetscObjectComposedDataGetScalar(obj,id,data,flag)                             \
740:         PetscObjectComposedDataGetReal(obj,id,data,flag)
741: #endif

743: /*MC
744:    PetscObjectComposedDataSetScalarstar - attach scalar array data to a PetscObject

746:    Synopsis:
747:    #include "petsc/private/petscimpl.h"
748:    PetscErrorCode PetscObjectComposedDataSetScalarstar(PetscObject obj,int id,PetscScalar *data)

750:    Not collective

752:    Input parameters:
753: +  obj - the object to which data is to be attached
754: .  id - the identifier for the data
755: -  data - the data to  be attached

757:    Notes
758:    The data identifier can best be determined through a call to
759:    PetscObjectComposedDataRegister()

761:    Level: developer
762: M*/
763: #if defined(PETSC_USE_COMPLEX)
764: #define PetscObjectComposedDataSetScalarstar(obj,id,data)                                             \
765:   ((((obj)->scalarstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseScalarstar(obj)) ||  \
766:    ((obj)->scalarstarcomposeddata[id] = data,(obj)->scalarstarcomposedstate[id] = (obj)->state, 0))
767: #else
768: #define PetscObjectComposedDataSetScalarstar(obj,id,data) \
769:         PetscObjectComposedDataSetRealstar(obj,id,data)
770: #endif
771: /*MC
772:    PetscObjectComposedDataGetScalarstar - retrieve scalar array data
773:    attached to an object

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

779:    Not collective

781:    Input parameters:
782: +  obj - the object from which data is to be retrieved
783: -  id - the identifier for the data

785:    Output parameters
786: +  data - the data to be retrieved
787: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

791:    Level: developer
792: M*/
793: #if defined(PETSC_USE_COMPLEX)
794: #define PetscObjectComposedDataGetScalarstar(obj,id,data,flag)                                 \
795:   ((((obj)->scalarstarcomposedstate && ((obj)->scalarstarcomposedstate[id] == (obj)->state)) ? \
796:        (data = (obj)->scalarstarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)
797: #else
798: #define PetscObjectComposedDataGetScalarstar(obj,id,data,flag)         \
799:         PetscObjectComposedDataGetRealstar(obj,id,data,flag)
800: #endif

802: PETSC_EXTERN PetscErrorCode PetscObjectGetId(PetscObject,PetscObjectId*);
803: PETSC_EXTERN PetscErrorCode PetscObjectCompareId(PetscObject,PetscObjectId,PetscBool*);

805: PETSC_EXTERN PetscErrorCode PetscMonitorCompare(PetscErrorCode (*)(void),void *,PetscErrorCode (*)(void**),PetscErrorCode (*)(void),void *,PetscErrorCode (*)(void**),PetscBool *);

807: PETSC_EXTERN PetscMPIInt Petsc_Counter_keyval;
808: PETSC_EXTERN PetscMPIInt Petsc_InnerComm_keyval;
809: PETSC_EXTERN PetscMPIInt Petsc_OuterComm_keyval;
810: PETSC_EXTERN PetscMPIInt Petsc_Seq_keyval;
811: PETSC_EXTERN PetscMPIInt Petsc_ShmComm_keyval;

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

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

826:    PETSC_OFFLOAD_UNALLOCATED  - no memory contains valid matrix entries; NEVER used for vectors
827:    PETSC_OFFLOAD_GPU - GPU has valid vector/matrix entries
828:    PETSC_OFFLOAD_CPU - CPU has valid vector/matrix entries
829:    PETSC_OFFLOAD_BOTH - Both GPU and CPU have valid vector/matrix entries and they match

831:    Level: developer
832: E*/
833: typedef enum {PETSC_OFFLOAD_UNALLOCATED=0x0,PETSC_OFFLOAD_CPU=0x1,PETSC_OFFLOAD_GPU=0x2,PETSC_OFFLOAD_BOTH=0x3} PetscOffloadMask;

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

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

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

853: PETSC_EXTERN PetscErrorCode PetscSplitReductionGet(MPI_Comm,PetscSplitReduction**);
854: PETSC_EXTERN PetscErrorCode PetscSplitReductionEnd(PetscSplitReduction*);
855: PETSC_EXTERN PetscErrorCode PetscSplitReductionExtend(PetscSplitReduction*);

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

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

916: #else
917: typedef int PetscSpinlock;
918: #define PetscSpinlockCreate(a)  0
919: #define PetscSpinlockLock(a)    0
920: #define PetscSpinlockUnlock(a)  0
921: #define PetscSpinlockDestroy(a) 0
922: #endif

924: #if defined(PETSC_HAVE_THREADSAFETY)
925: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockOpen;
926: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockStdout;
927: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockStderr;
928: PETSC_INTERN PetscSpinlock PetscCommSpinLock;
929: #endif
930: #endif

932: PETSC_EXTERN PetscLogEvent PETSC_Barrier;
933: PETSC_EXTERN PetscLogEvent PETSC_BuildTwoSided;
934: PETSC_EXTERN PetscLogEvent PETSC_BuildTwoSidedF;

936: #if defined(PETSC_HAVE_ADIOS)
937: PETSC_EXTERN int64_t Petsc_adios_group;
938: #endif

940: PETSC_EXTERN PetscBool use_gpu_aware_mpi;
941: #if defined(PETSC_HAVE_CUDA)
942: PETSC_EXTERN PetscBool sf_use_default_cuda_stream;
943: #endif

945: #endif /* PETSCIMPL_H */