Actual source code: destroy.c

petsc-master 2019-09-17
Report Typos and Errors

  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: #define CHKERRQI(incall,ierr) if (ierr) {incall = PETSC_FALSE; }

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

100:   Collective on PetscObject

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

107:   Level: intermediate

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

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

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

137:    Not Collective

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

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

148:    Level: intermediate

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

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

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

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

173:    Not Collective

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

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

182:    Level: intermediate

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

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

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

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

207:    Not Collective

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

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

217:    Level: intermediate

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

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

229:   *match = PETSC_FALSE;
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: }