Actual source code: destroy.c
petsc-3.9.4 2018-09-11
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: /*@C
106: PetscObjectTypeCompare - Determines whether a PETSc object is of a particular type.
108: Not Collective
110: Input Parameters:
111: + obj - any PETSc object, for example a Vec, Mat or KSP.
112: This must be cast with a (PetscObject), for example,
113: PetscObjectTypeCompare((PetscObject)mat);
114: - type_name - string containing a type name
116: Output Parameter:
117: . same - PETSC_TRUE if they are the same, else PETSC_FALSE
119: Level: intermediate
121: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType(), PetscObjectBaseTypeCompare()
123: Concepts: comparing^object types
124: Concepts: types^comparing
125: Concepts: object type^comparpeing
127: @*/
128: PetscErrorCode PetscObjectTypeCompare(PetscObject obj,const char type_name[],PetscBool *same)
129: {
133: if (!obj) *same = PETSC_FALSE;
134: else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
135: else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
136: else {
140: PetscStrcmp((char*)(obj->type_name),type_name,same);
141: }
142: return(0);
143: }
145: /*@C
146: PetscObjectBaseTypeCompare - Determines whether a PetscObject is of a given base type. For example the base type of MATSEQAIJPERM is MATSEQAIJ
148: Not Collective
150: Input Parameters:
151: + mat - the matrix
152: - type_name - string containing a type name
154: Output Parameter:
155: . same - PETSC_TRUE if it is of the same base type
157: Level: intermediate
159: .seealso: PetscObjectTypeCompare()
162: @*/
163: PetscErrorCode PetscObjectBaseTypeCompare(PetscObject obj,const char type_name[],PetscBool *same)
164: {
168: if (!obj) *same = PETSC_FALSE;
169: else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
170: else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
171: else {
175: PetscStrbeginswith((char*)(obj->type_name),type_name,same);
176: }
177: return(0);
178: }
180: /*@C
181: PetscObjectTypeCompareAny - Determines whether a PETSc object is of any of a list of types.
183: Not Collective
185: Input Parameters:
186: + obj - any PETSc object, for example a Vec, Mat or KSP.
187: This must be cast with a (PetscObject), for example, PetscObjectTypeCompareAny((PetscObject)mat,...);
188: - type_name - string containing a type name, pass the empty string "" to terminate the list
190: Output Parameter:
191: . match - PETSC_TRUE if the type of obj matches any in the list, else PETSC_FALSE
193: Level: intermediate
195: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType(), PetscObjectTypeCompare()
197: Concepts: comparing^object types
198: Concepts: types^comparing
199: Concepts: object type^comparing
201: @*/
202: PetscErrorCode PetscObjectTypeCompareAny(PetscObject obj,PetscBool *match,const char type_name[],...)
203: {
205: va_list Argp;
208: *match = PETSC_FALSE;
209: va_start(Argp,type_name);
210: while (type_name && type_name[0]) {
211: PetscBool found;
212: PetscObjectTypeCompare(obj,type_name,&found);
213: if (found) {
214: *match = PETSC_TRUE;
215: break;
216: }
217: type_name = va_arg(Argp,const char*);
218: }
219: va_end(Argp);
220: return(0);
221: }
223: #define MAXREGDESOBJS 256
224: static int PetscObjectRegisterDestroy_Count = 0;
225: static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];
227: /*@C
228: PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
229: PetscFinalize() is called.
231: Logically Collective on PetscObject
233: Input Parameter:
234: . obj - any PETSc object, for example a Vec, Mat or KSP.
235: This must be cast with a (PetscObject), for example,
236: PetscObjectRegisterDestroy((PetscObject)mat);
238: Level: developer
240: Notes:
241: This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
242: when PETSc ends.
244: .seealso: PetscObjectRegisterDestroyAll()
245: @*/
246: PetscErrorCode PetscObjectRegisterDestroy(PetscObject obj)
247: {
250: if (PetscObjectRegisterDestroy_Count < MAXREGDESOBJS) PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
251: 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);
252: return(0);
253: }
255: /*@C
256: PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
257: with PetscObjectRegisterDestroy(). Called by PetscFinalize()
259: Logically Collective on individual PetscObjects
261: Level: developer
263: .seealso: PetscObjectRegisterDestroy()
264: @*/
265: PetscErrorCode PetscObjectRegisterDestroyAll(void)
266: {
268: PetscInt i;
271: for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
272: PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i]);
273: }
274: PetscObjectRegisterDestroy_Count = 0;
275: return(0);
276: }
279: #define MAXREGFIN 256
280: static int PetscRegisterFinalize_Count = 0;
281: static PetscErrorCode (*PetscRegisterFinalize_Functions[MAXREGFIN])(void);
283: /*@C
284: PetscRegisterFinalize - Registers a function that is to be called in PetscFinalize()
286: Not Collective
288: Input Parameter:
289: . PetscErrorCode (*fun)(void) -
291: Level: developer
293: Notes:
294: This is used by, for example, DMInitializePackage() to have DMFinalizePackage() called
296: .seealso: PetscRegisterFinalizeAll()
297: @*/
298: PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*f)(void))
299: {
300: PetscInt i;
303: for (i=0; i<PetscRegisterFinalize_Count; i++) {
304: if (f == PetscRegisterFinalize_Functions[i]) return(0);
305: }
306: if (PetscRegisterFinalize_Count < MAXREGFIN) PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
307: 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);
308: return(0);
309: }
311: /*@C
312: PetscRegisterFinalizeAll - Runs all the finalize functions set with PetscRegisterFinalize()
314: Not Collective unless registered functions are collective
316: Level: developer
318: .seealso: PetscRegisterFinalize()
319: @*/
320: PetscErrorCode PetscRegisterFinalizeAll(void)
321: {
323: PetscInt i;
326: for (i=0; i<PetscRegisterFinalize_Count; i++) {
327: (*PetscRegisterFinalize_Functions[i])();
328: }
329: PetscRegisterFinalize_Count = 0;
330: return(0);
331: }