Actual source code: binv.c

petsc-master 2017-01-18
Report Typos and Errors

  2:  #include <petsc/private/viewerimpl.h>
  3: #include <fcntl.h>
  4: #if defined(PETSC_HAVE_UNISTD_H)
  5: #include <unistd.h>
  6: #endif
  7: #if defined(PETSC_HAVE_IO_H)
  8: #include <io.h>
  9: #endif

 11: typedef struct  {
 12:   int           fdes;                 /* file descriptor, ignored if using MPI IO */
 13: #if defined(PETSC_HAVE_MPIIO)
 14:   PetscBool     usempiio;
 15:   MPI_File      mfdes;                /* ignored unless using MPI IO */
 16:   MPI_Offset    moff;
 17: #endif
 18:   PetscFileMode btype;                /* read or write? */
 19:   FILE          *fdes_info;           /* optional file containing info on binary file*/
 20:   PetscBool     storecompressed;      /* gzip the write binary file when closing it*/
 21:   char          *filename;
 22:   PetscBool     skipinfo;             /* Don't create info file for writing; don't use for reading */
 23:   PetscBool     skipoptions;          /* don't use PETSc options database when loading */
 24:   PetscInt      flowcontrol;          /* allow only <flowcontrol> messages outstanding at a time while doing IO */
 25:   PetscBool     skipheader;           /* don't write header, only raw data */
 26:   PetscBool     matlabheaderwritten;  /* if format is PETSC_VIEWER_BINARY_MATLAB has the MATLAB .info header been written yet */
 27:   PetscBool     setfromoptionscalled;
 28: } PetscViewer_Binary;

 30: static PetscErrorCode PetscViewerGetSubViewer_Binary(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer)
 31: {
 32:   int                rank;
 33:   PetscErrorCode     ierr;
 34:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data,*obinary;

 37:   PetscViewerSetUp(viewer);
 38:   MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);
 39:   if (!rank) {
 40:     PetscViewerCreate(PETSC_COMM_SELF,outviewer);
 41:     PetscViewerSetType(*outviewer,PETSCVIEWERBINARY);
 42:     obinary = (PetscViewer_Binary*)(*outviewer)->data;
 43:     PetscMemcpy(obinary,vbinary,sizeof(PetscViewer_Binary));
 44:   } SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Cannot get subcomm viewer for binary files or sockets unless SubViewer contains the rank 0 process");
 45:   return(0);
 46: }

 48: static PetscErrorCode PetscViewerRestoreSubViewer_Binary(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer)
 49: {
 51:   PetscErrorCode rank;

 54:   MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);
 55:   if (!rank) {
 56:     PetscFree((*outviewer)->data);
 57:     PetscHeaderDestroy(outviewer);
 58:   }
 59:   return(0);
 60: }

 62: #if defined(PETSC_HAVE_MPIIO)
 63: /*@C
 64:     PetscViewerBinaryGetMPIIOOffset - Gets the current offset that should be passed to MPI_File_set_view()

 66:     Not Collective

 68:     Input Parameter:
 69: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

 71:     Output Parameter:
 72: .    off - the current offset

 74:     Level: advanced

 76:     Fortran Note:
 77:     This routine is not supported in Fortran.

 79:     Use PetscViewerBinaryAddMPIIOOffset() to increase this value after you have written a view.

 81:   Concepts: file descriptor^getting
 82:   Concepts: PetscViewerBinary^accessing file descriptor

 84: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
 85: @*/
 86: PetscErrorCode PetscViewerBinaryGetMPIIOOffset(PetscViewer viewer,MPI_Offset *off)
 87: {
 88:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

 91:   *off = vbinary->moff;
 92:   return(0);
 93: }

 95: /*@C
 96:     PetscViewerBinaryAddMPIIOOffset - Adds to the current offset that should be passed to MPI_File_set_view()

 98:     Not Collective

100:     Input Parameters:
101: +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
102: -    off - the addition to the offset

104:     Level: advanced

106:     Fortran Note:
107:     This routine is not supported in Fortran.

109:     Use PetscViewerBinaryGetMPIIOOffset() to get the value that you should pass to MPI_File_set_view()

111:   Concepts: file descriptor^getting
112:   Concepts: PetscViewerBinary^accessing file descriptor

114: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
115: @*/
116: PetscErrorCode PetscViewerBinaryAddMPIIOOffset(PetscViewer viewer,MPI_Offset off)
117: {
118:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

121:   vbinary->moff += off;
122:   return(0);
123: }

125: /*@C
126:     PetscViewerBinaryGetMPIIODescriptor - Extracts the MPI IO file descriptor from a PetscViewer.

128:     Not Collective

130:     Input Parameter:
131: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

133:     Output Parameter:
134: .   fdes - file descriptor

136:     Level: advanced

138:     Fortran Note:
139:     This routine is not supported in Fortran.

141:   Concepts: file descriptor^getting
142:   Concepts: PetscViewerBinary^accessing file descriptor

144: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
145: @*/
146: PetscErrorCode PetscViewerBinaryGetMPIIODescriptor(PetscViewer viewer,MPI_File *fdes)
147: {
148:   PetscErrorCode     ierr;
149:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

152:   PetscViewerSetUp(viewer);
153:   *fdes = vbinary->mfdes;
154:   return(0);
155: }

157: static PetscErrorCode PetscViewerBinaryGetUseMPIIO_Binary(PetscViewer viewer,PetscBool  *flg)
158: {
159:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
160: 
162:   *flg = vbinary->usempiio;
163:   return(0);
164: }
165: #endif


168: /*@C
169:     PetscViewerBinaryGetUseMPIIO - Returns PETSC_TRUE if the binary viewer uses MPI-IO.

171:     Not Collective

173:     Input Parameter:
174: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

176:     Output Parameter:
177: -   flg - PETSC_TRUE if MPI-IO is being used

179:     Options Database:
180:     -viewer_binary_mpiio : Flag for using MPI-IO

182:     Level: advanced

184:     Note:
185:     If MPI-IO is not available, this function will always return PETSC_FALSE

187:     Fortran Note:
188:     This routine is not supported in Fortran.

190:   Concepts: file descriptor^getting
191:   Concepts: PetscViewerBinary^accessing file descriptor

193: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer()
194: @*/
195: PetscErrorCode PetscViewerBinaryGetUseMPIIO(PetscViewer viewer,PetscBool *flg)
196: {

200:   *flg = PETSC_FALSE;
201:   PetscTryMethod(viewer,"PetscViewerBinaryGetUseMPIIO_C",(PetscViewer,PetscBool*),(viewer,flg));
202:   return(0);
203: }

