Actual source code: destroy.c


  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: {
 10:   PetscInt i;

 12:   if (obj->intstar_idmax > 0) {
 13:     for (i = 0; i < obj->intstar_idmax; i++) PetscFree(obj->intstarcomposeddata[i]);
 14:     PetscFree2(obj->intstarcomposeddata, obj->intstarcomposedstate);
 15:   }
 16:   if (obj->realstar_idmax > 0) {
 17:     for (i = 0; i < obj->realstar_idmax; i++) PetscFree(obj->realstarcomposeddata[i]);
 18:     PetscFree2(obj->realstarcomposeddata, obj->realstarcomposedstate);
 19:   }
 20:   if (obj->scalarstar_idmax > 0) {
 21:     for (i = 0; i < obj->scalarstar_idmax; i++) PetscFree(obj->scalarstarcomposeddata[i]);
 22:     PetscFree2(obj->scalarstarcomposeddata, obj->scalarstarcomposedstate);
 23:   }
 24:   PetscFree2(obj->intcomposeddata, obj->intcomposedstate);
 25:   PetscFree2(obj->realcomposeddata, obj->realcomposedstate);
 26:   PetscFree2(obj->scalarcomposeddata, obj->scalarcomposedstate);
 27:   return 0;
 28: }

 30: /*@
 31:    PetscObjectDestroy - Destroys any `PetscObject`, regardless of the type.

 33:    Collective

 35:    Input Parameter:
 36: .  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
 37:          This must be cast with a (`PetscObject`*), for example,
 38:          `PetscObjectDestroy`((`PetscObject`*)&mat);

 40:    Level: beginner

 42: .seealso: `PetscObject`
 43: @*/
 44: PetscErrorCode PetscObjectDestroy(PetscObject *obj)
 45: {
 46:   if (!obj || !*obj) return 0;
 49:   (*(*obj)->bops->destroy)(obj);
 50:   return 0;
 51: }

 53: /*@C
 54:    PetscObjectView - Views any `PetscObject`, regardless of the type.

 56:    Collective

 58:    Input Parameters:
 59: +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
 60:          This must be cast with a (`PetscObject`), for example,
 61:          `PetscObjectView`((`PetscObject`)mat,viewer);
 62: -  viewer - any PETSc viewer

 64:    Level: intermediate

 66: .seealso: `PetscObject`, `PetscObjectViewFromOptions()`
 67: @*/
 68: PetscErrorCode PetscObjectView(PetscObject obj, PetscViewer viewer)
 69: {
 72:   if (!viewer) PetscViewerASCIIGetStdout(obj->comm, &viewer);

 75:   (*obj->bops->view)(obj, viewer);
 76:   return 0;
 77: }

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

 82:   Collective

 84:   Input Parameters:
 85: + obj   - the object
 86: . bobj  - optional other object that provides prefix (if NULL then the prefix in obj is used)
 87: - optionname - option string that is used to activate viewing

 89:   Options Database Key:
 90: .  -optionname_view [viewertype]:... - option name and values. In actual usage this would be something like -mat_coarse_view

 92:   Notes:
 93: .vb
 94:     If no value is provided ascii:stdout is used
 95:        ascii[:[filename][:[format][:append]]]    defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
 96:                                                   for example ascii::ascii_info prints just the information about the object not all details
 97:                                                   unless :append is given filename opens in write mode, overwriting what was already there
 98:        binary[:[filename][:[format][:append]]]   defaults to the file binaryoutput
 99:        draw[:drawtype[:filename]]                for example, draw:tikz, draw:tikz:figure.tex  or draw:x
100:        socket[:port]                             defaults to the standard output port
101:        saws[:communicatorname]                    publishes object to the Scientific Application Webserver (SAWs)
102: .ve

104:   This is not called directly but is called by, for example, `MatCoarseViewFromOptions()`

106:   Level: developer

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

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

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

136:    Not Collective

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

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

147:    Level: intermediate

149: .seealso: `PetscObject`, `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectBaseTypeCompare()`, `PetscObjectTypeCompareAny()`, `PetscObjectBaseTypeCompareAny()`, `PetscObjectObjectTypeCompare()`
150: @*/
151: PetscErrorCode PetscObjectTypeCompare(PetscObject obj, const char type_name[], PetscBool *same)
152: {
154:   if (!obj) *same = PETSC_FALSE;
155:   else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
156:   else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
157:   else {
160:     PetscStrcmp((char *)(obj->type_name), type_name, same);
161:   }
162:   return 0;
163: }

165: /*@C
166:    PetscObjectObjectTypeCompare - Determines whether two PETSc objects are of the same type

168:    Logically Collective

170:    Input Parameters:
171: +  obj1 - any PETSc object, for example a Vec, Mat or KSP.
172: -  obj2 - anther PETSc object

174:    Output Parameter:
175: .  same - PETSC_TRUE if they are the same, else PETSC_FALSE

177:    Level: intermediate

179: .seealso: `PetscObjectTypeCompare()`, `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectBaseTypeCompare()`, `PetscObjectTypeCompareAny()`, `PetscObjectBaseTypeCompareAny()`

181: @*/
182: PetscErrorCode PetscObjectObjectTypeCompare(PetscObject obj1, PetscObject obj2, PetscBool *same)
183: {
187:   PetscStrcmp((char *)(obj1->type_name), (char *)(obj2->type_name), same);
188:   return 0;
189: }

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

