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