1 #define PETSC_DLL 2 /* 3 Provides utility routines for manulating any type of PETSc object. 4 */ 5 #include "petsc.h" /*I "petsc.h" I*/ 6 7 struct _p_Object { 8 PETSCHEADER(int); 9 }; 10 11 PetscErrorCode PetscObjectDestroy_PetscObject(PetscObject obj) 12 { 13 PetscErrorCode ierr; 14 PetscFunctionBegin; 15 PetscValidHeader(obj,1); 16 if (--obj->refct > 0) PetscFunctionReturn(0); 17 ierr = PetscHeaderDestroy(obj);CHKERRQ(ierr); 18 PetscFunctionReturn(0); 19 } 20 21 #undef __FUNCT__ 22 #define __FUNCT__ "PetscObjectCreate" 23 /*@C 24 PetscObjectCreate - Creates a PetscObject 25 26 Collective on PetscObject 27 28 Input Parameter: 29 . comm - An MPI communicator 30 31 Output Parameter: 32 . obj - The object 33 34 Level: developer 35 36 Notes: This is a template intended as a starting point to cut and paste with PetscObjectDestroy_PetscObject() 37 to make new object classes. 38 39 Concepts: destroying object 40 Concepts: freeing object 41 Concepts: deleting object 42 43 @*/ 44 PetscErrorCode PETSC_DLLEXPORT PetscObjectCreate(MPI_Comm comm, PetscObject *obj) 45 { 46 PetscObject o; 47 PetscErrorCode ierr; 48 49 PetscFunctionBegin; 50 PetscValidPointer(obj,2); 51 52 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 53 ierr = PetscInitializePackage(PETSC_NULL);CHKERRQ(ierr); 54 #endif 55 ierr = PetscHeaderCreate(o,_p_PetscObject,-1,PETSC_OBJECT_COOKIE,0,"PetscObject",comm,PetscObjectDestroy_PetscObject,0);CHKERRQ(ierr); 56 /* records not yet defined in PetscObject 57 o->data = 0; 58 o->setupcalled = 0; 59 */ 60 *obj = o; 61 PetscFunctionReturn(0); 62 } 63 64 #undef __FUNCT__ 65 #define __FUNCT__ "PetscObjectDestroy" 66 /*@ 67 PetscObjectDestroy - Destroys any PetscObject, regardless of the type. 68 69 Collective on PetscObject 70 71 Input Parameter: 72 . obj - any PETSc object, for example a Vec, Mat or KSP. 73 This must be cast with a (PetscObject), for example, 74 PetscObjectDestroy((PetscObject)mat); 75 76 Level: beginner 77 78 Concepts: destroying object 79 Concepts: freeing object 80 Concepts: deleting object 81 82 @*/ 83 PetscErrorCode PETSC_DLLEXPORT PetscObjectDestroy(PetscObject obj) 84 { 85 PetscErrorCode ierr; 86 87 PetscFunctionBegin; 88 PetscValidHeader(obj,1); 89 90 if (obj->bops->destroy) { 91 ierr = (*obj->bops->destroy)(obj);CHKERRQ(ierr); 92 } else { 93 SETERRQ1(PETSC_ERR_PLIB,"This PETSc object of class %s does not have a generic destroy routine",obj->class_name); 94 } 95 PetscFunctionReturn(0); 96 } 97 98 #undef __FUNCT__ 99 #define __FUNCT__ "PetscObjectView" 100 /*@C 101 PetscObjectView - Views any PetscObject, regardless of the type. 102 103 Collective on PetscObject 104 105 Input Parameters: 106 + obj - any PETSc object, for example a Vec, Mat or KSP. 107 This must be cast with a (PetscObject), for example, 108 PetscObjectView((PetscObject)mat,viewer); 109 - viewer - any PETSc viewer 110 111 Level: intermediate 112 113 @*/ 114 PetscErrorCode PETSC_DLLEXPORT PetscObjectView(PetscObject obj,PetscViewer viewer) 115 { 116 PetscErrorCode ierr; 117 118 PetscFunctionBegin; 119 PetscValidHeader(obj,1); 120 if (!viewer) viewer = PETSC_VIEWER_STDOUT_(obj->comm); 121 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2); 122 123 if (obj->bops->view) { 124 ierr = (*obj->bops->view)(obj,viewer);CHKERRQ(ierr); 125 } else { 126 SETERRQ(PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine"); 127 } 128 PetscFunctionReturn(0); 129 } 130 131 #undef __FUNCT__ 132 #define __FUNCT__ "PetscTypeCompare" 133 /*@C 134 PetscTypeCompare - Determines whether a PETSc object is of a particular type. 135 136 Not Collective 137 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 PetscObjectDestroy((PetscObject)mat); 142 - type_name - string containing a type name 143 144 Output Parameter: 145 . same - PETSC_TRUE if they are the same, else PETSC_FALSE 146 147 Level: intermediate 148 149 .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType() 150 151 Concepts: comparing^object types 152 Concepts: types^comparing 153 Concepts: object type^comparing 154 155 @*/ 156 PetscErrorCode PETSC_DLLEXPORT PetscTypeCompare(PetscObject obj,const char type_name[],PetscTruth *same) 157 { 158 PetscErrorCode ierr; 159 160 PetscFunctionBegin; 161 if (!obj) { 162 *same = PETSC_FALSE; 163 } else if (!type_name && !obj->type_name) { 164 *same = PETSC_TRUE; 165 } else if (!type_name || !obj->type_name) { 166 *same = PETSC_FALSE; 167 } else { 168 PetscValidHeader(obj,1); 169 PetscValidCharPointer(type_name,2); 170 PetscValidPointer(same,3); 171 ierr = PetscStrcmp((char*)(obj->type_name),type_name,same);CHKERRQ(ierr); 172 } 173 PetscFunctionReturn(0); 174 } 175 176 static int PetscObjectRegisterDestroy_Count = 0; 177 static PetscObject PetscObjectRegisterDestroy_Objects[256]; 178 179 #undef __FUNCT__ 180 #define __FUNCT__ "PetscObjectRegisterDestroy" 181 /*@C 182 PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when 183 PetscFinalize() is called. 184 185 Collective on PetscObject 186 187 Input Parameter: 188 . obj - any PETSc object, for example a Vec, Mat or KSP. 189 This must be cast with a (PetscObject), for example, 190 PetscObjectRegisterDestroy((PetscObject)mat); 191 192 Level: developer 193 194 Notes: 195 This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer 196 when PETSc ends. 197 198 .seealso: PetscObjectRegisterDestroyAll() 199 @*/ 200 PetscErrorCode PETSC_DLLEXPORT PetscObjectRegisterDestroy(PetscObject obj) 201 { 202 PetscFunctionBegin; 203 PetscValidHeader(obj,1); 204 PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj; 205 PetscFunctionReturn(0); 206 } 207 208 #undef __FUNCT__ 209 #define __FUNCT__ "PetscObjectRegisterDestroyAll" 210 /*@C 211 PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered 212 with PetscObjectRegisterDestroy(). Called by PetscFinalize() 213 PetscFinalize() is called. 214 215 Collective on individual PetscObjects 216 217 Level: developer 218 219 .seealso: PetscObjectRegisterDestroy() 220 @*/ 221 PetscErrorCode PETSC_DLLEXPORT PetscObjectRegisterDestroyAll(void) 222 { 223 PetscErrorCode ierr; 224 int i; 225 226 PetscFunctionBegin; 227 for (i=0; i<PetscObjectRegisterDestroy_Count; i++) { 228 ierr = PetscObjectDestroy(PetscObjectRegisterDestroy_Objects[i]);CHKERRQ(ierr); 229 } 230 PetscFunctionReturn(0); 231 } 232 233 234