Actual source code: state.c

petsc-3.5.1 2014-08-06
Report Typos and Errors
  2: /*
  3:      Provides utility routines for manulating any type of PETSc object.
  4: */
  5: #include <petsc-private/petscimpl.h>  /*I   "petscsys.h"    I*/

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

 13:    Not Collective

 15:    Input Parameter:
 16: .  obj - any PETSc object, for example a Vec, Mat or KSP. This must be
 17:          cast with a (PetscObject), for example,
 18:          PetscObjectStateGet((PetscObject)mat,&state);

 20:    Output Parameter:
 21: .  state - the object state

 23:    Notes: object state is an integer which gets increased every time
 24:    the object is changed. By saving and later querying the object state
 25:    one can determine whether information about the object is still current.
 26:    Currently, state is maintained for Vec and Mat objects.

 28:    Level: advanced

 30:    seealso: PetscObjectStateIncrease(), PetscObjectStateSet()

 32:    Concepts: state

 34: @*/
 35: PetscErrorCode PetscObjectStateGet(PetscObject obj,PetscObjectState *state)
 36: {
 40:   *state = obj->state;
 41:   return(0);
 42: }

 46: /*@C
 47:    PetscObjectStateSet - Sets the state of any PetscObject,
 48:    regardless of the type.

 50:    Logically Collective

 52:    Input Parameter:
 53: +  obj - any PETSc object, for example a Vec, Mat or KSP. This must be
 54:          cast with a (PetscObject), for example,
 55:          PetscObjectStateSet((PetscObject)mat,state);
 56: -  state - the object state

 58:    Notes: This function should be used with extreme caution. There is
 59:    essentially only one use for it: if the user calls Mat(Vec)GetRow(Array),
 60:    which increases the state, but does not alter the data, then this
 61:    routine can be used to reset the state.  Such a reset must be collective.

 63:    Level: advanced

 65:    seealso: PetscObjectStateGet(),PetscObjectStateIncrease()

 67:    Concepts: state

 69: @*/
 70: PetscErrorCode PetscObjectStateSet(PetscObject obj,PetscObjectState state)
 71: {
 74:   obj->state = state;
 75:   return(0);
 76: }

 78: PetscInt PetscObjectComposedDataMax = 10;

 82: /*@C
 83:    PetscObjectComposedDataRegister - Get an available id for composed data

 85:    Not Collective

 87:    Output parameter:
 88: .  id - an identifier under which data can be stored

 90:    Level: developer

 92:    Notes: You must keep this value (for example in a global variable) in order to attach the data to an object or 
 93:           access in an object.

 95:    seealso: PetscObjectComposedDataSetInt()

 97: @*/
 98: PetscErrorCode  PetscObjectComposedDataRegister(PetscInt *id)
 99: {
100:   static PetscInt globalcurrentstate = 0;

103:   *id = globalcurrentstate++;
104:   if (globalcurrentstate > PetscObjectComposedDataMax) PetscObjectComposedDataMax += 10;
105:   return(0);
106: }

110: PetscErrorCode  PetscObjectComposedDataIncreaseInt(PetscObject obj)
111: {
112:   PetscInt         *ar = obj->intcomposeddata,*new_ar,n = obj->int_idmax,new_n,i;
113:   PetscObjectState *ir = obj->intcomposedstate,*new_ir;
114:   PetscErrorCode   ierr;

117:   new_n = PetscObjectComposedDataMax;
118:   PetscCalloc1(new_n,&new_ar);
119:   PetscCalloc1(new_n,&new_ir);
120:   if (n) {
121:     for (i=0; i<n; i++) {
122:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
123:     }
124:     PetscFree(ar);
125:     PetscFree(ir);
126:   }
127:   obj->int_idmax       = new_n;
128:   obj->intcomposeddata = new_ar; obj->intcomposedstate = new_ir;
129:   return(0);
130: }

134: PetscErrorCode  PetscObjectComposedDataIncreaseIntstar(PetscObject obj)
135: {
136:   PetscInt         **ar = obj->intstarcomposeddata,**new_ar,n = obj->intstar_idmax,new_n,i;
137:   PetscObjectState *ir  = obj->intstarcomposedstate,*new_ir;
138:   PetscErrorCode   ierr;

141:   new_n = PetscObjectComposedDataMax;
142:   PetscCalloc1(new_n,&new_ar);
143:   PetscCalloc1(new_n,&new_ir);
144:   if (n) {
145:     for (i=0; i<n; i++) {
146:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
147:     }
148:     PetscFree(ar);
149:     PetscFree(ir);
150:   }
151:   obj->intstar_idmax       = new_n;
152:   obj->intstarcomposeddata = new_ar; obj->intstarcomposedstate = new_ir;
153:   return(0);
154: }