205: static PetscErrorCode  PetscViewerBinaryGetFlowControl_Binary(PetscViewer viewer,PetscInt *fc)
206: {
207:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

210:   *fc = vbinary->flowcontrol;
211:   return(0);
212: }

214: /*@C
215:     PetscViewerBinaryGetFlowControl - Returns how many messages are allowed to outstanding at the same time during parallel IO reads/writes

217:     Not Collective

219:     Input Parameter:
220: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

222:     Output Parameter:
223: .   fc - the number of messages

225:     Level: advanced

227: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetFlowControl()

229: @*/
230: PetscErrorCode PetscViewerBinaryGetFlowControl(PetscViewer viewer,PetscInt *fc)
231: {

235:   PetscUseMethod(viewer,"PetscViewerBinaryGetFlowControl_C",(PetscViewer,PetscInt*),(viewer,fc));
236:   return(0);
237: }

239: static PetscErrorCode PetscViewerBinarySetFlowControl_Binary(PetscViewer viewer,PetscInt fc)
240: {
241:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

244:   if (fc <= 1) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Flow control count must be greater than 1, %D was set",fc);
245:   vbinary->flowcontrol = fc;
246:   return(0);
247: }

249: /*@C
250:     PetscViewerBinarySetFlowControl - Sets how many messages are allowed to outstanding at the same time during parallel IO reads/writes

252:     Not Collective

254:     Input Parameter:
255: +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
256: -   fc - the number of messages, defaults to 256 if this function was not called

258:     Level: advanced

260: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetFlowControl()

262: @*/
263: PetscErrorCode  PetscViewerBinarySetFlowControl(PetscViewer viewer,PetscInt fc)
264: {

268:   PetscUseMethod(viewer,"PetscViewerBinarySetFlowControl_C",(PetscViewer,PetscInt),(viewer,fc));
269:   return(0);
270: }

272: /*@C
273:     PetscViewerBinaryGetDescriptor - Extracts the file descriptor from a PetscViewer.

275:     Collective On PetscViewer

277:     Input Parameter:
278: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

280:     Output Parameter:
281: .   fdes - file descriptor

283:     Level: advanced

285:     Notes:
286:       For writable binary PetscViewers, the descriptor will only be valid for the
287:     first processor in the communicator that shares the PetscViewer. For readable
288:     files it will only be valid on nodes that have the file. If node 0 does not
289:     have the file it generates an error even if another node does have the file.

291:     Fortran Note:
292:     This routine is not supported in Fortran.

294:     Developer Notes: This must be called on all processes because Dave May changed
295:     the source code that this may be trigger a PetscViewerSetUp() call if it was not previously triggered.


298:   Concepts: file descriptor^getting
299:   Concepts: PetscViewerBinary^accessing file descriptor

301: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
302: @*/
303: PetscErrorCode PetscViewerBinaryGetDescriptor(PetscViewer viewer,int *fdes)
304: {
305:   PetscErrorCode     ierr;
306:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

309:   PetscViewerSetUp(viewer);
310:   *fdes = vbinary->fdes;
311:   return(0);
312: }

314: /*@
315:     PetscViewerBinarySkipInfo - Binary file will not have .info file created with it

317:     Not Collective

319:     Input Paramter:
320: .   viewer - PetscViewer context, obtained from PetscViewerCreate()

322:     Options Database Key:
323: .   -viewer_binary_skip_info

325:     Level: advanced

327:     Notes: This must be called after PetscViewerSetType(). If you use PetscViewerBinaryOpen() then
328:     you can only skip the info file with the -viewer_binary_skip_info flag. To use the function you must open the
329:     viewer with PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinarySkipInfo().

331:     The .info contains meta information about the data in the binary file, for example the block size if it was
332:     set for a vector or matrix.

334:    Concepts: PetscViewerBinary^accessing info file

336: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(),
337:           PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo()
338: @*/
339: PetscErrorCode PetscViewerBinarySkipInfo(PetscViewer viewer)
340: {
341:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

344:   vbinary->skipinfo = PETSC_TRUE;
345:   return(0);
346: }

348: static PetscErrorCode PetscViewerBinarySetSkipInfo_Binary(PetscViewer viewer,PetscBool skip)
349: {
350:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

353:   vbinary->skipinfo = skip;
354:   return(0);
355: }

357: /*@
358:     PetscViewerBinarySetSkipInfo - Binary file will not have .info file created with it

360:     Not Collective

362:     Input Paramter:
363: .   viewer - PetscViewer context, obtained from PetscViewerCreate()

365:     Options Database Key:
366: .   -viewer_binary_skip_info

368:     Level: advanced

370:     Concepts: PetscViewerBinary^accessing info file

372: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(),
373:           PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo()
374: @*/
375: PetscErrorCode PetscViewerBinarySetSkipInfo(PetscViewer viewer,PetscBool skip)
376: {

380:   PetscUseMethod(viewer,"PetscViewerBinarySetSkipInfo_C",(PetscViewer,PetscBool),(viewer,skip));
381:   return(0);
382: }

384: static PetscErrorCode PetscViewerBinaryGetSkipInfo_Binary(PetscViewer viewer,PetscBool *skip)
385: {
386:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

389:   *skip  = vbinary->skipinfo;
390:   return(0);
391: }

