Actual source code: viewers.c

  1: #include <petsc/private/viewerimpl.h>

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

  9: /*@
 10:   PetscViewersDestroy - Destroys a set of `PetscViewer`s created with `PetscViewersCreate()`.

 12:   Collective

 14:   Input Parameter:
 15: . v - the `PetscViewers` to be destroyed.

 17:   Level: intermediate

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

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

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

 36:   Collective

 38:   Input Parameter:
 39: . comm - the MPI communicator

 41:   Output Parameter:
 42: . v - the collection of `PetscViewers`

 44:   Level: intermediate

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

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

 60: /*@
 61:   PetscViewersGetViewer - Gets a `PetscViewer` from a `PetscViewers` collection

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

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

 69:   Output Parameter:
 70: . viewer - the `PetscViewer`

 72:   Level: intermediate

 74: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewers`, `PetscViewersCreate()`, `PetscViewersDestroy()`
 75: @*/
 76: PetscErrorCode PetscViewersGetViewer(PetscViewers viewers, PetscInt n, PetscViewer *viewer)
 77: {
 78:   PetscFunctionBegin;
 79:   PetscAssertPointer(viewers, 1);
 80:   PetscAssertPointer(viewer, 3);
 81:   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot access using a negative index - %" PetscInt_FMT, n);
 82:   if (n >= viewers->n) {
 83:     PetscViewer *v;

 85:     PetscCall(PetscCalloc1(n + 64, &v));
 86:     PetscCall(PetscArraycpy(v, viewers->viewer, viewers->n));
 87:     PetscCall(PetscFree(viewers->viewer));

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

 96: /*@
 97:   PetscMonitorCompare - Checks if two monitors are identical; if they are then it destroys the new one

 99:   Not Collective

101:   Input Parameters:
102: + nmon      - The new monitor
103: . nmctx     - The new monitor context, or `NULL`
104: . nmdestroy - The new monitor context destroy function, or `NULL`, see `PetscCtxDestroyFn` for its calling sequence
105: . mon       - The old monitor
106: . mctx      - The old monitor context, or `NULL`
107: - mdestroy  - The old monitor context destroy function, or `NULL`, see `PetscCtxDestroyFn` for its calling sequence

109:   Output Parameter:
110: . identical - `PETSC_TRUE` if the monitors are the same

112:   Level: developer

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