Actual source code: destroy.c


  2: /*
  3:      Provides utility routines for manulating any type of PETSc object.
  4: */
  5: #include <petsc/private/petscimpl.h>
  6: #include <petscviewer.h>

  8: PetscErrorCode PetscComposedQuantitiesDestroy(PetscObject obj)
  9: {
 10:   PetscInt i;

 12:   if (obj->intstar_idmax>0) {
 13:     for (i=0; i<obj->intstar_idmax; i++) PetscFree(obj->intstarcomposeddata[i]);
 14:     PetscFree2(obj->intstarcomposeddata,obj->intstarcomposedstate);
 15:   }
 16:   if (obj->realstar_idmax>0) {
 17:     for (i=0; i<obj->realstar_idmax; i++) PetscFree(obj->realstarcomposeddata[i]);
 18:     PetscFree2(obj->realstarcomposeddata,obj->realstarcomposedstate);
 19:   }
 20:   if (obj->scalarstar_idmax>0) {
 21:     for (i=0; i<obj->scalarstar_idmax; i++) PetscFree(obj->scalarstarcomposeddata[i]);
 22:     PetscFree2(obj->scalarstarcomposeddata,obj->scalarstarcomposedstate);
 23:   }
 24:   PetscFree2(obj->intcomposeddata,obj->intcomposedstate);
 25:   PetscFree2(obj->realcomposeddata,obj->realcomposedstate);
 26:   PetscFree2(obj->scalarcomposeddata,obj->scalarcomposedstate);
 27:   return 0;
 28: }

 30: /*@
 31:    PetscObjectDestroy - Destroys any PetscObject, regardless of the type.

 33:    Collective on PetscObject

 35:    Input Parameter:
 36: .  obj - any PETSc object, for example a Vec, Mat or KSP.
 37:          This must be cast with a (PetscObject*), for example,
 38:          PetscObjectDestroy((PetscObject*)&mat);

 40:    Level: beginner

 42: @*/
 43: PetscErrorCode  PetscObjectDestroy(PetscObject *obj)
 44: {
 45:   if (!obj || !*obj) return 0;
 48:   (*(*obj)->bops->destroy)(obj);
 49:   return 0;
 50: }

 52: /*@C
 53:    PetscObjectView - Views any PetscObject, regardless of the type.

 55:    Collective on PetscObject

 57:    Input Parameters:
 58: +  obj - any PETSc object, for example a Vec, Mat or KSP.
 59:          This must be cast with a (PetscObject), for example,
 60:          PetscObjectView((PetscObject)mat,viewer);
 61: -  viewer - any PETSc viewer

 63:    Level: intermediate

 65: @*/
 66: PetscErrorCode  PetscObjectView(PetscObject obj,PetscViewer viewer)
 67: {
 70:   if (!viewer) PetscViewerASCIIGetStdout(obj->comm,&viewer);

 73:   (*obj->bops->view)(obj,viewer);
 74:   return 0;
 75: }

 77: /*@C
 78:   PetscObjectViewFromOptions - Processes command line options to determine if/how a PetscObject is to be viewed.

 80:   Collective on PetscObject

 82:   Input Parameters:
 83: + obj   - the object
 84: . bobj  - optional other object that provides prefix (if NULL then the prefix in obj is used)
 85: - optionname - option to activate viewing

 87:   Level: intermediate

 89: @*/
 90: PetscErrorCode PetscObjectViewFromOptions(PetscObject obj,PetscObject bobj,const char optionname[])
 91: {
 92:   PetscViewer       viewer;
 93:   PetscBool         flg;
 94:   static PetscBool  incall = PETSC_FALSE;
 95:   PetscViewerFormat format;
 96:   const char        *prefix;

 98:   if (incall) return 0;
 99:   incall = PETSC_TRUE;
100:   prefix = bobj ? bobj->prefix : obj->prefix;
101:   PetscOptionsGetViewer(PetscObjectComm((PetscObject)obj),obj->options,prefix,optionname,&viewer,&format,&flg);
102:   if (flg) {
103:     PetscViewerPushFormat(viewer,format);
104:     PetscObjectView(obj,viewer);
105:     PetscViewerFlush(viewer);
106:     PetscViewerPopFormat(viewer);
107:     PetscViewerDestroy(&viewer);
108:   }
109:   incall = PETSC_FALSE;
110:   return 0;
111: }

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(), PetscObjectBaseTypeCompare(), PetscObjectTypeCompareAny(), PetscObjectBaseTypeCompareAny()

131: @*/
132: PetscErrorCode  PetscObjectTypeCompare(PetscObject obj,const char type_name[],PetscBool  *same)
133: {
135:   if (!obj) *same = PETSC_FALSE;
136:   else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
137:   else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
138:   else {
141:     PetscStrcmp((char*)(obj->type_name),type_name,same);
142:   }
143:   return 0;
144: }

146: /*@C
147:    PetscObjectBaseTypeCompare - Determines whether a PetscObject is of a given base type. For example the base type of MATSEQAIJPERM is MATSEQAIJ

149:    Not Collective

151:    Input Parameters:
152: +  mat - the matrix
153: -  type_name - string containing a type name

155:    Output Parameter:
156: .  same - PETSC_TRUE if it is of the same base type

158:    Level: intermediate

160: .seealso: PetscObjectTypeCompare(), PetscObjectTypeCompareAny(), PetscObjectBaseTypeCompareAny()

162: @*/
163: PetscErrorCode  PetscObjectBaseTypeCompare(PetscObject obj,const char type_name[],PetscBool  *same)
164: {
166:   if (!obj) *same = PETSC_FALSE;
167:   else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
168:   else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
169:   else {
172:     PetscStrbeginswith((char*)(obj->type_name),type_name,same);
173:   }
174:   return 0;
175: }

