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) { 275f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerVUPrintDeferred(viewer, "};\n\n")); 285c6c1daeSBarry Smith } 295f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerVUFlushDeferred(viewer)); 305f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFClose(PetscObjectComm((PetscObject)viewer), vu->fd)); 310298fd71SBarry Smith vu->fd = NULL; 325f80ce2aSJacob Faibussowitsch CHKERRQ(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; 415f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerFileClose_VU(viewer)); 425f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(vu)); 435c6c1daeSBarry Smith PetscFunctionReturn(0); 445c6c1daeSBarry Smith } 455c6c1daeSBarry Smith 465c6c1daeSBarry Smith PetscErrorCode PetscViewerFlush_VU(PetscViewer viewer) 475c6c1daeSBarry Smith { 485c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 495c6c1daeSBarry Smith PetscMPIInt rank; 505c6c1daeSBarry Smith int err; 515c6c1daeSBarry Smith 525c6c1daeSBarry Smith PetscFunctionBegin; 535f80ce2aSJacob Faibussowitsch CHKERRMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank)); 54dd400576SPatrick Sanan if (rank == 0) { 555c6c1daeSBarry Smith err = fflush(vu->fd); 56*28b400f6SJacob Faibussowitsch PetscCheck(!err,PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 575c6c1daeSBarry Smith } 585c6c1daeSBarry Smith PetscFunctionReturn(0); 595c6c1daeSBarry Smith } 605c6c1daeSBarry Smith 617e4fd573SVaclav Hapla static PetscErrorCode PetscViewerFileSetMode_VU(PetscViewer viewer, PetscFileMode mode) 627e4fd573SVaclav Hapla { 637e4fd573SVaclav Hapla PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 647e4fd573SVaclav Hapla 657e4fd573SVaclav Hapla PetscFunctionBegin; 667e4fd573SVaclav Hapla vu->mode = mode; 677e4fd573SVaclav Hapla PetscFunctionReturn(0); 687e4fd573SVaclav Hapla } 697e4fd573SVaclav Hapla 707e4fd573SVaclav Hapla static PetscErrorCode PetscViewerFileGetMode_VU(PetscViewer viewer, PetscFileMode *type) 717e4fd573SVaclav Hapla { 727e4fd573SVaclav Hapla PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 737e4fd573SVaclav Hapla 747e4fd573SVaclav Hapla PetscFunctionBegin; 757e4fd573SVaclav Hapla *type = vu->mode; 767e4fd573SVaclav Hapla PetscFunctionReturn(0); 777e4fd573SVaclav Hapla } 787e4fd573SVaclav Hapla 797e4fd573SVaclav Hapla static PetscErrorCode PetscViewerFileGetName_VU(PetscViewer viewer, const char **name) 805c6c1daeSBarry Smith { 815c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 825c6c1daeSBarry Smith 835c6c1daeSBarry Smith PetscFunctionBegin; 845c6c1daeSBarry Smith *name = vu->filename; 855c6c1daeSBarry Smith PetscFunctionReturn(0); 865c6c1daeSBarry Smith } 875c6c1daeSBarry Smith 887e4fd573SVaclav Hapla static PetscErrorCode PetscViewerFileSetName_VU(PetscViewer viewer, const char name[]) 895c6c1daeSBarry Smith { 905c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 915c6c1daeSBarry Smith char fname[PETSC_MAX_PATH_LEN]; 925c6c1daeSBarry Smith int rank; 935c6c1daeSBarry Smith 945c6c1daeSBarry Smith PetscFunctionBegin; 955c6c1daeSBarry Smith if (!name) PetscFunctionReturn(0); 965f80ce2aSJacob Faibussowitsch CHKERRQ(PetscViewerFileClose_VU(viewer)); 975f80ce2aSJacob Faibussowitsch CHKERRMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank)); 985c6c1daeSBarry Smith if (rank != 0) PetscFunctionReturn(0); 995f80ce2aSJacob Faibussowitsch CHKERRQ(PetscStrallocpy(name, &vu->filename)); 1005f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFixFilename(name, fname)); 1015c6c1daeSBarry Smith switch (vu->mode) { 1025c6c1daeSBarry Smith case FILE_MODE_READ: 1035c6c1daeSBarry Smith vu->fd = fopen(fname, "r"); 1045c6c1daeSBarry Smith break; 1055c6c1daeSBarry Smith case FILE_MODE_WRITE: 1065c6c1daeSBarry Smith vu->fd = fopen(fname, "w"); 1075c6c1daeSBarry Smith break; 1085c6c1daeSBarry Smith case FILE_MODE_APPEND: 1095c6c1daeSBarry Smith vu->fd = fopen(fname, "a"); 1105c6c1daeSBarry Smith break; 1115c6c1daeSBarry Smith case FILE_MODE_UPDATE: 1125c6c1daeSBarry Smith vu->fd = fopen(fname, "r+"); 113a297a907SKarl Rupp if (!vu->fd) vu->fd = fopen(fname, "w+"); 1145c6c1daeSBarry Smith break; 1155c6c1daeSBarry Smith case FILE_MODE_APPEND_UPDATE: 1165c6c1daeSBarry Smith /* I really want a file which is opened at the end for updating, 1175c6c1daeSBarry Smith not a+, which opens at the beginning, but makes writes at the end. 1185c6c1daeSBarry Smith */ 1195c6c1daeSBarry Smith vu->fd = fopen(fname, "r+"); 120a297a907SKarl Rupp if (!vu->fd) vu->fd = fopen(fname, "w+"); 121a297a907SKarl Rupp else { 1225f80ce2aSJacob Faibussowitsch CHKERRQ(fseek(vu->fd, 0, SEEK_END)); 1235c6c1daeSBarry Smith } 1245c6c1daeSBarry Smith break; 1255c6c1daeSBarry Smith default: 12698921bdaSJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP, "Unsupported file mode %s",PetscFileModes[vu->mode]); 1275c6c1daeSBarry Smith } 1285c6c1daeSBarry Smith 129*28b400f6SJacob Faibussowitsch PetscCheck(vu->fd,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN, "Cannot open PetscViewer file: %s", fname); 1305c6c1daeSBarry Smith #if defined(PETSC_USE_LOG) 1315c6c1daeSBarry Smith PetscLogObjectState((PetscObject) viewer, "File: %s", name); 1325c6c1daeSBarry Smith #endif 1335c6c1daeSBarry Smith PetscFunctionReturn(0); 1345c6c1daeSBarry Smith } 1355c6c1daeSBarry Smith 1368cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_VU(PetscViewer viewer) 1375c6c1daeSBarry Smith { 1385c6c1daeSBarry Smith PetscViewer_VU *vu; 1395c6c1daeSBarry Smith 1405c6c1daeSBarry Smith PetscFunctionBegin; 1415f80ce2aSJacob Faibussowitsch CHKERRQ(PetscNewLog(viewer,&vu)); 1425c6c1daeSBarry Smith viewer->data = (void*) vu; 1435c6c1daeSBarry Smith 1445c6c1daeSBarry Smith viewer->ops->destroy = PetscViewerDestroy_VU; 1455c6c1daeSBarry Smith viewer->ops->flush = PetscViewerFlush_VU; 146559f443fSBarry Smith viewer->ops->getsubviewer = NULL; 147559f443fSBarry Smith viewer->ops->restoresubviewer = NULL; 1485c6c1daeSBarry Smith 1490298fd71SBarry Smith vu->fd = NULL; 1505c6c1daeSBarry Smith vu->mode = FILE_MODE_WRITE; 1510298fd71SBarry Smith vu->filename = NULL; 1525c6c1daeSBarry Smith vu->vecSeen = PETSC_FALSE; 1530298fd71SBarry Smith vu->queue = NULL; 1540298fd71SBarry Smith vu->queueBase = NULL; 1555c6c1daeSBarry Smith vu->queueLength = 0; 1565c6c1daeSBarry Smith 1575f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileSetName_C",PetscViewerFileSetName_VU)); 1585f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileGetName_C",PetscViewerFileGetName_VU)); 1595f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_VU)); 1605f80ce2aSJacob Faibussowitsch CHKERRQ(PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_VU)); 1615c6c1daeSBarry Smith PetscFunctionReturn(0); 1625c6c1daeSBarry Smith } 1635c6c1daeSBarry Smith 1645c6c1daeSBarry Smith /*@C 1655c6c1daeSBarry Smith PetscViewerVUGetPointer - Extracts the file pointer from a VU PetscViewer. 1665c6c1daeSBarry Smith 1675c6c1daeSBarry Smith Not Collective 1685c6c1daeSBarry Smith 1695c6c1daeSBarry Smith Input Parameter: 1705c6c1daeSBarry Smith . viewer - The PetscViewer 1715c6c1daeSBarry Smith 1725c6c1daeSBarry Smith Output Parameter: 1735c6c1daeSBarry Smith . fd - The file pointer 1745c6c1daeSBarry Smith 1755c6c1daeSBarry Smith Level: intermediate 1765c6c1daeSBarry Smith 1775c6c1daeSBarry Smith .seealso: PetscViewerASCIIGetPointer() 1785c6c1daeSBarry Smith @*/ 1795c6c1daeSBarry Smith PetscErrorCode PetscViewerVUGetPointer(PetscViewer viewer, FILE **fd) 1805c6c1daeSBarry Smith { 1815c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 1825c6c1daeSBarry Smith 1835c6c1daeSBarry Smith PetscFunctionBegin; 1845c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 1855c6c1daeSBarry Smith PetscValidPointer(fd,2); 1865c6c1daeSBarry Smith *fd = vu->fd; 1875c6c1daeSBarry Smith PetscFunctionReturn(0); 1885c6c1daeSBarry Smith } 1895c6c1daeSBarry Smith 1905c6c1daeSBarry Smith /*@C 1915c6c1daeSBarry Smith PetscViewerVUSetVecSeen - Sets the flag which indicates whether we have viewed 1925c6c1daeSBarry Smith a vector. This is usually called internally rather than by a user. 1935c6c1daeSBarry Smith 1945c6c1daeSBarry Smith Not Collective 1955c6c1daeSBarry Smith 1965c6c1daeSBarry Smith Input Parameters: 1975c6c1daeSBarry Smith + viewer - The PetscViewer 1985c6c1daeSBarry Smith - vecSeen - The flag which indicates whether we have viewed a vector 1995c6c1daeSBarry Smith 2005c6c1daeSBarry Smith Level: advanced 2015c6c1daeSBarry Smith 2025c6c1daeSBarry Smith .seealso: PetscViewerVUGetVecSeen() 2035c6c1daeSBarry Smith @*/ 2045c6c1daeSBarry Smith PetscErrorCode PetscViewerVUSetVecSeen(PetscViewer viewer, PetscBool vecSeen) 2055c6c1daeSBarry Smith { 2065c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 2075c6c1daeSBarry Smith 2085c6c1daeSBarry Smith PetscFunctionBegin; 2095c6c1daeSBarry Smith vu->vecSeen = vecSeen; 2105c6c1daeSBarry Smith PetscFunctionReturn(0); 2115c6c1daeSBarry Smith } 2125c6c1daeSBarry Smith 2135c6c1daeSBarry Smith /*@C 2145c6c1daeSBarry Smith PetscViewerVUGetVecSeen - Gets the flag which indicates whether we have viewed 2155c6c1daeSBarry Smith a vector. This is usually called internally rather than by a user. 2165c6c1daeSBarry Smith 2175c6c1daeSBarry Smith Not Collective 2185c6c1daeSBarry Smith 2195c6c1daeSBarry Smith Input Parameter: 2205c6c1daeSBarry Smith . viewer - The PetscViewer 2215c6c1daeSBarry Smith 2225c6c1daeSBarry Smith Output Parameter: 2235c6c1daeSBarry Smith . vecSeen - The flag which indicates whether we have viewed a vector 2245c6c1daeSBarry Smith 2255c6c1daeSBarry Smith Level: advanced 2265c6c1daeSBarry Smith 2275c6c1daeSBarry Smith .seealso: PetscViewerVUGetVecSeen() 2285c6c1daeSBarry Smith @*/ 2295c6c1daeSBarry Smith PetscErrorCode PetscViewerVUGetVecSeen(PetscViewer viewer, PetscBool *vecSeen) 2305c6c1daeSBarry Smith { 2315c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 2325c6c1daeSBarry Smith 2335c6c1daeSBarry Smith PetscFunctionBegin; 2345c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 2355c6c1daeSBarry Smith PetscValidPointer(vecSeen,2); 2365c6c1daeSBarry Smith *vecSeen = vu->vecSeen; 2375c6c1daeSBarry Smith PetscFunctionReturn(0); 2385c6c1daeSBarry Smith } 2395c6c1daeSBarry Smith 2405c6c1daeSBarry Smith /*@C 2415c6c1daeSBarry Smith PetscViewerVUPrintDeferred - Prints to the deferred write cache instead of the file. 2425c6c1daeSBarry Smith 2435c6c1daeSBarry Smith Not Collective 2445c6c1daeSBarry Smith 2455c6c1daeSBarry Smith Input Parameters: 2465c6c1daeSBarry Smith + viewer - The PetscViewer 2475c6c1daeSBarry Smith - format - The format string 2485c6c1daeSBarry Smith 2495c6c1daeSBarry Smith Level: intermediate 2505c6c1daeSBarry Smith 2515c6c1daeSBarry Smith .seealso: PetscViewerVUFlushDeferred() 2525c6c1daeSBarry Smith @*/ 2535c6c1daeSBarry Smith PetscErrorCode PetscViewerVUPrintDeferred(PetscViewer viewer, const char format[], ...) 2545c6c1daeSBarry Smith { 2555c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 2565c6c1daeSBarry Smith va_list Argp; 2575c6c1daeSBarry Smith size_t fullLength; 2585c6c1daeSBarry Smith PrintfQueue next; 2595c6c1daeSBarry Smith 2605c6c1daeSBarry Smith PetscFunctionBegin; 2615f80ce2aSJacob Faibussowitsch CHKERRQ(PetscNew(&next)); 2625c6c1daeSBarry Smith if (vu->queue) { 2635c6c1daeSBarry Smith vu->queue->next = next; 2645c6c1daeSBarry Smith vu->queue = next; 2650298fd71SBarry Smith vu->queue->next = NULL; 2665c6c1daeSBarry Smith } else { 2675c6c1daeSBarry Smith vu->queueBase = vu->queue = next; 2685c6c1daeSBarry Smith } 2695c6c1daeSBarry Smith vu->queueLength++; 2705c6c1daeSBarry Smith 2715c6c1daeSBarry Smith va_start(Argp, format); 2725f80ce2aSJacob Faibussowitsch CHKERRQ(PetscArrayzero(next->string,QUEUESTRINGSIZE)); 2735f80ce2aSJacob Faibussowitsch CHKERRQ(PetscVSNPrintf(next->string, QUEUESTRINGSIZE,format,&fullLength, Argp)); 2745c6c1daeSBarry Smith va_end(Argp); 2755c6c1daeSBarry Smith PetscFunctionReturn(0); 2765c6c1daeSBarry Smith } 2775c6c1daeSBarry Smith 2785c6c1daeSBarry Smith /*@C 2795c6c1daeSBarry Smith PetscViewerVUFlushDeferred - Flushes the deferred write cache to the file. 2805c6c1daeSBarry Smith 2815c6c1daeSBarry Smith Not Collective 2825c6c1daeSBarry Smith 2835c6c1daeSBarry Smith Input Parameter: 284a2b725a8SWilliam Gropp . viewer - The PetscViewer 2855c6c1daeSBarry Smith 2865c6c1daeSBarry Smith Level: intermediate 2875c6c1daeSBarry Smith 2885c6c1daeSBarry Smith .seealso: PetscViewerVUPrintDeferred() 2895c6c1daeSBarry Smith @*/ 2905c6c1daeSBarry Smith PetscErrorCode PetscViewerVUFlushDeferred(PetscViewer viewer) 2915c6c1daeSBarry Smith { 2925c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 2935c6c1daeSBarry Smith PrintfQueue next = vu->queueBase; 2945c6c1daeSBarry Smith PrintfQueue previous; 2955c6c1daeSBarry Smith int i; 2965c6c1daeSBarry Smith 2975c6c1daeSBarry Smith PetscFunctionBegin; 2985c6c1daeSBarry Smith for (i = 0; i < vu->queueLength; i++) { 299ce94432eSBarry Smith PetscFPrintf(PetscObjectComm((PetscObject)viewer), vu->fd, "%s", next->string); 3005c6c1daeSBarry Smith previous = next; 3015c6c1daeSBarry Smith next = next->next; 3025f80ce2aSJacob Faibussowitsch CHKERRQ(PetscFree(previous)); 3035c6c1daeSBarry Smith } 3040298fd71SBarry Smith vu->queue = NULL; 3055c6c1daeSBarry Smith vu->queueLength = 0; 3065c6c1daeSBarry Smith PetscFunctionReturn(0); 3075c6c1daeSBarry Smith } 308