194:    Not Collective

196:    Input Parameters:
197: +  mat - the matrix
198: -  type_name - string containing a type name

200:    Output Parameter:
201: .  same - `PETSC_TRUE` if it is of the same base type

203:    Level: intermediate

205: .seealso: `PetscObject`, `PetscObjectTypeCompare()`, `PetscObjectTypeCompareAny()`, `PetscObjectBaseTypeCompareAny()`
206: @*/
207: PetscErrorCode PetscObjectBaseTypeCompare(PetscObject obj, const char type_name[], PetscBool *same)
208: {
210:   if (!obj) *same = PETSC_FALSE;
211:   else if (!type_name && !obj->type_name) *same = PETSC_TRUE;
212:   else if (!type_name || !obj->type_name) *same = PETSC_FALSE;
213:   else {
216:     PetscStrbeginswith((char *)(obj->type_name), type_name, same);
217:   }
218:   return 0;
219: }

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

224:    Not Collective

226:    Input Parameters:
227: +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
228:          This must be cast with a (`PetscObject`), for example, `PetscObjectTypeCompareAny`((`PetscObject`)mat,...);
229: -  type_name - array of strings containing type names, pass the empty string "" to terminate the list

231:    Output Parameter:
232: .  match - `PETSC_TRUE` if the type of obj matches any in the list, else `PETSC_FALSE`

234:    Level: intermediate

236: .seealso: `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectTypeCompare()`, `PetscObjectBaseTypeCompare()`
237: @*/
238: PetscErrorCode PetscObjectTypeCompareAny(PetscObject obj, PetscBool *match, const char type_name[], ...)
239: {
240:   va_list Argp;

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

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

262:    Not Collective

264:    Input Parameters:
265: +  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
266:          This must be cast with a (`PetscObject`), for example, `PetscObjectBaseTypeCompareAny`((`PetscObject`)mat,...);
267: -  type_name - array of strings containing type names, pass the empty string "" to terminate the list

269:    Output Parameter:
270: .  match - `PETSC_TRUE` if the type of obj matches any in the list, else `PETSC_FALSE`

272:    Level: intermediate

274: .seealso: `VecGetType()`, `KSPGetType()`, `PCGetType()`, `SNESGetType()`, `PetscObjectTypeCompare()`, `PetscObjectBaseTypeCompare()`, `PetscObjectTypeCompareAny()`
275: @*/
276: PetscErrorCode PetscObjectBaseTypeCompareAny(PetscObject obj, PetscBool *match, const char type_name[], ...)
277: {
278:   va_list Argp;

281:   *match = PETSC_FALSE;
282:   va_start(Argp, type_name);
283:   while (type_name && type_name[0]) {
284:     PetscBool found;
285:     PetscObjectBaseTypeCompare(obj, type_name, &found);
286:     if (found) {
287:       *match = PETSC_TRUE;
288:       break;
289:     }
290:     type_name = va_arg(Argp, const char *);
291:   }
292:   va_end(Argp);
293:   return 0;
294: }

296: #define MAXREGDESOBJS 256
297: static int         PetscObjectRegisterDestroy_Count = 0;
298: static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];

300: /*@C
301:    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
302:      `PetscFinalize()` is called.

304:    Logically Collective

306:    Input Parameter:
307: .  obj - any PETSc object, for example a `Vec`, `Mat` or `KSP`.
308:          This must be cast with a (`PetscObject`), for example,
309:          `PetscObjectRegisterDestroy`((`PetscObject`)mat);

311:    Level: developer

313:    Note:
314:       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
315:     when PETSc ends.

317: .seealso: `PetscObjectRegisterDestroyAll()`
318: @*/
319: PetscErrorCode PetscObjectRegisterDestroy(PetscObject obj)
320: {
323:   PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
324:   return 0;
325: }

327: /*@C
328:    PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
329:      with `PetscObjectRegisterDestroy()`. Called by `PetscFinalize()`

331:    Logically Collective on the individual `PetscObject`s that are being processed

333:    Level: developer

335: .seealso: `PetscObjectRegisterDestroy()`
336: @*/
337: PetscErrorCode PetscObjectRegisterDestroyAll(void)
338: {
339:   for (PetscInt i = 0; i < PetscObjectRegisterDestroy_Count; i++) PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i]);
340:   PetscObjectRegisterDestroy_Count = 0;
341:   return 0;
342: }

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

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

351:    Not Collective

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

356:    Level: developer

358:    Note:
359:       This is used by, for example, `DMInitializePackage()` to have `DMFinalizePackage()` called

361: .seealso: `PetscRegisterFinalizeAll()`
362: @*/
363: PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*f)(void))
364: {
365:   for (PetscInt i = 0; i < PetscRegisterFinalize_Count; i++) {
366:     if (f == PetscRegisterFinalize_Functions[i]) return 0;
367:   }
369:   PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
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: {
384:   for (PetscInt i = 0; i < PetscRegisterFinalize_Count; i++) (*PetscRegisterFinalize_Functions[i])();
385:   PetscRegisterFinalize_Count = 0;
386:   return 0;
387: }