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: {
 11:   PetscInt       i;

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

 38: /*@
 39:    PetscObjectDestroy - Destroys any PetscObject, regardless of the type.

 41:    Collective on PetscObject

 43:    Input Parameter:
 44: .  obj - any PETSc object, for example a Vec, Mat or KSP.
 45:          This must be cast with a (PetscObject*), for example,
 46:          PetscObjectDestroy((PetscObject*)&mat);

 48:    Level: beginner

 50: @*/
 51: PetscErrorCode  PetscObjectDestroy(PetscObject *obj)
 52: {

 56:   if (!*obj) return(0);
 58:   if (*obj && (*obj)->bops->destroy) {
 59:     (*(*obj)->bops->destroy)(obj);
 60:   } 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);
 61:   return(0);
 62: }

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

 67:    Collective on PetscObject

 69:    Input Parameters:
 70: +  obj - any PETSc object, for example a Vec, Mat or KSP.
 71:          This must be cast with a (PetscObject), for example,
 72:          PetscObjectView((PetscObject)mat,viewer);
 73: -  viewer - any PETSc viewer

 75:    Level: intermediate

 77: @*/
 78: PetscErrorCode  PetscObjectView(PetscObject obj,PetscViewer viewer)
 79: {

 84:   if (!viewer) {
 85:     PetscViewerASCIIGetStdout(obj->comm,&viewer);
 86:   }

 89:   if (obj->bops->view) {
 90:     (*obj->bops->view)(obj,viewer);
 91:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
 92:   return(0);
 93: }

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

 98:   Collective on PetscObject

100:   Input Parameters:
101: + obj   - the object
102: . bobj  - optional other object that provides prefix (if NULL then the prefix in obj is used)
103: - optionname - option to activate viewing

105:   Level: intermediate

107: @*/
108: PetscErrorCode PetscObjectViewFromOptions(PetscObject obj,PetscObject bobj,const char optionname[])
109: {
110:   PetscErrorCode    ierr;
111:   PetscViewer       viewer;
112:   PetscBool         flg;
113:   static PetscBool  incall = PETSC_FALSE;
114:   PetscViewerFormat format;
115:   const char        *prefix;

118:   if (incall) return(0);
119:   incall = PETSC_TRUE;
120:   prefix = bobj ? bobj->prefix : obj->prefix;
121:   PetscOptionsGetViewer(PetscObjectComm((PetscObject)obj),obj->options,prefix,optionname,&viewer,&format,&flg);
122:   if (flg) {
123:     PetscViewerPushFormat(viewer,format);
124:     PetscObjectView(obj,viewer);
125:     PetscViewerFlush(viewer);
126:     PetscViewerPopFormat(viewer);
127:     PetscViewerDestroy(&viewer);
128:   }
129:   incall = PETSC_FALSE;
130:   return(0);
131: }

133: /*@C
134:    PetscObjectTypeCompare - Determines whether a PETSc object is of a particular type.

136:    Not Collective

138:    Input Parameters:
139: +  obj - any PETSc object, for example a Vec, Mat or KSP.
140:          This must be cast with a (PetscObject), for example,
141:          PetscObjectTypeCompare((PetscObject)mat);
142: -  type_name - string containing a type name

144:    Output Parameter:
145: .  same - PETSC_TRUE if they are the same, else PETSC_FALSE

147:    Level: intermediate

149: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType(), PetscObjectBaseTypeCompare(), PetscObjectTypeCompareAny(), PetscObjectBaseTypeCompareAny()

151: @*/
152: PetscErrorCode  PetscObjectTypeCompare(PetscObject obj,const char type_name[],PetscBool  *same)
153: {

158:   if (!obj) *same = PETSC_FALSE;
159:   else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
160:   else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
161:   else {
164:     PetscStrcmp((char*)(obj->type_name),type_name,same);
165:   }
166:   return(0);
167: }

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

172:    Not Collective

174:    Input Parameters:
175: +  mat - the matrix
176: -  type_name - string containing a type name

178:    Output Parameter:
179: .  same - PETSC_TRUE if it is of the same base type

181:    Level: intermediate

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

185: @*/
186: PetscErrorCode  PetscObjectBaseTypeCompare(PetscObject obj,const char type_name[],PetscBool  *same)
187: {

192:   if (!obj) *same = PETSC_FALSE;
193:   else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
194:   else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
195:   else {
198:     PetscStrbeginswith((char*)(obj->type_name),type_name,same);
199:   }
200:   return(0);
201: }

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

206:    Not Collective

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

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

216:    Level: intermediate

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

220: @*/
221: PetscErrorCode PetscObjectTypeCompareAny(PetscObject obj,PetscBool *match,const char type_name[],...)
222: {
224:   va_list        Argp;

228:   *match = PETSC_FALSE;
229:   if (!obj) return(0);
230:   va_start(Argp,type_name);
231:   while (type_name && type_name[0]) {
232:     PetscBool found;
233:     PetscObjectTypeCompare(obj,type_name,&found);
234:     if (found) {
235:       *match = PETSC_TRUE;
236:       break;
237:     }
238:     type_name = va_arg(Argp,const char*);
239:   }
240:   va_end(Argp);
241:   return(0);
242: }


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

248:    Not Collective

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

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

258:    Level: intermediate

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

262: @*/
263: PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject obj,PetscBool *match,const char type_name[],...)
264: {
266:   va_list        Argp;

270:   *match = PETSC_FALSE;
271:   va_start(Argp,type_name);
272:   while (type_name && type_name[0]) {
273:     PetscBool found;
274:     PetscObjectBaseTypeCompare(obj,type_name,&found);
275:     if (found) {
276:       *match = PETSC_TRUE;
277:       break;
278:     }
279:     type_name = va_arg(Argp,const char*);
280:   }
281:   va_end(Argp);
282:   return(0);
283: }

