Actual source code: destroy.c
petsc-3.10.5 2019-03-28
2: /*
3: Provides utility routines for manulating any type of PETSc object.
4: */
5: #include <petsc/private/petscimpl.h>
6: #include <petscviewer.h>
8: PetscErrorCode PetscComposedQuantitiesDestroy(PetscObject obj)
9: {
11: PetscInt i;
14: if (obj->intstar_idmax>0) {
15: for (i=0; i<obj->intstar_idmax; i++) {
16: PetscFree(obj->intstarcomposeddata[i]);
17: }
18: PetscFree(obj->intstarcomposeddata);
19: PetscFree(obj->intstarcomposedstate);
20: }
21: if (obj->realstar_idmax>0) {
22: for (i=0; i<obj->realstar_idmax; i++) {
23: PetscFree(obj->realstarcomposeddata[i]);
24: }
25: PetscFree(obj->realstarcomposeddata);
26: PetscFree(obj->realstarcomposedstate);
27: }
28: if (obj->scalarstar_idmax>0) {
29: for (i=0; i<obj->scalarstar_idmax; i++) {
30: PetscFree(obj->scalarstarcomposeddata[i]);
31: }
32: PetscFree(obj->scalarstarcomposeddata);
33: PetscFree(obj->scalarstarcomposedstate);
34: }
35: PetscFree(obj->intcomposeddata);
36: PetscFree(obj->intcomposedstate);
37: PetscFree(obj->realcomposeddata);
38: PetscFree(obj->realcomposedstate);
39: PetscFree(obj->scalarcomposeddata);
40: PetscFree(obj->scalarcomposedstate);
41: return(0);
42: }
44: /*@
45: PetscObjectDestroy - Destroys any PetscObject, regardless of the type.
47: Collective on PetscObject
49: Input Parameter:
50: . obj - any PETSc object, for example a Vec, Mat or KSP.
51: This must be cast with a (PetscObject*), for example,
52: PetscObjectDestroy((PetscObject*)&mat);
54: Level: beginner
56: Concepts: destroying object
57: Concepts: freeing object
58: Concepts: deleting object
60: @*/
61: PetscErrorCode PetscObjectDestroy(PetscObject *obj)
62: {
66: if (!*obj) return(0);
68: if (*obj && (*obj)->bops->destroy) {
69: (*(*obj)->bops->destroy)(obj);
70: } else if (*obj) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"This PETSc object of class %s does not have a generic destroy routine",(*obj)->class_name);
71: return(0);
72: }
74: /*@C
75: PetscObjectView - Views any PetscObject, regardless of the type.
77: Collective on PetscObject
79: Input Parameters:
80: + obj - any PETSc object, for example a Vec, Mat or KSP.
81: This must be cast with a (PetscObject), for example,
82: PetscObjectView((PetscObject)mat,viewer);
83: - viewer - any PETSc viewer
85: Level: intermediate
87: @*/
88: PetscErrorCode PetscObjectView(PetscObject obj,PetscViewer viewer)
89: {
94: if (!viewer) {
95: PetscViewerASCIIGetStdout(obj->comm,&viewer);
96: }
99: if (obj->bops->view) {
100: (*obj->bops->view)(obj,viewer);
101: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
102: return(0);
103: }
105: #define CHKERRQI(incall,ierr) if (ierr) {incall = PETSC_FALSE; }
107: /*@C
108: PetscObjectViewFromOptions - Processes command line options to determine if/how a PetscObject is to be viewed.
110: Collective on PetscObject
112: Input Parameters:
113: + obj - the object
114: . bobj - optional other object that provides prefix (if NULL then the prefix in obj is used)
115: - optionname - option to activate viewing
117: Level: intermediate
119: @*/
120: PetscErrorCode PetscObjectViewFromOptions(PetscObject obj,PetscObject bobj,const char optionname[])
121: {
122: PetscErrorCode ierr;
123: PetscViewer viewer;
124: PetscBool flg;
125: static PetscBool incall = PETSC_FALSE;
126: PetscViewerFormat format;
127: const char *prefix;
130: if (incall) return(0);
131: incall = PETSC_TRUE;
132: prefix = bobj ? bobj->prefix : obj->prefix;
133: PetscOptionsGetViewer(PetscObjectComm((PetscObject)obj),prefix,optionname,&viewer,&format,&flg);CHKERRQI(incall,ierr);
134: if (flg) {
135: PetscViewerPushFormat(viewer,format);CHKERRQI(incall,ierr);
136: PetscObjectView(obj,viewer);CHKERRQI(incall,ierr);
137: PetscViewerPopFormat(viewer);CHKERRQI(incall,ierr);
138: PetscViewerDestroy(&viewer);CHKERRQI(incall,ierr);
139: }
140: incall = PETSC_FALSE;
141: return(0);
142: }
144: /*@C
145: PetscObjectTypeCompare - Determines whether a PETSc object is of a particular type.
147: Not Collective
149: Input Parameters:
150: + obj - any PETSc object, for example a Vec, Mat or KSP.
151: This must be cast with a (PetscObject), for example,
152: PetscObjectTypeCompare((PetscObject)mat);
153: - type_name - string containing a type name
155: Output Parameter:
156: . same - PETSC_TRUE if they are the same, else PETSC_FALSE
158: Level: intermediate
160: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType(), PetscObjectBaseTypeCompare()
162: Concepts: comparing^object types
163: Concepts: types^comparing
164: Concepts: object type^comparpeing
166: @*/
167: PetscErrorCode PetscObjectTypeCompare(PetscObject obj,const char type_name[],PetscBool *same)
168: {
172: if (!obj) *same = PETSC_FALSE;
173: else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
174: else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
175: else {
179: PetscStrcmp((char*)(obj->type_name),type_name,same);
180: }
181: return(0);
182: }
184: /*@C
185: PetscObjectBaseTypeCompare - Determines whether a PetscObject is of a given base type. For example the base type of MATSEQAIJPERM is MATSEQAIJ
187: Not Collective
189: Input Parameters:
190: + mat - the matrix
191: - type_name - string containing a type name
193: Output Parameter:
194: . same - PETSC_TRUE if it is of the same base type
196: Level: intermediate
198: .seealso: PetscObjectTypeCompare()
201: @*/
202: PetscErrorCode PetscObjectBaseTypeCompare(PetscObject obj,const char type_name[],PetscBool *same)
203: {
207: if (!obj) *same = PETSC_FALSE;
208: else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
209: else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
210: else {
214: PetscStrbeginswith((char*)(obj->type_name),type_name,same);
215: }
216: return(0);
217: }
219: /*@C
220: PetscObjectTypeCompareAny - Determines whether a PETSc object is of any of a list of types.
222: Not Collective
224: Input Parameters:
225: + obj - any PETSc object, for example a Vec, Mat or KSP.
226: This must be cast with a (PetscObject), for example, PetscObjectTypeCompareAny((PetscObject)mat,...);
227: - type_name - string containing a type name, pass the empty string "" to terminate the list
229: Output Parameter:
230: . match - PETSC_TRUE if the type of obj matches any in the list, else PETSC_FALSE
232: Level: intermediate
234: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType(), PetscObjectTypeCompare()
236: Concepts: comparing^object types
237: Concepts: types^comparing
238: Concepts: object type^comparing
240: @*/
241: PetscErrorCode PetscObjectTypeCompareAny(PetscObject obj,PetscBool *match,const char type_name[],...)
242: {
244: va_list Argp;
247: *match = PETSC_FALSE;
248: va_start(Argp,type_name);
249: while (type_name && type_name[0]) {
250: PetscBool found;
251: PetscObjectTypeCompare(obj,type_name,&found);
252: if (found) {
253: *match = PETSC_TRUE;
254: break;
255: }
256: type_name = va_arg(Argp,const char*);
257: }
258: va_end(Argp);
259: return(0);
260: }
262: #define MAXREGDESOBJS 256
263: static int PetscObjectRegisterDestroy_Count = 0;
264: static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];
266: /*@C
267: PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
268: PetscFinalize() is called.
270: Logically Collective on PetscObject
272: Input Parameter:
273: . obj - any PETSc object, for example a Vec, Mat or KSP.
274: This must be cast with a (PetscObject), for example,
275: PetscObjectRegisterDestroy((PetscObject)mat);
277: Level: developer
279: Notes:
280: This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
281: when PETSc ends.
283: .seealso: PetscObjectRegisterDestroyAll()
284: @*/
285: PetscErrorCode PetscObjectRegisterDestroy(PetscObject obj)
286: {
289: if (PetscObjectRegisterDestroy_Count < MAXREGDESOBJS) PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
290: else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"No more room in array, limit %d \n recompile src/sys/objects/destroy.c with larger value for MAXREGDESOBJS\n",MAXREGDESOBJS);
291: return(0);
292: }
294: /*@C
295: PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
296: with PetscObjectRegisterDestroy(). Called by PetscFinalize()
298: Logically Collective on individual PetscObjects
300: Level: developer
302: .seealso: PetscObjectRegisterDestroy()
303: @*/
304: PetscErrorCode PetscObjectRegisterDestroyAll(void)
305: {
307: PetscInt i;
310: for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
311: PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i]);
312: }
313: PetscObjectRegisterDestroy_Count = 0;
314: return(0);
315: }
318: #define MAXREGFIN 256
319: static int PetscRegisterFinalize_Count = 0;
320: static PetscErrorCode (*PetscRegisterFinalize_Functions[MAXREGFIN])(void);
322: /*@C
323: PetscRegisterFinalize - Registers a function that is to be called in PetscFinalize()
325: Not Collective
327: Input Parameter:
328: . PetscErrorCode (*fun)(void) -
330: Level: developer
332: Notes:
333: This is used by, for example, DMInitializePackage() to have DMFinalizePackage() called
335: .seealso: PetscRegisterFinalizeAll()
336: @*/
337: PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*f)(void))
338: {
339: PetscInt i;
342: for (i=0; i<PetscRegisterFinalize_Count; i++) {
343: if (f == PetscRegisterFinalize_Functions[i]) return(0);
344: }
345: if (PetscRegisterFinalize_Count < MAXREGFIN) PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
346: else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"No more room in array, limit %d \n recompile src/sys/objects/destroy.c with larger value for MAXREGFIN\n",MAXREGFIN);
347: return(0);
348: }
350: /*@C
351: PetscRegisterFinalizeAll - Runs all the finalize functions set with PetscRegisterFinalize()
353: Not Collective unless registered functions are collective
355: Level: developer
357: .seealso: PetscRegisterFinalize()
358: @*/
359: PetscErrorCode PetscRegisterFinalizeAll(void)
360: {
362: PetscInt i;
365: for (i=0; i<PetscRegisterFinalize_Count; i++) {
366: (*PetscRegisterFinalize_Functions[i])();
367: }
368: PetscRegisterFinalize_Count = 0;
369: return(0);
370: }