xref: /petsc/src/sys/classes/viewer/impls/vu/petscvu.c (revision 030f984af8d8bb4c203755d35bded3c05b3d83ce)
1 
2 #include <petsc/private/viewerimpl.h>  /*I     "petscsys.h"   I*/
3 
4 #define QUEUESTRINGSIZE 1024
5 
6 typedef struct _PrintfQueue *PrintfQueue;
7 struct _PrintfQueue {
8   char        string[QUEUESTRINGSIZE];
9   PrintfQueue next;
10 };
11 
12 typedef struct {
13   FILE          *fd;
14   PetscFileMode mode;     /* The mode in which to open the file */
15   char          *filename;
16   PetscBool     vecSeen;  /* The flag indicating whether any vector has been viewed so far */
17   PrintfQueue   queue, queueBase;
18   int           queueLength;
19 } PetscViewer_VU;
20 
21 static PetscErrorCode PetscViewerFileClose_VU(PetscViewer viewer)
22 {
23   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
24   PetscErrorCode ierr;
25 
26   PetscFunctionBegin;
27   if (vu->vecSeen) {
28     ierr = PetscViewerVUPrintDeferred(viewer, "};\n\n");CHKERRQ(ierr);
29   }
30   ierr   = PetscViewerVUFlushDeferred(viewer);CHKERRQ(ierr);
31   ierr   = PetscFClose(PetscObjectComm((PetscObject)viewer), vu->fd);CHKERRQ(ierr);
32   vu->fd = NULL;
33   ierr   = PetscFree(vu->filename);CHKERRQ(ierr);
34   PetscFunctionReturn(0);
35 }
36 
37 PetscErrorCode PetscViewerDestroy_VU(PetscViewer viewer)
38 {
39   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
40   PetscErrorCode ierr;
41 
42   PetscFunctionBegin;
43   ierr = PetscViewerFileClose_VU(viewer);CHKERRQ(ierr);
44   ierr = PetscFree(vu);CHKERRQ(ierr);
45   PetscFunctionReturn(0);
46 }
47 
48 PetscErrorCode PetscViewerFlush_VU(PetscViewer viewer)
49 {
50   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
51   PetscMPIInt    rank;
52   int            err;
53   PetscErrorCode ierr;
54 
55   PetscFunctionBegin;
56   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);CHKERRMPI(ierr);
57   if (!rank) {
58     err = fflush(vu->fd);
59     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
60   }
61   PetscFunctionReturn(0);
62 }
63 
64 static PetscErrorCode  PetscViewerFileSetMode_VU(PetscViewer viewer, PetscFileMode mode)
65 {
66   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
67 
68   PetscFunctionBegin;
69   vu->mode = mode;
70   PetscFunctionReturn(0);
71 }
72 
73 static PetscErrorCode  PetscViewerFileGetMode_VU(PetscViewer viewer, PetscFileMode *type)
74 {
75   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
76 
77   PetscFunctionBegin;
78   *type = vu->mode;
79   PetscFunctionReturn(0);
80 }
81 
82 static PetscErrorCode  PetscViewerFileGetName_VU(PetscViewer viewer, const char **name)
83 {
84   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
85 
86   PetscFunctionBegin;
87   *name = vu->filename;
88   PetscFunctionReturn(0);
89 }
90 
91 static PetscErrorCode  PetscViewerFileSetName_VU(PetscViewer viewer, const char name[])
92 {
93   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
94   char           fname[PETSC_MAX_PATH_LEN];
95   int            rank;
96   PetscErrorCode ierr;
97 
98   PetscFunctionBegin;
99   if (!name) PetscFunctionReturn(0);
100   ierr = PetscViewerFileClose_VU(viewer);CHKERRQ(ierr);
101   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);CHKERRMPI(ierr);
102   if (rank != 0) PetscFunctionReturn(0);
103   ierr = PetscStrallocpy(name, &vu->filename);CHKERRQ(ierr);
104   ierr = PetscFixFilename(name, fname);CHKERRQ(ierr);
105   switch (vu->mode) {
106   case FILE_MODE_READ:
107     vu->fd = fopen(fname, "r");
108     break;
109   case FILE_MODE_WRITE:
110     vu->fd = fopen(fname, "w");
111     break;
112   case FILE_MODE_APPEND:
113     vu->fd = fopen(fname, "a");
114     break;
115   case FILE_MODE_UPDATE:
116     vu->fd = fopen(fname, "r+");
117     if (!vu->fd) vu->fd = fopen(fname, "w+");
118     break;
119   case FILE_MODE_APPEND_UPDATE:
120     /* I really want a file which is opened at the end for updating,
121        not a+, which opens at the beginning, but makes writes at the end.
122     */
123     vu->fd = fopen(fname, "r+");
124     if (!vu->fd) vu->fd = fopen(fname, "w+");
125     else {
126       ierr = fseek(vu->fd, 0, SEEK_END);CHKERRQ(ierr);
127     }
128     break;
129   default:
130     SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP, "Unsupported file mode %s",PetscFileModes[vu->mode]);
131   }
132 
133   if (!vu->fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN, "Cannot open PetscViewer file: %s", fname);
134 #if defined(PETSC_USE_LOG)
135   PetscLogObjectState((PetscObject) viewer, "File: %s", name);
136 #endif
137   PetscFunctionReturn(0);
138 }
139 
140 PETSC_EXTERN PetscErrorCode PetscViewerCreate_VU(PetscViewer viewer)
141 {
142   PetscViewer_VU *vu;
143   PetscErrorCode ierr;
144 
145   PetscFunctionBegin;
146   ierr         = PetscNewLog(viewer,&vu);CHKERRQ(ierr);
147   viewer->data = (void*) vu;
148 
149   viewer->ops->destroy          = PetscViewerDestroy_VU;
150   viewer->ops->flush            = PetscViewerFlush_VU;
151   viewer->ops->getsubviewer     = NULL;
152   viewer->ops->restoresubviewer = NULL;
153 
154   vu->fd          = NULL;
155   vu->mode        = FILE_MODE_WRITE;
156   vu->filename    = NULL;
157   vu->vecSeen     = PETSC_FALSE;
158   vu->queue       = NULL;
159   vu->queueBase   = NULL;
160   vu->queueLength = 0;
161 
162   ierr = PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileSetName_C",PetscViewerFileSetName_VU);CHKERRQ(ierr);
163   ierr = PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileGetName_C",PetscViewerFileGetName_VU);CHKERRQ(ierr);
164   ierr = PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_VU);CHKERRQ(ierr);
165   ierr = PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_VU);CHKERRQ(ierr);
166   PetscFunctionReturn(0);
167 }
168 
169 /*@C
170   PetscViewerVUGetPointer - Extracts the file pointer from a VU PetscViewer.
171 
172   Not Collective
173 
174   Input Parameter:
175 . viewer - The PetscViewer
176 
177   Output Parameter:
178 . fd     - The file pointer
179 
180   Level: intermediate
181 
182 .seealso: PetscViewerASCIIGetPointer()
183 @*/
184 PetscErrorCode  PetscViewerVUGetPointer(PetscViewer viewer, FILE **fd)
185 {
186   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
187 
188   PetscFunctionBegin;
189   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
190   PetscValidPointer(fd,2);
191   *fd = vu->fd;
192   PetscFunctionReturn(0);
193 }
194 
195 /*@C
196   PetscViewerVUSetVecSeen - Sets the flag which indicates whether we have viewed
197   a vector. This is usually called internally rather than by a user.
198 
199   Not Collective
200 
201   Input Parameters:
202 + viewer  - The PetscViewer
203 - vecSeen - The flag which indicates whether we have viewed a vector
204 
205   Level: advanced
206 
207 .seealso: PetscViewerVUGetVecSeen()
208 @*/
209 PetscErrorCode  PetscViewerVUSetVecSeen(PetscViewer viewer, PetscBool vecSeen)
210 {
211   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
212 
213   PetscFunctionBegin;
214   vu->vecSeen = vecSeen;
215   PetscFunctionReturn(0);
216 }
217 
218 /*@C
219   PetscViewerVUGetVecSeen - Gets the flag which indicates whether we have viewed
220   a vector. This is usually called internally rather than by a user.
221 
222   Not Collective
223 
224   Input Parameter:
225 . viewer  - The PetscViewer
226 
227   Output Parameter:
228 . vecSeen - The flag which indicates whether we have viewed a vector
229 
230   Level: advanced
231 
232 .seealso: PetscViewerVUGetVecSeen()
233 @*/
234 PetscErrorCode  PetscViewerVUGetVecSeen(PetscViewer viewer, PetscBool  *vecSeen)
235 {
236   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
237 
238   PetscFunctionBegin;
239   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
240   PetscValidPointer(vecSeen,2);
241   *vecSeen = vu->vecSeen;
242   PetscFunctionReturn(0);
243 }
244 
245 /*@C
246   PetscViewerVUPrintDeferred - Prints to the deferred write cache instead of the file.
247 
248   Not Collective
249 
250   Input Parameters:
251 + viewer - The PetscViewer
252 - format - The format string
253 
254   Level: intermediate
255 
256 .seealso: PetscViewerVUFlushDeferred()
257 @*/
258 PetscErrorCode  PetscViewerVUPrintDeferred(PetscViewer viewer, const char format[], ...)
259 {
260   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
261   va_list        Argp;
262   size_t         fullLength;
263   PrintfQueue    next;
264   PetscErrorCode ierr;
265 
266   PetscFunctionBegin;
267   ierr = PetscNew(&next);CHKERRQ(ierr);
268   if (vu->queue) {
269     vu->queue->next = next;
270     vu->queue       = next;
271     vu->queue->next = NULL;
272   } else {
273     vu->queueBase   = vu->queue = next;
274   }
275   vu->queueLength++;
276 
277   va_start(Argp, format);
278   ierr = PetscArrayzero(next->string,QUEUESTRINGSIZE);CHKERRQ(ierr);
279   ierr = PetscVSNPrintf(next->string, QUEUESTRINGSIZE,format,&fullLength, Argp);CHKERRQ(ierr);
280   va_end(Argp);
281   PetscFunctionReturn(0);
282 }
283 
284 /*@C
285   PetscViewerVUFlushDeferred - Flushes the deferred write cache to the file.
286 
287   Not Collective
288 
289   Input Parameter:
290 . viewer - The PetscViewer
291 
292   Level: intermediate
293 
294 .seealso: PetscViewerVUPrintDeferred()
295 @*/
296 PetscErrorCode  PetscViewerVUFlushDeferred(PetscViewer viewer)
297 {
298   PetscViewer_VU *vu  = (PetscViewer_VU*) viewer->data;
299   PrintfQueue    next = vu->queueBase;
300   PrintfQueue    previous;
301   int            i;
302   PetscErrorCode ierr;
303 
304   PetscFunctionBegin;
305   for (i = 0; i < vu->queueLength; i++) {
306     PetscFPrintf(PetscObjectComm((PetscObject)viewer), vu->fd, "%s", next->string);
307     previous = next;
308     next     = next->next;
309     ierr     = PetscFree(previous);CHKERRQ(ierr);
310   }
311   vu->queue       = NULL;
312   vu->queueLength = 0;
313   PetscFunctionReturn(0);
314 }
315