158: PetscErrorCode  PetscObjectComposedDataIncreaseReal(PetscObject obj)
159: {
160:   PetscReal        *ar = obj->realcomposeddata,*new_ar;
161:   PetscObjectState *ir = obj->realcomposedstate,*new_ir;
162:   PetscInt         n   = obj->real_idmax,new_n,i;
163:   PetscErrorCode   ierr;

166:   new_n = PetscObjectComposedDataMax;
167:   PetscCalloc1(new_n,&new_ar);
168:   PetscCalloc1(new_n,&new_ir);
169:   if (n) {
170:     for (i=0; i<n; i++) {
171:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
172:     }
173:     PetscFree(ar);
174:     PetscFree(ir);
175:   }
176:   obj->real_idmax       = new_n;
177:   obj->realcomposeddata = new_ar; obj->realcomposedstate = new_ir;
178:   return(0);
179: }

183: PetscErrorCode  PetscObjectComposedDataIncreaseRealstar(PetscObject obj)
184: {
185:   PetscReal        **ar = obj->realstarcomposeddata,**new_ar;
186:   PetscObjectState *ir  = obj->realstarcomposedstate,*new_ir;
187:   PetscInt         n    = obj->realstar_idmax,new_n,i;
188:   PetscErrorCode   ierr;

191:   new_n = PetscObjectComposedDataMax;
192:   PetscCalloc1(new_n,&new_ar);
193:   PetscCalloc1(new_n,&new_ir);
194:   if (n) {
195:     for (i=0; i<n; i++) {
196:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
197:     }
198:     PetscFree(ar);
199:     PetscFree(ir);
200:   }
201:   obj->realstar_idmax       = new_n;
202:   obj->realstarcomposeddata = new_ar; obj->realstarcomposedstate = new_ir;
203:   return(0);
204: }

208: PetscErrorCode  PetscObjectComposedDataIncreaseScalar(PetscObject obj)
209: {
210:   PetscScalar      *ar = obj->scalarcomposeddata,*new_ar;
211:   PetscObjectState *ir = obj->scalarcomposedstate,*new_ir;
212:   PetscInt         n   = obj->scalar_idmax,new_n,i;
213:   PetscErrorCode   ierr;

216:   new_n = PetscObjectComposedDataMax;
217:   PetscCalloc1(new_n,&new_ar);
218:   PetscCalloc1(new_n,&new_ir);
219:   if (n) {
220:     for (i=0; i<n; i++) {
221:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
222:     }
223:     PetscFree(ar);
224:     PetscFree(ir);
225:   }
226:   obj->scalar_idmax       = new_n;
227:   obj->scalarcomposeddata = new_ar; obj->scalarcomposedstate = new_ir;
228:   return(0);
229: }

233: PetscErrorCode  PetscObjectComposedDataIncreaseScalarstar(PetscObject obj)
234: {
235:   PetscScalar      **ar = obj->scalarstarcomposeddata,**new_ar;
236:   PetscObjectState *ir  = obj->scalarstarcomposedstate,*new_ir;
237:   PetscInt         n    = obj->scalarstar_idmax,new_n,i;
238:   PetscErrorCode   ierr;

241:   new_n = PetscObjectComposedDataMax;
242:   PetscCalloc1(new_n,&new_ar);
243:   PetscCalloc1(new_n,&new_ir);
244:   if (n) {
245:     for (i=0; i<n; i++) {
246:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
247:     }
248:     PetscFree(ar);
249:     PetscFree(ir);
250:   }
251:   obj->scalarstar_idmax       = new_n;
252:   obj->scalarstarcomposeddata = new_ar; obj->scalarstarcomposedstate = new_ir;
253:   return(0);
254: }

258: /*@
259:    PetscObjectGetId - get unique object ID

261:    Not Collective

263:    Input Arguments:
264: .  obj - object

266:    Output Arguments:
267: .  id - integer ID

269:    Level: developer

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

274: .seealso: PetscObjectStateGet()
275: @*/
276: PetscErrorCode PetscObjectGetId(PetscObject obj,PetscObjectId *id)
277: {

280:   *id = obj->id;
281:   return(0);
282: }