15c6c1daeSBarry Smith 2665c2dedSJed Brown #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h> /*I "petscviewer.h" I*/ 35c6c1daeSBarry Smith 45c6c1daeSBarry Smith #define QUEUESTRINGSIZE 8192 55c6c1daeSBarry Smith 65c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_ASCII(PetscViewer viewer) 75c6c1daeSBarry Smith { 85c6c1daeSBarry Smith PetscErrorCode ierr; 95c6c1daeSBarry Smith PetscMPIInt rank; 105c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data; 115c6c1daeSBarry Smith int err; 125c6c1daeSBarry Smith 135c6c1daeSBarry Smith PetscFunctionBegin; 14ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 155c6c1daeSBarry Smith if (!rank && vascii->fd != stderr && vascii->fd != PETSC_STDOUT) { 165c6c1daeSBarry Smith if (vascii->fd && vascii->closefile) { 175c6c1daeSBarry Smith err = fclose(vascii->fd); 185c6c1daeSBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 195c6c1daeSBarry Smith } 205c6c1daeSBarry Smith if (vascii->storecompressed) { 215c6c1daeSBarry Smith char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN]; 225c6c1daeSBarry Smith FILE *fp; 235c6c1daeSBarry Smith ierr = PetscStrcpy(par,"gzip ");CHKERRQ(ierr); 245c6c1daeSBarry Smith ierr = PetscStrcat(par,vascii->filename);CHKERRQ(ierr); 255c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN) 260298fd71SBarry Smith ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr); 27f23aa3ddSBarry Smith if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from compression command %s\n%s",par,buf); 280298fd71SBarry Smith ierr = PetscPClose(PETSC_COMM_SELF,fp,NULL);CHKERRQ(ierr); 295c6c1daeSBarry Smith #else 305c6c1daeSBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 315c6c1daeSBarry Smith #endif 325c6c1daeSBarry Smith } 335c6c1daeSBarry Smith } 345c6c1daeSBarry Smith ierr = PetscFree(vascii->filename);CHKERRQ(ierr); 355c6c1daeSBarry Smith PetscFunctionReturn(0); 365c6c1daeSBarry Smith } 375c6c1daeSBarry Smith 385c6c1daeSBarry Smith /* ----------------------------------------------------------------------*/ 395c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_ASCII(PetscViewer viewer) 405c6c1daeSBarry Smith { 415c6c1daeSBarry Smith PetscErrorCode ierr; 425c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data; 435c6c1daeSBarry Smith PetscViewerLink *vlink; 445c6c1daeSBarry Smith PetscBool flg; 455c6c1daeSBarry Smith 465c6c1daeSBarry Smith PetscFunctionBegin; 475c6c1daeSBarry Smith if (vascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"ASCII PetscViewer destroyed before restoring singleton or subcomm PetscViewer"); 485c6c1daeSBarry Smith ierr = PetscViewerFileClose_ASCII(viewer);CHKERRQ(ierr); 495c6c1daeSBarry Smith ierr = PetscFree(vascii);CHKERRQ(ierr); 505c6c1daeSBarry Smith 515c6c1daeSBarry Smith /* remove the viewer from the list in the MPI Communicator */ 525c6c1daeSBarry Smith if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) { 535c6c1daeSBarry Smith ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);CHKERRQ(ierr); 545c6c1daeSBarry Smith } 555c6c1daeSBarry Smith 56ce94432eSBarry Smith ierr = MPI_Attr_get(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr); 575c6c1daeSBarry Smith if (flg) { 585c6c1daeSBarry Smith if (vlink && vlink->viewer == viewer) { 59e5840a18SBarry Smith if (vlink->next) { 60ce94432eSBarry Smith ierr = MPI_Attr_put(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval,vlink->next);CHKERRQ(ierr); 61e5840a18SBarry Smith } else { 62e5840a18SBarry Smith ierr = MPI_Attr_delete(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval);CHKERRQ(ierr); 63e5840a18SBarry Smith } 645c6c1daeSBarry Smith ierr = PetscFree(vlink);CHKERRQ(ierr); 655c6c1daeSBarry Smith } else { 665c6c1daeSBarry Smith while (vlink && vlink->next) { 675c6c1daeSBarry Smith if (vlink->next->viewer == viewer) { 685c6c1daeSBarry Smith PetscViewerLink *nv = vlink->next; 695c6c1daeSBarry Smith vlink->next = vlink->next->next; 705c6c1daeSBarry Smith ierr = PetscFree(nv);CHKERRQ(ierr); 715c6c1daeSBarry Smith } 725c6c1daeSBarry Smith vlink = vlink->next; 735c6c1daeSBarry Smith } 745c6c1daeSBarry Smith } 755c6c1daeSBarry Smith } 765c6c1daeSBarry Smith PetscFunctionReturn(0); 775c6c1daeSBarry Smith } 785c6c1daeSBarry Smith 793f08860eSBarry Smith PetscErrorCode PetscViewerDestroy_ASCII_SubViewer(PetscViewer viewer) 805c6c1daeSBarry Smith { 815c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data; 825c6c1daeSBarry Smith PetscErrorCode ierr; 835fd66863SKarl Rupp 845c6c1daeSBarry Smith PetscFunctionBegin; 853f08860eSBarry Smith ierr = PetscViewerRestoreSubViewer(vascii->bviewer,0,&viewer);CHKERRQ(ierr); 865c6c1daeSBarry Smith PetscFunctionReturn(0); 875c6c1daeSBarry Smith } 885c6c1daeSBarry Smith 895c6c1daeSBarry Smith PetscErrorCode PetscViewerFlush_ASCII(PetscViewer viewer) 905c6c1daeSBarry Smith { 915c6c1daeSBarry Smith PetscErrorCode ierr; 925c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data; 935c6c1daeSBarry Smith int err; 94559f443fSBarry Smith MPI_Comm comm; 95559f443fSBarry Smith PetscMPIInt rank,size; 96559f443fSBarry Smith FILE *fd = vascii->fd; 975c6c1daeSBarry Smith 985c6c1daeSBarry Smith PetscFunctionBegin; 99559f443fSBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 100559f443fSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 101559f443fSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 102559f443fSBarry Smith 103559f443fSBarry Smith if (!vascii->bviewer && !rank && (vascii->mode != FILE_MODE_READ)) { 1045c6c1daeSBarry Smith err = fflush(vascii->fd); 1055c6c1daeSBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() call failed"); 1065c6c1daeSBarry Smith } 1075c6c1daeSBarry Smith 1085c6c1daeSBarry Smith if (vascii->allowsynchronized) { 109559f443fSBarry Smith PetscMPIInt tag,i,j,n = 0,dummy = 0; 110559f443fSBarry Smith char *message; 111559f443fSBarry Smith MPI_Status status; 112559f443fSBarry Smith 113559f443fSBarry Smith ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr); 114559f443fSBarry Smith 115559f443fSBarry Smith /* First processor waits for messages from all other processors */ 116559f443fSBarry Smith if (!rank) { 117559f443fSBarry Smith /* flush my own messages that I may have queued up */ 118559f443fSBarry Smith PrintfQueue next = vascii->petsc_printfqueuebase,previous; 119559f443fSBarry Smith for (i=0; i<vascii->petsc_printfqueuelength; i++) { 120559f443fSBarry Smith if (!vascii->bviewer) { 121559f443fSBarry Smith ierr = PetscFPrintf(comm,fd,"%s",next->string);CHKERRQ(ierr); 122559f443fSBarry Smith } else { 123559f443fSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(vascii->bviewer,"%s",next->string);CHKERRQ(ierr); 124559f443fSBarry Smith } 125559f443fSBarry Smith previous = next; 126559f443fSBarry Smith next = next->next; 127559f443fSBarry Smith ierr = PetscFree(previous->string);CHKERRQ(ierr); 128559f443fSBarry Smith ierr = PetscFree(previous);CHKERRQ(ierr); 129559f443fSBarry Smith } 130559f443fSBarry Smith vascii->petsc_printfqueue = 0; 131559f443fSBarry Smith vascii->petsc_printfqueuelength = 0; 132559f443fSBarry Smith for (i=1; i<size; i++) { 133559f443fSBarry Smith /* to prevent a flood of messages to process zero, request each message separately */ 134559f443fSBarry Smith ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRQ(ierr); 135559f443fSBarry Smith ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr); 136559f443fSBarry Smith for (j=0; j<n; j++) { 137559f443fSBarry Smith PetscMPIInt size = 0; 138559f443fSBarry Smith 139559f443fSBarry Smith ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr); 140559f443fSBarry Smith ierr = PetscMalloc1(size, &message);CHKERRQ(ierr); 141559f443fSBarry Smith ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRQ(ierr); 142559f443fSBarry Smith if (!vascii->bviewer) { 143559f443fSBarry Smith ierr = PetscFPrintf(comm,fd,"%s",message);CHKERRQ(ierr); 144559f443fSBarry Smith } else { 145559f443fSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(vascii->bviewer,"%s",message);CHKERRQ(ierr); 146559f443fSBarry Smith } 147559f443fSBarry Smith ierr = PetscFree(message);CHKERRQ(ierr); 148559f443fSBarry Smith } 149559f443fSBarry Smith } 150559f443fSBarry Smith } else { /* other processors send queue to processor 0 */ 151559f443fSBarry Smith PrintfQueue next = vascii->petsc_printfqueuebase,previous; 152559f443fSBarry Smith 153559f443fSBarry Smith ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr); 154559f443fSBarry Smith ierr = MPI_Send(&vascii->petsc_printfqueuelength,1,MPI_INT,0,tag,comm);CHKERRQ(ierr); 155559f443fSBarry Smith for (i=0; i<vascii->petsc_printfqueuelength; i++) { 156559f443fSBarry Smith ierr = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRQ(ierr); 157559f443fSBarry Smith ierr = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRQ(ierr); 158559f443fSBarry Smith previous = next; 159559f443fSBarry Smith next = next->next; 160559f443fSBarry Smith ierr = PetscFree(previous->string);CHKERRQ(ierr); 161559f443fSBarry Smith ierr = PetscFree(previous);CHKERRQ(ierr); 162559f443fSBarry Smith } 163559f443fSBarry Smith vascii->petsc_printfqueue = 0; 164559f443fSBarry Smith vascii->petsc_printfqueuelength = 0; 165559f443fSBarry Smith } 166559f443fSBarry Smith ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 1675c6c1daeSBarry Smith } 1685c6c1daeSBarry Smith PetscFunctionReturn(0); 1695c6c1daeSBarry Smith } 1705c6c1daeSBarry Smith 1715c6c1daeSBarry Smith /*@C 1725c6c1daeSBarry Smith PetscViewerASCIIGetPointer - Extracts the file pointer from an ASCII PetscViewer. 1735c6c1daeSBarry Smith 1745c6c1daeSBarry Smith Not Collective 1755c6c1daeSBarry Smith 1765c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerASCIIOpen() 1775c6c1daeSBarry Smith - fd - file pointer 1785c6c1daeSBarry Smith 1795c6c1daeSBarry Smith Level: intermediate 1805c6c1daeSBarry Smith 1815c6c1daeSBarry Smith Fortran Note: 1825c6c1daeSBarry Smith This routine is not supported in Fortran. 1835c6c1daeSBarry Smith 1845c6c1daeSBarry Smith Concepts: PetscViewer^file pointer 1855c6c1daeSBarry Smith Concepts: file pointer^getting from PetscViewer 1865c6c1daeSBarry Smith 1875c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerCreate(), PetscViewerASCIIPrintf(), 1885c6c1daeSBarry Smith PetscViewerASCIISynchronizedPrintf(), PetscViewerFlush() 1895c6c1daeSBarry Smith @*/ 1905c6c1daeSBarry Smith PetscErrorCode PetscViewerASCIIGetPointer(PetscViewer viewer,FILE **fd) 1915c6c1daeSBarry Smith { 1925c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data; 1935c6c1daeSBarry Smith 1945c6c1daeSBarry Smith PetscFunctionBegin; 1955c6c1daeSBarry Smith *fd = vascii->fd; 1965c6c1daeSBarry Smith PetscFunctionReturn(0); 1975c6c1daeSBarry Smith } 1985c6c1daeSBarry Smith 1995c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetMode_ASCII(PetscViewer viewer, PetscFileMode *mode) 2005c6c1daeSBarry Smith { 2015c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data; 2025c6c1daeSBarry Smith 2035c6c1daeSBarry Smith PetscFunctionBegin; 2045c6c1daeSBarry Smith *mode = vascii->mode; 2055c6c1daeSBarry Smith PetscFunctionReturn(0); 2065c6c1daeSBarry Smith } 2075c6c1daeSBarry Smith 2085c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetMode_ASCII(PetscViewer viewer, PetscFileMode mode) 2095c6c1daeSBarry Smith { 2105c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data; 2115c6c1daeSBarry Smith 2125c6c1daeSBarry Smith PetscFunctionBegin; 2135c6c1daeSBarry Smith vascii->mode = mode; 2145c6c1daeSBarry Smith PetscFunctionReturn(0); 2155c6c1daeSBarry Smith } 2165c6c1daeSBarry Smith 2175c6c1daeSBarry Smith /* 2185c6c1daeSBarry Smith If petsc_history is on, then all Petsc*Printf() results are saved 2195c6c1daeSBarry Smith if the appropriate (usually .petschistory) file. 2205c6c1daeSBarry Smith */ 2215c6c1daeSBarry Smith extern FILE *petsc_history; 2225c6c1daeSBarry Smith 2235c6c1daeSBarry Smith /*@ 2245c6c1daeSBarry Smith PetscViewerASCIISetTab - Causes PetscViewer to tab in a number of times 2255c6c1daeSBarry Smith 2265c6c1daeSBarry Smith Not Collective, but only first processor in set has any effect 2275c6c1daeSBarry Smith 2285c6c1daeSBarry Smith Input Parameters: 2291575c14dSBarry Smith + viewer - obtained with PetscViewerASCIIOpen() 2305c6c1daeSBarry Smith - tabs - number of tabs 2315c6c1daeSBarry Smith 2325c6c1daeSBarry Smith Level: developer 2335c6c1daeSBarry Smith 2345c6c1daeSBarry Smith Fortran Note: 2355c6c1daeSBarry Smith This routine is not supported in Fortran. 2365c6c1daeSBarry Smith 2375c6c1daeSBarry Smith Concepts: PetscViewerASCII^formating 2385c6c1daeSBarry Smith Concepts: tab^setting 2395c6c1daeSBarry Smith 2405c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIIGetTab(), 2415c6c1daeSBarry Smith PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(), 2425c6c1daeSBarry Smith PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab() 2435c6c1daeSBarry Smith @*/ 2445c6c1daeSBarry Smith PetscErrorCode PetscViewerASCIISetTab(PetscViewer viewer,PetscInt tabs) 2455c6c1daeSBarry Smith { 2465c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data; 2475c6c1daeSBarry Smith PetscBool iascii; 2485c6c1daeSBarry Smith PetscErrorCode ierr; 2495c6c1daeSBarry Smith 2505c6c1daeSBarry Smith PetscFunctionBegin; 2515c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 2525c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 253a297a907SKarl Rupp if (iascii) ascii->tab = tabs; 2545c6c1daeSBarry Smith PetscFunctionReturn(0); 2555c6c1daeSBarry Smith } 2565c6c1daeSBarry Smith 2575c6c1daeSBarry Smith /*@ 2585c6c1daeSBarry Smith PetscViewerASCIIGetTab - Return the number of tabs used by PetscViewer. 2595c6c1daeSBarry Smith 2605c6c1daeSBarry Smith Not Collective, meaningful on first processor only. 2615c6c1daeSBarry Smith 2625c6c1daeSBarry Smith Input Parameters: 2631575c14dSBarry Smith . viewer - obtained with PetscViewerASCIIOpen() 2645c6c1daeSBarry Smith Output Parameters: 2655c6c1daeSBarry Smith . tabs - number of tabs 2665c6c1daeSBarry Smith 2675c6c1daeSBarry Smith Level: developer 2685c6c1daeSBarry Smith 2695c6c1daeSBarry Smith Fortran Note: 2705c6c1daeSBarry Smith This routine is not supported in Fortran. 2715c6c1daeSBarry Smith 2725c6c1daeSBarry Smith Concepts: PetscViewerASCII^formating 2735c6c1daeSBarry Smith Concepts: tab^retrieval 2745c6c1daeSBarry Smith 2755c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISetTab(), 2765c6c1daeSBarry Smith PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(), 2775c6c1daeSBarry Smith PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab() 2785c6c1daeSBarry Smith @*/ 2795c6c1daeSBarry Smith PetscErrorCode PetscViewerASCIIGetTab(PetscViewer viewer,PetscInt *tabs) 2805c6c1daeSBarry Smith { 2815c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data; 2825c6c1daeSBarry Smith PetscBool iascii; 2835c6c1daeSBarry Smith PetscErrorCode ierr; 2845c6c1daeSBarry Smith 2855c6c1daeSBarry Smith PetscFunctionBegin; 2865c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 2875c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 288a297a907SKarl Rupp if (iascii && tabs) *tabs = ascii->tab; 2895c6c1daeSBarry Smith PetscFunctionReturn(0); 2905c6c1daeSBarry Smith } 2915c6c1daeSBarry Smith 2925c6c1daeSBarry Smith /*@ 2935c6c1daeSBarry Smith PetscViewerASCIIAddTab - Add to the number of times an ASCII viewer tabs before printing 2945c6c1daeSBarry Smith 2955c6c1daeSBarry Smith Not Collective, but only first processor in set has any effect 2965c6c1daeSBarry Smith 2975c6c1daeSBarry Smith Input Parameters: 2981575c14dSBarry Smith + viewer - obtained with PetscViewerASCIIOpen() 2995c6c1daeSBarry Smith - tabs - number of tabs 3005c6c1daeSBarry Smith 3015c6c1daeSBarry Smith Level: developer 3025c6c1daeSBarry Smith 3035c6c1daeSBarry Smith Fortran Note: 3045c6c1daeSBarry Smith This routine is not supported in Fortran. 3055c6c1daeSBarry Smith 3065c6c1daeSBarry Smith Concepts: PetscViewerASCII^formating 3075c6c1daeSBarry Smith Concepts: tab^setting 3085c6c1daeSBarry Smith 3095c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 3105c6c1daeSBarry Smith PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(), 3115c6c1daeSBarry Smith PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab() 3125c6c1daeSBarry Smith @*/ 3135c6c1daeSBarry Smith PetscErrorCode PetscViewerASCIIAddTab(PetscViewer viewer,PetscInt tabs) 3145c6c1daeSBarry Smith { 3155c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data; 3165c6c1daeSBarry Smith PetscBool iascii; 3175c6c1daeSBarry Smith PetscErrorCode ierr; 3185c6c1daeSBarry Smith 3195c6c1daeSBarry Smith PetscFunctionBegin; 3205c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 3215c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 322a297a907SKarl Rupp if (iascii) ascii->tab += tabs; 3235c6c1daeSBarry Smith PetscFunctionReturn(0); 3245c6c1daeSBarry Smith } 3255c6c1daeSBarry Smith 3265c6c1daeSBarry Smith /*@ 3275c6c1daeSBarry Smith PetscViewerASCIISubtractTab - Subtracts from the number of times an ASCII viewer tabs before printing 3285c6c1daeSBarry Smith 3295c6c1daeSBarry Smith Not Collective, but only first processor in set has any effect 3305c6c1daeSBarry Smith 3315c6c1daeSBarry Smith Input Parameters: 3321575c14dSBarry Smith + viewer - obtained with PetscViewerASCIIOpen() 3335c6c1daeSBarry Smith - tabs - number of tabs 3345c6c1daeSBarry Smith 3355c6c1daeSBarry Smith Level: developer 3365c6c1daeSBarry Smith 3375c6c1daeSBarry Smith Fortran Note: 3385c6c1daeSBarry Smith This routine is not supported in Fortran. 3395c6c1daeSBarry Smith 3405c6c1daeSBarry Smith Concepts: PetscViewerASCII^formating 3415c6c1daeSBarry Smith Concepts: tab^setting 3425c6c1daeSBarry Smith 3435c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 3445c6c1daeSBarry Smith PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(), 3455c6c1daeSBarry Smith PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab() 3465c6c1daeSBarry Smith @*/ 3475c6c1daeSBarry Smith PetscErrorCode PetscViewerASCIISubtractTab(PetscViewer viewer,PetscInt tabs) 3485c6c1daeSBarry Smith { 3495c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data; 3505c6c1daeSBarry Smith PetscBool iascii; 3515c6c1daeSBarry Smith PetscErrorCode ierr; 3525c6c1daeSBarry Smith 3535c6c1daeSBarry Smith PetscFunctionBegin; 3545c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 3555c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 356a297a907SKarl Rupp if (iascii) ascii->tab -= tabs; 3575c6c1daeSBarry Smith PetscFunctionReturn(0); 3585c6c1daeSBarry Smith } 3595c6c1daeSBarry Smith 3605c6c1daeSBarry Smith /*@C 3611575c14dSBarry Smith PetscViewerASCIIPushSynchronized - Allows calls to PetscViewerASCIISynchronizedPrintf() for this viewer 3625c6c1daeSBarry Smith 3635c6c1daeSBarry Smith Collective on PetscViewer 3645c6c1daeSBarry Smith 3655c6c1daeSBarry Smith Input Parameters: 3661575c14dSBarry Smith . viewer - obtained with PetscViewerASCIIOpen() 3675c6c1daeSBarry Smith 3685c6c1daeSBarry Smith Level: intermediate 3695c6c1daeSBarry Smith 3705c6c1daeSBarry Smith Concepts: PetscViewerASCII^formating 3715c6c1daeSBarry Smith Concepts: tab^setting 3725c6c1daeSBarry Smith 3731575c14dSBarry Smith .seealso: PetscViewerASCIIPopSynchronized(), PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 3745c6c1daeSBarry Smith PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(), 3755c6c1daeSBarry Smith PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer() 3765c6c1daeSBarry Smith @*/ 3771575c14dSBarry Smith PetscErrorCode PetscViewerASCIIPushSynchronized(PetscViewer viewer) 3785c6c1daeSBarry Smith { 3795c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data; 3805c6c1daeSBarry Smith PetscBool iascii; 3815c6c1daeSBarry Smith PetscErrorCode ierr; 3825c6c1daeSBarry Smith 3835c6c1daeSBarry Smith PetscFunctionBegin; 3845c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 3855c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 3861575c14dSBarry Smith if (iascii) ascii->allowsynchronized++; 3871575c14dSBarry Smith PetscFunctionReturn(0); 3881575c14dSBarry Smith } 3891575c14dSBarry Smith 3901575c14dSBarry Smith /*@C 3911575c14dSBarry Smith PetscViewerASCIIPopSynchronized - Undoes most recent PetscViewerASCIIPushSynchronized() for this viewer 3921575c14dSBarry Smith 3931575c14dSBarry Smith Collective on PetscViewer 3941575c14dSBarry Smith 3951575c14dSBarry Smith Input Parameters: 3961575c14dSBarry Smith . viewer - obtained with PetscViewerASCIIOpen() 3971575c14dSBarry Smith 3981575c14dSBarry Smith Level: intermediate 3991575c14dSBarry Smith 4001575c14dSBarry Smith Concepts: PetscViewerASCII^formating 4011575c14dSBarry Smith Concepts: tab^setting 4021575c14dSBarry Smith 4031575c14dSBarry Smith .seealso: PetscViewerASCIIPushSynchronized(), PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 4041575c14dSBarry Smith PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(), 4051575c14dSBarry Smith PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer() 4061575c14dSBarry Smith @*/ 4071575c14dSBarry Smith PetscErrorCode PetscViewerASCIIPopSynchronized(PetscViewer viewer) 4081575c14dSBarry Smith { 4091575c14dSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data; 4101575c14dSBarry Smith PetscBool iascii; 4111575c14dSBarry Smith PetscErrorCode ierr; 4121575c14dSBarry Smith 4131575c14dSBarry Smith PetscFunctionBegin; 4141575c14dSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 4151575c14dSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 4161575c14dSBarry Smith if (iascii) { 4171575c14dSBarry Smith ascii->allowsynchronized--; 4181575c14dSBarry Smith if (ascii->allowsynchronized < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called more times than PetscViewerASCIIPushSynchronized()"); 4191575c14dSBarry Smith } 4205c6c1daeSBarry Smith PetscFunctionReturn(0); 4215c6c1daeSBarry Smith } 4225c6c1daeSBarry Smith 4235c6c1daeSBarry Smith /*@ 4245c6c1daeSBarry Smith PetscViewerASCIIPushTab - Adds one more tab to the amount that PetscViewerASCIIPrintf() 4255c6c1daeSBarry Smith lines are tabbed. 4265c6c1daeSBarry Smith 4275c6c1daeSBarry Smith Not Collective, but only first processor in set has any effect 4285c6c1daeSBarry Smith 4295c6c1daeSBarry Smith Input Parameters: 4301575c14dSBarry Smith . viewer - obtained with PetscViewerASCIIOpen() 4315c6c1daeSBarry Smith 4325c6c1daeSBarry Smith Level: developer 4335c6c1daeSBarry Smith 4345c6c1daeSBarry Smith Fortran Note: 4355c6c1daeSBarry Smith This routine is not supported in Fortran. 4365c6c1daeSBarry Smith 4375c6c1daeSBarry Smith Concepts: PetscViewerASCII^formating 4385c6c1daeSBarry Smith Concepts: tab^setting 4395c6c1daeSBarry Smith 4405c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 4415c6c1daeSBarry Smith PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(), 4425c6c1daeSBarry Smith PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer() 4435c6c1daeSBarry Smith @*/ 4445c6c1daeSBarry Smith PetscErrorCode PetscViewerASCIIPushTab(PetscViewer viewer) 4455c6c1daeSBarry Smith { 4465c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data; 4475c6c1daeSBarry Smith PetscBool iascii; 4485c6c1daeSBarry Smith PetscErrorCode ierr; 4495c6c1daeSBarry Smith 4505c6c1daeSBarry Smith PetscFunctionBegin; 4515c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 4525c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 453a297a907SKarl Rupp if (iascii) ascii->tab++; 4545c6c1daeSBarry Smith PetscFunctionReturn(0); 4555c6c1daeSBarry Smith } 4565c6c1daeSBarry Smith 4575c6c1daeSBarry Smith /*@ 4585c6c1daeSBarry Smith PetscViewerASCIIPopTab - Removes one tab from the amount that PetscViewerASCIIPrintf() 4595c6c1daeSBarry Smith lines are tabbed. 4605c6c1daeSBarry Smith 4615c6c1daeSBarry Smith Not Collective, but only first processor in set has any effect 4625c6c1daeSBarry Smith 4635c6c1daeSBarry Smith Input Parameters: 4641575c14dSBarry Smith . viewer - obtained with PetscViewerASCIIOpen() 4655c6c1daeSBarry Smith 4665c6c1daeSBarry Smith Level: developer 4675c6c1daeSBarry Smith 4685c6c1daeSBarry Smith Fortran Note: 4695c6c1daeSBarry Smith This routine is not supported in Fortran. 4705c6c1daeSBarry Smith 4715c6c1daeSBarry Smith Concepts: PetscViewerASCII^formating 4725c6c1daeSBarry Smith Concepts: tab^setting 4735c6c1daeSBarry Smith 4745c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 4755c6c1daeSBarry Smith PetscViewerASCIIPushTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(), 4765c6c1daeSBarry Smith PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer() 4775c6c1daeSBarry Smith @*/ 4785c6c1daeSBarry Smith PetscErrorCode PetscViewerASCIIPopTab(PetscViewer viewer) 4795c6c1daeSBarry Smith { 4805c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data; 4815c6c1daeSBarry Smith PetscErrorCode ierr; 4825c6c1daeSBarry Smith PetscBool iascii; 4835c6c1daeSBarry Smith 4845c6c1daeSBarry Smith PetscFunctionBegin; 4855c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 4865c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 4875c6c1daeSBarry Smith if (iascii) { 4885c6c1daeSBarry Smith if (ascii->tab <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"More tabs popped than pushed"); 4895c6c1daeSBarry Smith ascii->tab--; 4905c6c1daeSBarry Smith } 4915c6c1daeSBarry Smith PetscFunctionReturn(0); 4925c6c1daeSBarry Smith } 4935c6c1daeSBarry Smith 4945c6c1daeSBarry Smith /*@ 4955c6c1daeSBarry Smith PetscViewerASCIIUseTabs - Turns on or off the use of tabs with the ASCII PetscViewer 4965c6c1daeSBarry Smith 4975c6c1daeSBarry Smith Not Collective, but only first processor in set has any effect 4985c6c1daeSBarry Smith 4995c6c1daeSBarry Smith Input Parameters: 5001575c14dSBarry Smith + viewer - obtained with PetscViewerASCIIOpen() 5015c6c1daeSBarry Smith - flg - PETSC_TRUE or PETSC_FALSE 5025c6c1daeSBarry Smith 5035c6c1daeSBarry Smith Level: developer 5045c6c1daeSBarry Smith 5055c6c1daeSBarry Smith Fortran Note: 5065c6c1daeSBarry Smith This routine is not supported in Fortran. 5075c6c1daeSBarry Smith 5085c6c1daeSBarry Smith Concepts: PetscViewerASCII^formating 5095c6c1daeSBarry Smith Concepts: tab^setting 5105c6c1daeSBarry Smith 5115c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 5125c6c1daeSBarry Smith PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPushTab(), PetscViewerASCIIOpen(), 5135c6c1daeSBarry Smith PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer() 5145c6c1daeSBarry Smith @*/ 5155c6c1daeSBarry Smith PetscErrorCode PetscViewerASCIIUseTabs(PetscViewer viewer,PetscBool flg) 5165c6c1daeSBarry Smith { 5175c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data; 5185c6c1daeSBarry Smith PetscBool iascii; 5195c6c1daeSBarry Smith PetscErrorCode ierr; 5205c6c1daeSBarry Smith 5215c6c1daeSBarry Smith PetscFunctionBegin; 5225c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 5235c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 5245c6c1daeSBarry Smith if (iascii) { 525a297a907SKarl Rupp if (flg) ascii->tab = ascii->tab_store; 526a297a907SKarl Rupp else { 5275c6c1daeSBarry Smith ascii->tab_store = ascii->tab; 5285c6c1daeSBarry Smith ascii->tab = 0; 5295c6c1daeSBarry Smith } 5305c6c1daeSBarry Smith } 5315c6c1daeSBarry Smith PetscFunctionReturn(0); 5325c6c1daeSBarry Smith } 5335c6c1daeSBarry Smith 5345c6c1daeSBarry Smith /* ----------------------------------------------------------------------- */ 5355c6c1daeSBarry Smith 5365c6c1daeSBarry Smith 5375c6c1daeSBarry Smith /*@C 5385c6c1daeSBarry Smith PetscViewerASCIIPrintf - Prints to a file, only from the first 5395c6c1daeSBarry Smith processor in the PetscViewer 5405c6c1daeSBarry Smith 5415c6c1daeSBarry Smith Not Collective, but only first processor in set has any effect 5425c6c1daeSBarry Smith 5435c6c1daeSBarry Smith Input Parameters: 5441575c14dSBarry Smith + viewer - obtained with PetscViewerASCIIOpen() 5455c6c1daeSBarry Smith - format - the usual printf() format string 5465c6c1daeSBarry Smith 5475c6c1daeSBarry Smith Level: developer 5485c6c1daeSBarry Smith 5495c6c1daeSBarry Smith Fortran Note: 5505c6c1daeSBarry Smith The call sequence is PetscViewerASCIIPrintf(PetscViewer, character(*), int ierr) from Fortran. 5515c6c1daeSBarry Smith That is, you can only pass a single character string from Fortran. 5525c6c1daeSBarry Smith 5535c6c1daeSBarry Smith Concepts: PetscViewerASCII^printing 5545c6c1daeSBarry Smith Concepts: printing^to file 5555c6c1daeSBarry Smith Concepts: printf 5565c6c1daeSBarry Smith 5575c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIOpen(), 5585c6c1daeSBarry Smith PetscViewerASCIIPushTab(), PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), 5591575c14dSBarry Smith PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushSynchronized() 5605c6c1daeSBarry Smith @*/ 5615c6c1daeSBarry Smith PetscErrorCode PetscViewerASCIIPrintf(PetscViewer viewer,const char format[],...) 5625c6c1daeSBarry Smith { 5635c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data; 5645c6c1daeSBarry Smith PetscMPIInt rank; 565dd2fa690SBarry Smith PetscInt tab,intab = ascii->tab; 5665c6c1daeSBarry Smith PetscErrorCode ierr; 5675c6c1daeSBarry Smith FILE *fd = ascii->fd; 5683f08860eSBarry Smith PetscBool iascii; 5695c6c1daeSBarry Smith int err; 5705c6c1daeSBarry Smith 5715c6c1daeSBarry Smith PetscFunctionBegin; 5725c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 5735c6c1daeSBarry Smith PetscValidCharPointer(format,2); 5745c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 5755c6c1daeSBarry Smith if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer"); 576ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 577559f443fSBarry Smith if (rank) PetscFunctionReturn(0); 5783f08860eSBarry Smith 5793f08860eSBarry Smith if (ascii->bviewer) { /* pass string up to parent viewer */ 5803f08860eSBarry Smith char *string; 5813f08860eSBarry Smith va_list Argp; 5823f08860eSBarry Smith size_t fullLength; 5833f08860eSBarry Smith 5843f08860eSBarry Smith ierr = PetscCalloc1(QUEUESTRINGSIZE, &string);CHKERRQ(ierr); 5853f08860eSBarry Smith va_start(Argp,format); 5861575c14dSBarry Smith ierr = PetscVSNPrintf(string,QUEUESTRINGSIZE,format,&fullLength,Argp);CHKERRQ(ierr); 5873f08860eSBarry Smith va_end(Argp); 588559f443fSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%s",string);CHKERRQ(ierr); 5893f08860eSBarry Smith ierr = PetscFree(string);CHKERRQ(ierr); 5903f08860eSBarry Smith } else { /* write directly to file */ 5915c6c1daeSBarry Smith va_list Argp; 592559f443fSBarry Smith /* flush my own messages that I may have queued up */ 593559f443fSBarry Smith PrintfQueue next = ascii->petsc_printfqueuebase,previous; 594559f443fSBarry Smith PetscInt i; 595559f443fSBarry Smith for (i=0; i<ascii->petsc_printfqueuelength; i++) { 596559f443fSBarry Smith ierr = PetscFPrintf(PETSC_COMM_SELF,fd,"%s",next->string);CHKERRQ(ierr); 597559f443fSBarry Smith previous = next; 598559f443fSBarry Smith next = next->next; 599559f443fSBarry Smith ierr = PetscFree(previous->string);CHKERRQ(ierr); 600559f443fSBarry Smith ierr = PetscFree(previous);CHKERRQ(ierr); 601559f443fSBarry Smith } 602559f443fSBarry Smith ascii->petsc_printfqueue = 0; 603559f443fSBarry Smith ascii->petsc_printfqueuelength = 0; 604dd2fa690SBarry Smith tab = intab; 605a297a907SKarl Rupp while (tab--) { 606a297a907SKarl Rupp ierr = PetscFPrintf(PETSC_COMM_SELF,fd," ");CHKERRQ(ierr); 607a297a907SKarl Rupp } 6085c6c1daeSBarry Smith 6095c6c1daeSBarry Smith va_start(Argp,format); 6105c6c1daeSBarry Smith ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr); 6115c6c1daeSBarry Smith err = fflush(fd); 6125c6c1daeSBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 6135c6c1daeSBarry Smith if (petsc_history) { 6145c6c1daeSBarry Smith va_start(Argp,format); 615dd2fa690SBarry Smith tab = intab; 616a297a907SKarl Rupp while (tab--) { 617706d7a88SBarry Smith ierr = PetscFPrintf(PETSC_COMM_SELF,petsc_history," ");CHKERRQ(ierr); 618a297a907SKarl Rupp } 6195c6c1daeSBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 6205c6c1daeSBarry Smith err = fflush(petsc_history); 6215c6c1daeSBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 6225c6c1daeSBarry Smith } 6235c6c1daeSBarry Smith va_end(Argp); 6245c6c1daeSBarry Smith } 6255c6c1daeSBarry Smith PetscFunctionReturn(0); 6265c6c1daeSBarry Smith } 6275c6c1daeSBarry Smith 6285c6c1daeSBarry Smith /*@C 6295c6c1daeSBarry Smith PetscViewerFileSetName - Sets the name of the file the PetscViewer uses. 6305c6c1daeSBarry Smith 6315c6c1daeSBarry Smith Collective on PetscViewer 6325c6c1daeSBarry Smith 6335c6c1daeSBarry Smith Input Parameters: 6345c6c1daeSBarry Smith + viewer - the PetscViewer; either ASCII or binary 6355c6c1daeSBarry Smith - name - the name of the file it should use 6365c6c1daeSBarry Smith 6375c6c1daeSBarry Smith Level: advanced 6385c6c1daeSBarry Smith 6395c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerDestroy(), 6405c6c1daeSBarry Smith PetscViewerASCIIGetPointer(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf() 6415c6c1daeSBarry Smith 6425c6c1daeSBarry Smith @*/ 6435c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetName(PetscViewer viewer,const char name[]) 6445c6c1daeSBarry Smith { 6455c6c1daeSBarry Smith PetscErrorCode ierr; 6465c6c1daeSBarry Smith 6475c6c1daeSBarry Smith PetscFunctionBegin; 6485c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 6495c6c1daeSBarry Smith PetscValidCharPointer(name,2); 6505c6c1daeSBarry Smith ierr = PetscTryMethod(viewer,"PetscViewerFileSetName_C",(PetscViewer,const char[]),(viewer,name));CHKERRQ(ierr); 6515c6c1daeSBarry Smith PetscFunctionReturn(0); 6525c6c1daeSBarry Smith } 6535c6c1daeSBarry Smith 6545c6c1daeSBarry Smith /*@C 6555c6c1daeSBarry Smith PetscViewerFileGetName - Gets the name of the file the PetscViewer uses. 6565c6c1daeSBarry Smith 6575c6c1daeSBarry Smith Not Collective 6585c6c1daeSBarry Smith 6595c6c1daeSBarry Smith Input Parameter: 6605c6c1daeSBarry Smith . viewer - the PetscViewer; either ASCII or binary 6615c6c1daeSBarry Smith 6625c6c1daeSBarry Smith Output Parameter: 6635c6c1daeSBarry Smith . name - the name of the file it is using 6645c6c1daeSBarry Smith 6655c6c1daeSBarry Smith Level: advanced 6665c6c1daeSBarry Smith 6675c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerFileSetName() 6685c6c1daeSBarry Smith 6695c6c1daeSBarry Smith @*/ 6705c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetName(PetscViewer viewer,const char **name) 6715c6c1daeSBarry Smith { 6725c6c1daeSBarry Smith PetscErrorCode ierr; 6735c6c1daeSBarry Smith 6745c6c1daeSBarry Smith PetscFunctionBegin; 6755c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 676163d334eSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerFileGetName_C",(PetscViewer,const char**),(viewer,name));CHKERRQ(ierr); 6775c6c1daeSBarry Smith PetscFunctionReturn(0); 6785c6c1daeSBarry Smith } 6795c6c1daeSBarry Smith 6805c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetName_ASCII(PetscViewer viewer,const char **name) 6815c6c1daeSBarry Smith { 6825c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data; 6835c6c1daeSBarry Smith 6845c6c1daeSBarry Smith PetscFunctionBegin; 6855c6c1daeSBarry Smith *name = vascii->filename; 6865c6c1daeSBarry Smith PetscFunctionReturn(0); 6875c6c1daeSBarry Smith } 6885c6c1daeSBarry Smith 6895c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetName_ASCII(PetscViewer viewer,const char name[]) 6905c6c1daeSBarry Smith { 6915c6c1daeSBarry Smith PetscErrorCode ierr; 6925c6c1daeSBarry Smith size_t len; 6935c6c1daeSBarry Smith char fname[PETSC_MAX_PATH_LEN],*gz; 6945c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data; 6955c6c1daeSBarry Smith PetscBool isstderr,isstdout; 6965c6c1daeSBarry Smith PetscMPIInt rank; 6975c6c1daeSBarry Smith 6985c6c1daeSBarry Smith PetscFunctionBegin; 6995c6c1daeSBarry Smith ierr = PetscViewerFileClose_ASCII(viewer);CHKERRQ(ierr); 7005c6c1daeSBarry Smith if (!name) PetscFunctionReturn(0); 7015c6c1daeSBarry Smith ierr = PetscStrallocpy(name,&vascii->filename);CHKERRQ(ierr); 7025c6c1daeSBarry Smith 7035c6c1daeSBarry Smith /* Is this file to be compressed */ 7045c6c1daeSBarry Smith vascii->storecompressed = PETSC_FALSE; 705a297a907SKarl Rupp 7065c6c1daeSBarry Smith ierr = PetscStrstr(vascii->filename,".gz",&gz);CHKERRQ(ierr); 7075c6c1daeSBarry Smith if (gz) { 7085c6c1daeSBarry Smith ierr = PetscStrlen(gz,&len);CHKERRQ(ierr); 7095c6c1daeSBarry Smith if (len == 3) { 7105c6c1daeSBarry Smith *gz = 0; 7115c6c1daeSBarry Smith vascii->storecompressed = PETSC_TRUE; 7125c6c1daeSBarry Smith } 7135c6c1daeSBarry Smith } 714ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 7155c6c1daeSBarry Smith if (!rank) { 7165c6c1daeSBarry Smith ierr = PetscStrcmp(name,"stderr",&isstderr);CHKERRQ(ierr); 7175c6c1daeSBarry Smith ierr = PetscStrcmp(name,"stdout",&isstdout);CHKERRQ(ierr); 7185c6c1daeSBarry Smith /* empty filename means stdout */ 7195c6c1daeSBarry Smith if (name[0] == 0) isstdout = PETSC_TRUE; 7205c6c1daeSBarry Smith if (isstderr) vascii->fd = PETSC_STDERR; 7215c6c1daeSBarry Smith else if (isstdout) vascii->fd = PETSC_STDOUT; 7225c6c1daeSBarry Smith else { 7235c6c1daeSBarry Smith 7245c6c1daeSBarry Smith 7255c6c1daeSBarry Smith ierr = PetscFixFilename(name,fname);CHKERRQ(ierr); 7265c6c1daeSBarry Smith switch (vascii->mode) { 7275c6c1daeSBarry Smith case FILE_MODE_READ: 7285c6c1daeSBarry Smith vascii->fd = fopen(fname,"r"); 7295c6c1daeSBarry Smith break; 7305c6c1daeSBarry Smith case FILE_MODE_WRITE: 7315c6c1daeSBarry Smith vascii->fd = fopen(fname,"w"); 7325c6c1daeSBarry Smith break; 7335c6c1daeSBarry Smith case FILE_MODE_APPEND: 7345c6c1daeSBarry Smith vascii->fd = fopen(fname,"a"); 7355c6c1daeSBarry Smith break; 7365c6c1daeSBarry Smith case FILE_MODE_UPDATE: 7375c6c1daeSBarry Smith vascii->fd = fopen(fname,"r+"); 738a297a907SKarl Rupp if (!vascii->fd) vascii->fd = fopen(fname,"w+"); 7395c6c1daeSBarry Smith break; 7405c6c1daeSBarry Smith case FILE_MODE_APPEND_UPDATE: 7415c6c1daeSBarry Smith /* I really want a file which is opened at the end for updating, 7425c6c1daeSBarry Smith not a+, which opens at the beginning, but makes writes at the end. 7435c6c1daeSBarry Smith */ 7445c6c1daeSBarry Smith vascii->fd = fopen(fname,"r+"); 745a297a907SKarl Rupp if (!vascii->fd) vascii->fd = fopen(fname,"w+"); 746a297a907SKarl Rupp else { 7475c6c1daeSBarry Smith ierr = fseek(vascii->fd, 0, SEEK_END);CHKERRQ(ierr); 7485c6c1daeSBarry Smith } 7495c6c1daeSBarry Smith break; 7505c6c1daeSBarry Smith default: 7515c6c1daeSBarry Smith SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid file mode %d", vascii->mode); 7525c6c1daeSBarry Smith } 7535c6c1daeSBarry Smith if (!vascii->fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open PetscViewer file: %s",fname); 7545c6c1daeSBarry Smith } 7555c6c1daeSBarry Smith } 7565c6c1daeSBarry Smith #if defined(PETSC_USE_LOG) 7575c6c1daeSBarry Smith PetscLogObjectState((PetscObject)viewer,"File: %s",name); 7585c6c1daeSBarry Smith #endif 7595c6c1daeSBarry Smith PetscFunctionReturn(0); 7605c6c1daeSBarry Smith } 7615c6c1daeSBarry Smith 7623f08860eSBarry Smith PetscErrorCode PetscViewerGetSubViewer_ASCII(PetscViewer viewer,MPI_Comm subcomm,PetscViewer *outviewer) 7635c6c1daeSBarry Smith { 7645c6c1daeSBarry Smith PetscMPIInt rank; 7655c6c1daeSBarry Smith PetscErrorCode ierr; 7665c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data,*ovascii; 7675c6c1daeSBarry Smith 7685c6c1daeSBarry Smith PetscFunctionBegin; 7693f08860eSBarry Smith if (vascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"SubViewer already obtained from PetscViewer and not restored"); 7701575c14dSBarry Smith ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr); 7713f08860eSBarry Smith ierr = PetscViewerCreate(subcomm,outviewer);CHKERRQ(ierr); 7725c6c1daeSBarry Smith ierr = PetscViewerSetType(*outviewer,PETSCVIEWERASCII);CHKERRQ(ierr); 7731575c14dSBarry Smith ierr = PetscViewerASCIIPushSynchronized(*outviewer);CHKERRQ(ierr); 7745c6c1daeSBarry Smith ovascii = (PetscViewer_ASCII*)(*outviewer)->data; 7755c6c1daeSBarry Smith ovascii->fd = vascii->fd; 7765c6c1daeSBarry Smith ovascii->tab = vascii->tab; 777ba5a0b41SBarry Smith ovascii->closefile = PETSC_FALSE; 7785c6c1daeSBarry Smith 7795c6c1daeSBarry Smith vascii->sviewer = *outviewer; 7805c6c1daeSBarry Smith 7815c6c1daeSBarry Smith (*outviewer)->format = viewer->format; 7825c6c1daeSBarry Smith 783ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 7845c6c1daeSBarry Smith ((PetscViewer_ASCII*)((*outviewer)->data))->bviewer = viewer; 7853f08860eSBarry Smith (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII_SubViewer; 7865c6c1daeSBarry Smith PetscFunctionReturn(0); 7875c6c1daeSBarry Smith } 7885c6c1daeSBarry Smith 7893f08860eSBarry Smith PetscErrorCode PetscViewerRestoreSubViewer_ASCII(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer) 7905c6c1daeSBarry Smith { 7915c6c1daeSBarry Smith PetscErrorCode ierr; 7925c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data; 7935c6c1daeSBarry Smith 7945c6c1daeSBarry Smith PetscFunctionBegin; 7951575c14dSBarry Smith if (!ascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"SubViewer never obtained from PetscViewer"); 7961575c14dSBarry Smith if (ascii->sviewer != *outviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"This PetscViewer did not generate this SubViewer"); 7975c6c1daeSBarry Smith 7985c6c1daeSBarry Smith ascii->sviewer = 0; 7995c6c1daeSBarry Smith (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII; 8005c6c1daeSBarry Smith ierr = PetscViewerDestroy(outviewer);CHKERRQ(ierr); 8015c6c1daeSBarry Smith PetscFunctionReturn(0); 8025c6c1daeSBarry Smith } 8035c6c1daeSBarry Smith 8042bf49c77SBarry Smith PetscErrorCode PetscViewerView_ASCII(PetscViewer v,PetscViewer viewer) 8052bf49c77SBarry Smith { 8062bf49c77SBarry Smith PetscErrorCode ierr; 8072bf49c77SBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)v->data; 8082bf49c77SBarry Smith 8092bf49c77SBarry Smith PetscFunctionBegin; 8102bf49c77SBarry Smith if (ascii->filename) { 8112bf49c77SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",ascii->filename);CHKERRQ(ierr); 8122bf49c77SBarry Smith } 8132bf49c77SBarry Smith PetscFunctionReturn(0); 8142bf49c77SBarry Smith } 8152bf49c77SBarry Smith 8168556b5ebSBarry Smith /*MC 8178556b5ebSBarry Smith PETSCVIEWERASCII - A viewer that prints to stdout or an ASCII file 8188556b5ebSBarry Smith 8198556b5ebSBarry Smith 8208556b5ebSBarry Smith .seealso: PETSC_VIEWER_STDOUT_(),PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDOUT_WORLD,PetscViewerCreate(), PetscViewerASCIIOpen(), 8218556b5ebSBarry Smith PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERBINARY, PETSCVIEWERMATLAB, 8228556b5ebSBarry Smith PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType() 8238556b5ebSBarry Smith 8241b266c99SBarry Smith Level: beginner 8251b266c99SBarry Smith 8268556b5ebSBarry Smith M*/ 8278cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_ASCII(PetscViewer viewer) 8285c6c1daeSBarry Smith { 8295c6c1daeSBarry Smith PetscViewer_ASCII *vascii; 8305c6c1daeSBarry Smith PetscErrorCode ierr; 8315c6c1daeSBarry Smith 8325c6c1daeSBarry Smith PetscFunctionBegin; 833b00a9115SJed Brown ierr = PetscNewLog(viewer,&vascii);CHKERRQ(ierr); 8345c6c1daeSBarry Smith viewer->data = (void*)vascii; 8355c6c1daeSBarry Smith 8365c6c1daeSBarry Smith viewer->ops->destroy = PetscViewerDestroy_ASCII; 8375c6c1daeSBarry Smith viewer->ops->flush = PetscViewerFlush_ASCII; 838559f443fSBarry Smith viewer->ops->getsubviewer = PetscViewerGetSubViewer_ASCII; 839559f443fSBarry Smith viewer->ops->restoresubviewer = PetscViewerRestoreSubViewer_ASCII; 8402bf49c77SBarry Smith viewer->ops->view = PetscViewerView_ASCII; 8411d641e7bSMichael Lange viewer->ops->read = PetscViewerASCIIRead; 8425c6c1daeSBarry Smith 8435c6c1daeSBarry Smith /* defaults to stdout unless set with PetscViewerFileSetName() */ 8445c6c1daeSBarry Smith vascii->fd = PETSC_STDOUT; 8455c6c1daeSBarry Smith vascii->mode = FILE_MODE_WRITE; 8465c6c1daeSBarry Smith vascii->bviewer = 0; 847dd2fa690SBarry Smith vascii->subviewer = 0; 8485c6c1daeSBarry Smith vascii->sviewer = 0; 8495c6c1daeSBarry Smith vascii->tab = 0; 8505c6c1daeSBarry Smith vascii->tab_store = 0; 8515c6c1daeSBarry Smith vascii->filename = 0; 8525c6c1daeSBarry Smith vascii->closefile = PETSC_TRUE; 8535c6c1daeSBarry Smith 854bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",PetscViewerFileSetName_ASCII);CHKERRQ(ierr); 855bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",PetscViewerFileGetName_ASCII);CHKERRQ(ierr); 856bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_ASCII);CHKERRQ(ierr); 857bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_ASCII);CHKERRQ(ierr); 8585c6c1daeSBarry Smith PetscFunctionReturn(0); 8595c6c1daeSBarry Smith } 8605c6c1daeSBarry Smith 8615c6c1daeSBarry Smith /*@C 8625c6c1daeSBarry Smith PetscViewerASCIISynchronizedPrintf - Prints synchronized output to the specified file from 8635c6c1daeSBarry Smith several processors. Output of the first processor is followed by that of the 8645c6c1daeSBarry Smith second, etc. 8655c6c1daeSBarry Smith 8665c6c1daeSBarry Smith Not Collective, must call collective PetscViewerFlush() to get the results out 8675c6c1daeSBarry Smith 8685c6c1daeSBarry Smith Input Parameters: 8695c6c1daeSBarry Smith + viewer - the ASCII PetscViewer 8705c6c1daeSBarry Smith - format - the usual printf() format string 8715c6c1daeSBarry Smith 8725c6c1daeSBarry Smith Level: intermediate 8735c6c1daeSBarry Smith 8745c6c1daeSBarry Smith Notes: You must have previously called PetscViewerASCIISynchronizeAllow() to allow this routine to be called. 8755c6c1daeSBarry Smith 8765c6c1daeSBarry Smith Fortran Note: 8775c6c1daeSBarry Smith Can only print a single character* string 8785c6c1daeSBarry Smith 8795c6c1daeSBarry Smith .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(), 8805c6c1daeSBarry Smith PetscFOpen(), PetscViewerFlush(), PetscViewerASCIIGetPointer(), PetscViewerDestroy(), PetscViewerASCIIOpen(), 8811575c14dSBarry Smith PetscViewerASCIIPrintf(), PetscViewerASCIIPushSynchronized() 8825c6c1daeSBarry Smith 8835c6c1daeSBarry Smith @*/ 8845c6c1daeSBarry Smith PetscErrorCode PetscViewerASCIISynchronizedPrintf(PetscViewer viewer,const char format[],...) 8855c6c1daeSBarry Smith { 8865c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data; 8875c6c1daeSBarry Smith PetscErrorCode ierr; 8883f08860eSBarry Smith PetscMPIInt rank; 8895c6c1daeSBarry Smith PetscInt tab = vascii->tab; 8905c6c1daeSBarry Smith MPI_Comm comm; 8915c6c1daeSBarry Smith FILE *fp; 892559f443fSBarry Smith PetscBool iascii,hasbviewer = PETSC_FALSE; 8935c6c1daeSBarry Smith int err; 8945c6c1daeSBarry Smith 8955c6c1daeSBarry Smith PetscFunctionBegin; 8965c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 8975c6c1daeSBarry Smith PetscValidCharPointer(format,2); 8985c6c1daeSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 8995c6c1daeSBarry Smith if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer"); 9001575c14dSBarry Smith if (!vascii->allowsynchronized) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"First call PetscViewerASCIIPushSynchronized() to allow this call"); 9015c6c1daeSBarry Smith 902ce94432eSBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 9035c6c1daeSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 9045c6c1daeSBarry Smith 905559f443fSBarry Smith if (vascii->bviewer) { 906559f443fSBarry Smith hasbviewer = PETSC_TRUE; 907559f443fSBarry Smith if (!rank) { 908559f443fSBarry Smith vascii = (PetscViewer_ASCII*)vascii->bviewer->data; 909559f443fSBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 910559f443fSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 911559f443fSBarry Smith } 912559f443fSBarry Smith } 9133f08860eSBarry Smith 914559f443fSBarry Smith fp = vascii->fd; 915559f443fSBarry Smith 916559f443fSBarry Smith if (!rank && !hasbviewer) { /* First processor prints immediately to fp */ 9175c6c1daeSBarry Smith va_list Argp; 918559f443fSBarry Smith /* flush my own messages that I may have queued up */ 919559f443fSBarry Smith PrintfQueue next = vascii->petsc_printfqueuebase,previous; 920559f443fSBarry Smith PetscInt i; 921559f443fSBarry Smith for (i=0; i<vascii->petsc_printfqueuelength; i++) { 922559f443fSBarry Smith ierr = PetscFPrintf(comm,fp,"%s",next->string);CHKERRQ(ierr); 923559f443fSBarry Smith previous = next; 924559f443fSBarry Smith next = next->next; 925559f443fSBarry Smith ierr = PetscFree(previous->string);CHKERRQ(ierr); 926559f443fSBarry Smith ierr = PetscFree(previous);CHKERRQ(ierr); 927559f443fSBarry Smith } 928559f443fSBarry Smith vascii->petsc_printfqueue = 0; 929559f443fSBarry Smith vascii->petsc_printfqueuelength = 0; 9305c6c1daeSBarry Smith 931a297a907SKarl Rupp while (tab--) { 932a297a907SKarl Rupp ierr = PetscFPrintf(PETSC_COMM_SELF,fp," ");CHKERRQ(ierr); 933a297a907SKarl Rupp } 9345c6c1daeSBarry Smith 9355c6c1daeSBarry Smith va_start(Argp,format); 9365c6c1daeSBarry Smith ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr); 9375c6c1daeSBarry Smith err = fflush(fp); 9385c6c1daeSBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 9395c6c1daeSBarry Smith if (petsc_history) { 9405c6c1daeSBarry Smith va_start(Argp,format); 9415c6c1daeSBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 9425c6c1daeSBarry Smith err = fflush(petsc_history); 9435c6c1daeSBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 9445c6c1daeSBarry Smith } 9455c6c1daeSBarry Smith va_end(Argp); 946559f443fSBarry Smith } else { /* other processors add to queue */ 9475c6c1daeSBarry Smith char *string; 9485c6c1daeSBarry Smith va_list Argp; 9495c6c1daeSBarry Smith size_t fullLength; 9505c6c1daeSBarry Smith PrintfQueue next; 9515c6c1daeSBarry Smith 952b00a9115SJed Brown ierr = PetscNew(&next);CHKERRQ(ierr); 953559f443fSBarry Smith if (vascii->petsc_printfqueue) { 954559f443fSBarry Smith vascii->petsc_printfqueue->next = next; 955559f443fSBarry Smith vascii->petsc_printfqueue = next; 956a297a907SKarl Rupp } else { 957559f443fSBarry Smith vascii->petsc_printfqueuebase = vascii->petsc_printfqueue = next; 958a297a907SKarl Rupp } 959559f443fSBarry Smith vascii->petsc_printfqueuelength++; 9605c6c1daeSBarry Smith next->size = QUEUESTRINGSIZE; 961785e854fSJed Brown ierr = PetscMalloc1(next->size, &next->string);CHKERRQ(ierr); 9625c6c1daeSBarry Smith ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr); 9635c6c1daeSBarry Smith string = next->string; 9645c6c1daeSBarry Smith tab *= 2; 965a297a907SKarl Rupp while (tab--) { 966a297a907SKarl Rupp *string++ = ' '; 967a297a907SKarl Rupp } 9685c6c1daeSBarry Smith va_start(Argp,format); 96922d28d08SBarry Smith ierr = PetscVSNPrintf(string,next->size-2*vascii->tab,format,&fullLength,Argp);CHKERRQ(ierr); 9705c6c1daeSBarry Smith va_end(Argp); 9715c6c1daeSBarry Smith } 9725c6c1daeSBarry Smith PetscFunctionReturn(0); 9735c6c1daeSBarry Smith } 9745c6c1daeSBarry Smith 9752655f987SMichael Lange /*@C 9762655f987SMichael Lange PetscViewerASCIIRead - Reads from am ASCII file 9772655f987SMichael Lange 9782655f987SMichael Lange Collective on MPI_Comm 9792655f987SMichael Lange 9802655f987SMichael Lange Input Parameters: 9812655f987SMichael Lange + viewer - the ascii viewer 9822655f987SMichael Lange . data - location to write the data 983060da220SMatthew G. Knepley . num - number of items of data to read 9842655f987SMichael Lange - datatype - type of data to read 9852655f987SMichael Lange 986f8e4bde8SMatthew G. Knepley Output Parameters: 987060da220SMatthew G. Knepley . count - number of items of data actually read, or NULL 988f8e4bde8SMatthew G. Knepley 9892655f987SMichael Lange Level: beginner 9902655f987SMichael Lange 9912655f987SMichael Lange Concepts: ascii files 9922655f987SMichael Lange 9936a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 9942655f987SMichael Lange VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 9952655f987SMichael Lange PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 9962655f987SMichael Lange @*/ 997060da220SMatthew G. Knepley PetscErrorCode PetscViewerASCIIRead(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype) 9982655f987SMichael Lange { 9992655f987SMichael Lange PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data; 10002655f987SMichael Lange FILE *fd = vascii->fd; 10012655f987SMichael Lange PetscInt i; 10023b7fe8c3SMatthew G. Knepley int ret = 0; 10032655f987SMichael Lange 10042655f987SMichael Lange PetscFunctionBegin; 10052655f987SMichael Lange PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 1006060da220SMatthew G. Knepley for (i=0; i<num; i++) { 1007f8e4bde8SMatthew G. Knepley if (dtype == PETSC_CHAR) ret = fscanf(fd, "%c", &(((char*)data)[i])); 1008f8e4bde8SMatthew G. Knepley else if (dtype == PETSC_STRING) ret = fscanf(fd, "%s", &(((char*)data)[i])); 1009a05e1a72SSatish Balay else if (dtype == PETSC_INT) ret = fscanf(fd, "%" PetscInt_FMT, &(((PetscInt*)data)[i])); 1010f8e4bde8SMatthew G. Knepley else if (dtype == PETSC_ENUM) ret = fscanf(fd, "%d", &(((int*)data)[i])); 1011f8e4bde8SMatthew G. Knepley else if (dtype == PETSC_FLOAT) ret = fscanf(fd, "%f", &(((float*)data)[i])); 1012f8e4bde8SMatthew G. Knepley else if (dtype == PETSC_DOUBLE) ret = fscanf(fd, "%lg", &(((double*)data)[i])); 1013*fba955ccSBarry Smith #if defined(PETSC_HAVE__FLOAT128) 1014*fba955ccSBarry Smith else if (dtype == PETSC___FLOAT128) { 1015*fba955ccSBarry Smith double tmp; 1016*fba955ccSBarry Smith ret = fscanf(fd, "%lg", &tmp); 1017*fba955ccSBarry Smith (__float128*)data)[i] = tmp; 1018*fba955ccSBarry Smith #endif 1019f8e4bde8SMatthew G. Knepley else {SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Data type %d not supported", (int) dtype);} 1020f8e4bde8SMatthew G. Knepley if (!ret) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Conversion error for data type %d", (int) dtype); 1021f8e4bde8SMatthew G. Knepley else if (ret < 0) break; /* Proxy for EOF, need to check for it in configure */ 10222655f987SMichael Lange } 1023060da220SMatthew G. Knepley if (count) *count = i; 1024060da220SMatthew G. Knepley else if (ret < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Insufficient data, read only %D < %D items", i, num); 10252655f987SMichael Lange PetscFunctionReturn(0); 10262655f987SMichael Lange } 10275c6c1daeSBarry Smith 1028