Actual source code: petscimpl.h


  2: /*
  3:     Defines the basic header of all PETSc objects.
  4: */
  5: #ifndef PETSCIMPL_H
  6: #define PETSCIMPL_H
  7: #include <petscsys.h>

  9: /* SUBMANSEC = Sys */

 11: #if defined(PETSC_CLANG_STATIC_ANALYZER)
 12:   #define PetscDisableStaticAnalyzerForExpressionUnderstandingThatThisIsDangerousAndBugprone(...)
 13: #else
 14:   #define PetscDisableStaticAnalyzerForExpressionUnderstandingThatThisIsDangerousAndBugprone(...) __VA_ARGS__
 15: #endif

 17: #if PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)
 18: PETSC_INTERN PetscErrorCode PetscStackSetCheck(PetscBool);
 19: PETSC_INTERN PetscErrorCode PetscStackView(FILE *);
 20: PETSC_INTERN PetscErrorCode PetscStackReset(void);
 21: PETSC_INTERN PetscErrorCode PetscStackCopy(PetscStack *, PetscStack *);
 22: PETSC_INTERN PetscErrorCode PetscStackPrint(PetscStack *, FILE *);
 23: #else
 24:   #define PetscStackSetCheck(check)         PETSC_SUCCESS
 25:   #define PetscStackView(file)              PETSC_SUCCESS
 26:   #define PetscStackReset()                 PETSC_SUCCESS
 27:   #define PetscStackCopy(stackin, stackout) PETSC_SUCCESS
 28:   #define PetscStackPrint(stack, file)      PETSC_SUCCESS
 29: #endif

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

 35: /*
 36:    All major PETSc data structures have a common core; this is defined
 37:    below by PETSCHEADER.

 39:    PetscHeaderCreate() should be used whenever creating a PETSc structure.
 40: */

 42: /*
 43:    PetscOps: structure of core operations that all PETSc objects support.

 45:       getcomm()         - Gets the object's communicator.
 46:       view()            - Is the routine for viewing the entire PETSc object; for
 47:                           example, MatView() is the general matrix viewing routine.
 48:                           This is used by PetscObjectView((PetscObject)obj) to allow
 49:                           viewing any PETSc object.
 50:       destroy()         - Is the routine for destroying the entire PETSc object;
 51:                           for example,MatDestroy() is the general matrix
 52:                           destruction routine.
 53:                           This is used by PetscObjectDestroy((PetscObject*)&obj) to allow
 54:                           destroying any PETSc object.
 55:       compose()         - Associates a PETSc object with another PETSc object with a name
 56:       query()           - Returns a different PETSc object that has been associated
 57:                           with the first object using a name.
 58:       composefunction() - Attaches an a function to a PETSc object with a name.
 59:       queryfunction()   - Requests a registered function that has been attached to a PETSc object.
 60: */

 62: typedef struct {
 63:   PetscErrorCode (*view)(PetscObject, PetscViewer);
 64:   PetscErrorCode (*destroy)(PetscObject *);
 65:   PetscErrorCode (*compose)(PetscObject, const char[], PetscObject);
 66:   PetscErrorCode (*query)(PetscObject, const char[], PetscObject *);
 67:   PetscErrorCode (*composefunction)(PetscObject, const char[], void (*)(void));
 68:   PetscErrorCode (*queryfunction)(PetscObject, const char[], void (**)(void));
 69: } PetscOps;

 71: typedef enum {
 72:   PETSC_FORTRAN_CALLBACK_CLASS,
 73:   PETSC_FORTRAN_CALLBACK_SUBTYPE,
 74:   PETSC_FORTRAN_CALLBACK_MAXTYPE
 75: } PetscFortranCallbackType;
 76: typedef size_t PetscFortranCallbackId;
 77: #define PETSC_SMALLEST_FORTRAN_CALLBACK ((PetscFortranCallbackId)1000)
 78: PETSC_EXTERN PetscErrorCode PetscFortranCallbackRegister(PetscClassId, const char *, PetscFortranCallbackId *);
 79: PETSC_EXTERN PetscErrorCode PetscFortranCallbackGetSizes(PetscClassId, PetscFortranCallbackId *, PetscFortranCallbackId *);

 81: typedef struct {
 82:   void (*func)(void);
 83:   void *ctx;
 84: } PetscFortranCallback;

 86: /*
 87:    All PETSc objects begin with the fields defined in PETSCHEADER.
 88:    The PetscObject is a way of examining these fields regardless of
 89:    the specific object. In C++ this could be a base abstract class
 90:    from which all objects are derived.
 91: */
 92: #define PETSC_MAX_OPTIONS_HANDLER 5
 93: typedef struct _p_PetscObject {
 94:   PetscOps      bops[1];
 95:   PetscClassId  classid;
 96:   MPI_Comm      comm;
 97:   PetscObjectId id; /* this is used to compare object for identity that may no longer exist since memory addresses get recycled for new objects */
 98:   PetscInt      refct;
 99:   PetscErrorCode (*non_cyclic_references)(PetscObject, PetscInt *);
100:   PetscInt64        cidx;
101:   PetscMPIInt       tag;
102:   PetscFunctionList qlist;
103:   PetscObjectList   olist;
104:   char             *class_name; /*  for example, "Vec" */
105:   char             *description;
106:   char             *mansec;
107:   char             *type_name; /*  this is the subclass, for example VECSEQ which equals "seq" */
108:   char             *name;
109:   char             *prefix;
110:   PetscInt          tablevel;
111:   void             *cpp;
112:   PetscObjectState  state;
113:   PetscInt          int_idmax, intstar_idmax;
114:   PetscObjectState *intcomposedstate, *intstarcomposedstate;
115:   PetscInt         *intcomposeddata, **intstarcomposeddata;
116:   PetscInt          real_idmax, realstar_idmax;
117:   PetscObjectState *realcomposedstate, *realstarcomposedstate;
118:   PetscReal        *realcomposeddata, **realstarcomposeddata;
119:   PetscInt          scalar_idmax, scalarstar_idmax;
120:   PetscObjectState *scalarcomposedstate, *scalarstarcomposedstate;
121:   PetscScalar      *scalarcomposeddata, **scalarstarcomposeddata;
122:   void (**fortran_func_pointers)(void);             /* used by Fortran interface functions to stash user provided Fortran functions */
123:   PetscFortranCallbackId num_fortran_func_pointers; /* number of Fortran function pointers allocated */
124:   PetscFortranCallback  *fortrancallback[PETSC_FORTRAN_CALLBACK_MAXTYPE];
125:   PetscFortranCallbackId num_fortrancallback[PETSC_FORTRAN_CALLBACK_MAXTYPE];
126:   void                  *python_context;
127:   PetscErrorCode (*python_destroy)(void *);

129:   PetscInt noptionhandler;
130:   PetscErrorCode (*optionhandler[PETSC_MAX_OPTIONS_HANDLER])(PetscObject, PetscOptionItems *, void *);
131:   PetscErrorCode (*optiondestroy[PETSC_MAX_OPTIONS_HANDLER])(PetscObject, void *);
132:   void *optionctx[PETSC_MAX_OPTIONS_HANDLER];
133: #if defined(PETSC_HAVE_SAWS)
134:   PetscBool amsmem;          /* if PETSC_TRUE then this object is registered with SAWs and visible to clients */
135:   PetscBool amspublishblock; /* if PETSC_TRUE and publishing objects then will block at PetscObjectSAWsBlock() */
136: #endif
137:   PetscOptions options; /* options database used, NULL means default */
138:   PetscBool    optionsprinted;
139:   PetscBool    donotPetscObjectPrintClassNamePrefixType;
140: } _p_PetscObject;

142: #define PETSCHEADER(ObjectOps) \
143:   _p_PetscObject hdr; \
144:   ObjectOps      ops[1]

146: #define PETSCFREEDHEADER -1

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

151: #define PetscHeaderInitialize_Private(h, classid, class_name, descr, mansec, comm, destroy, view) \
152:   ((PetscErrorCode)(PetscHeaderCreate_Private((PetscObject)(h), classid, class_name, descr, mansec, comm, (PetscObjectDestroyFunction)(destroy), (PetscObjectViewFunction)(view)) || PetscLogObjectCreate(h)))