393: /*@
394:     PetscViewerBinaryGetSkipInfo - check if viewer wrote a .info file

396:     Not Collective

398:     Input Parameter:
399: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

401:     Output Parameter:
402: .   skip - PETSC_TRUE implies the .info file was not generated

404:     Level: advanced

406:     Notes: This must be called after PetscViewerSetType()

408:     Concepts: PetscViewerBinary^accessing info file

410: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
411:           PetscViewerBinarySetSkipOptions(), PetscViewerBinarySetSkipInfo()
412: @*/
413: PetscErrorCode PetscViewerBinaryGetSkipInfo(PetscViewer viewer,PetscBool *skip)
414: {

418:   PetscUseMethod(viewer,"PetscViewerBinaryGetSkipInfo_C",(PetscViewer,PetscBool*),(viewer,skip));
419:   return(0);
420: }

422: static PetscErrorCode PetscViewerBinarySetSkipOptions_Binary(PetscViewer viewer,PetscBool skip)
423: {
424:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

427:   vbinary->skipoptions = skip;
428:   return(0);
429: }

431: /*@
432:     PetscViewerBinarySetSkipOptions - do not use the PETSc options database when loading objects

434:     Not Collective

436:     Input Parameters:
437: +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
438: -   skip - PETSC_TRUE means do not use

440:     Options Database Key:
441: .   -viewer_binary_skip_options

443:     Level: advanced

445:     Notes: This must be called after PetscViewerSetType()

447:    Concepts: PetscViewerBinary^accessing info file

449: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
450:           PetscViewerBinaryGetSkipOptions()
451: @*/
452: PetscErrorCode PetscViewerBinarySetSkipOptions(PetscViewer viewer,PetscBool skip)
453: {

457:   PetscUseMethod(viewer,"PetscViewerBinarySetSkipOptions_C",(PetscViewer,PetscBool),(viewer,skip));
458:   return(0);
459: }

461: static PetscErrorCode PetscViewerBinaryGetSkipOptions_Binary(PetscViewer viewer,PetscBool *skip)
462: {
463:   PetscViewer_Binary *vbinary;

467:   vbinary = (PetscViewer_Binary*)viewer->data;
468:   *skip   = vbinary->skipoptions;
469:   return(0);
470: }

472: /*@
473:     PetscViewerBinaryGetSkipOptions - checks if viewer uses the PETSc options database when loading objects

475:     Not Collective

477:     Input Parameter:
478: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

480:     Output Parameter:
481: .   skip - PETSC_TRUE means do not use

483:     Level: advanced

485:     Notes: This must be called after PetscViewerSetType()

487:    Concepts: PetscViewerBinary^accessing info file

489: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
490:           PetscViewerBinarySetSkipOptions()
491: @*/
492: PetscErrorCode PetscViewerBinaryGetSkipOptions(PetscViewer viewer,PetscBool *skip)
493: {

497:   PetscUseMethod(viewer,"PetscViewerBinaryGetSkipOptions_C",(PetscViewer,PetscBool*),(viewer,skip));
498:   return(0);
499: }

501: static PetscErrorCode PetscViewerBinarySetSkipHeader_Binary(PetscViewer viewer,PetscBool skip)
502: {
503:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

506:   vbinary->skipheader = skip;
507:   return(0);
508: }

510: /*@
511:     PetscViewerBinarySetSkipHeader - do not write a header with size information on output, just raw data

513:     Not Collective

515:     Input Parameters:
516: +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
517: -   skip - PETSC_TRUE means do not write header

519:     Options Database Key:
520: .   -viewer_binary_skip_header

522:     Level: advanced

524:     Notes: This must be called after PetscViewerSetType()

526:            Can ONLY be called on a binary viewer

528: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
529:           PetscViewerBinaryGetSkipHeader()
530: @*/
531: PetscErrorCode PetscViewerBinarySetSkipHeader(PetscViewer viewer,PetscBool skip)
532: {

536:   PetscUseMethod(viewer,"PetscViewerBinarySetSkipHeader_C",(PetscViewer,PetscBool),(viewer,skip));
537:   return(0);
538: }

540: static PetscErrorCode PetscViewerBinaryGetSkipHeader_Binary(PetscViewer viewer,PetscBool  *skip)
541: {
542:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

545:   *skip = vbinary->skipheader;
546:   return(0);
547: }

549: /*@
550:     PetscViewerBinaryGetSkipHeader - checks whether to write a header with size information on output, or just raw data

552:     Not Collective

554:     Input Parameter:
555: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

557:     Output Parameter:
558: .   skip - PETSC_TRUE means do not write header

560:     Level: advanced

562:     Notes: This must be called after PetscViewerSetType()

564:             Returns false for PETSCSOCKETVIEWER, you cannot skip the header for it.

566: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
567:           PetscViewerBinarySetSkipHeader()
568: @*/
569: PetscErrorCode PetscViewerBinaryGetSkipHeader(PetscViewer viewer,PetscBool  *skip)
570: {

574:   *skip = PETSC_FALSE;
575:   PetscUseMethod(viewer,"PetscViewerBinaryGetSkipHeader_C",(PetscViewer,PetscBool*),(viewer,skip));
576:   return(0);
577: }

