Actual source code: petscvu.c
1: #include <petsc/private/viewerimpl.h>
3: #define QUEUESTRINGSIZE 1024
5: typedef struct _PrintfQueue *PrintfQueue;
6: struct _PrintfQueue {
7: char string[QUEUESTRINGSIZE];
8: PrintfQueue next;
9: };
11: typedef struct {
12: FILE *fd;
13: PetscFileMode mode; /* The mode in which to open the file */
14: char *filename;
15: PetscBool vecSeen; /* The flag indicating whether any vector has been viewed so far */
16: PrintfQueue queue, queueBase;
17: int queueLength;
18: } PetscViewer_VU;
20: static PetscErrorCode PetscViewerFileClose_VU(PetscViewer viewer)
21: {
22: PetscViewer_VU *vu = (PetscViewer_VU *)viewer->data;
24: PetscFunctionBegin;
25: if (vu->vecSeen) PetscCall(PetscViewerVUPrintDeferred(viewer, "};\n\n"));
26: PetscCall(PetscViewerVUFlushDeferred(viewer));
27: PetscCall(PetscFClose(PetscObjectComm((PetscObject)viewer), vu->fd));
28: vu->fd = NULL;
29: PetscCall(PetscFree(vu->filename));
30: PetscFunctionReturn(PETSC_SUCCESS);
31: }
33: static PetscErrorCode PetscViewerDestroy_VU(PetscViewer viewer)
34: {
35: PetscViewer_VU *vu = (PetscViewer_VU *)viewer->data;
37: PetscFunctionBegin;
38: PetscCall(PetscViewerFileClose_VU(viewer));
39: PetscCall(PetscFree(vu));
40: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL));
41: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL));
42: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL));
43: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL));
44: PetscFunctionReturn(PETSC_SUCCESS);
45: }
47: static PetscErrorCode PetscViewerFlush_VU(PetscViewer viewer)
48: {
49: PetscMPIInt rank;
51: PetscFunctionBegin;
52: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
53: if (rank == 0) PetscCall(PetscFFlush(((PetscViewer_VU *)viewer->data)->fd));
54: PetscFunctionReturn(PETSC_SUCCESS);
55: }
57: static PetscErrorCode PetscViewerFileSetMode_VU(PetscViewer viewer, PetscFileMode mode)
58: {
59: PetscViewer_VU *vu = (PetscViewer_VU *)viewer->data;
61: PetscFunctionBegin;
62: vu->mode = mode;
63: PetscFunctionReturn(PETSC_SUCCESS);
64: }
66: static PetscErrorCode PetscViewerFileGetMode_VU(PetscViewer viewer, PetscFileMode *type)
67: {
68: PetscViewer_VU *vu = (PetscViewer_VU *)viewer->data;
70: PetscFunctionBegin;
71: *type = vu->mode;
72: PetscFunctionReturn(PETSC_SUCCESS);
73: }
75: static PetscErrorCode PetscViewerFileGetName_VU(PetscViewer viewer, const char **name)
76: {
77: PetscViewer_VU *vu = (PetscViewer_VU *)viewer->data;
79: PetscFunctionBegin;
80: *name = vu->filename;
81: PetscFunctionReturn(PETSC_SUCCESS);
82: }
84: static PetscErrorCode PetscViewerFileSetName_VU(PetscViewer viewer, const char name[])
85: {
86: PetscViewer_VU *vu = (PetscViewer_VU *)viewer->data;
87: char fname[PETSC_MAX_PATH_LEN];
88: int rank;
90: PetscFunctionBegin;
91: if (!name) PetscFunctionReturn(PETSC_SUCCESS);
92: PetscCall(PetscViewerFileClose_VU(viewer));
93: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
94: if (rank != 0) PetscFunctionReturn(PETSC_SUCCESS);
95: PetscCall(PetscStrallocpy(name, &vu->filename));
96: PetscCall(PetscFixFilename(name, fname));
97: switch (vu->mode) {
98: case FILE_MODE_READ:
99: vu->fd = fopen(fname, "r");
100: break;
101: case FILE_MODE_WRITE:
102: vu->fd = fopen(fname, "w");
103: break;
104: case FILE_MODE_APPEND:
105: vu->fd = fopen(fname, "a");
106: break;
107: case FILE_MODE_UPDATE:
108: vu->fd = fopen(fname, "r+");
109: if (!vu->fd) vu->fd = fopen(fname, "w+");
110: break;
111: case FILE_MODE_APPEND_UPDATE:
112: /* I really want a file which is opened at the end for updating,
113: not a+, which opens at the beginning, but makes writes at the end.
114: */
115: vu->fd = fopen(fname, "r+");
116: if (!vu->fd) vu->fd = fopen(fname, "w+");
117: else {
118: int ret = fseek(vu->fd, 0, SEEK_END);
119: PetscCheck(!ret, PETSC_COMM_SELF, PETSC_ERR_LIB, "fseek() failed with error code %d", ret);
120: }
121: break;
122: default:
123: SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[vu->mode]);
124: }
126: PetscCheck(vu->fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open PetscViewer file: %s", fname);
127: PetscCall(PetscLogObjectState((PetscObject)viewer, "File: %s", name));
128: PetscFunctionReturn(PETSC_SUCCESS);
129: }
131: /*MC
132: PETSCVIEWERVU - A viewer that prints to a VU file
134: Level: beginner
136: .seealso: [](sec_viewers), `PetscViewerVUFlushDeferred()`, `PetscViewerVUGetPointer()`, `PetscViewerVUSetVecSeen()`, `PetscViewerVUGetVecSeen()`,
137: `PetscViewerVUPrintDeferred()`
138: M*/
139: PETSC_EXTERN PetscErrorCode PetscViewerCreate_VU(PetscViewer viewer)
140: {
141: PetscViewer_VU *vu;
143: PetscFunctionBegin;
144: PetscCall(PetscNew(&vu));
145: viewer->data = (void *)vu;
147: viewer->ops->destroy = PetscViewerDestroy_VU;
148: viewer->ops->flush = PetscViewerFlush_VU;
150: vu->fd = NULL;
151: vu->mode = FILE_MODE_WRITE;
152: vu->filename = NULL;
153: vu->vecSeen = PETSC_FALSE;
154: vu->queue = NULL;
155: vu->queueBase = NULL;
156: vu->queueLength = 0;
158: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", PetscViewerFileSetName_VU));
159: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", PetscViewerFileGetName_VU));
160: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_VU));
161: PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_VU));
162: PetscFunctionReturn(PETSC_SUCCESS);
163: }
165: /*@
166: PetscViewerVUGetPointer - Extracts the file pointer from a `PETSCVIEWERVU` `PetscViewer`.
168: Not Collective
170: Input Parameter:
171: . viewer - The `PetscViewer`
173: Output Parameter:
174: . fd - The file pointer
176: Level: intermediate
178: .seealso: [](sec_viewers), `PETSCVIEWERVU`, `PetscViewerASCIIGetPointer()`
179: @*/
180: PetscErrorCode PetscViewerVUGetPointer(PetscViewer viewer, FILE **fd)
181: {
182: PetscViewer_VU *vu = (PetscViewer_VU *)viewer->data;
184: PetscFunctionBegin;
186: PetscAssertPointer(fd, 2);
187: *fd = vu->fd;
188: PetscFunctionReturn(PETSC_SUCCESS);
189: }
191: /*@
192: PetscViewerVUSetVecSeen - Sets the flag which indicates whether we have viewed
193: a vector. This is usually called internally rather than by a user.
195: Not Collective
197: Input Parameters:
198: + viewer - The `PETSCVIEWERVU` `PetscViewer`
199: - vecSeen - The flag which indicates whether we have viewed a vector
201: Level: developer
203: .seealso: [](sec_viewers), `PETSCVIEWERVU`, `PetscViewerVUGetVecSeen()`
204: @*/
205: PetscErrorCode PetscViewerVUSetVecSeen(PetscViewer viewer, PetscBool vecSeen)
206: {
207: PetscViewer_VU *vu = (PetscViewer_VU *)viewer->data;
209: PetscFunctionBegin;
210: vu->vecSeen = vecSeen;
211: PetscFunctionReturn(PETSC_SUCCESS);
212: }
214: /*@
215: PetscViewerVUGetVecSeen - Gets the flag which indicates whether we have viewed
216: a vector. This is usually called internally rather than by a user.
218: Not Collective
220: Input Parameter:
221: . viewer - The `PETSCVIEWERVU` `PetscViewer`
223: Output Parameter:
224: . vecSeen - The flag which indicates whether we have viewed a vector
226: Level: advanced
228: .seealso: [](sec_viewers), `PETSCVIEWERVU`
229: @*/
230: PetscErrorCode PetscViewerVUGetVecSeen(PetscViewer viewer, PetscBool *vecSeen)
231: {
232: PetscViewer_VU *vu = (PetscViewer_VU *)viewer->data;
234: PetscFunctionBegin;
236: PetscAssertPointer(vecSeen, 2);
237: *vecSeen = vu->vecSeen;
238: PetscFunctionReturn(PETSC_SUCCESS);
239: }
241: /*@
242: PetscViewerVUPrintDeferred - Prints to the deferred write cache instead of the file.
244: Not Collective
246: Input Parameters:
247: + viewer - The `PETSCVIEWERVU` `PetscViewer`
248: - format - The format string
250: Level: intermediate
252: .seealso: [](sec_viewers), `PETSCVIEWERVU`, `PetscViewerVUFlushDeferred()`
253: @*/
254: PetscErrorCode PetscViewerVUPrintDeferred(PetscViewer viewer, const char format[], ...)
255: {
256: PetscViewer_VU *vu = (PetscViewer_VU *)viewer->data;
257: va_list Argp;
258: size_t fullLength;
259: PrintfQueue next;
261: PetscFunctionBegin;
262: PetscCall(PetscNew(&next));
263: if (vu->queue) {
264: vu->queue->next = next;
265: vu->queue = next;
266: vu->queue->next = NULL;
267: } else {
268: vu->queueBase = vu->queue = next;
269: }
270: vu->queueLength++;
272: va_start(Argp, format);
273: PetscCall(PetscArrayzero(next->string, QUEUESTRINGSIZE));
274: PetscCall(PetscVSNPrintf(next->string, QUEUESTRINGSIZE, format, &fullLength, Argp));
275: va_end(Argp);
276: PetscFunctionReturn(PETSC_SUCCESS);
277: }
279: /*@
280: PetscViewerVUFlushDeferred - Flushes the deferred write cache to the file.
282: Not Collective
284: Input Parameter:
285: . viewer - The `PETSCVIEWERVU` `PetscViewer`
287: Level: intermediate
289: .seealso: [](sec_viewers), `PETSCVIEWERVU`, `PetscViewerVUPrintDeferred()`
290: @*/
291: PetscErrorCode PetscViewerVUFlushDeferred(PetscViewer viewer)
292: {
293: PetscViewer_VU *vu = (PetscViewer_VU *)viewer->data;
294: PrintfQueue next = vu->queueBase;
295: PrintfQueue previous;
296: int i;
298: PetscFunctionBegin;
299: for (i = 0; i < vu->queueLength; i++) {
300: PetscCall(PetscFPrintf(PetscObjectComm((PetscObject)viewer), vu->fd, "%s", next->string));
301: previous = next;
302: next = next->next;
303: PetscCall(PetscFree(previous));
304: }
305: vu->queue = NULL;
306: vu->queueLength = 0;
307: PetscFunctionReturn(PETSC_SUCCESS);
308: }