Actual source code: destroy.c

petsc-3.13.6 2020-09-29
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:     PetscViewerFlush(viewer);CHKERRQI(incall,ierr);
128:     PetscViewerPopFormat(viewer);CHKERRQI(incall,ierr);
129:     PetscViewerDestroy(&viewer);CHKERRQI(incall,ierr);
130:   }
131:   incall = PETSC_FALSE;
132:   return(0);
133: }

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

138:    Not Collective

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

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

149:    Level: intermediate

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

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

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

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

174:    Not Collective

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

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

183:    Level: intermediate

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

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

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

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

208:    Not Collective

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

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

218:    Level: intermediate

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

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

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


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

249:    Not Collective

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

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

259:    Level: intermediate

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

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

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

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

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

294:    Logically Collective on PetscObject

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

301:    Level: developer

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

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

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

322:    Logically Collective on individual PetscObjects

324:    Level: developer

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

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


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

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

349:    Not Collective

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

354:    Level: developer

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

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

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

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

377:    Not Collective unless registered functions are collective

379:    Level: developer

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

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