154: /*MC
155:     PetscHeaderCreate - Creates a PETSc object of a particular class

157:   Synopsis:
158: #include <petsc/private/petscimpl.h>
159:   PetscErrorCode PetscHeaderCreate(PetscObject h, PetscClassId classid, const char class_name[], const char descr[], const char mansec[], MPI_Comm comm, PetscObjectDestroyFunction destroy, PetscObjectViewFunction view)

161:   Input Parameters:
162: + classid    - The classid associated with this object (for example `VEC_CLASSID`)
163: . class_name - String name of class; should be static (for example "Vec"), may be
164:                `PETSC_NULLPTR`
165: . descr      - String containing short description; should be static (for example "Vector"),
166:                may be `PETSC_NULLPTR`
167: . mansec     - String indicating section in manual pages; should be static (for example "Vec"),
168:                may be `PETSC_NULLPTR`
169: . comm       - The MPI Communicator
170: . destroy    - The destroy routine for this object (for example `VecDestroy()`)
171: - view       - The view routine for this object (for example `VecView()`), may be
172:                `PETSC_NULLPTR`

174:   Output Parameter:
175: . h - The newly created `PetscObject`

177:   Notes:
178:   Can only be used to create a `PetscObject`. A `PetscObject` is defined as a pointer to a
179:   C/C++ structure which satisfies all of the following\:

181:   1. The first member of the structure must be a `_p_PetscObject`.
182:   2. C++ structures must be "Standard Layout". Generally speaking a standard layout class\:
183:      - Has no virtual functions or base classes.
184:      - Has only standard layout non-static members (if any).
185:      - Has only standard layout base classes (if any).

187:      See https://en.cppreference.com/w/cpp/language/classes#Standard-layout_class for further
188:      information.

190:   Example Usage:
191:   Existing `PetscObject`s may be easily created as shown. Unless otherwise stated, a particular
192:   objects `destroy` and `view` functions are exactly `<OBJECT_TYPE>Destroy()` and
193:   `<OBJECT_TYPE>View()`.
194: .vb
195:   Vec v;

197:   PetscHeaderCreate(v, VEC_CLASSID, "Vec", "A distributed vector class", "Vec", PETSC_COMM_WORLD, VecDestroy, VecView);
198: .ve

200:   It is possible to create custom `PetscObject`s, note however that they must abide by the
201:   restrictions set forth above.
202: .vb
203:   // OK, first member of C structure is _p_PetscObject
204:   struct MyCPetscObject_s
205:   {
206:     _p_PetscObject header;
207:     int            some_data;
208:   };
209:   typedef struct *MyCPetscObject_s MyCPetscObject;

211:   PetscErrorCode MyObjectDestroy(MyObject *);
212:   PetscErrorCode MyObjectView(MyObject);

214:   MyCPetscObject obj;

216:   // assume MY_PETSC_CLASSID is already registered
217:   PetscHeaderCreate(obj, MY_PETSC_CLASSID, "MyObject", "A custom PetscObject", PETSC_NULLPTR, PETSC_COMM_SELF, MyObjectDestroy, MyObjectView);

219:   // OK, only destroy function must be given, all others may be NULL
220:   PetscHeaderCreate(obj, MY_PETSC_CLASSID, PETSC_NULLPTR, PETSC_NULLPTR, PETSC_NULLPTR, PETSC_COMM_SELF, MyObjectDestroy, PETSC_NULLPTR);

222:   // ERROR must be a single-level pointer
223:   PetscHeaderCreate(&obj, ...);
224: .ve

226:   Illustrating proper construction from C++\:
227: .vb
228:   // ERROR, class is not standard layout, first member must be publicly accessible
229:   class BadCppPetscObject
230:   {
231:     _p_PetscObject header;
232:   };

234:   // ERROR, class is not standard layout, has a virtual function and virtual inheritance
235:   class BadCppPetscObject2 : virtual BaseClass
236:   {
237:   public:
238:     _p_PetscObject header;

240:     virtual void foo();
241:   };

243:   // ERROR, class is not standard layout! Has non-standard layout member
244:   class BadCppPetscObject2
245:   {
246:   public:
247:     _p_PetscObject    header;
248:     BadCppPetscObject non_standard_layout;
249:   };

251:   // OK, class is standard layout!
252:   class GoodCppPetscObject;
253:   using MyCppObject = GoodCppPetscObject *;

255:   // OK, non-virtual inheritance of other standard layout class does not affect layout
256:   class GoodCppPetscObject : StandardLayoutClass
257:   {
258:   public:
259:     // OK, non standard layout member is static, does not affect layout
260:     static BadCppPetscObject non_standard_layout;

262:     // OK, first non-static member is _p_PetscObject
263:     _p_PetscObject header;

265:      // OK, non-virtual member functions do not affect class layout
266:     void foo();

268:     // OK, may use "member" functions for destroy and view so long as they are static
269:     static PetscErrorCode destroy(MyCppObject *);
270:     static PetscErrorCode view(MyCppObject);
271:   };

273:   // OK, usage via pointer
274:   MyObject obj;

276:   PetscHeaderCreate(obj, MY_PETSC_CLASSID, "MyObject", "A custom C++ PetscObject", nullptr, PETSC_COMM_SELF, GoodCppPetscObject::destroy, GoodCppPetscObject::view);
277: .ve

279:   Level: developer

281: .seealso: `PetscHeaderDestroy()`, `PetscClassIdRegister()`
282: M*/
283: #define PetscHeaderCreate(h, classid, class_name, descr, mansec, comm, destroy, view) ((PetscErrorCode)(PetscNew(&(h)) || PetscHeaderInitialize_Private((h), (classid), (class_name), (descr), (mansec), (comm), (destroy), (view))))

285: PETSC_EXTERN PetscErrorCode PetscComposedQuantitiesDestroy(PetscObject obj);
286: PETSC_EXTERN PetscErrorCode PetscHeaderCreate_Private(PetscObject, PetscClassId, const char[], const char[], const char[], MPI_Comm, PetscObjectDestroyFunction, PetscObjectViewFunction);
287: PETSC_INTERN PetscObjectId  PetscObjectNewId_Internal(void);

289: /*MC
290:   PetscHeaderDestroy - Final step in destroying a `PetscObject`

292:   Synopsis:
293: #include <petsc/private/petscimpl.h>
294:   PetscErrorCode PetscHeaderDestroy(PetscObject *obj)

296:   Input Parameters:
297: . h - A pointer to the header created with `PetscHeaderCreate()`

299:   Notes:
300:   `h` is freed and set to `PETSC_NULLPTR` when this routine returns.

302:   Example Usage:
303: .vb
304:   PetscObject obj;

306:   PetscHeaderCreate(obj, ...);
307:   // use obj...

309:   // note pointer to obj is used
310:   PetscHeaderDestroy(&obj);
311: .ve

313:   Note that this routine is the _last_ step when destroying higher-level `PetscObject`s as it
314:   deallocates the memory for the structure itself\:
315: .vb
316:   typedef struct MyPetscObject_s *MyPetscObject;
317:   struct MyPetscObject_s
318:   {
319:     _p_PetscObject  hdr;
320:     PetscInt       *foo;
321:     PetscScalar    *bar;
322:   };

324:   // assume obj is created/initialized elsewhere...
325:   MyPetscObject obj;

327:   // OK, should dispose of all dynamically allocated resources before calling
328:   // PetscHeaderDestroy()
329:   PetscFree(obj->foo);

331:   // OK, dispose of obj
332:   PetscHeaderDestroy(&obj);

334:   // ERROR, obj points to NULL here, accessing obj->bar may result in segmentation violation!
335:   // obj->bar is potentially leaked!
336:   PetscFree(obj->bar);
337: .ve

339:   Level: developer

341: .seealso: `PetscHeaderCreate()`
342: M*/
343: #define PetscHeaderDestroy(h) ((PetscErrorCode)(PetscHeaderDestroy_Private((PetscObject)(*(h)), PETSC_FALSE) || PetscFree(*(h))))

