Actual source code: destroy.c

  1: #define PETSC_DLL
  2: /*
  3:      Provides utility routines for manulating any type of PETSc object.
  4: */
 5:  #include petsc.h

  7: typedef struct _p_GenericObject* GenericObject;

  9: struct _p_GenericObject {
 10:   PETSCHEADER(int);
 11: };

 13: PetscErrorCode PetscObjectDestroy_GenericObject(GenericObject obj)
 14: {
 18:   if (--((PetscObject)obj)->refct > 0) return(0);
 19:   PetscHeaderDestroy(obj);
 20:   return(0);
 21: }

 25: /*@C
 26:    PetscObjectCreate - Creates a PetscObject

 28:    Collective on PetscObject

 30:    Input Parameter:
 31: .  comm - An MPI communicator

 33:    Output Parameter:
 34: .  obj - The object

 36:    Level: developer

 38:    Notes: This is a template intended as a starting point to cut and paste with PetscObjectDestroy_GenericObject()
 39:           to make new object classes.

 41:     Concepts: destroying object
 42:     Concepts: freeing object
 43:     Concepts: deleting object

 45: @*/
 46: PetscErrorCode  PetscObjectCreate(MPI_Comm comm, PetscObject *obj)
 47: {
 48:   GenericObject  o;

 53: #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
 54:   PetscInitializePackage(PETSC_NULL);
 55: #endif
 56:   PetscHeaderCreate(o,_p_GenericObject,-1,PETSC_OBJECT_COOKIE,0,"PetscObject",comm,PetscObjectDestroy_GenericObject,0);
 57:   /* records not yet defined in PetscObject 
 58:   o->data        = 0;
 59:   o->setupcalled = 0;
 60:   */
 61:   *obj = (PetscObject)o;
 62:   return(0);
 63: }

 67: /*@C
 68:    PetscObjectCreateGeneric - Creates a PetscObject

 70:    Collective on PetscObject

 72:    Input Parameter:
 73: +  comm - An MPI communicator
 74: .  cookie - The class cookie
 75: -  name - The class name

 77:    Output Parameter:
 78: .  obj - The object

 80:    Level: developer

 82:    Notes: This is a template intended as a starting point to cut and paste with PetscObjectDestroy_GenericObject()
 83:           to make new object classes.

 85:     Concepts: destroying object
 86:     Concepts: freeing object
 87:     Concepts: deleting object

 89: @*/
 90: PetscErrorCode  PetscObjectCreateGeneric(MPI_Comm comm, PetscCookie cookie, const char name[], PetscObject *obj)
 91: {
 92:   GenericObject  o;

 97: #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
 98:   PetscInitializePackage(PETSC_NULL);
 99: #endif
100:   PetscHeaderCreate(o,_p_GenericObject,-1,cookie,0,name,comm,PetscObjectDestroy_GenericObject,0);
101:   /* records not yet defined in PetscObject 
102:   o->data        = 0;
103:   o->setupcalled = 0;
104:   */
105:   *obj = (PetscObject)o;
106:   return(0);
107: }

111: /*@
112:    PetscObjectDestroy - Destroys any PetscObject, regardless of the type. 

114:    Collective on PetscObject

116:    Input Parameter:
117: .  obj - any PETSc object, for example a Vec, Mat or KSP.
118:          This must be cast with a (PetscObject), for example, 
119:          PetscObjectDestroy((PetscObject)mat);

121:    Level: beginner

123:     Concepts: destroying object
124:     Concepts: freeing object
125:     Concepts: deleting object

127: @*/
128: PetscErrorCode  PetscObjectDestroy(PetscObject obj)
129: {

134:   if (obj->bops->destroy) {
135:     (*obj->bops->destroy)(obj);
136:   } else {
137:     SETERRQ1(PETSC_ERR_PLIB,"This PETSc object of class %s does not have a generic destroy routine",obj->class_name);
138:   }
139:   return(0);
140: }

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

147:    Collective on PetscObject

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:          PetscObjectView((PetscObject)mat,viewer);
153: -  viewer - any PETSc viewer

155:    Level: intermediate