579: static PetscErrorCode PetscViewerBinaryGetInfoPointer_Binary(PetscViewer viewer,FILE **file)
580: {
581:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
582:   PetscErrorCode     ierr;
583:   MPI_Comm           comm;

586:   PetscViewerSetUp(viewer);
587:   *file = vbinary->fdes_info;
588:   if (viewer->format == PETSC_VIEWER_BINARY_MATLAB && !vbinary->matlabheaderwritten) {
589:     vbinary->matlabheaderwritten = PETSC_TRUE;
590:     PetscObjectGetComm((PetscObject)viewer,&comm);
591:     PetscFPrintf(comm,*file,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");
592:     PetscFPrintf(comm,*file,"#$$ Set.filename = '%s';\n",vbinary->filename);
593:     PetscFPrintf(comm,*file,"#$$ fd = PetscOpenFile(Set.filename);\n");
594:     PetscFPrintf(comm,*file,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");
595:   }
596:   return(0);
597: }

599: /*@C
600:     PetscViewerBinaryGetInfoPointer - Extracts the file pointer for the ASCII
601:           info file associated with a binary file.

603:     Not Collective

605:     Input Parameter:
606: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

608:     Output Parameter:
609: .   file - file pointer  Always returns NULL if not a binary viewer

611:     Level: advanced

613:     Notes:
614:       For writable binary PetscViewers, the descriptor will only be valid for the
615:     first processor in the communicator that shares the PetscViewer.

617:     Fortran Note:
618:     This routine is not supported in Fortran.

620:   Concepts: PetscViewerBinary^accessing info file

622: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetDescriptor()
623: @*/
624: PetscErrorCode PetscViewerBinaryGetInfoPointer(PetscViewer viewer,FILE **file)
625: {

629:   *file = NULL;
630:   PetscTryMethod(viewer,"PetscViewerBinaryGetInfoPointer_C",(PetscViewer,FILE **),(viewer,file));
631:   return(0);
632: }

634: static PetscErrorCode PetscViewerFileClose_Binary(PetscViewer v)
635: {
636:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
637:   PetscErrorCode     ierr;
638:   PetscMPIInt        rank;
639:   int                err;

642:   MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);
643:   if ((!rank || vbinary->btype == FILE_MODE_READ) && vbinary->fdes) {
644:     close(vbinary->fdes);
645:     if (!rank && vbinary->storecompressed) {
646:       char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
647:       FILE *fp;
648:       /* compress the file */
649:       PetscStrcpy(par,"gzip -f ");
650:       PetscStrcat(par,vbinary->filename);
651: #if defined(PETSC_HAVE_POPEN)
652:       PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);
653:       if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from command %s\n%s",par,buf);
654:       PetscPClose(PETSC_COMM_SELF,fp,NULL);
655: #else
656:       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
657: #endif
658:     }
659:   }
660:   if (vbinary->fdes_info) {
661:     err = fclose(vbinary->fdes_info);
662:     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
663:   }
664:   return(0);
665: }

667: #if defined(PETSC_HAVE_MPIIO)
668: static PetscErrorCode PetscViewerFileClose_BinaryMPIIO(PetscViewer v)
669: {
670:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
671:   int                err;
672:   PetscErrorCode     ierr;

675:   if (vbinary->mfdes) {
676:     MPI_File_close(&vbinary->mfdes);
677:   }
678:   if (vbinary->fdes_info) {
679:     err = fclose(vbinary->fdes_info);
680:     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
681:   }
682:   return(0);
683: }
684: #endif

686: static PetscErrorCode PetscViewerDestroy_Binary(PetscViewer v)
687: {
688:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
689:   PetscErrorCode     ierr;

692:   if (v->format == PETSC_VIEWER_BINARY_MATLAB) {
693:     MPI_Comm comm;
694:     FILE     *info;

696:     PetscObjectGetComm((PetscObject)v,&comm);
697:     PetscViewerBinaryGetInfoPointer(v,&info);
698:     PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");
699:     PetscFPrintf(comm,info,"#$$ close(fd);\n");
700:     PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");
701:   }
702: #if defined(PETSC_HAVE_MPIIO)
703:   if (vbinary->usempiio) {
704:     PetscViewerFileClose_BinaryMPIIO(v);
705:   } else {
706: #endif
707:     PetscViewerFileClose_Binary(v);
708: #if defined(PETSC_HAVE_MPIIO)
709:   }
710: #endif
711:   if (vbinary->filename) { PetscFree(vbinary->filename); }
712:   PetscFree(vbinary);
713:   return(0);
714: }

716: /*@C
717:    PetscViewerBinaryOpen - Opens a file for binary input/output.

719:    Collective on MPI_Comm

721:    Input Parameters:
722: +  comm - MPI communicator
723: .  name - name of file
724: -  type - type of file
725: $    FILE_MODE_WRITE - create new file for binary output
726: $    FILE_MODE_READ - open existing file for binary input
727: $    FILE_MODE_APPEND - open existing file for binary output

729:    Output Parameter:
730: .  binv - PetscViewer for binary input/output to use with the specified file

732:     Options Database Keys:
733: +    -viewer_binary_filename <name>
734: .    -viewer_binary_skip_info
735: .    -viewer_binary_skip_options
736: .    -viewer_binary_skip_header
737: -    -viewer_binary_mpiio

739:    Level: beginner

741:    Note:
742:    This PetscViewer should be destroyed with PetscViewerDestroy().

744:     For reading files, the filename may begin with ftp:// or http:// and/or
745:     end with .gz; in this case file is brought over and uncompressed.

747:     For creating files, if the file name ends with .gz it is automatically
748:     compressed when closed.

750:     For writing files it only opens the file on processor 0 in the communicator.
751:     For readable files it opens the file on all nodes that have the file. If
752:     node 0 does not have the file it generates an error even if other nodes
753:     do have the file.

755:    Concepts: binary files
756:    Concepts: PetscViewerBinary^creating
757:    Concepts: gzip
758:    Concepts: accessing remote file
759:    Concepts: remote file

761: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
762:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
763:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscViewerBinaryRead()
764: @*/
765: PetscErrorCode PetscViewerBinaryOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv)
766: {

770:   PetscViewerCreate(comm,binv);
771:   PetscViewerSetType(*binv,PETSCVIEWERBINARY);
772:   PetscViewerFileSetMode(*binv,type);
773:   PetscViewerFileSetName(*binv,name);
774:   PetscViewerSetFromOptions(*binv);
775:   return(0);
776: }

778: #if defined(PETSC_HAVE_MPIIO)
779: static PetscErrorCode PetscViewerBinaryWriteReadMPIIO(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype,PetscBool write)
780: {
781:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
782:   PetscErrorCode     ierr;
783:   MPI_Datatype       mdtype;
784:   PetscMPIInt        cnt;
785:   MPI_Status         status;
786:   MPI_Aint           ul,dsize;

789:   PetscMPIIntCast(num,&cnt);
790:   PetscDataTypeToMPIDataType(dtype,&mdtype);
791:   MPI_File_set_view(vbinary->mfdes,vbinary->moff,mdtype,mdtype,(char*)"native",MPI_INFO_NULL);
792:   if (write) {
793:     MPIU_File_write_all(vbinary->mfdes,data,cnt,mdtype,&status);
794:   } else {
795:     MPIU_File_read_all(vbinary->mfdes,data,cnt,mdtype,&status);
796:   }
797:   MPI_Type_get_extent(mdtype,&ul,&dsize);

799:   vbinary->moff += dsize*cnt;
800:   if (count) *count = num;
801:   return(0);
802: }
803: #endif