285: #define MAXREGDESOBJS 256
286: static int         PetscObjectRegisterDestroy_Count = 0;
287: static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];

289: /*@C
290:    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
291:      PetscFinalize() is called.

293:    Logically Collective on PetscObject

295:    Input Parameter:
296: .  obj - any PETSc object, for example a Vec, Mat or KSP.
297:          This must be cast with a (PetscObject), for example,
298:          PetscObjectRegisterDestroy((PetscObject)mat);

300:    Level: developer

302:    Notes:
303:       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
304:     when PETSc ends.

306: .seealso: PetscObjectRegisterDestroyAll()
307: @*/
308: PetscErrorCode  PetscObjectRegisterDestroy(PetscObject obj)
309: {
312:   if (PetscObjectRegisterDestroy_Count < MAXREGDESOBJS) PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
313:   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);
314:   return(0);
315: }

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

321:    Logically Collective on individual PetscObjects

323:    Level: developer

325: .seealso: PetscObjectRegisterDestroy()
326: @*/
327: PetscErrorCode  PetscObjectRegisterDestroyAll(void)
328: {
330:   PetscInt       i;

333:   for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
334:     PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i]);
335:   }
336:   PetscObjectRegisterDestroy_Count = 0;
337:   return(0);
338: }


341: #define MAXREGFIN 256
342: static int PetscRegisterFinalize_Count = 0;
343: static PetscErrorCode (*PetscRegisterFinalize_Functions[MAXREGFIN])(void);

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

348:    Not Collective

350:    Input Parameter:
351: .  PetscErrorCode (*fun)(void) -

353:    Level: developer

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

358: .seealso: PetscRegisterFinalizeAll()
359: @*/
360: PetscErrorCode  PetscRegisterFinalize(PetscErrorCode (*f)(void))
361: {
362:   PetscInt i;

365:   for (i=0; i<PetscRegisterFinalize_Count; i++) {
366:     if (f == PetscRegisterFinalize_Functions[i]) return(0);
367:   }
368:   if (PetscRegisterFinalize_Count < MAXREGFIN) PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
369:   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);
370:   return(0);
371: }

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

376:    Not Collective unless registered functions are collective

378:    Level: developer

380: .seealso: PetscRegisterFinalize()
381: @*/
382: PetscErrorCode  PetscRegisterFinalizeAll(void)
383: {
385:   PetscInt       i;

388:   for (i=0; i<PetscRegisterFinalize_Count; i++) {
389:     (*PetscRegisterFinalize_Functions[i])();
390:   }
391:   PetscRegisterFinalize_Count = 0;
392:   return(0);
393: }