Actual source code: destroy.c

petsc-3.11.3 2019-06-26
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:     PetscFree(obj->intstarcomposeddata);
 19:     PetscFree(obj->intstarcomposedstate);
 20:   }
 21:   if (obj->realstar_idmax>0) {
 22:     for (i=0; i<obj->realstar_idmax; i++) {
 23:       PetscFree(obj->realstarcomposeddata[i]);
 24:     }
 25:     PetscFree(obj->realstarcomposeddata);
 26:     PetscFree(obj->realstarcomposedstate);
 27:   }
 28:   if (obj->scalarstar_idmax>0) {
 29:     for (i=0; i<obj->scalarstar_idmax; i++) {
 30:       PetscFree(obj->scalarstarcomposeddata[i]);
 31:     }
 32:     PetscFree(obj->scalarstarcomposeddata);
 33:     PetscFree(obj->scalarstarcomposedstate);
 34:   }
 35:   PetscFree(obj->intcomposeddata);
 36:   PetscFree(obj->intcomposedstate);
 37:   PetscFree(obj->realcomposeddata);
 38:   PetscFree(obj->realcomposedstate);
 39:   PetscFree(obj->scalarcomposeddata);
 40:   PetscFree(obj->scalarcomposedstate);
 41:   return(0);
 42: }

 44: /*@
 45:    PetscObjectDestroy - Destroys any PetscObject, regardless of the type.

 47:    Collective on PetscObject

 49:    Input Parameter:
 50: .  obj - any PETSc object, for example a Vec, Mat or KSP.
 51:          This must be cast with a (PetscObject*), for example,
 52:          PetscObjectDestroy((PetscObject*)&mat);

 54:    Level: beginner

 56:     Concepts: destroying object
 57:     Concepts: freeing object
 58:     Concepts: deleting object

 60: @*/
 61: PetscErrorCode  PetscObjectDestroy(PetscObject *obj)
 62: {

 66:   if (!*obj) return(0);
 68:   if (*obj && (*obj)->bops->destroy) {
 69:     (*(*obj)->bops->destroy)(obj);
 70:   } 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);
 71:   return(0);
 72: }

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

 77:    Collective on PetscObject

 79:    Input Parameters:
 80: +  obj - any PETSc object, for example a Vec, Mat or KSP.
 81:          This must be cast with a (PetscObject), for example,
 82:          PetscObjectView((PetscObject)mat,viewer);
 83: -  viewer - any PETSc viewer

 85:    Level: intermediate

 87: @*/
 88: PetscErrorCode  PetscObjectView(PetscObject obj,PetscViewer viewer)
 89: {

 94:   if (!viewer) {
 95:     PetscViewerASCIIGetStdout(obj->comm,&viewer);
 96:   }

 99:   if (obj->bops->view) {
100:     (*obj->bops->view)(obj,viewer);
101:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
102:   return(0);
103: }

105: #define CHKERRQI(incall,ierr) if (ierr) {incall = PETSC_FALSE; }

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

110:   Collective on PetscObject

112:   Input Parameters:
113: + obj   - the object
114: . bobj  - optional other object that provides prefix (if NULL then the prefix in obj is used)
115: - optionname - option to activate viewing

117:   Level: intermediate

119: @*/
120: PetscErrorCode PetscObjectViewFromOptions(PetscObject obj,PetscObject bobj,const char optionname[])
121: {
122:   PetscErrorCode    ierr;
123:   PetscViewer       viewer;
124:   PetscBool         flg;
125:   static PetscBool  incall = PETSC_FALSE;
126:   PetscViewerFormat format;
127:   const char        *prefix;

130:   if (incall) return(0);
131:   incall = PETSC_TRUE;
132:   prefix = bobj ? bobj->prefix : obj->prefix;
133:   PetscOptionsGetViewer(PetscObjectComm((PetscObject)obj),obj->options,prefix,optionname,&viewer,&format,&flg);CHKERRQI(incall,ierr);
134:   if (flg) {
135:     PetscViewerPushFormat(viewer,format);CHKERRQI(incall,ierr);
136:     PetscObjectView(obj,viewer);CHKERRQI(incall,ierr);
137:     PetscViewerPopFormat(viewer);CHKERRQI(incall,ierr);
138:     PetscViewerDestroy(&viewer);CHKERRQI(incall,ierr);
139:   }
140:   incall = PETSC_FALSE;
141:   return(0);
142: }

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

147:    Not Collective

149:    Input Parameters:
150: +  obj - any PETSc object, for example a Vec, Mat or KSP.
151:          This must be cast with a (PetscObject), for example,
152:          PetscObjectTypeCompare((PetscObject)mat);
153: -  type_name - string containing a type name

155:    Output Parameter:
156: .  same - PETSC_TRUE if they are the same, else PETSC_FALSE

158:    Level: intermediate

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

162:    Concepts: comparing^object types
163:    Concepts: types^comparing
164:    Concepts: object type^comparpeing

166: @*/
167: PetscErrorCode  PetscObjectTypeCompare(PetscObject obj,const char type_name[],PetscBool  *same)
168: {

172:   if (!obj) *same = PETSC_FALSE;
173:   else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
174:   else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
175:   else {
179:     PetscStrcmp((char*)(obj->type_name),type_name,same);
180:   }
181:   return(0);
182: }

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

187:    Not Collective

189:    Input Parameters:
190: +  mat - the matrix
191: -  type_name - string containing a type name

193:    Output Parameter:
194: .  same - PETSC_TRUE if it is of the same base type

196:    Level: intermediate

198: .seealso: PetscObjectTypeCompare(), PetscObjectTypeCompareAny()

200: @*/
201: PetscErrorCode  PetscObjectBaseTypeCompare(PetscObject obj,const char type_name[],PetscBool  *same)
202: {

206:   if (!obj) *same = PETSC_FALSE;
207:   else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
208:   else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
209:   else {
213:     PetscStrbeginswith((char*)(obj->type_name),type_name,same);
214:   }
215:   return(0);
216: }

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

221:    Not Collective

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

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

231:    Level: intermediate

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

235:    Concepts: comparing^object types
236:    Concepts: types^comparing
237:    Concepts: object type^comparing

239: @*/
240: PetscErrorCode PetscObjectTypeCompareAny(PetscObject obj,PetscBool *match,const char type_name[],...)
241: {
243:   va_list        Argp;

246:   *match = PETSC_FALSE;
247:   va_start(Argp,type_name);
248:   while (type_name && type_name[0]) {
249:     PetscBool found;
250:     PetscObjectTypeCompare(obj,type_name,&found);
251:     if (found) {
252:       *match = PETSC_TRUE;
253:       break;
254:     }
255:     type_name = va_arg(Argp,const char*);
256:   }
257:   va_end(Argp);
258:   return(0);
259: }

261: #define MAXREGDESOBJS 256
262: static int         PetscObjectRegisterDestroy_Count = 0;
263: static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];