805: /*@C
806:    PetscViewerBinaryRead - Reads from a binary file, all processors get the same result

808:    Collective on MPI_Comm

810:    Input Parameters:
811: +  viewer - the binary viewer
812: .  data - location of the data to be written
813: .  num - number of items of data to read
814: -  dtype - type of data to read

816:    Output Parameters:
817: .  count - number of items of data actually read, or NULL. Unless an error is generated this is always set to the input parameter num.

819:    Level: beginner

821:    Concepts: binary files

823:    Developer Note: Since count is always set to num it is not clear what purpose the output argument count serves.

825: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
826:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
827:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
828: @*/
829: PetscErrorCode PetscViewerBinaryRead(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype)
830: {
831:   PetscErrorCode     ierr;
832:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

834:   PetscViewerSetUp(viewer);
835: #if defined(PETSC_HAVE_MPIIO)
836:   if (vbinary->usempiio) {
837:     PetscViewerBinaryWriteReadMPIIO(viewer,data,num,count,dtype,PETSC_FALSE);
838:   } else {
839: #endif
840:     PetscBinarySynchronizedRead(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,num,dtype);
841:     if (count) *count = num;
842: #if defined(PETSC_HAVE_MPIIO)
843:   }
844: #endif
845:   return(0);
846: }

848: /*@C
849:    PetscViewerBinaryWrite - writes to a binary file, only from the first process

851:    Collective on MPI_Comm

853:    Input Parameters:
854: +  viewer - the binary viewer
855: .  data - location of data
856: .  count - number of items of data to write
857: .  dtype - type of data to write
858: -  istemp - data may be overwritten

860:    Level: beginner

862:    Notes: because byte-swapping may be done on the values in data it cannot be declared const

864:    Concepts: binary files

866: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
867:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), PetscDataType
868:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
869: @*/
870: PetscErrorCode PetscViewerBinaryWrite(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool istemp)
871: {
872:   PetscErrorCode     ierr;
873:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

876:   PetscViewerSetUp(viewer);
877: #if defined(PETSC_HAVE_MPIIO)
878:   if (vbinary->usempiio) {
879:     PetscViewerBinaryWriteReadMPIIO(viewer,data,count,NULL,dtype,PETSC_TRUE);
880:   } else {
881: #endif
882:     PetscBinarySynchronizedWrite(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,count,dtype,istemp);
883: #if defined(PETSC_HAVE_MPIIO)
884:   }
885: #endif
886:   return(0);
887: }

889: /*@C
890:    PetscViewerBinaryWriteStringArray - writes to a binary file, only from the first process an array of strings

892:    Collective on MPI_Comm

894:    Input Parameters:
895: +  viewer - the binary viewer
896: -  data - location of the array of strings


899:    Level: intermediate

901:    Concepts: binary files

903:     Notes: array of strings is null terminated

905: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
906:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
907:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
908: @*/
909: PetscErrorCode PetscViewerBinaryWriteStringArray(PetscViewer viewer,char **data)
910: {
912:   PetscInt       i,n = 0,*sizes;

914:   PetscViewerSetUp(viewer);
915:   /* count number of strings */
916:   while (data[n++]) ;
917:   n--;
918:   PetscMalloc1(n+1,&sizes);
919:   sizes[0] = n;
920:   for (i=0; i<n; i++) {
921:     size_t tmp;
922:     PetscStrlen(data[i],&tmp);
923:     sizes[i+1] = tmp + 1;   /* size includes space for the null terminator */
924:   }
925:   PetscViewerBinaryWrite(viewer,sizes,n+1,PETSC_INT,PETSC_FALSE);
926:   for (i=0; i<n; i++) {
927:     PetscViewerBinaryWrite(viewer,data[i],sizes[i+1],PETSC_CHAR,PETSC_FALSE);
928:   }
929:   PetscFree(sizes);
930:   return(0);
931: }

933: /*@C
934:    PetscViewerBinaryReadStringArray - reads a binary file an array of strings

936:    Collective on MPI_Comm

938:    Input Parameter:
939: .  viewer - the binary viewer

941:    Output Parameter:
942: .  data - location of the array of strings

944:    Level: intermediate

946:    Concepts: binary files

948:     Notes: array of strings is null terminated

950: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
951:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
952:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
953: @*/
954: PetscErrorCode PetscViewerBinaryReadStringArray(PetscViewer viewer,char ***data)
955: {
957:   PetscInt       i,n,*sizes,N = 0;

959:   PetscViewerSetUp(viewer);
960:   /* count number of strings */
961:   PetscViewerBinaryRead(viewer,&n,1,NULL,PETSC_INT);
962:   PetscMalloc1(n,&sizes);
963:   PetscViewerBinaryRead(viewer,sizes,n,NULL,PETSC_INT);
964:   for (i=0; i<n; i++) N += sizes[i];
965:   PetscMalloc((n+1)*sizeof(char*) + N*sizeof(char),data);
966:   (*data)[0] = (char*)((*data) + n + 1);
967:   for (i=1; i<n; i++) (*data)[i] = (*data)[i-1] + sizes[i-1];
968:   PetscViewerBinaryRead(viewer,(*data)[0],N,NULL,PETSC_CHAR);

970:   (*data)[n] = 0;

972:   PetscFree(sizes);
973:   return(0);
974: }

976: static PetscErrorCode PetscViewerFileGetName_Binary(PetscViewer viewer,const char **name)
977: {
978:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

981:   *name = vbinary->filename;
982:   return(0);
983: }

985: /*@C
986:      PetscViewerFileGetMode - Gets the type of file to be open

988:     Not Collective

990:   Input Parameter:
991: .  viewer - the PetscViewer; must be a PETSCVIEWERBINARY, PETSCVIEWERMATLAB, PETSCVIEWERHDF5, or PETSCVIEWERASCII  PetscViewer

993:   Output Parameter:
994: .  type - type of file
995: $    FILE_MODE_WRITE - create new file for binary output
996: $    FILE_MODE_READ - open existing file for binary input
997: $    FILE_MODE_APPEND - open existing file for binary output

999:   Level: advanced

1001: .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()

1003: @*/
1004: PetscErrorCode PetscViewerFileGetMode(PetscViewer viewer,PetscFileMode *type)
1005: {

1011:   PetscUseMethod(viewer,"PetscViewerFileGetMode_C",(PetscViewer,PetscFileMode*),(viewer,type));
1012:   return(0);
1013: }