345: PETSC_EXTERN PetscErrorCode                PetscHeaderDestroy_Private(PetscObject, PetscBool);
346: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode PetscHeaderReset_Internal(PetscObject);
347: PETSC_EXTERN PetscErrorCode                PetscObjectCopyFortranFunctionPointers(PetscObject, PetscObject);
348: PETSC_EXTERN PetscErrorCode                PetscObjectSetFortranCallback(PetscObject, PetscFortranCallbackType, PetscFortranCallbackId *, void (*)(void), void *ctx);
349: PETSC_EXTERN PetscErrorCode                PetscObjectGetFortranCallback(PetscObject, PetscFortranCallbackType, PetscFortranCallbackId, void (**)(void), void **ctx);

351: PETSC_INTERN PetscErrorCode PetscCitationsInitialize(void);
352: PETSC_INTERN PetscErrorCode PetscFreeMPIResources(void);
353: PETSC_INTERN PetscErrorCode PetscOptionsHasHelpIntro_Internal(PetscOptions, PetscBool *);

355: /* Code shared between C and Fortran */
356: PETSC_INTERN PetscErrorCode PetscInitialize_Common(const char *, const char *, const char *, PetscBool, PetscBool, PetscInt);

358: #if PetscDefined(HAVE_SETJMP_H)
359: PETSC_EXTERN PetscBool PetscCheckPointer(const void *, PetscDataType);
360: #else
361:   #define PetscCheckPointer(ptr, data_type) (ptr ? PETSC_TRUE : PETSC_FALSE)
362: #endif
363: #if !defined(PETSC_CLANG_STATIC_ANALYZER)
364:   /*
365:     Macros to test if a PETSc object is valid and if pointers are valid
366: */
367:   #if !defined(PETSC_USE_DEBUG)

370:       do { \
371:         (void)(h); \
372:       } while (0)
374:       do { \
375:         (void)(h); \
376:       } while (0)
378:       do { \
379:         (void)(h); \
380:       } while (0)
382:       do { \
383:         (void)(h); \
384:       } while (0)
386:       do { \
387:         (void)(h); \
388:       } while (0)
390:       do { \
391:         (void)(h); \
392:       } while (0)
394:       do { \
395:         (void)(h); \
396:       } while (0)
398:       do { \
399:         (void)(h); \
400:       } while (0)
402:       do { \
403:         (void)(h); \
404:       } while (0)
406:       do { \
407:         (void)(h); \
408:       } while (0)
410:       do { \
411:         (void)(h); \
412:       } while (0)
414:       do { \
415:         (void)(h); \
416:       } while (0)

418:   #else

420:     /*  This check is for subtype methods such as DMDAGetCorners() that do not use the PetscTryMethod() or PetscUseMethod() paradigm */
422:       do { \
423:         PetscBool _7_same; \
425:         PetscCall(PetscObjectTypeCompare((PetscObject)(h), t, &_7_same)); \
426:         PetscCheck(_7_same, 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); \
427:       } while (0)

430:       do { \
431:         PetscCheck(ptr, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Null Pointer: Parameter # %d", arg); \
432:         PetscCheck(PetscCheckPointer(ptr, ptype), PETSC_COMM_SELF, PETSC_ERR_ARG_BADPTR, "Invalid Pointer to " PetscStringize(ptrtype) ": Argument '" PetscStringize(ptr) "' (parameter # %d)", arg); \
433:       } while (0)

436:       do { \
438:         if (((PetscObject)(h))->classid != ck) { \
439:           PetscCheck(((PetscObject)(h))->classid != PETSCFREEDHEADER, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Object already free: Parameter # %d", arg); \
440:           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Wrong type of object: Parameter # %d", arg); \
441:         } \
442:       } while (0)

445:       do { \
447:         PetscCheck(((PetscObject)(h))->classid != PETSCFREEDHEADER, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Object already free: Parameter # %d", arg); \
448:         PetscCheck(((PetscObject)(h))->classid >= PETSC_SMALLEST_CLASSID && ((PetscObject)(h))->classid <= PETSC_LARGEST_CLASSID, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Invalid type of object: Parameter # %d", arg); \
449:       } while (0)


461:       do { \
462:         PetscCheck((f), PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Null Function Pointer: Parameter # %d", arg); \
463:       } while (0)
464:   #endif
465: #else  /* PETSC_CLANG_STATIC_ANALYZER */
466: template <typename T>
468: template <typename T>
470: template <typename T>
472: template <typename T>
474: template <typename T>
476: template <typename T>
478: template <typename T>
480: template <typename T>
482: template <typename T>
484: template <typename T>
486: template <typename T>
488: template <typename T>
490: template <typename T>
492: #endif /* PETSC_CLANG_STATIC_ANALYZER */

494: #define PetscSorted(n, idx, sorted) \
495:   do { \
496:     (sorted) = PETSC_TRUE; \
497:     for (PetscInt _i_ = 1; _i_ < (n); ++_i_) { \
498:       if ((idx)[_i_] < (idx)[_i_ - 1]) { \
499:         (sorted) = PETSC_FALSE; \
500:         break; \
501:       } \
502:     } \
503:   } while (0)

505: #if !defined(PETSC_CLANG_STATIC_ANALYZER)
506:   #if !defined(PETSC_USE_DEBUG)

508:     #define PetscCheckSameType(a, arga, b, argb) \
509:       do { \
510:         (void)(a); \
511:         (void)(b); \
512:       } while (0)
513:     #define PetscCheckTypeName(a, type) \
514:       do { \
515:         (void)(a); \
516:       } while (0)
517:     #define PetscCheckTypeNames(a, type1, type2) \
518:       do { \
519:         (void)(a); \
520:       } while (0)
522:       do { \
523:         (void)(a); \
524:       } while (0)
525:     #define PetscCheckSameComm(a, arga, b, argb) \
526:       do { \
527:         (void)(a); \
528:         (void)(b); \
529:       } while (0)
530:     #define PetscCheckSameTypeAndComm(a, arga, b, argb) \
531:       do { \
532:         (void)(a); \
533:         (void)(b); \
534:       } while (0)
536:       do { \
537:         (void)(a); \
538:         (void)(b); \
539:       } while (0)
541:       do { \
542:         (void)(a); \
543:         (void)(b); \
544:       } while (0)
546:       do { \
547:         (void)(a); \
548:         (void)(b); \
549:       } while (0)
551:       do { \
552:         (void)(a); \
553:         (void)(b); \
554:       } while (0)
556:       do { \
557:         (void)(a); \
558:         (void)(b); \
559:       } while (0)
561:       do { \
562:         (void)(a); \
563:         (void)(b); \
564:       } while (0)
565:     #define PetscCheckSorted(n, idx) \
566:       do { \
567:         (void)(n); \
568:         (void)(idx); \
569:       } while (0)

571:   #else

573:     /*
574:   This macro currently does nothing, the plan is for each PetscObject to have a PetscInt "type"
575:   member associated with the string type_name that can be quickly compared.

577:   **Do not swap this macro to compare string type_name!**

579:   This macro is used incorrectly in the code. Many places that do not need identity of the
580:   types incorrectly call this check and would need to be fixed if this macro is enabled.
581: */
582:     #if 0
583:       #define PetscCheckSameType(a, arga, b, argb) \
584:         do { \
585:           PetscBool pcst_type_eq_ = PETSC_TRUE; \
586:           PetscCall(PetscStrcmp(((PetscObject)(a))->type_name, (((PetscObject)(b)))->type_name, &pcst_type_eq_)); \
587:           PetscCheck(pcst_type_eq_, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMETYPE, "Objects not of same type : Argument # % d and % d, % s != % s ", arga, argb, ((PetscObject)(a))->type_name, ((PetscObject)(b))->type_name); \
588:         } while (0)
589:     #else
590:       #define PetscCheckSameType(a, arga, b, argb) \
591:         do { \
592:           (void)(a); \
593:           (void)(b); \
594:         } while (0)
595:     #endif

597:     /*
598:     Check type_name
599: */
600:     #define PetscCheckTypeName(a, type) \
601:       do { \
602:         PetscBool _7_match; \
603:         PetscCall(PetscObjectTypeCompare(((PetscObject)(a)), (type), &_7_match)); \
604:         PetscCheck(_7_match, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Object (%s) is not %s", (char *)(((PetscObject)(a))->type_name), type); \
605:       } while (0)

607:     #define PetscCheckTypeNames(a, type1, type2) \
608:       do { \
609:         PetscBool _7_match; \
610:         PetscCall(PetscObjectTypeCompareAny(((PetscObject)(a)), &_7_match, (type1), (type2), "")); \
611:         PetscCheck(_7_match, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Object (%s) is not %s or %s", (char *)(((PetscObject)(a))->type_name), type1, type2); \
612:       } while (0)

614:     /*
615:    Use this macro to check if the type is set
616: */

619:     /*
620:    Sometimes object must live on same communicator to inter-operate
621: */
622:     #define PetscCheckSameComm(a, arga, b, argb) \
623:       do { \
624:         PetscMPIInt _7_flag; \
625:         PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)(a)), PetscObjectComm((PetscObject)(b)), &_7_flag)); \
626:         PetscCheck(_7_flag == MPI_CONGRUENT || _7_flag == MPI_IDENT, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMECOMM, "Different communicators in the two objects: Argument # %d and %d flag %d", arga, argb, _7_flag); \
627:       } while (0)

629:     #define PetscCheckSameTypeAndComm(a, arga, b, argb) \
630:       do { \
631:         PetscCheckSameType(a, arga, b, argb); \
632:         PetscCheckSameComm(a, arga, b, argb); \
633:       } while (0)

636:       do { \
637:         PetscScalar b0 = (b); \
638:         PetscReal   b1[5], b2[5]; \
639:         if (PetscIsNanScalar(b0)) { \
640:           b1[4] = 1; \
641:         } else { \
642:           b1[4] = 0; \
643:         }; \
644:         b1[0] = -PetscRealPart(b0); \
645:         b1[1] = PetscRealPart(b0); \
646:         b1[2] = -PetscImaginaryPart(b0); \
647:         b1[3] = PetscImaginaryPart(b0); \
648:         PetscCall(MPIU_Allreduce(b1, b2, 5, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)(a)))); \
649:         PetscCheck(b2[4] > 0 || (PetscEqualReal(-b2[0], b2[1]) && PetscEqualReal(-b2[2], b2[3])), PetscObjectComm((PetscObject)(a)), PETSC_ERR_ARG_WRONG, "Scalar value must be same on all processes, argument # %d", arg); \
650:       } while (0)

653:       do { \
654:         PetscReal b0 = (b), b1[3], b2[3]; \
655:         if (PetscIsNanReal(b0)) { \
656:           b1[2] = 1; \
657:         } else { \
658:           b1[2] = 0; \
659:         }; \
660:         b1[0] = -b0; \
661:         b1[1] = b0; \
662:         PetscCall(MPIU_Allreduce(b1, b2, 3, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)(a)))); \
663:         PetscCheck(b2[2] > 0 || PetscEqualReal(-b2[0], b2[1]), PetscObjectComm((PetscObject)(a)), PETSC_ERR_ARG_WRONG, "Real value must be same on all processes, argument # %d", arg); \
664:       } while (0)

667:       do { \
668:         PetscInt b0 = (b), b1[2], b2[2]; \
669:         b1[0]       = -b0; \
670:         b1[1]       = b0; \
671:         PetscCall(MPIU_Allreduce(b1, b2, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)(a)))); \
672:         PetscCheck(-b2[0] == b2[1], PetscObjectComm((PetscObject)(a)), PETSC_ERR_ARG_WRONG, "Int value must be same on all processes, argument # %d", arg); \
673:       } while (0)

