Actual source code: state.c

petsc-master 2019-09-16
Report Typos and Errors

  2: /*
  3:      Provides utility routines for manulating any type of PETSc object.
  4: */
  5:  #include <petsc/private/petscimpl.h>

  7: /*@C
  8:    PetscObjectStateGet - Gets the state of any PetscObject,
  9:    regardless of the type.

 11:    Not Collective

 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);

 18:    Output Parameter:
 19: .  state - the object state

 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.

 27:    Level: advanced

 29:    seealso: PetscObjectStateIncrease(), PetscObjectStateSet()

 31: @*/
 32: PetscErrorCode PetscObjectStateGet(PetscObject obj,PetscObjectState *state)
 33: {
 37:   *state = obj->state;
 38:   return(0);
 39: }

 41: /*@C
 42:    PetscObjectStateSet - Sets the state of any PetscObject,
 43:    regardless of the type.

 45:    Logically Collective

 47:    Input Parameter:
 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

 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.

 59:    Level: advanced

 61:    seealso: PetscObjectStateGet(),PetscObjectStateIncrease()

 63: @*/
 64: PetscErrorCode PetscObjectStateSet(PetscObject obj,PetscObjectState state)
 65: {
 68:   obj->state = state;
 69:   return(0);
 70: }

 72: PetscInt PetscObjectComposedDataMax = 10;

 74: /*@C
 75:    PetscObjectComposedDataRegister - Get an available id for composed data

 77:    Not Collective

 79:    Output parameter:
 80: .  id - an identifier under which data can be stored

 82:    Level: developer

 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.

 88:    seealso: PetscObjectComposedDataSetInt()

 90: @*/
 91: PetscErrorCode  PetscObjectComposedDataRegister(PetscInt *id)
 92: {
 93:   static PetscInt globalcurrentstate = 0;

 96:   *id = globalcurrentstate++;
 97:   if (globalcurrentstate > PetscObjectComposedDataMax) PetscObjectComposedDataMax += 10;
 98:   return(0);
 99: }

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:   PetscErrorCode   ierr;

108:   new_n = PetscObjectComposedDataMax;
109:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
110:   PetscMemcpy(new_ar,ar,n*sizeof(PetscInt));
111:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
112:   PetscFree2(ar,ir);
113:   obj->int_idmax       = new_n;
114:   obj->intcomposeddata = new_ar; obj->intcomposedstate = new_ir;
115:   return(0);
116: }

118: PetscErrorCode  PetscObjectComposedDataIncreaseIntstar(PetscObject obj)
119: {
120:   PetscInt         **ar = obj->intstarcomposeddata,**new_ar,n = obj->intstar_idmax,new_n;
121:   PetscObjectState *ir  = obj->intstarcomposedstate,*new_ir;
122:   PetscErrorCode   ierr;

125:   new_n = PetscObjectComposedDataMax;
126:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
127:   PetscMemcpy(new_ar,ar,n*sizeof(PetscInt*));
128:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
129:   PetscFree2(ar,ir);
130:   obj->intstar_idmax        = new_n;
131:   obj->intstarcomposeddata  = new_ar;
132:   obj->intstarcomposedstate = new_ir;
133:   return(0);
134: }

136: PetscErrorCode  PetscObjectComposedDataIncreaseReal(PetscObject obj)
137: {
138:   PetscReal        *ar = obj->realcomposeddata,*new_ar;
139:   PetscObjectState *ir = obj->realcomposedstate,*new_ir;
140:   PetscInt         n   = obj->real_idmax,new_n;
141:   PetscErrorCode   ierr;

144:   new_n = PetscObjectComposedDataMax;
145:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
146:   PetscMemcpy(new_ar,ar,n*sizeof(PetscReal));
147:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
148:   PetscFree2(ar,ir);
149:   obj->real_idmax       = new_n;
150:   obj->realcomposeddata = new_ar; obj->realcomposedstate = new_ir;
151:   return(0);
152: }

154: PetscErrorCode  PetscObjectComposedDataIncreaseRealstar(PetscObject obj)
155: {
156:   PetscReal        **ar = obj->realstarcomposeddata,**new_ar;
157:   PetscObjectState *ir  = obj->realstarcomposedstate,*new_ir;
158:   PetscInt         n    = obj->realstar_idmax,new_n;
159:   PetscErrorCode   ierr;

162:   new_n = PetscObjectComposedDataMax;
163:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
164:   PetscMemcpy(new_ar,ar,n*sizeof(PetscReal*));
165:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
166:   PetscFree2(ar,ir);
167:   obj->realstar_idmax       = new_n;
168:   obj->realstarcomposeddata = new_ar; obj->realstarcomposedstate = new_ir;
169:   return(0);
170: }

172: PetscErrorCode  PetscObjectComposedDataIncreaseScalar(PetscObject obj)
173: {
174:   PetscScalar      *ar = obj->scalarcomposeddata,*new_ar;
175:   PetscObjectState *ir = obj->scalarcomposedstate,*new_ir;
176:   PetscInt         n   = obj->scalar_idmax,new_n;
177:   PetscErrorCode   ierr;

180:   new_n = PetscObjectComposedDataMax;
181:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
182:   PetscMemcpy(new_ar,ar,n*sizeof(PetscScalar));
183:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
184:   PetscFree2(ar,ir);
185:   obj->scalar_idmax       = new_n;
186:   obj->scalarcomposeddata = new_ar; obj->scalarcomposedstate = new_ir;
187:   return(0);
188: }

190: PetscErrorCode  PetscObjectComposedDataIncreaseScalarstar(PetscObject obj)
191: {
192:   PetscScalar      **ar = obj->scalarstarcomposeddata,**new_ar;
193:   PetscObjectState *ir  = obj->scalarstarcomposedstate,*new_ir;
194:   PetscInt         n    = obj->scalarstar_idmax,new_n;
195:   PetscErrorCode   ierr;

198:   new_n = PetscObjectComposedDataMax;
199:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
200:   PetscMemcpy(new_ar,ar,n*sizeof(PetscScalar*));
201:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
202:   PetscFree2(ar,ir);
203:   obj->scalarstar_idmax       = new_n;
204:   obj->scalarstarcomposeddata = new_ar; obj->scalarstarcomposedstate = new_ir;
205:   return(0);
206: }

208: /*@
209:    PetscObjectGetId - get unique object ID

211:    Not Collective

213:    Input Arguments:
214: .  obj - object

216:    Output Arguments:
217: .  id - integer ID

219:    Level: developer

221:    Notes:
222:    The object ID may be different on different processes, but object IDs are never reused so local equality implies global equality.

224: .seealso: PetscObjectStateGet(), PetscObjectCompareId()
225: @*/
226: PetscErrorCode PetscObjectGetId(PetscObject obj,PetscObjectId *id)
227: {

230:   *id = obj->id;
231:   return(0);
232: }

234: /*@
235:    PetscObjectCompareId - compares the objects ID with a given id

237:    Not Collective

239:    Input Arguments:
240: +  obj - object
241: -  id - integer ID

243:    Output Argument;
244: .  eq - the ids are equal

246:    Level: developer

248:    Notes:
249:    The object ID may be different on different processes, but object IDs are never reused so local equality implies global equality.

251: .seealso: PetscObjectStateGet(), PetscObjectGetId()
252: @*/
253: PetscErrorCode PetscObjectCompareId(PetscObject obj,PetscObjectId id,PetscBool *eq)
254: {

257:   *eq = (id == obj->id) ? PETSC_TRUE : PETSC_FALSE;
258:   return(0);
259: }