xref: /petsc/src/sys/classes/viewer/impls/ascii/vcreatea.c (revision 6a9046bcf1dc7e213a87d3843bfa02f323786ad4)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h>  /*I     "petscsys.h"   I*/
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
55c6c1daeSBarry Smith 
6e2dcd6d3SBarry Smith /*
7e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stdout_keyval is used to indicate an MPI attribute that
8e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
9e2dcd6d3SBarry Smith */
10e2dcd6d3SBarry Smith static PetscMPIInt Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;
11e2dcd6d3SBarry Smith 
125c6c1daeSBarry Smith #undef __FUNCT__
135c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIGetStdout"
145c6c1daeSBarry Smith /*@C
155c6c1daeSBarry Smith    PetscViewerASCIIGetStdout - Creates a ASCII PetscViewer shared by all processors
165c6c1daeSBarry Smith                     in a communicator. Error returning version of PETSC_VIEWER_STDOUT_()
175c6c1daeSBarry Smith 
185c6c1daeSBarry Smith    Collective on MPI_Comm
195c6c1daeSBarry Smith 
205c6c1daeSBarry Smith    Input Parameter:
215c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
225c6c1daeSBarry Smith 
235c6c1daeSBarry Smith    Level: beginner
245c6c1daeSBarry Smith 
255c6c1daeSBarry Smith    Notes:
265c6c1daeSBarry Smith      This should be used in all PETSc source code instead of PETSC_VIEWER_STDOUT_()
275c6c1daeSBarry Smith 
285c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
295c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_SELF
305c6c1daeSBarry Smith 
315c6c1daeSBarry Smith @*/
325c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetStdout(MPI_Comm comm,PetscViewer *viewer)
335c6c1daeSBarry Smith {
345c6c1daeSBarry Smith   PetscErrorCode ierr;
35e2dcd6d3SBarry Smith   PetscBool      flg;
36e2dcd6d3SBarry Smith   MPI_Comm       ncomm;
375c6c1daeSBarry Smith 
385c6c1daeSBarry Smith   PetscFunctionBegin;
395ad9ad5bSBarry Smith   ierr = PetscSpinlockLock(&PetscViewerASCIISpinLockStdout);CHKERRQ(ierr);
40e2dcd6d3SBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,NULL);CHKERRQ(ierr);
41e2dcd6d3SBarry Smith   if (Petsc_Viewer_Stdout_keyval == MPI_KEYVAL_INVALID) {
42e2dcd6d3SBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stdout_keyval,0);CHKERRQ(ierr);
43e2dcd6d3SBarry Smith   }
44e2dcd6d3SBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Stdout_keyval,(void**)viewer,(PetscMPIInt*)&flg);CHKERRQ(ierr);
45e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
46e2dcd6d3SBarry Smith     ierr = PetscViewerASCIIOpen(ncomm,"stdout",viewer);CHKERRQ(ierr);
47e2dcd6d3SBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)*viewer);CHKERRQ(ierr);
48e2dcd6d3SBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Stdout_keyval,(void*)*viewer);CHKERRQ(ierr);
49e2dcd6d3SBarry Smith   }
50e2dcd6d3SBarry Smith   ierr = PetscCommDestroy(&ncomm);CHKERRQ(ierr);
515ad9ad5bSBarry Smith   ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLockStdout);CHKERRQ(ierr);
525c6c1daeSBarry Smith   PetscFunctionReturn(0);
535c6c1daeSBarry Smith }
545c6c1daeSBarry Smith 
555c6c1daeSBarry Smith #undef __FUNCT__
565c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_STDOUT_"
575c6c1daeSBarry Smith /*@C
585c6c1daeSBarry Smith    PETSC_VIEWER_STDOUT_ - Creates a ASCII PetscViewer shared by all processors
595c6c1daeSBarry Smith                     in a communicator.
605c6c1daeSBarry Smith 
615c6c1daeSBarry Smith    Collective on MPI_Comm
625c6c1daeSBarry Smith 
635c6c1daeSBarry Smith    Input Parameter:
645c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
655c6c1daeSBarry Smith 
665c6c1daeSBarry Smith    Level: beginner
675c6c1daeSBarry Smith 
685c6c1daeSBarry Smith    Notes:
695c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
705c6c1daeSBarry Smith    an error code. Usually used in the form
715c6c1daeSBarry Smith $      XXXView(XXX object,PETSC_VIEWER_STDOUT_(comm));
725c6c1daeSBarry Smith 
735c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
745c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_SELF
755c6c1daeSBarry Smith 
765c6c1daeSBarry Smith @*/
775c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_STDOUT_(MPI_Comm comm)
785c6c1daeSBarry Smith {
795c6c1daeSBarry Smith   PetscErrorCode ierr;
805c6c1daeSBarry Smith   PetscViewer    viewer;
815c6c1daeSBarry Smith 
825c6c1daeSBarry Smith   PetscFunctionBegin;
835c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStdout(comm,&viewer);
84efca3c55SSatish Balay   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," "); PetscFunctionReturn(0);}
855c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
865c6c1daeSBarry Smith }
875c6c1daeSBarry Smith 
885c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
895c6c1daeSBarry Smith 
90e2dcd6d3SBarry Smith /*
91e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
92e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
93e2dcd6d3SBarry Smith */
94e2dcd6d3SBarry Smith static PetscMPIInt Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;
95e2dcd6d3SBarry Smith 
965c6c1daeSBarry Smith #undef __FUNCT__
975c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIGetStderr"
985c6c1daeSBarry Smith /*@C
995c6c1daeSBarry Smith    PetscViewerASCIIGetStderr - Creates a ASCII PetscViewer shared by all processors
1005c6c1daeSBarry Smith                     in a communicator. Error returning version of PETSC_VIEWER_STDERR_()
1015c6c1daeSBarry Smith 
1025c6c1daeSBarry Smith    Collective on MPI_Comm
1035c6c1daeSBarry Smith 
1045c6c1daeSBarry Smith    Input Parameter:
1055c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
1065c6c1daeSBarry Smith 
1075c6c1daeSBarry Smith    Level: beginner
1085c6c1daeSBarry Smith 
1095c6c1daeSBarry Smith    Notes:
1105c6c1daeSBarry Smith      This should be used in all PETSc source code instead of PETSC_VIEWER_STDERR_()
1115c6c1daeSBarry Smith 
1125c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDERR_WORLD,
1135c6c1daeSBarry Smith           PETSC_VIEWER_STDERR_SELF
1145c6c1daeSBarry Smith 
1155c6c1daeSBarry Smith @*/
1165c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetStderr(MPI_Comm comm,PetscViewer *viewer)
1175c6c1daeSBarry Smith {
1185c6c1daeSBarry Smith   PetscErrorCode ierr;
119e2dcd6d3SBarry Smith   PetscBool      flg;
120e2dcd6d3SBarry Smith   MPI_Comm       ncomm;
121e2dcd6d3SBarry Smith 
122e2dcd6d3SBarry Smith   PetscFunctionBegin;
1235ad9ad5bSBarry Smith   ierr = PetscSpinlockLock(&PetscViewerASCIISpinLockStderr);CHKERRQ(ierr);
124e2dcd6d3SBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,NULL);CHKERRQ(ierr);
125e2dcd6d3SBarry Smith   if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) {
126e2dcd6d3SBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stderr_keyval,0);CHKERRQ(ierr);
127e2dcd6d3SBarry Smith   }
128e2dcd6d3SBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Stderr_keyval,(void**)viewer,(PetscMPIInt*)&flg);CHKERRQ(ierr);
129e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
130e2dcd6d3SBarry Smith     ierr = PetscViewerASCIIOpen(ncomm,"stderr",viewer);CHKERRQ(ierr);
131e2dcd6d3SBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)*viewer);CHKERRQ(ierr);
132e2dcd6d3SBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Stderr_keyval,(void*)*viewer);CHKERRQ(ierr);
133e2dcd6d3SBarry Smith   }
134e2dcd6d3SBarry Smith   ierr = PetscCommDestroy(&ncomm);CHKERRQ(ierr);
1355ad9ad5bSBarry Smith   ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLockStderr);CHKERRQ(ierr);
1365c6c1daeSBarry Smith   PetscFunctionReturn(0);
1375c6c1daeSBarry Smith }
1385c6c1daeSBarry Smith 
1395c6c1daeSBarry Smith #undef __FUNCT__
1405c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_STDERR_"
1415c6c1daeSBarry Smith /*@C
1425c6c1daeSBarry Smith    PETSC_VIEWER_STDERR_ - Creates a ASCII PetscViewer shared by all processors
1435c6c1daeSBarry Smith                     in a communicator.
1445c6c1daeSBarry Smith 
1455c6c1daeSBarry Smith    Collective on MPI_Comm
1465c6c1daeSBarry Smith 
1475c6c1daeSBarry Smith    Input Parameter:
1485c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
1495c6c1daeSBarry Smith 
1505c6c1daeSBarry Smith    Level: beginner
1515c6c1daeSBarry Smith 
1525c6c1daeSBarry Smith    Note:
1535c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
1545c6c1daeSBarry Smith    an error code. Usually used in the form
1555c6c1daeSBarry Smith $      XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));
1565c6c1daeSBarry Smith 
1575c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_, PetscViewerASCIIOpen(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDOUT_WORLD,
1585c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDERR_WORLD, PETSC_VIEWER_STDERR_SELF
1595c6c1daeSBarry Smith @*/
1605c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_STDERR_(MPI_Comm comm)
1615c6c1daeSBarry Smith {
1625c6c1daeSBarry Smith   PetscErrorCode ierr;
1635c6c1daeSBarry Smith   PetscViewer    viewer;
1645c6c1daeSBarry Smith 
1655c6c1daeSBarry Smith   PetscFunctionBegin;
1665c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStderr(comm,&viewer);
167efca3c55SSatish Balay   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," "); PetscFunctionReturn(0);}
1685c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
1695c6c1daeSBarry Smith }
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith 
1725c6c1daeSBarry Smith PetscMPIInt Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
1735c6c1daeSBarry Smith #undef __FUNCT__
1745c6c1daeSBarry Smith #define __FUNCT__ "Petsc_DelViewer"
1755c6c1daeSBarry Smith /*
1765c6c1daeSBarry 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
1775c6c1daeSBarry Smith    PetscObjectDestroyRegisterAll(). PetscViewerASCIIGetStdout() registers the viewer with PetscObjectDestroyRegister() to be destroyed when PetscFinalize() is called.
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith   This is called by MPI, not by users.
1805c6c1daeSBarry Smith 
1815c6c1daeSBarry Smith */
1828cc058d9SJed Brown PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelViewer(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
1835c6c1daeSBarry Smith {
1845c6c1daeSBarry Smith   PetscErrorCode ierr;
1855c6c1daeSBarry Smith 
1865c6c1daeSBarry Smith   PetscFunctionBegin;
1875c6c1daeSBarry Smith   ierr = PetscInfo1(0,"Removing viewer data attribute in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
1885c6c1daeSBarry Smith   PetscFunctionReturn(MPI_SUCCESS);
1895c6c1daeSBarry Smith }
1905c6c1daeSBarry Smith 
1915c6c1daeSBarry Smith #undef __FUNCT__
1925c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIOpen"
1935c6c1daeSBarry Smith /*@C
1945c6c1daeSBarry Smith    PetscViewerASCIIOpen - Opens an ASCII file as a PetscViewer.
1955c6c1daeSBarry Smith 
1965c6c1daeSBarry Smith    Collective on MPI_Comm
1975c6c1daeSBarry Smith 
1985c6c1daeSBarry Smith    Input Parameters:
1995c6c1daeSBarry Smith +  comm - the communicator
2005c6c1daeSBarry Smith -  name - the file name
2015c6c1daeSBarry Smith 
2025c6c1daeSBarry Smith    Output Parameter:
2035c6c1daeSBarry Smith .  lab - the PetscViewer to use with the specified file
2045c6c1daeSBarry Smith 
2055c6c1daeSBarry Smith    Level: beginner
2065c6c1daeSBarry Smith 
2075c6c1daeSBarry Smith    Notes:
2085c6c1daeSBarry Smith    This PetscViewer can be destroyed with PetscViewerDestroy().
2095c6c1daeSBarry Smith 
2102ea3bc1cSBarry Smith    The MPI communicator used here must match that used by the object one is viewing. For example if the
2112ea3bc1cSBarry Smith    Mat was created with a PETSC_COMM_WORLD, then the Viewer must be created with PETSC_COMM_WORLD
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(),
227*6a9046bcSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerPushFormat(), 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   }
2445ad9ad5bSBarry Smith   ierr = PetscSpinlockLock(&PetscViewerASCIISpinLockOpen);CHKERRQ(ierr);
2455c6c1daeSBarry Smith   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
2465c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);CHKERRQ(ierr);
2475c6c1daeSBarry Smith   }
2482bf49c77SBarry Smith   /*
2492bf49c77SBarry Smith        It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
2502bf49c77SBarry Smith      we cannot do that, since PetscFileSetName() takes a communicator that already exists.
2512bf49c77SBarry Smith 
2522bf49c77SBarry Smith       Plus if the original communicator that created the file has since been close this will not detect the old
2532bf49c77SBarry Smith       communictor and hence will overwrite the old data. It may be better to simply remove all this code
2542bf49c77SBarry Smith   */
2555c6c1daeSBarry Smith   /* make sure communicator is a PETSc communicator */
2560298fd71SBarry Smith   ierr = PetscCommDuplicate(comm,&comm,NULL);CHKERRQ(ierr);
2575c6c1daeSBarry Smith   /* has file already been opened into a viewer */
2585c6c1daeSBarry Smith   ierr = MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
2595c6c1daeSBarry Smith   if (flg) {
2605c6c1daeSBarry Smith     while (vlink) {
2615c6c1daeSBarry Smith       ierr = PetscStrcmp(name,((PetscViewer_ASCII*)(vlink->viewer->data))->filename,&eq);CHKERRQ(ierr);
2625c6c1daeSBarry Smith       if (eq) {
2635c6c1daeSBarry Smith         ierr = PetscObjectReference((PetscObject)vlink->viewer);CHKERRQ(ierr);
2645c6c1daeSBarry Smith         *lab = vlink->viewer;
2655c6c1daeSBarry Smith         ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
2665ad9ad5bSBarry Smith         ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen);CHKERRQ(ierr);
2675c6c1daeSBarry Smith         PetscFunctionReturn(0);
2685c6c1daeSBarry Smith       }
2695c6c1daeSBarry Smith       vlink = vlink->next;
2705c6c1daeSBarry Smith     }
2715c6c1daeSBarry Smith   }
2725c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
2735c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERASCII);CHKERRQ(ierr);
2745c6c1daeSBarry Smith   if (name) {
2755c6c1daeSBarry Smith     ierr = PetscViewerFileSetName(*lab,name);CHKERRQ(ierr);
2765c6c1daeSBarry Smith   }
2775c6c1daeSBarry Smith   /* save viewer into communicator if needed later */
278b00a9115SJed Brown   ierr       = PetscNew(&nv);CHKERRQ(ierr);
2795c6c1daeSBarry Smith   nv->viewer = *lab;
2805c6c1daeSBarry Smith   if (!flg) {
2815c6c1daeSBarry Smith     ierr = MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);CHKERRQ(ierr);
2825c6c1daeSBarry Smith   } else {
2835c6c1daeSBarry Smith     ierr = MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
2845c6c1daeSBarry Smith     if (vlink) {
2855c6c1daeSBarry Smith       while (vlink->next) vlink = vlink->next;
2865c6c1daeSBarry Smith       vlink->next = nv;
2875c6c1daeSBarry Smith     } else {
2885c6c1daeSBarry Smith       ierr = MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);CHKERRQ(ierr);
2895c6c1daeSBarry Smith     }
2905c6c1daeSBarry Smith   }
2915c6c1daeSBarry Smith   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
2925ad9ad5bSBarry Smith   ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen);CHKERRQ(ierr);
2935c6c1daeSBarry Smith   PetscFunctionReturn(0);
2945c6c1daeSBarry Smith }
2955c6c1daeSBarry Smith 
2965c6c1daeSBarry Smith #undef __FUNCT__
2975c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIOpenWithFILE"
2985c6c1daeSBarry Smith /*@C
2995c6c1daeSBarry Smith    PetscViewerASCIIOpenWithFILE - Given an open file creates an ASCII viewer that prints to it.
3005c6c1daeSBarry Smith 
3015c6c1daeSBarry Smith    Collective on MPI_Comm
3025c6c1daeSBarry Smith 
3035c6c1daeSBarry Smith    Input Parameters:
3045c6c1daeSBarry Smith +  comm - the communicator
3055c6c1daeSBarry Smith -  fd - the FILE pointer
3065c6c1daeSBarry Smith 
3075c6c1daeSBarry Smith    Output Parameter:
3085c6c1daeSBarry Smith .  lab - the PetscViewer to use with the specified file
3095c6c1daeSBarry Smith 
3105c6c1daeSBarry Smith    Level: beginner
3115c6c1daeSBarry Smith 
3125c6c1daeSBarry Smith    Notes:
3135c6c1daeSBarry Smith    This PetscViewer can be destroyed with PetscViewerDestroy(), but the fd will NOT be closed.
3145c6c1daeSBarry Smith 
3155c6c1daeSBarry Smith    If a multiprocessor communicator is used (such as PETSC_COMM_WORLD),
3165c6c1daeSBarry Smith    then only the first processor in the group uses the file.  All other
3175c6c1daeSBarry Smith    processors send their data to the first processor to print.
3185c6c1daeSBarry Smith 
3195c6c1daeSBarry Smith   Concepts: PetscViewerASCII^creating
3205c6c1daeSBarry Smith   Concepts: printf
3215c6c1daeSBarry Smith   Concepts: printing
3225c6c1daeSBarry Smith   Concepts: accessing remote file
3235c6c1daeSBarry Smith   Concepts: remote file
3245c6c1daeSBarry Smith 
3255c6c1daeSBarry Smith .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
326*6a9046bcSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerPushFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
3275c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF, PetscViewerASCIIOpen()
3285c6c1daeSBarry Smith @*/
3295c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIOpenWithFILE(MPI_Comm comm,FILE *fd,PetscViewer *lab)
3305c6c1daeSBarry Smith {
3315c6c1daeSBarry Smith   PetscErrorCode ierr;
3325c6c1daeSBarry Smith 
3335c6c1daeSBarry Smith   PetscFunctionBegin;
3345c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
3355c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERASCII);CHKERRQ(ierr);
3365c6c1daeSBarry Smith   ierr = PetscViewerASCIISetFILE(*lab,fd);CHKERRQ(ierr);
3375c6c1daeSBarry Smith   PetscFunctionReturn(0);
3385c6c1daeSBarry Smith }
3395c6c1daeSBarry Smith 
3405c6c1daeSBarry Smith #undef __FUNCT__
3415c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIISetFILE"
3425c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISetFILE(PetscViewer viewer,FILE *fd)
3435c6c1daeSBarry Smith {
3445c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
3455c6c1daeSBarry Smith 
3465c6c1daeSBarry Smith   PetscFunctionBegin;
3475c6c1daeSBarry Smith   vascii->fd        = fd;
3485c6c1daeSBarry Smith   vascii->closefile = PETSC_FALSE;
3495c6c1daeSBarry Smith   PetscFunctionReturn(0);
3505c6c1daeSBarry Smith }
3515c6c1daeSBarry Smith 
3525c6c1daeSBarry Smith 
3535c6c1daeSBarry Smith 
354