xref: /petsc/src/sys/classes/viewer/impls/ascii/vcreatea.c (revision 2bf49c77dedd4b28e6efd10dcebd37482390fa09)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h>  /*I     "petscsys.h"   I*/
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
55c6c1daeSBarry Smith /*
65c6c1daeSBarry Smith     The variable Petsc_Viewer_Stdout_keyval is used to indicate an MPI attribute that
75c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
85c6c1daeSBarry Smith */
95c6c1daeSBarry Smith static PetscMPIInt Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;
105c6c1daeSBarry Smith 
115c6c1daeSBarry Smith #undef __FUNCT__
125c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIGetStdout"
135c6c1daeSBarry Smith /*@C
145c6c1daeSBarry Smith    PetscViewerASCIIGetStdout - Creates a ASCII PetscViewer shared by all processors
155c6c1daeSBarry Smith                     in a communicator. Error returning version of PETSC_VIEWER_STDOUT_()
165c6c1daeSBarry Smith 
175c6c1daeSBarry Smith    Collective on MPI_Comm
185c6c1daeSBarry Smith 
195c6c1daeSBarry Smith    Input Parameter:
205c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
215c6c1daeSBarry Smith 
225c6c1daeSBarry Smith    Level: beginner
235c6c1daeSBarry Smith 
245c6c1daeSBarry Smith    Notes:
255c6c1daeSBarry Smith      This should be used in all PETSc source code instead of PETSC_VIEWER_STDOUT_()
265c6c1daeSBarry Smith 
275c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
285c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_SELF
295c6c1daeSBarry Smith 
305c6c1daeSBarry Smith @*/
315c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetStdout(MPI_Comm comm,PetscViewer *viewer)
325c6c1daeSBarry Smith {
335c6c1daeSBarry Smith   PetscErrorCode ierr;
345c6c1daeSBarry Smith   PetscBool      flg;
355c6c1daeSBarry Smith   MPI_Comm       ncomm;
365c6c1daeSBarry Smith 
375c6c1daeSBarry Smith   PetscFunctionBegin;
385c6c1daeSBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,PETSC_NULL);CHKERRQ(ierr);
395c6c1daeSBarry Smith   if (Petsc_Viewer_Stdout_keyval == MPI_KEYVAL_INVALID) {
405c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stdout_keyval,0);CHKERRQ(ierr);
415c6c1daeSBarry Smith   }
425c6c1daeSBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Stdout_keyval,(void **)viewer,(PetscMPIInt*)&flg);CHKERRQ(ierr);
435c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
445c6c1daeSBarry Smith     ierr = PetscViewerASCIIOpen(ncomm,"stdout",viewer);CHKERRQ(ierr);
455c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)*viewer);CHKERRQ(ierr);
465c6c1daeSBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Stdout_keyval,(void*)*viewer);CHKERRQ(ierr);
475c6c1daeSBarry Smith   }
485c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);CHKERRQ(ierr);
495c6c1daeSBarry Smith   PetscFunctionReturn(0);
505c6c1daeSBarry Smith }
515c6c1daeSBarry Smith 
525c6c1daeSBarry Smith #undef __FUNCT__
535c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_STDOUT_"
545c6c1daeSBarry Smith /*@C
555c6c1daeSBarry Smith    PETSC_VIEWER_STDOUT_ - Creates a ASCII PetscViewer shared by all processors
565c6c1daeSBarry Smith                     in a communicator.
575c6c1daeSBarry Smith 
585c6c1daeSBarry Smith    Collective on MPI_Comm
595c6c1daeSBarry Smith 
605c6c1daeSBarry Smith    Input Parameter:
615c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
625c6c1daeSBarry Smith 
635c6c1daeSBarry Smith    Level: beginner
645c6c1daeSBarry Smith 
655c6c1daeSBarry Smith    Notes:
665c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
675c6c1daeSBarry Smith    an error code. Usually used in the form
685c6c1daeSBarry Smith $      XXXView(XXX object,PETSC_VIEWER_STDOUT_(comm));
695c6c1daeSBarry Smith 
705c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
715c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_SELF
725c6c1daeSBarry Smith 
735c6c1daeSBarry Smith @*/
745c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_STDOUT_(MPI_Comm comm)
755c6c1daeSBarry Smith {
765c6c1daeSBarry Smith   PetscErrorCode ierr;
775c6c1daeSBarry Smith   PetscViewer    viewer;
785c6c1daeSBarry Smith 
795c6c1daeSBarry Smith   PetscFunctionBegin;
805c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStdout(comm,&viewer);
815c6c1daeSBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," "); PetscFunctionReturn(0);}
825c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
835c6c1daeSBarry Smith }
845c6c1daeSBarry Smith 
855c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
865c6c1daeSBarry Smith /*
875c6c1daeSBarry Smith     The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
885c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
895c6c1daeSBarry Smith */
905c6c1daeSBarry Smith static PetscMPIInt Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;
915c6c1daeSBarry Smith 
925c6c1daeSBarry Smith #undef __FUNCT__
935c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIGetStderr"
945c6c1daeSBarry Smith /*@C
955c6c1daeSBarry Smith    PetscViewerASCIIGetStderr - Creates a ASCII PetscViewer shared by all processors
965c6c1daeSBarry Smith                     in a communicator. Error returning version of PETSC_VIEWER_STDERR_()
975c6c1daeSBarry Smith 
985c6c1daeSBarry Smith    Collective on MPI_Comm
995c6c1daeSBarry Smith 
1005c6c1daeSBarry Smith    Input Parameter:
1015c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
1025c6c1daeSBarry Smith 
1035c6c1daeSBarry Smith    Level: beginner
1045c6c1daeSBarry Smith 
1055c6c1daeSBarry Smith    Notes:
1065c6c1daeSBarry Smith      This should be used in all PETSc source code instead of PETSC_VIEWER_STDERR_()
1075c6c1daeSBarry Smith 
1085c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDERR_WORLD,
1095c6c1daeSBarry Smith           PETSC_VIEWER_STDERR_SELF
1105c6c1daeSBarry Smith 
1115c6c1daeSBarry Smith @*/
1125c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetStderr(MPI_Comm comm,PetscViewer *viewer)
1135c6c1daeSBarry Smith {
1145c6c1daeSBarry Smith   PetscErrorCode ierr;
1155c6c1daeSBarry Smith   PetscBool      flg;
1165c6c1daeSBarry Smith   MPI_Comm       ncomm;
1175c6c1daeSBarry Smith 
1185c6c1daeSBarry Smith   PetscFunctionBegin;
1195c6c1daeSBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,PETSC_NULL);CHKERRQ(ierr);
1205c6c1daeSBarry Smith   if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) {
1215c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stderr_keyval,0);CHKERRQ(ierr);
1225c6c1daeSBarry Smith   }
1235c6c1daeSBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Stderr_keyval,(void **)viewer,(PetscMPIInt*)&flg);CHKERRQ(ierr);
1245c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
1255c6c1daeSBarry Smith     ierr = PetscViewerASCIIOpen(ncomm,"stderr",viewer);CHKERRQ(ierr);
1265c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)*viewer);CHKERRQ(ierr);
1275c6c1daeSBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Stderr_keyval,(void*)*viewer);CHKERRQ(ierr);
1285c6c1daeSBarry Smith   }
1295c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);CHKERRQ(ierr);
1305c6c1daeSBarry Smith   PetscFunctionReturn(0);
1315c6c1daeSBarry Smith }
1325c6c1daeSBarry Smith 
1335c6c1daeSBarry Smith #undef __FUNCT__
1345c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_STDERR_"
1355c6c1daeSBarry Smith /*@C
1365c6c1daeSBarry Smith    PETSC_VIEWER_STDERR_ - Creates a ASCII PetscViewer shared by all processors
1375c6c1daeSBarry Smith                     in a communicator.
1385c6c1daeSBarry Smith 
1395c6c1daeSBarry Smith    Collective on MPI_Comm
1405c6c1daeSBarry Smith 
1415c6c1daeSBarry Smith    Input Parameter:
1425c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
1435c6c1daeSBarry Smith 
1445c6c1daeSBarry Smith    Level: beginner
1455c6c1daeSBarry Smith 
1465c6c1daeSBarry Smith    Note:
1475c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
1485c6c1daeSBarry Smith    an error code. Usually used in the form
1495c6c1daeSBarry Smith $      XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));
1505c6c1daeSBarry Smith 
1515c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_, PetscViewerASCIIOpen(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDOUT_WORLD,
1525c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDERR_WORLD, PETSC_VIEWER_STDERR_SELF
1535c6c1daeSBarry Smith @*/
1545c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_STDERR_(MPI_Comm comm)
1555c6c1daeSBarry Smith {
1565c6c1daeSBarry Smith   PetscErrorCode ierr;
1575c6c1daeSBarry Smith   PetscViewer    viewer;
1585c6c1daeSBarry Smith 
1595c6c1daeSBarry Smith   PetscFunctionBegin;
1605c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStderr(comm,&viewer);
1615c6c1daeSBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," "); PetscFunctionReturn(0);}
1625c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
1635c6c1daeSBarry Smith }
1645c6c1daeSBarry Smith 
1655c6c1daeSBarry Smith 
1665c6c1daeSBarry Smith PetscMPIInt Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
1675c6c1daeSBarry Smith EXTERN_C_BEGIN
1685c6c1daeSBarry Smith #undef __FUNCT__
1695c6c1daeSBarry Smith #define __FUNCT__ "Petsc_DelViewer"
1705c6c1daeSBarry Smith /*
1715c6c1daeSBarry Smith    Called with MPI_Comm_free() is called on a communicator that has a viewer as an attribute. The viewer is not actually destroyed because that is managed by
1725c6c1daeSBarry Smith    PetscObjectDestroyRegisterAll(). PetscViewerASCIIGetStdout() registers the viewer with PetscObjectDestroyRegister() to be destroyed when PetscFinalize() is called.
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith   This is called by MPI, not by users.
1755c6c1daeSBarry Smith 
1765c6c1daeSBarry Smith */
1775c6c1daeSBarry Smith PetscMPIInt  MPIAPI Petsc_DelViewer(MPI_Comm comm,PetscMPIInt keyval,void* attr_val,void* extra_state)
1785c6c1daeSBarry Smith {
1795c6c1daeSBarry Smith   PetscErrorCode ierr;
1805c6c1daeSBarry Smith 
1815c6c1daeSBarry Smith   PetscFunctionBegin;
1825c6c1daeSBarry Smith   ierr = PetscInfo1(0,"Removing viewer data attribute in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
1835c6c1daeSBarry Smith   PetscFunctionReturn(MPI_SUCCESS);
1845c6c1daeSBarry Smith }
1855c6c1daeSBarry Smith EXTERN_C_END
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith #undef __FUNCT__
1885c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIOpen"
1895c6c1daeSBarry Smith /*@C
1905c6c1daeSBarry Smith    PetscViewerASCIIOpen - Opens an ASCII file as a PetscViewer.
1915c6c1daeSBarry Smith 
1925c6c1daeSBarry Smith    Collective on MPI_Comm
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith    Input Parameters:
1955c6c1daeSBarry Smith +  comm - the communicator
1965c6c1daeSBarry Smith -  name - the file name
1975c6c1daeSBarry Smith 
1985c6c1daeSBarry Smith    Output Parameter:
1995c6c1daeSBarry Smith .  lab - the PetscViewer to use with the specified file
2005c6c1daeSBarry Smith 
2015c6c1daeSBarry Smith    Level: beginner
2025c6c1daeSBarry Smith 
2035c6c1daeSBarry Smith    Notes:
2045c6c1daeSBarry Smith    This PetscViewer can be destroyed with PetscViewerDestroy().
2055c6c1daeSBarry Smith 
2065c6c1daeSBarry Smith    If a multiprocessor communicator is used (such as PETSC_COMM_WORLD),
2075c6c1daeSBarry Smith    then only the first processor in the group opens the file.  All other
2085c6c1daeSBarry Smith    processors send their data to the first processor to print.
2095c6c1daeSBarry Smith 
2105c6c1daeSBarry Smith    Each processor can instead write its own independent output by
2115c6c1daeSBarry Smith    specifying the communicator PETSC_COMM_SELF.
2125c6c1daeSBarry Smith 
2135c6c1daeSBarry Smith    As shown below, PetscViewerASCIIOpen() is useful in conjunction with
2145c6c1daeSBarry Smith    MatView() and VecView()
2155c6c1daeSBarry Smith .vb
2165c6c1daeSBarry Smith      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
2175c6c1daeSBarry Smith      MatView(matrix,viewer);
2185c6c1daeSBarry Smith .ve
2195c6c1daeSBarry Smith 
2205c6c1daeSBarry Smith   Concepts: PetscViewerASCII^creating
2215c6c1daeSBarry Smith   Concepts: printf
2225c6c1daeSBarry Smith   Concepts: printing
2235c6c1daeSBarry Smith   Concepts: accessing remote file
2245c6c1daeSBarry Smith   Concepts: remote file
2255c6c1daeSBarry Smith 
2265c6c1daeSBarry Smith .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
2275c6c1daeSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
2285c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF,
2295c6c1daeSBarry Smith @*/
2305c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIOpen(MPI_Comm comm,const char name[],PetscViewer *lab)
2315c6c1daeSBarry Smith {
2325c6c1daeSBarry Smith   PetscErrorCode    ierr;
2335c6c1daeSBarry Smith   PetscViewerLink   *vlink,*nv;
2345c6c1daeSBarry Smith   PetscBool         flg,eq;
2355c6c1daeSBarry Smith   size_t            len;
2365c6c1daeSBarry Smith 
2375c6c1daeSBarry Smith   PetscFunctionBegin;
2385c6c1daeSBarry Smith   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
2395c6c1daeSBarry Smith   if (!len) {
2405c6c1daeSBarry Smith     ierr = PetscViewerASCIIGetStdout(comm,lab);CHKERRQ(ierr);
2415c6c1daeSBarry Smith     ierr = PetscObjectReference((PetscObject)*lab);CHKERRQ(ierr);
2425c6c1daeSBarry Smith     PetscFunctionReturn(0);
2435c6c1daeSBarry Smith   }
2445c6c1daeSBarry Smith   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
2455c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);CHKERRQ(ierr);
2465c6c1daeSBarry Smith   }
247*2bf49c77SBarry Smith   /*
248*2bf49c77SBarry Smith        It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
249*2bf49c77SBarry Smith      we cannot do that, since PetscFileSetName() takes a communicator that already exists.
250*2bf49c77SBarry Smith 
251*2bf49c77SBarry Smith       Plus if the original communicator that created the file has since been close this will not detect the old
252*2bf49c77SBarry Smith       communictor and hence will overwrite the old data. It may be better to simply remove all this code
253*2bf49c77SBarry Smith   */
2545c6c1daeSBarry Smith   /* make sure communicator is a PETSc communicator */
2555c6c1daeSBarry Smith   ierr = PetscCommDuplicate(comm,&comm,PETSC_NULL);CHKERRQ(ierr);
2565c6c1daeSBarry Smith   /* has file already been opened into a viewer */
2575c6c1daeSBarry Smith   ierr = MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
2585c6c1daeSBarry Smith   if (flg) {
2595c6c1daeSBarry Smith     while (vlink) {
2605c6c1daeSBarry Smith       ierr = PetscStrcmp(name,((PetscViewer_ASCII*)(vlink->viewer->data))->filename,&eq);CHKERRQ(ierr);
2615c6c1daeSBarry Smith       if (eq) {
2625c6c1daeSBarry Smith         ierr = PetscObjectReference((PetscObject)vlink->viewer);CHKERRQ(ierr);
2635c6c1daeSBarry Smith         *lab = vlink->viewer;
2645c6c1daeSBarry Smith         ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
2655c6c1daeSBarry Smith         PetscFunctionReturn(0);
2665c6c1daeSBarry Smith       }
2675c6c1daeSBarry Smith       vlink = vlink->next;
2685c6c1daeSBarry Smith     }
2695c6c1daeSBarry Smith   }
2705c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
2715c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERASCII);CHKERRQ(ierr);
2725c6c1daeSBarry Smith   if (name) {
2735c6c1daeSBarry Smith     ierr = PetscViewerFileSetName(*lab,name);CHKERRQ(ierr);
2745c6c1daeSBarry Smith   }
2755c6c1daeSBarry Smith   /* save viewer into communicator if needed later */
2765c6c1daeSBarry Smith   ierr       = PetscNew(PetscViewerLink,&nv);CHKERRQ(ierr);
2775c6c1daeSBarry Smith   nv->viewer = *lab;
2785c6c1daeSBarry Smith   if (!flg) {
2795c6c1daeSBarry Smith     ierr = MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);CHKERRQ(ierr);
2805c6c1daeSBarry Smith   } else {
2815c6c1daeSBarry Smith     ierr = MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
2825c6c1daeSBarry Smith     if (vlink) {
2835c6c1daeSBarry Smith       while (vlink->next) vlink = vlink->next;
2845c6c1daeSBarry Smith       vlink->next = nv;
2855c6c1daeSBarry Smith     } else {
2865c6c1daeSBarry Smith       ierr = MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);CHKERRQ(ierr);
2875c6c1daeSBarry Smith     }
2885c6c1daeSBarry Smith   }
2895c6c1daeSBarry Smith   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
2905c6c1daeSBarry Smith   PetscFunctionReturn(0);
2915c6c1daeSBarry Smith }
2925c6c1daeSBarry Smith 
2935c6c1daeSBarry Smith #undef __FUNCT__
2945c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIOpenWithFILE"
2955c6c1daeSBarry Smith /*@C
2965c6c1daeSBarry Smith    PetscViewerASCIIOpenWithFILE - Given an open file creates an ASCII viewer that prints to it.
2975c6c1daeSBarry Smith 
2985c6c1daeSBarry Smith    Collective on MPI_Comm
2995c6c1daeSBarry Smith 
3005c6c1daeSBarry Smith    Input Parameters:
3015c6c1daeSBarry Smith +  comm - the communicator
3025c6c1daeSBarry Smith -  fd - the FILE pointer
3035c6c1daeSBarry Smith 
3045c6c1daeSBarry Smith    Output Parameter:
3055c6c1daeSBarry Smith .  lab - the PetscViewer to use with the specified file
3065c6c1daeSBarry Smith 
3075c6c1daeSBarry Smith    Level: beginner
3085c6c1daeSBarry Smith 
3095c6c1daeSBarry Smith    Notes:
3105c6c1daeSBarry Smith    This PetscViewer can be destroyed with PetscViewerDestroy(), but the fd will NOT be closed.
3115c6c1daeSBarry Smith 
3125c6c1daeSBarry Smith    If a multiprocessor communicator is used (such as PETSC_COMM_WORLD),
3135c6c1daeSBarry Smith    then only the first processor in the group uses the file.  All other
3145c6c1daeSBarry Smith    processors send their data to the first processor to print.
3155c6c1daeSBarry Smith 
3165c6c1daeSBarry Smith   Concepts: PetscViewerASCII^creating
3175c6c1daeSBarry Smith   Concepts: printf
3185c6c1daeSBarry Smith   Concepts: printing
3195c6c1daeSBarry Smith   Concepts: accessing remote file
3205c6c1daeSBarry Smith   Concepts: remote file
3215c6c1daeSBarry Smith 
3225c6c1daeSBarry Smith .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
3235c6c1daeSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
3245c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF, PetscViewerASCIIOpen()
3255c6c1daeSBarry Smith @*/
3265c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIOpenWithFILE(MPI_Comm comm,FILE *fd,PetscViewer *lab)
3275c6c1daeSBarry Smith {
3285c6c1daeSBarry Smith   PetscErrorCode    ierr;
3295c6c1daeSBarry Smith 
3305c6c1daeSBarry Smith   PetscFunctionBegin;
3315c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
3325c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERASCII);CHKERRQ(ierr);
3335c6c1daeSBarry Smith   ierr = PetscViewerASCIISetFILE(*lab,fd);CHKERRQ(ierr);
3345c6c1daeSBarry Smith   PetscFunctionReturn(0);
3355c6c1daeSBarry Smith }
3365c6c1daeSBarry Smith 
3375c6c1daeSBarry Smith #undef __FUNCT__
3385c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIISetFILE"
3395c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISetFILE(PetscViewer viewer,FILE *fd)
3405c6c1daeSBarry Smith {
3415c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
3425c6c1daeSBarry Smith 
3435c6c1daeSBarry Smith   PetscFunctionBegin;
3445c6c1daeSBarry Smith   vascii->fd        = fd;
3455c6c1daeSBarry Smith   vascii->closefile = PETSC_FALSE;
3465c6c1daeSBarry Smith   PetscFunctionReturn(0);
3475c6c1daeSBarry Smith }
3485c6c1daeSBarry Smith 
3495c6c1daeSBarry Smith 
3505c6c1daeSBarry Smith 
351