xref: /petsc/src/sys/classes/viewer/interface/viewers.c (revision cd7e8a5e83fb5f23fbe440fa5d826b1569053b5a)
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 @*/
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 @*/
55 PetscErrorCode  PetscViewersCreate(MPI_Comm comm,PetscViewers *v)
56 {
57   PetscErrorCode ierr;
58 
59   PetscFunctionBegin;
60   PetscValidPointer(v,2);
61   ierr       = PetscNew(v);CHKERRQ(ierr);
62   (*v)->n    = 64;
63   (*v)->comm = comm;
64 
65   ierr = PetscCalloc1(64,&(*v)->viewer);CHKERRQ(ierr);
66   PetscFunctionReturn(0);
67 }
68 
69 /*@C
70    PetscViewersGetViewer - Gets a PetscViewer from a PetscViewer collection
71 
72    Not Collective, but PetscViewer will be collective object on PetscViewers
73 
74    Input Parameters:
75 +   viewers - object created with PetscViewersCreate()
76 -   n - number of PetscViewer you want
77 
78    Output Parameter:
79 .  viewer - the PetscViewer
80 
81    Level: intermediate
82 
83 .seealso: PetscViewersCreate(), PetscViewersDestroy()
84 
85 @*/
86 PetscErrorCode  PetscViewersGetViewer(PetscViewers viewers,PetscInt n,PetscViewer *viewer)
87 {
88   PetscErrorCode ierr;
89 
90   PetscFunctionBegin;
91   PetscValidPointer(viewers,1);
92   PetscValidPointer(viewer,3);
93   if (PetscUnlikely(n < 0)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot access using a negative index - %" PetscInt_FMT "\n",n);
94   if (n >= viewers->n) {
95     PetscViewer *v;
96     int         newn = n + 64; /* add 64 new ones at a time */
97 
98     ierr = PetscCalloc1(newn,&v);CHKERRQ(ierr);
99     ierr = PetscArraycpy(v,viewers->viewer,viewers->n);CHKERRQ(ierr);
100     ierr = PetscFree(viewers->viewer);CHKERRQ(ierr);
101 
102     viewers->viewer = v;
103   }
104   if (!viewers->viewer[n]) {
105     ierr = PetscViewerCreate(viewers->comm,&viewers->viewer[n]);CHKERRQ(ierr);
106   }
107   *viewer = viewers->viewer[n];
108   PetscFunctionReturn(0);
109 }
110 
111 /*
112   PetscMonitorCompare - Checks if two monitors are identical; if they are then it destroys the new one
113 
114   Not collective
115 
116   Input Parameters:
117 + nmon      - The new monitor
118 . nmctx     - The new monitor context, or NULL
119 . nmdestroy - The new monitor destroy function, or NULL
120 . mon       - The old monitor
121 . mctx      - The old monitor context, or NULL
122 - mdestroy  - The old monitor destroy function, or NULL
123 
124   Output Parameter:
125 . identical - PETSC_TRUE if the monitors are the same
126 
127   Level: developer
128 
129 .seealsp: DMMonitorSetFromOptions(), KSPMonitorSetFromOptions(), SNESMonitorSetFromOptions()
130 */
131 PetscErrorCode PetscMonitorCompare(PetscErrorCode (*nmon)(void), void *nmctx, PetscErrorCode (*nmdestroy)(void **), PetscErrorCode (*mon)(void), void *mctx, PetscErrorCode (*mdestroy)(void **), PetscBool *identical)
132 {
133   PetscFunctionBegin;
134   PetscValidBoolPointer(identical,7);
135   *identical = PETSC_FALSE;
136   if (nmon == mon && nmdestroy == mdestroy) {
137     if (nmctx == mctx) *identical = PETSC_TRUE;
138     else if (nmdestroy == (PetscErrorCode (*)(void**)) PetscViewerAndFormatDestroy) {
139       PetscViewerAndFormat *old = (PetscViewerAndFormat*)mctx, *newo = (PetscViewerAndFormat*)nmctx;
140       if (old->viewer == newo->viewer && old->format == newo->format) *identical = PETSC_TRUE;
141     }
142     if (*identical) {
143       if (mdestroy) {
144         PetscErrorCode ierr;
145         ierr = (*mdestroy)(&nmctx);CHKERRQ(ierr);
146       }
147     }
148   }
149   PetscFunctionReturn(0);
150 }
151