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 7 /*@C 8 PetscObjectStateGet - Gets the state of any PetscObject, 9 regardless of the type. 10 11 Not Collective 12 13 Input Parameter: 14 . obj - any PETSc object, for example a Vec, Mat or KSP. This must be 15 cast with a (PetscObject), for example, 16 PetscObjectStateGet((PetscObject)mat,&state); 17 18 Output Parameter: 19 . state - the object state 20 21 Notes: 22 object state is an integer which gets increased every time 23 the object is changed. By saving and later querying the object state 24 one can determine whether information about the object is still current. 25 Currently, state is maintained for Vec and Mat objects. 26 27 Level: advanced 28 29 seealso: PetscObjectStateIncrease(), PetscObjectStateSet() 30 31 @*/ 32 PetscErrorCode PetscObjectStateGet(PetscObject obj,PetscObjectState *state) 33 { 34 PetscFunctionBegin; 35 PetscValidHeader(obj,1); 36 PetscValidIntPointer(state,2); 37 *state = obj->state; 38 PetscFunctionReturn(0); 39 } 40 41 /*@C 42 PetscObjectStateSet - Sets the state of any PetscObject, 43 regardless of the type. 44 45 Logically Collective 46 47 Input Parameters: 48 + obj - any PETSc object, for example a Vec, Mat or KSP. This must be 49 cast with a (PetscObject), for example, 50 PetscObjectStateSet((PetscObject)mat,state); 51 - state - the object state 52 53 Notes: 54 This function should be used with extreme caution. There is 55 essentially only one use for it: if the user calls Mat(Vec)GetRow(Array), 56 which increases the state, but does not alter the data, then this 57 routine can be used to reset the state. Such a reset must be collective. 58 59 Level: advanced 60 61 seealso: PetscObjectStateGet(),PetscObjectStateIncrease() 62 63 @*/ 64 PetscErrorCode PetscObjectStateSet(PetscObject obj,PetscObjectState state) 65 { 66 PetscFunctionBegin; 67 PetscValidHeader(obj,1); 68 obj->state = state; 69 PetscFunctionReturn(0); 70 } 71 72 PetscInt PetscObjectComposedDataMax = 10; 73 74 /*@C 75 PetscObjectComposedDataRegister - Get an available id for composed data 76 77 Not Collective 78 79 Output parameter: 80 . id - an identifier under which data can be stored 81 82 Level: developer 83 84 Notes: 85 You must keep this value (for example in a global variable) in order to attach the data to an object or 86 access in an object. 87 88 seealso: PetscObjectComposedDataSetInt() 89 90 @*/ 91 PetscErrorCode PetscObjectComposedDataRegister(PetscInt *id) 92 { 93 static PetscInt globalcurrentstate = 0; 94 95 PetscFunctionBegin; 96 *id = globalcurrentstate++; 97 if (globalcurrentstate > PetscObjectComposedDataMax) PetscObjectComposedDataMax += 10; 98 PetscFunctionReturn(0); 99 } 100 101 PetscErrorCode PetscObjectComposedDataIncreaseInt(PetscObject obj) 102 { 103 PetscInt *ar = obj->intcomposeddata,*new_ar,n = obj->int_idmax,new_n; 104 PetscObjectState *ir = obj->intcomposedstate,*new_ir; 105 106 PetscFunctionBegin; 107 new_n = PetscObjectComposedDataMax; 108 PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir)); 109 PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscInt))); 110 PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState))); 111 PetscCall(PetscFree2(ar,ir)); 112 obj->int_idmax = new_n; 113 obj->intcomposeddata = new_ar; obj->intcomposedstate = new_ir; 114 PetscFunctionReturn(0); 115 } 116 117 PetscErrorCode PetscObjectComposedDataIncreaseIntstar(PetscObject obj) 118 { 119 PetscInt **ar = obj->intstarcomposeddata,**new_ar,n = obj->intstar_idmax,new_n; 120 PetscObjectState *ir = obj->intstarcomposedstate,*new_ir; 121 122 PetscFunctionBegin; 123 new_n = PetscObjectComposedDataMax; 124 PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir)); 125 PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscInt*))); 126 PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState))); 127 PetscCall(PetscFree2(ar,ir)); 128 obj->intstar_idmax = new_n; 129 obj->intstarcomposeddata = new_ar; 130 obj->intstarcomposedstate = new_ir; 131 PetscFunctionReturn(0); 132 } 133 134 PetscErrorCode PetscObjectComposedDataIncreaseReal(PetscObject obj) 135 { 136 PetscReal *ar = obj->realcomposeddata,*new_ar; 137 PetscObjectState *ir = obj->realcomposedstate,*new_ir; 138 PetscInt n = obj->real_idmax,new_n; 139 140 PetscFunctionBegin; 141 new_n = PetscObjectComposedDataMax; 142 PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir)); 143 PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscReal))); 144 PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState))); 145 PetscCall(PetscFree2(ar,ir)); 146 obj->real_idmax = new_n; 147 obj->realcomposeddata = new_ar; obj->realcomposedstate = new_ir; 148 PetscFunctionReturn(0); 149 } 150 151 PetscErrorCode PetscObjectComposedDataIncreaseRealstar(PetscObject obj) 152 { 153 PetscReal **ar = obj->realstarcomposeddata,**new_ar; 154 PetscObjectState *ir = obj->realstarcomposedstate,*new_ir; 155 PetscInt n = obj->realstar_idmax,new_n; 156 157 PetscFunctionBegin; 158 new_n = PetscObjectComposedDataMax; 159 PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir)); 160 PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscReal*))); 161 PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState))); 162 PetscCall(PetscFree2(ar,ir)); 163 obj->realstar_idmax = new_n; 164 obj->realstarcomposeddata = new_ar; obj->realstarcomposedstate = new_ir; 165 PetscFunctionReturn(0); 166 } 167 168 PetscErrorCode PetscObjectComposedDataIncreaseScalar(PetscObject obj) 169 { 170 PetscScalar *ar = obj->scalarcomposeddata,*new_ar; 171 PetscObjectState *ir = obj->scalarcomposedstate,*new_ir; 172 PetscInt n = obj->scalar_idmax,new_n; 173 174 PetscFunctionBegin; 175 new_n = PetscObjectComposedDataMax; 176 PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir)); 177 PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscScalar))); 178 PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState))); 179 PetscCall(PetscFree2(ar,ir)); 180 obj->scalar_idmax = new_n; 181 obj->scalarcomposeddata = new_ar; obj->scalarcomposedstate = new_ir; 182 PetscFunctionReturn(0); 183 } 184 185 PetscErrorCode PetscObjectComposedDataIncreaseScalarstar(PetscObject obj) 186 { 187 PetscScalar **ar = obj->scalarstarcomposeddata,**new_ar; 188 PetscObjectState *ir = obj->scalarstarcomposedstate,*new_ir; 189 PetscInt n = obj->scalarstar_idmax,new_n; 190 191 PetscFunctionBegin; 192 new_n = PetscObjectComposedDataMax; 193 PetscCall(PetscCalloc2(new_n,&new_ar,new_n,&new_ir)); 194 PetscCall(PetscMemcpy(new_ar,ar,n*sizeof(PetscScalar*))); 195 PetscCall(PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState))); 196 PetscCall(PetscFree2(ar,ir)); 197 obj->scalarstar_idmax = new_n; 198 obj->scalarstarcomposeddata = new_ar; obj->scalarstarcomposedstate = new_ir; 199 PetscFunctionReturn(0); 200 } 201 202 /*@ 203 PetscObjectGetId - get unique object ID 204 205 Not Collective 206 207 Input Parameter: 208 . obj - object 209 210 Output Parameter: 211 . id - integer ID 212 213 Level: developer 214 215 Notes: 216 The object ID may be different on different processes, but object IDs are never reused so local equality implies global equality. 217 218 .seealso: `PetscObjectStateGet()`, `PetscObjectCompareId()` 219 @*/ 220 PetscErrorCode PetscObjectGetId(PetscObject obj,PetscObjectId *id) 221 { 222 PetscFunctionBegin; 223 *id = obj->id; 224 PetscFunctionReturn(0); 225 } 226 227 /*@ 228 PetscObjectCompareId - compares the objects ID with a given id 229 230 Not Collective 231 232 Input Parameters: 233 + obj - object 234 - id - integer ID 235 236 Output Parameter; 237 . eq - the ids are equal 238 239 Level: developer 240 241 Notes: 242 The object ID may be different on different processes, but object IDs are never reused so local equality implies global equality. 243 244 .seealso: `PetscObjectStateGet()`, `PetscObjectGetId()` 245 @*/ 246 PetscErrorCode PetscObjectCompareId(PetscObject obj,PetscObjectId id,PetscBool *eq) 247 { 248 PetscFunctionBegin; 249 *eq = (id == obj->id) ? PETSC_TRUE : PETSC_FALSE; 250 PetscFunctionReturn(0); 251 } 252