Actual source code: state.c

petsc-3.4.4 2014-03-13
  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:    PetscObjectStateQuery - 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:          PetscObjectStateQuery((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(), PetscObjectSetState()

 32:    Concepts: state

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

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

 50:    Not 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:          PetscObjectSetState((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.

 63:    Level: advanced

 65:    seealso: PetscObjectStateQuery(),PetscObjectStateIncrease()

 67:    Concepts: state

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

 78: PetscInt PetscObjectComposedDataMax = 10;

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

 86:    Not Collective

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

 91:    Level: developer

 93:    seealso: PetscObjectComposedDataSetInt()

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

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

108: PetscErrorCode  PetscObjectComposedDataIncreaseInt(PetscObject obj)
109: {
110:   PetscInt       *ar = obj->intcomposeddata,*new_ar;
111:   PetscInt       *ir = obj->intcomposedstate,*new_ir,n = obj->int_idmax,new_n,i;

115:   new_n = PetscObjectComposedDataMax;
116:   PetscMalloc(new_n*sizeof(PetscInt),&new_ar);
117:   PetscMemzero(new_ar,new_n*sizeof(PetscInt));
118:   PetscMalloc(new_n*sizeof(PetscInt),&new_ir);
119:   PetscMemzero(new_ir,new_n*sizeof(PetscInt));
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;
137:   PetscInt       *ir  = obj->intstarcomposedstate,*new_ir,n = obj->intstar_idmax,new_n,i;

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

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

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

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

193:   new_n = PetscObjectComposedDataMax;
194:   PetscMalloc(new_n*sizeof(PetscReal*),&new_ar);
195:   PetscMemzero(new_ar,new_n*sizeof(PetscReal*));
196:   PetscMalloc(new_n*sizeof(PetscInt),&new_ir);
197:   PetscMemzero(new_ir,new_n*sizeof(PetscInt));
198:   if (n) {
199:     for (i=0; i<n; i++) {
200:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
201:     }
202:     PetscFree(ar);
203:     PetscFree(ir);
204:   }
205:   obj->realstar_idmax       = new_n;
206:   obj->realstarcomposeddata = new_ar; obj->realstarcomposedstate = new_ir;
207:   return(0);
208: }

212: PetscErrorCode  PetscObjectComposedDataIncreaseScalar(PetscObject obj)
213: {
214:   PetscScalar    *ar = obj->scalarcomposeddata,*new_ar;
215:   PetscInt       *ir = obj->scalarcomposedstate,*new_ir,n = obj->scalar_idmax,new_n,i;

219:   new_n = PetscObjectComposedDataMax;
220:   PetscMalloc(new_n*sizeof(PetscScalar),&new_ar);
221:   PetscMemzero(new_ar,new_n*sizeof(PetscScalar));
222:   PetscMalloc(new_n*sizeof(PetscInt),&new_ir);
223:   PetscMemzero(new_ir,new_n*sizeof(PetscInt));
224:   if (n) {
225:     for (i=0; i<n; i++) {
226:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
227:     }
228:     PetscFree(ar);
229:     PetscFree(ir);
230:   }
231:   obj->scalar_idmax       = new_n;
232:   obj->scalarcomposeddata = new_ar; obj->scalarcomposedstate = new_ir;
233:   return(0);
234: }

238: PetscErrorCode  PetscObjectComposedDataIncreaseScalarstar(PetscObject obj)
239: {
240:   PetscScalar    **ar = obj->scalarstarcomposeddata,**new_ar;
241:   PetscInt       *ir  = obj->scalarstarcomposedstate,*new_ir,n = obj->scalarstar_idmax,new_n,i;

245:   new_n = PetscObjectComposedDataMax;
246:   PetscMalloc(new_n*sizeof(PetscScalar*),&new_ar);
247:   PetscMemzero(new_ar,new_n*sizeof(PetscScalar*));
248:   PetscMalloc(new_n*sizeof(PetscInt),&new_ir);
249:   PetscMemzero(new_ir,new_n*sizeof(PetscInt));
250:   if (n) {
251:     for (i=0; i<n; i++) {
252:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
253:     }
254:     PetscFree(ar);
255:     PetscFree(ir);
256:   }
257:   obj->scalarstar_idmax       = new_n;
258:   obj->scalarstarcomposeddata = new_ar; obj->scalarstarcomposedstate = new_ir;
259:   return(0);
260: }