Actual source code: vcreatea.c

petsc-3.4.5 2014-06-29
  2: #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h>  /*I     "petscsys.h"   I*/

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

 13: /*@C
 14:    PetscViewerASCIIGetStdout - Creates a ASCII PetscViewer shared by all processors
 15:                     in a communicator. Error returning version of PETSC_VIEWER_STDOUT_()

 17:    Collective on MPI_Comm

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

 22:    Level: beginner

 24:    Notes:
 25:      This should be used in all PETSc source code instead of PETSC_VIEWER_STDOUT_()

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

 30: @*/
 31: PetscErrorCode  PetscViewerASCIIGetStdout(MPI_Comm comm,PetscViewer *viewer)
 32: {
 34:   PetscBool      flg;
 35:   MPI_Comm       ncomm;

 38:   PetscCommDuplicate(comm,&ncomm,NULL);
 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:   }
 42:   MPI_Attr_get(ncomm,Petsc_Viewer_Stdout_keyval,(void**)viewer,(PetscMPIInt*)&flg);
 43:   if (!flg) { /* PetscViewer not yet created */
 44:     PetscViewerASCIIOpen(ncomm,"stdout",viewer);
 45:     PetscObjectRegisterDestroy((PetscObject)*viewer);
 46:     MPI_Attr_put(ncomm,Petsc_Viewer_Stdout_keyval,(void*)*viewer);
 47:   }
 48:   PetscCommDestroy(&ncomm);
 49:   return(0);
 50: }

 54: /*@C
 55:    PETSC_VIEWER_STDOUT_ - Creates a ASCII PetscViewer shared by all processors
 56:                     in a communicator.

 58:    Collective on MPI_Comm

 60:    Input Parameter:
 61: .  comm - the MPI communicator to share the PetscViewer

 63:    Level: beginner

 65:    Notes:
 66:    Unlike almost all other PETSc routines, this does not return
 67:    an error code. Usually used in the form
 68: $      XXXView(XXX object,PETSC_VIEWER_STDOUT_(comm));

 70: .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDOUT_WORLD,
 71:           PETSC_VIEWER_STDOUT_SELF

 73: @*/
 74: PetscViewer  PETSC_VIEWER_STDOUT_(MPI_Comm comm)
 75: {
 77:   PetscViewer    viewer;

 80:   PetscViewerASCIIGetStdout(comm,&viewer);
 81:   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," "); return(0);}
 82:   PetscFunctionReturn(viewer);
 83: }

 85: /* ---------------------------------------------------------------------*/
 86: /*
 87:     The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
 88:   is attached to a communicator, in this case the attribute is a PetscViewer.
 89: */
 90: static PetscMPIInt Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;

 94: /*@C
 95:    PetscViewerASCIIGetStderr - Creates a ASCII PetscViewer shared by all processors
 96:                     in a communicator. Error returning version of PETSC_VIEWER_STDERR_()

 98:    Collective on MPI_Comm

100:    Input Parameter:
101: .  comm - the MPI communicator to share the PetscViewer

103:    Level: beginner

105:    Notes:
106:      This should be used in all PETSc source code instead of PETSC_VIEWER_STDERR_()

108: .seealso: PETSC_VIEWER_DRAW_(), PetscViewerASCIIOpen(), PETSC_VIEWER_STDERR_, PETSC_VIEWER_STDERR_WORLD,
109:           PETSC_VIEWER_STDERR_SELF

111: @*/
112: PetscErrorCode  PetscViewerASCIIGetStderr(MPI_Comm comm,PetscViewer *viewer)
113: {
115:   PetscBool      flg;
116:   MPI_Comm       ncomm;

119:   PetscCommDuplicate(comm,&ncomm,NULL);
120:   if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) {
121:     MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Stderr_keyval,0);
122:   }
123:   MPI_Attr_get(ncomm,Petsc_Viewer_Stderr_keyval,(void**)viewer,(PetscMPIInt*)&flg);
124:   if (!flg) { /* PetscViewer not yet created */
125:     PetscViewerASCIIOpen(ncomm,"stderr",viewer);
126:     PetscObjectRegisterDestroy((PetscObject)*viewer);
127:     MPI_Attr_put(ncomm,Petsc_Viewer_Stderr_keyval,(void*)*viewer);
128:   }
129:   PetscCommDestroy(&ncomm);
130:   return(0);
131: }

135: /*@C
136:    PETSC_VIEWER_STDERR_ - Creates a ASCII PetscViewer shared by all processors
137:                     in a communicator.

139:    Collective on MPI_Comm

141:    Input Parameter:
142: .  comm - the MPI communicator to share the PetscViewer

144:    Level: beginner

146:    Note:
147:    Unlike almost all other PETSc routines, this does not return
148:    an error code. Usually used in the form
149: $      XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));

151: .seealso: PETSC_VIEWER_DRAW_, PetscViewerASCIIOpen(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDOUT_WORLD,
152:           PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDERR_WORLD, PETSC_VIEWER_STDERR_SELF
153: @*/
154: PetscViewer  PETSC_VIEWER_STDERR_(MPI_Comm comm)
155: {
157:   PetscViewer    viewer;

160:   PetscViewerASCIIGetStderr(comm,&viewer);
161:   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_STDERR_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," "); return(0);}
162:   PetscFunctionReturn(viewer);
163: }