1015: /*@
1016:     PetscViewerBinarySetUseMPIIO - Sets a binary viewer to use MPI-IO for reading/writing. Must be called
1017:         before PetscViewerFileSetName()

1019:     Logically Collective on PetscViewer

1021:     Input Parameters:
1022: +   viewer - the PetscViewer; must be a binary
1023: -   flg - PETSC_TRUE means MPI-IO will be used

1025:     Options Database:
1026:     -viewer_binary_mpiio : Flag for using MPI-IO

1028:     Level: advanced

1030: .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen(),
1031:           PetscViewerBinaryGetUseMPIIO()

1033: @*/
1034: PetscErrorCode PetscViewerBinarySetUseMPIIO(PetscViewer viewer,PetscBool flg)
1035: {

1040:   PetscTryMethod(viewer,"PetscViewerBinarySetUseMPIIO_C",(PetscViewer,PetscBool),(viewer,flg));
1041:   return(0);
1042: }

1044: /*@C
1045:      PetscViewerFileSetMode - Sets the type of file to be open

1047:     Logically Collective on PetscViewer

1049:   Input Parameters:
1050: +  viewer - the PetscViewer; must be a a PETSCVIEWERBINARY, PETSCVIEWERMATLAB, PETSCVIEWERHDF5, or PETSCVIEWERASCII  PetscViewer
1051: -  type - type of file
1052: $    FILE_MODE_WRITE - create new file for binary output
1053: $    FILE_MODE_READ - open existing file for binary input
1054: $    FILE_MODE_APPEND - open existing file for binary output

1056:   Level: advanced

1058: .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()

1060: @*/
1061: PetscErrorCode PetscViewerFileSetMode(PetscViewer viewer,PetscFileMode type)
1062: {

1068:   PetscTryMethod(viewer,"PetscViewerFileSetMode_C",(PetscViewer,PetscFileMode),(viewer,type));
1069:   return(0);
1070: }

1072: static PetscErrorCode PetscViewerFileGetMode_Binary(PetscViewer viewer,PetscFileMode *type)
1073: {
1074:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

1077:   *type = vbinary->btype;
1078:   return(0);
1079: }

1081: static PetscErrorCode PetscViewerFileSetMode_Binary(PetscViewer viewer,PetscFileMode type)
1082: {
1083:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

1086:   vbinary->btype = type;
1087:   return(0);
1088: }

1090: static PetscErrorCode PetscViewerFileSetName_Binary(PetscViewer viewer,const char name[])
1091: {
1092:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1093:   PetscErrorCode     ierr;
1094: 
1096:   if (vbinary->filename) { PetscFree(vbinary->filename); }
1097:   PetscStrallocpy(name,&vbinary->filename);
1098:   return(0);
1099: }
1100: /*
1101:         Actually opens the file
1102: */
1103: static PetscErrorCode PetscViewerFileSetUp_Binary(PetscViewer viewer)
1104: {
1105:   PetscMPIInt        rank;
1106:   PetscErrorCode     ierr;
1107:   size_t             len;
1108:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1109:   const char         *fname;
1110:   char               bname[PETSC_MAX_PATH_LEN],*gz;
1111:   PetscBool          found;
1112:   PetscFileMode      type = vbinary->btype;

1115:   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()");
1116:   if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()");
1117:   PetscViewerFileClose_Binary(viewer);

1119:   MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);

1121:   /* if ends in .gz strip that off and note user wants file compressed */
1122:   vbinary->storecompressed = PETSC_FALSE;
1123:   if (!rank && type == FILE_MODE_WRITE) {
1124:     /* remove .gz if it ends library name */
1125:     PetscStrstr(vbinary->filename,".gz",&gz);
1126:     if (gz) {
1127:       PetscStrlen(gz,&len);
1128:       if (len == 3) {
1129:         *gz = 0;
1130:         vbinary->storecompressed = PETSC_TRUE;
1131:       }
1132:     }
1133:   }

1135:   /* only first processor opens file if writeable */
1136:   if (!rank || type == FILE_MODE_READ) {

1138:     if (type == FILE_MODE_READ) {
1139:       /* possibly get the file from remote site or compressed file */
1140:       PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),vbinary->filename,bname,PETSC_MAX_PATH_LEN,&found);
1141:       fname = bname;
1142:       if (!rank && !found) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot locate file: %s on node zero",vbinary->filename);
1143:       else if (!found) {
1144:         PetscInfo(viewer,"Nonzero processor did not locate readonly file\n");
1145:         fname = 0;
1146:       }
1147:     } else fname = vbinary->filename;

