Actual source code: destroy.c
1: /*
2: Provides utility routines for manulating any type of PETSc object.
3: */
4: #include <petsc/private/petscimpl.h>
5: #include <petscviewer.h>
7: static PetscErrorCode DestroyComposedData(void ***composed_star, PetscObjectState **state_star, PetscInt *count_star, void **composed, PetscObjectState **state)
8: {
9: void **tmp_star = *composed_star;
11: PetscFunctionBegin;
12: for (PetscInt i = 0, imax = *count_star; i < imax; ++i) PetscCall(PetscFree(tmp_star[i]));
13: PetscCall(PetscFree2(*composed_star, *state_star));
14: PetscCall(PetscFree2(*composed, *state));
15: *count_star = 0;
16: PetscFunctionReturn(PETSC_SUCCESS);
17: }
19: PetscErrorCode PetscComposedQuantitiesDestroy(PetscObject obj)
20: {
21: PetscFunctionBegin;
23: PetscCall(DestroyComposedData((void ***)&obj->intstarcomposeddata, &obj->intstarcomposedstate, &obj->intstar_idmax, (void **)&obj->intcomposeddata, &obj->intcomposedstate));
24: PetscCall(DestroyComposedData((void ***)&obj->realstarcomposeddata, &obj->realstarcomposedstate, &obj->realstar_idmax, (void **)&obj->realcomposeddata, &obj->realcomposedstate));
25: #if PetscDefined(USE_COMPLEX)
26: PetscCall(DestroyComposedData((void ***)&obj->scalarstarcomposeddata, &obj->scalarstarcomposedstate, &obj->scalarstar_idmax, (void **)&obj->scalarcomposeddata, &obj->scalarcomposedstate));
27: #endif
28: PetscFunctionReturn(PETSC_SUCCESS);
29: }
31: /*@
32: PetscObjectDestroy - Destroys a `PetscObject`, regardless of the class.
34: Collective
36: Input Parameter:
37: . obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. It must be cast with a (`PetscObject`\*), for example,
38: `PetscObjectDestroy`((`PetscObject`\*)&mat);
40: Level: beginner
42: .seealso: `PetscObject`
43: @*/
44: PetscErrorCode PetscObjectDestroy(PetscObject *obj)
45: {
46: PetscFunctionBegin;
47: if (!obj || !*obj) PetscFunctionReturn(PETSC_SUCCESS);
49: PetscCheck((*obj)->bops->destroy, PETSC_COMM_SELF, PETSC_ERR_PLIB, "This PETSc object of class %s does not have a generic destroy routine", (*obj)->class_name);
50: PetscCall((*(*obj)->bops->destroy)(obj));
51: PetscFunctionReturn(PETSC_SUCCESS);
52: }
54: /*@
55: PetscObjectView - Views, that is displays or stores information about a `PetscObject`.
57: Collective
59: Input Parameters:
60: + obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`. It must be cast with a (`PetscObject`), for example,
61: `PetscObjectView`((`PetscObject`)mat,`viewer`);
62: - viewer - any PETSc viewer
64: Level: intermediate
66: .seealso: `PetscObject`, `PetscObjectViewFromOptions()`, `PetscViewer`
67: @*/
68: PetscErrorCode PetscObjectView(PetscObject obj, PetscViewer viewer)
69: {
70: PetscFunctionBegin;
72: PetscCheck(obj->bops->view, PETSC_COMM_SELF, PETSC_ERR_SUP, "This PETSc object does not have a generic viewer routine");
73: if (!viewer) PetscCall(PetscViewerASCIIGetStdout(obj->comm, &viewer));
76: PetscCall((*obj->bops->view)(obj, viewer));
77: PetscFunctionReturn(PETSC_SUCCESS);
78: }
80: /*@
81: PetscObjectViewFromOptions - Processes command line options to determine if/how a `PetscObject` is to be viewed.
83: Collective
85: Input Parameters:
86: + obj - the object
87: . bobj - optional other object that provides prefix (if `NULL` then the prefix in `obj` is used)
88: - name - option string that is used to activate viewing. It typically ends with _view.
90: Options Database Key:
91: . -name [viewertype][:...] - option name and values. In actual usage the key might be something like `-vec_view`
93: Level: developer
95: Notes:
96: For a `viewertype` that represents files, including `ascii`, `binary`, `matlab`, `vu`, `vtk`, `glvis`, `cgns`, and `hdf5`, the argument has the following form
97: .vb
98: viewertype[:filename[:format[:filemode]]]
99: .ve
100: where all parts are optional, but you need to include the colon to access the next part.
101: `filename` is the name of the file where the object data will be stored. `format` is the string name
102: of a `PetscViewerFormat`, for example, `default` or `ascii_info`. `filemode` can be `append` to indicate the file named `filename` should be appended
103: to and not replaced, while `read` indicates the file is for reading.
105: For example, to read from an HDF5 file, use
106: .vb
107: hdf5:sol.h5::read
108: .ve
110: For a `viewertype` of `draw` the argument is of the form
111: .vb
112: draw[:drawtype[:filename]]
113: .ve
114: where `drawtype` is, for example, `tikz` or `x` and `filename` indicates where the data is to be saved if it is not directly displayed.
116: Other formats, such as
117: .vb
118: socket[:port]
119: saws[:communicatorname]
120: .ve
121: that send the data to a Unix socket or publish the object to the Scientific Application Webserver (SAWs) exist.
123: If no value is provided `ascii:stdout` is used
125: This function is usually not called directly but is called by, for example, `MatViewFromOptions()`.
127: .seealso: `PetscObject`, `PetscObjectView()`, `PetscOptionsCreateViewer()`
128: @*/
129: PetscErrorCode PetscObjectViewFromOptions(PetscObject obj, PetscObject bobj, const char name[])
130: {
131: PetscViewer viewer;
132: PetscBool flg;
133: static PetscBool incall = PETSC_FALSE;
134: PetscViewerFormat format;
135: const char *prefix;
137: PetscFunctionBegin;
140: if (incall) PetscFunctionReturn(PETSC_SUCCESS);
141: incall = PETSC_TRUE;
142: prefix = bobj ? bobj->prefix : obj->prefix;
143: PetscCall(PetscOptionsCreateViewer(PetscObjectComm(obj), obj->options, prefix, name, &viewer, &format, &flg));
144: if (flg) {
145: PetscCall(PetscViewerPushFormat(viewer, format));
146: PetscCall(PetscObjectView(obj, viewer));
147: PetscCall(PetscViewerFlush(viewer));
148: PetscCall(PetscViewerPopFormat(viewer));
149: PetscCall(PetscViewerDestroy(&viewer));
150: }
151: incall = PETSC_FALSE;
152: PetscFunctionReturn(PETSC_SUCCESS);
153: }
155: /*@
156: PetscObjectTypeCompare - Determines whether a PETSc object is of a particular type.
158: Not Collective
160: Input Parameters:
161: + obj - a PETSc object, for example a `Vec`, `Mat` or `KSP`. It must be cast with a (`PetscObject`), for example,
162: `PetscObjectTypeCompare`((`PetscObject`)mat);
163: - type_name - string containing a type name
165: Output Parameter:
166: . same - `PETSC_TRUE` if the type of `obj` and `type_name` are the same or both `NULL`, else `PETSC_FALSE`
168: Level: intermediate
170: .seealso: `PetscObject`, `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectBaseTypeCompare()`, `PetscObjectTypeCompareAny()`, `PetscObjectBaseTypeCompareAny()`, `PetscObjectObjectTypeCompare()`
171: @*/
172: PetscErrorCode PetscObjectTypeCompare(PetscObject obj, const char type_name[], PetscBool *same)
173: {
174: PetscFunctionBegin;
175: PetscAssertPointer(same, 3);
176: if (!obj) *same = (PetscBool)!type_name;
177: else {
179: if (!type_name || !obj->type_name) *same = (PetscBool)(!obj->type_name == !type_name);
180: else {
181: PetscAssertPointer(type_name, 2);
182: PetscCall(PetscStrcmp(obj->type_name, type_name, same));
183: }
184: }
185: PetscFunctionReturn(PETSC_SUCCESS);
186: }
188: /*@
189: PetscObjectObjectTypeCompare - Determines whether two PETSc objects are of the same type
191: Logically Collective
193: Input Parameters:
194: + obj1 - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
195: - obj2 - another PETSc object
197: Output Parameter:
198: . same - `PETSC_TRUE` if they are the same or both unset, else `PETSC_FALSE`
200: Level: intermediate
202: Note:
203: Both objects must be of the same class, for example both `Vec` objects.
205: .seealso: `PetscObjectTypeCompare()`, `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectBaseTypeCompare()`, `PetscObjectTypeCompareAny()`, `PetscObjectBaseTypeCompareAny()`
206: @*/
207: PetscErrorCode PetscObjectObjectTypeCompare(PetscObject obj1, PetscObject obj2, PetscBool *same)
208: {
209: PetscFunctionBegin;
212: PetscAssertPointer(same, 3);
213: PetscCall(PetscStrcmp(obj1->type_name, obj2->type_name, same));
214: PetscFunctionReturn(PETSC_SUCCESS);
215: }
217: /*@
218: PetscObjectBaseTypeCompare - Determines whether a `PetscObject` is of a given base type. For example the base type of `MATSEQAIJPERM` is `MATSEQAIJ`
220: Not Collective
222: Input Parameters:
223: + obj - the object
224: - type_name - string containing a type name
226: Output Parameter:
227: . same - `PETSC_TRUE` if the object is of the same base type identified by `type_name` or both `NULL`, `PETSC_FALSE` otherwise
229: Level: intermediate
231: .seealso: `PetscObject`, `PetscObjectTypeCompare()`, `PetscObjectTypeCompareAny()`, `PetscObjectBaseTypeCompareAny()`
232: @*/
233: PetscErrorCode PetscObjectBaseTypeCompare(PetscObject obj, const char type_name[], PetscBool *same)
234: {
235: PetscFunctionBegin;
236: PetscAssertPointer(same, 3);
237: if (!obj) *same = (PetscBool)!type_name;
238: else {
240: if (!type_name || !obj->type_name) *same = (PetscBool)(!obj->type_name == !type_name);
241: else {
242: PetscAssertPointer(type_name, 2);
243: PetscCall(PetscStrbeginswith(obj->type_name, type_name, same));
244: }
245: }
246: PetscFunctionReturn(PETSC_SUCCESS);
247: }
249: /*@C
250: PetscObjectTypeCompareAny - Determines whether a PETSc object is of any of a list of types.
252: Not Collective
254: Input Parameters:
255: + obj - a PETSc object, for example a `Vec`, `Mat` or `KSP`. It must be cast with a (`PetscObject`),
256: for example, `PetscObjectTypeCompareAny`((`PetscObject`)mat,...);
257: - type_name - one or more string arguments containing type names, pass the empty string "" as the last argument
259: Output Parameter:
260: . match - `PETSC_TRUE` if the type of `obj` matches any in the list, else `PETSC_FALSE`
262: Level: intermediate
264: .seealso: `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectTypeCompare()`, `PetscObjectBaseTypeCompare()`
265: @*/
266: PetscErrorCode PetscObjectTypeCompareAny(PetscObject obj, PetscBool *match, const char type_name[], ...)
267: {
268: va_list Argp;
270: PetscFunctionBegin;
271: PetscAssertPointer(match, 2);
272: *match = PETSC_FALSE;
273: if (!obj) PetscFunctionReturn(PETSC_SUCCESS);
274: va_start(Argp, type_name);
275: while (type_name && type_name[0]) {
276: PetscBool found;
277: PetscCall(PetscObjectTypeCompare(obj, type_name, &found));
278: if (found) {
279: *match = PETSC_TRUE;
280: break;
281: }
282: type_name = va_arg(Argp, const char *);
283: }
284: va_end(Argp);
285: PetscFunctionReturn(PETSC_SUCCESS);
286: }
288: /*@C
289: PetscObjectBaseTypeCompareAny - Determines whether a PETSc object has the base type of any of a list of types.
291: Not Collective
293: Input Parameters:
294: + obj - a PETSc object, for example a `Vec`, `Mat` or `KSP`. It must be cast with a (`PetscObject`),
295: for example, `PetscObjectBaseTypeCompareAny`((`PetscObject`)mat,...);
296: - type_name - one or more string arguments containing type names, pass the empty string "" as the last argument
298: Output Parameter:
299: . match - `PETSC_TRUE` if the type of `obj` matches any in the list, else `PETSC_FALSE`
301: Level: intermediate
303: .seealso: `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectTypeCompare()`, `PetscObjectBaseTypeCompare()`, `PetscObjectTypeCompareAny()`
304: @*/
305: PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject obj, PetscBool *match, const char type_name[], ...)
306: {
307: va_list Argp;
309: PetscFunctionBegin;
310: PetscAssertPointer(match, 2);
311: *match = PETSC_FALSE;
312: va_start(Argp, type_name);
313: while (type_name && type_name[0]) {
314: PetscBool found;
315: PetscCall(PetscObjectBaseTypeCompare(obj, type_name, &found));
316: if (found) {
317: *match = PETSC_TRUE;
318: break;
319: }
320: type_name = va_arg(Argp, const char *);
321: }
322: va_end(Argp);
323: PetscFunctionReturn(PETSC_SUCCESS);
324: }
326: typedef struct {
327: PetscErrorCode (*func)(void);
328: } PetscFinalizeFunction;
330: typedef struct {
331: PetscErrorCode (*func)(void *);
332: PetscCtx ctx;
333: } PetscFinalizeFunctionWithCtx;
335: typedef enum {
336: PETSC_FINALIZE_EMPTY,
337: PETSC_FINALIZE_OBJECT,
338: PETSC_FINALIZE_FUNC,
339: PETSC_FINALIZE_FUNC_WITH_CTX
340: } PetscFinalizeType;
342: static const char *const PetscFinalizeTypes[] = {"PETSC_FINALIZE_EMPTY", "PETSC_FINALIZE_OBJECT", "PETSC_FINALIZE_FUNC", "PETSC_FINALIZE_FUNC_WITH_CTX", PETSC_NULLPTR};
344: typedef struct {
345: union ThunkUnion
346: {
347: PetscObject obj;
348: PetscFinalizeFunction fn;
349: PetscFinalizeFunctionWithCtx fnctx;
350: } thunk;
351: PetscFinalizeType type;
352: } PetscFinalizerContainer;
354: #define PETSC_MAX_REGISTERED_FINALIZERS 256
355: static int reg_count = 0;
356: static PetscFinalizerContainer regfin[PETSC_MAX_REGISTERED_FINALIZERS];
358: static PetscErrorCode PetscRunRegisteredFinalizers(void)
359: {
360: PetscFunctionBegin;
361: while (reg_count) {
362: PetscFinalizerContainer top = regfin[--reg_count];
364: regfin[reg_count].type = PETSC_FINALIZE_EMPTY;
365: PetscCall(PetscArrayzero(®fin[reg_count].thunk, 1));
366: switch (top.type) {
367: case PETSC_FINALIZE_OBJECT:
368: PetscCall(PetscObjectDestroy(&top.thunk.obj));
369: break;
370: case PETSC_FINALIZE_FUNC:
371: PetscCall((*top.thunk.fn.func)());
372: break;
373: case PETSC_FINALIZE_FUNC_WITH_CTX:
374: PetscCall((*top.thunk.fnctx.func)(top.thunk.fnctx.ctx));
375: break;
376: case PETSC_FINALIZE_EMPTY:
377: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Finalizer at position %d is empty, yet registration count %d != 0", reg_count, reg_count);
378: break;
379: }
380: }
381: PetscFunctionReturn(PETSC_SUCCESS);
382: }
384: static int PetscFinalizerContainerEqual(const PetscFinalizerContainer *a, const PetscFinalizerContainer *b)
385: {
386: if (a->type != b->type) return 0;
387: switch (a->type) {
388: case PETSC_FINALIZE_EMPTY:
389: break;
390: case PETSC_FINALIZE_OBJECT:
391: return a->thunk.obj == b->thunk.obj;
392: case PETSC_FINALIZE_FUNC:
393: return a->thunk.fn.func == b->thunk.fn.func;
394: case PETSC_FINALIZE_FUNC_WITH_CTX:
395: return a->thunk.fnctx.func == b->thunk.fnctx.func && a->thunk.fnctx.ctx == b->thunk.fnctx.ctx;
396: }
397: return 1;
398: }
400: static PetscErrorCode RegisterFinalizer(PetscFinalizerContainer container)
401: {
402: PetscFunctionBegin;
403: PetscAssert(reg_count < (int)PETSC_STATIC_ARRAY_LENGTH(regfin), PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "No more room in array, limit %zu, recompile %s with larger value for " PetscStringize(regfin), PETSC_STATIC_ARRAY_LENGTH(regfin), __FILE__);
404: PetscAssert(regfin[reg_count].type == PETSC_FINALIZE_EMPTY, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Finalizer type (%s) at position %d is not PETSC_FINALIZE_EMPTY!", PetscFinalizeTypes[regfin[reg_count].type], reg_count);
405: if (PetscDefined(USE_DEBUG)) {
406: for (int i = 0; i < reg_count; ++i) PetscCheck(!PetscFinalizerContainerEqual(regfin + i, &container), PETSC_COMM_SELF, PETSC_ERR_ORDER, "Finalizer (of type %s) already registered!", PetscFinalizeTypes[container.type]);
407: }
408: regfin[reg_count++] = container;
409: PetscFunctionReturn(PETSC_SUCCESS);
410: }
412: /*@
413: PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
414: `PetscFinalize()` is called.
416: Logically Collective
418: Input Parameter:
419: . obj - a PETSc object, for example a `Vec`, `Mat` or `KSP`. It must be cast with a (`PetscObject`), for example,
420: `PetscObjectRegisterDestroy`((`PetscObject`)mat);
422: Level: developer
424: Notes:
425: This is used by, for example, `PETSC_VIEWER_XXX_()` routines to free the viewer
426: when PETSc ends.
428: This should be used only where there is no natural location in the code to put a call to the destructor of the object.
430: .seealso: `PetscObjectRegisterDestroyAll()`
431: @*/
432: PetscErrorCode PetscObjectRegisterDestroy(PetscObject obj)
433: {
434: PetscFinalizerContainer container;
436: PetscFunctionBegin;
438: container.thunk.obj = obj;
439: container.type = PETSC_FINALIZE_OBJECT;
440: PetscCall(RegisterFinalizer(container));
441: PetscFunctionReturn(PETSC_SUCCESS);
442: }
444: /*@C
445: PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
446: with `PetscObjectRegisterDestroy()`. Called by `PetscFinalize()`
448: Logically Collective on the individual `PetscObject`s that are being processed
450: Level: developer
452: .seealso: `PetscObjectRegisterDestroy()`
453: @*/
454: PetscErrorCode PetscObjectRegisterDestroyAll(void)
455: {
456: PetscFunctionBegin;
457: PetscCall(PetscRunRegisteredFinalizers());
458: PetscFunctionReturn(PETSC_SUCCESS);
459: }
461: /*@C
462: PetscRegisterFinalize - Registers a function that is to be called in `PetscFinalize()`
464: Not Collective
466: Input Parameter:
467: . f - function to be called
469: Level: developer
471: Notes:
472: This is used by, for example, `DMInitializePackage()` to have `DMFinalizePackage()` called
474: Use `PetscObjectRegisterDestroy()` to register the destruction of an object in `PetscFinalize()`
476: .seealso: `PetscRegisterFinalizeAll()`, `PetscObjectRegisterDestroy()`
477: @*/
478: PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*f)(void))
479: {
480: PetscFinalizerContainer container;
482: PetscFunctionBegin;
484: container.thunk.fn.func = f;
485: container.type = PETSC_FINALIZE_FUNC;
486: PetscCall(RegisterFinalizer(container));
487: PetscFunctionReturn(PETSC_SUCCESS);
488: }
490: /*@C
491: PetscRegisterFinalizeAll - Runs all the finalize functions set with `PetscRegisterFinalize()`
493: Not Collective except for registered functions that are collective
495: Level: developer
497: .seealso: `PetscRegisterFinalize()`, `PetscObjectRegisterDestroyAll()`
498: @*/
499: PetscErrorCode PetscRegisterFinalizeAll(void)
500: {
501: PetscFunctionBegin;
502: PetscCall(PetscRunRegisteredFinalizers());
503: PetscFunctionReturn(PETSC_SUCCESS);
504: }