xref: /petsc/src/sys/objects/destroy.c (revision e32f2f54e699d0aa6e733466c00da7e34666fe5e)
1 #define PETSC_DLL
2 /*
3      Provides utility routines for manulating any type of PETSc object.
4 */
5 #include "petscsys.h"  /*I   "petscsys.h"    I*/
6 
7 typedef struct _p_GenericObject* GenericObject;
8 
9 struct _p_GenericObject {
10   PETSCHEADER(int);
11 };
12 
13 PetscErrorCode PetscObjectDestroy_GenericObject(GenericObject obj)
14 {
15   PetscErrorCode ierr;
16   PetscFunctionBegin;
17   PetscValidHeader(obj,1);
18   if (--((PetscObject)obj)->refct > 0) PetscFunctionReturn(0);
19   ierr = PetscHeaderDestroy(obj);CHKERRQ(ierr);
20   PetscFunctionReturn(0);
21 }
22 
23 PetscErrorCode PetscComposedQuantitiesDestroy(PetscObject obj)
24 {
25   PetscErrorCode ierr; int i;
26   PetscFunctionBegin;
27   if (obj->intstar_idmax>0) {
28     for (i=0; i<obj->intstar_idmax; i++) {
29       if (obj->intstarcomposeddata[i]) {
30 	ierr = PetscFree(obj->intstarcomposeddata[i]);CHKERRQ(ierr);
31 	obj->intstarcomposeddata[i] = 0;
32       }
33     }
34     ierr = PetscFree(obj->intstarcomposeddata);CHKERRQ(ierr);
35     ierr = PetscFree(obj->intstarcomposedstate);CHKERRQ(ierr);
36   }
37   if (obj->realstar_idmax>0) {
38     for (i=0; i<obj->realstar_idmax; i++) {
39       if (obj->realstarcomposeddata[i]) {
40 	ierr = PetscFree(obj->realstarcomposeddata[i]);CHKERRQ(ierr);
41 	obj->realstarcomposeddata[i] = 0;
42       }
43     }
44     ierr = PetscFree(obj->realstarcomposeddata);CHKERRQ(ierr);
45     ierr = PetscFree(obj->realstarcomposedstate);CHKERRQ(ierr);
46   }
47   if (obj->scalarstar_idmax>0) {
48     for (i=0; i<obj->scalarstar_idmax; i++) {
49       if (obj->scalarstarcomposeddata[i]) {
50 	ierr = PetscFree(obj->scalarstarcomposeddata[i]);CHKERRQ(ierr);
51 	obj->scalarstarcomposeddata[i] = 0;
52       }
53     }
54     ierr = PetscFree(obj->scalarstarcomposeddata);CHKERRQ(ierr);
55     ierr = PetscFree(obj->scalarstarcomposedstate);CHKERRQ(ierr);
56   }
57   PetscFunctionReturn(0);
58 }
59 
60 #undef __FUNCT__
61 #define __FUNCT__ "PetscObjectCreate"
62 /*@C
63    PetscObjectCreate - Creates a PetscObject
64 
65    Collective on PetscObject
66 
67    Input Parameter:
68 .  comm - An MPI communicator
69 
70    Output Parameter:
71 .  obj - The object
72 
73    Level: developer
74 
75    Notes: This is a template intended as a starting point to cut and paste with PetscObjectDestroy_GenericObject()
76           to make new object classes.
77 
78     Concepts: destroying object
79     Concepts: freeing object
80     Concepts: deleting object
81 
82 @*/
83 PetscErrorCode PETSC_DLLEXPORT PetscObjectCreate(MPI_Comm comm, PetscObject *obj)
84 {
85   GenericObject  o;
86   PetscErrorCode ierr;
87 
88   PetscFunctionBegin;
89   PetscValidPointer(obj,2);
90 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
91   ierr = PetscInitializePackage(PETSC_NULL);CHKERRQ(ierr);
92 #endif
93   ierr = PetscHeaderCreate(o,_p_GenericObject,-1,PETSC_OBJECT_CLASSID,0,"PetscObject",comm,PetscObjectDestroy_GenericObject,0);CHKERRQ(ierr);
94   /* records not yet defined in PetscObject
95   o->data        = 0;
96   o->setupcalled = 0;
97   */
98   *obj = (PetscObject)o;
99   PetscFunctionReturn(0);
100 }
101 
102 #undef __FUNCT__
103 #define __FUNCT__ "PetscObjectCreateGeneric"
104 /*@C
105    PetscObjectCreateGeneric - Creates a PetscObject
106 
107    Collective on PetscObject
108 
109    Input Parameter:
110 +  comm - An MPI communicator
111 .  classid - The class classid
112 -  name - The class name
113 
114    Output Parameter:
115 .  obj - The object
116 
117    Level: developer
118 
119    Notes: This is a template intended as a starting point to cut and paste with PetscObjectDestroy_GenericObject()
120           to make new object classes.
121 
122     Concepts: destroying object
123     Concepts: freeing object
124     Concepts: deleting object
125 
126 @*/
127 PetscErrorCode PETSC_DLLEXPORT PetscObjectCreateGeneric(MPI_Comm comm, PetscClassId classid, const char name[], PetscObject *obj)
128 {
129   GenericObject  o;
130   PetscErrorCode ierr;
131 
132   PetscFunctionBegin;
133   PetscValidPointer(obj,2);
134 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
135   ierr = PetscInitializePackage(PETSC_NULL);CHKERRQ(ierr);
136 #endif
137   ierr = PetscHeaderCreate(o,_p_GenericObject,-1,classid,0,name,comm,PetscObjectDestroy_GenericObject,0);CHKERRQ(ierr);
138   /* records not yet defined in PetscObject
139   o->data        = 0;
140   o->setupcalled = 0;
141   */
142   *obj = (PetscObject)o;
143   PetscFunctionReturn(0);
144 }
145 
146 #undef __FUNCT__
147 #define __FUNCT__ "PetscObjectDestroy"
148 /*@
149    PetscObjectDestroy - Destroys any PetscObject, regardless of the type.
150 
151    Collective on PetscObject
152 
153    Input Parameter:
154 .  obj - any PETSc object, for example a Vec, Mat or KSP.
155          This must be cast with a (PetscObject), for example,
156          PetscObjectDestroy((PetscObject)mat);
157 
158    Level: beginner
159 
160     Concepts: destroying object
161     Concepts: freeing object
162     Concepts: deleting object
163 
164 @*/
165 PetscErrorCode PETSC_DLLEXPORT PetscObjectDestroy(PetscObject obj)
166 {
167   PetscErrorCode ierr;
168 
169   PetscFunctionBegin;
170   PetscValidHeader(obj,1);
171   if (obj->bops->destroy) {
172     ierr = (*obj->bops->destroy)(obj);CHKERRQ(ierr);
173   } else {
174     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"This PETSc object of class %s does not have a generic destroy routine",obj->class_name);
175   }
176   PetscFunctionReturn(0);
177 }
178 
179 #undef __FUNCT__
180 #define __FUNCT__ "PetscObjectView"
181 /*@C
182    PetscObjectView - Views any PetscObject, regardless of the type.
183 
184    Collective on PetscObject
185 
186    Input Parameters:
187 +  obj - any PETSc object, for example a Vec, Mat or KSP.
188          This must be cast with a (PetscObject), for example,
189          PetscObjectView((PetscObject)mat,viewer);
190 -  viewer - any PETSc viewer
191 
192    Level: intermediate
193 
194 @*/
195 PetscErrorCode PETSC_DLLEXPORT PetscObjectView(PetscObject obj,PetscViewer viewer)
196 {
197   PetscErrorCode ierr;
198 
199   PetscFunctionBegin;
200   PetscValidHeader(obj,1);
201   if (!viewer) {
202     ierr = PetscViewerASCIIGetStdout(obj->comm,&viewer);CHKERRQ(ierr);
203   }
204   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
205 
206   if (obj->bops->view) {
207     ierr = (*obj->bops->view)(obj,viewer);CHKERRQ(ierr);
208   } else {
209     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
210   }
211   PetscFunctionReturn(0);
212 }
213 
214 #undef __FUNCT__
215 #define __FUNCT__ "PetscTypeCompare"
216 /*@C
217    PetscTypeCompare - Determines whether a PETSc object is of a particular type.
218 
219    Not Collective
220 
221    Input Parameters:
222 +  obj - any PETSc object, for example a Vec, Mat or KSP.
223          This must be cast with a (PetscObject), for example,
224          PetscObjectDestroy((PetscObject)mat);
225 -  type_name - string containing a type name
226 
227    Output Parameter:
228 .  same - PETSC_TRUE if they are the same, else PETSC_FALSE
229 
230    Level: intermediate
231 
232 .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType()
233 
234    Concepts: comparing^object types
235    Concepts: types^comparing
236    Concepts: object type^comparing
237 
238 @*/
239 PetscErrorCode PETSC_DLLEXPORT PetscTypeCompare(PetscObject obj,const char type_name[],PetscTruth *same)
240 {
241   PetscErrorCode ierr;
242 
243   PetscFunctionBegin;
244   if (!obj) {
245     *same = PETSC_FALSE;
246   } else if (!type_name && !obj->type_name) {
247     *same = PETSC_TRUE;
248   } else if (!type_name || !obj->type_name) {
249     *same = PETSC_FALSE;
250   } else {
251     PetscValidHeader(obj,1);
252     PetscValidCharPointer(type_name,2);
253     PetscValidPointer(same,3);
254     ierr = PetscStrcmp((char*)(obj->type_name),type_name,same);CHKERRQ(ierr);
255   }
256   PetscFunctionReturn(0);
257 }
258 
259 #define MAXREGDESOBJS 256
260 static int         PetscObjectRegisterDestroy_Count = 0;
261 static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];
262 
263 #undef __FUNCT__
264 #define __FUNCT__ "PetscObjectRegisterDestroy"
265 /*@C
266    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
267      PetscFinalize() is called.
268 
269    Collective on PetscObject
270 
271    Input Parameter:
272 .  obj - any PETSc object, for example a Vec, Mat or KSP.
273          This must be cast with a (PetscObject), for example,
274          PetscObjectRegisterDestroy((PetscObject)mat);
275 
276    Level: developer
277 
278    Notes:
279       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
280     when PETSc ends.
281 
282 .seealso: PetscObjectRegisterDestroyAll()
283 @*/
284 PetscErrorCode PETSC_DLLEXPORT PetscObjectRegisterDestroy(PetscObject obj)
285 {
286   PetscFunctionBegin;
287   PetscValidHeader(obj,1);
288   if (PetscObjectRegisterDestroy_Count < MAXREGDESOBJS) {
289     PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
290   } else {
291     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);
292 
293   }
294   PetscFunctionReturn(0);
295 }
296 
297 #undef __FUNCT__
298 #define __FUNCT__ "PetscObjectRegisterDestroyAll"
299 /*@C
300    PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
301      with PetscObjectRegisterDestroy(). Called by PetscFinalize()
302 
303    Collective on individual PetscObjects
304 
305    Level: developer
306 
307 .seealso: PetscObjectRegisterDestroy()
308 @*/
309 PetscErrorCode PETSC_DLLEXPORT PetscObjectRegisterDestroyAll(void)
310 {
311   PetscErrorCode ierr;
312   int i;
313 
314   PetscFunctionBegin;
315   for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
316     ierr = PetscObjectDestroy(PetscObjectRegisterDestroy_Objects[i]);CHKERRQ(ierr);
317   }
318   PetscObjectRegisterDestroy_Count = 0;
319   PetscFunctionReturn(0);
320 }
321 
322 
323 #define MAXREGFIN 256
324 static int         PetscRegisterFinalize_Count = 0;
325 static PetscErrorCode ((*PetscRegisterFinalize_Functions[MAXREGFIN])(void));
326 
327 #undef __FUNCT__
328 #define __FUNCT__ "PetscRegisterFinalize"
329 /*@C
330    PetscRegisterFinalize - Registers a function that is to be called in PetscFinalize()
331 
332    Not Collective
333 
334    Input Parameter:
335 .  PetscErrorCode (*fun)(void) -
336 
337    Level: developer
338 
339    Notes:
340       This is used by, for example, DMInitializePackage() to have DMFinalizePackage() called
341 
342 .seealso: PetscRegisterFinalizeAll()
343 @*/
344 PetscErrorCode PETSC_DLLEXPORT PetscRegisterFinalize(PetscErrorCode (*f)(void))
345 {
346   PetscFunctionBegin;
347 
348   if (PetscRegisterFinalize_Count < MAXREGFIN) {
349     PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
350   } else {
351     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);
352 
353   }
354   PetscFunctionReturn(0);
355 }
356 
357 #undef __FUNCT__
358 #define __FUNCT__ "PetsRegisterFinalizeAll"
359 /*@C
360    PetscRegisterFinalizeAll - Runs all the finalize functions set with PetscRegisterFinalize()
361 
362    Not Collective unless registered functions are collective
363 
364    Level: developer
365 
366 .seealso: PetscRegisterFinalize()
367 @*/
368 PetscErrorCode PETSC_DLLEXPORT PetscRegisterFinalizeAll(void)
369 {
370   PetscErrorCode ierr;
371   int i;
372 
373   PetscFunctionBegin;
374   for (i=0; i<PetscRegisterFinalize_Count; i++) {
375     ierr = (*PetscRegisterFinalize_Functions[i])();CHKERRQ(ierr);
376   }
377   PetscRegisterFinalize_Count = 0;
378   PetscFunctionReturn(0);
379 }
380