1149: #if defined(PETSC_HAVE_O_BINARY)
1150:     if (type == FILE_MODE_WRITE) {
1151:       if ((vbinary->fdes = open(fname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname);
1152:     } else if (type == FILE_MODE_READ && fname) {
1153:       if ((vbinary->fdes = open(fname,O_RDONLY|O_BINARY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s for reading",fname);
1154:     } else if (type == FILE_MODE_APPEND) {
1155:       if ((vbinary->fdes = open(fname,O_WRONLY|O_APPEND|O_BINARY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s for writing",fname);
1156:     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
1157: #else
1158:     if (type == FILE_MODE_WRITE) {
1159:       if ((vbinary->fdes = creat(fname,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname);
1160:     } else if (type == FILE_MODE_READ && fname) {
1161:       if ((vbinary->fdes = open(fname,O_RDONLY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s for reading",fname);
1162:     } else if (type == FILE_MODE_APPEND) {
1163:       if ((vbinary->fdes = open(fname,O_WRONLY|O_APPEND,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s for writing",fname);
1164:     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
1165: #endif
1166:   } else vbinary->fdes = -1;

1168:   /*
1169:       try to open info file: all processors open this file if read only
1170:   */
1171:   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
1172:     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];

1174:     PetscStrcpy(infoname,vbinary->filename);
1175:     /* remove .gz if it ends library name */
1176:     PetscStrstr(infoname,".gz",&gz);
1177:     if (gz) {
1178:       PetscStrlen(gz,&len);
1179:       if (len == 3) *gz = 0;
1180:     }

1182:     PetscStrcat(infoname,".info");
1183:     PetscFixFilename(infoname,iname);
1184:     if (type == FILE_MODE_READ) {
1185:       PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);
1186:       PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),((PetscObject)viewer)->options,infoname,PETSC_FALSE);
1187:     } else {
1188:       vbinary->fdes_info = fopen(infoname,"w");
1189:       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
1190:     }
1191:   }
1192: #if defined(PETSC_USE_LOG)
1193:   PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename);
1194: #endif
1195:   return(0);
1196: }

1198: #if defined(PETSC_HAVE_MPIIO)
1199: static PetscErrorCode PetscViewerFileSetUp_BinaryMPIIO(PetscViewer viewer)
1200: {
1201:   PetscMPIInt        rank;
1202:   PetscErrorCode     ierr;
1203:   size_t             len;
1204:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1205:   char               *gz;
1206:   PetscBool          found;
1207:   PetscFileMode      type = vbinary->btype;

1210:   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()");
1211:   if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()");
1212:   PetscViewerFileClose_BinaryMPIIO(viewer);

1214:   MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);

1216:   vbinary->storecompressed = PETSC_FALSE;

1218:   /* only first processor opens file if writeable */
1219:   if (type == FILE_MODE_READ) {
1220:     MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_RDONLY,MPI_INFO_NULL,&vbinary->mfdes);
1221:   } else if (type == FILE_MODE_WRITE) {
1222:     MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_WRONLY | MPI_MODE_CREATE,MPI_INFO_NULL,&vbinary->mfdes);
1223:   }

1225:   /*
1226:       try to open info file: all processors open this file if read only

1228:       Below is identical code to the code for Binary above, should be put in separate routine
1229:   */
1230:   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
1231:     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];

1233:     PetscStrcpy(infoname,vbinary->filename);
1234:     /* remove .gz if it ends library name */
1235:     PetscStrstr(infoname,".gz",&gz);
1236:     if (gz) {
1237:       PetscStrlen(gz,&len);
1238:       if (len == 3) *gz = 0;
1239:     }

1241:     PetscStrcat(infoname,".info");
1242:     PetscFixFilename(infoname,iname);
1243:     if (type == FILE_MODE_READ) {
1244:       PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);
1245:       PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),((PetscObject)viewer)->options,infoname,PETSC_FALSE);
1246:     } else {
1247:       vbinary->fdes_info = fopen(infoname,"w");
1248:       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
1249:     }
1250:   }
1251: #if defined(PETSC_USE_LOG)
1252:   PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename);
1253: #endif
1254:   return(0);
1255: }

1257: static PetscErrorCode PetscViewerBinarySetUseMPIIO_Binary(PetscViewer viewer,PetscBool flg)
1258: {
1259:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1261:   vbinary->usempiio = flg;
1262:   return(0);
1263: }
1264: #endif

1266: static PetscErrorCode PetscViewerView_Binary(PetscViewer v,PetscViewer viewer)
1267: {
1268:   PetscErrorCode     ierr;
1269:   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;

1272:   if (binary->filename) {
1273:     PetscViewerASCIIPrintf(viewer,"Filename: %s\n",binary->filename);
1274:   }
1275:   return(0);
1276: }

1278: static PetscErrorCode PetscViewerSetUp_Binary(PetscViewer v)
1279: {
1280:   PetscErrorCode     ierr;
1281:   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;

1284:   if (!binary->setfromoptionscalled) { PetscViewerSetFromOptions(v); }
1285: 
1286: #if defined(PETSC_HAVE_MPIIO)
1287:   if (binary->usempiio) {
1288:     PetscViewerFileSetUp_BinaryMPIIO(v);
1289:   } else {
1290: #endif
1291:     PetscViewerFileSetUp_Binary(v);
1292: #if defined(PETSC_HAVE_MPIIO)
1293:   }
1294: #endif
1295:   return(0);
1296: }

1298: static PetscErrorCode PetscViewerSetFromOptions_Binary(PetscOptionItems *PetscOptionsObject,PetscViewer v)
1299: {
1300:   PetscErrorCode     ierr;
1301:   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;
1302:   char               defaultname[PETSC_MAX_PATH_LEN];
1303:   PetscBool          flg;

1306:   PetscOptionsHead(PetscOptionsObject,"Binary PetscViewer Options");
1307:   PetscSNPrintf(defaultname,PETSC_MAX_PATH_LEN-1,"binaryoutput");
1308:   PetscOptionsString("-viewer_binary_filename","Specify filename","PetscViewerFileSetName",defaultname,defaultname,PETSC_MAX_PATH_LEN-1,&flg);
1309:   if (flg) { PetscViewerFileSetName_Binary(v,defaultname); }
1310:   PetscOptionsBool("-viewer_binary_skip_info","Skip writing/reading .info file","PetscViewerBinarySetSkipInfo",PETSC_FALSE,&binary->skipinfo,NULL);
1311:   PetscOptionsBool("-viewer_binary_skip_options","Skip parsing vec load options","PetscViewerBinarySetSkipOptions",PETSC_TRUE,&binary->skipoptions,NULL);
1312:   PetscOptionsBool("-viewer_binary_skip_header","Skip writing/reading header information","PetscViewerBinarySetSkipHeader",PETSC_FALSE,&binary->skipheader,NULL);
1313: #if defined(PETSC_HAVE_MPIIO)
1314:   PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file","PetscViewerBinarySetUseMPIIO",PETSC_FALSE,&binary->usempiio,NULL);
1315: #elif defined(PETSC_HAVE_MPIUNI)
1316:   PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file","PetscViewerBinarySetUseMPIIO",PETSC_FALSE,NULL,NULL);
1317: #endif
1318:   PetscOptionsTail();
1319:   binary->setfromoptionscalled = PETSC_TRUE;
1320:   return(0);
1321: }

1323: /*MC
1324:    PETSCVIEWERBINARY - A viewer that saves to binary files


1327: .seealso:  PetscViewerBinaryOpen(), PETSC_VIEWER_STDOUT_(),PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDOUT_WORLD, PetscViewerCreate(), PetscViewerASCIIOpen(),
1328:            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB, PETSCVIEWERDRAW,
1329:            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()

1331:   Level: beginner

1333: M*/

1335: PETSC_EXTERN PetscErrorCode PetscViewerCreate_Binary(PetscViewer v)
1336: {
1337:   PetscErrorCode     ierr;
1338:   PetscViewer_Binary *vbinary;

1341:   PetscNewLog(v,&vbinary);
1342:   v->data                  = (void*)vbinary;
1343:   v->ops->setfromoptions   = PetscViewerSetFromOptions_Binary;
1344:   v->ops->destroy          = PetscViewerDestroy_Binary;
1345:   v->ops->view             = PetscViewerView_Binary;
1346:   v->ops->setup            = PetscViewerSetUp_Binary;
1347:   v->ops->flush            = NULL;
1348:   vbinary->fdes_info       = 0;
1349:   vbinary->fdes            = 0;
1350:   vbinary->skipinfo        = PETSC_FALSE;
1351:   vbinary->skipoptions     = PETSC_TRUE;
1352:   vbinary->skipheader      = PETSC_FALSE;
1353:   vbinary->setfromoptionscalled = PETSC_FALSE;
1354:   v->ops->getsubviewer     = PetscViewerGetSubViewer_Binary;
1355:   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_Binary;
1356:   v->ops->read             = PetscViewerBinaryRead;
1357:   vbinary->btype           = (PetscFileMode) -1;
1358:   vbinary->storecompressed = PETSC_FALSE;
1359:   vbinary->filename        = 0;
1360:   vbinary->flowcontrol     = 256; /* seems a good number for Cray XT-5 */

1362:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",PetscViewerBinaryGetFlowControl_Binary);
1363:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetFlowControl_C",PetscViewerBinarySetFlowControl_Binary);
1364:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",PetscViewerBinarySetSkipHeader_Binary);
1365:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",PetscViewerBinaryGetSkipHeader_Binary);
1366:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipOptions_C",PetscViewerBinaryGetSkipOptions_Binary);
1367:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipOptions_C",PetscViewerBinarySetSkipOptions_Binary);
1368:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipInfo_C",PetscViewerBinaryGetSkipInfo_Binary);
1369:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipInfo_C",PetscViewerBinarySetSkipInfo_Binary);
1370:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetInfoPointer_C",PetscViewerBinaryGetInfoPointer_Binary);
1371:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_Binary);
1372:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_Binary);
1373:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_Binary);
1374:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_Binary);
1375: #if defined(PETSC_HAVE_MPIIO)
1376:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetUseMPIIO_C",PetscViewerBinaryGetUseMPIIO_Binary);
1377:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetUseMPIIO_C",PetscViewerBinarySetUseMPIIO_Binary);
1378: #endif
1379:   return(0);
1380: }

