Actual source code: destroy.c
petsc-3.3-p7 2013-05-11
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: PetscObjectTypeCompare - 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: PetscObjectTypeCompare((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^comparpeing
137: @*/
138: PetscErrorCode PetscObjectTypeCompare(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: PetscObjectTypeCompareAny - 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, PetscObjectTypeCompareAny((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(), PetscObjectTypeCompare()
177: Concepts: comparing^object types
178: Concepts: types^comparing
179: Concepts: object type^comparing
181: @*/
182: PetscErrorCode PetscObjectTypeCompareAny(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: PetscObjectTypeCompare(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 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);
235: return(0);
236: }
240: /*@C
241: PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
242: with PetscObjectRegisterDestroy(). Called by PetscFinalize()
244: Logically Collective on individual PetscObjects
246: Level: developer
248: .seealso: PetscObjectRegisterDestroy()
249: @*/
250: PetscErrorCode PetscObjectRegisterDestroyAll(void)
251: {
253: PetscInt i;
256: for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
257: PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i]);
258: }
259: PetscObjectRegisterDestroy_Count = 0;
260: return(0);
261: }
264: #define MAXREGFIN 256
265: static int PetscRegisterFinalize_Count = 0;
266: static PetscErrorCode ((*PetscRegisterFinalize_Functions[MAXREGFIN])(void));
270: /*@C
271: PetscRegisterFinalize - Registers a function that is to be called in PetscFinalize()
273: Not Collective
275: Input Parameter:
276: . PetscErrorCode (*fun)(void) -
278: Level: developer
280: Notes:
281: This is used by, for example, DMInitializePackage() to have DMFinalizePackage() called
283: .seealso: PetscRegisterFinalizeAll()
284: @*/
285: PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*f)(void))
286: {
289: if (PetscRegisterFinalize_Count < MAXREGFIN) {
290: PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
291: } 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);
292: return(0);
293: }
297: /*@C
298: PetscRegisterFinalizeAll - Runs all the finalize functions set with PetscRegisterFinalize()
300: Not Collective unless registered functions are collective
302: Level: developer
304: .seealso: PetscRegisterFinalize()
305: @*/
306: PetscErrorCode PetscRegisterFinalizeAll(void)
307: {
309: PetscInt i;
312: for (i=0; i<PetscRegisterFinalize_Count; i++) {
313: (*PetscRegisterFinalize_Functions[i])();
314: }
315: PetscRegisterFinalize_Count = 0;
316: return(0);
317: }