676:       do { \
677:         PetscMPIInt b0 = (b), b1[2], b2[2]; \
678:         b1[0]          = -b0; \
679:         b1[1]          = b0; \
680:         PetscCall(MPIU_Allreduce(b1, b2, 2, MPI_INT, MPI_MAX, PetscObjectComm((PetscObject)(a)))); \
681:         PetscCheck(-b2[0] == b2[1], PetscObjectComm((PetscObject)(a)), PETSC_ERR_ARG_WRONG, "PetscMPIInt value must be same on all processes, argument # %d", arg); \
682:       } while (0)

685:       do { \
686:         PetscMPIInt b0 = (PetscMPIInt)(b), b1[2], b2[2]; \
687:         b1[0]          = -b0; \
688:         b1[1]          = b0; \
689:         PetscCall(MPIU_Allreduce(b1, b2, 2, MPI_INT, MPI_MAX, PetscObjectComm((PetscObject)(a)))); \
690:         PetscCheck(-b2[0] == b2[1], PetscObjectComm((PetscObject)(a)), PETSC_ERR_ARG_WRONG, "Bool value must be same on all processes, argument # %d", arg); \
691:       } while (0)

694:       do { \
695:         PetscMPIInt b0 = (PetscMPIInt)(b), b1[2], b2[2]; \
696:         b1[0]          = -b0; \
697:         b1[1]          = b0; \
698:         PetscCall(MPIU_Allreduce(b1, b2, 2, MPI_INT, MPI_MAX, PetscObjectComm((PetscObject)(a)))); \
699:         PetscCheck(-b2[0] == b2[1], PetscObjectComm((PetscObject)(a)), PETSC_ERR_ARG_WRONG, "Enum value must be same on all processes, argument # %d", arg); \
700:       } while (0)

