Actual source code: viewers.c


  2: #include <petscsys.h>
  3: #include <petsc/private/viewerimpl.h>

  5: struct _n_PetscViewers {
  6:   MPI_Comm     comm;
  7:   PetscViewer *viewer;
  8:   int          n;
  9: };

 11: /*@C
 12:    PetscViewersDestroy - Destroys a set of `PetscViewer`s created with `PetscViewersCreate()`.

 14:    Collective

 16:    Input Parameter:
 17: .  v - the `PetscViewers` to be destroyed.

 19:    Level: intermediate

 21: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerDestroy()`, `PetscViewers`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewersCreate()`
 22: @*/
 23: PetscErrorCode PetscViewersDestroy(PetscViewers *v)
 24: {
 25:   int i;

 27:   PetscFunctionBegin;
 28:   if (!*v) PetscFunctionReturn(PETSC_SUCCESS);
 29:   for (i = 0; i < (*v)->n; i++) PetscCall(PetscViewerDestroy(&(*v)->viewer[i]));
 30:   PetscCall(PetscFree((*v)->viewer));
 31:   PetscCall(PetscFree(*v));
 32:   PetscFunctionReturn(PETSC_SUCCESS);
 33: }

 35: /*@C
 36:    PetscViewersCreate - Creates a container to hold a set of `PetscViewer`'s. The container is essentially a sparse, growable in length array of `PetscViewer`s

 38:    Collective

 40:    Input Parameter:
 41: .   comm - the MPI communicator

 43:    Output Parameter:
 44: .  v - the collection of `PetscViewers`

 46:    Level: intermediate

 48: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewers`, `PetscViewerCreate()`, `PetscViewersDestroy()`
 49: @*/
 50: PetscErrorCode PetscViewersCreate(MPI_Comm comm, PetscViewers *v)
 51: {
 52:   PetscFunctionBegin;
 54:   PetscCall(PetscNew(v));
 55:   (*v)->n    = 64;
 56:   (*v)->comm = comm;

 58:   PetscCall(PetscCalloc1(64, &(*v)->viewer));
 59:   PetscFunctionReturn(PETSC_SUCCESS);
 60: }

 62: /*@C
 63:    PetscViewersGetViewer - Gets a `PetscViewer` from a `PetscViewers` collection

 65:    Collective if the viewer has not previously be obtained.

 67:    Input Parameters:
 68: +   viewers - object created with `PetscViewersCreate()`
 69: -   n - number of `PetscViewer` you want

 71:    Output Parameter:
 72: .  viewer - the `PetscViewer`

 74:    Level: intermediate

 76: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewers`, `PetscViewersCreate()`, `PetscViewersDestroy()`
 77: @*/
 78: PetscErrorCode PetscViewersGetViewer(PetscViewers viewers, PetscInt n, PetscViewer *viewer)
 79: {
 80:   PetscFunctionBegin;
 83:   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot access using a negative index - %" PetscInt_FMT, n);
 84:   if (n >= viewers->n) {
 85:     PetscViewer *v;
 86:     int          newn = n + 64; /* add 64 new ones at a time */

 88:     PetscCall(PetscCalloc1(newn, &v));
 89:     PetscCall(PetscArraycpy(v, viewers->viewer, viewers->n));
 90:     PetscCall(PetscFree(viewers->viewer));

 92:     viewers->viewer = v;
 93:   }
 94:   if (!viewers->viewer[n]) PetscCall(PetscViewerCreate(viewers->comm, &viewers->viewer[n]));
 95:   *viewer = viewers->viewer[n];
 96:   PetscFunctionReturn(PETSC_SUCCESS);
 97: }

 99: /*
100:   PetscMonitorCompare - Checks if two monitors are identical; if they are then it destroys the new one

102:   Not Collective

104:   Input Parameters:
105: + nmon      - The new monitor
106: . nmctx     - The new monitor context, or `NULL`
107: . nmdestroy - The new monitor destroy function, or `NULL`
108: . mon       - The old monitor
109: . mctx      - The old monitor context, or `NULL`
110: - mdestroy  - The old monitor destroy function, or `NULL`

112:   Output Parameter:
113: . identical - `PETSC_TRUE` if the monitors are the same

115:   Level: developer

117: .seealso: [](sec_viewers), `DMMonitorSetFromOptions()`, `KSPMonitorSetFromOptions()`, `SNESMonitorSetFromOptions()`
118: */
119: PetscErrorCode PetscMonitorCompare(PetscErrorCode (*nmon)(void), void *nmctx, PetscErrorCode (*nmdestroy)(void **), PetscErrorCode (*mon)(void), void *mctx, PetscErrorCode (*mdestroy)(void **), PetscBool *identical)
120: {
121:   PetscFunctionBegin;
123:   *identical = PETSC_FALSE;
124:   if (nmon == mon && nmdestroy == mdestroy) {
125:     if (nmctx == mctx) *identical = PETSC_TRUE;
126:     else if (nmdestroy == (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy) {
127:       PetscViewerAndFormat *old = (PetscViewerAndFormat *)mctx, *newo = (PetscViewerAndFormat *)nmctx;
128:       if (old->viewer == newo->viewer && old->format == newo->format) *identical = PETSC_TRUE;
129:     }
130:     if (*identical) {
131:       if (mdestroy) PetscCall((*mdestroy)(&nmctx));
132:     }
133:   }
134:   PetscFunctionReturn(PETSC_SUCCESS);
135: }