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 Parameters:
17: . v - the `PetscViewer`s to be destroyed.
19: Level: intermediate
21: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerCreate()`, `PetscViewerDrawOpen()`, `PetscViewersCreate()`
22: @*/
23: PetscErrorCode PetscViewersDestroy(PetscViewers *v)
24: {
25: int i;
27: if (!*v) return 0;
28: for (i = 0; i < (*v)->n; i++) PetscViewerDestroy(&(*v)->viewer[i]);
29: PetscFree((*v)->viewer);
30: PetscFree(*v);
31: return 0;
32: }
34: /*@C
35: PetscViewersCreate - Creates a container to hold a set of `PetscViewer`s. The container is essentially a sparse, growable in length array of `PetscViewer`s
37: Collective
39: Input Parameter:
40: . comm - the MPI communicator
42: Output Parameter:
43: . v - the collection of `PetscViewer`s
45: Level: intermediate
47: .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewersDestroy()`
48: @*/
49: PetscErrorCode PetscViewersCreate(MPI_Comm comm, PetscViewers *v)
50: {
52: PetscNew(v);
53: (*v)->n = 64;
54: (*v)->comm = comm;
56: PetscCalloc1(64, &(*v)->viewer);
57: return 0;
58: }
60: /*@C
61: PetscViewersGetViewer - Gets a `PetscViewer` from a `PetscViewer` collection
63: Not Collective, but the resulting `PetscViewer` will be collective object on viewers
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`, `PetscViewersCreate()`, `PetscViewersDestroy()`
75: @*/
76: PetscErrorCode PetscViewersGetViewer(PetscViewers viewers, PetscInt n, PetscViewer *viewer)
77: {
81: if (n >= viewers->n) {
82: PetscViewer *v;
83: int newn = n + 64; /* add 64 new ones at a time */
85: PetscCalloc1(newn, &v);
86: PetscArraycpy(v, viewers->viewer, viewers->n);
87: PetscFree(viewers->viewer);
89: viewers->viewer = v;
90: }
91: if (!viewers->viewer[n]) PetscViewerCreate(viewers->comm, &viewers->viewer[n]);
92: *viewer = viewers->viewer[n];
93: return 0;
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 destroy function, or NULL
105: . mon - The old monitor
106: . mctx - The old monitor context, or NULL
107: - mdestroy - The old monitor destroy function, or NULL
109: Output Parameter:
110: . identical - `PETSC_TRUE` if the monitors are the same
112: Level: developer
114: .seealso: [](sec_viewers), `DMMonitorSetFromOptions()`, `KSPMonitorSetFromOptions()`, `SNESMonitorSetFromOptions()`
115: */
116: PetscErrorCode PetscMonitorCompare(PetscErrorCode (*nmon)(void), void *nmctx, PetscErrorCode (*nmdestroy)(void **), PetscErrorCode (*mon)(void), void *mctx, PetscErrorCode (*mdestroy)(void **), PetscBool *identical)
117: {
119: *identical = PETSC_FALSE;
120: if (nmon == mon && nmdestroy == mdestroy) {
121: if (nmctx == mctx) *identical = PETSC_TRUE;
122: else if (nmdestroy == (PetscErrorCode(*)(void **))PetscViewerAndFormatDestroy) {
123: PetscViewerAndFormat *old = (PetscViewerAndFormat *)mctx, *newo = (PetscViewerAndFormat *)nmctx;
124: if (old->viewer == newo->viewer && old->format == newo->format) *identical = PETSC_TRUE;
125: }
126: if (*identical) {
127: if (mdestroy) (*mdestroy)(&nmctx);
128: }
129: }
130: return 0;
131: }