xref: /petsc/src/sys/classes/viewer/impls/ascii/vcreatea.c (revision e2dcd6d3cab71b8154b27dbcfaac094bcae4a0c7)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h>  /*I     "petscsys.h"   I*/
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
55c6c1daeSBarry Smith 
6*e2dcd6d3SBarry Smith /*
7*e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stdout_keyval is used to indicate an MPI attribute that
8*e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
9*e2dcd6d3SBarry Smith */
10*e2dcd6d3SBarry Smith static PetscMPIInt Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;
11*e2dcd6d3SBarry 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;
35*e2dcd6d3SBarry Smith   PetscBool      flg;
36*e2dcd6d3SBarry Smith   MPI_Comm       ncomm;
375c6c1daeSBarry Smith 
385c6c1daeSBarry Smith   PetscFunctionBegin;
39*e2dcd6d3SBarry Smith   ierr = PetscSpinlockLock(&PetscViewerASCIISpinLock);CHKERRQ(ierr);
40*e2dcd6d3SBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,NULL);CHKERRQ(ierr);
41*e2dcd6d3SBarry Smith   if (Petsc_Viewer_Stdout_keyval == MPI_KEYVAL_INVALID) {
42*e2dcd6d3SBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stdout_keyval,0);CHKERRQ(ierr);
43*e2dcd6d3SBarry Smith   }
44*e2dcd6d3SBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Stdout_keyval,(void**)viewer,(PetscMPIInt*)&flg);CHKERRQ(ierr);
45*e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
46*e2dcd6d3SBarry Smith     ierr = PetscViewerASCIIOpen(ncomm,"stdout",viewer);CHKERRQ(ierr);
47*e2dcd6d3SBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)*viewer);CHKERRQ(ierr);
48*e2dcd6d3SBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Stdout_keyval,(void*)*viewer);CHKERRQ(ierr);
49*e2dcd6d3SBarry Smith   }
50*e2dcd6d3SBarry Smith   ierr = PetscCommDestroy(&ncomm);CHKERRQ(ierr);
51*e2dcd6d3SBarry Smith   ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLock);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 
90*e2dcd6d3SBarry Smith /*
91*e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
92*e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
93*e2dcd6d3SBarry Smith */
94*e2dcd6d3SBarry Smith static PetscMPIInt Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;
95*e2dcd6d3SBarry 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;
1195c6c1daeSBarry Smith 
1205c6c1daeSBarry Smith   PetscFunctionBegin;
121*e2dcd6d3SBarry Smith   PetscBool      flg;
122*e2dcd6d3SBarry Smith   MPI_Comm       ncomm;
123*e2dcd6d3SBarry Smith 
124*e2dcd6d3SBarry Smith   PetscFunctionBegin;
125*e2dcd6d3SBarry Smith   ierr = PetscSpinlockLock(&PetscViewerASCIISpinLock);CHKERRQ(ierr);
126*e2dcd6d3SBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,NULL);CHKERRQ(ierr);
127*e2dcd6d3SBarry Smith   if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) {
128*e2dcd6d3SBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stderr_keyval,0);CHKERRQ(ierr);
129*e2dcd6d3SBarry Smith   }
130*e2dcd6d3SBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Stderr_keyval,(void**)viewer,(PetscMPIInt*)&flg);CHKERRQ(ierr);
131*e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
132*e2dcd6d3SBarry Smith     ierr = PetscViewerASCIIOpen(ncomm,"stderr",viewer);CHKERRQ(ierr);
133*e2dcd6d3SBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)*viewer);CHKERRQ(ierr);
134*e2dcd6d3SBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Stderr_keyval,(void*)*viewer);CHKERRQ(ierr);
135*e2dcd6d3SBarry Smith   }
136*e2dcd6d3SBarry Smith   ierr = PetscCommDestroy(&ncomm);CHKERRQ(ierr);
137*e2dcd6d3SBarry Smith   ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLock);CHKERRQ(ierr);
1385c6c1daeSBarry Smith   PetscFunctionReturn(0);
1395c6c1daeSBarry Smith }
1405c6c1daeSBarry Smith 
1415c6c1daeSBarry Smith #undef __FUNCT__
1425c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_STDERR_"
1435c6c1daeSBarry Smith /*@C
1445c6c1daeSBarry Smith    PETSC_VIEWER_STDERR_ - Creates a ASCII PetscViewer shared by all processors
1455c6c1daeSBarry Smith                     in a communicator.
1465c6c1daeSBarry Smith 
1475c6c1daeSBarry Smith    Collective on MPI_Comm
1485c6c1daeSBarry Smith 
1495c6c1daeSBarry Smith    Input Parameter:
1505c6c1daeSBarry Smith .  comm - the MPI communicator to share the PetscViewer
1515c6c1daeSBarry Smith 
1525c6c1daeSBarry Smith    Level: beginner
1535c6c1daeSBarry Smith 
1545c6c1daeSBarry Smith    Note:
1555c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
1565c6c1daeSBarry Smith    an error code. Usually used in the form
1575c6c1daeSBarry Smith $      XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));
1585c6c1daeSBarry Smith 
1595c6c1daeSBarry Smith .seealso: PETSC_VIEWER_DRAW_, PetscViewerASCIIOpen(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDOUT_WORLD,
1605c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDERR_WORLD, PETSC_VIEWER_STDERR_SELF
1615c6c1daeSBarry Smith @*/
1625c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_STDERR_(MPI_Comm comm)
1635c6c1daeSBarry Smith {
1645c6c1daeSBarry Smith   PetscErrorCode ierr;
1655c6c1daeSBarry Smith   PetscViewer    viewer;
1665c6c1daeSBarry Smith 
1675c6c1daeSBarry Smith   PetscFunctionBegin;
1685c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStderr(comm,&viewer);
169efca3c55SSatish Balay   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," "); PetscFunctionReturn(0);}
1705c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
1715c6c1daeSBarry Smith }
1725c6c1daeSBarry Smith 
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith PetscMPIInt Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
1755c6c1daeSBarry Smith #undef __FUNCT__
1765c6c1daeSBarry Smith #define __FUNCT__ "Petsc_DelViewer"
1775c6c1daeSBarry Smith /*
1785c6c1daeSBarry 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
1795c6c1daeSBarry Smith    PetscObjectDestroyRegisterAll(). PetscViewerASCIIGetStdout() registers the viewer with PetscObjectDestroyRegister() to be destroyed when PetscFinalize() is called.
1805c6c1daeSBarry Smith 
1815c6c1daeSBarry Smith   This is called by MPI, not by users.
1825c6c1daeSBarry Smith 
1835c6c1daeSBarry Smith */
1848cc058d9SJed Brown PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelViewer(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
1855c6c1daeSBarry Smith {
1865c6c1daeSBarry Smith   PetscErrorCode ierr;
1875c6c1daeSBarry Smith 
1885c6c1daeSBarry Smith   PetscFunctionBegin;
1895c6c1daeSBarry Smith   ierr = PetscInfo1(0,"Removing viewer data attribute in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
1905c6c1daeSBarry Smith   PetscFunctionReturn(MPI_SUCCESS);
1915c6c1daeSBarry Smith }
1925c6c1daeSBarry Smith 
1935c6c1daeSBarry Smith #undef __FUNCT__
1945c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIOpen"
1955c6c1daeSBarry Smith /*@C
1965c6c1daeSBarry Smith    PetscViewerASCIIOpen - Opens an ASCII file as a PetscViewer.
1975c6c1daeSBarry Smith 
1985c6c1daeSBarry Smith    Collective on MPI_Comm
1995c6c1daeSBarry Smith 
2005c6c1daeSBarry Smith    Input Parameters:
2015c6c1daeSBarry Smith +  comm - the communicator
2025c6c1daeSBarry Smith -  name - the file name
2035c6c1daeSBarry Smith 
2045c6c1daeSBarry Smith    Output Parameter:
2055c6c1daeSBarry Smith .  lab - the PetscViewer to use with the specified file
2065c6c1daeSBarry Smith 
2075c6c1daeSBarry Smith    Level: beginner
2085c6c1daeSBarry Smith 
2095c6c1daeSBarry Smith    Notes:
2105c6c1daeSBarry Smith    This PetscViewer can be destroyed with PetscViewerDestroy().
2115c6c1daeSBarry Smith 
2122ea3bc1cSBarry Smith    The MPI communicator used here must match that used by the object one is viewing. For example if the
2132ea3bc1cSBarry Smith    Mat was created with a PETSC_COMM_WORLD, then the Viewer must be created with PETSC_COMM_WORLD
2145c6c1daeSBarry Smith 
2155c6c1daeSBarry Smith    As shown below, PetscViewerASCIIOpen() is useful in conjunction with
2165c6c1daeSBarry Smith    MatView() and VecView()
2175c6c1daeSBarry Smith .vb
2185c6c1daeSBarry Smith      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
2195c6c1daeSBarry Smith      MatView(matrix,viewer);
2205c6c1daeSBarry Smith .ve
2215c6c1daeSBarry Smith 
2225c6c1daeSBarry Smith   Concepts: PetscViewerASCII^creating
2235c6c1daeSBarry Smith   Concepts: printf
2245c6c1daeSBarry Smith   Concepts: printing
2255c6c1daeSBarry Smith   Concepts: accessing remote file
2265c6c1daeSBarry Smith   Concepts: remote file
2275c6c1daeSBarry Smith 
2285c6c1daeSBarry Smith .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
2295c6c1daeSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
2305c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF,
2315c6c1daeSBarry Smith @*/
2325c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIOpen(MPI_Comm comm,const char name[],PetscViewer *lab)
2335c6c1daeSBarry Smith {
2345c6c1daeSBarry Smith   PetscErrorCode  ierr;
2355c6c1daeSBarry Smith   PetscViewerLink *vlink,*nv;
2365c6c1daeSBarry Smith   PetscBool       flg,eq;
2375c6c1daeSBarry Smith   size_t          len;
2385c6c1daeSBarry Smith 
2395c6c1daeSBarry Smith   PetscFunctionBegin;
2405c6c1daeSBarry Smith   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
2415c6c1daeSBarry Smith   if (!len) {
2425c6c1daeSBarry Smith     ierr = PetscViewerASCIIGetStdout(comm,lab);CHKERRQ(ierr);
2435c6c1daeSBarry Smith     ierr = PetscObjectReference((PetscObject)*lab);CHKERRQ(ierr);
2445c6c1daeSBarry Smith     PetscFunctionReturn(0);
2455c6c1daeSBarry Smith   }
246ef19f930SBarry Smith   ierr = PetscSpinlockLock(&PetscViewerASCIISpinLock);CHKERRQ(ierr);
2475c6c1daeSBarry Smith   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
2485c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);CHKERRQ(ierr);
2495c6c1daeSBarry Smith   }
2502bf49c77SBarry Smith   /*
2512bf49c77SBarry Smith        It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
2522bf49c77SBarry Smith      we cannot do that, since PetscFileSetName() takes a communicator that already exists.
2532bf49c77SBarry Smith 
2542bf49c77SBarry Smith       Plus if the original communicator that created the file has since been close this will not detect the old
2552bf49c77SBarry Smith       communictor and hence will overwrite the old data. It may be better to simply remove all this code
2562bf49c77SBarry Smith   */
2575c6c1daeSBarry Smith   /* make sure communicator is a PETSc communicator */
2580298fd71SBarry Smith   ierr = PetscCommDuplicate(comm,&comm,NULL);CHKERRQ(ierr);
2595c6c1daeSBarry Smith   /* has file already been opened into a viewer */
2605c6c1daeSBarry Smith   ierr = MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
2615c6c1daeSBarry Smith   if (flg) {
2625c6c1daeSBarry Smith     while (vlink) {
2635c6c1daeSBarry Smith       ierr = PetscStrcmp(name,((PetscViewer_ASCII*)(vlink->viewer->data))->filename,&eq);CHKERRQ(ierr);
2645c6c1daeSBarry Smith       if (eq) {
2655c6c1daeSBarry Smith         ierr = PetscObjectReference((PetscObject)vlink->viewer);CHKERRQ(ierr);
2665c6c1daeSBarry Smith         *lab = vlink->viewer;
2675c6c1daeSBarry Smith         ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
268ef19f930SBarry Smith         ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLock);CHKERRQ(ierr);
2695c6c1daeSBarry Smith         PetscFunctionReturn(0);
2705c6c1daeSBarry Smith       }
2715c6c1daeSBarry Smith       vlink = vlink->next;
2725c6c1daeSBarry Smith     }
2735c6c1daeSBarry Smith   }
2745c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
2755c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERASCII);CHKERRQ(ierr);
2765c6c1daeSBarry Smith   if (name) {
2775c6c1daeSBarry Smith     ierr = PetscViewerFileSetName(*lab,name);CHKERRQ(ierr);
2785c6c1daeSBarry Smith   }
2795c6c1daeSBarry Smith   /* save viewer into communicator if needed later */
280b00a9115SJed Brown   ierr       = PetscNew(&nv);CHKERRQ(ierr);
2815c6c1daeSBarry Smith   nv->viewer = *lab;
2825c6c1daeSBarry Smith   if (!flg) {
2835c6c1daeSBarry Smith     ierr = MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);CHKERRQ(ierr);
2845c6c1daeSBarry Smith   } else {
2855c6c1daeSBarry Smith     ierr = MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
2865c6c1daeSBarry Smith     if (vlink) {
2875c6c1daeSBarry Smith       while (vlink->next) vlink = vlink->next;
2885c6c1daeSBarry Smith       vlink->next = nv;
2895c6c1daeSBarry Smith     } else {
2905c6c1daeSBarry Smith       ierr = MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);CHKERRQ(ierr);
2915c6c1daeSBarry Smith     }
2925c6c1daeSBarry Smith   }
2935c6c1daeSBarry Smith   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
294ef19f930SBarry Smith   ierr = PetscSpinlockUnlock(&PetscViewerASCIISpinLock);CHKERRQ(ierr);
2955c6c1daeSBarry Smith   PetscFunctionReturn(0);
2965c6c1daeSBarry Smith }
2975c6c1daeSBarry Smith 
2985c6c1daeSBarry Smith #undef __FUNCT__
2995c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIOpenWithFILE"
3005c6c1daeSBarry Smith /*@C
3015c6c1daeSBarry Smith    PetscViewerASCIIOpenWithFILE - Given an open file creates an ASCII viewer that prints to it.
3025c6c1daeSBarry Smith 
3035c6c1daeSBarry Smith    Collective on MPI_Comm
3045c6c1daeSBarry Smith 
3055c6c1daeSBarry Smith    Input Parameters:
3065c6c1daeSBarry Smith +  comm - the communicator
3075c6c1daeSBarry Smith -  fd - the FILE pointer
3085c6c1daeSBarry Smith 
3095c6c1daeSBarry Smith    Output Parameter:
3105c6c1daeSBarry Smith .  lab - the PetscViewer to use with the specified file
3115c6c1daeSBarry Smith 
3125c6c1daeSBarry Smith    Level: beginner
3135c6c1daeSBarry Smith 
3145c6c1daeSBarry Smith    Notes:
3155c6c1daeSBarry Smith    This PetscViewer can be destroyed with PetscViewerDestroy(), but the fd will NOT be closed.
3165c6c1daeSBarry Smith 
3175c6c1daeSBarry Smith    If a multiprocessor communicator is used (such as PETSC_COMM_WORLD),
3185c6c1daeSBarry Smith    then only the first processor in the group uses the file.  All other
3195c6c1daeSBarry Smith    processors send their data to the first processor to print.
3205c6c1daeSBarry Smith 
3215c6c1daeSBarry Smith   Concepts: PetscViewerASCII^creating
3225c6c1daeSBarry Smith   Concepts: printf
3235c6c1daeSBarry Smith   Concepts: printing
3245c6c1daeSBarry Smith   Concepts: accessing remote file
3255c6c1daeSBarry Smith   Concepts: remote file
3265c6c1daeSBarry Smith 
3275c6c1daeSBarry Smith .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
3285c6c1daeSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
3295c6c1daeSBarry Smith           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF, PetscViewerASCIIOpen()
3305c6c1daeSBarry Smith @*/
3315c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIOpenWithFILE(MPI_Comm comm,FILE *fd,PetscViewer *lab)
3325c6c1daeSBarry Smith {
3335c6c1daeSBarry Smith   PetscErrorCode ierr;
3345c6c1daeSBarry Smith 
3355c6c1daeSBarry Smith   PetscFunctionBegin;
3365c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,lab);CHKERRQ(ierr);
3375c6c1daeSBarry Smith   ierr = PetscViewerSetType(*lab,PETSCVIEWERASCII);CHKERRQ(ierr);
3385c6c1daeSBarry Smith   ierr = PetscViewerASCIISetFILE(*lab,fd);CHKERRQ(ierr);
3395c6c1daeSBarry Smith   PetscFunctionReturn(0);
3405c6c1daeSBarry Smith }
3415c6c1daeSBarry Smith 
3425c6c1daeSBarry Smith #undef __FUNCT__
3435c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIISetFILE"
3445c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISetFILE(PetscViewer viewer,FILE *fd)
3455c6c1daeSBarry Smith {
3465c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
3475c6c1daeSBarry Smith 
3485c6c1daeSBarry Smith   PetscFunctionBegin;
3495c6c1daeSBarry Smith   vascii->fd        = fd;
3505c6c1daeSBarry Smith   vascii->closefile = PETSC_FALSE;
3515c6c1daeSBarry Smith   PetscFunctionReturn(0);
3525c6c1daeSBarry Smith }
3535c6c1daeSBarry Smith 
3545c6c1daeSBarry Smith 
3555c6c1daeSBarry Smith 
356