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: /*MC
 13:   PETSC_VIEWER_STDOUT_WORLD  - same as PETSC_VIEWER_STDOUT_(PETSC_COMM_WORLD)

 15:   Level: beginner
 16: M*/

 18: /*MC
 19:   PETSC_VIEWER_STDOUT_SELF  - same as PETSC_VIEWER_STDOUT_(PETSC_COMM_SELF)

 21:   Level: beginner
 22: M*/

 24: #undef __FUNCT__  
 26: /*@C
 27:    PETSC_VIEWER_STDOUT_ - Creates a ASCII PetscViewer shared by all processors 
 28:                     in a communicator.

 30:    Collective on MPI_Comm

 32:    Input Parameter:
 33: .  comm - the MPI communicator to share the PetscViewer

 35:    Level: beginner

 37:    Notes: 
 38:    Unlike almost all other PETSc routines, this does not return 
 39:    an error code. Usually used in the form
 40: $      XXXView(XXX object,PETSC_VIEWER_STDOUT_(comm));

 42: .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
 43:           PETSC_VIEWER_STDOUT_SELF

 45: @*/
 46: PetscViewer PETSC_VIEWER_STDOUT_(MPI_Comm comm)
 47: {
 48:   int         ierr;
 49:   PetscTruth  flg;
 50:   PetscViewer viewer;

 53:   if (Petsc_Viewer_Stdout_keyval == MPI_KEYVAL_INVALID) {
 54:     MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stdout_keyval,0);
 55:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
 56:   }
 57:   MPI_Attr_get(comm,Petsc_Viewer_Stdout_keyval,(void **)&viewer,(int*)&flg);
 58:   if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
 59:   if (!flg) { /* PetscViewer not yet created */
 60:     PetscViewerASCIIOpen(comm,"stdout",&viewer);
 61:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
 62:     PetscObjectRegisterDestroy((PetscObject)viewer);
 63:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
 64:     MPI_Attr_put(comm,Petsc_Viewer_Stdout_keyval,(void*)viewer);
 65:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
 66:   }
 67:   PetscFunctionReturn(viewer);
 68: }

 70: /* ---------------------------------------------------------------------*/
 71: /*
 72:     The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
 73:   is attached to a communicator, in this case the attribute is a PetscViewer.
 74: */
 75: static int Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;

 77: #undef __FUNCT__  
 79: /*@C
 80:    PETSC_VIEWER_STDERR_ - Creates a ASCII PetscViewer shared by all processors 
 81:                     in a communicator.

 83:    Collective on MPI_Comm

 85:    Input Parameter:
 86: .  comm - the MPI communicator to share the PetscViewer

 88:    Level: beginner

 90:    Note: 
 91:    Unlike almost all other PETSc routines, this does not return 
 92:    an error code. Usually used in the form
 93: $      XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));

 95: .seealso: PETSC_VIEWER_DRAW_, PetscViewerASCIIOpen(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDOUT_WORLD,
 96:           PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDERR_WORLD, PETSC_VIEWER_STDERR_SELF
 97: @*/
 98: PetscViewer PETSC_VIEWER_STDERR_(MPI_Comm comm)
 99: {
100:   int         ierr;
101:   PetscTruth  flg;
102:   PetscViewer viewer;

105:   if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) {
106:     MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stderr_keyval,0);
107:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
108:   }
109:   MPI_Attr_get(comm,Petsc_Viewer_Stderr_keyval,(void **)&viewer,(int*)&flg);
110:   if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
111:   if (!flg) { /* PetscViewer not yet created */
112:     PetscViewerASCIIOpen(comm,"stderr",&viewer);
113:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
114:     PetscObjectRegisterDestroy((PetscObject)viewer);
115:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); return(0);}
116:     MPI_Attr_put(comm,Petsc_Viewer_Stderr_keyval,(void*)viewer);
117:     if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,1,1," "); return(0);}
118:   }
119:   PetscFunctionReturn(viewer);
120: }

122: #undef __FUNCT__  
124: /*@C
125:    PetscViewerASCIIOpen - Opens an ASCII file as a PetscViewer.

127:    Collective on MPI_Comm

129:    Input Parameters:
130: +  comm - the communicator
131: -  name - the file name

133:    Output Parameter:
134: .  lab - the PetscViewer to use with the specified file

136:    Level: beginner

138:    Notes:
139:    This PetscViewer can be destroyed with PetscViewerDestroy().

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

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

148:    As shown below, PetscViewerASCIIOpen() is useful in conjunction with 
149:    MatView() and VecView()
150: .vb
151:      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
152:      MatView(matrix,viewer);
153: .ve

155:   Concepts: PetscViewerASCII^creating
156:   Concepts: printf
157:   Concepts: printing
158:   Concepts: accessing remote file
159:   Concepts: remote file

161: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
162:           PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
163:           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF, 
164: @*/
165: int PetscViewerASCIIOpen(MPI_Comm comm,const char name[],PetscViewer *lab)
166: {

170:   PetscViewerCreate(comm,lab);
171:   PetscViewerSetType(*lab,PETSC_VIEWER_ASCII);
172:   if (name) {
173:     PetscViewerSetFilename(*lab,name);
174:   }
175:   return(0);
176: }