Actual source code: pams.c

      1: #include <petsc/private/petscimpl.h>
      2: #include <petscviewersaws.h>
      3: #include <petscsys.h>

      5: /*@
      6:   PetscObjectSAWsTakeAccess - Take access of the data fields that have been published to SAWs
      7:   by a `PetscObject` so their values may  be changed in the computation

      9:   Collective

     11:   Input Parameter:
     12: . obj - the `PetscObject` variable. This must be cast with a (`PetscObject`), for example, `PetscObjectSAWSTakeAccess`((`PetscObject`)mat);

     14:   Level: advanced

     16:   Developer Notes:
     17:   The naming should perhaps be changed to `PetscObjectSAWsGetAccess()` and
     18:   `PetscObjectSAWsRestoreAccess()`

     20: .seealso: `PetscObjectSetName()`, `PetscObjectSAWsViewOff()`, `PetscObjectSAWsGrantAccess()`
     21: @*/
     22: PetscErrorCode PetscObjectSAWsTakeAccess(PetscObject obj)
     23: {
     24:   if (obj->amsmem) {
     25:     /* cannot wrap with PetscPushStack() because that also deals with the locks */
     26:     SAWs_Lock();
     27:   }
     28:   return PETSC_SUCCESS;
     29: }

     31: /*@
     32:   PetscObjectSAWsGrantAccess - Grants access of the data fields that have been published to
     33:   SAWs called when the changes made during `PetscObjectSAWsTakeAccess()` are complete.

     35:   Collective

     37:   Input Parameter:
     38: . obj - the `PetscObject` variable. This must be cast with a (`PetscObject`), for example, `PetscObjectSAWSRestoreAccess`((`PetscObject`)mat);

     40:   Level: advanced

     42:   Notes:
     43:   This allows the webserve to change the published values.

     45: .seealso: `PetscObjectSetName()`, `PetscObjectSAWsViewOff()`, `PetscObjectSAWsTakeAccess()`
     46: @*/
     47: PetscErrorCode PetscObjectSAWsGrantAccess(PetscObject obj)
     48: {
     49:   if (obj->amsmem) {
     50:     /* cannot wrap with PetscPushStack() because that also deals with the locks */
     51:     SAWs_Unlock();
     52:   }
     53:   return PETSC_SUCCESS;
     54: }

     56: /*@
     57:   PetscSAWsBlock - Blocks on SAWs until a client (person using the web browser) unblocks it

     59:   Not Collective

     61:   Level: advanced

     63: .seealso: `PetscObjectSetName()`, `PetscObjectSAWsViewOff()`, `PetscObjectSAWsSetBlock()`, `PetscObjectSAWsBlock()`
     64: @*/
     65: PetscErrorCode PetscSAWsBlock(void)
     66: {
     67:   volatile PetscBool block = PETSC_TRUE;

     69:   PetscFunctionBegin;
     70:   PetscCallSAWs(SAWs_Register, ("__Block", (PetscBool *)&block, 1, SAWs_WRITE, SAWs_BOOLEAN));
     71:   SAWs_Lock();
     72:   while (block) {
     73:     SAWs_Unlock();
     74:     PetscCall(PetscInfo(NULL, "Blocking on SAWs\n"));
     75:     PetscCall(PetscSleep(.3));
     76:     SAWs_Lock();
     77:   }
     78:   SAWs_Unlock();
     79:   PetscCallSAWs(SAWs_Delete, ("__Block"));
     80:   PetscCall(PetscInfo(NULL, "Out of SAWs block\n"));
     81:   PetscFunctionReturn(PETSC_SUCCESS);
     82: }

     84: /*@
     85:   PetscObjectSAWsBlock - Blocks the object if `PetscObjectSAWsSetBlock()` has been called

     87:   Collective

     89:   Input Parameter:
     90: . obj - the PETSc variable

     92:   Level: advanced

     94: .seealso: `PetscObjectSetName()`, `PetscObjectSAWsViewOff()`, `PetscObjectSAWsSetBlock()`, `PetscSAWsBlock()`
     95: @*/
     96: PetscErrorCode PetscObjectSAWsBlock(PetscObject obj)
     97: {
     98:   PetscFunctionBegin;

    101:   if (!obj->amspublishblock || !obj->amsmem) PetscFunctionReturn(PETSC_SUCCESS);
    102:   PetscCall(PetscSAWsBlock());
    103:   PetscFunctionReturn(PETSC_SUCCESS);
    104: }

    106: /*@
    107:   PetscObjectSAWsSetBlock - Sets whether an object will block at `PetscObjectSAWsBlock()`

    109:   Collective

    111:   Input Parameters:
    112: + obj - the PETSc variable
    113: - flg - whether it should block

    115:   Level: advanced

    117: .seealso: `PetscObjectSetName()`, `PetscObjectSAWsViewOff()`, `PetscObjectSAWsBlock()`, `PetscSAWsBlock()`
    118: @*/
    119: PetscErrorCode PetscObjectSAWsSetBlock(PetscObject obj, PetscBool flg)
    120: {
    121:   PetscFunctionBegin;
    123:   obj->amspublishblock = flg;
    124:   PetscFunctionReturn(PETSC_SUCCESS);
    125: }

    127: /*@C
    128:   PetscObjectSAWsViewOff - Remove a `PetscObject`'s SAWs (Scientific Application Web server) directory so the object is no longer published

    130:   Logically Collective

    132:   Input Parameter:
    133: . obj - the `PetscObject` whose SAWs publication should be torn down

    135:   Level: developer

    137:   Notes:
    138:   No-op for `PetscViewer` objects, and for any object that was never published via SAWs.

    140:   Called from `PetscObjectDestroy()`; users normally do not need to call this directly.

    142: .seealso: `PetscObject`, `PetscObjectSAWsBlock()`, `PetscObjectSAWsTakeAccess()`, `PetscObjectSAWsGrantAccess()`
    143: @*/
    144: PetscErrorCode PetscObjectSAWsViewOff(PetscObject obj)
    145: {
    146:   char dir[PETSC_MAX_PATH_LEN];

    148:   PetscFunctionBegin;
    149:   if (obj->classid == PETSC_VIEWER_CLASSID) PetscFunctionReturn(PETSC_SUCCESS);
    150:   if (!obj->amsmem) PetscFunctionReturn(PETSC_SUCCESS);
    151:   PetscCall(PetscSNPrintf(dir, sizeof(dir), "/PETSc/Objects/%s", obj->name));
    152:   PetscCallSAWs(SAWs_Delete, (dir));
    153:   PetscFunctionReturn(PETSC_SUCCESS);
    154: }