xref: /petsc/src/sys/classes/viewer/impls/vu/petscvu.c (revision 2e956fe4fc852fabc23b437482e1fb7b77fddb0d)
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 
255c6c1daeSBarry Smith   PetscFunctionBegin;
265c6c1daeSBarry Smith   if (vu->vecSeen) {
279566063dSJacob Faibussowitsch     PetscCall(PetscViewerVUPrintDeferred(viewer, "};\n\n"));
285c6c1daeSBarry Smith   }
299566063dSJacob Faibussowitsch   PetscCall(PetscViewerVUFlushDeferred(viewer));
309566063dSJacob Faibussowitsch   PetscCall(PetscFClose(PetscObjectComm((PetscObject)viewer), vu->fd));
310298fd71SBarry Smith   vu->fd = NULL;
329566063dSJacob Faibussowitsch   PetscCall(PetscFree(vu->filename));
335c6c1daeSBarry Smith   PetscFunctionReturn(0);
345c6c1daeSBarry Smith }
355c6c1daeSBarry Smith 
365c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_VU(PetscViewer viewer)
375c6c1daeSBarry Smith {
385c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
395c6c1daeSBarry Smith 
405c6c1daeSBarry Smith   PetscFunctionBegin;
419566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileClose_VU(viewer));
429566063dSJacob Faibussowitsch   PetscCall(PetscFree(vu));
43*2e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",NULL));
44*2e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",NULL));
45*2e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",NULL));
46*2e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetMode_C",NULL));
475c6c1daeSBarry Smith   PetscFunctionReturn(0);
485c6c1daeSBarry Smith }
495c6c1daeSBarry Smith 
505c6c1daeSBarry Smith PetscErrorCode PetscViewerFlush_VU(PetscViewer viewer)
515c6c1daeSBarry Smith {
525c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
535c6c1daeSBarry Smith   PetscMPIInt    rank;
545c6c1daeSBarry Smith   int            err;
555c6c1daeSBarry Smith 
565c6c1daeSBarry Smith   PetscFunctionBegin;
579566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
58dd400576SPatrick Sanan   if (rank == 0) {
595c6c1daeSBarry Smith     err = fflush(vu->fd);
6028b400f6SJacob Faibussowitsch     PetscCheck(!err,PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
615c6c1daeSBarry Smith   }
625c6c1daeSBarry Smith   PetscFunctionReturn(0);
635c6c1daeSBarry Smith }
645c6c1daeSBarry Smith 
657e4fd573SVaclav Hapla static PetscErrorCode  PetscViewerFileSetMode_VU(PetscViewer viewer, PetscFileMode mode)
667e4fd573SVaclav Hapla {
677e4fd573SVaclav Hapla   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
687e4fd573SVaclav Hapla 
697e4fd573SVaclav Hapla   PetscFunctionBegin;
707e4fd573SVaclav Hapla   vu->mode = mode;
717e4fd573SVaclav Hapla   PetscFunctionReturn(0);
727e4fd573SVaclav Hapla }
737e4fd573SVaclav Hapla 
747e4fd573SVaclav Hapla static PetscErrorCode  PetscViewerFileGetMode_VU(PetscViewer viewer, PetscFileMode *type)
757e4fd573SVaclav Hapla {
767e4fd573SVaclav Hapla   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
777e4fd573SVaclav Hapla 
787e4fd573SVaclav Hapla   PetscFunctionBegin;
797e4fd573SVaclav Hapla   *type = vu->mode;
807e4fd573SVaclav Hapla   PetscFunctionReturn(0);
817e4fd573SVaclav Hapla }
827e4fd573SVaclav Hapla 
837e4fd573SVaclav Hapla static PetscErrorCode  PetscViewerFileGetName_VU(PetscViewer viewer, const char **name)
845c6c1daeSBarry Smith {
855c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
865c6c1daeSBarry Smith 
875c6c1daeSBarry Smith   PetscFunctionBegin;
885c6c1daeSBarry Smith   *name = vu->filename;
895c6c1daeSBarry Smith   PetscFunctionReturn(0);
905c6c1daeSBarry Smith }
915c6c1daeSBarry Smith 
927e4fd573SVaclav Hapla static PetscErrorCode  PetscViewerFileSetName_VU(PetscViewer viewer, const char name[])
935c6c1daeSBarry Smith {
945c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
955c6c1daeSBarry Smith   char           fname[PETSC_MAX_PATH_LEN];
965c6c1daeSBarry Smith   int            rank;
975c6c1daeSBarry Smith 
985c6c1daeSBarry Smith   PetscFunctionBegin;
995c6c1daeSBarry Smith   if (!name) PetscFunctionReturn(0);
1009566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileClose_VU(viewer));
1019566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
1025c6c1daeSBarry Smith   if (rank != 0) PetscFunctionReturn(0);
1039566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name, &vu->filename));
1049566063dSJacob Faibussowitsch   PetscCall(PetscFixFilename(name, fname));
1055c6c1daeSBarry Smith   switch (vu->mode) {
1065c6c1daeSBarry Smith   case FILE_MODE_READ:
1075c6c1daeSBarry Smith     vu->fd = fopen(fname, "r");
1085c6c1daeSBarry Smith     break;
1095c6c1daeSBarry Smith   case FILE_MODE_WRITE:
1105c6c1daeSBarry Smith     vu->fd = fopen(fname, "w");
1115c6c1daeSBarry Smith     break;
1125c6c1daeSBarry Smith   case FILE_MODE_APPEND:
1135c6c1daeSBarry Smith     vu->fd = fopen(fname, "a");
1145c6c1daeSBarry Smith     break;
1155c6c1daeSBarry Smith   case FILE_MODE_UPDATE:
1165c6c1daeSBarry Smith     vu->fd = fopen(fname, "r+");
117a297a907SKarl Rupp     if (!vu->fd) vu->fd = fopen(fname, "w+");
1185c6c1daeSBarry Smith     break;
1195c6c1daeSBarry Smith   case FILE_MODE_APPEND_UPDATE:
1205c6c1daeSBarry Smith     /* I really want a file which is opened at the end for updating,
1215c6c1daeSBarry Smith        not a+, which opens at the beginning, but makes writes at the end.
1225c6c1daeSBarry Smith     */
1235c6c1daeSBarry Smith     vu->fd = fopen(fname, "r+");
124a297a907SKarl Rupp     if (!vu->fd) vu->fd = fopen(fname, "w+");
125a297a907SKarl Rupp     else {
1269566063dSJacob Faibussowitsch       PetscCall(fseek(vu->fd, 0, SEEK_END));
1275c6c1daeSBarry Smith     }
1285c6c1daeSBarry Smith     break;
1295c6c1daeSBarry Smith   default:
13098921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP, "Unsupported file mode %s",PetscFileModes[vu->mode]);
1315c6c1daeSBarry Smith   }
1325c6c1daeSBarry Smith 
13328b400f6SJacob Faibussowitsch   PetscCheck(vu->fd,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN, "Cannot open PetscViewer file: %s", fname);
1345c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
1355c6c1daeSBarry Smith   PetscLogObjectState((PetscObject) viewer, "File: %s", name);
1365c6c1daeSBarry Smith #endif
1375c6c1daeSBarry Smith   PetscFunctionReturn(0);
1385c6c1daeSBarry Smith }
1395c6c1daeSBarry Smith 
1408cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_VU(PetscViewer viewer)
1415c6c1daeSBarry Smith {
1425c6c1daeSBarry Smith   PetscViewer_VU *vu;
1435c6c1daeSBarry Smith 
1445c6c1daeSBarry Smith   PetscFunctionBegin;
1459566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(viewer,&vu));
1465c6c1daeSBarry Smith   viewer->data = (void*) vu;
1475c6c1daeSBarry Smith 
1485c6c1daeSBarry Smith   viewer->ops->destroy          = PetscViewerDestroy_VU;
1495c6c1daeSBarry Smith   viewer->ops->flush            = PetscViewerFlush_VU;
150559f443fSBarry Smith   viewer->ops->getsubviewer     = NULL;
151559f443fSBarry Smith   viewer->ops->restoresubviewer = NULL;
1525c6c1daeSBarry Smith 
1530298fd71SBarry Smith   vu->fd          = NULL;
1545c6c1daeSBarry Smith   vu->mode        = FILE_MODE_WRITE;
1550298fd71SBarry Smith   vu->filename    = NULL;
1565c6c1daeSBarry Smith   vu->vecSeen     = PETSC_FALSE;
1570298fd71SBarry Smith   vu->queue       = NULL;
1580298fd71SBarry Smith   vu->queueBase   = NULL;
1595c6c1daeSBarry Smith   vu->queueLength = 0;
1605c6c1daeSBarry Smith 
1619566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileSetName_C",PetscViewerFileSetName_VU));
1629566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileGetName_C",PetscViewerFileGetName_VU));
1639566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_VU));
1649566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_VU));
1655c6c1daeSBarry Smith   PetscFunctionReturn(0);
1665c6c1daeSBarry Smith }
1675c6c1daeSBarry Smith 
1685c6c1daeSBarry Smith /*@C
1695c6c1daeSBarry Smith   PetscViewerVUGetPointer - Extracts the file pointer from a VU PetscViewer.
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith   Not Collective
1725c6c1daeSBarry Smith 
1735c6c1daeSBarry Smith   Input Parameter:
1745c6c1daeSBarry Smith . viewer - The PetscViewer
1755c6c1daeSBarry Smith 
1765c6c1daeSBarry Smith   Output Parameter:
1775c6c1daeSBarry Smith . fd     - The file pointer
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith   Level: intermediate
1805c6c1daeSBarry Smith 
181db781477SPatrick Sanan .seealso: `PetscViewerASCIIGetPointer()`
1825c6c1daeSBarry Smith @*/
1835c6c1daeSBarry Smith PetscErrorCode  PetscViewerVUGetPointer(PetscViewer viewer, FILE **fd)
1845c6c1daeSBarry Smith {
1855c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith   PetscFunctionBegin;
1885c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1895c6c1daeSBarry Smith   PetscValidPointer(fd,2);
1905c6c1daeSBarry Smith   *fd = vu->fd;
1915c6c1daeSBarry Smith   PetscFunctionReturn(0);
1925c6c1daeSBarry Smith }
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith /*@C
1955c6c1daeSBarry Smith   PetscViewerVUSetVecSeen - Sets the flag which indicates whether we have viewed
1965c6c1daeSBarry Smith   a vector. This is usually called internally rather than by a user.
1975c6c1daeSBarry Smith 
1985c6c1daeSBarry Smith   Not Collective
1995c6c1daeSBarry Smith 
2005c6c1daeSBarry Smith   Input Parameters:
2015c6c1daeSBarry Smith + viewer  - The PetscViewer
2025c6c1daeSBarry Smith - vecSeen - The flag which indicates whether we have viewed a vector
2035c6c1daeSBarry Smith 
2045c6c1daeSBarry Smith   Level: advanced
2055c6c1daeSBarry Smith 
206db781477SPatrick Sanan .seealso: `PetscViewerVUGetVecSeen()`
2075c6c1daeSBarry Smith @*/
2085c6c1daeSBarry Smith PetscErrorCode  PetscViewerVUSetVecSeen(PetscViewer viewer, PetscBool vecSeen)
2095c6c1daeSBarry Smith {
2105c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
2115c6c1daeSBarry Smith 
2125c6c1daeSBarry Smith   PetscFunctionBegin;
2135c6c1daeSBarry Smith   vu->vecSeen = vecSeen;
2145c6c1daeSBarry Smith   PetscFunctionReturn(0);
2155c6c1daeSBarry Smith }
2165c6c1daeSBarry Smith 
2175c6c1daeSBarry Smith /*@C
2185c6c1daeSBarry Smith   PetscViewerVUGetVecSeen - Gets the flag which indicates whether we have viewed
2195c6c1daeSBarry Smith   a vector. This is usually called internally rather than by a user.
2205c6c1daeSBarry Smith 
2215c6c1daeSBarry Smith   Not Collective
2225c6c1daeSBarry Smith 
2235c6c1daeSBarry Smith   Input Parameter:
2245c6c1daeSBarry Smith . viewer  - The PetscViewer
2255c6c1daeSBarry Smith 
2265c6c1daeSBarry Smith   Output Parameter:
2275c6c1daeSBarry Smith . vecSeen - The flag which indicates whether we have viewed a vector
2285c6c1daeSBarry Smith 
2295c6c1daeSBarry Smith   Level: advanced
2305c6c1daeSBarry Smith 
231db781477SPatrick Sanan .seealso: `PetscViewerVUGetVecSeen()`
2325c6c1daeSBarry Smith @*/
2335c6c1daeSBarry Smith PetscErrorCode  PetscViewerVUGetVecSeen(PetscViewer viewer, PetscBool  *vecSeen)
2345c6c1daeSBarry Smith {
2355c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
2365c6c1daeSBarry Smith 
2375c6c1daeSBarry Smith   PetscFunctionBegin;
2385c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
239dadcf809SJacob Faibussowitsch   PetscValidBoolPointer(vecSeen,2);
2405c6c1daeSBarry Smith   *vecSeen = vu->vecSeen;
2415c6c1daeSBarry Smith   PetscFunctionReturn(0);
2425c6c1daeSBarry Smith }
2435c6c1daeSBarry Smith 
2445c6c1daeSBarry Smith /*@C
2455c6c1daeSBarry Smith   PetscViewerVUPrintDeferred - Prints to the deferred write cache instead of the file.
2465c6c1daeSBarry Smith 
2475c6c1daeSBarry Smith   Not Collective
2485c6c1daeSBarry Smith 
2495c6c1daeSBarry Smith   Input Parameters:
2505c6c1daeSBarry Smith + viewer - The PetscViewer
2515c6c1daeSBarry Smith - format - The format string
2525c6c1daeSBarry Smith 
2535c6c1daeSBarry Smith   Level: intermediate
2545c6c1daeSBarry Smith 
255db781477SPatrick Sanan .seealso: `PetscViewerVUFlushDeferred()`
2565c6c1daeSBarry Smith @*/
2575c6c1daeSBarry Smith PetscErrorCode  PetscViewerVUPrintDeferred(PetscViewer viewer, const char format[], ...)
2585c6c1daeSBarry Smith {
2595c6c1daeSBarry Smith   PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data;
2605c6c1daeSBarry Smith   va_list        Argp;
2615c6c1daeSBarry Smith   size_t         fullLength;
2625c6c1daeSBarry Smith   PrintfQueue    next;
2635c6c1daeSBarry Smith 
2645c6c1daeSBarry Smith   PetscFunctionBegin;
2659566063dSJacob Faibussowitsch   PetscCall(PetscNew(&next));
2665c6c1daeSBarry Smith   if (vu->queue) {
2675c6c1daeSBarry Smith     vu->queue->next = next;
2685c6c1daeSBarry Smith     vu->queue       = next;
2690298fd71SBarry Smith     vu->queue->next = NULL;
2705c6c1daeSBarry Smith   } else {
2715c6c1daeSBarry Smith     vu->queueBase   = vu->queue = next;
2725c6c1daeSBarry Smith   }
2735c6c1daeSBarry Smith   vu->queueLength++;
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith   va_start(Argp, format);
2769566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(next->string,QUEUESTRINGSIZE));
2779566063dSJacob Faibussowitsch   PetscCall(PetscVSNPrintf(next->string, QUEUESTRINGSIZE,format,&fullLength, Argp));
2785c6c1daeSBarry Smith   va_end(Argp);
2795c6c1daeSBarry Smith   PetscFunctionReturn(0);
2805c6c1daeSBarry Smith }
2815c6c1daeSBarry Smith 
2825c6c1daeSBarry Smith /*@C
2835c6c1daeSBarry Smith   PetscViewerVUFlushDeferred - Flushes the deferred write cache to the file.
2845c6c1daeSBarry Smith 
2855c6c1daeSBarry Smith   Not Collective
2865c6c1daeSBarry Smith 
2875c6c1daeSBarry Smith   Input Parameter:
288a2b725a8SWilliam Gropp . viewer - The PetscViewer
2895c6c1daeSBarry Smith 
2905c6c1daeSBarry Smith   Level: intermediate
2915c6c1daeSBarry Smith 
292db781477SPatrick Sanan .seealso: `PetscViewerVUPrintDeferred()`
2935c6c1daeSBarry Smith @*/
2945c6c1daeSBarry Smith PetscErrorCode  PetscViewerVUFlushDeferred(PetscViewer viewer)
2955c6c1daeSBarry Smith {
2965c6c1daeSBarry Smith   PetscViewer_VU *vu  = (PetscViewer_VU*) viewer->data;
2975c6c1daeSBarry Smith   PrintfQueue    next = vu->queueBase;
2985c6c1daeSBarry Smith   PrintfQueue    previous;
2995c6c1daeSBarry Smith   int            i;
3005c6c1daeSBarry Smith 
3015c6c1daeSBarry Smith   PetscFunctionBegin;
3025c6c1daeSBarry Smith   for (i = 0; i < vu->queueLength; i++) {
303ce94432eSBarry Smith     PetscFPrintf(PetscObjectComm((PetscObject)viewer), vu->fd, "%s", next->string);
3045c6c1daeSBarry Smith     previous = next;
3055c6c1daeSBarry Smith     next     = next->next;
3069566063dSJacob Faibussowitsch     PetscCall(PetscFree(previous));
3075c6c1daeSBarry Smith   }
3080298fd71SBarry Smith   vu->queue       = NULL;
3095c6c1daeSBarry Smith   vu->queueLength = 0;
3105c6c1daeSBarry Smith   PetscFunctionReturn(0);
3115c6c1daeSBarry Smith }
312