1 
2 #include <petscsys.h>
3 #include <petsc/private/viewerimpl.h>
4 
5 struct _n_PetscViewers {
6   MPI_Comm    comm;
7   PetscViewer *viewer;
8   int         n;
9 };
10 
11 /*@C
12    PetscViewersDestroy - Destroys a set of PetscViewers created with PetscViewersCreate().
13 
14    Collective on PetscViewers
15 
16    Input Parameters:
17 .  v - the PetscViewers to be destroyed.
18 
19    Level: intermediate
20 
21 .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewersCreate()
22 
23 @*/
PetscViewersDestroy(PetscViewers * v)24 PetscErrorCode  PetscViewersDestroy(PetscViewers *v)
25 {
26   int            i;
27   PetscErrorCode ierr;
28 
29   PetscFunctionBegin;
30   if (!*v) PetscFunctionReturn(0);
31   for (i=0; i<(*v)->n; i++) {
32     ierr = PetscViewerDestroy(&(*v)->viewer[i]);CHKERRQ(ierr);
33   }
34   ierr = PetscFree((*v)->viewer);CHKERRQ(ierr);
35   ierr = PetscFree(*v);CHKERRQ(ierr);
36   PetscFunctionReturn(0);
37 }
38 
39 /*@C
40    PetscViewersCreate - Creates a container to hold a set of PetscViewers.
41 
42    Collective
43 
44    Input Parameter:
45 .   comm - the MPI communicator
46 
47    Output Parameter:
48 .  v - the collection of PetscViewers
49 
50    Level: intermediate
51 
52 .seealso: PetscViewerCreate(), PetscViewersDestroy()
53 
54 @*/
PetscViewersCreate(MPI_Comm comm,PetscViewers * v)55 PetscErrorCode  PetscViewersCreate(MPI_Comm comm,PetscViewers *v)
56 {
57   PetscErrorCode ierr;
58 
59   PetscFunctionBegin;
60   ierr       = PetscNew(v);CHKERRQ(ierr);
61   (*v)->n    = 64;
62   (*v)->comm = comm;
63 
64   ierr = PetscCalloc1(64,&(*v)->viewer);CHKERRQ(ierr);
65   PetscFunctionReturn(0);
66 }
67 
68 /*@C
69    PetscViewersGetViewer - Gets a PetscViewer from a PetscViewer collection
70 
71    Not Collective, but PetscViewer will be collective object on PetscViewers
72 
73    Input Parameter:
74 +   viewers - object created with PetscViewersCreate()
75 -   n - number of PetscViewer you want
76 
77    Output Parameter:
78 .  viewer - the PetscViewer
79 
80    Level: intermediate
81 
82 .seealso: PetscViewersCreate(), PetscViewersDestroy()
83 
84 @*/
PetscViewersGetViewer(PetscViewers viewers,PetscInt n,PetscViewer * viewer)85 PetscErrorCode  PetscViewersGetViewer(PetscViewers viewers,PetscInt n,PetscViewer *viewer)
86 {
87   PetscErrorCode ierr;
88 
89   PetscFunctionBegin;
90   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot access using a negative index - %d\n",n);
91   if (n >= viewers->n) {
92     PetscViewer *v;
93     int         newn = n + 64; /* add 64 new ones at a time */
94 
95     ierr = PetscCalloc1(newn,&v);CHKERRQ(ierr);
96     ierr = PetscArraycpy(v,viewers->viewer,viewers->n);CHKERRQ(ierr);
97     ierr = PetscFree(viewers->viewer);CHKERRQ(ierr);
98 
99     viewers->viewer = v;
100   }
101   if (!viewers->viewer[n]) {
102     ierr = PetscViewerCreate(viewers->comm,&viewers->viewer[n]);CHKERRQ(ierr);
103   }
104   *viewer = viewers->viewer[n];
105   PetscFunctionReturn(0);
106 }
107 
108 /*
109   PetscMonitorCompare - Checks if two monitors are identical; if they are then it destroys the new one
110 
111   Not collective
112 
113   Input Parameters:
114 + nmon      - The new monitor
115 . nmctx     - The new monitor context, or NULL
116 . nmdestroy - The new monitor destroy function, or NULL
117 . mon       - The old monitor
118 . mctx      - The old monitor context, or NULL
119 - mdestroy  - The old monitor destroy function, or NULL
120 
121   Output Parameter:
122 . identical - PETSC_TRUE if the monitors are the same
123 
124   Level: developer
125 
126 .seealsp: DMMonitorSetFromOptions(), KSPMonitorSetFromOptions(), SNESMonitorSetFromOptions()
127 */
PetscMonitorCompare(PetscErrorCode (* nmon)(void),void * nmctx,PetscErrorCode (* nmdestroy)(void **),PetscErrorCode (* mon)(void),void * mctx,PetscErrorCode (* mdestroy)(void **),PetscBool * identical)128 PetscErrorCode PetscMonitorCompare(PetscErrorCode (*nmon)(void), void *nmctx, PetscErrorCode (*nmdestroy)(void **), PetscErrorCode (*mon)(void), void *mctx, PetscErrorCode (*mdestroy)(void **), PetscBool *identical)
129 {
130   *identical = PETSC_FALSE;
131   if (nmon == mon && nmdestroy == mdestroy) {
132     if (nmctx == mctx) *identical = PETSC_TRUE;
133     else if (nmdestroy == (PetscErrorCode (*)(void**)) PetscViewerAndFormatDestroy) {
134       PetscViewerAndFormat *old = (PetscViewerAndFormat*)mctx, *newo = (PetscViewerAndFormat*)nmctx;
135       if (old->viewer == newo->viewer && old->format == newo->format) *identical = PETSC_TRUE;
136     }
137     if (*identical) {
138       if (mdestroy) {
139         PetscErrorCode ierr;
140         ierr = (*mdestroy)(&nmctx);CHKERRQ(ierr);
141       }
142     }
143   }
144   PetscFunctionReturn(0);
145 }
146