1 2 /* 3 Provides utility routines for manulating any type of PETSc object. 4 */ 5 #include <petsc-private/petscimpl.h> /*I "petscsys.h" I*/ 6 #include <petscviewer.h> 7 8 #undef __FUNCT__ 9 #define __FUNCT__ "PetscComposedQuantitiesDestroy" 10 PetscErrorCode PetscComposedQuantitiesDestroy(PetscObject obj) 11 { 12 PetscErrorCode ierr; 13 PetscInt i; 14 15 PetscFunctionBegin; 16 if (obj->intstar_idmax>0) { 17 for (i=0; i<obj->intstar_idmax; i++) { 18 ierr = PetscFree(obj->intstarcomposeddata[i]);CHKERRQ(ierr); 19 } 20 ierr = PetscFree(obj->intstarcomposeddata);CHKERRQ(ierr); 21 ierr = PetscFree(obj->intstarcomposedstate);CHKERRQ(ierr); 22 } 23 if (obj->realstar_idmax>0) { 24 for (i=0; i<obj->realstar_idmax; i++) { 25 ierr = PetscFree(obj->realstarcomposeddata[i]);CHKERRQ(ierr); 26 } 27 ierr = PetscFree(obj->realstarcomposeddata);CHKERRQ(ierr); 28 ierr = PetscFree(obj->realstarcomposedstate);CHKERRQ(ierr); 29 } 30 if (obj->scalarstar_idmax>0) { 31 for (i=0; i<obj->scalarstar_idmax; i++) { 32 ierr = PetscFree(obj->scalarstarcomposeddata[i]);CHKERRQ(ierr); 33 } 34 ierr = PetscFree(obj->scalarstarcomposeddata);CHKERRQ(ierr); 35 ierr = PetscFree(obj->scalarstarcomposedstate);CHKERRQ(ierr); 36 } 37 ierr = PetscFree(obj->intcomposeddata);CHKERRQ(ierr); 38 ierr = PetscFree(obj->intcomposedstate);CHKERRQ(ierr); 39 ierr = PetscFree(obj->realcomposeddata);CHKERRQ(ierr); 40 ierr = PetscFree(obj->realcomposedstate);CHKERRQ(ierr); 41 ierr = PetscFree(obj->scalarcomposeddata);CHKERRQ(ierr); 42 ierr = PetscFree(obj->scalarcomposedstate);CHKERRQ(ierr); 43 PetscFunctionReturn(0); 44 } 45 46 #undef __FUNCT__ 47 #define __FUNCT__ "PetscObjectDestroy" 48 /*@ 49 PetscObjectDestroy - Destroys any PetscObject, regardless of the type. 50 51 Collective on PetscObject 52 53 Input Parameter: 54 . obj - any PETSc object, for example a Vec, Mat or KSP. 55 This must be cast with a (PetscObject*), for example, 56 PetscObjectDestroy((PetscObject*)&mat); 57 58 Level: beginner 59 60 Concepts: destroying object 61 Concepts: freeing object 62 Concepts: deleting object 63 64 @*/ 65 PetscErrorCode PetscObjectDestroy(PetscObject *obj) 66 { 67 PetscErrorCode ierr; 68 69 PetscFunctionBegin; 70 if (!*obj) PetscFunctionReturn(0); 71 PetscValidHeader(*obj,1); 72 if (*obj && (*obj)->bops->destroy) { 73 ierr = (*(*obj)->bops->destroy)(obj);CHKERRQ(ierr); 74 } 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); 75 PetscFunctionReturn(0); 76 } 77 78 #undef __FUNCT__ 79 #define __FUNCT__ "PetscObjectView" 80 /*@C 81 PetscObjectView - Views any PetscObject, regardless of the type. 82 83 Collective on PetscObject 84 85 Input Parameters: 86 + obj - any PETSc object, for example a Vec, Mat or KSP. 87 This must be cast with a (PetscObject), for example, 88 PetscObjectView((PetscObject)mat,viewer); 89 - viewer - any PETSc viewer 90 91 Level: intermediate 92 93 @*/ 94 PetscErrorCode PetscObjectView(PetscObject obj,PetscViewer viewer) 95 { 96 PetscErrorCode ierr; 97 98 PetscFunctionBegin; 99 PetscValidHeader(obj,1); 100 if (!viewer) { 101 ierr = PetscViewerASCIIGetStdout(obj->comm,&viewer);CHKERRQ(ierr); 102 } 103 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 104 105 if (obj->bops->view) { 106 ierr = (*obj->bops->view)(obj,viewer);CHKERRQ(ierr); 107 } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine"); 108 PetscFunctionReturn(0); 109 } 110 111 #undef __FUNCT__ 112 #define __FUNCT__ "PetscObjectTypeCompare" 113 /*@C 114 PetscObjectTypeCompare - Determines whether a PETSc object is of a particular type. 115 116 Not Collective 117 118 Input Parameters: 119 + obj - any PETSc object, for example a Vec, Mat or KSP. 120 This must be cast with a (PetscObject), for example, 121 PetscObjectTypeCompare((PetscObject)mat); 122 - type_name - string containing a type name 123 124 Output Parameter: 125 . same - PETSC_TRUE if they are the same, else PETSC_FALSE 126 127 Level: intermediate 128 129 .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType() 130 131 Concepts: comparing^object types 132 Concepts: types^comparing 133 Concepts: object type^comparpeing 134 135 @*/ 136 PetscErrorCode PetscObjectTypeCompare(PetscObject obj,const char type_name[],PetscBool *same) 137 { 138 PetscErrorCode ierr; 139 140 PetscFunctionBegin; 141 if (!obj) *same = PETSC_FALSE; 142 else if (!type_name && !obj->type_name) *same = PETSC_TRUE; 143 else if (!type_name || !obj->type_name) *same = PETSC_FALSE; 144 else { 145 PetscValidHeader(obj,1); 146 PetscValidCharPointer(type_name,2); 147 PetscValidPointer(same,3); 148 ierr = PetscStrcmp((char*)(obj->type_name),type_name,same);CHKERRQ(ierr); 149 } 150 PetscFunctionReturn(0); 151 } 152 153 #undef __FUNCT__ 154 #define __FUNCT__ "PetscObjectTypeCompareAny" 155 /*@C 156 PetscObjectTypeCompareAny - Determines whether a PETSc object is of any of a list of types. 157 158 Not Collective 159 160 Input Parameters: 161 + obj - any PETSc object, for example a Vec, Mat or KSP. 162 This must be cast with a (PetscObject), for example, PetscObjectTypeCompareAny((PetscObject)mat,...); 163 - type_name - string containing a type name, pass the empty string "" to terminate the list 164 165 Output Parameter: 166 . match - PETSC_TRUE if the type of obj matches any in the list, else PETSC_FALSE 167 168 Level: intermediate 169 170 .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType(), PetscObjectTypeCompare() 171 172 Concepts: comparing^object types 173 Concepts: types^comparing 174 Concepts: object type^comparing 175 176 @*/ 177 PetscErrorCode PetscObjectTypeCompareAny(PetscObject obj,PetscBool *match,const char type_name[],...) 178 { 179 PetscErrorCode ierr; 180 va_list Argp; 181 182 PetscFunctionBegin; 183 *match = PETSC_FALSE; 184 va_start(Argp,type_name); 185 while (type_name && type_name[0]) { 186 PetscBool found; 187 ierr = PetscObjectTypeCompare(obj,type_name,&found);CHKERRQ(ierr); 188 if (found) { 189 *match = PETSC_TRUE; 190 break; 191 } 192 type_name = va_arg(Argp,const char*); 193 } 194 va_end(Argp); 195 PetscFunctionReturn(0); 196 } 197 198 #define MAXREGDESOBJS 256 199 static int PetscObjectRegisterDestroy_Count = 0; 200 static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS]; 201 202 #undef __FUNCT__ 203 #define __FUNCT__ "PetscObjectRegisterDestroy" 204 /*@C 205 PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when 206 PetscFinalize() is called. 207 208 Logically Collective on PetscObject 209 210 Input Parameter: 211 . obj - any PETSc object, for example a Vec, Mat or KSP. 212 This must be cast with a (PetscObject), for example, 213 PetscObjectRegisterDestroy((PetscObject)mat); 214 215 Level: developer 216 217 Notes: 218 This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer 219 when PETSc ends. 220 221 .seealso: PetscObjectRegisterDestroyAll() 222 @*/ 223 PetscErrorCode PetscObjectRegisterDestroy(PetscObject obj) 224 { 225 PetscFunctionBegin; 226 PetscValidHeader(obj,1); 227 if (PetscObjectRegisterDestroy_Count < MAXREGDESOBJS) 228 PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj; 229 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); 230 PetscFunctionReturn(0); 231 } 232 233 #undef __FUNCT__ 234 #define __FUNCT__ "PetscObjectRegisterDestroyAll" 235 /*@C 236 PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered 237 with PetscObjectRegisterDestroy(). Called by PetscFinalize() 238 239 Logically Collective on individual PetscObjects 240 241 Level: developer 242 243 .seealso: PetscObjectRegisterDestroy() 244 @*/ 245 PetscErrorCode PetscObjectRegisterDestroyAll(void) 246 { 247 PetscErrorCode ierr; 248 PetscInt i; 249 250 PetscFunctionBegin; 251 for (i=0; i<PetscObjectRegisterDestroy_Count; i++) { 252 ierr = PetscObjectDestroy(&PetscObjectRegisterDestroy_Objects[i]);CHKERRQ(ierr); 253 } 254 PetscObjectRegisterDestroy_Count = 0; 255 PetscFunctionReturn(0); 256 } 257 258 259 #define MAXREGFIN 256 260 static int PetscRegisterFinalize_Count = 0; 261 static PetscErrorCode ((*PetscRegisterFinalize_Functions[MAXREGFIN])(void)); 262 263 #undef __FUNCT__ 264 #define __FUNCT__ "PetscRegisterFinalize" 265 /*@C 266 PetscRegisterFinalize - Registers a function that is to be called in PetscFinalize() 267 268 Not Collective 269 270 Input Parameter: 271 . PetscErrorCode (*fun)(void) - 272 273 Level: developer 274 275 Notes: 276 This is used by, for example, DMInitializePackage() to have DMFinalizePackage() called 277 278 .seealso: PetscRegisterFinalizeAll() 279 @*/ 280 PetscErrorCode PetscRegisterFinalize(PetscErrorCode (*f)(void)) 281 { 282 PetscInt i; 283 284 PetscFunctionBegin; 285 for (i=0; i<PetscRegisterFinalize_Count; i++) { 286 if (f == PetscRegisterFinalize_Functions[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Called twice with same function to register"); 287 } 288 if (PetscRegisterFinalize_Count < MAXREGFIN) PetscRegisterFinalize_Functions[PetscRegisterFinalize_Count++] = f; 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 MAXREGFIN\n",MAXREGFIN); 290 PetscFunctionReturn(0); 291 } 292 293 #undef __FUNCT__ 294 #define __FUNCT__ "PetscRegisterFinalizeAll" 295 /*@C 296 PetscRegisterFinalizeAll - Runs all the finalize functions set with PetscRegisterFinalize() 297 298 Not Collective unless registered functions are collective 299 300 Level: developer 301 302 .seealso: PetscRegisterFinalize() 303 @*/ 304 PetscErrorCode PetscRegisterFinalizeAll(void) 305 { 306 PetscErrorCode ierr; 307 PetscInt i; 308 309 PetscFunctionBegin; 310 for (i=0; i<PetscRegisterFinalize_Count; i++) { 311 ierr = (*PetscRegisterFinalize_Functions[i])();CHKERRQ(ierr); 312 } 313 PetscRegisterFinalize_Count = 0; 314 PetscFunctionReturn(0); 315 } 316