166: PetscMPIInt Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
169: /*
170:    Called with MPI_Comm_free() is called on a communicator that has a viewer as an attribute. The viewer is not actually destroyed because that is managed by
171:    PetscObjectDestroyRegisterAll(). PetscViewerASCIIGetStdout() registers the viewer with PetscObjectDestroyRegister() to be destroyed when PetscFinalize() is called.

173:   This is called by MPI, not by users.

175: */
176: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelViewer(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
177: {

181:   PetscInfo1(0,"Removing viewer data attribute in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
182:   PetscFunctionReturn(MPI_SUCCESS);
183: }

187: /*@C
188:    PetscViewerASCIIOpen - Opens an ASCII file as a PetscViewer.

190:    Collective on MPI_Comm

192:    Input Parameters:
193: +  comm - the communicator
194: -  name - the file name

196:    Output Parameter:
197: .  lab - the PetscViewer to use with the specified file

199:    Level: beginner

201:    Notes:
202:    This PetscViewer can be destroyed with PetscViewerDestroy().

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

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

211:    As shown below, PetscViewerASCIIOpen() is useful in conjunction with
212:    MatView() and VecView()
213: .vb
214:      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
215:      MatView(matrix,viewer);
216: .ve

218:   Concepts: PetscViewerASCII^creating
219:   Concepts: printf
220:   Concepts: printing
221:   Concepts: accessing remote file
222:   Concepts: remote file

224: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
225:           PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
226:           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF,
227: @*/
228: PetscErrorCode  PetscViewerASCIIOpen(MPI_Comm comm,const char name[],PetscViewer *lab)
229: {
230:   PetscErrorCode  ierr;
231:   PetscViewerLink *vlink,*nv;
232:   PetscBool       flg,eq;
233:   size_t          len;

236:   PetscStrlen(name,&len);
237:   if (!len) {
238:     PetscViewerASCIIGetStdout(comm,lab);
239:     PetscObjectReference((PetscObject)*lab);
240:     return(0);
241:   }
242:   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
243:     MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);
244:   }
245:   /*
246:        It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
247:      we cannot do that, since PetscFileSetName() takes a communicator that already exists.

249:       Plus if the original communicator that created the file has since been close this will not detect the old
250:       communictor and hence will overwrite the old data. It may be better to simply remove all this code
251:   */
252:   /* make sure communicator is a PETSc communicator */
253:   PetscCommDuplicate(comm,&comm,NULL);
254:   /* has file already been opened into a viewer */
255:   MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);
256:   if (flg) {
257:     while (vlink) {
258:       PetscStrcmp(name,((PetscViewer_ASCII*)(vlink->viewer->data))->filename,&eq);
259:       if (eq) {
260:         PetscObjectReference((PetscObject)vlink->viewer);
261:         *lab = vlink->viewer;
262:         PetscCommDestroy(&comm);
263:         return(0);
264:       }
265:       vlink = vlink->next;
266:     }
267:   }
268:   PetscViewerCreate(comm,lab);
269:   PetscViewerSetType(*lab,PETSCVIEWERASCII);
270:   if (name) {
271:     PetscViewerFileSetName(*lab,name);
272:   }
273:   /* save viewer into communicator if needed later */
274:   PetscNew(PetscViewerLink,&nv);
275:   nv->viewer = *lab;
276:   if (!flg) {
277:     MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);
278:   } else {
279:     MPI_Attr_get(comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);
280:     if (vlink) {
281:       while (vlink->next) vlink = vlink->next;
282:       vlink->next = nv;
283:     } else {
284:       MPI_Attr_put(comm,Petsc_Viewer_keyval,nv);
285:     }
286:   }
287:   PetscCommDestroy(&comm);
288:   return(0);
289: }

293: /*@C
294:    PetscViewerASCIIOpenWithFILE - Given an open file creates an ASCII viewer that prints to it.

296:    Collective on MPI_Comm

298:    Input Parameters:
299: +  comm - the communicator
300: -  fd - the FILE pointer

302:    Output Parameter:
303: .  lab - the PetscViewer to use with the specified file

305:    Level: beginner

307:    Notes:
308:    This PetscViewer can be destroyed with PetscViewerDestroy(), but the fd will NOT be closed.

310:    If a multiprocessor communicator is used (such as PETSC_COMM_WORLD),
311:    then only the first processor in the group uses the file.  All other
312:    processors send their data to the first processor to print.

314:   Concepts: PetscViewerASCII^creating
315:   Concepts: printf
316:   Concepts: printing
317:   Concepts: accessing remote file
318:   Concepts: remote file

320: .seealso: MatView(), VecView(), PetscViewerDestroy(), PetscViewerBinaryOpen(),
321:           PetscViewerASCIIGetPointer(), PetscViewerSetFormat(), PETSC_VIEWER_STDOUT_, PETSC_VIEWER_STDERR_,
322:           PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_STDOUT_SELF, PetscViewerASCIIOpen()
323: @*/
324: PetscErrorCode  PetscViewerASCIIOpenWithFILE(MPI_Comm comm,FILE *fd,PetscViewer *lab)
325: {

329:   PetscViewerCreate(comm,lab);
330:   PetscViewerSetType(*lab,PETSCVIEWERASCII);
331:   PetscViewerASCIISetFILE(*lab,fd);
332:   return(0);
333: }

337: PetscErrorCode  PetscViewerASCIISetFILE(PetscViewer viewer,FILE *fd)
338: {
339:   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;

342:   vascii->fd        = fd;
343:   vascii->closefile = PETSC_FALSE;
344:   return(0);
345: }