xref: /petsc/src/sys/objects/destroy.c (revision db090513750e512db36c746fd2b7b9368f9e7cc5) !
1 #define PETSC_DLL
2 /*
3      Provides utility routines for manulating any type of PETSc object.
4 */
5 #include "petsc.h"  /*I   "petsc.h"    I*/
6 
7 PetscCookie PETSC_OBJECT_COOKIE = 0;
8 
9 struct _p_Object {
10   PETSCHEADER(int);
11 };
12 
13 PetscErrorCode PetscObjectDestroy_Private(PetscObject obj)
14 {
15   return 0;
16 }
17 
18 #undef __FUNCT__
19 #define __FUNCT__ "PetscObjectCreate"
20 /*@C
21    PetscObjectCreate - Creates a PetscObject
22 
23    Collective on PetscObject
24 
25    Input Parameter:
26 .  comm - An MPI communicator
27 
28    Output Parameter:
29 .  obj - The object
30 
31    Level: beginner
32 
33     Concepts: destroying object
34     Concepts: freeing object
35     Concepts: deleting object
36 
37 @*/
38 PetscErrorCode PETSC_DLLEXPORT PetscObjectCreate(MPI_Comm comm, PetscObject *obj)
39 {
40   PetscObject    o;
41   PetscErrorCode ierr;
42 
43   PetscFunctionBegin;
44   PetscValidPointer(obj,2);
45 
46 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
47   ierr = PetscInitializePackage(PETSC_NULL);CHKERRQ(ierr);
48 #endif
49   ierr = PetscHeaderCreate(o,_p_PetscObject,-1,PETSC_OBJECT_COOKIE,0,"PetscObject",comm,PetscObjectDestroy_Private,0);CHKERRQ(ierr);
50   /* records not yet defined in PetscObject
51   o->data        = 0;
52   o->setupcalled = 0;
53   */
54   *obj = o;
55   PetscFunctionReturn(0);
56 }
57 
58 #undef __FUNCT__
59 #define __FUNCT__ "PetscObjectDestroy"
60 /*@C
61    PetscObjectDestroy - Destroys any PetscObject, regardless of the type.
62 
63    Collective on PetscObject
64 
65    Input Parameter:
66 .  obj - any PETSc object, for example a Vec, Mat or KSP.
67          This must be cast with a (PetscObject), for example,
68          PetscObjectDestroy((PetscObject)mat);
69 
70    Level: beginner
71 
72     Concepts: destroying object
73     Concepts: freeing object
74     Concepts: deleting object
75 
76 @*/
77 PetscErrorCode PETSC_DLLEXPORT PetscObjectDestroy(PetscObject obj)
78 {
79   PetscErrorCode ierr;
80 
81   PetscFunctionBegin;
82   PetscValidHeader(obj,1);
83 
84   if (obj->bops->destroy) {
85     ierr = (*obj->bops->destroy)(obj);CHKERRQ(ierr);
86   } else {
87     SETERRQ1(PETSC_ERR_PLIB,"This PETSc object of class %s does not have a generic destroy routine",obj->class_name);
88   }
89   PetscFunctionReturn(0);
90 }
91 
92 #undef __FUNCT__
93 #define __FUNCT__ "PetscObjectView"
94 /*@C
95    PetscObjectView - Views any PetscObject, regardless of the type.
96 
97    Collective on PetscObject
98 
99    Input Parameters:
100 +  obj - any PETSc object, for example a Vec, Mat or KSP.
101          This must be cast with a (PetscObject), for example,
102          PetscObjectView((PetscObject)mat,viewer);
103 -  viewer - any PETSc viewer
104 
105    Level: intermediate
106 
107 @*/
108 PetscErrorCode PETSC_DLLEXPORT PetscObjectView(PetscObject obj,PetscViewer viewer)
109 {
110   PetscErrorCode ierr;
111 
112   PetscFunctionBegin;
113   PetscValidHeader(obj,1);
114   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(obj->comm);
115   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
116 
117   if (obj->bops->view) {
118     ierr = (*obj->bops->view)(obj,viewer);CHKERRQ(ierr);
119   } else {
120     SETERRQ(PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
121   }
122   PetscFunctionReturn(0);
123 }
124 
125 #undef __FUNCT__
126 #define __FUNCT__ "PetscTypeCompare"
127 /*@C
128    PetscTypeCompare - Determines whether a PETSc object is of a particular type.
129 
130    Not Collective
131 
132    Input Parameters:
133 +  obj - any PETSc object, for example a Vec, Mat or KSP.
134          This must be cast with a (PetscObject), for example,
135          PetscObjectDestroy((PetscObject)mat);
136 -  type_name - string containing a type name
137 
138    Output Parameter:
139 .  same - PETSC_TRUE if they are the same, else PETSC_FALSE
140 
141    Level: intermediate
142 
143 .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType()
144 
145    Concepts: comparing^object types
146    Concepts: types^comparing
147    Concepts: object type^comparing
148 
149 @*/
150 PetscErrorCode PETSC_DLLEXPORT PetscTypeCompare(PetscObject obj,const char type_name[],PetscTruth *same)
151 {
152   PetscErrorCode ierr;
153 
154   PetscFunctionBegin;
155   if (!obj) {
156     *same = PETSC_FALSE;
157   } else if (!type_name && !obj->type_name) {
158     *same = PETSC_TRUE;
159   } else if (!type_name || !obj->type_name) {
160     *same = PETSC_FALSE;
161   } else {
162     PetscValidHeader(obj,1);
163     PetscValidCharPointer(type_name,2);
164     PetscValidPointer(same,3);
165     ierr = PetscStrcmp((char*)(obj->type_name),type_name,same);CHKERRQ(ierr);
166   }
167   PetscFunctionReturn(0);
168 }
169 
170 static int         PetscObjectRegisterDestroy_Count = 0;
171 static PetscObject PetscObjectRegisterDestroy_Objects[256];
172 
173 #undef __FUNCT__
174 #define __FUNCT__ "PetscObjectRegisterDestroy"
175 /*@C
176    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
177      PetscFinalize() is called.
178 
179    Collective on PetscObject
180 
181    Input Parameter:
182 .  obj - any PETSc object, for example a Vec, Mat or KSP.
183          This must be cast with a (PetscObject), for example,
184          PetscObjectRegisterDestroy((PetscObject)mat);
185 
186    Level: developer
187 
188    Notes:
189       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
190     when PETSc ends.
191 
192 .seealso: PetscObjectRegisterDestroyAll()
193 @*/
194 PetscErrorCode PETSC_DLLEXPORT PetscObjectRegisterDestroy(PetscObject obj)
195 {
196   PetscFunctionBegin;
197   PetscValidHeader(obj,1);
198   PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
199   PetscFunctionReturn(0);
200 }
201 
202 #undef __FUNCT__
203 #define __FUNCT__ "PetscObjectRegisterDestroyAll"
204 /*@C
205    PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
206      with PetscObjectRegisterDestroy(). Called by PetscFinalize()
207      PetscFinalize() is called.
208 
209    Collective on individual PetscObjects
210 
211    Level: developer
212 
213 .seealso: PetscObjectRegisterDestroy()
214 @*/
215 PetscErrorCode PETSC_DLLEXPORT PetscObjectRegisterDestroyAll(void)
216 {
217   PetscErrorCode ierr;
218   int i;
219 
220   PetscFunctionBegin;
221   for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
222     ierr = PetscObjectDestroy(PetscObjectRegisterDestroy_Objects[i]);CHKERRQ(ierr);
223   }
224   PetscFunctionReturn(0);
225 }
226 
227 
228