Actual source code: state.c
petsc-3.10.5 2019-03-28
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:    Concepts: state

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

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

     47:    Logically Collective

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

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

     61:    Level: advanced

     63:    seealso: PetscObjectStateGet(),PetscObjectStateIncrease()

     65:    Concepts: state

     67: @*/
     68: PetscErrorCode PetscObjectStateSet(PetscObject obj,PetscObjectState state)
     69: {
     72:   obj->state = state;
     73:   return(0);
     74: }

     76: PetscInt PetscObjectComposedDataMax = 10;

     78: /*@C
     79:    PetscObjectComposedDataRegister - Get an available id for composed data

     81:    Not Collective

     83:    Output parameter:
     84: .  id - an identifier under which data can be stored

     86:    Level: developer

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

     92:    seealso: PetscObjectComposedDataSetInt()

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    241: /*@
    242:    PetscObjectGetId - get unique object ID

    244:    Not Collective

    246:    Input Arguments:
    247: .  obj - object

    249:    Output Arguments:
    250: .  id - integer ID

    252:    Level: developer

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

    257: .seealso: PetscObjectStateGet()
    258: @*/
    259: PetscErrorCode PetscObjectGetId(PetscObject obj,PetscObjectId *id)
    260: {

    263:   *id = obj->id;
    264:   return(0);
    265: }