Actual source code: vcreatea.c

  1: #define PETSC_DLL

 3:  #include src/sys/viewer/impls/ascii/asciiimpl.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: }

110: PetscMPIInt Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
114: /*
115:    Private routine to delete internal storage when a communicator is freed.
116:   This is called by MPI, not by users.

118: */
119: PetscMPIInt PETSC_DLLEXPORT Petsc_DelViewer(MPI_Comm comm,PetscMPIInt keyval,void* attr_val,void* extra_state)
120: {

124:   PetscInfo1(0,"Deleting viewer data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
125:   PetscFunctionReturn(MPI_SUCCESS);
126: }

131: /*@C
132:    PetscViewerASCIIOpen - Opens an ASCII file as a PetscViewer.

134:    Collective on MPI_Comm

136:    Input Parameters:
137: +  comm - the communicator
138: -  name - the file name

140:    Output Parameter:
141: .  lab - the PetscViewer to use with the specified file

143:    Level: beginner

145:    Notes:
146:    This PetscViewer can be destroyed with PetscViewerDestroy().

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

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

155:    As shown below, PetscViewerASCIIOpen() is useful in conjunction with 
156:    MatView() and VecView()
157: .vb
158:      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
159:      MatView(matrix,viewer);
160: .ve

162:   Concepts: PetscViewerASCII^creating
163:   Concepts: printf
164:   Concepts: printing
165:   Concepts: accessing remote file
166:   Concepts: remote file

168: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
169:           PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
170:           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF, 
171: @*/
172: PetscErrorCode PETSC_DLLEXPORT PetscViewerASCIIOpen(MPI_Comm comm,const char name[],PetscViewer *lab)
173: {
174:   PetscErrorCode    ierr;
175:   PetscViewerLink   *vlink,*nv;
176:   PetscTruth        flg,eq;
177:   size_t            len;

180:   PetscStrlen(name,&len);
181:   if (!len) {
182:     *lab = PETSC_VIEWER_STDOUT_(comm);
183:     PetscObjectReference((PetscObject)*lab);
184:     return(0);
185:   }
186:   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
187:     MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);
188:   }

190:   /* make sure communicator is a PETSc communicator */
191:   PetscCommDuplicate(comm,&comm,PETSC_NULL);

193:   /* has file already been opened into a viewer */
194:   MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);
195:   if (flg) {
196:     while (vlink) {
197:       PetscStrcmp(name,((PetscViewer_ASCII*)(vlink->viewer->data))->filename,&eq);
198:       if (eq) {
199:         PetscObjectReference((PetscObject)vlink->viewer);
200:         *lab = vlink->viewer;
201:         PetscCommDestroy(&comm);
202:         return(0);
203:       }
204:       vlink = vlink->next;
205:     }
206:   }

208:   PetscViewerCreate(comm,lab);
209:   PetscViewerSetType(*lab,PETSC_VIEWER_ASCII);

211:   if (name) {
212:     PetscViewerFileSetName(*lab,name);
213:   }

215:   /* save viewer into communicator if needed later */
216:   PetscNew(PetscViewerLink,&nv);
217:   nv->viewer = *lab;
218:   if (!flg) {
219:     MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);
220:   } else {
221:     MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);
222:     if (vlink) {
223:       while (vlink->next) vlink = vlink->next;
224:       vlink->next = nv;
225:     } else {
226:       MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);
227:     }
228:   }
229:   PetscCommDestroy(&comm);
230:   return(0);
231: }