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;

 12: /*@C
 13:    PETSC_VIEWER_STDOUT_ - Creates a ASCII PetscViewer shared by all processors 
 14:                     in a communicator.

 16:    Collective on MPI_Comm

 18:    Input Parameter:
 19: .  comm - the MPI communicator to share the PetscViewer

 21:    Level: beginner

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

 28: .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
 29:           PETSC_VIEWER_STDOUT_SELF

 31: @*/
 32: PetscViewer PETSC_VIEWER_STDOUT_(MPI_Comm comm)
 33: {
 34:   int         ierr;
 35:   PetscTruth  flg;
 36:   PetscViewer viewer;

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

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

 63: /*@C
 64:    PETSC_VIEWER_STDERR_ - Creates a ASCII PetscViewer shared by all processors 
 65:                     in a communicator.

 67:    Collective on MPI_Comm

 69:    Input Parameter:
 70: .  comm - the MPI communicator to share the PetscViewer

 72:    Level: beginner

 74:    Note: 
 75:    Unlike almost all other PETSc routines, this does not return 
 76:    an error code. Usually used in the form
 77: $      XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));

 79: .seealso: PETSC_VIEWER_DRAW_, PetscViewerASCIIOpen(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDOUT_WORLD,
 80:           PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDERR_WORLD, PETSC_VIEWER_STDERR_SELF
 81: @*/
 82: PetscViewer PETSC_VIEWER_STDERR_(MPI_Comm comm)
 83: {
 84:   int         ierr;
 85:   PetscTruth  flg;
 86:   PetscViewer viewer;

 89:   if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) {
 90:     MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stderr_keyval,0);
 91:     if (ierr) {PetscError(__LINE__,"VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
 92:   }
 93:   MPI_Attr_get(comm,Petsc_Viewer_Stderr_keyval,(void **)&viewer,(int*)&flg);
 94:   if (ierr) {PetscError(__LINE__,"VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
 95:   if (!flg) { /* PetscViewer not yet created */
 96:     PetscViewerASCIIOpen(comm,"stderr",&viewer);
 97:     if (ierr) {PetscError(__LINE__,"VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
 98:     PetscObjectRegisterDestroy((PetscObject)viewer);
 99:     if (ierr) {PetscError(__LINE__,"VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
100:     MPI_Attr_put(comm,Petsc_Viewer_Stderr_keyval,(void*)viewer);
101:     if (ierr) {PetscError(__LINE__,"VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
102:   }
103:   PetscFunctionReturn(viewer);
104: }

106: /*@C
107:    PetscViewerASCIIOpen - Opens an ASCII file as a PetscViewer.

109:    Collective on MPI_Comm

111:    Input Parameters:
112: +  comm - the communicator
113: -  name - the file name

115:    Output Parameter:
116: .  lab - the PetscViewer to use with the specified file

118:    Level: beginner

120:    Notes:
121:    This PetscViewer can be destroyed with PetscViewerDestroy().

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

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

130:    As shown below, PetscViewerASCIIOpen() is useful in conjunction with 
131:    MatView() and VecView()
132: .vb
133:      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
134:      MatView(matrix,viewer);
135: .ve

137:   Concepts: PetscViewerASCII^creating
138:   Concepts: printf
139:   Concepts: printing
140:   Concepts: accessing remote file
141:   Concepts: remote file

143: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
144:           PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
145:           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF, 
146: @*/
147: int PetscViewerASCIIOpen(MPI_Comm comm,const char name[],PetscViewer *lab)
148: {

152:   PetscViewerCreate(comm,lab);
153:   PetscViewerSetType(*lab,PETSC_VIEWER_ASCII);
154:   if (name) {
155:     PetscViewerSetFilename(*lab,name);
156:   }
157:   return(0);
158: }