Actual source code: petscimpl.h

petsc-3.10.2 2018-10-09
Report Typos and Errors

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

  6: #if !defined(_PETSCHEAD_H)
  7: #define _PETSCHEAD_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_CLOSURES)
 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)


207: #else

209: /*  This check is for subtype methods such as DMDAGetCorners() that do not use the PetscTryMethod() or PetscUseMethod() paradigm */
211:   do {   \
212:     PetscErrorCode __ierr; \
213:     PetscBool      same; \
215:     __PetscObjectTypeCompare((PetscObject)h,t,&same);CHKERRQ(__ierr);      \
216:     if (!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); \
217:   } while (0)

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

230:   do {                                                                  \
231:     if (!h) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Null Object: Parameter # %d",arg); \
233:     if (((PetscObject)(h))->classid == PETSCFREEDHEADER) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Object already free: Parameter # %d",arg); \
234:     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); \
235:   } while (0)

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

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

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

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

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

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

272: #endif

274: #if !defined(PETSC_USE_DEBUG)


289: #else

291: /*
292:     For example, in the dot product between two vectors,
293:   both vectors must be either Seq or MPI, not one of each
294: */
296:   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);
297: /*
298:     Check type_name
299: */
301:   do {                                                                  \
302:     PetscBool      __match;                                             \
303:     PetscErrorCode _7_ierr;                                             \
304:     _7_PetscObjectTypeCompare(((PetscObject)a),(type),&__match);CHKERRQ(_7_ierr); \
305:     if (!__match) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Object (%s) is not %s",(char*)(((PetscObject)a)->type_name),type); \
306:   } while (0)

309:   do {                                                                  \
310:     PetscBool       __match;                                            \
311:     PetscErrorCode _7_ierr;                                             \
312:     _7_PetscObjectTypeCompareAny(((PetscObject)a),&__match,(type1),(type2),"");CHKERRQ(_7_ierr); \
313:     if (!__match) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Object (%s) is not %s or %s",(char*)(((PetscObject)a)->type_name),type1,type2); \
314:   } while (0)
315: /*
316:    Use this macro to check if the type is set
317: */
319:   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);
320: /*
321:    Sometimes object must live on same communicator to inter-operate
322: */
324:   do {                                                                  \
325:     PetscErrorCode _6_ierr,__flag;                                      \
326:     _6_MPI_Comm_compare(PetscObjectComm((PetscObject)a),PetscObjectComm((PetscObject)b),&__flag);CHKERRQ(_6_ierr);                                                   \
327:     if (__flag != MPI_CONGRUENT && __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,__flag); \
328:   } while (0)

331:   do {                                                  \
334:   } while (0)

337:   do {                                                                  \
338:     PetscErrorCode _7_ierr;                                             \
339:     PetscReal b1[5],b2[5];                                              \
340:     if (PetscIsNanScalar(b)) {b1[4] = 1;} else {b1[4] = 0;};            \
341:     b1[0] = -PetscRealPart(b); b1[1] = PetscRealPart(b);b1[2] = -PetscImaginaryPart(b); b1[3] = PetscImaginaryPart(b);         \
342:     _7_MPI_Allreduce(b1,b2,5,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)a));CHKERRQ(_7_ierr); \
343:     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); \
344:   } while (0)

347:   do {                                                                  \
348:     PetscErrorCode _7_ierr;                                             \
349:     PetscReal b1[3],b2[3];                                              \
350:     if (PetscIsNanReal(b)) {b1[2] = 1;} else {b1[2] = 0;};              \
351:     b1[0] = -b; b1[1] = b;                                              \
352:     _7_MPI_Allreduce(b1,b2,3,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)a));CHKERRQ(_7_ierr); \
353:     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); \
354:   } while (0)

