Actual source code: vcreatea.c

  1: #define PETSC_DLL

 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 PetscMPIInt Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;

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

 18:    Collective on MPI_Comm

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

 23:    Level: beginner

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

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

 33: @*/
 34: PetscViewer PETSC_DLLEXPORT PETSC_VIEWER_STDOUT_(MPI_Comm comm)
 35: {
 37:   PetscTruth     flg;
 38:   PetscViewer    viewer;

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

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

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

 71:    Collective on MPI_Comm

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

 76:    Level: beginner

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

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

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

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

115:    Collective on MPI_Comm

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

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

124:    Level: beginner

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

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

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

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

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

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

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