Actual source code: vcreatea.c

  1: /*$Id: vcreatea.c,v 1.20 2001/06/21 21:15:11 bsmith Exp $*/

 3:  #include petsc.h

  5: /* ---------------------------------------------------------------------*/
  6: /*
  7:     The variable Petsc_Viewer_Stdout_keyval is used to indicate an MPI attribute that
  8:   is attached to a communicator, in this case the attribute is a PetscViewer.
  9: */
 10: static int Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;


 15: /*@C
 16:    PETSC_VIEWER_STDOUT_ - Creates a ASCII PetscViewer shared by all processors 
 17:                     in a communicator.

 19:    Collective on MPI_Comm

 21:    Input Parameter:
 22: .  comm - the MPI communicator to share the PetscViewer

 24:    Level: beginner

 26:    Notes: 
 27:    Unlike almost all other PETSc routines, this does not return 
 28:    an error code. Usually used in the form
 29: $      XXXView(XXX object,PETSC_VIEWER_STDOUT_(comm));

 31: .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
 32:           PETSC_VIEWER_STDOUT_SELF

 34: @*/
 35: PetscViewer PETSC_VIEWER_STDOUT_(MPI_Comm comm)
 36: {
 37:   int         ierr;
 38:   PetscTruth  flg;
 39:   PetscViewer viewer;

 42:   if (Petsc_Viewer_Stdout_keyval == MPI_KEYVAL_INVALID) {
 43:     MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stdout_keyval,0);
 44:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
 45:   }
 46:   MPI_Attr_get(comm,Petsc_Viewer_Stdout_keyval,(void **)&viewer,(int*)&flg);
 47:   if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
 48:   if (!flg) { /* PetscViewer not yet created */
 49:     PetscViewerASCIIOpen(comm,"stdout",&viewer);
 50:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
 51:     PetscObjectRegisterDestroy((PetscObject)viewer);
 52:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
 53:     MPI_Attr_put(comm,Petsc_Viewer_Stdout_keyval,(void*)viewer);
 54:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
 55:   }
 56:   PetscFunctionReturn(viewer);
 57: }

 59: /* ---------------------------------------------------------------------*/
 60: /*
 61:     The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
 62:   is attached to a communicator, in this case the attribute is a PetscViewer.
 63: */
 64: static int Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;

 68: /*@C
 69:    PETSC_VIEWER_STDERR_ - Creates a ASCII PetscViewer shared by all processors 
 70:                     in a communicator.

 72:    Collective on MPI_Comm

 74:    Input Parameter:
 75: .  comm - the MPI communicator to share the PetscViewer

 77:    Level: beginner

 79:    Note: 
 80:    Unlike almost all other PETSc routines, this does not return 
 81:    an error code. Usually used in the form
 82: $      XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));

 84: .seealso: PETSC_VIEWER_DRAW_, PetscViewerASCIIOpen(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDOUT_WORLD,
 85:           PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDERR_WORLD, PETSC_VIEWER_STDERR_SELF
 86: @*/
 87: PetscViewer PETSC_VIEWER_STDERR_(MPI_Comm comm)
 88: {
 89:   int         ierr;
 90:   PetscTruth  flg;
 91:   PetscViewer viewer;

 94:   if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) {
 95:     MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stderr_keyval,0);
 96:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
 97:   }
 98:   MPI_Attr_get(comm,Petsc_Viewer_Stderr_keyval,(void **)&viewer,(int*)&flg);
 99:   if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
100:   if (!flg) { /* PetscViewer not yet created */
101:     PetscViewerASCIIOpen(comm,"stderr",&viewer);
102:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
103:     PetscObjectRegisterDestroy((PetscObject)viewer);
104:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
105:     MPI_Attr_put(comm,Petsc_Viewer_Stderr_keyval,(void*)viewer);
106:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
107:   }
108:   PetscFunctionReturn(viewer);
109: }

113: /*@C
114:    PetscViewerASCIIOpen - Opens an ASCII file as a PetscViewer.

116:    Collective on MPI_Comm

118:    Input Parameters:
119: +  comm - the communicator
120: -  name - the file name

122:    Output Parameter:
123: .  lab - the PetscViewer to use with the specified file

125:    Level: beginner

127:    Notes:
128:    This PetscViewer can be destroyed with PetscViewerDestroy().

130:    If a multiprocessor communicator is used (such as PETSC_COMM_WORLD), 
131:    then only the first processor in the group opens the file.  All other 
132:    processors send their data to the first processor to print. 

134:    Each processor can instead write its own independent output by
135:    specifying the communicator PETSC_COMM_SELF.

137:    As shown below, PetscViewerASCIIOpen() is useful in conjunction with 
138:    MatView() and VecView()
139: .vb
140:      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
141:      MatView(matrix,viewer);
142: .ve

144:   Concepts: PetscViewerASCII^creating
145:   Concepts: printf
146:   Concepts: printing
147:   Concepts: accessing remote file
148:   Concepts: remote file

150: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
151:           PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
152:           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF, 
153: @*/
154: int PetscViewerASCIIOpen(MPI_Comm comm,const char name[],PetscViewer *lab)
155: {

159:   PetscViewerCreate(comm,lab);
160:   PetscViewerSetType(*lab,PETSC_VIEWER_ASCII);
161:   if (name) {
162:     PetscViewerSetFilename(*lab,name);
163:   }
164:   return(0);
165: }