357:   do {                                                                  \
358:     PetscErrorCode _7_ierr;                                             \
359:     PetscInt b1[2],b2[2];                                               \
360:     b1[0] = -b; b1[1] = b;                                              \
361:     _7_MPIU_Allreduce(b1,b2,2,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)a));CHKERRQ(_7_ierr); \
362:     if (-b2[0] != b2[1]) SETERRQ1(PetscObjectComm((PetscObject)a),PETSC_ERR_ARG_WRONG,"Int value must be same on all processes, argument # %d",c); \
363:   } while (0)


368:   do {                                                                  \
369:     PetscErrorCode _7_ierr;                                             \
370:     PetscMPIInt b1[2],b2[2];                                            \
371:     b1[0] = -(PetscMPIInt)b; b1[1] = (PetscMPIInt)b;                    \
372:     _7_MPIU_Allreduce(b1,b2,2,MPI_INT,MPI_MAX,PetscObjectComm((PetscObject)a));CHKERRQ(_7_ierr); \
373:     if (-b2[0] != b2[1]) SETERRQ1(PetscObjectComm((PetscObject)a),PETSC_ERR_ARG_WRONG,"Bool value must be same on all processes, argument # %d",c); \
374:   } while (0)

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

385: #endif

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

391:    Level: developer

393: .seealso: PetscUseMethod()
394: */
395: #define  PetscTryMethod(obj,A,B,C) \
396:   0;{ PetscErrorCode (*f)B, __ierr; \
397:     __PetscObjectQueryFunction((PetscObject)obj,A,&f);CHKERRQ(__ierr); \
398:     if (f) {__(*f)C;CHKERRQ(__ierr);}\
399:   }

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

405:    Level: developer

407: .seealso: PetscTryMethod()
408: */
409: #define  PetscUseMethod(obj,A,B,C) \
410:   0;{ PetscErrorCode (*f)B, __ierr; \
411:     __PetscObjectQueryFunction((PetscObject)obj,A,&f);CHKERRQ(__ierr); \
412:     if (f) {__(*f)C;CHKERRQ(__ierr);}\
413:     else SETERRQ1(PetscObjectComm((PetscObject)obj),PETSC_ERR_SUP,"Cannot locate function %s in object",A); \
414:   }

