Date: 2022-03-31T04:46:07+00:00

Actual source code: state.c

      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: {
     36:   *state = obj->state;
     37:   return 0;
     38: }

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

     44:    Logically Collective

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

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

     58:    Level: advanced

     60:    seealso: PetscObjectStateGet(),PetscObjectStateIncrease()

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

     70: PetscInt PetscObjectComposedDataMax = 10;

     72: /*@C
     73:    PetscObjectComposedDataRegister - Get an available id for composed data

     75:    Not Collective

     77:    Output parameter:
     78: .  id - an identifier under which data can be stored

     80:    Level: developer

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

     86:    seealso: PetscObjectComposedDataSetInt()

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

     93:   *id = globalcurrentstate++;
     94:   if (globalcurrentstate > PetscObjectComposedDataMax) PetscObjectComposedDataMax += 10;
     95:   return 0;
     96: }

     98: PetscErrorCode PetscObjectComposedDataIncreaseInt(PetscObject obj)
     99: {
    100:   PetscInt         *ar = obj->intcomposeddata,*new_ar,n = obj->int_idmax,new_n;
    101:   PetscObjectState *ir = obj->intcomposedstate,*new_ir;

    103:   new_n = PetscObjectComposedDataMax;
    104:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
    105:   PetscMemcpy(new_ar,ar,n*sizeof(PetscInt));
    106:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
    107:   PetscFree2(ar,ir);
    108:   obj->int_idmax       = new_n;
    109:   obj->intcomposeddata = new_ar; obj->intcomposedstate = new_ir;
    110:   return 0;
    111: }

    113: PetscErrorCode PetscObjectComposedDataIncreaseIntstar(PetscObject obj)
    114: {
    115:   PetscInt         **ar = obj->intstarcomposeddata,**new_ar,n = obj->intstar_idmax,new_n;
    116:   PetscObjectState *ir  = obj->intstarcomposedstate,*new_ir;

    118:   new_n = PetscObjectComposedDataMax;
    119:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
    120:   PetscMemcpy(new_ar,ar,n*sizeof(PetscInt*));
    121:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
    122:   PetscFree2(ar,ir);
    123:   obj->intstar_idmax        = new_n;
    124:   obj->intstarcomposeddata  = new_ar;
    125:   obj->intstarcomposedstate = new_ir;
    126:   return 0;
    127: }

    129: PetscErrorCode PetscObjectComposedDataIncreaseReal(PetscObject obj)
    130: {
    131:   PetscReal        *ar = obj->realcomposeddata,*new_ar;
    132:   PetscObjectState *ir = obj->realcomposedstate,*new_ir;
    133:   PetscInt         n   = obj->real_idmax,new_n;

    135:   new_n = PetscObjectComposedDataMax;
    136:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
    137:   PetscMemcpy(new_ar,ar,n*sizeof(PetscReal));
    138:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
    139:   PetscFree2(ar,ir);
    140:   obj->real_idmax       = new_n;
    141:   obj->realcomposeddata = new_ar; obj->realcomposedstate = new_ir;
    142:   return 0;
    143: }

    145: PetscErrorCode PetscObjectComposedDataIncreaseRealstar(PetscObject obj)
    146: {
    147:   PetscReal        **ar = obj->realstarcomposeddata,**new_ar;
    148:   PetscObjectState *ir  = obj->realstarcomposedstate,*new_ir;
    149:   PetscInt         n    = obj->realstar_idmax,new_n;

    151:   new_n = PetscObjectComposedDataMax;
    152:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
    153:   PetscMemcpy(new_ar,ar,n*sizeof(PetscReal*));
    154:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
    155:   PetscFree2(ar,ir);
    156:   obj->realstar_idmax       = new_n;
    157:   obj->realstarcomposeddata = new_ar; obj->realstarcomposedstate = new_ir;
    158:   return 0;
    159: }

    161: PetscErrorCode PetscObjectComposedDataIncreaseScalar(PetscObject obj)
    162: {
    163:   PetscScalar      *ar = obj->scalarcomposeddata,*new_ar;
    164:   PetscObjectState *ir = obj->scalarcomposedstate,*new_ir;
    165:   PetscInt         n   = obj->scalar_idmax,new_n;

    167:   new_n = PetscObjectComposedDataMax;
    168:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
    169:   PetscMemcpy(new_ar,ar,n*sizeof(PetscScalar));
    170:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
    171:   PetscFree2(ar,ir);
    172:   obj->scalar_idmax       = new_n;
    173:   obj->scalarcomposeddata = new_ar; obj->scalarcomposedstate = new_ir;
    174:   return 0;
    175: }

    177: PetscErrorCode PetscObjectComposedDataIncreaseScalarstar(PetscObject obj)
    178: {
    179:   PetscScalar      **ar = obj->scalarstarcomposeddata,**new_ar;
    180:   PetscObjectState *ir  = obj->scalarstarcomposedstate,*new_ir;
    181:   PetscInt         n    = obj->scalarstar_idmax,new_n;

    183:   new_n = PetscObjectComposedDataMax;
    184:   PetscCalloc2(new_n,&new_ar,new_n,&new_ir);
    185:   PetscMemcpy(new_ar,ar,n*sizeof(PetscScalar*));
    186:   PetscMemcpy(new_ir,ir,n*sizeof(PetscObjectState));
    187:   PetscFree2(ar,ir);
    188:   obj->scalarstar_idmax       = new_n;
    189:   obj->scalarstarcomposeddata = new_ar; obj->scalarstarcomposedstate = new_ir;
    190:   return 0;
    191: }

    193: /*@
    194:    PetscObjectGetId - get unique object ID

    196:    Not Collective

    198:    Input Parameter:
    199: .  obj - object

    201:    Output Parameter:
    202: .  id - integer ID

    204:    Level: developer

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

    209: .seealso: PetscObjectStateGet(), PetscObjectCompareId()
    210: @*/
    211: PetscErrorCode PetscObjectGetId(PetscObject obj,PetscObjectId *id)
    212: {
    213:   *id = obj->id;
    214:   return 0;
    215: }

    217: /*@
    218:    PetscObjectCompareId - compares the objects ID with a given id

    220:    Not Collective

    222:    Input Parameters:
    223: +  obj - object
    224: -  id - integer ID

    226:    Output Parameter;
    227: .  eq - the ids are equal

    229:    Level: developer

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

    234: .seealso: PetscObjectStateGet(), PetscObjectGetId()
    235: @*/
    236: PetscErrorCode PetscObjectCompareId(PetscObject obj,PetscObjectId id,PetscBool *eq)
    237: {
    238:   *eq = (id == obj->id) ? PETSC_TRUE : PETSC_FALSE;
    239:   return 0;
    240: }