Actual source code: destroy.c

petsc-master 2020-07-09
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:   if (!obj) return(0);
232:   va_start(Argp,type_name);
233:   while (type_name && type_name[0]) {
234:     PetscBool found;
235:     PetscObjectTypeCompare(obj,type_name,&found);
236:     if (found) {
237:       *match = PETSC_TRUE;
238:       break;
239:     }
240:     type_name = va_arg(Argp,const char*);
241:   }
242:   va_end(Argp);
243:   return(0);
244: }


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

250:    Not Collective

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

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

260:    Level: intermediate

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

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

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

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

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

295:    Logically Collective on PetscObject

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

302:    Level: developer

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

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

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

323:    Logically Collective on individual PetscObjects

325:    Level: developer

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

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


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

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

350:    Not Collective

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

355:    Level: developer

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

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

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

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

378:    Not Collective unless registered functions are collective

380:    Level: developer

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

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