1382: /* ---------------------------------------------------------------------*/
1383: /*
1384:     The variable Petsc_Viewer_Binary_keyval is used to indicate an MPI attribute that
1385:   is attached to a communicator, in this case the attribute is a PetscViewer.
1386: */
1387: static int Petsc_Viewer_Binary_keyval = MPI_KEYVAL_INVALID;

1389: /*@C
1390:      PETSC_VIEWER_BINARY_ - Creates a binary PetscViewer shared by all processors
1391:                      in a communicator.

1393:      Collective on MPI_Comm

1395:      Input Parameter:
1396: .    comm - the MPI communicator to share the binary PetscViewer

1398:      Level: intermediate

1400:    Options Database Keys:
1401: +    -viewer_binary_filename <name>
1402: .    -viewer_binary_skip_info
1403: .    -viewer_binary_skip_options
1404: .    -viewer_binary_skip_header
1405: -    -viewer_binary_mpiio

1407:    Environmental variables:
1408: -   PETSC_VIEWER_BINARY_FILENAME

1410:      Notes:
1411:      Unlike almost all other PETSc routines, PETSC_VIEWER_BINARY_ does not return
1412:      an error code.  The binary PetscViewer is usually used in the form
1413: $       XXXView(XXX object,PETSC_VIEWER_BINARY_(comm));

1415: .seealso: PETSC_VIEWER_BINARY_WORLD, PETSC_VIEWER_BINARY_SELF, PetscViewerBinaryOpen(), PetscViewerCreate(),
1416:           PetscViewerDestroy()
1417: @*/
1418: PetscViewer PETSC_VIEWER_BINARY_(MPI_Comm comm)
1419: {
1421:   PetscBool      flg;
1422:   PetscViewer    viewer;
1423:   char           fname[PETSC_MAX_PATH_LEN];
1424:   MPI_Comm       ncomm;

1427:   PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1428:   if (Petsc_Viewer_Binary_keyval == MPI_KEYVAL_INVALID) {
1429:     MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Binary_keyval,0);
1430:     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1431:   }
1432:   MPI_Attr_get(ncomm,Petsc_Viewer_Binary_keyval,(void**)&viewer,(int*)&flg);
1433:   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1434:   if (!flg) { /* PetscViewer not yet created */
1435:     PetscOptionsGetenv(ncomm,"PETSC_VIEWER_BINARY_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
1436:     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1437:     if (!flg) {
1438:       PetscStrcpy(fname,"binaryoutput");
1439:       if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1440:     }
1441:     PetscViewerBinaryOpen(ncomm,fname,FILE_MODE_WRITE,&viewer);
1442:     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1443:     PetscObjectRegisterDestroy((PetscObject)viewer);
1444:     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1445:     MPI_Attr_put(ncomm,Petsc_Viewer_Binary_keyval,(void*)viewer);
1446:     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1447:   }
1448:   PetscCommDestroy(&ncomm);
1449:   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1450:   PetscFunctionReturn(viewer);
1451: }