Actual source code: state.c

petsc-3.8.4 2018-03-24
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: object state is an integer which gets increased every time
 22:    the object is changed. By saving and later querying the object state
 23:    one can determine whether information about the object is still current.
 24:    Currently, state is maintained for Vec and Mat objects.

 26:    Level: advanced

 28:    seealso: PetscObjectStateIncrease(), PetscObjectStateSet()

 30:    Concepts: state

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

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

 46:    Logically Collective

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

 54:    Notes: 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:    Concepts: state

 65: @*/
 66: PetscErrorCode PetscObjectStateSet(PetscObject obj,PetscObjectState state)
 67: {
 70:   obj->state = state;
 71:   return(0);
 72: }

 74: PetscInt PetscObjectComposedDataMax = 10;

 76: /*@C
 77:    PetscObjectComposedDataRegister - Get an available id for composed data

 79:    Not Collective

 81:    Output parameter:
 82: .  id - an identifier under which data can be stored

 84:    Level: developer

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

 89:    seealso: PetscObjectComposedDataSetInt()

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

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

102: PetscErrorCode  PetscObjectComposedDataIncreaseInt(PetscObject obj)
103: {
104:   PetscInt         *ar = obj->intcomposeddata,*new_ar,n = obj->int_idmax,new_n,i;
105:   PetscObjectState *ir = obj->intcomposedstate,*new_ir;
106:   PetscErrorCode   ierr;

109:   new_n = PetscObjectComposedDataMax;
110:   PetscCalloc1(new_n,&new_ar);
111:   PetscCalloc1(new_n,&new_ir);
112:   if (n) {
113:     for (i=0; i<n; i++) {
114:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
115:     }
116:     PetscFree(ar);
117:     PetscFree(ir);
118:   }
119:   obj->int_idmax       = new_n;
120:   obj->intcomposeddata = new_ar; obj->intcomposedstate = new_ir;
121:   return(0);
122: }

124: PetscErrorCode  PetscObjectComposedDataIncreaseIntstar(PetscObject obj)
125: {
126:   PetscInt         **ar = obj->intstarcomposeddata,**new_ar,n = obj->intstar_idmax,new_n,i;
127:   PetscObjectState *ir  = obj->intstarcomposedstate,*new_ir;
128:   PetscErrorCode   ierr;

131:   new_n = PetscObjectComposedDataMax;
132:   PetscCalloc1(new_n,&new_ar);
133:   PetscCalloc1(new_n,&new_ir);
134:   if (n) {
135:     for (i=0; i<n; i++) {
136:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
137:     }
138:     PetscFree(ar);
139:     PetscFree(ir);
140:   }
141:   obj->intstar_idmax       = new_n;
142:   obj->intstarcomposeddata = new_ar; obj->intstarcomposedstate = new_ir;
143:   return(0);
144: }

146: PetscErrorCode  PetscObjectComposedDataIncreaseReal(PetscObject obj)
147: {
148:   PetscReal        *ar = obj->realcomposeddata,*new_ar;
149:   PetscObjectState *ir = obj->realcomposedstate,*new_ir;
150:   PetscInt         n   = obj->real_idmax,new_n,i;
151:   PetscErrorCode   ierr;

154:   new_n = PetscObjectComposedDataMax;
155:   PetscCalloc1(new_n,&new_ar);
156:   PetscCalloc1(new_n,&new_ir);
157:   if (n) {
158:     for (i=0; i<n; i++) {
159:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
160:     }
161:     PetscFree(ar);
162:     PetscFree(ir);
163:   }
164:   obj->real_idmax       = new_n;
165:   obj->realcomposeddata = new_ar; obj->realcomposedstate = new_ir;
166:   return(0);
167: }

169: PetscErrorCode  PetscObjectComposedDataIncreaseRealstar(PetscObject obj)
170: {
171:   PetscReal        **ar = obj->realstarcomposeddata,**new_ar;
172:   PetscObjectState *ir  = obj->realstarcomposedstate,*new_ir;
173:   PetscInt         n    = obj->realstar_idmax,new_n,i;
174:   PetscErrorCode   ierr;

177:   new_n = PetscObjectComposedDataMax;
178:   PetscCalloc1(new_n,&new_ar);
179:   PetscCalloc1(new_n,&new_ir);
180:   if (n) {
181:     for (i=0; i<n; i++) {
182:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
183:     }
184:     PetscFree(ar);
185:     PetscFree(ir);
186:   }
187:   obj->realstar_idmax       = new_n;
188:   obj->realstarcomposeddata = new_ar; obj->realstarcomposedstate = new_ir;
189:   return(0);
190: }

192: PetscErrorCode  PetscObjectComposedDataIncreaseScalar(PetscObject obj)
193: {
194:   PetscScalar      *ar = obj->scalarcomposeddata,*new_ar;
195:   PetscObjectState *ir = obj->scalarcomposedstate,*new_ir;
196:   PetscInt         n   = obj->scalar_idmax,new_n,i;
197:   PetscErrorCode   ierr;

200:   new_n = PetscObjectComposedDataMax;
201:   PetscCalloc1(new_n,&new_ar);
202:   PetscCalloc1(new_n,&new_ir);
203:   if (n) {
204:     for (i=0; i<n; i++) {
205:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
206:     }
207:     PetscFree(ar);
208:     PetscFree(ir);
209:   }
210:   obj->scalar_idmax       = new_n;
211:   obj->scalarcomposeddata = new_ar; obj->scalarcomposedstate = new_ir;
212:   return(0);
213: }

215: PetscErrorCode  PetscObjectComposedDataIncreaseScalarstar(PetscObject obj)
216: {
217:   PetscScalar      **ar = obj->scalarstarcomposeddata,**new_ar;
218:   PetscObjectState *ir  = obj->scalarstarcomposedstate,*new_ir;
219:   PetscInt         n    = obj->scalarstar_idmax,new_n,i;
220:   PetscErrorCode   ierr;

223:   new_n = PetscObjectComposedDataMax;
224:   PetscCalloc1(new_n,&new_ar);
225:   PetscCalloc1(new_n,&new_ir);
226:   if (n) {
227:     for (i=0; i<n; i++) {
228:       new_ar[i] = ar[i]; new_ir[i] = ir[i];
229:     }
230:     PetscFree(ar);
231:     PetscFree(ir);
232:   }
233:   obj->scalarstar_idmax       = new_n;
234:   obj->scalarstarcomposeddata = new_ar; obj->scalarstarcomposedstate = new_ir;
235:   return(0);
236: }

238: /*@
239:    PetscObjectGetId - get unique object ID

241:    Not Collective

243:    Input Arguments:
244: .  obj - object

246:    Output Arguments:
247: .  id - integer ID

249:    Level: developer

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

254: .seealso: PetscObjectStateGet()
255: @*/
256: PetscErrorCode PetscObjectGetId(PetscObject obj,PetscObjectId *id)
257: {

260:   *id = obj->id;
261:   return(0);
262: }