xref: /petsc/src/sys/classes/viewer/impls/ascii/filev.c (revision 0298fd7132830bec7daee99a80be0eddb2b310a5)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h>  /*I     "petscsys.h"   I*/
35c6c1daeSBarry Smith #include <stdarg.h>
45c6c1daeSBarry Smith 
55c6c1daeSBarry Smith #define QUEUESTRINGSIZE 8192
65c6c1daeSBarry Smith 
75c6c1daeSBarry Smith #undef __FUNCT__
85c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileClose_ASCII"
95c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_ASCII(PetscViewer viewer)
105c6c1daeSBarry Smith {
115c6c1daeSBarry Smith   PetscErrorCode    ierr;
125c6c1daeSBarry Smith   PetscMPIInt       rank;
135c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
145c6c1daeSBarry Smith   int               err;
155c6c1daeSBarry Smith 
165c6c1daeSBarry Smith   PetscFunctionBegin;
175c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
185c6c1daeSBarry Smith   if (!rank && vascii->fd != stderr && vascii->fd != PETSC_STDOUT) {
195c6c1daeSBarry Smith     if (vascii->fd && vascii->closefile) {
205c6c1daeSBarry Smith       err = fclose(vascii->fd);
215c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
225c6c1daeSBarry Smith     }
235c6c1daeSBarry Smith     if (vascii->storecompressed) {
245c6c1daeSBarry Smith       char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
255c6c1daeSBarry Smith       FILE *fp;
265c6c1daeSBarry Smith       ierr = PetscStrcpy(par,"gzip ");CHKERRQ(ierr);
275c6c1daeSBarry Smith       ierr = PetscStrcat(par,vascii->filename);CHKERRQ(ierr);
285c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN)
29*0298fd71SBarry Smith       ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr);
30f23aa3ddSBarry Smith       if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from compression command %s\n%s",par,buf);
31*0298fd71SBarry Smith       ierr = PetscPClose(PETSC_COMM_SELF,fp,NULL);CHKERRQ(ierr);
325c6c1daeSBarry Smith #else
335c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
345c6c1daeSBarry Smith #endif
355c6c1daeSBarry Smith     }
365c6c1daeSBarry Smith   }
375c6c1daeSBarry Smith   ierr = PetscFree(vascii->filename);CHKERRQ(ierr);
385c6c1daeSBarry Smith   PetscFunctionReturn(0);
395c6c1daeSBarry Smith }
405c6c1daeSBarry Smith 
415c6c1daeSBarry Smith /* ----------------------------------------------------------------------*/
425c6c1daeSBarry Smith #undef __FUNCT__
435c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_ASCII"
445c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_ASCII(PetscViewer viewer)
455c6c1daeSBarry Smith {
465c6c1daeSBarry Smith   PetscErrorCode    ierr;
475c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
485c6c1daeSBarry Smith   PetscViewerLink   *vlink;
495c6c1daeSBarry Smith   PetscBool         flg;
505c6c1daeSBarry Smith 
515c6c1daeSBarry Smith   PetscFunctionBegin;
525c6c1daeSBarry Smith   if (vascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"ASCII PetscViewer destroyed before restoring singleton or subcomm PetscViewer");
535c6c1daeSBarry Smith   ierr = PetscViewerFileClose_ASCII(viewer);CHKERRQ(ierr);
545c6c1daeSBarry Smith   ierr = PetscFree(vascii);CHKERRQ(ierr);
555c6c1daeSBarry Smith 
565c6c1daeSBarry Smith   /* remove the viewer from the list in the MPI Communicator */
575c6c1daeSBarry Smith   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
585c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);CHKERRQ(ierr);
595c6c1daeSBarry Smith   }
605c6c1daeSBarry Smith 
615c6c1daeSBarry Smith   ierr = MPI_Attr_get(((PetscObject)viewer)->comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
625c6c1daeSBarry Smith   if (flg) {
635c6c1daeSBarry Smith     if (vlink && vlink->viewer == viewer) {
645c6c1daeSBarry Smith       ierr = MPI_Attr_put(((PetscObject)viewer)->comm,Petsc_Viewer_keyval,vlink->next);CHKERRQ(ierr);
655c6c1daeSBarry Smith       ierr = PetscFree(vlink);CHKERRQ(ierr);
665c6c1daeSBarry Smith     } else {
675c6c1daeSBarry Smith       while (vlink && vlink->next) {
685c6c1daeSBarry Smith         if (vlink->next->viewer == viewer) {
695c6c1daeSBarry Smith           PetscViewerLink *nv = vlink->next;
705c6c1daeSBarry Smith           vlink->next = vlink->next->next;
715c6c1daeSBarry Smith           ierr = PetscFree(nv);CHKERRQ(ierr);
725c6c1daeSBarry Smith         }
735c6c1daeSBarry Smith         vlink = vlink->next;
745c6c1daeSBarry Smith       }
755c6c1daeSBarry Smith     }
765c6c1daeSBarry Smith   }
775c6c1daeSBarry Smith   PetscFunctionReturn(0);
785c6c1daeSBarry Smith }
795c6c1daeSBarry Smith 
805c6c1daeSBarry Smith #undef __FUNCT__
815c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_ASCII_Singleton"
825c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_ASCII_Singleton(PetscViewer viewer)
835c6c1daeSBarry Smith {
845c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
855c6c1daeSBarry Smith   PetscErrorCode    ierr;
865fd66863SKarl Rupp 
875c6c1daeSBarry Smith   PetscFunctionBegin;
885c6c1daeSBarry Smith   ierr = PetscViewerRestoreSingleton(vascii->bviewer,&viewer);CHKERRQ(ierr);
895c6c1daeSBarry Smith   PetscFunctionReturn(0);
905c6c1daeSBarry Smith }
915c6c1daeSBarry Smith 
925c6c1daeSBarry Smith #undef __FUNCT__
935c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_ASCII_Subcomm"
945c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_ASCII_Subcomm(PetscViewer viewer)
955c6c1daeSBarry Smith {
965c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
975c6c1daeSBarry Smith   PetscErrorCode    ierr;
985fd66863SKarl Rupp 
995c6c1daeSBarry Smith   PetscFunctionBegin;
1005c6c1daeSBarry Smith   ierr = PetscViewerRestoreSubcomm(vascii->bviewer,((PetscObject)viewer)->comm,&viewer);CHKERRQ(ierr);
1015c6c1daeSBarry Smith   PetscFunctionReturn(0);
1025c6c1daeSBarry Smith }
1035c6c1daeSBarry Smith 
1045c6c1daeSBarry Smith #undef __FUNCT__
1055c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFlush_ASCII_Singleton_0"
1065c6c1daeSBarry Smith PetscErrorCode PetscViewerFlush_ASCII_Singleton_0(PetscViewer viewer)
1075c6c1daeSBarry Smith {
1085c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
1095c6c1daeSBarry Smith   int               err;
1105c6c1daeSBarry Smith 
1115c6c1daeSBarry Smith   PetscFunctionBegin;
1125c6c1daeSBarry Smith   err = fflush(vascii->fd);
1135c6c1daeSBarry Smith   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
1145c6c1daeSBarry Smith   PetscFunctionReturn(0);
1155c6c1daeSBarry Smith }
1165c6c1daeSBarry Smith 
1175c6c1daeSBarry Smith #undef __FUNCT__
1185c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFlush_ASCII"
1195c6c1daeSBarry Smith PetscErrorCode PetscViewerFlush_ASCII(PetscViewer viewer)
1205c6c1daeSBarry Smith {
1215c6c1daeSBarry Smith   PetscMPIInt       rank;
1225c6c1daeSBarry Smith   PetscErrorCode    ierr;
1235c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
1245c6c1daeSBarry Smith   int               err;
1255c6c1daeSBarry Smith 
1265c6c1daeSBarry Smith   PetscFunctionBegin;
1275c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
1285c6c1daeSBarry Smith   /* fflush() fails on OSX for read-only descriptors */
1295c6c1daeSBarry Smith   if (!rank && (vascii->mode != FILE_MODE_READ)) {
1305c6c1daeSBarry Smith     err = fflush(vascii->fd);
1315c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() call failed");
1325c6c1daeSBarry Smith   }
1335c6c1daeSBarry Smith 
1345c6c1daeSBarry Smith   if (vascii->allowsynchronized) {
1355c6c1daeSBarry Smith     /* Also flush anything printed with PetscViewerASCIISynchronizedPrintf()  */
1365c6c1daeSBarry Smith     ierr = PetscSynchronizedFlush(((PetscObject)viewer)->comm);CHKERRQ(ierr);
1375c6c1daeSBarry Smith   }
1385c6c1daeSBarry Smith   PetscFunctionReturn(0);
1395c6c1daeSBarry Smith }
1405c6c1daeSBarry Smith 
1415c6c1daeSBarry Smith #undef __FUNCT__
1425c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIGetPointer"
1435c6c1daeSBarry Smith /*@C
1445c6c1daeSBarry Smith     PetscViewerASCIIGetPointer - Extracts the file pointer from an ASCII PetscViewer.
1455c6c1daeSBarry Smith 
1465c6c1daeSBarry Smith     Not Collective
1475c6c1daeSBarry Smith 
1485c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerASCIIOpen()
1495c6c1daeSBarry Smith -   fd - file pointer
1505c6c1daeSBarry Smith 
1515c6c1daeSBarry Smith     Level: intermediate
1525c6c1daeSBarry Smith 
1535c6c1daeSBarry Smith     Fortran Note:
1545c6c1daeSBarry Smith     This routine is not supported in Fortran.
1555c6c1daeSBarry Smith 
1565c6c1daeSBarry Smith   Concepts: PetscViewer^file pointer
1575c6c1daeSBarry Smith   Concepts: file pointer^getting from PetscViewer
1585c6c1daeSBarry Smith 
1595c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerCreate(), PetscViewerASCIIPrintf(),
1605c6c1daeSBarry Smith           PetscViewerASCIISynchronizedPrintf(), PetscViewerFlush()
1615c6c1daeSBarry Smith @*/
1625c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetPointer(PetscViewer viewer,FILE **fd)
1635c6c1daeSBarry Smith {
1645c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
1655c6c1daeSBarry Smith 
1665c6c1daeSBarry Smith   PetscFunctionBegin;
1675c6c1daeSBarry Smith   *fd = vascii->fd;
1685c6c1daeSBarry Smith   PetscFunctionReturn(0);
1695c6c1daeSBarry Smith }
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith EXTERN_C_BEGIN
1725c6c1daeSBarry Smith #undef __FUNCT__
1735c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetMode_ASCII"
1745c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetMode_ASCII(PetscViewer viewer, PetscFileMode *mode)
1755c6c1daeSBarry Smith {
1765c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
1775c6c1daeSBarry Smith 
1785c6c1daeSBarry Smith   PetscFunctionBegin;
1795c6c1daeSBarry Smith   *mode = vascii->mode;
1805c6c1daeSBarry Smith   PetscFunctionReturn(0);
1815c6c1daeSBarry Smith }
1825c6c1daeSBarry Smith EXTERN_C_END
1835c6c1daeSBarry Smith 
1845c6c1daeSBarry Smith /*@C
1855c6c1daeSBarry Smith     PetscViewerFileSetMode - Sets the mode in which to open the file.
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith     Not Collective
1885c6c1daeSBarry Smith 
1895c6c1daeSBarry Smith +   viewer - viewer context, obtained from PetscViewerCreate()
1905c6c1daeSBarry Smith -   mode   - The file mode
1915c6c1daeSBarry Smith 
1925c6c1daeSBarry Smith     Level: intermediate
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith     Fortran Note:
1955c6c1daeSBarry Smith     This routine is not supported in Fortran.
1965c6c1daeSBarry Smith 
1975c6c1daeSBarry Smith .keywords: Viewer, file, get, pointer
1985c6c1daeSBarry Smith 
1995c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerBinaryOpen()
2005c6c1daeSBarry Smith @*/
2015c6c1daeSBarry Smith 
2025c6c1daeSBarry Smith EXTERN_C_BEGIN
2035c6c1daeSBarry Smith #undef __FUNCT__
2045c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode_ASCII"
2055c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetMode_ASCII(PetscViewer viewer, PetscFileMode mode)
2065c6c1daeSBarry Smith {
2075c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
2085c6c1daeSBarry Smith 
2095c6c1daeSBarry Smith   PetscFunctionBegin;
2105c6c1daeSBarry Smith   vascii->mode = mode;
2115c6c1daeSBarry Smith   PetscFunctionReturn(0);
2125c6c1daeSBarry Smith }
2135c6c1daeSBarry Smith EXTERN_C_END
2145c6c1daeSBarry Smith 
2155c6c1daeSBarry Smith /*
2165c6c1daeSBarry Smith    If petsc_history is on, then all Petsc*Printf() results are saved
2175c6c1daeSBarry Smith    if the appropriate (usually .petschistory) file.
2185c6c1daeSBarry Smith */
2195c6c1daeSBarry Smith extern FILE *petsc_history;
2205c6c1daeSBarry Smith 
2215c6c1daeSBarry Smith #undef __FUNCT__
2225c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIISetTab"
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:
2295c6c1daeSBarry Smith +    viewer - optained 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 #undef __FUNCT__
2585c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIGetTab"
2595c6c1daeSBarry Smith /*@
2605c6c1daeSBarry Smith     PetscViewerASCIIGetTab - Return the number of tabs used by PetscViewer.
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith     Not Collective, meaningful on first processor only.
2635c6c1daeSBarry Smith 
2645c6c1daeSBarry Smith     Input Parameters:
2655c6c1daeSBarry Smith .    viewer - optained with PetscViewerASCIIOpen()
2665c6c1daeSBarry Smith     Output Parameters:
2675c6c1daeSBarry Smith .    tabs - number of tabs
2685c6c1daeSBarry Smith 
2695c6c1daeSBarry Smith     Level: developer
2705c6c1daeSBarry Smith 
2715c6c1daeSBarry Smith     Fortran Note:
2725c6c1daeSBarry Smith     This routine is not supported in Fortran.
2735c6c1daeSBarry Smith 
2745c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
2755c6c1daeSBarry Smith   Concepts: tab^retrieval
2765c6c1daeSBarry Smith 
2775c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISetTab(),
2785c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
2795c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
2805c6c1daeSBarry Smith @*/
2815c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetTab(PetscViewer viewer,PetscInt *tabs)
2825c6c1daeSBarry Smith {
2835c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
2845c6c1daeSBarry Smith   PetscBool         iascii;
2855c6c1daeSBarry Smith   PetscErrorCode    ierr;
2865c6c1daeSBarry Smith 
2875c6c1daeSBarry Smith   PetscFunctionBegin;
2885c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2895c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
290a297a907SKarl Rupp   if (iascii && tabs) *tabs = ascii->tab;
2915c6c1daeSBarry Smith   PetscFunctionReturn(0);
2925c6c1daeSBarry Smith }
2935c6c1daeSBarry Smith 
2945c6c1daeSBarry Smith #undef __FUNCT__
2955c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIAddTab"
2965c6c1daeSBarry Smith /*@
2975c6c1daeSBarry Smith     PetscViewerASCIIAddTab - Add to the number of times an ASCII viewer tabs before printing
2985c6c1daeSBarry Smith 
2995c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
3005c6c1daeSBarry Smith 
3015c6c1daeSBarry Smith     Input Parameters:
3025c6c1daeSBarry Smith +    viewer - optained with PetscViewerASCIIOpen()
3035c6c1daeSBarry Smith -    tabs - number of tabs
3045c6c1daeSBarry Smith 
3055c6c1daeSBarry Smith     Level: developer
3065c6c1daeSBarry Smith 
3075c6c1daeSBarry Smith     Fortran Note:
3085c6c1daeSBarry Smith     This routine is not supported in Fortran.
3095c6c1daeSBarry Smith 
3105c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
3115c6c1daeSBarry Smith   Concepts: tab^setting
3125c6c1daeSBarry Smith 
3135c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
3145c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
3155c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
3165c6c1daeSBarry Smith @*/
3175c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIAddTab(PetscViewer viewer,PetscInt tabs)
3185c6c1daeSBarry Smith {
3195c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
3205c6c1daeSBarry Smith   PetscBool         iascii;
3215c6c1daeSBarry Smith   PetscErrorCode    ierr;
3225c6c1daeSBarry Smith 
3235c6c1daeSBarry Smith   PetscFunctionBegin;
3245c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3255c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
326a297a907SKarl Rupp   if (iascii) ascii->tab += tabs;
3275c6c1daeSBarry Smith   PetscFunctionReturn(0);
3285c6c1daeSBarry Smith }
3295c6c1daeSBarry Smith 
3305c6c1daeSBarry Smith #undef __FUNCT__
3315c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIISubtractTab"
3325c6c1daeSBarry Smith /*@
3335c6c1daeSBarry Smith     PetscViewerASCIISubtractTab - Subtracts from the number of times an ASCII viewer tabs before printing
3345c6c1daeSBarry Smith 
3355c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
3365c6c1daeSBarry Smith 
3375c6c1daeSBarry Smith     Input Parameters:
3385c6c1daeSBarry Smith +    viewer - optained with PetscViewerASCIIOpen()
3395c6c1daeSBarry Smith -    tabs - number of tabs
3405c6c1daeSBarry Smith 
3415c6c1daeSBarry Smith     Level: developer
3425c6c1daeSBarry Smith 
3435c6c1daeSBarry Smith     Fortran Note:
3445c6c1daeSBarry Smith     This routine is not supported in Fortran.
3455c6c1daeSBarry Smith 
3465c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
3475c6c1daeSBarry Smith   Concepts: tab^setting
3485c6c1daeSBarry Smith 
3495c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
3505c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
3515c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
3525c6c1daeSBarry Smith @*/
3535c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISubtractTab(PetscViewer viewer,PetscInt tabs)
3545c6c1daeSBarry Smith {
3555c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
3565c6c1daeSBarry Smith   PetscBool         iascii;
3575c6c1daeSBarry Smith   PetscErrorCode    ierr;
3585c6c1daeSBarry Smith 
3595c6c1daeSBarry Smith   PetscFunctionBegin;
3605c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3615c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
362a297a907SKarl Rupp   if (iascii) ascii->tab -= tabs;
3635c6c1daeSBarry Smith   PetscFunctionReturn(0);
3645c6c1daeSBarry Smith }
3655c6c1daeSBarry Smith 
3665c6c1daeSBarry Smith #undef __FUNCT__
3675c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIISynchronizedAllow"
3685c6c1daeSBarry Smith /*@C
3695c6c1daeSBarry Smith     PetscViewerASCIISynchronizedAllow - Allows calls to PetscViewerASCIISynchronizedPrintf() for this viewer
3705c6c1daeSBarry Smith 
3715c6c1daeSBarry Smith     Collective on PetscViewer
3725c6c1daeSBarry Smith 
3735c6c1daeSBarry Smith     Input Parameters:
3745c6c1daeSBarry Smith +    viewer - optained with PetscViewerASCIIOpen()
3755c6c1daeSBarry Smith -    allow - PETSC_TRUE to allow the synchronized printing
3765c6c1daeSBarry Smith 
3775c6c1daeSBarry Smith     Level: intermediate
3785c6c1daeSBarry Smith 
3795c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
3805c6c1daeSBarry Smith   Concepts: tab^setting
3815c6c1daeSBarry Smith 
3825c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
3835c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
3845c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
3855c6c1daeSBarry Smith @*/
3865c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISynchronizedAllow(PetscViewer viewer,PetscBool allow)
3875c6c1daeSBarry Smith {
3885c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
3895c6c1daeSBarry Smith   PetscBool         iascii;
3905c6c1daeSBarry Smith   PetscErrorCode    ierr;
3915c6c1daeSBarry Smith 
3925c6c1daeSBarry Smith   PetscFunctionBegin;
3935c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3945c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
395a297a907SKarl Rupp   if (iascii) ascii->allowsynchronized = allow;
3965c6c1daeSBarry Smith   PetscFunctionReturn(0);
3975c6c1daeSBarry Smith }
3985c6c1daeSBarry Smith 
3995c6c1daeSBarry Smith #undef __FUNCT__
4005c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIPushTab"
4015c6c1daeSBarry Smith /*@
4025c6c1daeSBarry Smith     PetscViewerASCIIPushTab - Adds one more tab to the amount that PetscViewerASCIIPrintf()
4035c6c1daeSBarry Smith      lines are tabbed.
4045c6c1daeSBarry Smith 
4055c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
4065c6c1daeSBarry Smith 
4075c6c1daeSBarry Smith     Input Parameters:
4085c6c1daeSBarry Smith .    viewer - optained with PetscViewerASCIIOpen()
4095c6c1daeSBarry Smith 
4105c6c1daeSBarry Smith     Level: developer
4115c6c1daeSBarry Smith 
4125c6c1daeSBarry Smith     Fortran Note:
4135c6c1daeSBarry Smith     This routine is not supported in Fortran.
4145c6c1daeSBarry Smith 
4155c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
4165c6c1daeSBarry Smith   Concepts: tab^setting
4175c6c1daeSBarry Smith 
4185c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
4195c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
4205c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
4215c6c1daeSBarry Smith @*/
4225c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPushTab(PetscViewer viewer)
4235c6c1daeSBarry Smith {
4245c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
4255c6c1daeSBarry Smith   PetscBool         iascii;
4265c6c1daeSBarry Smith   PetscErrorCode    ierr;
4275c6c1daeSBarry Smith 
4285c6c1daeSBarry Smith   PetscFunctionBegin;
4295c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4305c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
431a297a907SKarl Rupp   if (iascii) ascii->tab++;
4325c6c1daeSBarry Smith   PetscFunctionReturn(0);
4335c6c1daeSBarry Smith }
4345c6c1daeSBarry Smith 
4355c6c1daeSBarry Smith #undef __FUNCT__
4365c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIPopTab"
4375c6c1daeSBarry Smith /*@
4385c6c1daeSBarry Smith     PetscViewerASCIIPopTab - Removes one tab from the amount that PetscViewerASCIIPrintf()
4395c6c1daeSBarry Smith      lines are tabbed.
4405c6c1daeSBarry Smith 
4415c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
4425c6c1daeSBarry Smith 
4435c6c1daeSBarry Smith     Input Parameters:
4445c6c1daeSBarry Smith .    viewer - optained with PetscViewerASCIIOpen()
4455c6c1daeSBarry Smith 
4465c6c1daeSBarry Smith     Level: developer
4475c6c1daeSBarry Smith 
4485c6c1daeSBarry Smith     Fortran Note:
4495c6c1daeSBarry Smith     This routine is not supported in Fortran.
4505c6c1daeSBarry Smith 
4515c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
4525c6c1daeSBarry Smith   Concepts: tab^setting
4535c6c1daeSBarry Smith 
4545c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
4555c6c1daeSBarry Smith           PetscViewerASCIIPushTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
4565c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
4575c6c1daeSBarry Smith @*/
4585c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPopTab(PetscViewer viewer)
4595c6c1daeSBarry Smith {
4605c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
4615c6c1daeSBarry Smith   PetscErrorCode    ierr;
4625c6c1daeSBarry Smith   PetscBool         iascii;
4635c6c1daeSBarry Smith 
4645c6c1daeSBarry Smith   PetscFunctionBegin;
4655c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4665c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
4675c6c1daeSBarry Smith   if (iascii) {
4685c6c1daeSBarry Smith     if (ascii->tab <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"More tabs popped than pushed");
4695c6c1daeSBarry Smith     ascii->tab--;
4705c6c1daeSBarry Smith   }
4715c6c1daeSBarry Smith   PetscFunctionReturn(0);
4725c6c1daeSBarry Smith }
4735c6c1daeSBarry Smith 
4745c6c1daeSBarry Smith #undef __FUNCT__
4755c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIUseTabs"
4765c6c1daeSBarry Smith /*@
4775c6c1daeSBarry Smith     PetscViewerASCIIUseTabs - Turns on or off the use of tabs with the ASCII PetscViewer
4785c6c1daeSBarry Smith 
4795c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
4805c6c1daeSBarry Smith 
4815c6c1daeSBarry Smith     Input Parameters:
4825c6c1daeSBarry Smith +    viewer - optained with PetscViewerASCIIOpen()
4835c6c1daeSBarry Smith -    flg - PETSC_TRUE or PETSC_FALSE
4845c6c1daeSBarry Smith 
4855c6c1daeSBarry Smith     Level: developer
4865c6c1daeSBarry Smith 
4875c6c1daeSBarry Smith     Fortran Note:
4885c6c1daeSBarry Smith     This routine is not supported in Fortran.
4895c6c1daeSBarry Smith 
4905c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
4915c6c1daeSBarry Smith   Concepts: tab^setting
4925c6c1daeSBarry Smith 
4935c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
4945c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPushTab(), PetscViewerASCIIOpen(),
4955c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
4965c6c1daeSBarry Smith @*/
4975c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIUseTabs(PetscViewer viewer,PetscBool flg)
4985c6c1daeSBarry Smith {
4995c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
5005c6c1daeSBarry Smith   PetscBool         iascii;
5015c6c1daeSBarry Smith   PetscErrorCode    ierr;
5025c6c1daeSBarry Smith 
5035c6c1daeSBarry Smith   PetscFunctionBegin;
5045c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
5055c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5065c6c1daeSBarry Smith   if (iascii) {
507a297a907SKarl Rupp     if (flg) ascii->tab = ascii->tab_store;
508a297a907SKarl Rupp     else {
5095c6c1daeSBarry Smith       ascii->tab_store = ascii->tab;
5105c6c1daeSBarry Smith       ascii->tab       = 0;
5115c6c1daeSBarry Smith     }
5125c6c1daeSBarry Smith   }
5135c6c1daeSBarry Smith   PetscFunctionReturn(0);
5145c6c1daeSBarry Smith }
5155c6c1daeSBarry Smith 
5165c6c1daeSBarry Smith /* ----------------------------------------------------------------------- */
5175c6c1daeSBarry Smith 
5185c6c1daeSBarry Smith #include <../src/sys/fileio/mprint.h> /* defines the queue datastructures and variables */
5195c6c1daeSBarry Smith 
5205c6c1daeSBarry Smith #undef __FUNCT__
5215c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIPrintf"
5225c6c1daeSBarry Smith /*@C
5235c6c1daeSBarry Smith     PetscViewerASCIIPrintf - Prints to a file, only from the first
5245c6c1daeSBarry Smith     processor in the PetscViewer
5255c6c1daeSBarry Smith 
5265c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
5275c6c1daeSBarry Smith 
5285c6c1daeSBarry Smith     Input Parameters:
5295c6c1daeSBarry Smith +    viewer - optained with PetscViewerASCIIOpen()
5305c6c1daeSBarry Smith -    format - the usual printf() format string
5315c6c1daeSBarry Smith 
5325c6c1daeSBarry Smith     Level: developer
5335c6c1daeSBarry Smith 
5345c6c1daeSBarry Smith     Fortran Note:
5355c6c1daeSBarry Smith     The call sequence is PetscViewerASCIIPrintf(PetscViewer, character(*), int ierr) from Fortran.
5365c6c1daeSBarry Smith     That is, you can only pass a single character string from Fortran.
5375c6c1daeSBarry Smith 
5385c6c1daeSBarry Smith   Concepts: PetscViewerASCII^printing
5395c6c1daeSBarry Smith   Concepts: printing^to file
5405c6c1daeSBarry Smith   Concepts: printf
5415c6c1daeSBarry Smith 
5425c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIOpen(),
5435c6c1daeSBarry Smith           PetscViewerASCIIPushTab(), PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(),
5445c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIISynchronizedAllow()
5455c6c1daeSBarry Smith @*/
5465c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPrintf(PetscViewer viewer,const char format[],...)
5475c6c1daeSBarry Smith {
5485c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
5495c6c1daeSBarry Smith   PetscMPIInt       rank;
5505c6c1daeSBarry Smith   PetscInt          tab;
5515c6c1daeSBarry Smith   PetscErrorCode    ierr;
5525c6c1daeSBarry Smith   FILE              *fd = ascii->fd;
5535c6c1daeSBarry Smith   PetscBool         iascii;
5545c6c1daeSBarry Smith   int               err;
5555c6c1daeSBarry Smith 
5565c6c1daeSBarry Smith   PetscFunctionBegin;
5575c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
5585c6c1daeSBarry Smith   PetscValidCharPointer(format,2);
5595c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5605c6c1daeSBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer");
5615c6c1daeSBarry Smith 
5625c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
5635c6c1daeSBarry Smith   if (!rank) {
5645c6c1daeSBarry Smith     va_list Argp;
565a297a907SKarl Rupp     if (ascii->bviewer) petsc_printfqueuefile = fd;
5665c6c1daeSBarry Smith 
5675c6c1daeSBarry Smith     tab = ascii->tab;
568a297a907SKarl Rupp     while (tab--) {
569a297a907SKarl Rupp       ierr = PetscFPrintf(PETSC_COMM_SELF,fd,"  ");CHKERRQ(ierr);
570a297a907SKarl Rupp     }
5715c6c1daeSBarry Smith 
5725c6c1daeSBarry Smith     va_start(Argp,format);
5735c6c1daeSBarry Smith     ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr);
5745c6c1daeSBarry Smith     err  = fflush(fd);
5755c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
5765c6c1daeSBarry Smith     if (petsc_history) {
5775c6c1daeSBarry Smith       va_start(Argp,format);
5785c6c1daeSBarry Smith       tab = ascii->tab;
579a297a907SKarl Rupp       while (tab--) {
580a297a907SKarl Rupp         ierr = PetscFPrintf(PETSC_COMM_SELF,fd,"  ");CHKERRQ(ierr);
581a297a907SKarl Rupp       }
5825c6c1daeSBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
5835c6c1daeSBarry Smith       err  = fflush(petsc_history);
5845c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
5855c6c1daeSBarry Smith     }
5865c6c1daeSBarry Smith     va_end(Argp);
5875c6c1daeSBarry Smith   }
5885c6c1daeSBarry Smith   PetscFunctionReturn(0);
5895c6c1daeSBarry Smith }
5905c6c1daeSBarry Smith 
5915c6c1daeSBarry Smith #undef __FUNCT__
5925c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName"
5935c6c1daeSBarry Smith /*@C
5945c6c1daeSBarry Smith      PetscViewerFileSetName - Sets the name of the file the PetscViewer uses.
5955c6c1daeSBarry Smith 
5965c6c1daeSBarry Smith     Collective on PetscViewer
5975c6c1daeSBarry Smith 
5985c6c1daeSBarry Smith   Input Parameters:
5995c6c1daeSBarry Smith +  viewer - the PetscViewer; either ASCII or binary
6005c6c1daeSBarry Smith -  name - the name of the file it should use
6015c6c1daeSBarry Smith 
6025c6c1daeSBarry Smith     Level: advanced
6035c6c1daeSBarry Smith 
6045c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerDestroy(),
6055c6c1daeSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf()
6065c6c1daeSBarry Smith 
6075c6c1daeSBarry Smith @*/
6085c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName(PetscViewer viewer,const char name[])
6095c6c1daeSBarry Smith {
6105c6c1daeSBarry Smith   PetscErrorCode ierr;
6115c6c1daeSBarry Smith 
6125c6c1daeSBarry Smith   PetscFunctionBegin;
6135c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
6145c6c1daeSBarry Smith   PetscValidCharPointer(name,2);
6155c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerFileSetName_C",(PetscViewer,const char[]),(viewer,name));CHKERRQ(ierr);
6165c6c1daeSBarry Smith   PetscFunctionReturn(0);
6175c6c1daeSBarry Smith }
6185c6c1daeSBarry Smith 
6195c6c1daeSBarry Smith #undef __FUNCT__
6205c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetName"
6215c6c1daeSBarry Smith /*@C
6225c6c1daeSBarry Smith      PetscViewerFileGetName - Gets the name of the file the PetscViewer uses.
6235c6c1daeSBarry Smith 
6245c6c1daeSBarry Smith     Not Collective
6255c6c1daeSBarry Smith 
6265c6c1daeSBarry Smith   Input Parameter:
6275c6c1daeSBarry Smith .  viewer - the PetscViewer; either ASCII or binary
6285c6c1daeSBarry Smith 
6295c6c1daeSBarry Smith   Output Parameter:
6305c6c1daeSBarry Smith .  name - the name of the file it is using
6315c6c1daeSBarry Smith 
6325c6c1daeSBarry Smith     Level: advanced
6335c6c1daeSBarry Smith 
6345c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerFileSetName()
6355c6c1daeSBarry Smith 
6365c6c1daeSBarry Smith @*/
6375c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetName(PetscViewer viewer,const char **name)
6385c6c1daeSBarry Smith {
6395c6c1daeSBarry Smith   PetscErrorCode ierr;
6405c6c1daeSBarry Smith 
6415c6c1daeSBarry Smith   PetscFunctionBegin;
6425c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
6435c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerFileGetName_C",(PetscViewer,const char**),(viewer,name));CHKERRQ(ierr);
6445c6c1daeSBarry Smith   PetscFunctionReturn(0);
6455c6c1daeSBarry Smith }
6465c6c1daeSBarry Smith 
6475c6c1daeSBarry Smith EXTERN_C_BEGIN
6485c6c1daeSBarry Smith #undef __FUNCT__
6495c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetName_ASCII"
6505c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetName_ASCII(PetscViewer viewer,const char **name)
6515c6c1daeSBarry Smith {
6525c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
6535c6c1daeSBarry Smith 
6545c6c1daeSBarry Smith   PetscFunctionBegin;
6555c6c1daeSBarry Smith   *name = vascii->filename;
6565c6c1daeSBarry Smith   PetscFunctionReturn(0);
6575c6c1daeSBarry Smith }
6585c6c1daeSBarry Smith EXTERN_C_END
6595c6c1daeSBarry Smith 
6605c6c1daeSBarry Smith 
6615c6c1daeSBarry Smith EXTERN_C_BEGIN
6625c6c1daeSBarry Smith #undef __FUNCT__
6635c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName_ASCII"
6645c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName_ASCII(PetscViewer viewer,const char name[])
6655c6c1daeSBarry Smith {
6665c6c1daeSBarry Smith   PetscErrorCode    ierr;
6675c6c1daeSBarry Smith   size_t            len;
6685c6c1daeSBarry Smith   char              fname[PETSC_MAX_PATH_LEN],*gz;
6695c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
6705c6c1daeSBarry Smith   PetscBool         isstderr,isstdout;
6715c6c1daeSBarry Smith   PetscMPIInt       rank;
6725c6c1daeSBarry Smith 
6735c6c1daeSBarry Smith   PetscFunctionBegin;
6745c6c1daeSBarry Smith   ierr = PetscViewerFileClose_ASCII(viewer);CHKERRQ(ierr);
6755c6c1daeSBarry Smith   if (!name) PetscFunctionReturn(0);
6765c6c1daeSBarry Smith   ierr = PetscStrallocpy(name,&vascii->filename);CHKERRQ(ierr);
6775c6c1daeSBarry Smith 
6785c6c1daeSBarry Smith   /* Is this file to be compressed */
6795c6c1daeSBarry Smith   vascii->storecompressed = PETSC_FALSE;
680a297a907SKarl Rupp 
6815c6c1daeSBarry Smith   ierr = PetscStrstr(vascii->filename,".gz",&gz);CHKERRQ(ierr);
6825c6c1daeSBarry Smith   if (gz) {
6835c6c1daeSBarry Smith     ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
6845c6c1daeSBarry Smith     if (len == 3) {
6855c6c1daeSBarry Smith       *gz = 0;
6865c6c1daeSBarry Smith       vascii->storecompressed = PETSC_TRUE;
6875c6c1daeSBarry Smith     }
6885c6c1daeSBarry Smith   }
6895c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
6905c6c1daeSBarry Smith   if (!rank) {
6915c6c1daeSBarry Smith     ierr = PetscStrcmp(name,"stderr",&isstderr);CHKERRQ(ierr);
6925c6c1daeSBarry Smith     ierr = PetscStrcmp(name,"stdout",&isstdout);CHKERRQ(ierr);
6935c6c1daeSBarry Smith     /* empty filename means stdout */
6945c6c1daeSBarry Smith     if (name[0] == 0)  isstdout = PETSC_TRUE;
6955c6c1daeSBarry Smith     if (isstderr)      vascii->fd = PETSC_STDERR;
6965c6c1daeSBarry Smith     else if (isstdout) vascii->fd = PETSC_STDOUT;
6975c6c1daeSBarry Smith     else {
6985c6c1daeSBarry Smith 
6995c6c1daeSBarry Smith 
7005c6c1daeSBarry Smith       ierr = PetscFixFilename(name,fname);CHKERRQ(ierr);
7015c6c1daeSBarry Smith       switch (vascii->mode) {
7025c6c1daeSBarry Smith       case FILE_MODE_READ:
7035c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r");
7045c6c1daeSBarry Smith         break;
7055c6c1daeSBarry Smith       case FILE_MODE_WRITE:
7065c6c1daeSBarry Smith         vascii->fd = fopen(fname,"w");
7075c6c1daeSBarry Smith         break;
7085c6c1daeSBarry Smith       case FILE_MODE_APPEND:
7095c6c1daeSBarry Smith         vascii->fd = fopen(fname,"a");
7105c6c1daeSBarry Smith         break;
7115c6c1daeSBarry Smith       case FILE_MODE_UPDATE:
7125c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r+");
713a297a907SKarl Rupp         if (!vascii->fd) vascii->fd = fopen(fname,"w+");
7145c6c1daeSBarry Smith         break;
7155c6c1daeSBarry Smith       case FILE_MODE_APPEND_UPDATE:
7165c6c1daeSBarry Smith         /* I really want a file which is opened at the end for updating,
7175c6c1daeSBarry Smith            not a+, which opens at the beginning, but makes writes at the end.
7185c6c1daeSBarry Smith         */
7195c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r+");
720a297a907SKarl Rupp         if (!vascii->fd) vascii->fd = fopen(fname,"w+");
721a297a907SKarl Rupp         else {
7225c6c1daeSBarry Smith           ierr     = fseek(vascii->fd, 0, SEEK_END);CHKERRQ(ierr);
7235c6c1daeSBarry Smith         }
7245c6c1daeSBarry Smith         break;
7255c6c1daeSBarry Smith       default:
7265c6c1daeSBarry Smith         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid file mode %d", vascii->mode);
7275c6c1daeSBarry Smith       }
7285c6c1daeSBarry Smith       if (!vascii->fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open PetscViewer file: %s",fname);
7295c6c1daeSBarry Smith     }
7305c6c1daeSBarry Smith   }
7315c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
7325c6c1daeSBarry Smith   PetscLogObjectState((PetscObject)viewer,"File: %s",name);
7335c6c1daeSBarry Smith #endif
7345c6c1daeSBarry Smith   PetscFunctionReturn(0);
7355c6c1daeSBarry Smith }
7365c6c1daeSBarry Smith EXTERN_C_END
7375c6c1daeSBarry Smith 
7385c6c1daeSBarry Smith #undef __FUNCT__
7395c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerGetSingleton_ASCII"
7405c6c1daeSBarry Smith PetscErrorCode PetscViewerGetSingleton_ASCII(PetscViewer viewer,PetscViewer *outviewer)
7415c6c1daeSBarry Smith {
7425c6c1daeSBarry Smith   PetscMPIInt       rank;
7435c6c1daeSBarry Smith   PetscErrorCode    ierr;
7445c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data,*ovascii;
7455c6c1daeSBarry Smith   const char        *name;
7465c6c1daeSBarry Smith 
7475c6c1daeSBarry Smith   PetscFunctionBegin;
7485c6c1daeSBarry Smith   if (vascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Singleton already obtained from PetscViewer and not restored");
7495c6c1daeSBarry Smith   ierr         = PetscViewerCreate(PETSC_COMM_SELF,outviewer);CHKERRQ(ierr);
7505c6c1daeSBarry Smith   ierr         = PetscViewerSetType(*outviewer,PETSCVIEWERASCII);CHKERRQ(ierr);
7515c6c1daeSBarry Smith   ovascii      = (PetscViewer_ASCII*)(*outviewer)->data;
7525c6c1daeSBarry Smith   ovascii->fd  = vascii->fd;
7535c6c1daeSBarry Smith   ovascii->tab = vascii->tab;
7545c6c1daeSBarry Smith 
7555c6c1daeSBarry Smith   vascii->sviewer = *outviewer;
7565c6c1daeSBarry Smith 
7575c6c1daeSBarry Smith   (*outviewer)->format  = viewer->format;
7585c6c1daeSBarry Smith   (*outviewer)->iformat = viewer->iformat;
7595c6c1daeSBarry Smith 
7605c6c1daeSBarry Smith   ierr = PetscObjectGetName((PetscObject)viewer,&name);CHKERRQ(ierr);
7615c6c1daeSBarry Smith   ierr = PetscObjectSetName((PetscObject)(*outviewer),name);CHKERRQ(ierr);
7625c6c1daeSBarry Smith 
7635c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
7645c6c1daeSBarry Smith   ((PetscViewer_ASCII*)((*outviewer)->data))->bviewer = viewer;
7655c6c1daeSBarry Smith   (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII_Singleton;
766a297a907SKarl Rupp   if (rank) (*outviewer)->ops->flush = 0;
767a297a907SKarl Rupp   else      (*outviewer)->ops->flush = PetscViewerFlush_ASCII_Singleton_0;
7685c6c1daeSBarry Smith   PetscFunctionReturn(0);
7695c6c1daeSBarry Smith }
7705c6c1daeSBarry Smith 
7715c6c1daeSBarry Smith #undef __FUNCT__
7725c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRestoreSingleton_ASCII"
7735c6c1daeSBarry Smith PetscErrorCode PetscViewerRestoreSingleton_ASCII(PetscViewer viewer,PetscViewer *outviewer)
7745c6c1daeSBarry Smith {
7755c6c1daeSBarry Smith   PetscErrorCode    ierr;
7765c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)(*outviewer)->data;
7775c6c1daeSBarry Smith   PetscViewer_ASCII *ascii  = (PetscViewer_ASCII*)viewer->data;
7785c6c1daeSBarry Smith 
7795c6c1daeSBarry Smith   PetscFunctionBegin;
7805c6c1daeSBarry Smith   if (!ascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Singleton never obtained from PetscViewer");
7815c6c1daeSBarry Smith   if (ascii->sviewer != *outviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"This PetscViewer did not generate singleton");
7825c6c1daeSBarry Smith 
7835c6c1daeSBarry Smith   ascii->sviewer             = 0;
7845c6c1daeSBarry Smith   vascii->fd                 = PETSC_STDOUT;
7855c6c1daeSBarry Smith   (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII;
7865c6c1daeSBarry Smith   ierr                       = PetscViewerDestroy(outviewer);CHKERRQ(ierr);
7875c6c1daeSBarry Smith   ierr                       = PetscViewerFlush(viewer);CHKERRQ(ierr);
7885c6c1daeSBarry Smith   PetscFunctionReturn(0);
7895c6c1daeSBarry Smith }
7905c6c1daeSBarry Smith 
7915c6c1daeSBarry Smith #undef __FUNCT__
7925c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerGetSubcomm_ASCII"
7935c6c1daeSBarry Smith PetscErrorCode PetscViewerGetSubcomm_ASCII(PetscViewer viewer,MPI_Comm subcomm,PetscViewer *outviewer)
7945c6c1daeSBarry Smith {
7955c6c1daeSBarry Smith   PetscErrorCode    ierr;
7965c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data,*ovascii;
7975c6c1daeSBarry Smith   const char        *name;
7985c6c1daeSBarry Smith 
7995c6c1daeSBarry Smith   PetscFunctionBegin;
8005c6c1daeSBarry Smith   if (vascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Subcomm viewer already obtained from PetscViewer and not restored");
8015c6c1daeSBarry Smith   /* Note that we need to open vascii->filename for the subcomm:
8025c6c1daeSBarry Smith      we can't count on reusing viewer's fd since the root in comm and subcomm may differ.
8035c6c1daeSBarry Smith      Further, if the subcomm happens to be the same as comm, PetscViewerASCIIOpen() will
8045c6c1daeSBarry Smith      will return the current viewer, having increfed it.
8055c6c1daeSBarry Smith    */
8065c6c1daeSBarry Smith   ierr = PetscViewerASCIIOpen(subcomm,vascii->filename, outviewer);CHKERRQ(ierr);
8075c6c1daeSBarry Smith   if (*outviewer == viewer) PetscFunctionReturn(0);
8085c6c1daeSBarry Smith   ovascii = (PetscViewer_ASCII*)(*outviewer)->data;
8095c6c1daeSBarry Smith 
8105c6c1daeSBarry Smith   ovascii->tab    = vascii->tab;
8115c6c1daeSBarry Smith   vascii->sviewer = *outviewer;
8125c6c1daeSBarry Smith 
8135c6c1daeSBarry Smith   (*outviewer)->format  = viewer->format;
8145c6c1daeSBarry Smith   (*outviewer)->iformat = viewer->iformat;
8155c6c1daeSBarry Smith 
8165c6c1daeSBarry Smith   ierr = PetscObjectGetName((PetscObject)viewer,&name);CHKERRQ(ierr);
8175c6c1daeSBarry Smith   ierr = PetscObjectSetName((PetscObject)(*outviewer),name);CHKERRQ(ierr);
8185c6c1daeSBarry Smith 
8195c6c1daeSBarry Smith   ((PetscViewer_ASCII*)((*outviewer)->data))->bviewer = viewer;
820a297a907SKarl Rupp 
8215c6c1daeSBarry Smith   (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII_Subcomm;
8225c6c1daeSBarry Smith   PetscFunctionReturn(0);
8235c6c1daeSBarry Smith }
8245c6c1daeSBarry Smith 
8255c6c1daeSBarry Smith #undef __FUNCT__
8265c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRestoreSubcomm_ASCII"
8275c6c1daeSBarry Smith PetscErrorCode PetscViewerRestoreSubcomm_ASCII(PetscViewer viewer,MPI_Comm subcomm,PetscViewer *outviewer)
8285c6c1daeSBarry Smith {
8295c6c1daeSBarry Smith   PetscErrorCode    ierr;
8305c6c1daeSBarry Smith   PetscViewer_ASCII *oascii = (PetscViewer_ASCII*)(*outviewer)->data;
8315c6c1daeSBarry Smith   PetscViewer_ASCII *ascii  = (PetscViewer_ASCII*)viewer->data;
8325c6c1daeSBarry Smith 
8335c6c1daeSBarry Smith   PetscFunctionBegin;
8345c6c1daeSBarry Smith   if (!ascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Subcomm never obtained from PetscViewer");
8355c6c1daeSBarry Smith   if (ascii->sviewer != *outviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"The given PetscViewer did not generate this subcomm viewer");
8365c6c1daeSBarry Smith 
8375c6c1daeSBarry Smith   ascii->sviewer             = 0;
8385c6c1daeSBarry Smith   oascii->fd                 = PETSC_STDOUT;
8395c6c1daeSBarry Smith   (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII;
840a297a907SKarl Rupp 
8415c6c1daeSBarry Smith   ierr = PetscViewerDestroy(outviewer);CHKERRQ(ierr);
8425c6c1daeSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
8435c6c1daeSBarry Smith   PetscFunctionReturn(0);
8445c6c1daeSBarry Smith }
8455c6c1daeSBarry Smith 
8462bf49c77SBarry Smith #undef __FUNCT__
8472bf49c77SBarry Smith #define __FUNCT__ "PetscViewerView_ASCII"
8482bf49c77SBarry Smith PetscErrorCode  PetscViewerView_ASCII(PetscViewer v,PetscViewer viewer)
8492bf49c77SBarry Smith {
8502bf49c77SBarry Smith   PetscErrorCode    ierr;
8512bf49c77SBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)v->data;
8522bf49c77SBarry Smith 
8532bf49c77SBarry Smith   PetscFunctionBegin;
8542bf49c77SBarry Smith   if (ascii->filename) {
8552bf49c77SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",ascii->filename);CHKERRQ(ierr);
8562bf49c77SBarry Smith   }
8572bf49c77SBarry Smith   PetscFunctionReturn(0);
8582bf49c77SBarry Smith }
8592bf49c77SBarry Smith 
8605c6c1daeSBarry Smith EXTERN_C_BEGIN
8615c6c1daeSBarry Smith #undef __FUNCT__
8625c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_ASCII"
8635c6c1daeSBarry Smith PetscErrorCode  PetscViewerCreate_ASCII(PetscViewer viewer)
8645c6c1daeSBarry Smith {
8655c6c1daeSBarry Smith   PetscViewer_ASCII *vascii;
8665c6c1daeSBarry Smith   PetscErrorCode    ierr;
8675c6c1daeSBarry Smith 
8685c6c1daeSBarry Smith   PetscFunctionBegin;
8695c6c1daeSBarry Smith   ierr         = PetscNewLog(viewer,PetscViewer_ASCII,&vascii);CHKERRQ(ierr);
8705c6c1daeSBarry Smith   viewer->data = (void*)vascii;
8715c6c1daeSBarry Smith 
8725c6c1daeSBarry Smith   viewer->ops->destroy          = PetscViewerDestroy_ASCII;
8735c6c1daeSBarry Smith   viewer->ops->flush            = PetscViewerFlush_ASCII;
8745c6c1daeSBarry Smith   viewer->ops->getsingleton     = PetscViewerGetSingleton_ASCII;
8755c6c1daeSBarry Smith   viewer->ops->restoresingleton = PetscViewerRestoreSingleton_ASCII;
8765c6c1daeSBarry Smith   viewer->ops->getsubcomm       = PetscViewerGetSubcomm_ASCII;
8775c6c1daeSBarry Smith   viewer->ops->restoresubcomm   = PetscViewerRestoreSubcomm_ASCII;
8782bf49c77SBarry Smith   viewer->ops->view             = PetscViewerView_ASCII;
8795c6c1daeSBarry Smith 
8805c6c1daeSBarry Smith   /* defaults to stdout unless set with PetscViewerFileSetName() */
8815c6c1daeSBarry Smith   vascii->fd        = PETSC_STDOUT;
8825c6c1daeSBarry Smith   vascii->mode      = FILE_MODE_WRITE;
8835c6c1daeSBarry Smith   vascii->bviewer   = 0;
8845c6c1daeSBarry Smith   vascii->sviewer   = 0;
8855c6c1daeSBarry Smith   viewer->format    = PETSC_VIEWER_DEFAULT;
8865c6c1daeSBarry Smith   viewer->iformat   = 0;
8875c6c1daeSBarry Smith   vascii->tab       = 0;
8885c6c1daeSBarry Smith   vascii->tab_store = 0;
8895c6c1daeSBarry Smith   vascii->filename  = 0;
8905c6c1daeSBarry Smith   vascii->closefile = PETSC_TRUE;
8915c6c1daeSBarry Smith 
8925c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileSetName_C","PetscViewerFileSetName_ASCII",
8935c6c1daeSBarry Smith                                            PetscViewerFileSetName_ASCII);CHKERRQ(ierr);
8945c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileGetName_C","PetscViewerFileGetName_ASCII",
8955c6c1daeSBarry Smith                                            PetscViewerFileGetName_ASCII);CHKERRQ(ierr);
8965c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileGetMode_C","PetscViewerFileGetMode_ASCII",
8975c6c1daeSBarry Smith                                            PetscViewerFileGetMode_ASCII);CHKERRQ(ierr);
8985c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileSetMode_C","PetscViewerFileSetMode_ASCII",
8995c6c1daeSBarry Smith                                            PetscViewerFileSetMode_ASCII);CHKERRQ(ierr);
9005c6c1daeSBarry Smith   PetscFunctionReturn(0);
9015c6c1daeSBarry Smith }
9025c6c1daeSBarry Smith EXTERN_C_END
9035c6c1daeSBarry Smith 
9045c6c1daeSBarry Smith 
9055c6c1daeSBarry Smith #undef __FUNCT__
9065c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIISynchronizedPrintf"
9075c6c1daeSBarry Smith /*@C
9085c6c1daeSBarry Smith     PetscViewerASCIISynchronizedPrintf - Prints synchronized output to the specified file from
9095c6c1daeSBarry Smith     several processors.  Output of the first processor is followed by that of the
9105c6c1daeSBarry Smith     second, etc.
9115c6c1daeSBarry Smith 
9125c6c1daeSBarry Smith     Not Collective, must call collective PetscViewerFlush() to get the results out
9135c6c1daeSBarry Smith 
9145c6c1daeSBarry Smith     Input Parameters:
9155c6c1daeSBarry Smith +   viewer - the ASCII PetscViewer
9165c6c1daeSBarry Smith -   format - the usual printf() format string
9175c6c1daeSBarry Smith 
9185c6c1daeSBarry Smith     Level: intermediate
9195c6c1daeSBarry Smith 
9205c6c1daeSBarry Smith     Notes: You must have previously called PetscViewerASCIISynchronizeAllow() to allow this routine to be called.
9215c6c1daeSBarry Smith 
9225c6c1daeSBarry Smith     Fortran Note:
9235c6c1daeSBarry Smith       Can only print a single character* string
9245c6c1daeSBarry Smith 
9255c6c1daeSBarry Smith .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(),
9265c6c1daeSBarry Smith           PetscFOpen(), PetscViewerFlush(), PetscViewerASCIIGetPointer(), PetscViewerDestroy(), PetscViewerASCIIOpen(),
9275c6c1daeSBarry Smith           PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedAllow()
9285c6c1daeSBarry Smith 
9295c6c1daeSBarry Smith @*/
9305c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISynchronizedPrintf(PetscViewer viewer,const char format[],...)
9315c6c1daeSBarry Smith {
9325c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
9335c6c1daeSBarry Smith   PetscErrorCode    ierr;
9345c6c1daeSBarry Smith   PetscMPIInt       rank,size;
9355c6c1daeSBarry Smith   PetscInt          tab = vascii->tab;
9365c6c1daeSBarry Smith   MPI_Comm          comm;
9375c6c1daeSBarry Smith   FILE              *fp;
9385c6c1daeSBarry Smith   PetscBool         iascii;
9395c6c1daeSBarry Smith   int               err;
9405c6c1daeSBarry Smith 
9415c6c1daeSBarry Smith   PetscFunctionBegin;
9425c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
9435c6c1daeSBarry Smith   PetscValidCharPointer(format,2);
9445c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
9455c6c1daeSBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer");
9465c6c1daeSBarry Smith   ierr = MPI_Comm_size(((PetscObject)viewer)->comm,&size);CHKERRQ(ierr);
9475c6c1daeSBarry Smith   if (size > 1 && !vascii->allowsynchronized) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"First call PetscViewerASCIISynchronizedAllow() to allow this call");
9485c6c1daeSBarry Smith   if (!viewer->ops->flush) PetscFunctionReturn(0); /* This viewer obtained via PetscViewerGetSubcomm_ASCII(), should not participate. */
9495c6c1daeSBarry Smith 
9505c6c1daeSBarry Smith   comm = ((PetscObject)viewer)->comm;
9515c6c1daeSBarry Smith   fp   = vascii->fd;
9525c6c1daeSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
9535c6c1daeSBarry Smith 
9545c6c1daeSBarry Smith   /* First processor prints immediately to fp */
9555c6c1daeSBarry Smith   if (!rank) {
9565c6c1daeSBarry Smith     va_list Argp;
9575c6c1daeSBarry Smith 
958a297a907SKarl Rupp     while (tab--) {
959a297a907SKarl Rupp       ierr = PetscFPrintf(PETSC_COMM_SELF,fp,"  ");CHKERRQ(ierr);
960a297a907SKarl Rupp     }
9615c6c1daeSBarry Smith 
9625c6c1daeSBarry Smith     va_start(Argp,format);
9635c6c1daeSBarry Smith     ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr);
9645c6c1daeSBarry Smith     err  = fflush(fp);
9655c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
9665c6c1daeSBarry Smith     petsc_printfqueuefile = fp;
9675c6c1daeSBarry Smith     if (petsc_history) {
9685c6c1daeSBarry Smith       va_start(Argp,format);
9695c6c1daeSBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
9705c6c1daeSBarry Smith       err  = fflush(petsc_history);
9715c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
9725c6c1daeSBarry Smith     }
9735c6c1daeSBarry Smith     va_end(Argp);
9745c6c1daeSBarry Smith   } else { /* other processors add to local queue */
9755c6c1daeSBarry Smith     char        *string;
9765c6c1daeSBarry Smith     va_list     Argp;
9775c6c1daeSBarry Smith     size_t      fullLength;
9785c6c1daeSBarry Smith     PrintfQueue next;
9795c6c1daeSBarry Smith 
9805c6c1daeSBarry Smith     ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr);
981a297a907SKarl Rupp     if (petsc_printfqueue) {
982a297a907SKarl Rupp       petsc_printfqueue->next = next;
983a297a907SKarl Rupp       petsc_printfqueue       = next;
984a297a907SKarl Rupp     } else {
985a297a907SKarl Rupp       petsc_printfqueuebase = petsc_printfqueue = next;
986a297a907SKarl Rupp     }
9875c6c1daeSBarry Smith     petsc_printfqueuelength++;
9885c6c1daeSBarry Smith     next->size = QUEUESTRINGSIZE;
9895c6c1daeSBarry Smith     ierr       = PetscMalloc(next->size*sizeof(char), &next->string);CHKERRQ(ierr);
9905c6c1daeSBarry Smith     ierr       = PetscMemzero(next->string,next->size);CHKERRQ(ierr);
9915c6c1daeSBarry Smith     string     = next->string;
9925c6c1daeSBarry Smith     tab       *= 2;
993a297a907SKarl Rupp     while (tab--) {
994a297a907SKarl Rupp       *string++ = ' ';
995a297a907SKarl Rupp     }
9965c6c1daeSBarry Smith     va_start(Argp,format);
99722d28d08SBarry Smith     ierr = PetscVSNPrintf(string,next->size-2*vascii->tab,format,&fullLength,Argp);CHKERRQ(ierr);
9985c6c1daeSBarry Smith     va_end(Argp);
9995c6c1daeSBarry Smith   }
10005c6c1daeSBarry Smith   PetscFunctionReturn(0);
10015c6c1daeSBarry Smith }
10025c6c1daeSBarry Smith 
10035c6c1daeSBarry Smith 
1004