15c6c1daeSBarry Smith 25c6c1daeSBarry 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 #undef __FUNCT__ 225c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileClose_VU" 235c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_VU(PetscViewer viewer) 245c6c1daeSBarry Smith { 255c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 265c6c1daeSBarry Smith PetscErrorCode ierr; 275c6c1daeSBarry Smith 285c6c1daeSBarry Smith PetscFunctionBegin; 295c6c1daeSBarry Smith if (vu->vecSeen) { 305c6c1daeSBarry Smith ierr = PetscViewerVUPrintDeferred(viewer, "};\n\n");CHKERRQ(ierr); 315c6c1daeSBarry Smith } 325c6c1daeSBarry Smith ierr = PetscViewerVUFlushDeferred(viewer);CHKERRQ(ierr); 33ce94432eSBarry Smith ierr = PetscFClose(PetscObjectComm((PetscObject)viewer), vu->fd);CHKERRQ(ierr); 340298fd71SBarry Smith vu->fd = NULL; 355c6c1daeSBarry Smith ierr = PetscFree(vu->filename);CHKERRQ(ierr); 365c6c1daeSBarry Smith PetscFunctionReturn(0); 375c6c1daeSBarry Smith } 385c6c1daeSBarry Smith 395c6c1daeSBarry Smith #undef __FUNCT__ 405c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_VU" 415c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_VU(PetscViewer viewer) 425c6c1daeSBarry Smith { 435c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 445c6c1daeSBarry Smith PetscErrorCode ierr; 455c6c1daeSBarry Smith 465c6c1daeSBarry Smith PetscFunctionBegin; 475c6c1daeSBarry Smith ierr = PetscViewerFileClose_VU(viewer);CHKERRQ(ierr); 485c6c1daeSBarry Smith ierr = PetscFree(vu);CHKERRQ(ierr); 495c6c1daeSBarry Smith PetscFunctionReturn(0); 505c6c1daeSBarry Smith } 515c6c1daeSBarry Smith 525c6c1daeSBarry Smith #undef __FUNCT__ 535c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFlush_VU" 545c6c1daeSBarry Smith PetscErrorCode PetscViewerFlush_VU(PetscViewer viewer) 555c6c1daeSBarry Smith { 565c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 575c6c1daeSBarry Smith PetscMPIInt rank; 585c6c1daeSBarry Smith int err; 595c6c1daeSBarry Smith PetscErrorCode ierr; 605c6c1daeSBarry Smith 615c6c1daeSBarry Smith PetscFunctionBegin; 62ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);CHKERRQ(ierr); 635c6c1daeSBarry Smith if (!rank) { 645c6c1daeSBarry Smith err = fflush(vu->fd); 655c6c1daeSBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 665c6c1daeSBarry Smith } 675c6c1daeSBarry Smith PetscFunctionReturn(0); 685c6c1daeSBarry Smith } 695c6c1daeSBarry Smith 705c6c1daeSBarry Smith #undef __FUNCT__ 715c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetName_VU" 725c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetName_VU(PetscViewer viewer, const char **name) 735c6c1daeSBarry Smith { 745c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 755c6c1daeSBarry Smith 765c6c1daeSBarry Smith PetscFunctionBegin; 775c6c1daeSBarry Smith *name = vu->filename; 785c6c1daeSBarry Smith PetscFunctionReturn(0); 795c6c1daeSBarry Smith } 805c6c1daeSBarry Smith 815c6c1daeSBarry Smith #undef __FUNCT__ 825c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName_VU" 835c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetName_VU(PetscViewer viewer, const char name[]) 845c6c1daeSBarry Smith { 855c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 865c6c1daeSBarry Smith char fname[PETSC_MAX_PATH_LEN]; 875c6c1daeSBarry Smith int rank; 885c6c1daeSBarry Smith PetscErrorCode ierr; 895c6c1daeSBarry Smith 905c6c1daeSBarry Smith PetscFunctionBegin; 915c6c1daeSBarry Smith if (!name) PetscFunctionReturn(0); 925c6c1daeSBarry Smith ierr = PetscViewerFileClose_VU(viewer);CHKERRQ(ierr); 93ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank);CHKERRQ(ierr); 945c6c1daeSBarry Smith if (rank != 0) PetscFunctionReturn(0); 955c6c1daeSBarry Smith ierr = PetscStrallocpy(name, &vu->filename);CHKERRQ(ierr); 965c6c1daeSBarry Smith ierr = PetscFixFilename(name, fname);CHKERRQ(ierr); 975c6c1daeSBarry Smith switch (vu->mode) { 985c6c1daeSBarry Smith case FILE_MODE_READ: 995c6c1daeSBarry Smith vu->fd = fopen(fname, "r"); 1005c6c1daeSBarry Smith break; 1015c6c1daeSBarry Smith case FILE_MODE_WRITE: 1025c6c1daeSBarry Smith vu->fd = fopen(fname, "w"); 1035c6c1daeSBarry Smith break; 1045c6c1daeSBarry Smith case FILE_MODE_APPEND: 1055c6c1daeSBarry Smith vu->fd = fopen(fname, "a"); 1065c6c1daeSBarry Smith break; 1075c6c1daeSBarry Smith case FILE_MODE_UPDATE: 1085c6c1daeSBarry Smith vu->fd = fopen(fname, "r+"); 109a297a907SKarl Rupp if (!vu->fd) vu->fd = fopen(fname, "w+"); 1105c6c1daeSBarry Smith break; 1115c6c1daeSBarry Smith case FILE_MODE_APPEND_UPDATE: 1125c6c1daeSBarry Smith /* I really want a file which is opened at the end for updating, 1135c6c1daeSBarry Smith not a+, which opens at the beginning, but makes writes at the end. 1145c6c1daeSBarry Smith */ 1155c6c1daeSBarry Smith vu->fd = fopen(fname, "r+"); 116a297a907SKarl Rupp if (!vu->fd) vu->fd = fopen(fname, "w+"); 117a297a907SKarl Rupp else { 1185c6c1daeSBarry Smith ierr = fseek(vu->fd, 0, SEEK_END);CHKERRQ(ierr); 1195c6c1daeSBarry Smith } 1205c6c1daeSBarry Smith break; 1215c6c1daeSBarry Smith default: 1225c6c1daeSBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid file mode %d", vu->mode); 1235c6c1daeSBarry Smith } 1245c6c1daeSBarry Smith 1255c6c1daeSBarry Smith if (!vu->fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN, "Cannot open PetscViewer file: %s", fname); 1265c6c1daeSBarry Smith #if defined(PETSC_USE_LOG) 1275c6c1daeSBarry Smith PetscLogObjectState((PetscObject) viewer, "File: %s", name); 1285c6c1daeSBarry Smith #endif 1295c6c1daeSBarry Smith PetscFunctionReturn(0); 1305c6c1daeSBarry Smith } 1315c6c1daeSBarry Smith 1325c6c1daeSBarry Smith #undef __FUNCT__ 1335c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_VU" 134*8cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_VU(PetscViewer viewer) 1355c6c1daeSBarry Smith { 1365c6c1daeSBarry Smith PetscViewer_VU *vu; 1375c6c1daeSBarry Smith PetscErrorCode ierr; 1385c6c1daeSBarry Smith 1395c6c1daeSBarry Smith PetscFunctionBegin; 1405c6c1daeSBarry Smith ierr = PetscNewLog(viewer,PetscViewer_VU, &vu);CHKERRQ(ierr); 1415c6c1daeSBarry Smith viewer->data = (void*) vu; 1425c6c1daeSBarry Smith 1435c6c1daeSBarry Smith viewer->ops->destroy = PetscViewerDestroy_VU; 1445c6c1daeSBarry Smith viewer->ops->flush = PetscViewerFlush_VU; 1450298fd71SBarry Smith viewer->ops->getsingleton = NULL; 1460298fd71SBarry Smith viewer->ops->restoresingleton = NULL; 1475c6c1daeSBarry Smith viewer->format = PETSC_VIEWER_DEFAULT; 1485c6c1daeSBarry Smith viewer->iformat = 0; 1495c6c1daeSBarry Smith 1500298fd71SBarry Smith vu->fd = NULL; 1515c6c1daeSBarry Smith vu->mode = FILE_MODE_WRITE; 1520298fd71SBarry Smith vu->filename = NULL; 1535c6c1daeSBarry Smith vu->vecSeen = PETSC_FALSE; 1540298fd71SBarry Smith vu->queue = NULL; 1550298fd71SBarry Smith vu->queueBase = NULL; 1565c6c1daeSBarry Smith vu->queueLength = 0; 1575c6c1daeSBarry Smith 15800de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileSetName_C", "PetscViewerFileSetName_VU",PetscViewerFileSetName_VU);CHKERRQ(ierr); 15900de8ff0SBarry Smith ierr = PetscObjectComposeFunction((PetscObject) viewer,"PetscViewerFileGetName_C", "PetscViewerFileGetName_VU",PetscViewerFileGetName_VU);CHKERRQ(ierr); 1605c6c1daeSBarry Smith PetscFunctionReturn(0); 1615c6c1daeSBarry Smith } 1625c6c1daeSBarry Smith 1635c6c1daeSBarry Smith #undef __FUNCT__ 1645c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVUGetPointer" 1655c6c1daeSBarry Smith /*@C 1665c6c1daeSBarry Smith PetscViewerVUGetPointer - Extracts the file pointer from a VU PetscViewer. 1675c6c1daeSBarry Smith 1685c6c1daeSBarry Smith Not Collective 1695c6c1daeSBarry Smith 1705c6c1daeSBarry Smith Input Parameter: 1715c6c1daeSBarry Smith . viewer - The PetscViewer 1725c6c1daeSBarry Smith 1735c6c1daeSBarry Smith Output Parameter: 1745c6c1daeSBarry Smith . fd - The file pointer 1755c6c1daeSBarry Smith 1765c6c1daeSBarry Smith Level: intermediate 1775c6c1daeSBarry Smith 1785c6c1daeSBarry Smith Concepts: PetscViewer^file pointer 1795c6c1daeSBarry Smith Concepts: file pointer^getting from PetscViewer 1805c6c1daeSBarry Smith 1815c6c1daeSBarry Smith .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 #undef __FUNCT__ 1955c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVUSetMode" 1965c6c1daeSBarry Smith /*@C 1975c6c1daeSBarry Smith PetscViewerVUSetMode - Sets the mode in which to open the file. 1985c6c1daeSBarry Smith 1995c6c1daeSBarry Smith Not Collective 2005c6c1daeSBarry Smith 2015c6c1daeSBarry Smith Input Parameters: 2025c6c1daeSBarry Smith + viewer - The PetscViewer 2035c6c1daeSBarry Smith - mode - The file mode 2045c6c1daeSBarry Smith 2055c6c1daeSBarry Smith Level: intermediate 2065c6c1daeSBarry Smith 2075c6c1daeSBarry Smith .keywords: Viewer, file, get, pointer 2085c6c1daeSBarry Smith .seealso: PetscViewerASCIISetMode() 2095c6c1daeSBarry Smith @*/ 2105c6c1daeSBarry Smith PetscErrorCode PetscViewerVUSetMode(PetscViewer viewer, PetscFileMode mode) 2115c6c1daeSBarry Smith { 2125c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 2135c6c1daeSBarry Smith 2145c6c1daeSBarry Smith PetscFunctionBegin; 2155c6c1daeSBarry Smith vu->mode = mode; 2165c6c1daeSBarry Smith PetscFunctionReturn(0); 2175c6c1daeSBarry Smith } 2185c6c1daeSBarry Smith 2195c6c1daeSBarry Smith #undef __FUNCT__ 2205c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVUSetVecSeen" 2215c6c1daeSBarry Smith /*@C 2225c6c1daeSBarry Smith PetscViewerVUSetVecSeen - Sets the flag which indicates whether we have viewed 2235c6c1daeSBarry Smith a vector. This is usually called internally rather than by a user. 2245c6c1daeSBarry Smith 2255c6c1daeSBarry Smith Not Collective 2265c6c1daeSBarry Smith 2275c6c1daeSBarry Smith Input Parameters: 2285c6c1daeSBarry Smith + viewer - The PetscViewer 2295c6c1daeSBarry Smith - vecSeen - The flag which indicates whether we have viewed a vector 2305c6c1daeSBarry Smith 2315c6c1daeSBarry Smith Level: advanced 2325c6c1daeSBarry Smith 2335c6c1daeSBarry Smith .keywords: Viewer, Vec 2345c6c1daeSBarry Smith .seealso: PetscViewerVUGetVecSeen() 2355c6c1daeSBarry Smith @*/ 2365c6c1daeSBarry Smith PetscErrorCode PetscViewerVUSetVecSeen(PetscViewer viewer, PetscBool vecSeen) 2375c6c1daeSBarry Smith { 2385c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 2395c6c1daeSBarry Smith 2405c6c1daeSBarry Smith PetscFunctionBegin; 2415c6c1daeSBarry Smith vu->vecSeen = vecSeen; 2425c6c1daeSBarry Smith PetscFunctionReturn(0); 2435c6c1daeSBarry Smith } 2445c6c1daeSBarry Smith 2455c6c1daeSBarry Smith #undef __FUNCT__ 2465c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVUGetVecSeen" 2475c6c1daeSBarry Smith /*@C 2485c6c1daeSBarry Smith PetscViewerVUGetVecSeen - Gets the flag which indicates whether we have viewed 2495c6c1daeSBarry Smith a vector. This is usually called internally rather than by a user. 2505c6c1daeSBarry Smith 2515c6c1daeSBarry Smith Not Collective 2525c6c1daeSBarry Smith 2535c6c1daeSBarry Smith Input Parameter: 2545c6c1daeSBarry Smith . viewer - The PetscViewer 2555c6c1daeSBarry Smith 2565c6c1daeSBarry Smith Output Parameter: 2575c6c1daeSBarry Smith . vecSeen - The flag which indicates whether we have viewed a vector 2585c6c1daeSBarry Smith 2595c6c1daeSBarry Smith Level: advanced 2605c6c1daeSBarry Smith 2615c6c1daeSBarry Smith .keywords: Viewer, Vec 2625c6c1daeSBarry Smith .seealso: PetscViewerVUGetVecSeen() 2635c6c1daeSBarry Smith @*/ 2645c6c1daeSBarry Smith PetscErrorCode PetscViewerVUGetVecSeen(PetscViewer viewer, PetscBool *vecSeen) 2655c6c1daeSBarry Smith { 2665c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 2675c6c1daeSBarry Smith 2685c6c1daeSBarry Smith PetscFunctionBegin; 2695c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 2705c6c1daeSBarry Smith PetscValidPointer(vecSeen,2); 2715c6c1daeSBarry Smith *vecSeen = vu->vecSeen; 2725c6c1daeSBarry Smith PetscFunctionReturn(0); 2735c6c1daeSBarry Smith } 2745c6c1daeSBarry Smith 2755c6c1daeSBarry Smith #undef __FUNCT__ 2765c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVUPrintDeferred" 2775c6c1daeSBarry Smith /*@C 2785c6c1daeSBarry Smith PetscViewerVUPrintDeferred - Prints to the deferred write cache instead of the file. 2795c6c1daeSBarry Smith 2805c6c1daeSBarry Smith Not Collective 2815c6c1daeSBarry Smith 2825c6c1daeSBarry Smith Input Parameters: 2835c6c1daeSBarry Smith + viewer - The PetscViewer 2845c6c1daeSBarry Smith - format - The format string 2855c6c1daeSBarry Smith 2865c6c1daeSBarry Smith Level: intermediate 2875c6c1daeSBarry Smith 2885c6c1daeSBarry Smith .keywords: Viewer, print, deferred 2895c6c1daeSBarry Smith .seealso: PetscViewerVUFlushDeferred() 2905c6c1daeSBarry Smith @*/ 2915c6c1daeSBarry Smith PetscErrorCode PetscViewerVUPrintDeferred(PetscViewer viewer, const char format[], ...) 2925c6c1daeSBarry Smith { 2935c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 2945c6c1daeSBarry Smith va_list Argp; 2955c6c1daeSBarry Smith size_t fullLength; 2965c6c1daeSBarry Smith PrintfQueue next; 2975c6c1daeSBarry Smith PetscErrorCode ierr; 2985c6c1daeSBarry Smith 2995c6c1daeSBarry Smith PetscFunctionBegin; 3005c6c1daeSBarry Smith ierr = PetscNew(struct _PrintfQueue, &next);CHKERRQ(ierr); 3015c6c1daeSBarry Smith if (vu->queue) { 3025c6c1daeSBarry Smith vu->queue->next = next; 3035c6c1daeSBarry Smith vu->queue = next; 3040298fd71SBarry Smith vu->queue->next = NULL; 3055c6c1daeSBarry Smith } else { 3065c6c1daeSBarry Smith vu->queueBase = vu->queue = next; 3075c6c1daeSBarry Smith } 3085c6c1daeSBarry Smith vu->queueLength++; 3095c6c1daeSBarry Smith 3105c6c1daeSBarry Smith va_start(Argp, format); 3115c6c1daeSBarry Smith ierr = PetscMemzero(next->string,QUEUESTRINGSIZE);CHKERRQ(ierr); 3125c6c1daeSBarry Smith ierr = PetscVSNPrintf(next->string, QUEUESTRINGSIZE,format,&fullLength, Argp);CHKERRQ(ierr); 3135c6c1daeSBarry Smith va_end(Argp); 3145c6c1daeSBarry Smith PetscFunctionReturn(0); 3155c6c1daeSBarry Smith } 3165c6c1daeSBarry Smith 3175c6c1daeSBarry Smith #undef __FUNCT__ 3185c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerVUFlushDeferred" 3195c6c1daeSBarry Smith /*@C 3205c6c1daeSBarry Smith PetscViewerVUFlushDeferred - Flushes the deferred write cache to the file. 3215c6c1daeSBarry Smith 3225c6c1daeSBarry Smith Not Collective 3235c6c1daeSBarry Smith 3245c6c1daeSBarry Smith Input Parameter: 3255c6c1daeSBarry Smith + viewer - The PetscViewer 3265c6c1daeSBarry Smith 3275c6c1daeSBarry Smith Level: intermediate 3285c6c1daeSBarry Smith 3295c6c1daeSBarry Smith .keywords: Viewer, flush, deferred 3305c6c1daeSBarry Smith .seealso: PetscViewerVUPrintDeferred() 3315c6c1daeSBarry Smith @*/ 3325c6c1daeSBarry Smith PetscErrorCode PetscViewerVUFlushDeferred(PetscViewer viewer) 3335c6c1daeSBarry Smith { 3345c6c1daeSBarry Smith PetscViewer_VU *vu = (PetscViewer_VU*) viewer->data; 3355c6c1daeSBarry Smith PrintfQueue next = vu->queueBase; 3365c6c1daeSBarry Smith PrintfQueue previous; 3375c6c1daeSBarry Smith int i; 3385c6c1daeSBarry Smith PetscErrorCode ierr; 3395c6c1daeSBarry Smith 3405c6c1daeSBarry Smith PetscFunctionBegin; 3415c6c1daeSBarry Smith for (i = 0; i < vu->queueLength; i++) { 342ce94432eSBarry Smith PetscFPrintf(PetscObjectComm((PetscObject)viewer), vu->fd, "%s", next->string); 3435c6c1daeSBarry Smith previous = next; 3445c6c1daeSBarry Smith next = next->next; 3455c6c1daeSBarry Smith ierr = PetscFree(previous);CHKERRQ(ierr); 3465c6c1daeSBarry Smith } 3470298fd71SBarry Smith vu->queue = NULL; 3485c6c1daeSBarry Smith vu->queueLength = 0; 3495c6c1daeSBarry Smith PetscFunctionReturn(0); 3505c6c1daeSBarry Smith } 351