Actual source code: destroy.c

petsc-master 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: @*/
 57: PetscErrorCode  PetscObjectDestroy(PetscObject *obj)
 58: {

 62:   if (!*obj) return(0);
 64:   if (*obj && (*obj)->bops->destroy) {
 65:     (*(*obj)->bops->destroy)(obj);
 66:   } 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);
 67:   return(0);
 68: }

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

 73:    Collective on PetscObject

 75:    Input Parameters:
 76: +  obj - any PETSc object, for example a Vec, Mat or KSP.
 77:          This must be cast with a (PetscObject), for example,
 78:          PetscObjectView((PetscObject)mat,viewer);
 79: -  viewer - any PETSc viewer

 81:    Level: intermediate

 83: @*/
 84: PetscErrorCode  PetscObjectView(PetscObject obj,PetscViewer viewer)
 85: {

 90:   if (!viewer) {
 91:     PetscViewerASCIIGetStdout(obj->comm,&viewer);
 92:   }

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

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

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

106:   Collective on PetscObject

108:   Input Parameters:
109: + obj   - the object
110: . bobj  - optional other object that provides prefix (if NULL then the prefix in obj is used)
111: - optionname - option to activate viewing

113:   Level: intermediate

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

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

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

143:    Not Collective

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

151:    Output Parameter:
152: .  same - PETSC_TRUE if they are the same, else PETSC_FALSE

154:    Level: intermediate

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

158: @*/
159: PetscErrorCode  PetscObjectTypeCompare(PetscObject obj,const char type_name[],PetscBool  *same)
160: {

165:   if (!obj) *same = PETSC_FALSE;
166:   else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
167:   else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
168:   else {
171:     PetscStrcmp((char*)(obj->type_name),type_name,same);
172:   }
173:   return(0);
174: }

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

179:    Not Collective

181:    Input Parameters:
182: +  mat - the matrix
183: -  type_name - string containing a type name

185:    Output Parameter:
186: .  same - PETSC_TRUE if it is of the same base type

188:    Level: intermediate

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

192: @*/
193: PetscErrorCode  PetscObjectBaseTypeCompare(PetscObject obj,const char type_name[],PetscBool  *same)
194: {

199:   if (!obj) *same = PETSC_FALSE;
200:   else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
201:   else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
202:   else {
205:     PetscStrbeginswith((char*)(obj->type_name),type_name,same);
206:   }
207:   return(0);
208: }

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

213:    Not Collective

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

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

223:    Level: intermediate

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

227: @*/
228: PetscErrorCode PetscObjectTypeCompareAny(PetscObject obj,PetscBool *match,const char type_name[],...)
229: {
231:   va_list        Argp;

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

250: #define MAXREGDESOBJS 256
251: static int         PetscObjectRegisterDestroy_Count = 0;
252: static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];

254: /*@C
255:    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
256:      PetscFinalize() is called.

258:    Logically Collective on PetscObject

260:    Input Parameter:
261: .  obj - any PETSc object, for example a Vec, Mat or KSP.
262:          This must be cast with a (PetscObject), for example,
263:          PetscObjectRegisterDestroy((PetscObject)mat);

265:    Level: developer

267:    Notes:
268:       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
269:     when PETSc ends.

271: .seealso: PetscObjectRegisterDestroyAll()
272: @*/
273: PetscErrorCode  PetscObjectRegisterDestroy(PetscObject obj)
274: {
277:   if (PetscObjectRegisterDestroy_Count < MAXREGDESOBJS) PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
278:   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);
279:   return(0);
280: }

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

286:    Logically Collective on individual PetscObjects

288:    Level: developer

290: .seealso: PetscObjectRegisterDestroy()
291: @*/
292: PetscErrorCode  PetscObjectRegisterDestroyAll(void)
293: {
295:   PetscInt       i;

298:   for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
299:     PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i]);
300:   }
301:   PetscObjectRegisterDestroy_Count = 0;
302:   return(0);
303: }


306: #define MAXREGFIN 256
307: static int PetscRegisterFinalize_Count = 0;
308: static PetscErrorCode (*PetscRegisterFinalize_Functions[MAXREGFIN])(void);

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

313:    Not Collective

315:    Input Parameter:
316: .  PetscErrorCode (*fun)(void) -

318:    Level: developer

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

323: .seealso: PetscRegisterFinalizeAll()
324: @*/
325: PetscErrorCode  PetscRegisterFinalize(PetscErrorCode (*f)(void))
326: {
327:   PetscInt i;

330:   for (i=0; i<PetscRegisterFinalize_Count; i++) {
331:     if (f == PetscRegisterFinalize_Functions[i]) return(0);
332:   }
333:   if (PetscRegisterFinalize_Count < MAXREGFIN) PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
334:   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);
335:   return(0);
336: }

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

341:    Not Collective unless registered functions are collective

343:    Level: developer

345: .seealso: PetscRegisterFinalize()
346: @*/
347: PetscErrorCode  PetscRegisterFinalizeAll(void)
348: {
350:   PetscInt       i;

353:   for (i=0; i<PetscRegisterFinalize_Count; i++) {
354:     (*PetscRegisterFinalize_Functions[i])();
355:   }
356:   PetscRegisterFinalize_Count = 0;
357:   return(0);
358: }