702:     #define PetscCheckSorted(n, idx) \
703:       do { \
704:         PetscBool _1_flg; \
705:         PetscSorted(n, idx, _1_flg); \
706:         PetscCheck(_1_flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input array needs to be sorted"); \
707:       } while (0)

709:   #endif
710: #else  /* PETSC_CLANG_STATIC_ANALYZER */
711: template <typename Ta, typename Tb>
712: void PetscCheckSameType(Ta, int, Tb, int);
713: template <typename Ta, typename Tb>
714: void PetscCheckTypeName(Ta, Tb);
715: template <typename Ta, typename Tb, typename Tc>
716: void PetscCheckTypeName(Ta, Tb, Tc);
717: template <typename T>
719: template <typename Ta, typename Tb>
720: void PetscCheckSameComm(Ta, int, Tb, int);
721: template <typename Ta, typename Tb>
722: void PetscCheckSameTypeAndComm(Ta, int, Tb, int);
723: template <typename Ta, typename Tb>
725: template <typename Ta, typename Tb>
727: template <typename Ta, typename Tb>
729: template <typename Ta, typename Tb>
731: template <typename Ta, typename Tb>
733: template <typename Ta, typename Tb>
735: template <typename T>
736: void PetscCheckSorted(PetscInt, T);
737: #endif /* PETSC_CLANG_STATIC_ANALYZER */

739: /*MC
740:    PetscTryMethod - Queries an object for a method added with `PetscObjectComposeFunction()`, if it exists then calls it.

742:   Synopsis:
743:    #include "petsc/private/petscimpl.h"
744:    PetscTryMethod(PetscObject obj,const char *name,(arg_types),(arg_value))

746:    Input Parameters:
747: +   obj - the object
748: .   name - the name of the method, for example, "KSPGMRESSetRestart_C" for the function `KSPGMRESSetRestart()`
749: .   arg_types - the argument types for the method, for example, (KSP,PetscInt)
750: -   args - the arguments for the method, for example, (ksp,restart))

752:    Level: developer

754:    Notes:
755:    The object is always the implicit first argument of the method and is not listed in arg_types or args

757:    This does not return an error code, it is a macro that returns with an error code on error.

759:    Use `PetscUseTypeMethod()` or `PetscTryTypeMethod()` to call functions that are included in the objects function table, the `ops` array
760:    in the object.

762: .seealso: `PetscUseMethod()`, `PetscCall()`, `PetscUseTypeMethod()`, `PetscTryTypeMethod()`, `PetscCheck()`
763: M*/
764: #define PetscTryMethod(obj, A, B, C) \
765:   do { \
766:     PetscErrorCode(*_7_f) B; \
767:     PetscCall(PetscObjectQueryFunction((PetscObject)(obj), A, &_7_f)); \
768:     if (_7_f) PetscCall((*_7_f)C); \
769:   } while (0)

771: /*MC
772:    PetscUseMethod - Queries an object for a method added with `PetscObjectComposeFunction()`, if it exists then calls it, otherwise generates an error.

774:   Synopsis:
775:    #include "petsc/private/petscimpl.h"
776:    PetscUseMethod(PetscObject obj,const char *name,(arg_types),(arg_value))

778:    Input Parameters:
779: +   obj - the object
780: .   name - the name of the method, for example, "KSPGMRESSetRestart_C" for the function `KSPGMRESSetRestart()`
781: .   arg_types - the argument types for the method, for example, (KSP,PetscInt)
782: -   args - the arguments for the method, for example, (ksp,restart))

784:    Level: developer

786:    Notes:
787:    The object is always the implicit first argument of the method and is not listed in arg_types or args

789:    This does not return an error code, it is a macro that returns with an error code on error.

791:    Use `PetscUseTypeMethod()` or `PetscTryTypeMethod()` to call functions that are included in the objects function table, the `ops` array
792:    in the object.

794: .seealso: `PetscTryMethod()`, `PetscCall()`, `PetscUseTypeMethod()`, `PetscTryTypeMethod()`, `PetscCheck()`
795: M*/
796: #define PetscUseMethod(obj, A, B, C) \
797:   do { \
798:     PetscErrorCode(*_7_f) B; \
799:     PetscCall(PetscObjectQueryFunction((PetscObject)(obj), A, &_7_f)); \
800:     PetscCheck(_7_f, PetscObjectComm((PetscObject)(obj)), PETSC_ERR_SUP, "Cannot locate function %s in object", A); \
801:     PetscCall((*_7_f)C); \
802:   } while (0)

804: /*
805:   Use Microsoft traditional preprocessor.

807:   The Microsoft compiler option -Zc:preprocessor available in recent versions of the compiler
808:   sets  _MSVC_TRADITIONAL to zero so this code path is not used.

810:   It appears the Intel Windows compiler icl does not have an equaivalent of  -Zc:preprocessor

812:   These macros use the trick that Windows compilers remove the , before the __VA_ARGS__ if __VA_ARGS__ does not exist

814:   PetscCall() cannot be used in the macros because the remove the , trick does not work in a macro in a macro
815: */
816: #if (defined(_MSC_VER) && (!defined(_MSVC_TRADITIONAL) || _MSVC_TRADITIONAL)) || defined(__ICL)

818:   #define PetscUseTypeMethod(obj, OP, ...) \
819:     do { \
820:       PetscErrorCode ierr_p_; \
821:       PetscStackUpdateLine; \
822:       PetscCheck((obj)->ops->OP, PetscObjectComm((PetscObject)obj), PETSC_ERR_SUP, "No method %s for %s of type %s", PetscStringize(OP), ((PetscObject)obj)->class_name, ((PetscObject)obj)->type_name); \
823:       ierr_p_ = (*(obj)->ops->OP)(obj, __VA_ARGS__); \
824:       PetscCall(ierr_p_); \
825:     } while (0)

827:   #define PetscTryTypeMethod(obj, OP, ...) \
828:     do { \
829:       if ((obj)->ops->OP) { \
830:         PetscErrorCode ierr_p_; \
831:         PetscStackUpdateLine; \
832:         ierr_p_ = (*(obj)->ops->OP)(obj, __VA_ARGS__); \
833:         PetscCall(ierr_p_); \
834:       } \
835:     } while (0)

837: #else

839:   /*MC
840:    PetscUseTypeMethod - Call a method on a PETSc object, that is a function in the objects function table obj->ops, error if the method does not exist

842:   Synopsis:
843:    #include "petsc/private/petscimpl.h"
844:    PetscUseTypeMethod(obj,method,other_args)

846:    Input Parameters:
847: +   obj - the object the method is called on
848: .   method - the name of the method, for example, mult for the PETSc routine MatMult()
849: -   other_args - the other arguments for the method, obj is the first argument

851:    Level: developer

853:    Note:
854:    This does not return an error code, it is a macro that returns with an error code on error.

856:    Use `PetscUseMethod()` or `PetscTryMethod()` to call functions that have been composed to an object with `PetscObjectComposeFunction()`

858: .seealso: `PetscTryMethod()`, `PetscUseMethod()`, `PetscCall()`, `PetscCheck()`, `PetscTryTypeMethod()`
859: M*/
860:   #define PetscUseTypeMethod(obj, ...) \
861:     do { \
862:       PetscCheck((obj)->ops->PETSC_FIRST_ARG((__VA_ARGS__, unused)), PetscObjectComm((PetscObject)obj), PETSC_ERR_SUP, "No method %s for %s of type %s", \
863:                  PetscStringize(PETSC_FIRST_ARG((__VA_ARGS__,unused))), ((PetscObject)obj)->class_name, ((PetscObject)obj)->type_name); \
864:       PetscCall((*(obj)->ops->PETSC_FIRST_ARG((__VA_ARGS__, unused)))(obj PETSC_REST_ARG(__VA_ARGS__))); \
865:     } while (0)

867:   /*MC
868:    PetscTryTypeMethod - Call a method on a PETSc object, that is a function in the objects function table obj->ops, skip if the method does not exist

870:   Synopsis:
871:    #include "petsc/private/petscimpl.h"
872:    PetscTryTtype(obj,method,other_args)

874:    Input Parameters:
875: +   obj - the object the method is called on
876: .   method - the name of the method, for example, mult for the PETSc routine MatMult()
877: -   other_args - the other arguments for the method, obj is the first argument

879:    Level: developer

881:    Note:
882:    This does not return an error code, it is a macro that returns with an error code on error.

884:    Use `PetscUseMethod()` or `PetscTryMethod()` to call functions that have been composed to an object with `PetscObjectComposeFunction()`

886: .seealso: `PetscTryMethod()`, `PetscUseMethod()`, `PetscCall()`, `PetscCheck()`, `PetscUseTypeMethod()`
887: M*/
888:   #define PetscTryTypeMethod(obj, ...) \
889:     do { \
890:       if ((obj)->ops->PETSC_FIRST_ARG((__VA_ARGS__, unused))) PetscCall((*(obj)->ops->PETSC_FIRST_ARG((__VA_ARGS__, unused)))(obj PETSC_REST_ARG(__VA_ARGS__))); \
891:     } while (0)

893: #endif

895: /*MC
896:    PetscObjectStateIncrease - Increases the state of any `PetscObject`

898:    Synopsis:
899:    #include "petsc/private/petscimpl.h"
900:    PetscErrorCode PetscObjectStateIncrease(PetscObject obj)

902:    Logically Collective

904:    Input Parameter:
905: .  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. This must be
906:          cast with a (PetscObject), for example,
907:          `PetscObjectStateIncrease`((`PetscObject`)mat);

909:    Notes:
910:    Object state is a 64 bit integer which gets increased every time
911:    the object is changed internally. By saving and later querying the object state
912:    one can determine whether information about the object is still current.
913:    Currently, state is maintained for `Vec` and `Mat` objects.

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

920:    Routines such as `VecNorm()` can by-pass the computation if the norm has already been computed and the vector's state has not changed.

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

924:    `Mat` also has `MatGetNonzeroState()` for tracking changes to the nonzero structure.

926:    Level: developer

928: .seealso: `PetscObjectStateGet()`, `PetscObject`

930: M*/
931: #define PetscObjectStateIncrease(obj) ((obj)->state++, PETSC_SUCCESS)

933: PETSC_EXTERN PetscErrorCode PetscObjectStateGet(PetscObject, PetscObjectState *);
934: PETSC_EXTERN PetscErrorCode PetscObjectStateSet(PetscObject, PetscObjectState);
935: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataRegister(PetscInt *);
936: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseInt(PetscObject);
937: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseIntstar(PetscObject);
938: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseReal(PetscObject);
939: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseRealstar(PetscObject);
940: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseScalar(PetscObject);
941: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseScalarstar(PetscObject);
942: PETSC_EXTERN PetscInt       PetscObjectComposedDataMax;

944: /*MC
945:    PetscObjectComposedDataSetInt - attach integer data to a `PetscObject` that may be accessed with `PetscObjectComposedDataGetInt()`

947:    Synopsis:
948:    #include "petsc/private/petscimpl.h"
949:    PetscErrorCode PetscObjectComposedDataSetInt(PetscObject obj,int id,int data)

951:    Not collective

953:    Input parameters:
954: +  obj - the object to which data is to be attached
955: .  id - the identifier for the data
956: -  data - the data to  be attached

958:    Notes:
959:    The data identifier can be created through a call to  `PetscObjectComposedDataRegister()`

961:    This allows the efficient composition of a single integer value with a `PetscObject`. Complex data may be
962:    attached with `PetscObjectCompose()`

964:    Level: developer

966: .seealso: `PetscObjectComposedDataGetInt()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetReal()`,
967:           `PetscObjectComposedDataGetIntstar()`, `PetscObjectComposedDataSetIntstar()`, `PetscObject`,
968:           `PetscObjectCompose()`, `PetscObjectQuery()`
969: M*/
970: #define PetscObjectComposedDataSetInt(obj, id, data) \
971:   ((PetscErrorCode)((((obj)->int_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseInt(obj)) || ((obj)->intcomposeddata[id] = data, (obj)->intcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))

973: /*MC
974:    PetscObjectComposedDataGetInt - retrieve integer data attached to an object with `PetscObjectComposedDataSetInt()`

976:    Synopsis:
977:    #include "petsc/private/petscimpl.h"
978:    PetscErrorCode PetscObjectComposedDataGetInt(PetscObject obj,int id,int data,PetscBool  flag)

980:    Not collective

982:    Input parameters:
983: +  obj - the object from which data is to be retrieved
984: -  id - the identifier for the data

986:    Output parameters:
987: +  data - the data to be retrieved
988: -  flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise

990:    Level: developer

992:    Notes:
993:    The 'data' and 'flag' variables are inlined, so they are not pointers.

995:    The length of the array accessed must be known.

997: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetReal()`,
998:           `PetscObjectComposedDataGetIntstar()`, `PetscObjectComposedDataSetIntstar()`, `PetscObject`,
999:           `PetscObjectCompose()`, `PetscObjectQuery()`
1000: M*/
1001: #define PetscObjectComposedDataGetInt(obj, id, data, flag) (((obj)->intcomposedstate ? (data = (obj)->intcomposeddata[id], flag = (PetscBool)((obj)->intcomposedstate[id] == (obj)->state)) : (flag = PETSC_FALSE)), PETSC_SUCCESS)

1003: /*MC
1004:    PetscObjectComposedDataSetIntstar - attach an integer array data to a `PetscObject` that may be accessed with `PetscObjectComposedDataGetIntstar()`

1006:    Synopsis:
1007:    #include "petsc/private/petscimpl.h"
1008:    PetscErrorCode PetscObjectComposedDataSetIntstar(PetscObject obj,int id,int *data)

1010:    Not collective

1012:    Input parameters:
1013: +  obj - the object to which data is to be attached
1014: .  id - the identifier for the data
1015: -  data - the data to  be attached

1017:    Notes:
1018:    The data identifier can be determined through a call to `PetscObjectComposedDataRegister()`

1020:    The length of the array accessed must be known, it is not available through this API.

1022:    Level: developer

1024: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetReal()`,
1025:           `PetscObjectComposedDataGetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1026:           `PetscObjectCompose()`, `PetscObjectQuery()`
1027: M*/
1028: #define PetscObjectComposedDataSetIntstar(obj, id, data) \
1029:   ((PetscErrorCode)((((obj)->intstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseIntstar(obj)) || ((obj)->intstarcomposeddata[id] = data, (obj)->intstarcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))

1031: /*MC
1032:    PetscObjectComposedDataGetIntstar - retrieve integer array data attached to an object with `PetscObjectComposedDataSetIntstar()`

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

1038:    Not collective

1040:    Input parameters:
1041: +  obj - the object from which data is to be retrieved
1042: -  id - the identifier for the data

1044:    Output parameters:
1045: +  data - the data to be retrieved
1046: -  flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise

1048:    Notes:
1049:    The 'data' and 'flag' variables are inlined, so they are not pointers.

1051:    The length of the array accessed must be known, it is not available through this API.

1053:    Level: developer

1055: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetReal()`,
1056:           `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1057:           `PetscObjectCompose()`, `PetscObjectQuery()`
1058: M*/
1059: #define PetscObjectComposedDataGetIntstar(obj, id, data, flag) \
1060:   ((PetscErrorCode)(((obj)->intstarcomposedstate ? (data = (obj)->intstarcomposeddata[id], flag = (PetscBool)((obj)->intstarcomposedstate[id] == (obj)->state)) : (flag = PETSC_FALSE)), PETSC_SUCCESS))

1062: /*MC
1063:    PetscObjectComposedDataSetReal - attach real data to a `PetscObject` that may be accessed with `PetscObjectComposedDataGetReal()`

1065:    Synopsis:
1066:    #include "petsc/private/petscimpl.h"
1067:    PetscErrorCode PetscObjectComposedDataSetReal(PetscObject obj,int id,PetscReal data)

1069:    Not collective

1071:    Input parameters:
1072: +  obj - the object to which data is to be attached
1073: .  id - the identifier for the data
1074: -  data - the data to  be attached

1076:    Note:
1077:    The data identifier can be determined through a call to  `PetscObjectComposedDataRegister()`

1079:    Level: developer

1081: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1082:           `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1083:           `PetscObjectCompose()`, `PetscObjectQuery()`
1084: M*/
1085: #define PetscObjectComposedDataSetReal(obj, id, data) \
1086:   ((PetscErrorCode)((((obj)->real_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseReal(obj)) || ((obj)->realcomposeddata[id] = data, (obj)->realcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))

1088: /*MC
1089:    PetscObjectComposedDataGetReal - retrieve real data attached to an object set with `PetscObjectComposedDataSetReal()`

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

1095:    Not collective

1097:    Input parameters:
1098: +  obj - the object from which data is to be retrieved
1099: -  id - the identifier for the data

1101:    Output parameters:
1102: +  data - the data to be retrieved
1103: -  flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise

1105:    Note:
1106:    The 'data' and 'flag' variables are inlined, so they are not pointers.

1108:    Level: developer

1110: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataSetIntstar()`,
1111:           `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1112:           `PetscObjectCompose()`, `PetscObjectQuery()`
1113: M*/
1114: #define PetscObjectComposedDataGetReal(obj, id, data, flag) ((PetscErrorCode)(((obj)->realcomposedstate ? (data = (obj)->realcomposeddata[id], flag = (PetscBool)((obj)->realcomposedstate[id] == (obj)->state)) : (flag = PETSC_FALSE)), PETSC_SUCCESS))

1116: /*MC
1117:    PetscObjectComposedDataSetRealstar - attach real array data to a `PetscObject` that may be retrieved with `PetscObjectComposedDataGetRealstar()`

1119:    Synopsis:
1120:    #include "petsc/private/petscimpl.h"
1121:    PetscErrorCode PetscObjectComposedDataSetRealstar(PetscObject obj,int id,PetscReal *data)

1123:    Not collective

1125:    Input parameters:
1126: +  obj - the object to which data is to be attached
1127: .  id - the identifier for the data
1128: -  data - the data to  be attached

1130:    Notes:
1131:    The data identifier can be determined through a call to `PetscObjectComposedDataRegister()`

1133:    The length of the array accessed must be known, it is not available through this API.

1135:    Level: developer

1137: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1138:           `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1139:           `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataGetRealstar()`
1140: M*/
1141: #define PetscObjectComposedDataSetRealstar(obj, id, data) \
1142:   ((PetscErrorCode)((((obj)->realstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseRealstar(obj)) || ((obj)->realstarcomposeddata[id] = data, (obj)->realstarcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))

1144: /*MC
1145:    PetscObjectComposedDataGetRealstar - retrieve real array data attached to an object with `PetscObjectComposedDataSetRealstar()`

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

1151:    Not collective

1153:    Input parameters:
1154: +  obj - the object from which data is to be retrieved
1155: -  id - the identifier for the data

1157:    Output parameters:
1158: +  data - the data to be retrieved
1159: -  flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise

1161:    Notes:
1162:    The 'data' and 'flag' variables are inlined, so they are not pointers.

1164:    The length of the array accessed must be known, it is not available through this API.

1166:    Level: developer

1168: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1169:           `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1170:           `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataSetRealstar()`
1171: M*/
1172: #define PetscObjectComposedDataGetRealstar(obj, id, data, flag) \
1173:   ((PetscErrorCode)(((obj)->realstarcomposedstate ? (data = (obj)->realstarcomposeddata[id], flag = (PetscBool)((obj)->realstarcomposedstate[id] == (obj)->state)) : (flag = PETSC_FALSE)), PETSC_SUCCESS))

1175: /*MC
1176:    PetscObjectComposedDataSetScalar - attach scalar data to a PetscObject that may be retrieved with `PetscObjectComposedDataGetScalar()`

1178:    Synopsis:
1179:    #include "petsc/private/petscimpl.h"
1180:    PetscErrorCode PetscObjectComposedDataSetScalar(PetscObject obj,int id,PetscScalar data)

1182:    Not collective

1184:    Input parameters:
1185: +  obj - the object to which data is to be attached
1186: .  id - the identifier for the data
1187: -  data - the data to  be attached

1189:    Note:
1190:    The data identifier can be determined through a call to `PetscObjectComposedDataRegister()`

1192:    Level: developer

1194: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1195:           `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1196:           `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataSetRealstar()`, `PetscObjectComposedDataGetScalar()`
1197: M*/
1198: #if defined(PETSC_USE_COMPLEX)
1199:   #define PetscObjectComposedDataSetScalar(obj, id, data) \
1200:     ((PetscErrorCode)((((obj)->scalar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseScalar(obj)) || ((obj)->scalarcomposeddata[id] = data, (obj)->scalarcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))
1201: #else
1202:   #define PetscObjectComposedDataSetScalar(obj, id, data) PetscObjectComposedDataSetReal(obj, id, data)
1203: #endif
1204: /*MC
1205:    PetscObjectComposedDataGetScalar - retrieve scalar data attached to an object that was set with `PetscObjectComposedDataSetScalar()`

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

1211:    Not collective

1213:    Input parameters:
1214: +  obj - the object from which data is to be retrieved
1215: -  id - the identifier for the data

1217:    Output parameters:
1218: +  data - the data to be retrieved
1219: -  flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise

1221:    Note:
1222:    The 'data' and 'flag' variables are inlined, so they are not pointers.

1224:    Level: developer

1226: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1227:           `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1228:           `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataSetRealstar()`, `PetscObjectComposedDataSetScalar()`
1229: M*/
1230: #if defined(PETSC_USE_COMPLEX)
1231:   #define PetscObjectComposedDataGetScalar(obj, id, data, flag) \
1232:     ((PetscErrorCode)(((obj)->scalarcomposedstate ? (data = (obj)->scalarcomposeddata[id], flag = (PetscBool)((obj)->scalarcomposedstate[id] == (obj)->state)) : (flag = PETSC_FALSE)), PETSC_SUCCESS))
1233: #else
1234:   #define PetscObjectComposedDataGetScalar(obj, id, data, flag) PetscObjectComposedDataGetReal(obj, id, data, flag)
1235: #endif

1237: /*MC
1238:    PetscObjectComposedDataSetScalarstar - attach scalar array data to a `PetscObject` that may be retrieved with `PetscObjectComposedDataSetScalarstar()`

1240:    Synopsis:
1241:    #include "petsc/private/petscimpl.h"
1242:    PetscErrorCode PetscObjectComposedDataSetScalarstar(PetscObject obj,int id,PetscScalar *data)

1244:    Not collective

1246:    Input parameters:
1247: +  obj - the object to which data is to be attached
1248: .  id - the identifier for the data
1249: -  data - the data to  be attached

1251:    Notes:
1252:    The data identifier can be determined through a call to `PetscObjectComposedDataRegister()`

1254:    The length of the array accessed must be known, it is not available through this API.

1256:    Level: developer

1258: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1259:           `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1260:           `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataSetRealstar()`, `PetscObjectComposedDataGetScalarstar()`
1261: M*/
1262: #if defined(PETSC_USE_COMPLEX)
1263:   #define PetscObjectComposedDataSetScalarstar(obj, id, data) \
1264:     ((PetscErrorCode)((((obj)->scalarstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseScalarstar(obj)) || ((obj)->scalarstarcomposeddata[id] = data, (obj)->scalarstarcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))
1265: #else
1266:   #define PetscObjectComposedDataSetScalarstar(obj, id, data) PetscObjectComposedDataSetRealstar(obj, id, data)
1267: #endif
1268: /*MC
1269:    PetscObjectComposedDataGetScalarstar - retrieve scalar array data set with `PetscObjectComposedDataSetScalarstar()`
1270:    attached to an object

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

1276:    Not collective

1278:    Input parameters:
1279: +  obj - the object from which data is to be retrieved
1280: -  id - the identifier for the data

1282:    Output parameters:
1283: +  data - the data to be retrieved
1284: -  flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise

1286:    Notes:
1287:    The 'data' and 'flag' variables are inlined, so they are not pointers.

1289:    The length of the array accessed must be known, it is not available through this API.

1291:    Level: developer

1293: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1294:           `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1295:           `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataSetRealstar()`, `PetscObjectComposedDataSetScalarstar()`
1296: M*/
1297: #if defined(PETSC_USE_COMPLEX)
1298:   #define PetscObjectComposedDataGetScalarstar(obj, id, data, flag) \
1299:     ((PetscErrorCode)(((obj)->scalarstarcomposedstate ? (data = (obj)->scalarstarcomposeddata[id], flag = (PetscBool)((obj)->scalarstarcomposedstate[id] == (obj)->state)) : (flag = PETSC_FALSE)), PETSC_SUCCESS))
1300: #else
1301:   #define PetscObjectComposedDataGetScalarstar(obj, id, data, flag) PetscObjectComposedDataGetRealstar(obj, id, data, flag)
1302: #endif

1304: PETSC_EXTERN PetscMPIInt Petsc_Counter_keyval;
1305: PETSC_EXTERN PetscMPIInt Petsc_InnerComm_keyval;
1306: PETSC_EXTERN PetscMPIInt Petsc_OuterComm_keyval;
1307: PETSC_EXTERN PetscMPIInt Petsc_Seq_keyval;
1308: PETSC_EXTERN PetscMPIInt Petsc_ShmComm_keyval;
1309: PETSC_EXTERN PetscMPIInt Petsc_CreationIdx_keyval;
1310: PETSC_EXTERN PetscMPIInt Petsc_Garbage_HMap_keyval;

1312: struct PetscCommStash {
1313:   struct PetscCommStash *next;
1314:   MPI_Comm               comm;
1315: };

1317: /*
1318:   PETSc communicators have this attribute, see
1319:   PetscCommDuplicate(), PetscCommDestroy(), PetscCommGetNewTag(), PetscObjectGetName()
1320: */
1321: typedef struct {
1322:   PetscMPIInt            tag;       /* next free tag value */
1323:   PetscInt               refcount;  /* number of references, communicator can be freed when this reaches 0 */
1324:   PetscInt               namecount; /* used to generate the next name, as in Vec_0, Mat_1, ... */
1325:   PetscMPIInt           *iflags;    /* length of comm size, shared by all calls to PetscCommBuildTwoSided_Allreduce/RedScatter on this comm */
1326:   struct PetscCommStash *comms;     /* communicators available for PETSc to pass off to other packages */
1327: } PetscCommCounter;

1329: typedef enum {
1330:   STATE_BEGIN,
1331:   STATE_PENDING,
1332:   STATE_END
1333: } SRState;

1335: typedef enum {
1336:   PETSC_SR_REDUCE_SUM = 0,
1337:   PETSC_SR_REDUCE_MAX = 1,
1338:   PETSC_SR_REDUCE_MIN = 2
1339: } PetscSRReductionType;

1341: typedef struct {
1342:   MPI_Comm     comm;
1343:   MPI_Request  request;
1344:   PetscBool    mix;
1345:   PetscBool    async;
1346:   PetscScalar *lvalues;    /* this are the reduced values before call to MPI_Allreduce() */
1347:   PetscScalar *gvalues;    /* values after call to MPI_Allreduce() */
1348:   void       **invecs;     /* for debugging only, vector/memory used with each op */
1349:   PetscInt    *reducetype; /* is particular value to be summed or maxed? */
1350:   struct {
1351:     PetscScalar v;
1352:     PetscInt    i;
1353:   }       *lvalues_mix, *gvalues_mix; /* used when mixing reduce operations */
1354:   SRState  state;                     /* are we calling xxxBegin() or xxxEnd()? */
1355:   PetscInt maxops;                    /* total amount of space we have for requests */
1356:   PetscInt numopsbegin;               /* number of requests that have been queued in */
1357:   PetscInt numopsend;                 /* number of requests that have been gotten by user */
1358: } PetscSplitReduction;

1360: PETSC_EXTERN PetscErrorCode PetscSplitReductionGet(MPI_Comm, PetscSplitReduction **);
1361: PETSC_EXTERN PetscErrorCode PetscSplitReductionEnd(PetscSplitReduction *);
1362: PETSC_EXTERN PetscErrorCode PetscSplitReductionExtend(PetscSplitReduction *);

1364: #if defined(PETSC_HAVE_THREADSAFETY)
1365:   #if defined(PETSC_HAVE_CONCURRENCYKIT)
1366:     #if defined(__cplusplus)
1367: /*  CK does not have extern "C" protection in their include files */
1368: extern "C" {
1369:     #endif
1370:     #include <ck_spinlock.h>
1371:     #if defined(__cplusplus)
1372: }
1373:     #endif
1374: typedef ck_spinlock_t PetscSpinlock;

1376: static inline PetscErrorCode PetscSpinlockCreate(PetscSpinlock *ck_spinlock)
1377: {
1378:   ck_spinlock_init(ck_spinlock);
1379:   return PETSC_SUCCESS;
1380: }
1381: static inline PetscErrorCode PetscSpinlockLock(PetscSpinlock *ck_spinlock)
1382: {
1383:   ck_spinlock_lock(ck_spinlock);
1384:   return PETSC_SUCCESS;
1385: }
1386: static inline PetscErrorCode PetscSpinlockUnlock(PetscSpinlock *ck_spinlock)
1387: {
1388:   ck_spinlock_unlock(ck_spinlock);
1389:   return PETSC_SUCCESS;
1390: }
1391: static inline PetscErrorCode PetscSpinlockDestroy(PetscSpinlock *ck_spinlock)
1392: {
1393:   return PETSC_SUCCESS;
1394: }
1395:   #elif (defined(__cplusplus) && defined(PETSC_HAVE_CXX_ATOMIC)) || (!defined(__cplusplus) && defined(PETSC_HAVE_STDATOMIC_H))
1396:     #if defined(__cplusplus)
1397:       #include <atomic>
1398:       #define petsc_atomic_flag                 std::atomic_flag
1399:       #define petsc_atomic_flag_test_and_set(p) std::atomic_flag_test_and_set_explicit(p, std::memory_order_relaxed)
1400:       #define petsc_atomic_flag_clear(p)        std::atomic_flag_clear_explicit(p, std::memory_order_relaxed)
1401:     #else
1402:       #include <stdatomic.h>
1403:       #define petsc_atomic_flag                 atomic_flag
1404:       #define petsc_atomic_flag_test_and_set(p) atomic_flag_test_and_set_explicit(p, memory_order_relaxed)
1405:       #define petsc_atomic_flag_clear(p)        atomic_flag_clear_explicit(p, memory_order_relaxed)
1406:     #endif

1408: typedef petsc_atomic_flag PetscSpinlock;

1410: static inline PetscErrorCode PetscSpinlockCreate(PetscSpinlock *spinlock)
1411: {
1412:   petsc_atomic_flag_clear(spinlock);
1413:   return PETSC_SUCCESS;
1414: }
1415: static inline PetscErrorCode PetscSpinlockLock(PetscSpinlock *spinlock)
1416: {
1417:   do {
1418:   } while (petsc_atomic_flag_test_and_set(spinlock));
1419:   return PETSC_SUCCESS;
1420: }
1421: static inline PetscErrorCode PetscSpinlockUnlock(PetscSpinlock *spinlock)
1422: {
1423:   petsc_atomic_flag_clear(spinlock);
1424:   return PETSC_SUCCESS;
1425: }
1426: static inline PetscErrorCode PetscSpinlockDestroy(PetscSpinlock *spinlock)
1427: {
1428:   return PETSC_SUCCESS;
1429: }
1430:     #undef petsc_atomic_flag_test_and_set
1431:     #undef petsc_atomic_flag_clear
1432:     #undef petsc_atomic_flag

1434:   #elif defined(PETSC_HAVE_OPENMP)

1436:     #include <omp.h>
1437: typedef omp_lock_t PetscSpinlock;

1439: static inline PetscErrorCode PetscSpinlockCreate(PetscSpinlock *omp_lock)
1440: {
1441:   omp_init_lock(omp_lock);
1442:   return PETSC_SUCCESS;
1443: }
1444: static inline PetscErrorCode PetscSpinlockLock(PetscSpinlock *omp_lock)
1445: {
1446:   omp_set_lock(omp_lock);
1447:   return PETSC_SUCCESS;
1448: }
1449: static inline PetscErrorCode PetscSpinlockUnlock(PetscSpinlock *omp_lock)
1450: {
1451:   omp_unset_lock(omp_lock);
1452:   return PETSC_SUCCESS;
1453: }
1454: static inline PetscErrorCode PetscSpinlockDestroy(PetscSpinlock *omp_lock)
1455: {
1456:   omp_destroy_lock(omp_lock);
1457:   return PETSC_SUCCESS;
1458: }
1459:   #else
1460:     #if defined(__cplusplus)
1461:       #error "Thread safety requires either --download-concurrencykit, std::atomic, or --with-openmp"
1462:     #else
1463:       #error "Thread safety requires either --download-concurrencykit, stdatomic.h, or --with-openmp"
1464:     #endif
1465:   #endif

1467: #else
1468: typedef int PetscSpinlock;
1469:   #define PetscSpinlockCreate(a)  PETSC_SUCCESS
1470:   #define PetscSpinlockLock(a)    PETSC_SUCCESS
1471:   #define PetscSpinlockUnlock(a)  PETSC_SUCCESS
1472:   #define PetscSpinlockDestroy(a) PETSC_SUCCESS
1473: #endif

1475: #if defined(PETSC_HAVE_THREADSAFETY)
1476: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockOpen;
1477: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockStdout;
1478: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockStderr;
1479: PETSC_INTERN PetscSpinlock PetscCommSpinLock;
1480: #endif

1482: PETSC_EXTERN PetscLogEvent PETSC_Barrier;
1483: PETSC_EXTERN PetscLogEvent PETSC_BuildTwoSided;
1484: PETSC_EXTERN PetscLogEvent PETSC_BuildTwoSidedF;
1485: PETSC_EXTERN PetscBool     use_gpu_aware_mpi;
1486: PETSC_EXTERN PetscBool     PetscPrintFunctionList;

1488: #if defined(PETSC_HAVE_ADIOS)
1489: PETSC_EXTERN int64_t Petsc_adios_group;
1490: #endif

1492: #if defined(PETSC_HAVE_KOKKOS)
1493: PETSC_INTERN PetscBool      PetscBeganKokkos;
1494: PETSC_EXTERN PetscBool      PetscKokkosInitialized;
1495: PETSC_INTERN PetscErrorCode PetscKokkosIsInitialized_Private(PetscBool *);
1496: PETSC_INTERN PetscErrorCode PetscKokkosFinalize_Private(void);
1497: #endif

1499: #if defined(PETSC_HAVE_OPENMP)
1500: PETSC_EXTERN PetscInt PetscNumOMPThreads;
1501: #endif

1503: #endif /* PETSCIMPL_H */