265: /*@C
266:    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
267:      PetscFinalize() is called.

269:    Logically Collective on PetscObject

271:    Input Parameter:
272: .  obj - any PETSc object, for example a Vec, Mat or KSP.
273:          This must be cast with a (PetscObject), for example,
274:          PetscObjectRegisterDestroy((PetscObject)mat);

276:    Level: developer

278:    Notes:
279:       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
280:     when PETSc ends.

282: .seealso: PetscObjectRegisterDestroyAll()
283: @*/
284: PetscErrorCode  PetscObjectRegisterDestroy(PetscObject obj)
285: {
288:   if (PetscObjectRegisterDestroy_Count < MAXREGDESOBJS) PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
289:   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);
290:   return(0);
291: }

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

297:    Logically Collective on individual PetscObjects

299:    Level: developer

301: .seealso: PetscObjectRegisterDestroy()
302: @*/
303: PetscErrorCode  PetscObjectRegisterDestroyAll(void)
304: {
306:   PetscInt       i;

309:   for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
310:     PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i]);
311:   }
312:   PetscObjectRegisterDestroy_Count = 0;
313:   return(0);
314: }


317: #define MAXREGFIN 256
318: static int PetscRegisterFinalize_Count = 0;
319: static PetscErrorCode (*PetscRegisterFinalize_Functions[MAXREGFIN])(void);

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

324:    Not Collective

326:    Input Parameter:
327: .  PetscErrorCode (*fun)(void) -

329:    Level: developer

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

334: .seealso: PetscRegisterFinalizeAll()
335: @*/
336: PetscErrorCode  PetscRegisterFinalize(PetscErrorCode (*f)(void))
337: {
338:   PetscInt i;

341:   for (i=0; i<PetscRegisterFinalize_Count; i++) {
342:     if (f == PetscRegisterFinalize_Functions[i]) return(0);
343:   }
344:   if (PetscRegisterFinalize_Count < MAXREGFIN) PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
345:   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);
346:   return(0);
347: }

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

352:    Not Collective unless registered functions are collective

354:    Level: developer

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

364:   for (i=0; i<PetscRegisterFinalize_Count; i++) {
365:     (*PetscRegisterFinalize_Functions[i])();
366:   }
367:   PetscRegisterFinalize_Count = 0;
368:   return(0);
369: }