157: @*/
158: PetscErrorCode  PetscObjectView(PetscObject obj,PetscViewer viewer)
159: {

164:   if (!viewer) {
165:     PetscViewerASCIIGetStdout(obj->comm,&viewer);
166:   }

169:   if (obj->bops->view) {
170:     (*obj->bops->view)(obj,viewer);
171:   } else {
172:     SETERRQ(PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
173:   }
174:   return(0);
175: }

179: /*@C
180:    PetscTypeCompare - Determines whether a PETSc object is of a particular type.

182:    Not Collective

184:    Input Parameters:
185: +  obj - any PETSc object, for example a Vec, Mat or KSP.
186:          This must be cast with a (PetscObject), for example, 
187:          PetscObjectDestroy((PetscObject)mat);
188: -  type_name - string containing a type name

190:    Output Parameter:
191: .  same - PETSC_TRUE if they are the same, else PETSC_FALSE
192:   
193:    Level: intermediate

195: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType()

197:    Concepts: comparing^object types
198:    Concepts: types^comparing
199:    Concepts: object type^comparing

201: @*/
202: PetscErrorCode  PetscTypeCompare(PetscObject obj,const char type_name[],PetscTruth *same)
203: {

207:   if (!obj) {
208:     *same = PETSC_FALSE;
209:   } else if (!type_name && !obj->type_name) {
210:     *same = PETSC_TRUE;
211:   } else if (!type_name || !obj->type_name) {
212:     *same = PETSC_FALSE;
213:   } else {
217:     PetscStrcmp((char*)(obj->type_name),type_name,same);
218:   }
219:   return(0);
220: }

222: #define MAXREGDESOBJS 256
223: static int         PetscObjectRegisterDestroy_Count = 0;
224: static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];

228: /*@C
229:    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
230:      PetscFinalize() is called.

232:    Collective on PetscObject

234:    Input Parameter:
235: .  obj - any PETSc object, for example a Vec, Mat or KSP.
236:          This must be cast with a (PetscObject), for example, 
237:          PetscObjectRegisterDestroy((PetscObject)mat);

239:    Level: developer

241:    Notes:
242:       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
243:     when PETSc ends.

245: .seealso: PetscObjectRegisterDestroyAll()
246: @*/
247: PetscErrorCode  PetscObjectRegisterDestroy(PetscObject obj)
248: {
251:   if (PetscObjectRegisterDestroy_Count < MAXREGDESOBJS) {
252:     PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
253:   } else {
254:     SETERRQ1(PETSC_ERR_PLIB,"No more room in array, limit %d \n recompile src/sys/objects/destroy.c with larger value for MAXREGDESOBJS\n",MAXREGDESOBJS);
255: 
256:   }
257:   return(0);
258: }

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

266:    Collective on individual PetscObjects

268:    Level: developer

270: .seealso: PetscObjectRegisterDestroy()
271: @*/
272: PetscErrorCode  PetscObjectRegisterDestroyAll(void)
273: {
275:   int i;

278:   for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
279:     PetscObjectDestroy(PetscObjectRegisterDestroy_Objects[i]);
280:   }
281:   PetscObjectRegisterDestroy_Count = 0;
282:   return(0);
283: }


286: #define MAXREGFIN 256
287: static int         PetscRegisterFinalize_Count = 0;
288: static PetscErrorCode ((*PetscRegisterFinalize_Functions[MAXREGFIN])(void));

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

295:    Not Collective

297:    Input Parameter:
298: .  PetscErrorCode (*fun)(void) - 

300:    Level: developer

302:    Notes:
303:       This is used by, for example, DMPackageInitialize() to have DMPackageFinalize() called 

305: .seealso: PetscRegisterFinalizeAll()
306: @*/
307: PetscErrorCode  PetscRegisterFinalize(PetscErrorCode (*f)(void))
308: {

311:   if (PetscRegisterFinalize_Count < MAXREGFIN) {
312:     PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f;
313:   } else {
314:     SETERRQ1(PETSC_ERR_PLIB,"No more room in array, limit %d \n recompile src/sys/objects/destroy.c with larger value for MAXREGFIN\n",MAXREGFIN);
315: 
316:   }
317:   return(0);
318: }

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

325:    Not Collective unless registered functions are collective

327:    Level: developer

329: .seealso: PetscRegisterFinalize()
330: @*/
331: PetscErrorCode  PetscRegisterFinalizeAll(void)
332: {
334:   int i;

337:   for (i=0; i<PetscRegisterFinalize_Count; i++) {
338:     (*PetscRegisterFinalize_Functions[i])();
339:   }
340:   PetscRegisterFinalize_Count = 0;
341:   return(0);
342: }