Actual source code: destroy.c
2: /*
3: Provides utility routines for manulating any type of PETSc object.
4: */
5: #include <petscsys.h> /*I "petscsys.h" I*/
10: PetscErrorCode PetscComposedQuantitiesDestroy(PetscObject obj)
11: {
13: PetscInt i;
16: if (obj->intstar_idmax>0) {
17: for (i=0; i<obj->intstar_idmax; i++) {
18: PetscFree(obj->intstarcomposeddata[i]);
19: }
20: PetscFree(obj->intstarcomposeddata);
21: PetscFree(obj->intstarcomposedstate);
22: }
23: if (obj->realstar_idmax>0) {
24: for (i=0; i<obj->realstar_idmax; i++) {
25: PetscFree(obj->realstarcomposeddata[i]);
26: }
27: PetscFree(obj->realstarcomposeddata);
28: PetscFree(obj->realstarcomposedstate);
29: }
30: if (obj->scalarstar_idmax>0) {
31: for (i=0; i<obj->scalarstar_idmax; i++) {
32: PetscFree(obj->scalarstarcomposeddata[i]);
33: }
34: PetscFree(obj->scalarstarcomposeddata);
35: PetscFree(obj->scalarstarcomposedstate);
36: }
37: PetscFree(obj->intcomposeddata);
38: PetscFree(obj->intcomposedstate);
39: PetscFree(obj->realcomposeddata);
40: PetscFree(obj->realcomposedstate);
41: PetscFree(obj->scalarcomposeddata);
42: PetscFree(obj->scalarcomposedstate);
43: return(0);
44: }
48: /*@
49: PetscObjectDestroy - Destroys any PetscObject, regardless of the type.
51: Collective on PetscObject
53: Input Parameter:
54: . obj - any PETSc object, for example a Vec, Mat or KSP.
55: This must be cast with a (PetscObject*), for example,
56: PetscObjectDestroy((PetscObject*)&mat);
58: Level: beginner
60: Concepts: destroying object
61: Concepts: freeing object
62: Concepts: deleting object
64: @*/
65: PetscErrorCode PetscObjectDestroy(PetscObject *obj)
66: {
70: if (!*obj) return(0);
72: if (*obj && (*obj)->bops->destroy) {
73: (*(*obj)->bops->destroy)(obj);
74: } 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);
75: return(0);
76: }
80: /*@C
81: PetscObjectView - Views any PetscObject, regardless of the type.
83: Collective on PetscObject
85: Input Parameters:
86: + obj - any PETSc object, for example a Vec, Mat or KSP.
87: This must be cast with a (PetscObject), for example,
88: PetscObjectView((PetscObject)mat,viewer);
89: - viewer - any PETSc viewer
91: Level: intermediate
93: @*/
94: PetscErrorCode PetscObjectView(PetscObject obj,PetscViewer viewer)
95: {
100: if (!viewer) {
101: PetscViewerASCIIGetStdout(obj->comm,&viewer);
102: }
105: if (obj->bops->view) {
106: (*obj->bops->view)(obj,viewer);
107: } else {
108: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
109: }
110: return(0);
111: }
115: /*@C
116: PetscTypeCompare - Determines whether a PETSc object is of a particular type.
118: Not Collective
120: Input Parameters:
121: + obj - any PETSc object, for example a Vec, Mat or KSP.
122: This must be cast with a (PetscObject), for example,
123: PetscTypeCompare((PetscObject)mat);
124: - type_name - string containing a type name
126: Output Parameter:
127: . same - PETSC_TRUE if they are the same, else PETSC_FALSE
128:
129: Level: intermediate
131: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType()
133: Concepts: comparing^object types
134: Concepts: types^comparing
135: Concepts: object type^comparing
137: @*/
138: PetscErrorCode PetscTypeCompare(PetscObject obj,const char type_name[],PetscBool *same)
139: {
143: if (!obj) {
144: *same = PETSC_FALSE;
145: } else if (!type_name && !obj->type_name) {
146: *same = PETSC_TRUE;
147: } else if (!type_name || !obj->type_name) {
148: *same = PETSC_FALSE;
149: } else {
153: PetscStrcmp((char*)(obj->type_name),type_name,same);
154: }
155: return(0);
156: }
160: /*@C
161: PetscTypeCompareAny - Determines whether a PETSc object is of any of a list of types.
163: Not Collective
165: Input Parameters:
166: + obj - any PETSc object, for example a Vec, Mat or KSP.
167: This must be cast with a (PetscObject), for example, PetscTypeCompareAny((PetscObject)mat,...);
168: - type_name - string containing a type name, pass the empty string "" to terminate the list
170: Output Parameter:
171: . match - PETSC_TRUE if the type of obj matches any in the list, else PETSC_FALSE
173: Level: intermediate
175: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType(), PetscTypeCompare()
177: Concepts: comparing^object types
178: Concepts: types^comparing
179: Concepts: object type^comparing
181: @*/
182: PetscErrorCode PetscTypeCompareAny(PetscObject obj,PetscBool *match,const char type_name[],...)
183: {
185: va_list Argp;
188: *match = PETSC_FALSE;
189: va_start(Argp,type_name);
190: while (type_name && type_name[0]) {
191: PetscBool found;
192: PetscTypeCompare(obj,type_name,&found);
193: if (found) {
194: *match = PETSC_TRUE;
195: break;
196: }
197: type_name = va_arg(Argp,const char*);
198: }
199: va_end(Argp);
200: return(0);
201: }
203: #define MAXREGDESOBJS 256
204: static int PetscObjectRegisterDestroy_Count = 0;
205: static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];
209: /*@C
210: PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
211: PetscFinalize() is called.
213: Logically Collective on PetscObject
215: Input Parameter:
216: . obj - any PETSc object, for example a Vec, Mat or KSP.
217: This must be cast with a (PetscObject), for example,
218: PetscObjectRegisterDestroy((PetscObject)mat);
220: Level: developer
222: Notes:
223: This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
224: when PETSc ends.
226: .seealso: PetscObjectRegisterDestroyAll()
227: @*/
228: PetscErrorCode PetscObjectRegisterDestroy(PetscObject obj)
229: {
232: if (PetscObjectRegisterDestroy_Count < MAXREGDESOBJS) {
233: PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
234: } else {
235: 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);
236:
237: }
238: return(0);
239: }
243: /*@C
244: PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
245: with PetscObjectRegisterDestroy(). Called by PetscFinalize()
247: Logically Collective on individual PetscObjects
249: Level: developer
251: .seealso: PetscObjectRegisterDestroy()
252: @*/
253: PetscErrorCode PetscObjectRegisterDestroyAll(void)
254: {
256: int i;
259: for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
260: PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i]);
261: }
262: PetscObjectRegisterDestroy_Count = 0;
263: return(0);
264: }
267: #define MAXREGFIN 256
268: static int PetscRegisterFinalize_Count = 0;
269: static PetscErrorCode ((*PetscRegisterFinalize_Functions[MAXREGFIN])(void));
273: /*@C
274: PetscRegisterFinalize - Registers a function that is to be called in PetscFinalize()
276: Not Collective
278: Input Parameter:
279: . PetscErrorCode (*fun)(void) -
281: Level: developer
283: Notes:
284: This is used by, for example, DMInitializePackage() to have DMFinalizePackage() called
286: .seealso: PetscRegisterFinalizeAll()
287: @*/
288: PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*f)(void))
289: {
292: if (PetscRegisterFinalize_Count < MAXREGFIN) {
293: PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
294: } else {
295: 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);
296:
297: }
298: return(0);
299: }
303: /*@C
304: PetscRegisterFinalizeAll - Runs all the finalize functions set with PetscRegisterFinalize()
306: Not Collective unless registered functions are collective
308: Level: developer
310: .seealso: PetscRegisterFinalize()
311: @*/
312: PetscErrorCode PetscRegisterFinalizeAll(void)
313: {
315: int i;
318: for (i=0; i<PetscRegisterFinalize_Count; i++) {
319: (*PetscRegisterFinalize_Functions[i])();
320: }
321: PetscRegisterFinalize_Count = 0;
322: return(0);
323: }