xref: /petsc/src/sys/classes/viewer/impls/vu/petscvu.c (revision a2b725a8db0d6bf6cc2a1c6df7dd8029aadfff6e)
15c6c1daeSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h>  /*I     "petscsys.h"   I*/
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith #define QUEUESTRINGSIZE 1024
55c6c1daeSBarry Smith 
65c6c1daeSBarry Smith typedef struct _PrintfQueue *PrintfQueue;
75c6c1daeSBarry Smith struct _PrintfQueue {
85c6c1daeSBarry Smith   char        string[QUEUESTRINGSIZE];
95c6c1daeSBarry Smith   PrintfQueue next;
105c6c1daeSBarry Smith };
115c6c1daeSBarry Smith 
125c6c1daeSBarry Smith typedef struct {
135c6c1daeSBarry Smith   FILE          *fd;
145c6c1daeSBarry Smith   PetscFileMode mode;     /* The mode in which to open the file */
155c6c1daeSBarry Smith   char          *filename;
165c6c1daeSBarry Smith   PetscBool     vecSeen;  /* The flag indicating whether any vector has been viewed so far */
175c6c1daeSBarry Smith   PrintfQueue   queue, queueBase;
185c6c1daeSBarry Smith   int           queueLength;
195c6c1daeSBarry Smith } PetscViewer_VU;
205c6c1daeSBarry Smith 
215c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_VU(PetscViewer viewer)
225c6c1daeSBarry Smith {
235c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
245c6c1daeSBarry Smith   PetscErrorCode ierr;
255c6c1daeSBarry Smith 
265c6c1daeSBarry Smith   PetscFunctionBegin;
275c6c1daeSBarry Smith   if (vu->vecSeen) {
285c6c1daeSBarry Smith     ierr = PetscViewerVUPrintDeferred(viewer, "};\n\n");CHKERRQ(ierr);
295c6c1daeSBarry Smith   }
305c6c1daeSBarry Smith   ierr   = PetscViewerVUFlushDeferred(viewer);CHKERRQ(ierr);
31ce94432eSBarry Smith   ierr   = PetscFClose(PetscObjectComm((PetscObject)viewer), vu->fd);CHKERRQ(ierr);
320298fd71SBarry Smith   vu->fd = NULL;
335c6c1daeSBarry Smith   ierr   = PetscFree(vu->filename);CHKERRQ(ierr);
345c6c1daeSBarry Smith   PetscFunctionReturn(0);
355c6c1daeSBarry Smith }
365c6c1daeSBarry Smith 
375c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_VU(PetscViewer viewer)
385c6c1daeSBarry Smith {
395c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
405c6c1daeSBarry Smith   PetscErrorCode ierr;
415c6c1daeSBarry Smith 
425c6c1daeSBarry Smith   PetscFunctionBegin;
435c6c1daeSBarry Smith   ierr = PetscViewerFileClose_VU(viewer);CHKERRQ(ierr);
445c6c1daeSBarry Smith   ierr = PetscFree(vu);CHKERRQ(ierr);
455c6c1daeSBarry Smith   PetscFunctionReturn(0);
465c6c1daeSBarry Smith }
475c6c1daeSBarry Smith 
485c6c1daeSBarry Smith PetscErrorCode PetscViewerFlush_VU(PetscViewer viewer)
495c6c1daeSBarry Smith {
505c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
515c6c1daeSBarry Smith   PetscMPIInt    rank;
525c6c1daeSBarry Smith   int            err;
535c6c1daeSBarry Smith   PetscErrorCode ierr;
545c6c1daeSBarry Smith 
555c6c1daeSBarry Smith   PetscFunctionBegin;
56ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);CHKERRQ(ierr);
575c6c1daeSBarry Smith   if (!rank) {
585c6c1daeSBarry Smith     err = fflush(vu->fd);
595c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
605c6c1daeSBarry Smith   }
615c6c1daeSBarry Smith   PetscFunctionReturn(0);
625c6c1daeSBarry Smith }
635c6c1daeSBarry Smith 
645c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetName_VU(PetscViewer viewer, const char **name)
655c6c1daeSBarry Smith {
665c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
675c6c1daeSBarry Smith 
685c6c1daeSBarry Smith   PetscFunctionBegin;
695c6c1daeSBarry Smith   *name = vu->filename;
705c6c1daeSBarry Smith   PetscFunctionReturn(0);
715c6c1daeSBarry Smith }
725c6c1daeSBarry Smith 
735c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName_VU(PetscViewer viewer, const char name[])
745c6c1daeSBarry Smith {
755c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
765c6c1daeSBarry Smith   char           fname[PETSC_MAX_PATH_LEN];
775c6c1daeSBarry Smith   int            rank;
785c6c1daeSBarry Smith   PetscErrorCode ierr;
795c6c1daeSBarry Smith 
805c6c1daeSBarry Smith   PetscFunctionBegin;
815c6c1daeSBarry Smith   if (!name) PetscFunctionReturn(0);
825c6c1daeSBarry Smith   ierr = PetscViewerFileClose_VU(viewer);CHKERRQ(ierr);
83ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);CHKERRQ(ierr);
845c6c1daeSBarry Smith   if (rank != 0) PetscFunctionReturn(0);
855c6c1daeSBarry Smith   ierr = PetscStrallocpy(name, &vu->filename);CHKERRQ(ierr);
865c6c1daeSBarry Smith   ierr = PetscFixFilename(name, fname);CHKERRQ(ierr);
875c6c1daeSBarry Smith   switch (vu->mode) {
885c6c1daeSBarry Smith   case FILE_MODE_READ:
895c6c1daeSBarry Smith     vu->fd = fopen(fname, "r");
905c6c1daeSBarry Smith     break;
915c6c1daeSBarry Smith   case FILE_MODE_WRITE:
925c6c1daeSBarry Smith     vu->fd = fopen(fname, "w");
935c6c1daeSBarry Smith     break;
945c6c1daeSBarry Smith   case FILE_MODE_APPEND:
955c6c1daeSBarry Smith     vu->fd = fopen(fname, "a");
965c6c1daeSBarry Smith     break;
975c6c1daeSBarry Smith   case FILE_MODE_UPDATE:
985c6c1daeSBarry Smith     vu->fd = fopen(fname, "r+");
99a297a907SKarl Rupp     if (!vu->fd) vu->fd = fopen(fname, "w+");
1005c6c1daeSBarry Smith     break;
1015c6c1daeSBarry Smith   case FILE_MODE_APPEND_UPDATE:
1025c6c1daeSBarry Smith     /* I really want a file which is opened at the end for updating,
1035c6c1daeSBarry Smith        not a+, which opens at the beginning, but makes writes at the end.
1045c6c1daeSBarry Smith     */
1055c6c1daeSBarry Smith     vu->fd = fopen(fname, "r+");
106a297a907SKarl Rupp     if (!vu->fd) vu->fd = fopen(fname, "w+");
107a297a907SKarl Rupp     else {
1085c6c1daeSBarry Smith       ierr = fseek(vu->fd, 0, SEEK_END);CHKERRQ(ierr);
1095c6c1daeSBarry Smith     }
1105c6c1daeSBarry Smith     break;
1115c6c1daeSBarry Smith   default:
1125c6c1daeSBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid file mode %d", vu->mode);
1135c6c1daeSBarry Smith   }
1145c6c1daeSBarry Smith 
1155c6c1daeSBarry Smith   if (!vu->fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN, "Cannot open PetscViewer file: %s", fname);
1165c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
1175c6c1daeSBarry Smith   PetscLogObjectState((PetscObject) viewer, "File: %s", name);
1185c6c1daeSBarry Smith #endif
1195c6c1daeSBarry Smith   PetscFunctionReturn(0);
1205c6c1daeSBarry Smith }
1215c6c1daeSBarry Smith 
1228cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_VU(PetscViewer viewer)
1235c6c1daeSBarry Smith {
1245c6c1daeSBarry Smith   PetscViewer_VU *vu;
1255c6c1daeSBarry Smith   PetscErrorCode ierr;
1265c6c1daeSBarry Smith 
1275c6c1daeSBarry Smith   PetscFunctionBegin;
128b00a9115SJed Brown   ierr         = PetscNewLog(viewer,&vu);CHKERRQ(ierr);
1295c6c1daeSBarry Smith   viewer->data = (void*) vu;
1305c6c1daeSBarry Smith 
1315c6c1daeSBarry Smith   viewer->ops->destroy          = PetscViewerDestroy_VU;
1325c6c1daeSBarry Smith   viewer->ops->flush            = PetscViewerFlush_VU;
133559f443fSBarry Smith   viewer->ops->getsubviewer     = NULL;
134559f443fSBarry Smith   viewer->ops->restoresubviewer = NULL;
1355c6c1daeSBarry Smith 
1360298fd71SBarry Smith   vu->fd          = NULL;
1375c6c1daeSBarry Smith   vu->mode        = FILE_MODE_WRITE;
1380298fd71SBarry Smith   vu->filename    = NULL;
1395c6c1daeSBarry Smith   vu->vecSeen     = PETSC_FALSE;
1400298fd71SBarry Smith   vu->queue       = NULL;
1410298fd71SBarry Smith   vu->queueBase   = NULL;
1425c6c1daeSBarry Smith   vu->queueLength = 0;
1435c6c1daeSBarry Smith 
144bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileSetName_C",PetscViewerFileSetName_VU);CHKERRQ(ierr);
145bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileGetName_C",PetscViewerFileGetName_VU);CHKERRQ(ierr);
1465c6c1daeSBarry Smith   PetscFunctionReturn(0);
1475c6c1daeSBarry Smith }
1485c6c1daeSBarry Smith 
1495c6c1daeSBarry Smith /*@C
1505c6c1daeSBarry Smith   PetscViewerVUGetPointer - Extracts the file pointer from a VU PetscViewer.
1515c6c1daeSBarry Smith 
1525c6c1daeSBarry Smith   Not Collective
1535c6c1daeSBarry Smith 
1545c6c1daeSBarry Smith   Input Parameter:
1555c6c1daeSBarry Smith . viewer - The PetscViewer
1565c6c1daeSBarry Smith 
1575c6c1daeSBarry Smith   Output Parameter:
1585c6c1daeSBarry Smith . fd     - The file pointer
1595c6c1daeSBarry Smith 
1605c6c1daeSBarry Smith   Level: intermediate
1615c6c1daeSBarry Smith 
1625c6c1daeSBarry Smith   Concepts: PetscViewer^file pointer
1635c6c1daeSBarry Smith   Concepts: file pointer^getting from PetscViewer
1645c6c1daeSBarry Smith 
1655c6c1daeSBarry Smith .seealso: PetscViewerASCIIGetPointer()
1665c6c1daeSBarry Smith @*/
1675c6c1daeSBarry Smith PetscErrorCode  PetscViewerVUGetPointer(PetscViewer viewer, FILE **fd)
1685c6c1daeSBarry Smith {
1695c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith   PetscFunctionBegin;
1725c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1735c6c1daeSBarry Smith   PetscValidPointer(fd,2);
1745c6c1daeSBarry Smith   *fd = vu->fd;
1755c6c1daeSBarry Smith   PetscFunctionReturn(0);
1765c6c1daeSBarry Smith }
1775c6c1daeSBarry Smith 
1785c6c1daeSBarry Smith /*@C
1795c6c1daeSBarry Smith   PetscViewerVUSetMode - Sets the mode in which to open the file.
1805c6c1daeSBarry Smith 
1815c6c1daeSBarry Smith   Not Collective
1825c6c1daeSBarry Smith 
1835c6c1daeSBarry Smith   Input Parameters:
1845c6c1daeSBarry Smith + viewer - The PetscViewer
1855c6c1daeSBarry Smith - mode   - The file mode
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith   Level: intermediate
1885c6c1daeSBarry Smith 
1895c6c1daeSBarry Smith .keywords: Viewer, file, get, pointer
1905c6c1daeSBarry Smith .seealso: PetscViewerASCIISetMode()
1915c6c1daeSBarry Smith @*/
1925c6c1daeSBarry Smith PetscErrorCode  PetscViewerVUSetMode(PetscViewer viewer, PetscFileMode mode)
1935c6c1daeSBarry Smith {
1945c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
1955c6c1daeSBarry Smith 
1965c6c1daeSBarry Smith   PetscFunctionBegin;
1975c6c1daeSBarry Smith   vu->mode = mode;
1985c6c1daeSBarry Smith   PetscFunctionReturn(0);
1995c6c1daeSBarry Smith }
2005c6c1daeSBarry Smith 
2015c6c1daeSBarry Smith /*@C
2025c6c1daeSBarry Smith   PetscViewerVUSetVecSeen - Sets the flag which indicates whether we have viewed
2035c6c1daeSBarry Smith   a vector. This is usually called internally rather than by a user.
2045c6c1daeSBarry Smith 
2055c6c1daeSBarry Smith   Not Collective
2065c6c1daeSBarry Smith 
2075c6c1daeSBarry Smith   Input Parameters:
2085c6c1daeSBarry Smith + viewer  - The PetscViewer
2095c6c1daeSBarry Smith - vecSeen - The flag which indicates whether we have viewed a vector
2105c6c1daeSBarry Smith 
2115c6c1daeSBarry Smith   Level: advanced
2125c6c1daeSBarry Smith 
2135c6c1daeSBarry Smith .keywords: Viewer, Vec
2145c6c1daeSBarry Smith .seealso: PetscViewerVUGetVecSeen()
2155c6c1daeSBarry Smith @*/
2165c6c1daeSBarry Smith PetscErrorCode  PetscViewerVUSetVecSeen(PetscViewer viewer, PetscBool vecSeen)
2175c6c1daeSBarry Smith {
2185c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
2195c6c1daeSBarry Smith 
2205c6c1daeSBarry Smith   PetscFunctionBegin;
2215c6c1daeSBarry Smith   vu->vecSeen = vecSeen;
2225c6c1daeSBarry Smith   PetscFunctionReturn(0);
2235c6c1daeSBarry Smith }
2245c6c1daeSBarry Smith 
2255c6c1daeSBarry Smith /*@C
2265c6c1daeSBarry Smith   PetscViewerVUGetVecSeen - Gets the flag which indicates whether we have viewed
2275c6c1daeSBarry Smith   a vector. This is usually called internally rather than by a user.
2285c6c1daeSBarry Smith 
2295c6c1daeSBarry Smith   Not Collective
2305c6c1daeSBarry Smith 
2315c6c1daeSBarry Smith   Input Parameter:
2325c6c1daeSBarry Smith . viewer  - The PetscViewer
2335c6c1daeSBarry Smith 
2345c6c1daeSBarry Smith   Output Parameter:
2355c6c1daeSBarry Smith . vecSeen - The flag which indicates whether we have viewed a vector
2365c6c1daeSBarry Smith 
2375c6c1daeSBarry Smith   Level: advanced
2385c6c1daeSBarry Smith 
2395c6c1daeSBarry Smith .keywords: Viewer, Vec
2405c6c1daeSBarry Smith .seealso: PetscViewerVUGetVecSeen()
2415c6c1daeSBarry Smith @*/
2425c6c1daeSBarry Smith PetscErrorCode  PetscViewerVUGetVecSeen(PetscViewer viewer, PetscBool  *vecSeen)
2435c6c1daeSBarry Smith {
2445c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
2455c6c1daeSBarry Smith 
2465c6c1daeSBarry Smith   PetscFunctionBegin;
2475c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2485c6c1daeSBarry Smith   PetscValidPointer(vecSeen,2);
2495c6c1daeSBarry Smith   *vecSeen = vu->vecSeen;
2505c6c1daeSBarry Smith   PetscFunctionReturn(0);
2515c6c1daeSBarry Smith }
2525c6c1daeSBarry Smith 
2535c6c1daeSBarry Smith /*@C
2545c6c1daeSBarry Smith   PetscViewerVUPrintDeferred - Prints to the deferred write cache instead of the file.
2555c6c1daeSBarry Smith 
2565c6c1daeSBarry Smith   Not Collective
2575c6c1daeSBarry Smith 
2585c6c1daeSBarry Smith   Input Parameters:
2595c6c1daeSBarry Smith + viewer - The PetscViewer
2605c6c1daeSBarry Smith - format - The format string
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith   Level: intermediate
2635c6c1daeSBarry Smith 
2645c6c1daeSBarry Smith .keywords: Viewer, print, deferred
2655c6c1daeSBarry Smith .seealso: PetscViewerVUFlushDeferred()
2665c6c1daeSBarry Smith @*/
2675c6c1daeSBarry Smith PetscErrorCode  PetscViewerVUPrintDeferred(PetscViewer viewer, const char format[], ...)
2685c6c1daeSBarry Smith {
2695c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
2705c6c1daeSBarry Smith   va_list        Argp;
2715c6c1daeSBarry Smith   size_t         fullLength;
2725c6c1daeSBarry Smith   PrintfQueue    next;
2735c6c1daeSBarry Smith   PetscErrorCode ierr;
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith   PetscFunctionBegin;
276b00a9115SJed Brown   ierr = PetscNew(&next);CHKERRQ(ierr);
2775c6c1daeSBarry Smith   if (vu->queue) {
2785c6c1daeSBarry Smith     vu->queue->next = next;
2795c6c1daeSBarry Smith     vu->queue       = next;
2800298fd71SBarry Smith     vu->queue->next = NULL;
2815c6c1daeSBarry Smith   } else {
2825c6c1daeSBarry Smith     vu->queueBase   = vu->queue = next;
2835c6c1daeSBarry Smith   }
2845c6c1daeSBarry Smith   vu->queueLength++;
2855c6c1daeSBarry Smith 
2865c6c1daeSBarry Smith   va_start(Argp, format);
2875c6c1daeSBarry Smith   ierr = PetscMemzero(next->string,QUEUESTRINGSIZE);CHKERRQ(ierr);
2885c6c1daeSBarry Smith   ierr = PetscVSNPrintf(next->string, QUEUESTRINGSIZE,format,&fullLength, Argp);CHKERRQ(ierr);
2895c6c1daeSBarry Smith   va_end(Argp);
2905c6c1daeSBarry Smith   PetscFunctionReturn(0);
2915c6c1daeSBarry Smith }
2925c6c1daeSBarry Smith 
2935c6c1daeSBarry Smith /*@C
2945c6c1daeSBarry Smith   PetscViewerVUFlushDeferred - Flushes the deferred write cache to the file.
2955c6c1daeSBarry Smith 
2965c6c1daeSBarry Smith   Not Collective
2975c6c1daeSBarry Smith 
2985c6c1daeSBarry Smith   Input Parameter:
299*a2b725a8SWilliam Gropp . viewer - The PetscViewer
3005c6c1daeSBarry Smith 
3015c6c1daeSBarry Smith   Level: intermediate
3025c6c1daeSBarry Smith 
3035c6c1daeSBarry Smith .keywords: Viewer, flush, deferred
3045c6c1daeSBarry Smith .seealso: PetscViewerVUPrintDeferred()
3055c6c1daeSBarry Smith @*/
3065c6c1daeSBarry Smith PetscErrorCode  PetscViewerVUFlushDeferred(PetscViewer viewer)
3075c6c1daeSBarry Smith {
3085c6c1daeSBarry Smith   PetscViewer_VU *vu  = (PetscViewer_VU*) viewer->data;
3095c6c1daeSBarry Smith   PrintfQueue    next = vu->queueBase;
3105c6c1daeSBarry Smith   PrintfQueue    previous;
3115c6c1daeSBarry Smith   int            i;
3125c6c1daeSBarry Smith   PetscErrorCode ierr;
3135c6c1daeSBarry Smith 
3145c6c1daeSBarry Smith   PetscFunctionBegin;
3155c6c1daeSBarry Smith   for (i = 0; i < vu->queueLength; i++) {
316ce94432eSBarry Smith     PetscFPrintf(PetscObjectComm((PetscObject)viewer), vu->fd, "%s", next->string);
3175c6c1daeSBarry Smith     previous = next;
3185c6c1daeSBarry Smith     next     = next->next;
3195c6c1daeSBarry Smith     ierr     = PetscFree(previous);CHKERRQ(ierr);
3205c6c1daeSBarry Smith   }
3210298fd71SBarry Smith   vu->queue       = NULL;
3225c6c1daeSBarry Smith   vu->queueLength = 0;
3235c6c1daeSBarry Smith   PetscFunctionReturn(0);
3245c6c1daeSBarry Smith }
325