416: /*MC
417:    PetscObjectStateIncrease - Increases the state of any PetscObject

419:    Synopsis:
420:    #include "petsc/private/petscimpl.h"
421:    PetscErrorCode PetscObjectStateIncrease(PetscObject obj)

423:    Logically Collective

425:    Input Parameter:
426: .  obj - any PETSc object, for example a Vec, Mat or KSP. This must be
427:          cast with a (PetscObject), for example,
428:          PetscObjectStateIncrease((PetscObject)mat);

430:    Notes:
431:     object state is an integer which gets increased every time
432:    the object is changed internally. By saving and later querying the object state
433:    one can determine whether information about the object is still current.
434:    Currently, state is maintained for Vec and Mat objects.

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

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

443:    Level: developer

445:    seealso: PetscObjectStateGet()

447:    Concepts: state

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

452: PETSC_EXTERN PetscErrorCode PetscObjectStateGet(PetscObject,PetscObjectState*);
453: PETSC_EXTERN PetscErrorCode PetscObjectStateSet(PetscObject,PetscObjectState);
454: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataRegister(PetscInt*);
455: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseInt(PetscObject);
456: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseIntstar(PetscObject);
457: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseReal(PetscObject);
458: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseRealstar(PetscObject);
459: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseScalar(PetscObject);
460: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseScalarstar(PetscObject);
461: PETSC_EXTERN PetscInt         PetscObjectComposedDataMax;
462: /*MC
463:    PetscObjectComposedDataSetInt - attach integer data to a PetscObject

465:    Synopsis:
466:    #include "petsc/private/petscimpl.h"
467:    PetscErrorCode PetscObjectComposedDataSetInt(PetscObject obj,int id,int data)

469:    Not collective

471:    Input parameters:
472: +  obj - the object to which data is to be attached
473: .  id - the identifier for the data
474: -  data - the data to  be attached

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

479:    Level: developer
480: M*/
481: #define PetscObjectComposedDataSetInt(obj,id,data)                                      \
482:   ((((obj)->int_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseInt(obj)) ||  \
483:    ((obj)->intcomposeddata[id] = data,(obj)->intcomposedstate[id] = (obj)->state, 0))

485: /*MC
486:    PetscObjectComposedDataGetInt - retrieve integer data attached to an object

488:    Synopsis:
489:    #include "petsc/private/petscimpl.h"
490:    PetscErrorCode PetscObjectComposedDataGetInt(PetscObject obj,int id,int data,PetscBool  flag)

492:    Not collective

494:    Input parameters:
495: +  obj - the object from which data is to be retrieved
496: -  id - the identifier for the data

498:    Output parameters
499: +  data - the data to be retrieved
500: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

504:    Level: developer
505: M*/
506: #define PetscObjectComposedDataGetInt(obj,id,data,flag)                            \
507:   ((((obj)->intcomposedstate && ((obj)->intcomposedstate[id] == (obj)->state)) ?   \
508:    (data = (obj)->intcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

510: /*MC
511:    PetscObjectComposedDataSetIntstar - attach integer array data to a PetscObject

513:    Synopsis:
514:    #include "petsc/private/petscimpl.h"
515:    PetscErrorCode PetscObjectComposedDataSetIntstar(PetscObject obj,int id,int *data)

517:    Not collective

519:    Input parameters:
520: +  obj - the object to which data is to be attached
521: .  id - the identifier for the data
522: -  data - the data to  be attached

524:    Notes
525:    The data identifier can best be determined through a call to
526:    PetscObjectComposedDataRegister()

528:    Level: developer
529: M*/
530: #define PetscObjectComposedDataSetIntstar(obj,id,data)                                          \
531:   ((((obj)->intstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseIntstar(obj)) ||  \
532:    ((obj)->intstarcomposeddata[id] = data,(obj)->intstarcomposedstate[id] = (obj)->state, 0))

534: /*MC
535:    PetscObjectComposedDataGetIntstar - retrieve integer array data
536:    attached to an object

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

542:    Not collective

544:    Input parameters:
545: +  obj - the object from which data is to be retrieved
546: -  id - the identifier for the data

548:    Output parameters
549: +  data - the data to be retrieved
550: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

554:    Level: developer
555: M*/
556: #define PetscObjectComposedDataGetIntstar(obj,id,data,flag)                               \
557:   ((((obj)->intstarcomposedstate && ((obj)->intstarcomposedstate[id] == (obj)->state)) ?  \
558:    (data = (obj)->intstarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

560: /*MC
561:    PetscObjectComposedDataSetReal - attach real data to a PetscObject

563:    Synopsis:
564:    #include "petsc/private/petscimpl.h"
565:    PetscErrorCode PetscObjectComposedDataSetReal(PetscObject obj,int id,PetscReal data)

567:    Not collective

569:    Input parameters:
570: +  obj - the object to which data is to be attached
571: .  id - the identifier for the data
572: -  data - the data to  be attached

574:    Notes
575:    The data identifier can best be determined through a call to
576:    PetscObjectComposedDataRegister()

578:    Level: developer
579: M*/
580: #define PetscObjectComposedDataSetReal(obj,id,data)                                       \
581:   ((((obj)->real_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseReal(obj)) ||  \
582:    ((obj)->realcomposeddata[id] = data,(obj)->realcomposedstate[id] = (obj)->state, 0))

584: /*MC
585:    PetscObjectComposedDataGetReal - retrieve real data attached to an object

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

591:    Not collective

593:    Input parameters:
594: +  obj - the object from which data is to be retrieved
595: -  id - the identifier for the data

597:    Output parameters
598: +  data - the data to be retrieved
599: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

603:    Level: developer
604: M*/
605: #define PetscObjectComposedDataGetReal(obj,id,data,flag)                            \
606:   ((((obj)->realcomposedstate && ((obj)->realcomposedstate[id] == (obj)->state)) ?  \
607:    (data = (obj)->realcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

609: /*MC
610:    PetscObjectComposedDataSetRealstar - attach real array data to a PetscObject

612:    Synopsis:
613:    #include "petsc/private/petscimpl.h"
614:    PetscErrorCode PetscObjectComposedDataSetRealstar(PetscObject obj,int id,PetscReal *data)

616:    Not collective

618:    Input parameters:
619: +  obj - the object to which data is to be attached
620: .  id - the identifier for the data
621: -  data - the data to  be attached

623:    Notes
624:    The data identifier can best be determined through a call to
625:    PetscObjectComposedDataRegister()

627:    Level: developer
628: M*/
629: #define PetscObjectComposedDataSetRealstar(obj,id,data)                                           \
630:   ((((obj)->realstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseRealstar(obj)) ||  \
631:    ((obj)->realstarcomposeddata[id] = data, (obj)->realstarcomposedstate[id] = (obj)->state, 0))

633: /*MC
634:    PetscObjectComposedDataGetRealstar - retrieve real array data
635:    attached to an object

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

641:    Not collective

643:    Input parameters:
644: +  obj - the object from which data is to be retrieved
645: -  id - the identifier for the data

647:    Output parameters
648: +  data - the data to be retrieved
649: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

653:    Level: developer
654: M*/
655: #define PetscObjectComposedDataGetRealstar(obj,id,data,flag)                                \
656:   ((((obj)->realstarcomposedstate && ((obj)->realstarcomposedstate[id] == (obj)->state)) ?  \
657:    (data = (obj)->realstarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)

659: /*MC
660:    PetscObjectComposedDataSetScalar - attach scalar data to a PetscObject

662:    Synopsis:
663:    #include "petsc/private/petscimpl.h"
664:    PetscErrorCode PetscObjectComposedDataSetScalar(PetscObject obj,int id,PetscScalar data)

666:    Not collective

668:    Input parameters:
669: +  obj - the object to which data is to be attached
670: .  id - the identifier for the data
671: -  data - the data to  be attached

673:    Notes
674:    The data identifier can best be determined through a call to
675:    PetscObjectComposedDataRegister()

677:    Level: developer
678: M*/
679: #if defined(PETSC_USE_COMPLEX)
680: #define PetscObjectComposedDataSetScalar(obj,id,data)                                        \
681:   ((((obj)->scalar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseScalar(obj)) || \
682:    ((obj)->scalarcomposeddata[id] = data,(obj)->scalarcomposedstate[id] = (obj)->state, 0))
683: #else
684: #define PetscObjectComposedDataSetScalar(obj,id,data) \
685:         PetscObjectComposedDataSetReal(obj,id,data)
686: #endif
687: /*MC
688:    PetscObjectComposedDataGetScalar - retrieve scalar data attached to an object

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

694:    Not collective

696:    Input parameters:
697: +  obj - the object from which data is to be retrieved
698: -  id - the identifier for the data

700:    Output parameters
701: +  data - the data to be retrieved
702: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

706:    Level: developer
707: M*/
708: #if defined(PETSC_USE_COMPLEX)
709: #define PetscObjectComposedDataGetScalar(obj,id,data,flag)                              \
710:   ((((obj)->scalarcomposedstate && ((obj)->scalarcomposedstate[id] == (obj)->state) ) ? \
711:    (data = (obj)->scalarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)
712: #else
713: #define PetscObjectComposedDataGetScalar(obj,id,data,flag)                             \
714:         PetscObjectComposedDataGetReal(obj,id,data,flag)
715: #endif

717: /*MC
718:    PetscObjectComposedDataSetScalarstar - attach scalar array data to a PetscObject

720:    Synopsis:
721:    #include "petsc/private/petscimpl.h"
722:    PetscErrorCode PetscObjectComposedDataSetScalarstar(PetscObject obj,int id,PetscScalar *data)

724:    Not collective

726:    Input parameters:
727: +  obj - the object to which data is to be attached
728: .  id - the identifier for the data
729: -  data - the data to  be attached

731:    Notes
732:    The data identifier can best be determined through a call to
733:    PetscObjectComposedDataRegister()

735:    Level: developer
736: M*/
737: #if defined(PETSC_USE_COMPLEX)
738: #define PetscObjectComposedDataSetScalarstar(obj,id,data)                                             \
739:   ((((obj)->scalarstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseScalarstar(obj)) ||  \
740:    ((obj)->scalarstarcomposeddata[id] = data,(obj)->scalarstarcomposedstate[id] = (obj)->state, 0))
741: #else
742: #define PetscObjectComposedDataSetScalarstar(obj,id,data) \
743:         PetscObjectComposedDataSetRealstar(obj,id,data)
744: #endif
745: /*MC
746:    PetscObjectComposedDataGetScalarstar - retrieve scalar array data
747:    attached to an object

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

753:    Not collective

755:    Input parameters:
756: +  obj - the object from which data is to be retrieved
757: -  id - the identifier for the data

759:    Output parameters
760: +  data - the data to be retrieved
761: -  flag - PETSC_TRUE if the data item exists and is valid, PETSC_FALSE otherwise

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

765:    Level: developer
766: M*/
767: #if defined(PETSC_USE_COMPLEX)
768: #define PetscObjectComposedDataGetScalarstar(obj,id,data,flag)                                 \
769:   ((((obj)->scalarstarcomposedstate && ((obj)->scalarstarcomposedstate[id] == (obj)->state)) ? \
770:        (data = (obj)->scalarstarcomposeddata[id],flag = PETSC_TRUE) : (flag = PETSC_FALSE)),0)
771: #else
772: #define PetscObjectComposedDataGetScalarstar(obj,id,data,flag)         \
773:         PetscObjectComposedDataGetRealstar(obj,id,data,flag)
774: #endif

776: PETSC_EXTERN PetscErrorCode PetscObjectGetId(PetscObject,PetscObjectId*);

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

780: PETSC_EXTERN PetscMPIInt Petsc_Counter_keyval;
781: PETSC_EXTERN PetscMPIInt Petsc_InnerComm_keyval;
782: PETSC_EXTERN PetscMPIInt Petsc_OuterComm_keyval;
783: PETSC_EXTERN PetscMPIInt Petsc_Seq_keyval;
784: PETSC_EXTERN PetscMPIInt Petsc_ShmComm_keyval;

786: /*
787:   PETSc communicators have this attribute, see
788:   PetscCommDuplicate(), PetscCommDestroy(), PetscCommGetNewTag(), PetscObjectGetName()
789: */
790: typedef struct {
791:   PetscMPIInt tag;              /* next free tag value */
792:   PetscInt    refcount;         /* number of references, communicator can be freed when this reaches 0 */
793:   PetscInt    namecount;        /* used to generate the next name, as in Vec_0, Mat_1, ... */
794: } PetscCommCounter;

796: /*E
797:     PetscOffloadFlag - indicates which memory (CPU, GPU, or none contains valid vector

799:    PETSC_OFFLOAD_UNALLOCATED  - no memory contains valid matrix entries; NEVER used for vectors
800:    PETSC_OFFLOAD_GPU - GPU has valid vector/matrix entries
801:    PETSC_OFFLOAD_CPU - CPU has valid vector/matrix entries
802:    PETSC_OFFLOAD_BOTH - Both GPU and CPU have valid vector/matrix entries and they match

804:    Level: developer
805: E*/
806: typedef enum {PETSC_OFFLOAD_UNALLOCATED,PETSC_OFFLOAD_GPU,PETSC_OFFLOAD_CPU,PETSC_OFFLOAD_BOTH} PetscOffloadFlag;

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

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

812: typedef struct {
813:   MPI_Comm    comm;
814:   MPI_Request request;
815:   PetscBool   async;
816:   PetscScalar *lvalues;     /* this are the reduced values before call to MPI_Allreduce() */
817:   PetscScalar *gvalues;     /* values after call to MPI_Allreduce() */
818:   void        **invecs;     /* for debugging only, vector/memory used with each op */
819:   PetscInt    *reducetype;  /* is particular value to be summed or maxed? */
820:   SRState     state;        /* are we calling xxxBegin() or xxxEnd()? */
821:   PetscInt    maxops;       /* total amount of space we have for requests */
822:   PetscInt    numopsbegin;  /* number of requests that have been queued in */
823:   PetscInt    numopsend;    /* number of requests that have been gotten by user */
824: } PetscSplitReduction;

826: PETSC_EXTERN PetscErrorCode PetscSplitReductionGet(MPI_Comm,PetscSplitReduction**);
827: PETSC_EXTERN PetscErrorCode PetscSplitReductionEnd(PetscSplitReduction*);
828: PETSC_EXTERN PetscErrorCode PetscSplitReductionExtend(PetscSplitReduction*);

830: #if !defined(PETSC_SKIP_SPINLOCK)
831: #if defined(PETSC_HAVE_THREADSAFETY)
832: #  if defined(PETSC_HAVE_CONCURRENCYKIT)
833: #if defined(__cplusplus)
834: /*  CK does not have extern "C" protection in their include files */
835: extern "C" {
836: #endif
837: #include <ck_spinlock.h>
838: #if defined(__cplusplus)
839: }
840: #endif
841: typedef ck_spinlock_t PetscSpinlock;
842: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockCreate(PetscSpinlock *ck_spinlock)
843: {
844:   ck_spinlock_init(ck_spinlock);
845:   return 0;
846: }
847: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockLock(PetscSpinlock *ck_spinlock)
848: {
849:   ck_spinlock_lock(ck_spinlock);
850:   return 0;
851: }
852: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockUnlock(PetscSpinlock *ck_spinlock)
853: {
854:   ck_spinlock_unlock(ck_spinlock);
855:   return 0;
856: }
857: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockDestroy(PetscSpinlock *ck_spinlock)
858: {
859:   return 0;
860: }
861: #  elif defined(PETSC_HAVE_OPENMP)

863: #include <omp.h>
864: typedef omp_lock_t PetscSpinlock;
865: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockCreate(PetscSpinlock *omp_lock)
866: {
867:   omp_init_lock(omp_lock);
868:   return 0;
869: }
870: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockLock(PetscSpinlock *omp_lock)
871: {
872:   omp_set_lock(omp_lock);
873:   return 0;
874: }
875: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockUnlock(PetscSpinlock *omp_lock)
876: {
877:   omp_unset_lock(omp_lock);
878:   return 0;
879: }
880: PETSC_STATIC_INLINE PetscErrorCode PetscSpinlockDestroy(PetscSpinlock *omp_lock)
881: {
882:   omp_destroy_lock(omp_lock);
883:   return 0;
884: }
885: #else
886: Thread safety requires either --with-openmp or --download-concurrencykit
887: #endif

889: #else
890: typedef int PetscSpinlock;
891: #define PetscSpinlockCreate(a)  0
892: #define PetscSpinlockLock(a)    0
893: #define PetscSpinlockUnlock(a)  0
894: #define PetscSpinlockDestroy(a) 0
895: #endif

897: #if defined(PETSC_HAVE_THREADSAFETY)
898: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockOpen;
899: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockStdout;
900: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockStderr;
901: PETSC_INTERN PetscSpinlock PetscCommSpinLock;
902: #endif
903: #endif

905: PETSC_EXTERN PetscLogEvent PETSC_Barrier;
906: PETSC_EXTERN PetscLogEvent PETSC_BuildTwoSided;
907: PETSC_EXTERN PetscLogEvent PETSC_BuildTwoSidedF;

909: #if defined(PETSC_HAVE_ADIOS)
910: PETSC_EXTERN int64_t Petsc_adios_group;
911: #endif

913: #endif /* _PETSCHEAD_H */