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: }

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

247:    Not Collective

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

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

257:    Level: intermediate

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

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

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

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

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

292:    Logically Collective on PetscObject

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

299:    Level: developer

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

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

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

320:    Logically Collective on individual PetscObjects

322:    Level: developer

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

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

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

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

346:    Not Collective

348:    Input Parameter:
349: .  PetscErrorCode (*fun)(void) -

351:    Level: developer

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

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

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

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

374:    Not Collective unless registered functions are collective

376:    Level: developer

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

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