Actual source code: destroy.c
petsc-3.6.4 2016-04-12
2: /*
3: Provides utility routines for manulating any type of PETSc object.
4: */
5: #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/
6: #include <petscviewer.h>
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 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
108: return(0);
109: }
113: /*@C
114: PetscObjectTypeCompare - Determines whether a PETSc object is of a particular type.
116: Not Collective
118: Input Parameters:
119: + obj - any PETSc object, for example a Vec, Mat or KSP.
120: This must be cast with a (PetscObject), for example,
121: PetscObjectTypeCompare((PetscObject)mat);
122: - type_name - string containing a type name
124: Output Parameter:
125: . same - PETSC_TRUE if they are the same, else PETSC_FALSE
127: Level: intermediate
129: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType()
131: Concepts: comparing^object types
132: Concepts: types^comparing
133: Concepts: object type^comparpeing
135: @*/
136: PetscErrorCode PetscObjectTypeCompare(PetscObject obj,const char type_name[],PetscBool *same)
137: {
141: if (!obj) *same = PETSC_FALSE;
142: else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
143: else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
144: else {
148: PetscStrcmp((char*)(obj->type_name),type_name,same);
149: }
150: return(0);
151: }
155: /*@C
156: PetscObjectTypeCompareAny - Determines whether a PETSc object is of any of a list of types.
158: Not Collective
160: Input Parameters:
161: + obj - any PETSc object, for example a Vec, Mat or KSP.
162: This must be cast with a (PetscObject), for example, PetscObjectTypeCompareAny((PetscObject)mat,...);
163: - type_name - string containing a type name, pass the empty string "" to terminate the list
165: Output Parameter:
166: . match - PETSC_TRUE if the type of obj matches any in the list, else PETSC_FALSE
168: Level: intermediate
170: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType(), PetscObjectTypeCompare()
172: Concepts: comparing^object types
173: Concepts: types^comparing
174: Concepts: object type^comparing
176: @*/
177: PetscErrorCode PetscObjectTypeCompareAny(PetscObject obj,PetscBool *match,const char type_name[],...)
178: {
180: va_list Argp;
183: *match = PETSC_FALSE;
184: va_start(Argp,type_name);
185: while (type_name && type_name[0]) {
186: PetscBool found;
187: PetscObjectTypeCompare(obj,type_name,&found);
188: if (found) {
189: *match = PETSC_TRUE;
190: break;
191: }
192: type_name = va_arg(Argp,const char*);
193: }
194: va_end(Argp);
195: return(0);
196: }
198: #define MAXREGDESOBJS 256
199: static int PetscObjectRegisterDestroy_Count = 0;
200: static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];
204: /*@C
205: PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
206: PetscFinalize() is called.
208: Logically Collective on PetscObject
210: Input Parameter:
211: . obj - any PETSc object, for example a Vec, Mat or KSP.
212: This must be cast with a (PetscObject), for example,
213: PetscObjectRegisterDestroy((PetscObject)mat);
215: Level: developer
217: Notes:
218: This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
219: when PETSc ends.
221: .seealso: PetscObjectRegisterDestroyAll()
222: @*/
223: PetscErrorCode PetscObjectRegisterDestroy(PetscObject obj)
224: {
227: if (PetscObjectRegisterDestroy_Count < MAXREGDESOBJS) PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
228: 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);
229: return(0);
230: }
234: /*@C
235: PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
236: with PetscObjectRegisterDestroy(). Called by PetscFinalize()
238: Logically Collective on individual PetscObjects
240: Level: developer
242: .seealso: PetscObjectRegisterDestroy()
243: @*/
244: PetscErrorCode PetscObjectRegisterDestroyAll(void)
245: {
247: PetscInt i;
250: for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
251: PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i]);
252: }
253: PetscObjectRegisterDestroy_Count = 0;
254: return(0);
255: }
258: #define MAXREGFIN 256
259: static int PetscRegisterFinalize_Count = 0;
260: static PetscErrorCode ((*PetscRegisterFinalize_Functions[MAXREGFIN])(void));
264: /*@C
265: PetscRegisterFinalize - Registers a function that is to be called in PetscFinalize()
267: Not Collective
269: Input Parameter:
270: . PetscErrorCode (*fun)(void) -
272: Level: developer
274: Notes:
275: This is used by, for example, DMInitializePackage() to have DMFinalizePackage() called
277: .seealso: PetscRegisterFinalizeAll()
278: @*/
279: PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*f)(void))
280: {
281: PetscInt i;
284: for (i=0; i<PetscRegisterFinalize_Count; i++) {
285: if (f == PetscRegisterFinalize_Functions[i]) return(0);
286: }
287: if (PetscRegisterFinalize_Count < MAXREGFIN) PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
288: 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);
289: return(0);
290: }
294: /*@C
295: PetscRegisterFinalizeAll - Runs all the finalize functions set with PetscRegisterFinalize()
297: Not Collective unless registered functions are collective
299: Level: developer
301: .seealso: PetscRegisterFinalize()
302: @*/
303: PetscErrorCode PetscRegisterFinalizeAll(void)
304: {
306: PetscInt i;
309: for (i=0; i<PetscRegisterFinalize_Count; i++) {
310: (*PetscRegisterFinalize_Functions[i])();
311: }
312: PetscRegisterFinalize_Count = 0;
313: return(0);
314: }