177: /*@C
178:    PetscObjectTypeCompareAny - Determines whether a PETSc object is of any of a list of types.

180:    Not Collective

182:    Input Parameters:
183: +  obj - any PETSc object, for example a Vec, Mat or KSP.
184:          This must be cast with a (PetscObject), for example, PetscObjectTypeCompareAny((PetscObject)mat,...);
185: -  type_name - string containing a type name, pass the empty string "" to terminate the list

187:    Output Parameter:
188: .  match - PETSC_TRUE if the type of obj matches any in the list, else PETSC_FALSE

190:    Level: intermediate

192: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType(), PetscObjectTypeCompare(), PetscObjectBaseTypeCompare(), PetscObjectTypeCompareAny()

194: @*/
195: PetscErrorCode PetscObjectTypeCompareAny(PetscObject obj,PetscBool *match,const char type_name[],...)
196: {
197:   va_list Argp;

200:   *match = PETSC_FALSE;
201:   if (!obj) return 0;
202:   va_start(Argp,type_name);
203:   while (type_name && type_name[0]) {
204:     PetscBool found;
205:     PetscObjectTypeCompare(obj,type_name,&found);
206:     if (found) {
207:       *match = PETSC_TRUE;
208:       break;
209:     }
210:     type_name = va_arg(Argp,const char*);
211:   }
212:   va_end(Argp);
213:   return 0;
214: }

216: /*@C
217:    PetscObjectBaseTypeCompareAny - Determines whether a PETSc object has the base type of any of a list of types.

219:    Not Collective

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, PetscObjectBaseTypeCompareAny((PetscObject)mat,...);
224: -  type_name - string containing a type name, pass the empty string "" to terminate the list

226:    Output Parameter:
227: .  match - PETSC_TRUE if the type of obj matches any in the list, else PETSC_FALSE

229:    Level: intermediate

231: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType(), PetscObjectTypeCompare(), PetscObjectBaseTypeCompare(), PetscObjectTypeCompareAny()

233: @*/
234: PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject obj,PetscBool *match,const char type_name[],...)
235: {
236:   va_list Argp;

239:   *match = PETSC_FALSE;
240:   va_start(Argp,type_name);
241:   while (type_name && type_name[0]) {
242:     PetscBool found;
243:     PetscObjectBaseTypeCompare(obj,type_name,&found);
244:     if (found) {
245:       *match = PETSC_TRUE;
246:       break;
247:     }
248:     type_name = va_arg(Argp,const char*);
249:   }
250:   va_end(Argp);
251:   return 0;
252: }

254: #define MAXREGDESOBJS 256
255: static int         PetscObjectRegisterDestroy_Count = 0;
256: static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];

258: /*@C
259:    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
260:      PetscFinalize() is called.

262:    Logically Collective on PetscObject

264:    Input Parameter:
265: .  obj - any PETSc object, for example a Vec, Mat or KSP.
266:          This must be cast with a (PetscObject), for example,
267:          PetscObjectRegisterDestroy((PetscObject)mat);

269:    Level: developer

271:    Notes:
272:       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
273:     when PETSc ends.

275: .seealso: PetscObjectRegisterDestroyAll()
276: @*/
277: PetscErrorCode  PetscObjectRegisterDestroy(PetscObject obj)
278: {
281:   PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
282:   return 0;
283: }

285: /*@C
286:    PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
287:      with PetscObjectRegisterDestroy(). Called by PetscFinalize()

289:    Logically Collective on individual PetscObjects

291:    Level: developer

293: .seealso: PetscObjectRegisterDestroy()
294: @*/
295: PetscErrorCode  PetscObjectRegisterDestroyAll(void)
296: {
297:   for (PetscInt i=0; i<PetscObjectRegisterDestroy_Count; i++) PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i]);
298:   PetscObjectRegisterDestroy_Count = 0;
299:   return 0;
300: }

302: #define MAXREGFIN 256
303: static int PetscRegisterFinalize_Count = 0;
304: static PetscErrorCode (*PetscRegisterFinalize_Functions[MAXREGFIN])(void);

306: /*@C
307:    PetscRegisterFinalize - Registers a function that is to be called in PetscFinalize()

309:    Not Collective

311:    Input Parameter:
312: .  PetscErrorCode (*fun)(void) -

314:    Level: developer

316:    Notes:
317:       This is used by, for example, DMInitializePackage() to have DMFinalizePackage() called

319: .seealso: PetscRegisterFinalizeAll()
320: @*/
321: PetscErrorCode  PetscRegisterFinalize(PetscErrorCode (*f)(void))
322: {
323:   for (PetscInt i=0; i<PetscRegisterFinalize_Count; i++) {
324:     if (f == PetscRegisterFinalize_Functions[i]) return 0;
325:   }
327:   PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
328:   return 0;
329: }

331: /*@C
332:    PetscRegisterFinalizeAll - Runs all the finalize functions set with PetscRegisterFinalize()

334:    Not Collective unless registered functions are collective

336:    Level: developer

338: .seealso: PetscRegisterFinalize()
339: @*/
340: PetscErrorCode  PetscRegisterFinalizeAll(void)
341: {
342:   for (PetscInt i=0; i<PetscRegisterFinalize_Count; i++) (*PetscRegisterFinalize_Functions[i])();
343:   PetscRegisterFinalize_Count = 0;
344:   return 0;
345: }