Actual source code: petscimpl.h
1: /*
2: Defines the basic header of all PETSc objects.
3: */
4: #pragma once
5: #include <petscsys.h>
7: /* SUBMANSEC = Sys */
9: #if defined(PETSC_CLANG_STATIC_ANALYZER)
10: #define PetscDisableStaticAnalyzerForExpressionUnderstandingThatThisIsDangerousAndBugprone(...)
11: #else
12: #define PetscDisableStaticAnalyzerForExpressionUnderstandingThatThisIsDangerousAndBugprone(...) __VA_ARGS__
13: #endif
15: #if PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)
16: PETSC_INTERN PetscErrorCode PetscStackSetCheck(PetscBool);
17: PETSC_INTERN PetscErrorCode PetscStackView(FILE *);
18: PETSC_INTERN PetscErrorCode PetscStackReset(void);
19: PETSC_INTERN PetscErrorCode PetscStackCopy(PetscStack *, PetscStack *);
20: PETSC_INTERN PetscErrorCode PetscStackPrint(PetscStack *, FILE *);
21: #else
22: #define PetscStackSetCheck(check) PETSC_SUCCESS
23: #define PetscStackView(file) PETSC_SUCCESS
24: #define PetscStackReset() PETSC_SUCCESS
25: #define PetscStackCopy(stackin, stackout) PETSC_SUCCESS
26: #define PetscStackPrint(stack, file) PETSC_SUCCESS
27: #endif
29: /* These are used internally by PETSc ASCII IO routines*/
30: #include <stdarg.h>
31: PETSC_EXTERN PetscErrorCode PetscVFPrintfDefault(FILE *, const char[], va_list);
33: /*
34: All major PETSc data structures have a common core; this is defined
35: below by PETSCHEADER.
37: PetscHeaderCreate() should be used whenever creating a PETSc structure.
38: */
40: /*
41: PetscOps: structure of core operations that all PETSc objects support.
43: view() - Is the routine for viewing the entire PETSc object; for
44: example, MatView() is the general matrix viewing routine.
45: This is used by PetscObjectView((PetscObject)obj) to allow
46: viewing any PETSc object.
47: destroy() - Is the routine for destroying the entire PETSc object;
48: for example,MatDestroy() is the general matrix
49: destruction routine.
50: This is used by PetscObjectDestroy((PetscObject*)&obj) to allow
51: destroying any PETSc object.
52: */
54: typedef struct {
55: PetscErrorCode (*view)(PetscObject, PetscViewer);
56: PetscErrorCode (*destroy)(PetscObject *);
57: } PetscOps;
59: typedef enum {
60: PETSC_FORTRAN_CALLBACK_CLASS,
61: PETSC_FORTRAN_CALLBACK_SUBTYPE,
62: PETSC_FORTRAN_CALLBACK_MAXTYPE
63: } PetscFortranCallbackType;
64: typedef size_t PetscFortranCallbackId;
65: #define PETSC_SMALLEST_FORTRAN_CALLBACK ((PetscFortranCallbackId)1000)
66: PETSC_EXTERN PetscErrorCode PetscFortranCallbackRegister(PetscClassId, const char *, PetscFortranCallbackId *);
67: PETSC_EXTERN PetscErrorCode PetscFortranCallbackGetSizes(PetscClassId, PetscFortranCallbackId *, PetscFortranCallbackId *);
69: typedef struct {
70: void (*func)(void);
71: void *ctx;
72: } PetscFortranCallback;
74: /*
75: All PETSc objects begin with the fields defined in PETSCHEADER.
76: The PetscObject is a way of examining these fields regardless of
77: the specific object. In C++ this could be a base abstract class
78: from which all objects are derived.
79: */
80: #define PETSC_MAX_OPTIONS_HANDLER 5
81: typedef struct _p_PetscObject {
82: PetscOps bops[1];
83: PetscClassId classid;
84: MPI_Comm comm;
85: PetscObjectId id; /* this is used to compare object for identity that may no longer exist since memory addresses get recycled for new objects */
86: PetscInt refct;
87: PetscErrorCode (*non_cyclic_references)(PetscObject, PetscInt *);
88: PetscInt64 cidx;
89: PetscMPIInt tag;
90: PetscFunctionList qlist;
91: PetscObjectList olist;
92: char *class_name; /* for example, "Vec" */
93: char *description;
94: char *mansec;
95: char *type_name; /* this is the subclass, for example VECSEQ which equals "seq" */
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: #if PetscDefined(USE_COMPLEX)
108: PetscInt scalar_idmax, scalarstar_idmax;
109: PetscObjectState *scalarcomposedstate, *scalarstarcomposedstate;
110: PetscScalar *scalarcomposeddata, **scalarstarcomposeddata;
111: #endif
112: void (**fortran_func_pointers)(void); /* used by Fortran interface functions to stash user provided Fortran functions */
113: PetscFortranCallbackId num_fortran_func_pointers; /* number of Fortran function pointers allocated */
114: PetscFortranCallback *fortrancallback[PETSC_FORTRAN_CALLBACK_MAXTYPE];
115: PetscFortranCallbackId num_fortrancallback[PETSC_FORTRAN_CALLBACK_MAXTYPE];
116: void *python_context;
117: PetscErrorCode (*python_destroy)(void *);
119: PetscInt noptionhandler;
120: PetscErrorCode (*optionhandler[PETSC_MAX_OPTIONS_HANDLER])(PetscObject, PetscOptionItems *, void *);
121: PetscErrorCode (*optiondestroy[PETSC_MAX_OPTIONS_HANDLER])(PetscObject, void *);
122: void *optionctx[PETSC_MAX_OPTIONS_HANDLER];
123: #if defined(PETSC_HAVE_SAWS)
124: PetscBool amsmem; /* if PETSC_TRUE then this object is registered with SAWs and visible to clients */
125: PetscBool amspublishblock; /* if PETSC_TRUE and publishing objects then will block at PetscObjectSAWsBlock() */
126: #endif
127: PetscOptions options; /* options database used, NULL means default */
128: PetscBool optionsprinted;
129: PetscBool donotPetscObjectPrintClassNamePrefixType;
130: } _p_PetscObject;
132: #define PETSCHEADER(ObjectOps) \
133: _p_PetscObject hdr; \
134: ObjectOps ops[1]
136: #define PETSCFREEDHEADER -1
138: /*S
139: PetscObjectDestroyFn - A prototype of a function that can destroy a `PetscObject`
141: Calling Sequence:
142: . obj - the `PetscObject` to destroy
144: Level: beginner
146: Note:
147: The deprecated `PetscObjectDestroyFunction` works as a replacement for `PetscObjectDestroyFn` *.
149: .seealso: `PetscObject`, `PetscObjectDestroy()`
150: S*/
151: PETSC_EXTERN_TYPEDEF typedef PetscErrorCode(PetscObjectDestroyFn)(PetscObject *obj);
153: PETSC_EXTERN_TYPEDEF typedef PetscObjectDestroyFn *PetscObjectDestroyFunction;
155: /*S
156: PetscObjectViewFn - A prototype of a function that can view a `PetscObject`
158: Calling Sequence:
159: + obj - the `PetscObject` to view
160: - v - the viewer
162: Level: beginner
164: Note:
165: The deprecated `PetscObjectViewFunction` works as a replacement for `PetscObjectViewFn` *.
167: .seealso: `PetscObject`, `PetscObjectDestroy()`, `PetscViewer`, `PetscObjectView()`
168: S*/
169: PETSC_EXTERN_TYPEDEF typedef PetscErrorCode(PetscObjectViewFn)(PetscObject obj, PetscViewer v);
171: PETSC_EXTERN_TYPEDEF typedef PetscObjectViewFn *PetscObjectViewFunction;
173: /*MC
174: PetscHeaderCreate - Creates a raw PETSc object of a particular class
176: Synopsis:
177: #include <petsc/private/petscimpl.h>
178: PetscErrorCode PetscHeaderCreate(PetscObject h, PetscClassId classid, const char class_name[], const char descr[], const char mansec[], MPI_Comm comm, PetscObjectDestroyFn * destroy, PetscObjectViewFn * view)
180: Input Parameters:
181: + classid - The classid associated with this object (for example `VEC_CLASSID`)
182: . class_name - String name of class; should be static (for example "Vec"), may be
183: `PETSC_NULLPTR`
184: . descr - String containing short description; should be static (for example "Vector"),
185: may be `PETSC_NULLPTR`
186: . mansec - String indicating section in manual pages; should be static (for example "Vec"),
187: may be `PETSC_NULLPTR`
188: . comm - The MPI Communicator
189: . destroy - The destroy routine for this object (for example `VecDestroy()`)
190: - view - The view routine for this object (for example `VecView()`), may be
191: `PETSC_NULLPTR`
193: Output Parameter:
194: . h - The newly created `PetscObject`
196: Level: developer
198: Notes:
199: Can only be used to create a `PetscObject`. A `PetscObject` is defined as a pointer to a
200: C/C++ structure which satisfies all of the following\:
202: 1. The first member of the structure must be a `_p_PetscObject`.
203: 2. C++ structures must be "Standard Layout". Generally speaking a standard layout class\:
204: - Has no virtual functions or base classes.
205: - Has only standard layout non-static members (if any).
206: - Has only standard layout base classes (if any).
208: See https://en.cppreference.com/w/cpp/language/classes#Standard-layout_class for further
209: information.
211: Example Usage:
212: Existing `PetscObject`s may be easily created as shown. Unless otherwise stated, a particular
213: objects `destroy` and `view` functions are exactly `<OBJECT_TYPE>Destroy()` and
214: `<OBJECT_TYPE>View()`.
215: .vb
216: Vec v;
218: PetscHeaderCreate(v, VEC_CLASSID, "Vec", "A distributed vector class", "Vec", PETSC_COMM_WORLD, VecDestroy, VecView);
219: .ve
221: It is possible to create custom `PetscObject`s, note however that they must abide by the
222: restrictions set forth above.
223: .vb
224: // OK, first member of C structure is _p_PetscObject
225: struct MyCPetscObject_s
226: {
227: _p_PetscObject header;
228: int some_data;
229: };
230: typedef struct *MyCPetscObject_s MyCPetscObject;
232: PetscErrorCode MyObjectDestroy(MyObject *);
233: PetscErrorCode MyObjectView(MyObject);
235: MyCPetscObject obj;
237: // assume MY_PETSC_CLASSID is already registered
238: PetscHeaderCreate(obj, MY_PETSC_CLASSID, "MyObject", "A custom PetscObject", PETSC_NULLPTR, PETSC_COMM_SELF, MyObjectDestroy, MyObjectView);
240: // OK, only destroy function must be given, all others may be NULL
241: PetscHeaderCreate(obj, MY_PETSC_CLASSID, PETSC_NULLPTR, PETSC_NULLPTR, PETSC_NULLPTR, PETSC_COMM_SELF, MyObjectDestroy, PETSC_NULLPTR);
243: // ERROR must be a single-level pointer
244: PetscHeaderCreate(&obj, ...);
245: .ve
247: Illustrating proper construction from C++\:
248: .vb
249: // ERROR, class is not standard layout, first member must be publicly accessible
250: class BadCppPetscObject
251: {
252: _p_PetscObject header;
253: };
255: // ERROR, class is not standard layout, has a virtual function and virtual inheritance
256: class BadCppPetscObject2 : virtual BaseClass
257: {
258: public:
259: _p_PetscObject header;
261: virtual void foo();
262: };
264: // ERROR, class is not standard layout! Has non-standard layout member
265: class BadCppPetscObject2
266: {
267: public:
268: _p_PetscObject header;
269: BadCppPetscObject non_standard_layout;
270: };
272: // OK, class is standard layout!
273: class GoodCppPetscObject;
274: using MyCppObject = GoodCppPetscObject *;
276: // OK, non-virtual inheritance of other standard layout class does not affect layout
277: class GoodCppPetscObject : StandardLayoutClass
278: {
279: public:
280: // OK, non standard layout member is static, does not affect layout
281: static BadCppPetscObject non_standard_layout;
283: // OK, first non-static member is _p_PetscObject
284: _p_PetscObject header;
286: // OK, non-virtual member functions do not affect class layout
287: void foo();
289: // OK, may use "member" functions for destroy and view so long as they are static
290: static PetscErrorCode destroy(MyCppObject *);
291: static PetscErrorCode view(MyCppObject);
292: };
294: // OK, usage via pointer
295: MyObject obj;
297: PetscHeaderCreate(obj, MY_PETSC_CLASSID, "MyObject", "A custom C++ PetscObject", nullptr, PETSC_COMM_SELF, GoodCppPetscObject::destroy, GoodCppPetscObject::view);
298: .ve
300: .seealso: `PetscObject`, `PetscHeaderDestroy()`, `PetscClassIdRegister()`
301: M*/
302: #define PetscHeaderCreate(h, classid, class_name, descr, mansec, comm, destroy, view) \
303: PetscHeaderCreate_Function(PetscNew(&(h)), (PetscObject *)&(h), (classid), (class_name), (descr), (mansec), (comm), (PetscObjectDestroyFn *)(destroy), (PetscObjectViewFn *)(view))
305: PETSC_EXTERN PetscErrorCode PetscHeaderCreate_Function(PetscErrorCode, PetscObject *, PetscClassId, const char[], const char[], const char[], MPI_Comm, PetscObjectDestroyFn *, PetscObjectViewFn *);
306: PETSC_EXTERN PetscErrorCode PetscHeaderCreate_Private(PetscObject, PetscClassId, const char[], const char[], const char[], MPI_Comm, PetscObjectDestroyFn *, PetscObjectViewFn *);
307: PETSC_EXTERN PetscErrorCode PetscHeaderDestroy_Function(PetscObject *);
308: PETSC_EXTERN PetscErrorCode PetscComposedQuantitiesDestroy(PetscObject obj);
309: PETSC_INTERN PetscObjectId PetscObjectNewId_Internal(void);
311: /*MC
312: PetscHeaderDestroy - Final step in destroying a `PetscObject`
314: Synopsis:
315: #include <petsc/private/petscimpl.h>
316: PetscErrorCode PetscHeaderDestroy(PetscObject *obj)
318: Input Parameter:
319: . h - A pointer to the header created with `PetscHeaderCreate()`
321: Level: developer
323: Notes:
324: `h` is freed and set to `PETSC_NULLPTR` when this routine returns.
326: Example Usage:
327: .vb
328: PetscObject obj;
330: PetscHeaderCreate(obj, ...);
331: // use obj...
333: // note pointer to obj is used
334: PetscHeaderDestroy(&obj);
335: .ve
337: Note that this routine is the _last_ step when destroying higher-level `PetscObject`s as it
338: deallocates the memory for the structure itself\:
339: .vb
340: typedef struct MyPetscObject_s *MyPetscObject;
341: struct MyPetscObject_s
342: {
343: _p_PetscObject hdr;
344: PetscInt *foo;
345: PetscScalar *bar;
346: };
348: // assume obj is created/initialized elsewhere...
349: MyPetscObject obj;
351: // OK, should dispose of all dynamically allocated resources before calling
352: // PetscHeaderDestroy()
353: PetscFree(obj->foo);
355: // OK, dispose of obj
356: PetscHeaderDestroy(&obj);
358: // ERROR, obj points to NULL here, accessing obj->bar may result in segmentation violation!
359: // obj->bar is potentially leaked!
360: PetscFree(obj->bar);
361: .ve
363: .seealso: `PetscObject`, `PetscHeaderCreate()`
364: M*/
365: #define PetscHeaderDestroy(h) PetscHeaderDestroy_Function((PetscObject *)h)
367: PETSC_EXTERN PetscErrorCode PetscHeaderDestroy_Private(PetscObject, PetscBool);
368: PETSC_INTERN PetscErrorCode PetscHeaderDestroy_Private_Unlogged(PetscObject, PetscBool);
369: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode PetscHeaderReset_Internal(PetscObject);
370: PETSC_EXTERN PetscErrorCode PetscObjectCopyFortranFunctionPointers(PetscObject, PetscObject);
371: PETSC_EXTERN PetscErrorCode PetscObjectSetFortranCallback(PetscObject, PetscFortranCallbackType, PetscFortranCallbackId *, void (*)(void), void *ctx);
372: PETSC_EXTERN PetscErrorCode PetscObjectGetFortranCallback(PetscObject, PetscFortranCallbackType, PetscFortranCallbackId, void (**)(void), void **ctx);
374: PETSC_INTERN PetscErrorCode PetscCitationsInitialize(void);
375: PETSC_INTERN PetscErrorCode PetscFreeMPIResources(void);
376: PETSC_INTERN PetscErrorCode PetscOptionsHasHelpIntro_Internal(PetscOptions, PetscBool *);
378: /* Code shared between C and Fortran */
379: PETSC_INTERN PetscErrorCode PetscInitialize_Common(const char *, const char *, const char *, PetscBool, PetscBool, PetscInt);
381: #if PetscDefined(HAVE_SETJMP_H)
382: PETSC_EXTERN PetscBool PetscCheckPointer(const void *, PetscDataType);
383: #else
384: #define PetscCheckPointer(ptr, data_type) (ptr ? PETSC_TRUE : PETSC_FALSE)
385: #endif
387: #if defined(PETSC_CLANG_STATIC_ANALYZER)
388: template <typename T>
390: template <typename T>
392: template <typename T>
394: template <typename T>
395: extern void PetscAssertPointer(T, int)
396: {
397: }
398: template <typename T>
400: #else
401: // Macros to test if a PETSc object is valid and if pointers are valid
402: #if PetscDefined(USE_DEBUG)
403: /* This check is for subtype methods such as DMDAGetCorners() that do not use the PetscTryMethod() or PetscUseMethod() paradigm */
405: do { \
406: PetscBool _7_same; \
408: PetscCall(PetscObjectTypeCompare((PetscObject)(h), t, &_7_same)); \
409: 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); \
410: } while (0)
412: #define PetscAssertPointer_Internal(ptr, arg, ptype, ptrtype) \
413: do { \
414: PetscCheck(ptr, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Null Pointer: Parameter # %d", arg); \
415: PetscCheck(PetscCheckPointer(ptr, ptype), PETSC_COMM_SELF, PETSC_ERR_ARG_BADPTR, "Invalid Pointer to %s: Argument '" PetscStringize(ptr) "' (parameter # %d)", ptrtype, arg); \
416: } while (0)
419: do { \
420: PetscAssertPointer_Internal(h, arg, PETSC_OBJECT, "PetscObject"); \
421: if (((PetscObject)(h))->classid != ck) { \
422: PetscCheck(((PetscObject)(h))->classid != PETSCFREEDHEADER, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Object already free: Parameter # %d", arg); \
423: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Wrong type of object: Parameter # %d", arg); \
424: } \
425: } while (0)
428: do { \
429: PetscAssertPointer_Internal(h, arg, PETSC_OBJECT, "PetscObject"); \
430: PetscCheck(((PetscObject)(h))->classid != PETSCFREEDHEADER, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Object already free: Parameter # %d", arg); \
431: 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); \
432: } while (0)
434: #if defined(__cplusplus)
435: #include <type_traits> // std::decay
437: namespace Petsc
438: {
440: namespace util
441: {
443: template <typename T>
444: struct PetscAssertPointerImpl {
445: PETSC_NODISCARD static constexpr PetscDataType type() noexcept { return PETSC_CHAR; }
446: PETSC_NODISCARD static constexpr const char *string() noexcept { return "memory"; }
447: };
449: #define PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(T, PETSC_TYPE) \
450: template <> \
451: struct PetscAssertPointerImpl<T *> { \
452: PETSC_NODISCARD static constexpr PetscDataType type() noexcept \
453: { \
454: return PETSC_TYPE; \
455: } \
456: PETSC_NODISCARD static constexpr const char *string() noexcept \
457: { \
458: return PetscStringize(T); \
459: } \
460: }; \
461: template <> \
462: struct PetscAssertPointerImpl<const T *> : PetscAssertPointerImpl<T *> { }; \
463: template <> \
464: struct PetscAssertPointerImpl<volatile T *> : PetscAssertPointerImpl<T *> { }; \
465: template <> \
466: struct PetscAssertPointerImpl<const volatile T *> : PetscAssertPointerImpl<T *> { }
468: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(char, PETSC_CHAR);
469: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(signed char, PETSC_CHAR);
470: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(unsigned char, PETSC_CHAR);
471: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(short, PETSC_SHORT);
472: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(unsigned short, PETSC_SHORT);
473: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(PetscBool, PETSC_BOOL);
474: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(float, PETSC_FLOAT);
475: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(double, PETSC_DOUBLE);
476: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(int32_t, PETSC_INT32);
477: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(uint32_t, PETSC_INT32);
478: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(int64_t, PETSC_INT64);
479: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(uint64_t, PETSC_INT64);
480: #if defined(PETSC_HAVE_COMPLEX)
481: PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION(PetscComplex, PETSC_COMPLEX);
482: #endif
484: #undef PETSC_ASSERT_POINTER_IMPL_SPECIALIZATION
486: } // namespace util
488: } // namespace Petsc
490: #define PetscAssertPointer_PetscDataType(h) ::Petsc::util::PetscAssertPointerImpl<typename std::decay<decltype(h)>::type>::type()
491: #define PetscAssertPointer_String(h) ::Petsc::util::PetscAssertPointerImpl<typename std::decay<decltype(h)>::type>::string()
493: #elif PETSC_C_VERSION >= 11
494: #define PETSC_GENERIC_CV(type, result) type * : result, const type * : result, volatile type * : result, const volatile type * : result
496: #if PetscDefined(HAVE_COMPLEX)
497: #define PETSC_GENERIC_CV_COMPLEX(result) PETSC_GENERIC_CV(PetscComplex, result)
498: #else
499: #define PETSC_GENERIC_CV_COMPLEX(result)
500: #endif
502: #define PetscAssertPointer_PetscDataType(h) \
503: _Generic((h), \
504: default: PETSC_CHAR, \
505: PETSC_GENERIC_CV( char, PETSC_CHAR), \
506: PETSC_GENERIC_CV( signed char, PETSC_CHAR), \
507: PETSC_GENERIC_CV( unsigned char, PETSC_CHAR), \
508: PETSC_GENERIC_CV( short, PETSC_SHORT), \
509: PETSC_GENERIC_CV(unsigned short, PETSC_SHORT), \
510: PETSC_GENERIC_CV( float, PETSC_FLOAT), \
511: PETSC_GENERIC_CV( double, PETSC_DOUBLE), \
512: PETSC_GENERIC_CV( int32_t, PETSC_INT32), \
513: PETSC_GENERIC_CV( uint32_t, PETSC_INT32), \
514: PETSC_GENERIC_CV( int64_t, PETSC_INT64), \
515: PETSC_GENERIC_CV( uint64_t, PETSC_INT64), \
516: PETSC_GENERIC_CV_COMPLEX(PETSC_COMPLEX))
518: #define PETSC_GENERIC_CV_STRINGIZE(type) PETSC_GENERIC_CV(type, PetscStringize(type))
520: #if PetscDefined(HAVE_COMPLEX)
521: #define PETSC_GENERIC_CV_STRINGIZE_COMPLEX PETSC_GENERIC_CV_STRINGIZE(PetscComplex)
522: #else
523: #define PETSC_GENERIC_CV_STRINGIZE_COMPLEX
524: #endif
526: #define PetscAssertPointer_String(h) \
527: _Generic((h), \
528: default: "memory", \
529: PETSC_GENERIC_CV_STRINGIZE(char), \
530: PETSC_GENERIC_CV_STRINGIZE(signed char), \
531: PETSC_GENERIC_CV_STRINGIZE(unsigned char), \
532: PETSC_GENERIC_CV_STRINGIZE(short), \
533: PETSC_GENERIC_CV_STRINGIZE(unsigned short), \
534: PETSC_GENERIC_CV_STRINGIZE(float), \
535: PETSC_GENERIC_CV_STRINGIZE(double), \
536: PETSC_GENERIC_CV_STRINGIZE(int32_t), \
537: PETSC_GENERIC_CV_STRINGIZE(uint32_t), \
538: PETSC_GENERIC_CV_STRINGIZE(int64_t), \
539: PETSC_GENERIC_CV_STRINGIZE(uint64_t), \
540: PETSC_GENERIC_CV_STRINGIZE_COMPLEX)
541: #else // PETSC_C_VERSION >= 11 || defined(__cplusplus)
542: #define PetscAssertPointer_PetscDataType(h) PETSC_CHAR
543: #define PetscAssertPointer_String(h) "memory"
544: #endif // PETSC_C_VERSION >= 11 || defined(__cplusplus)
545: #define PetscAssertPointer(h, arg) PetscAssertPointer_Internal(h, arg, PetscAssertPointer_PetscDataType(h), PetscAssertPointer_String(h))
547: #else // PetscDefined(USE_DEBUG)
549: do { \
550: (void)(h); \
551: } while (0)
553: do { \
554: (void)(h); \
555: } while (0)
557: do { \
558: (void)(h); \
559: } while (0)
560: #define PetscAssertPointer(h, arg) \
561: do { \
562: (void)(h); \
563: } while (0)
565: do { \
566: (void)(h); \
567: } while (0)
568: #endif // PetscDefined(USE_DEBUG)
569: #endif // PETSC_CLANG_STATIC_ANALYZER
580: #define PetscSorted(n, idx, sorted) \
581: do { \
582: (sorted) = PETSC_TRUE; \
583: for (PetscInt _i_ = 1; _i_ < (n); ++_i_) { \
584: if ((idx)[_i_] < (idx)[_i_ - 1]) { \
585: (sorted) = PETSC_FALSE; \
586: break; \
587: } \
588: } \
589: } while (0)
591: #if !defined(PETSC_CLANG_STATIC_ANALYZER)
592: #if !defined(PETSC_USE_DEBUG)
594: #define PetscCheckSameType(a, arga, b, argb) \
595: do { \
596: (void)(a); \
597: (void)(b); \
598: } while (0)
599: #define PetscCheckTypeName(a, type) \
600: do { \
601: (void)(a); \
602: } while (0)
603: #define PetscCheckTypeNames(a, type1, type2) \
604: do { \
605: (void)(a); \
606: } while (0)
608: do { \
609: (void)(a); \
610: } while (0)
611: #define PetscCheckSameComm(a, arga, b, argb) \
612: do { \
613: (void)(a); \
614: (void)(b); \
615: } while (0)
616: #define PetscCheckSameTypeAndComm(a, arga, b, argb) \
617: do { \
618: (void)(a); \
619: (void)(b); \
620: } while (0)
622: do { \
623: (void)(a); \
624: (void)(b); \
625: } while (0)
627: do { \
628: (void)(a); \
629: (void)(b); \
630: } while (0)
632: do { \
633: (void)(a); \
634: (void)(b); \
635: } while (0)
637: do { \
638: (void)(a); \
639: (void)(b); \
640: } while (0)
642: do { \
643: (void)(a); \
644: (void)(b); \
645: } while (0)
647: do { \
648: (void)(a); \
649: (void)(b); \
650: } while (0)
651: #define PetscCheckSorted(n, idx) \
652: do { \
653: (void)(n); \
654: (void)(idx); \
655: } while (0)
657: #else
659: /*
660: This macro currently does nothing, the plan is for each PetscObject to have a PetscInt "type"
661: member associated with the string type_name that can be quickly compared.
663: **Do not swap this macro to compare string type_name!**
665: This macro is used incorrectly in the code. Many places that do not need identity of the
666: types incorrectly call this check and would need to be fixed if this macro is enabled.
667: */
668: #if 0
669: #define PetscCheckSameType(a, arga, b, argb) \
670: do { \
671: PetscBool pcst_type_eq_ = PETSC_TRUE; \
672: PetscCall(PetscStrcmp(((PetscObject)(a))->type_name, ((PetscObject)(b))->type_name, &pcst_type_eq_)); \
673: 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); \
674: } while (0)
675: #else
676: #define PetscCheckSameType(a, arga, b, argb) \
677: do { \
678: (void)(a); \
679: (void)(b); \
680: } while (0)
681: #endif
683: /*
684: Check type_name
685: */
686: #define PetscCheckTypeName(a, type) \
687: do { \
688: PetscBool _7_match; \
689: PetscCall(PetscObjectTypeCompare(((PetscObject)(a)), (type), &_7_match)); \
690: PetscCheck(_7_match, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Object (%s) is not %s", (char *)(((PetscObject)(a))->type_name), type); \
691: } while (0)
693: #define PetscCheckTypeNames(a, type1, type2) \
694: do { \
695: PetscBool _7_match; \
696: PetscCall(PetscObjectTypeCompareAny(((PetscObject)(a)), &_7_match, (type1), (type2), "")); \
697: PetscCheck(_7_match, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Object (%s) is not %s or %s", (char *)(((PetscObject)(a))->type_name), type1, type2); \
698: } while (0)
700: /*
701: Use this macro to check if the type is set
702: */
705: /*
706: Sometimes object must live on same communicator to inter-operate
707: */
708: #define PetscCheckSameComm(a, arga, b, argb) \
709: do { \
710: PetscMPIInt _7_flag; \
711: PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)(a)), PetscObjectComm((PetscObject)(b)), &_7_flag)); \
712: 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); \
713: } while (0)
715: #define PetscCheckSameTypeAndComm(a, arga, b, argb) \
716: do { \
717: PetscCheckSameType(a, arga, b, argb); \
718: PetscCheckSameComm(a, arga, b, argb); \
719: } while (0)
722: do { \
723: PetscScalar b0 = (b); \
724: PetscReal b1[5], b2[5]; \
725: if (PetscIsNanScalar(b0)) { \
726: b1[4] = 1; \
727: } else { \
728: b1[4] = 0; \
729: }; \
730: b1[0] = -PetscRealPart(b0); \
731: b1[1] = PetscRealPart(b0); \
732: b1[2] = -PetscImaginaryPart(b0); \
733: b1[3] = PetscImaginaryPart(b0); \
734: PetscCall(MPIU_Allreduce(b1, b2, 5, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)(a)))); \
735: 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); \
736: } while (0)
739: do { \
740: PetscReal b0 = (b), b1[3], b2[3]; \
741: if (PetscIsNanReal(b0)) { \
742: b1[2] = 1; \
743: } else { \
744: b1[2] = 0; \
745: }; \
746: b1[0] = -b0; \
747: b1[1] = b0; \
748: PetscCall(MPIU_Allreduce(b1, b2, 3, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)(a)))); \
749: 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); \
750: } while (0)
753: do { \
754: PetscInt b0 = (b), b1[2], b2[2]; \
755: b1[0] = -b0; \
756: b1[1] = b0; \
757: PetscCall(MPIU_Allreduce(b1, b2, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)(a)))); \
758: PetscCheck(-b2[0] == b2[1], PetscObjectComm((PetscObject)(a)), PETSC_ERR_ARG_WRONG, "Int value must be same on all processes, argument # %d", arg); \
759: } while (0)
762: do { \
763: PetscMPIInt b0 = (b), b1[2], b2[2]; \
764: b1[0] = -b0; \
765: b1[1] = b0; \
766: PetscCall(MPIU_Allreduce(b1, b2, 2, MPI_INT, MPI_MAX, PetscObjectComm((PetscObject)(a)))); \
767: PetscCheck(-b2[0] == b2[1], PetscObjectComm((PetscObject)(a)), PETSC_ERR_ARG_WRONG, "PetscMPIInt value must be same on all processes, argument # %d", arg); \
768: } while (0)
771: do { \
772: PetscMPIInt b0 = (PetscMPIInt)(b), b1[2], b2[2]; \
773: b1[0] = -b0; \
774: b1[1] = b0; \
775: PetscCall(MPIU_Allreduce(b1, b2, 2, MPI_INT, MPI_MAX, PetscObjectComm((PetscObject)(a)))); \
776: PetscCheck(-b2[0] == b2[1], PetscObjectComm((PetscObject)(a)), PETSC_ERR_ARG_WRONG, "Bool value must be same on all processes, argument # %d", arg); \
777: } while (0)
780: do { \
781: PetscMPIInt b0 = (PetscMPIInt)(b), b1[2], b2[2]; \
782: b1[0] = -b0; \
783: b1[1] = b0; \
784: PetscCall(MPIU_Allreduce(b1, b2, 2, MPI_INT, MPI_MAX, PetscObjectComm((PetscObject)(a)))); \
785: PetscCheck(-b2[0] == b2[1], PetscObjectComm((PetscObject)(a)), PETSC_ERR_ARG_WRONG, "Enum value must be same on all processes, argument # %d", arg); \
786: } while (0)
788: #define PetscCheckSorted(n, idx) \
789: do { \
790: PetscBool _1_flg; \
791: PetscSorted(n, idx, _1_flg); \
792: PetscCheck(_1_flg, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Input array needs to be sorted"); \
793: } while (0)
795: #endif
796: #else /* PETSC_CLANG_STATIC_ANALYZER */
797: template <typename Ta, typename Tb>
798: extern void PetscCheckSameType(Ta, int, Tb, int);
799: template <typename Ta, typename Tb>
800: extern void PetscCheckTypeName(Ta, Tb);
801: template <typename Ta, typename Tb, typename Tc>
802: extern void PetscCheckTypeNames(Ta, Tb, Tc);
803: template <typename T>
805: template <typename Ta, typename Tb>
806: extern void PetscCheckSameComm(Ta, int, Tb, int);
807: template <typename Ta, typename Tb>
808: extern void PetscCheckSameTypeAndComm(Ta, int, Tb, int);
809: template <typename Ta, typename Tb>
811: template <typename Ta, typename Tb>
813: template <typename Ta, typename Tb>
815: template <typename Ta, typename Tb>
817: template <typename Ta, typename Tb>
819: template <typename Ta, typename Tb>
821: template <typename T>
822: extern void PetscCheckSorted(PetscInt, T);
823: #endif /* PETSC_CLANG_STATIC_ANALYZER */
825: /*MC
826: PetscTryMethod - Queries a `PetscObject` for a method added with `PetscObjectComposeFunction()`, if it exists then calls it.
828: Synopsis:
829: #include "petsc/private/petscimpl.h"
830: PetscTryMethod(PetscObject obj, const char *name, (arg_types), (arg_value))
832: Input Parameters:
833: + obj - the object, for example a `Mat`, that does not need to be cast to `PetscObject`
834: . name - the name of the method, for example, "KSPGMRESSetRestart_C" for the function `KSPGMRESSetRestart()`
835: . arg_types - the argument types for the method, for example, (KSP,PetscInt)
836: - args - the arguments for the method, for example, (ksp,restart))
838: Level: developer
840: Notes:
841: This does not return an error code, it is a macro that returns from the subroutine with an error code on error.
843: Use `PetscUseTypeMethod()` or `PetscTryTypeMethod()` to call functions that are included in the object's function table, the `ops` array
844: in the object.
846: .seealso: `PetscUseMethod()`, `PetscCall()`, `PetscUseTypeMethod()`, `PetscTryTypeMethod()`, `PetscCheck()`, `PetscObject`
847: M*/
848: #define PetscTryMethod(obj, A, B, C) \
849: do { \
850: PetscErrorCode(*_7_f) B; \
851: PetscCall(PetscObjectQueryFunction((PetscObject)(obj), A, &_7_f)); \
852: if (_7_f) PetscCall((*_7_f)C); \
853: } while (0)
855: /*MC
856: PetscUseMethod - Queries a `PetscObject` for a method added with `PetscObjectComposeFunction()`, if it exists then calls it, otherwise generates an error.
858: Synopsis:
859: #include "petsc/private/petscimpl.h"
860: PetscUseMethod(PetscObject obj, const char *name, (arg_types), (arg_value))
862: Input Parameters:
863: + obj - the object, for example a `Mat`, that does not need to be cast to `PetscObject`
864: . name - the name of the method, for example, "KSPGMRESSetRestart_C" for the function `KSPGMRESSetRestart()`
865: . arg_types - the argument types for the method, for example, (KSP,PetscInt)
866: - args - the arguments for the method, for example, (ksp,restart))
868: Level: developer
870: Notes:
871: This does not return an error code, it is a macro that returns from the subroutine with an error code on error.
873: Use `PetscUseTypeMethod()` or `PetscTryTypeMethod()` to call functions that are included in the object's function table, the `ops` array
874: in the object.
876: .seealso: `PetscTryMethod()`, `PetscCall()`, `PetscUseTypeMethod()`, `PetscTryTypeMethod()`, `PetscCheck()`, `PetscObject`
877: M*/
878: #define PetscUseMethod(obj, A, B, C) \
879: do { \
880: PetscErrorCode(*_7_f) B; \
881: PetscCall(PetscObjectQueryFunction((PetscObject)(obj), A, &_7_f)); \
882: PetscCheck(_7_f, PetscObjectComm((PetscObject)(obj)), PETSC_ERR_SUP, "Cannot locate function %s in object", A); \
883: PetscCall((*_7_f)C); \
884: } while (0)
886: /*
887: Use Microsoft traditional preprocessor.
889: The Microsoft compiler option -Zc:preprocessor available in recent versions of the compiler
890: sets _MSVC_TRADITIONAL to zero so this code path is not used.
892: It appears the Intel Microsoft Windows compiler icl does not have an equivalent of -Zc:preprocessor
894: These macros use the trick that Windows compilers remove the , before the __VA_ARGS__ if __VA_ARGS__ does not exist
896: PetscCall() cannot be used in the macros because the remove the , trick does not work in a macro in a macro
897: */
898: #if (defined(_MSC_VER) && (!defined(_MSVC_TRADITIONAL) || _MSVC_TRADITIONAL)) || defined(__ICL)
900: #define PetscUseTypeMethod(obj, OP, ...) \
901: do { \
902: PetscErrorCode ierr_p_; \
903: PetscStackUpdateLine; \
904: 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); \
905: ierr_p_ = (*(obj)->ops->OP)(obj, __VA_ARGS__); \
906: PetscCall(ierr_p_); \
907: } while (0)
909: #define PetscTryTypeMethod(obj, OP, ...) \
910: do { \
911: if ((obj)->ops->OP) { \
912: PetscErrorCode ierr_p_; \
913: PetscStackUpdateLine; \
914: ierr_p_ = (*(obj)->ops->OP)(obj, __VA_ARGS__); \
915: PetscCall(ierr_p_); \
916: } \
917: } while (0)
919: #else
921: /*MC
922: PetscUseTypeMethod - Call a method on a `PetscObject`, that is a function in the objects function table `obj->ops`, error if the method does not exist
924: Synopsis:
925: #include "petsc/private/petscimpl.h"
926: PetscUseTypeMethod(obj, method, other_args)
928: Input Parameters:
929: + obj - the object, for example a `Mat`, that does not need to be cast to `PetscObject`
930: . method - the name of the method, for example, mult for the PETSc routine `MatMult()`
931: - other_args - the other arguments for the method, `obj` is the first argument
933: Level: developer
935: Note:
936: This does not return an error code, it is a macro that returns from the subroutine with an error code on error.
938: Use `PetscUseMethod()` or `PetscTryMethod()` to call functions that have been composed to an object with `PetscObjectComposeFunction()`
940: .seealso: `PetscTryMethod()`, `PetscUseMethod()`, `PetscCall()`, `PetscCheck()`, `PetscTryTypeMethod()`, `PetscCallBack()`
941: M*/
942: #define PetscUseTypeMethod(obj, ...) \
943: do { \
944: PetscCheck((obj)->ops->PETSC_FIRST_ARG((__VA_ARGS__, unused)), PetscObjectComm((PetscObject)obj), PETSC_ERR_SUP, "No method %s for %s of type %s", \
945: PetscStringize(PETSC_FIRST_ARG((__VA_ARGS__,unused))), ((PetscObject)obj)->class_name, ((PetscObject)obj)->type_name); \
946: PetscCall((*(obj)->ops->PETSC_FIRST_ARG((__VA_ARGS__, unused)))(obj PETSC_REST_ARG(__VA_ARGS__))); \
947: } while (0)
949: /*MC
950: PetscTryTypeMethod - Call a method on a `PetscObject`, that is a function in the objects function table `obj->ops`, skip if the method does not exist
952: Synopsis:
953: #include "petsc/private/petscimpl.h"
954: PetscTryTypeMethod(obj, method, other_args)
956: Input Parameters:
957: + obj - the object, for example a `Mat`, that does not need to be cast to `PetscObject`
958: . method - the name of the method, for example, mult for the PETSc routine `MatMult()`
959: - other_args - the other arguments for the method, `obj` is the first argument
961: Level: developer
963: Note:
964: This does not return an error code, it is a macro that returns from the subroutine with an error code on error.
966: Use `PetscUseMethod()` or `PetscTryMethod()` to call functions that have been composed to an object with `PetscObjectComposeFunction()`
968: .seealso: `PetscTryMethod()`, `PetscUseMethod()`, `PetscCall()`, `PetscCheck()`, `PetscUseTypeMethod()`
969: M*/
970: #define PetscTryTypeMethod(obj, ...) \
971: do { \
972: if ((obj)->ops->PETSC_FIRST_ARG((__VA_ARGS__, unused))) PetscCall((*(obj)->ops->PETSC_FIRST_ARG((__VA_ARGS__, unused)))(obj PETSC_REST_ARG(__VA_ARGS__))); \
973: } while (0)
975: #endif
977: /*MC
978: PetscObjectStateIncrease - Increases the state of any `PetscObject`
980: Synopsis:
981: #include "petsc/private/petscimpl.h"
982: PetscErrorCode PetscObjectStateIncrease(PetscObject obj)
984: Logically Collective
986: Input Parameter:
987: . obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. This must be
988: cast with a (PetscObject), for example,
989: `PetscObjectStateIncrease`((`PetscObject`)mat);
991: Level: developer
993: Notes:
994: Object state is a 64-bit integer which gets increased every time
995: the object is changed internally. By saving and later querying the object state
996: one can determine whether information about the object is still current.
997: Currently, state is maintained for `Vec` and `Mat` objects.
999: This routine is mostly for internal use by PETSc; a developer need only
1000: call it after explicit access to an object's internals. Routines such
1001: as `VecSet()` or `MatScale()` already call this routine. It is also called, as a
1002: precaution, in `VecRestoreArray()`, `MatRestoreRow()`, `MatDenseRestoreArray()`.
1004: Routines such as `VecNorm()` can by-pass the computation if the norm has already been computed and the vector's state has not changed.
1006: This routine is logically collective because state equality comparison needs to be possible without communication.
1008: `Mat` also has `MatGetNonzeroState()` for tracking changes to the nonzero structure.
1010: .seealso: `PetscObjectStateGet()`, `PetscObject`
1011: M*/
1012: #define PetscObjectStateIncrease(obj) ((obj)->state++, PETSC_SUCCESS)
1014: PETSC_EXTERN PetscErrorCode PetscObjectStateGet(PetscObject, PetscObjectState *);
1015: PETSC_EXTERN PetscErrorCode PetscObjectStateSet(PetscObject, PetscObjectState);
1016: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataRegister(PetscInt *);
1017: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseInt(PetscObject);
1018: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseIntstar(PetscObject);
1019: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseReal(PetscObject);
1020: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseRealstar(PetscObject);
1021: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseScalar(PetscObject);
1022: PETSC_EXTERN PetscErrorCode PetscObjectComposedDataIncreaseScalarstar(PetscObject);
1023: PETSC_EXTERN PetscInt PetscObjectComposedDataMax;
1025: /*MC
1026: PetscObjectComposedDataSetInt - attach `PetscInt` data to a `PetscObject` that may be later accessed with `PetscObjectComposedDataGetInt()`
1028: Synopsis:
1029: #include "petsc/private/petscimpl.h"
1030: PetscErrorCode PetscObjectComposedDataSetInt(PetscObject obj, PetscInt id, PetscInt data)
1032: Not Collective
1034: Input Parameters:
1035: + obj - the object to which data is to be attached
1036: . id - the identifier for the data
1037: - data - the data to be attached, a `PetscInt`
1039: Level: developer
1041: Notes:
1042: The `data` identifier can be created through a call to `PetscObjectComposedDataRegister()`
1044: This allows the efficient composition of a single integer value with a `PetscObject`. Complex data may be
1045: attached with `PetscObjectCompose()`
1047: .seealso: `PetscObjectComposedDataGetInt()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetReal()`,
1048: `PetscObjectComposedDataGetIntstar()`, `PetscObjectComposedDataSetIntstar()`, `PetscObject`,
1049: `PetscObjectCompose()`, `PetscObjectQuery()`
1050: M*/
1051: #define PetscObjectComposedDataSetInt(obj, id, data) \
1052: ((PetscErrorCode)((((obj)->int_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseInt(obj)) || ((obj)->intcomposeddata[id] = data, (obj)->intcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))
1054: /*MC
1055: PetscObjectComposedDataGetInt - retrieve `PetscInt` data attached to a `PetscObject` `PetscObjectComposedDataSetInt()`
1057: Synopsis:
1058: #include "petsc/private/petscimpl.h"
1059: PetscErrorCode PetscObjectComposedDataGetInt(PetscObject obj, PetscInt id, PetscInt data, PetscBool flag)
1061: Not Collective
1063: Input Parameters:
1064: + obj - the object from which data is to be retrieved
1065: - id - the identifier for the data
1067: Output Parameters:
1068: + data - the data to be retrieved, a `PetscInt`
1069: - flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise
1071: Level: developer
1073: Notes:
1074: The `data` and `flag` variables are inlined, so they are not pointers.
1076: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetReal()`,
1077: `PetscObjectComposedDataGetIntstar()`, `PetscObjectComposedDataSetIntstar()`, `PetscObject`,
1078: `PetscObjectCompose()`, `PetscObjectQuery()`
1079: M*/
1080: #define PetscObjectComposedDataGetInt(obj, id, data, flag) (((obj)->intcomposedstate ? (data = (obj)->intcomposeddata[id], flag = (PetscBool)((obj)->intcomposedstate[id] == (obj)->state)) : (flag = PETSC_FALSE)), PETSC_SUCCESS)
1082: /*MC
1083: PetscObjectComposedDataSetIntstar - attach `PetscInt` array data to a `PetscObject` that may be accessed later with `PetscObjectComposedDataGetIntstar()`
1085: Synopsis:
1086: #include "petsc/private/petscimpl.h"
1087: PetscErrorCode PetscObjectComposedDataSetIntstar(PetscObject obj, PetscInt id, PetscInt *data)
1089: Not Collective
1091: Input Parameters:
1092: + obj - the object to which data is to be attached
1093: . id - the identifier for the data
1094: - data - the data to be attached, a `PetscInt` array
1096: Level: developer
1098: Notes:
1099: The `data` identifier can be determined through a call to `PetscObjectComposedDataRegister()`
1101: The length of the array accessed must be known, it is not available through this API.
1103: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetReal()`,
1104: `PetscObjectComposedDataGetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1105: `PetscObjectCompose()`, `PetscObjectQuery()`
1106: M*/
1107: #define PetscObjectComposedDataSetIntstar(obj, id, data) \
1108: ((PetscErrorCode)((((obj)->intstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseIntstar(obj)) || ((obj)->intstarcomposeddata[id] = data, (obj)->intstarcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))
1110: /*MC
1111: PetscObjectComposedDataGetIntstar - retrieve `PetscInt` array data attached to a `PetscObject` with `PetscObjectComposedDataSetIntstar()`
1113: Synopsis:
1114: #include "petsc/private/petscimpl.h"
1115: PetscErrorCode PetscObjectComposedDataGetIntstar(PetscObject obj, PetscInt id, PetscInt *data, PetscBool flag)
1117: Not Collective
1119: Input Parameters:
1120: + obj - the object from which data is to be retrieved
1121: - id - the identifier for the data
1123: Output Parameters:
1124: + data - the data to be retrieved, a `PetscInt` array
1125: - flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise
1127: Level: developer
1129: Notes:
1130: The `data` and `flag` variables are inlined, so they are not pointers.
1132: The length of the array accessed must be known, it is not available through this API.
1134: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetReal()`,
1135: `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1136: `PetscObjectCompose()`, `PetscObjectQuery()`
1137: M*/
1138: #define PetscObjectComposedDataGetIntstar(obj, id, data, flag) \
1139: ((PetscErrorCode)(((obj)->intstarcomposedstate ? (data = (obj)->intstarcomposeddata[id], flag = (PetscBool)((obj)->intstarcomposedstate[id] == (obj)->state)) : (flag = PETSC_FALSE)), PETSC_SUCCESS))
1141: /*MC
1142: PetscObjectComposedDataSetReal - attach `PetscReal` data to a `PetscObject` that may be later accessed with `PetscObjectComposedDataGetReal()`
1144: Synopsis:
1145: #include "petsc/private/petscimpl.h"
1146: PetscErrorCode PetscObjectComposedDataSetReal(PetscObject obj, PetscInt id, PetscReal data)
1148: Not Collective
1150: Input Parameters:
1151: + obj - the object to which data is to be attached
1152: . id - the identifier for the data
1153: - data - the data to be attached, a `PetscReal`
1155: Level: developer
1157: Note:
1158: The `data` identifier can be determined through a call to `PetscObjectComposedDataRegister()`
1160: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1161: `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1162: `PetscObjectCompose()`, `PetscObjectQuery()`
1163: M*/
1164: #define PetscObjectComposedDataSetReal(obj, id, data) \
1165: ((PetscErrorCode)((((obj)->real_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseReal(obj)) || ((obj)->realcomposeddata[id] = data, (obj)->realcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))
1167: /*MC
1168: PetscObjectComposedDataGetReal - retrieve `PetscReal` data attached to a `PetscObject` set with `PetscObjectComposedDataSetReal()`
1170: Synopsis:
1171: #include "petsc/private/petscimpl.h"
1172: PetscErrorCode PetscObjectComposedDataGetReal(PetscObject obj, PetscInt id, PetscReal data, PetscBool flag)
1174: Not Collective
1176: Input Parameters:
1177: + obj - the object from which data is to be retrieved
1178: - id - the identifier for the data
1180: Output Parameters:
1181: + data - the data to be retrieved, a `PetscReal`
1182: - flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise
1184: Level: developer
1186: Note:
1187: The `data` and `flag` variables are inlined, so they are not pointers.
1189: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataSetIntstar()`,
1190: `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1191: `PetscObjectCompose()`, `PetscObjectQuery()`
1192: M*/
1193: #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))
1195: /*MC
1196: PetscObjectComposedDataSetRealstar - attach `PetscReal` array data to a `PetscObject` that may be retrieved with `PetscObjectComposedDataGetRealstar()`
1198: Synopsis:
1199: #include "petsc/private/petscimpl.h"
1200: PetscErrorCode PetscObjectComposedDataSetRealstar(PetscObject obj, PetscInt id, PetscReal *data)
1202: Not Collective
1204: Input Parameters:
1205: + obj - the object to which data is to be attached
1206: . id - the identifier for the data
1207: - data - the data to be attached
1209: Level: developer
1211: Notes:
1212: The `data` identifier can be determined through a call to `PetscObjectComposedDataRegister()`
1214: The length of the array accessed must be known, it is not available through this API.
1216: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1217: `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1218: `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataGetRealstar()`
1219: M*/
1220: #define PetscObjectComposedDataSetRealstar(obj, id, data) \
1221: ((PetscErrorCode)((((obj)->realstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseRealstar(obj)) || ((obj)->realstarcomposeddata[id] = data, (obj)->realstarcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))
1223: /*MC
1224: PetscObjectComposedDataGetRealstar - retrieve `PetscReal` array data attached to a `PetscObject` with `PetscObjectComposedDataSetRealstar()`
1226: Synopsis:
1227: #include "petsc/private/petscimpl.h"
1228: PetscErrorCode PetscObjectComposedDataGetRealstar(PetscObject obj, PetscInt id, PetscReal *data, PetscBool flag)
1230: Not Collective
1232: Input Parameters:
1233: + obj - the object from which data is to be retrieved
1234: - id - the identifier for the data
1236: Output Parameters:
1237: + data - the data to be retrieved, a `PetscReal` array
1238: - flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise
1240: Level: developer
1242: Notes:
1243: The `data` and `flag` variables are inlined, so they are not pointers.
1245: The length of the array accessed must be known, it is not available through this API.
1247: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1248: `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1249: `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataSetRealstar()`
1250: M*/
1251: #define PetscObjectComposedDataGetRealstar(obj, id, data, flag) \
1252: ((PetscErrorCode)(((obj)->realstarcomposedstate ? (data = (obj)->realstarcomposeddata[id], flag = (PetscBool)((obj)->realstarcomposedstate[id] == (obj)->state)) : (flag = PETSC_FALSE)), PETSC_SUCCESS))
1254: /*MC
1255: PetscObjectComposedDataSetScalar - attach `PetscScalar` data to a `PetscObject` that may be later retrieved with `PetscObjectComposedDataGetScalar()`
1257: Synopsis:
1258: #include "petsc/private/petscimpl.h"
1259: PetscErrorCode PetscObjectComposedDataSetScalar(PetscObject obj, PetscInt id, PetscScalar data)
1261: Not Collective
1263: Input Parameters:
1264: + obj - the object to which data is to be attached
1265: . id - the identifier for the data
1266: - data - the data to be attached, a `PetscScalar`
1268: Level: developer
1270: Note:
1271: The `data` identifier can be determined through a call to `PetscObjectComposedDataRegister()`
1273: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1274: `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1275: `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataSetRealstar()`, `PetscObjectComposedDataGetScalar()`
1276: M*/
1277: #if defined(PETSC_USE_COMPLEX)
1278: #define PetscObjectComposedDataSetScalar(obj, id, data) \
1279: ((PetscErrorCode)((((obj)->scalar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseScalar(obj)) || ((obj)->scalarcomposeddata[id] = data, (obj)->scalarcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))
1280: #else
1281: #define PetscObjectComposedDataSetScalar(obj, id, data) PetscObjectComposedDataSetReal(obj, id, data)
1282: #endif
1283: /*MC
1284: PetscObjectComposedDataGetScalar - retrieve `PetscScalar` data attached to a `PetscObject` that was set with `PetscObjectComposedDataSetScalar()`
1286: Synopsis:
1287: #include "petsc/private/petscimpl.h"
1288: PetscErrorCode PetscObjectComposedDataGetScalar(PetscObject obj, PetscInt id, PetscScalar data, PetscBool flag)
1290: Not Collective
1292: Input Parameters:
1293: + obj - the object from which data is to be retrieved
1294: - id - the identifier for the data
1296: Output Parameters:
1297: + data - the data to be retrieved, a `PetscScalar`
1298: - flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise
1300: Level: developer
1302: Note:
1303: The `data` and `flag` variables are inlined, so they are not pointers.
1305: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1306: `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1307: `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataSetRealstar()`, `PetscObjectComposedDataSetScalar()`
1308: M*/
1309: #if defined(PETSC_USE_COMPLEX)
1310: #define PetscObjectComposedDataGetScalar(obj, id, data, flag) \
1311: ((PetscErrorCode)(((obj)->scalarcomposedstate ? (data = (obj)->scalarcomposeddata[id], flag = (PetscBool)((obj)->scalarcomposedstate[id] == (obj)->state)) : (flag = PETSC_FALSE)), PETSC_SUCCESS))
1312: #else
1313: #define PetscObjectComposedDataGetScalar(obj, id, data, flag) PetscObjectComposedDataGetReal(obj, id, data, flag)
1314: #endif
1316: /*MC
1317: PetscObjectComposedDataSetScalarstar - attach `PetscScalar` array data to a `PetscObject` that may be later retrieved with `PetscObjectComposedDataSetScalarstar()`
1319: Synopsis:
1320: #include "petsc/private/petscimpl.h"
1321: PetscErrorCode PetscObjectComposedDataSetScalarstar(PetscObject obj, PetscInt id, PetscScalar *data)
1323: Not Collective
1325: Input Parameters:
1326: + obj - the object to which data is to be attached
1327: . id - the identifier for the data
1328: - data - the data to be attached, a `PetscScalar` array
1330: Level: developer
1332: Notes:
1333: The `data` identifier can be determined through a call to `PetscObjectComposedDataRegister()`
1335: The length of the array accessed must be known, it is not available through this API.
1337: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1338: `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1339: `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataSetRealstar()`, `PetscObjectComposedDataGetScalarstar()`
1340: M*/
1341: #if defined(PETSC_USE_COMPLEX)
1342: #define PetscObjectComposedDataSetScalarstar(obj, id, data) \
1343: ((PetscErrorCode)((((obj)->scalarstar_idmax < PetscObjectComposedDataMax) && PetscObjectComposedDataIncreaseScalarstar(obj)) || ((obj)->scalarstarcomposeddata[id] = data, (obj)->scalarstarcomposedstate[id] = (obj)->state, PETSC_SUCCESS)))
1344: #else
1345: #define PetscObjectComposedDataSetScalarstar(obj, id, data) PetscObjectComposedDataSetRealstar(obj, id, data)
1346: #endif
1347: /*MC
1348: PetscObjectComposedDataGetScalarstar - retrieve `PetscScalar` array data attached to a `PetscObject` that was set with `PetscObjectComposedDataSetScalarstar()`
1349: attached to an object
1351: Synopsis:
1352: #include "petsc/private/petscimpl.h"
1353: PetscErrorCode PetscObjectComposedDataGetScalarstar(PetscObject obj, PetscInt id, PetscScalar *data, PetscBool flag)
1355: Not Collective
1357: Input Parameters:
1358: + obj - the object from which data is to be retrieved
1359: - id - the identifier for the data
1361: Output Parameters:
1362: + data - the data to be retrieved, a `PetscScalar` array
1363: - flag - `PETSC_TRUE` if the data item exists and is valid, `PETSC_FALSE` otherwise
1365: Level: developer
1367: Notes:
1368: The `data` and `flag` variables are inlined, so they are not pointers.
1370: The length of the array accessed must be known, it is not available through this API.
1372: .seealso: `PetscObjectComposedDataSetInt()`, `PetscObjectComposedDataSetReal()`, `PetscObjectComposedDataGetReal()`, `PetscObjectComposedDataSetIntstar()`,
1373: `PetscObjectComposedDataSetIntstar()`, `PetscObjectComposedDataGetInt()`, `PetscObject`,
1374: `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObjectComposedDataSetRealstar()`, `PetscObjectComposedDataSetScalarstar()`
1375: M*/
1376: #if defined(PETSC_USE_COMPLEX)
1377: #define PetscObjectComposedDataGetScalarstar(obj, id, data, flag) \
1378: ((PetscErrorCode)(((obj)->scalarstarcomposedstate ? (data = (obj)->scalarstarcomposeddata[id], flag = (PetscBool)((obj)->scalarstarcomposedstate[id] == (obj)->state)) : (flag = PETSC_FALSE)), PETSC_SUCCESS))
1379: #else
1380: #define PetscObjectComposedDataGetScalarstar(obj, id, data, flag) PetscObjectComposedDataGetRealstar(obj, id, data, flag)
1381: #endif
1383: PETSC_EXTERN PetscMPIInt Petsc_Counter_keyval;
1384: PETSC_EXTERN PetscMPIInt Petsc_InnerComm_keyval;
1385: PETSC_EXTERN PetscMPIInt Petsc_OuterComm_keyval;
1386: PETSC_EXTERN PetscMPIInt Petsc_Seq_keyval;
1387: PETSC_EXTERN PetscMPIInt Petsc_ShmComm_keyval;
1388: PETSC_EXTERN PetscMPIInt Petsc_CreationIdx_keyval;
1389: PETSC_EXTERN PetscMPIInt Petsc_Garbage_HMap_keyval;
1391: PETSC_EXTERN PetscMPIInt Petsc_SharedWD_keyval;
1392: PETSC_EXTERN PetscMPIInt Petsc_SharedTmp_keyval;
1394: struct PetscCommStash {
1395: struct PetscCommStash *next;
1396: MPI_Comm comm;
1397: };
1399: /*
1400: PETSc communicators have this attribute, see
1401: PetscCommDuplicate(), PetscCommDestroy(), PetscCommGetNewTag(), PetscObjectGetName()
1402: */
1403: typedef struct {
1404: PetscMPIInt tag; /* next free tag value */
1405: PetscInt refcount; /* number of references, communicator can be freed when this reaches 0 */
1406: PetscInt namecount; /* used to generate the next name, as in Vec_0, Mat_1, ... */
1407: PetscMPIInt *iflags; /* length of comm size, shared by all calls to PetscCommBuildTwoSided_Allreduce/RedScatter on this comm */
1408: struct PetscCommStash *comms; /* communicators available for PETSc to pass off to other packages */
1409: } PetscCommCounter;
1411: typedef enum {
1412: STATE_BEGIN,
1413: STATE_PENDING,
1414: STATE_END
1415: } SRState;
1417: typedef enum {
1418: PETSC_SR_REDUCE_SUM = 0,
1419: PETSC_SR_REDUCE_MAX = 1,
1420: PETSC_SR_REDUCE_MIN = 2
1421: } PetscSRReductionType;
1423: typedef struct {
1424: MPI_Comm comm;
1425: MPI_Request request;
1426: PetscBool mix;
1427: PetscBool async;
1428: PetscScalar *lvalues; /* this are the reduced values before call to MPI_Allreduce() */
1429: PetscScalar *gvalues; /* values after call to MPI_Allreduce() */
1430: void **invecs; /* for debugging only, vector/memory used with each op */
1431: PetscInt *reducetype; /* is particular value to be summed or maxed? */
1432: struct {
1433: PetscScalar v;
1434: PetscInt i;
1435: } *lvalues_mix, *gvalues_mix; /* used when mixing reduce operations */
1436: SRState state; /* are we calling xxxBegin() or xxxEnd()? */
1437: PetscInt maxops; /* total amount of space we have for requests */
1438: PetscInt numopsbegin; /* number of requests that have been queued in */
1439: PetscInt numopsend; /* number of requests that have been gotten by user */
1440: } PetscSplitReduction;
1442: PETSC_EXTERN PetscErrorCode PetscSplitReductionGet(MPI_Comm, PetscSplitReduction **);
1443: PETSC_EXTERN PetscErrorCode PetscSplitReductionEnd(PetscSplitReduction *);
1444: PETSC_EXTERN PetscErrorCode PetscSplitReductionExtend(PetscSplitReduction *);
1446: #if defined(PETSC_HAVE_THREADSAFETY)
1447: #if defined(PETSC_HAVE_CONCURRENCYKIT)
1448: #if defined(__cplusplus)
1449: /* CK does not have extern "C" protection in their include files */
1450: extern "C" {
1451: #endif
1452: #include <ck_spinlock.h>
1453: #if defined(__cplusplus)
1454: }
1455: #endif
1456: typedef ck_spinlock_t PetscSpinlock;
1458: static inline PetscErrorCode PetscSpinlockCreate(PetscSpinlock *ck_spinlock)
1459: {
1460: ck_spinlock_init(ck_spinlock);
1461: return PETSC_SUCCESS;
1462: }
1463: static inline PetscErrorCode PetscSpinlockLock(PetscSpinlock *ck_spinlock)
1464: {
1465: ck_spinlock_lock(ck_spinlock);
1466: return PETSC_SUCCESS;
1467: }
1468: static inline PetscErrorCode PetscSpinlockUnlock(PetscSpinlock *ck_spinlock)
1469: {
1470: ck_spinlock_unlock(ck_spinlock);
1471: return PETSC_SUCCESS;
1472: }
1473: static inline PetscErrorCode PetscSpinlockDestroy(PetscSpinlock *ck_spinlock)
1474: {
1475: return PETSC_SUCCESS;
1476: }
1477: #elif (defined(__cplusplus) && defined(PETSC_HAVE_CXX_ATOMIC)) || (!defined(__cplusplus) && defined(PETSC_HAVE_STDATOMIC_H))
1478: #if defined(__cplusplus)
1479: // See the example at https://en.cppreference.com/w/cpp/atomic/atomic_flag
1480: #include <atomic>
1481: #define petsc_atomic_flag std::atomic_flag
1482: #define petsc_atomic_flag_test_and_set(p) std::atomic_flag_test_and_set_explicit(p, std::memory_order_acquire)
1483: #define petsc_atomic_flag_clear(p) std::atomic_flag_clear_explicit(p, std::memory_order_release)
1484: #else
1485: #include <stdatomic.h>
1486: #define petsc_atomic_flag atomic_flag
1487: #define petsc_atomic_flag_test_and_set(p) atomic_flag_test_and_set_explicit(p, memory_order_acquire)
1488: #define petsc_atomic_flag_clear(p) atomic_flag_clear_explicit(p, memory_order_release)
1489: #endif
1491: typedef petsc_atomic_flag PetscSpinlock;
1493: static inline PetscErrorCode PetscSpinlockCreate(PetscSpinlock *spinlock)
1494: {
1495: petsc_atomic_flag_clear(spinlock);
1496: return PETSC_SUCCESS;
1497: }
1498: static inline PetscErrorCode PetscSpinlockLock(PetscSpinlock *spinlock)
1499: {
1500: do {
1501: } while (petsc_atomic_flag_test_and_set(spinlock));
1502: return PETSC_SUCCESS;
1503: }
1504: static inline PetscErrorCode PetscSpinlockUnlock(PetscSpinlock *spinlock)
1505: {
1506: petsc_atomic_flag_clear(spinlock);
1507: return PETSC_SUCCESS;
1508: }
1509: static inline PetscErrorCode PetscSpinlockDestroy(PETSC_UNUSED PetscSpinlock *spinlock)
1510: {
1511: return PETSC_SUCCESS;
1512: }
1513: #undef petsc_atomic_flag_test_and_set
1514: #undef petsc_atomic_flag_clear
1515: #undef petsc_atomic_flag
1517: #elif defined(PETSC_HAVE_OPENMP)
1519: #include <omp.h>
1520: typedef omp_lock_t PetscSpinlock;
1522: static inline PetscErrorCode PetscSpinlockCreate(PetscSpinlock *omp_lock)
1523: {
1524: omp_init_lock(omp_lock);
1525: return PETSC_SUCCESS;
1526: }
1527: static inline PetscErrorCode PetscSpinlockLock(PetscSpinlock *omp_lock)
1528: {
1529: omp_set_lock(omp_lock);
1530: return PETSC_SUCCESS;
1531: }
1532: static inline PetscErrorCode PetscSpinlockUnlock(PetscSpinlock *omp_lock)
1533: {
1534: omp_unset_lock(omp_lock);
1535: return PETSC_SUCCESS;
1536: }
1537: static inline PetscErrorCode PetscSpinlockDestroy(PetscSpinlock *omp_lock)
1538: {
1539: omp_destroy_lock(omp_lock);
1540: return PETSC_SUCCESS;
1541: }
1542: #else
1543: #if defined(__cplusplus)
1544: #error "Thread safety requires either --download-concurrencykit, std::atomic, or --with-openmp"
1545: #else
1546: #error "Thread safety requires either --download-concurrencykit, stdatomic.h, or --with-openmp"
1547: #endif
1548: #endif
1550: #else
1551: typedef int PetscSpinlock;
1552: #define PetscSpinlockCreate(a) PETSC_SUCCESS
1553: #define PetscSpinlockLock(a) PETSC_SUCCESS
1554: #define PetscSpinlockUnlock(a) PETSC_SUCCESS
1555: #define PetscSpinlockDestroy(a) PETSC_SUCCESS
1556: #endif
1558: #if defined(PETSC_HAVE_THREADSAFETY)
1559: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockOpen;
1560: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockStdout;
1561: PETSC_INTERN PetscSpinlock PetscViewerASCIISpinLockStderr;
1562: PETSC_INTERN PetscSpinlock PetscCommSpinLock;
1563: #endif
1565: PETSC_EXTERN PetscLogEvent PETSC_Barrier;
1566: PETSC_EXTERN PetscLogEvent PETSC_BuildTwoSided;
1567: PETSC_EXTERN PetscLogEvent PETSC_BuildTwoSidedF;
1568: PETSC_EXTERN PetscBool use_gpu_aware_mpi;
1569: PETSC_EXTERN PetscBool PetscPrintFunctionList;
1571: #if defined(PETSC_HAVE_ADIOS)
1572: PETSC_EXTERN int64_t Petsc_adios_group;
1573: #endif
1575: #if defined(PETSC_HAVE_KOKKOS)
1576: PETSC_INTERN PetscBool PetscBeganKokkos;
1577: PETSC_EXTERN PetscBool PetscKokkosInitialized;
1578: PETSC_INTERN PetscErrorCode PetscKokkosIsInitialized_Private(PetscBool *);
1579: PETSC_INTERN PetscErrorCode PetscKokkosFinalize_Private(void);
1580: #endif
1582: #if defined(PETSC_HAVE_OPENMP)
1583: PETSC_EXTERN PetscInt PetscNumOMPThreads;
1584: #endif
1586: struct _n_PetscObjectList {
1587: char name[256];
1588: PetscBool skipdereference; /* when the PetscObjectList is destroyed do not call PetscObjectDereference() on this object */
1589: PetscObject obj;
1590: PetscObjectList next;
1591: };