Actual source code: binv.c

petsc-3.6.4 2016-04-12
Report Typos and Errors
  2: #include <petsc/private/viewerimpl.h>    /*I   "petscviewer.h"   I*/
  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;

 32: PetscErrorCode PetscViewerGetSingleton_Binary(PetscViewer viewer,PetscViewer *outviewer)
 33: {
 34:   int                rank;
 35:   PetscErrorCode     ierr;
 36:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data,*obinary;

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

 52: PetscErrorCode PetscViewerRestoreSingleton_Binary(PetscViewer viewer,PetscViewer *outviewer)
 53: {
 55:   PetscErrorCode rank;

 58:   MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);
 59:   if (!rank) {
 60:     PetscFree((*outviewer)->data);
 61:     PetscHeaderDestroy(outviewer);
 62:   }
 63:   return(0);
 64: }

 66: #if defined(PETSC_HAVE_MPIIO)
 69: /*@C
 70:     PetscViewerBinaryGetMPIIOOffset - Gets the current offset that should be passed to MPI_File_set_view()

 72:     Not Collective

 74:     Input Parameter:
 75: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

 77:     Output Parameter:
 78: .    off - the current offset

 80:     Level: advanced

 82:     Fortran Note:
 83:     This routine is not supported in Fortran.

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

 87:   Concepts: file descriptor^getting
 88:   Concepts: PetscViewerBinary^accessing file descriptor

 90: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
 91: @*/
 92: PetscErrorCode PetscViewerBinaryGetMPIIOOffset(PetscViewer viewer,MPI_Offset *off)
 93: {
 94:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

 97:   *off = vbinary->moff;
 98:   return(0);
 99: }

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

106:     Not Collective

108:     Input Parameters:
109: +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
110: -    off - the addition to the offset

112:     Level: advanced

114:     Fortran Note:
115:     This routine is not supported in Fortran.

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

119:   Concepts: file descriptor^getting
120:   Concepts: PetscViewerBinary^accessing file descriptor

122: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
123: @*/
124: PetscErrorCode PetscViewerBinaryAddMPIIOOffset(PetscViewer viewer,MPI_Offset off)
125: {
126:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

129:   vbinary->moff += off;
130:   return(0);
131: }

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

138:     Not Collective

140:     Input Parameter:
141: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

143:     Output Parameter:
144: .   fdes - file descriptor

146:     Level: advanced

148:     Fortran Note:
149:     This routine is not supported in Fortran.

151:   Concepts: file descriptor^getting
152:   Concepts: PetscViewerBinary^accessing file descriptor

154: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
155: @*/
156: PetscErrorCode PetscViewerBinaryGetMPIIODescriptor(PetscViewer viewer,MPI_File *fdes)
157: {
158:   PetscErrorCode     ierr;
159:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

162:   PetscViewerSetUp(viewer);
163:   *fdes = vbinary->mfdes;
164:   return(0);
165: }

169: PetscErrorCode PetscViewerBinaryGetUseMPIIO_Binary(PetscViewer viewer,PetscBool  *flg)
170: {
171:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
172: 
174:   *flg = vbinary->usempiio;
175:   return(0);
176: }
177: #endif


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

185:     Not Collective

187:     Input Parameter:
188: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

190:     Output Parameter:
191: -   flg - PETSC_TRUE if MPI-IO is being used

193:     Options Database:
194:     -viewer_binary_mpiio : Flag for using MPI-IO

196:     Level: advanced

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

201:     Fortran Note:
202:     This routine is not supported in Fortran.

204:   Concepts: file descriptor^getting
205:   Concepts: PetscViewerBinary^accessing file descriptor

207: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer()
208: @*/
209: PetscErrorCode PetscViewerBinaryGetUseMPIIO(PetscViewer viewer,PetscBool *flg)
210: {
212: 
214:   *flg = PETSC_FALSE;
215:   PetscTryMethod(viewer,"PetscViewerBinaryGetUseMPIIO_C",(PetscViewer,PetscBool*),(viewer,flg));
216:   return(0);
217: }

221: PetscErrorCode  PetscViewerBinaryGetFlowControl_Binary(PetscViewer viewer,PetscInt *fc)
222: {
223:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

226:   *fc = vbinary->flowcontrol;
227:   return(0);
228: }

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

235:     Not Collective

237:     Input Parameter:
238: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

240:     Output Parameter:
241: .   fc - the number of messages

243:     Level: advanced

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

247: @*/
248: PetscErrorCode PetscViewerBinaryGetFlowControl(PetscViewer viewer,PetscInt *fc)
249: {

253:   PetscTryMethod(viewer,"PetscViewerBinaryGetFlowControl_C",(PetscViewer,PetscInt*),(viewer,fc));
254:   return(0);
255: }

259: PetscErrorCode PetscViewerBinarySetFlowControl_Binary(PetscViewer viewer,PetscInt fc)
260: {
261:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

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

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

274:     Not Collective

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

280:     Level: advanced

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

284: @*/
285: PetscErrorCode  PetscViewerBinarySetFlowControl(PetscViewer viewer,PetscInt fc)
286: {

290:   PetscUseMethod(viewer,"PetscViewerBinarySetFlowControl_C",(PetscViewer,PetscInt),(viewer,fc));
291:   return(0);
292: }

296: /*@C
297:     PetscViewerBinaryGetDescriptor - Extracts the file descriptor from a PetscViewer.

299:     Collective On PetscViewer

301:     Input Parameter:
302: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

304:     Output Parameter:
305: .   fdes - file descriptor

307:     Level: advanced

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

315:     Fortran Note:
316:     This routine is not supported in Fortran.

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


322:   Concepts: file descriptor^getting
323:   Concepts: PetscViewerBinary^accessing file descriptor

325: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
326: @*/
327: PetscErrorCode PetscViewerBinaryGetDescriptor(PetscViewer viewer,int *fdes)
328: {
329:   PetscErrorCode     ierr;
330:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

333:   PetscViewerSetUp(viewer);
334:   *fdes = vbinary->fdes;
335:   return(0);
336: }

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

343:     Not Collective

345:     Input Paramter:
346: .   viewer - PetscViewer context, obtained from PetscViewerCreate()

348:     Options Database Key:
349: .   -viewer_binary_skip_info

351:     Level: advanced

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

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

360:    Concepts: PetscViewerBinary^accessing info file

362: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(),
363:           PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo()
364: @*/
365: PetscErrorCode PetscViewerBinarySkipInfo(PetscViewer viewer)
366: {
367:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

370:   vbinary->skipinfo = PETSC_TRUE;
371:   return(0);
372: }

376: PetscErrorCode PetscViewerBinarySetSkipInfo_Binary(PetscViewer viewer,PetscBool skip)
377: {
378:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

381:   vbinary->skipinfo = skip;
382:   return(0);
383: }

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

390:     Not Collective

392:     Input Paramter:
393: .   viewer - PetscViewer context, obtained from PetscViewerCreate()

395:     Options Database Key:
396: .   -viewer_binary_skip_info

398:     Level: advanced

400:     Concepts: PetscViewerBinary^accessing info file

402: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(),
403:           PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo()
404: @*/
405: PetscErrorCode PetscViewerBinarySetSkipInfo(PetscViewer viewer,PetscBool skip)
406: {

410:   PetscUseMethod(viewer,"PetscViewerBinarySetSkipInfo_C",(PetscViewer,PetscBool),(viewer,skip));
411:   return(0);
412: }

416: PetscErrorCode PetscViewerBinaryGetSkipInfo_Binary(PetscViewer viewer,PetscBool *skip)
417: {
418:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

421:   *skip  = vbinary->skipinfo;
422:   return(0);
423: }

427: /*@
428:     PetscViewerBinaryGetSkipInfo - check if viewer wrote a .info file

430:     Not Collective

432:     Input Parameter:
433: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

435:     Output Parameter:
436: .   skip - PETSC_TRUE implies the .info file was not generated

438:     Level: advanced

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

442:     Concepts: PetscViewerBinary^accessing info file

444: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
445:           PetscViewerBinarySetSkipOptions(), PetscViewerBinarySetSkipInfo()
446: @*/
447: PetscErrorCode PetscViewerBinaryGetSkipInfo(PetscViewer viewer,PetscBool *skip)
448: {

452:   PetscUseMethod(viewer,"PetscViewerBinaryGetSkipInfo_C",(PetscViewer,PetscBool*),(viewer,skip));
453:   return(0);
454: }

458: PetscErrorCode PetscViewerBinarySetSkipOptions_Binary(PetscViewer viewer,PetscBool skip)
459: {
460:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

463:   vbinary->skipoptions = skip;
464:   return(0);
465: }

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

472:     Not Collective

474:     Input Parameters:
475: +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
476: -   skip - PETSC_TRUE means do not use

478:     Options Database Key:
479: .   -viewer_binary_skip_options

481:     Level: advanced

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

485:    Concepts: PetscViewerBinary^accessing info file

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

495:   PetscUseMethod(viewer,"PetscViewerBinarySetSkipOptions_C",(PetscViewer,PetscBool),(viewer,skip));
496:   return(0);
497: }

501: PetscErrorCode PetscViewerBinaryGetSkipOptions_Binary(PetscViewer viewer,PetscBool *skip)
502: {
503:   PetscViewer_Binary *vbinary;

507:   vbinary = (PetscViewer_Binary*)viewer->data;
508:   *skip   = vbinary->skipoptions;
509:   return(0);
510: }

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

517:     Not Collective

519:     Input Parameter:
520: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

522:     Output Parameter:
523: .   skip - PETSC_TRUE means do not use

525:     Level: advanced

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

529:    Concepts: PetscViewerBinary^accessing info file

531: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
532:           PetscViewerBinarySetSkipOptions()
533: @*/
534: PetscErrorCode PetscViewerBinaryGetSkipOptions(PetscViewer viewer,PetscBool *skip)
535: {

539:   PetscUseMethod(viewer,"PetscViewerBinaryGetSkipOptions_C",(PetscViewer,PetscBool*),(viewer,skip));
540:   return(0);
541: }

545: PetscErrorCode PetscViewerBinarySetSkipHeader_Binary(PetscViewer viewer,PetscBool skip)
546: {
547:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

550:   vbinary->skipheader = skip;
551:   return(0);
552: }

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

559:     Not Collective

561:     Input Parameters:
562: +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
563: -   skip - PETSC_TRUE means do not write header

565:     Options Database Key:
566: .   -viewer_binary_skip_header

568:     Level: advanced

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

572:            Can ONLY be called on a binary viewer

574: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
575:           PetscViewerBinaryGetSkipHeader()
576: @*/
577: PetscErrorCode PetscViewerBinarySetSkipHeader(PetscViewer viewer,PetscBool skip)
578: {

582:   PetscUseMethod(viewer,"PetscViewerBinarySetSkipHeader_C",(PetscViewer,PetscBool),(viewer,skip));
583:   return(0);
584: }

588: PetscErrorCode PetscViewerBinaryGetSkipHeader_Binary(PetscViewer viewer,PetscBool  *skip)
589: {
590:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

593:   *skip = vbinary->skipheader;
594:   return(0);
595: }

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

602:     Not Collective

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

607:     Output Parameter:
608: .   skip - PETSC_TRUE means do not write header

610:     Level: advanced

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

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

616: .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
617:           PetscViewerBinarySetSkipHeader()
618: @*/
619: PetscErrorCode PetscViewerBinaryGetSkipHeader(PetscViewer viewer,PetscBool  *skip)
620: {

624:   *skip = PETSC_FALSE;
625:   PetscTryMethod(viewer,"PetscViewerBinaryGetSkipHeader_C",(PetscViewer,PetscBool*),(viewer,skip));
626:   return(0);
627: }

631: PetscErrorCode PetscViewerBinaryGetInfoPointer_Binary(PetscViewer viewer,FILE **file)
632: {
633:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
634:   PetscErrorCode     ierr;
635:   MPI_Comm           comm;

638:   PetscViewerSetUp(viewer);
639:   *file = vbinary->fdes_info;
640:   if (viewer->format == PETSC_VIEWER_BINARY_MATLAB && !vbinary->matlabheaderwritten) {
641:     vbinary->matlabheaderwritten = PETSC_TRUE;
642:     PetscObjectGetComm((PetscObject)viewer,&comm);
643:     PetscFPrintf(comm,*file,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");
644:     PetscFPrintf(comm,*file,"#$$ Set.filename = '%s';\n",vbinary->filename);
645:     PetscFPrintf(comm,*file,"#$$ fd = PetscOpenFile(Set.filename);\n");
646:     PetscFPrintf(comm,*file,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");
647:   }
648:   return(0);
649: }

653: /*@C
654:     PetscViewerBinaryGetInfoPointer - Extracts the file pointer for the ASCII
655:           info file associated with a binary file.

657:     Not Collective

659:     Input Parameter:
660: .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()

662:     Output Parameter:
663: .   file - file pointer  Always returns NULL if not a binary viewer

665:     Level: advanced

667:     Notes:
668:       For writable binary PetscViewers, the descriptor will only be valid for the
669:     first processor in the communicator that shares the PetscViewer.

671:     Fortran Note:
672:     This routine is not supported in Fortran.

674:   Concepts: PetscViewerBinary^accessing info file

676: .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetDescriptor()
677: @*/
678: PetscErrorCode PetscViewerBinaryGetInfoPointer(PetscViewer viewer,FILE **file)
679: {

683:   *file = NULL;
684:   PetscTryMethod(viewer,"PetscViewerBinaryGetInfoPointer_C",(PetscViewer,FILE **),(viewer,file));
685:   return(0);
686: }

690: static PetscErrorCode PetscViewerFileClose_Binary(PetscViewer v)
691: {
692:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
693:   PetscErrorCode     ierr;
694:   PetscMPIInt        rank;
695:   int                err;

698:   MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);
699:   if ((!rank || vbinary->btype == FILE_MODE_READ) && vbinary->fdes) {
700:     close(vbinary->fdes);
701:     if (!rank && vbinary->storecompressed) {
702:       char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
703:       FILE *fp;
704:       /* compress the file */
705:       PetscStrcpy(par,"gzip -f ");
706:       PetscStrcat(par,vbinary->filename);
707: #if defined(PETSC_HAVE_POPEN)
708:       PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);
709:       if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from command %s\n%s",par,buf);
710:       PetscPClose(PETSC_COMM_SELF,fp,NULL);
711: #else
712:       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
713: #endif
714:     }
715:   }
716:   if (vbinary->fdes_info) {
717:     err = fclose(vbinary->fdes_info);
718:     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
719:   }
720:   return(0);
721: }

723: #if defined(PETSC_HAVE_MPIIO)
726: static PetscErrorCode PetscViewerFileClose_BinaryMPIIO(PetscViewer v)
727: {
728:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
729:   int                err;
730:   PetscErrorCode     ierr;

733:   if (vbinary->mfdes) {
734:     MPI_File_close(&vbinary->mfdes);
735:   }
736:   if (vbinary->fdes_info) {
737:     err = fclose(vbinary->fdes_info);
738:     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
739:   }
740:   return(0);
741: }
742: #endif

746: PetscErrorCode PetscViewerDestroy_Binary(PetscViewer v)
747: {
748:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
749:   PetscErrorCode     ierr;

752:   if (v->format == PETSC_VIEWER_BINARY_MATLAB) {
753:     MPI_Comm comm;
754:     FILE     *info;

756:     PetscObjectGetComm((PetscObject)v,&comm);
757:     PetscViewerBinaryGetInfoPointer(v,&info);
758:     PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");
759:     PetscFPrintf(comm,info,"#$$ close(fd);\n");
760:     PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");
761:   }
762: #if defined(PETSC_HAVE_MPIIO)
763:   if (vbinary->usempiio) {
764:     PetscViewerFileClose_BinaryMPIIO(v);
765:   } else {
766: #endif
767:     PetscViewerFileClose_Binary(v);
768: #if defined(PETSC_HAVE_MPIIO)
769:   }
770: #endif
771:   if (vbinary->filename) { PetscFree(vbinary->filename); }
772:   PetscFree(vbinary);
773:   return(0);
774: }

778: /*@C
779:    PetscViewerBinaryOpen - Opens a file for binary input/output.

781:    Collective on MPI_Comm

783:    Input Parameters:
784: +  comm - MPI communicator
785: .  name - name of file
786: -  type - type of file
787: $    FILE_MODE_WRITE - create new file for binary output
788: $    FILE_MODE_READ - open existing file for binary input
789: $    FILE_MODE_APPEND - open existing file for binary output

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

794:     Options Database Keys:
795: +    -viewer_binary_filename <name>
796: .    -viewer_binary_skip_info
797: .    -viewer_binary_skip_options
798: .    -viewer_binary_skip_header
799: -    -viewer_binary_mpiio

801:    Level: beginner

803:    Note:
804:    This PetscViewer should be destroyed with PetscViewerDestroy().

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

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

812:     For writing files it only opens the file on processor 0 in the communicator.
813:     For readable files it opens the file on all nodes that have the file. If
814:     node 0 does not have the file it generates an error even if other nodes
815:     do have the file.

817:    Concepts: binary files
818:    Concepts: PetscViewerBinary^creating
819:    Concepts: gzip
820:    Concepts: accessing remote file
821:    Concepts: remote file

823: .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
824:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
825:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscViewerBinaryRead()
826: @*/
827: PetscErrorCode PetscViewerBinaryOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv)
828: {

832:   PetscViewerCreate(comm,binv);
833:   PetscViewerSetType(*binv,PETSCVIEWERBINARY);
834:   PetscViewerFileSetMode(*binv,type);
835:   PetscViewerFileSetName(*binv,name);
836:   PetscViewerSetFromOptions(*binv);
837:   return(0);
838: }

840: #if defined(PETSC_HAVE_MPIIO)
843: static PetscErrorCode PetscViewerBinaryWriteReadMPIIO(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype,PetscBool write)
844: {
845:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
846:   PetscErrorCode     ierr;
847:   MPI_Datatype       mdtype;
848:   PetscMPIInt        cnt;
849:   MPI_Status         status;
850:   MPI_Aint           ul,dsize;

853:   PetscMPIIntCast(num,&cnt);
854:   PetscDataTypeToMPIDataType(dtype,&mdtype);
855:   MPI_File_set_view(vbinary->mfdes,vbinary->moff,mdtype,mdtype,(char*)"native",MPI_INFO_NULL);
856:   if (write) {
857:     MPIU_File_write_all(vbinary->mfdes,data,cnt,mdtype,&status);
858:   } else {
859:     MPIU_File_read_all(vbinary->mfdes,data,cnt,mdtype,&status);
860:   }
861:   MPI_Type_get_extent(mdtype,&ul,&dsize);

863:   vbinary->moff += dsize*cnt;
864:   if (count) *count = num;
865:   return(0);
866: }
867: #endif

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

874:    Collective on MPI_Comm

876:    Input Parameters:
877: +  viewer - the binary viewer
878: .  data - location of the data to be written
879: .  num - number of items of data to read
880: -  dtype - type of data to read

882:    Output Parameters:
883: .  count - number of items of data actually read, or NULL

885:    Level: beginner

887:    Concepts: binary files

889: .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
890:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
891:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
892: @*/
893: PetscErrorCode PetscViewerBinaryRead(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype)
894: {
895:   PetscErrorCode     ierr;
896:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

898:   PetscViewerSetUp(viewer);
899: #if defined(PETSC_HAVE_MPIIO)
900:   if (vbinary->usempiio) {
901:     PetscViewerBinaryWriteReadMPIIO(viewer,data,num,count,dtype,PETSC_FALSE);
902:   } else {
903: #endif
904:     PetscBinarySynchronizedRead(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,num,dtype);
905: #if defined(PETSC_HAVE_MPIIO)
906:   }
907: #endif
908:   return(0);
909: }

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

916:    Collective on MPI_Comm

918:    Input Parameters:
919: +  viewer - the binary viewer
920: .  data - location of data
921: .  count - number of items of data to write
922: .  dtype - type of data to write
923: -  istemp - data may be overwritten

925:    Level: beginner

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

929:    Concepts: binary files

931: .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
932:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), PetscDataType
933:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
934: @*/
935: PetscErrorCode PetscViewerBinaryWrite(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool istemp)
936: {
937:   PetscErrorCode     ierr;
938:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

941:   PetscViewerSetUp(viewer);
942: #if defined(PETSC_HAVE_MPIIO)
943:   if (vbinary->usempiio) {
944:     PetscViewerBinaryWriteReadMPIIO(viewer,data,count,NULL,dtype,PETSC_TRUE);
945:   } else {
946: #endif
947:     PetscBinarySynchronizedWrite(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,count,dtype,istemp);
948: #if defined(PETSC_HAVE_MPIIO)
949:   }
950: #endif
951:   return(0);
952: }

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

959:    Collective on MPI_Comm

961:    Input Parameters:
962: +  viewer - the binary viewer
963: -  data - location of the array of strings


966:    Level: intermediate

968:    Concepts: binary files

970:     Notes: array of strings is null terminated

972: .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
973:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
974:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
975: @*/
976: PetscErrorCode PetscViewerBinaryWriteStringArray(PetscViewer viewer,char **data)
977: {
979:   PetscInt       i,n = 0,*sizes;

981:   PetscViewerSetUp(viewer);
982:   /* count number of strings */
983:   while (data[n++]) ;
984:   n--;
985:   PetscMalloc1(n+1,&sizes);
986:   sizes[0] = n;
987:   for (i=0; i<n; i++) {
988:     size_t tmp;
989:     PetscStrlen(data[i],&tmp);
990:     sizes[i+1] = tmp + 1;   /* size includes space for the null terminator */
991:   }
992:   PetscViewerBinaryWrite(viewer,sizes,n+1,PETSC_INT,PETSC_FALSE);
993:   for (i=0; i<n; i++) {
994:     PetscViewerBinaryWrite(viewer,data[i],sizes[i+1],PETSC_CHAR,PETSC_FALSE);
995:   }
996:   PetscFree(sizes);
997:   return(0);
998: }

1002: /*@C
1003:    PetscViewerBinaryReadStringArray - reads a binary file an array of strings

1005:    Collective on MPI_Comm

1007:    Input Parameter:
1008: .  viewer - the binary viewer

1010:    Output Parameter:
1011: .  data - location of the array of strings

1013:    Level: intermediate

1015:    Concepts: binary files

1017:     Notes: array of strings is null terminated

1019: .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
1020:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
1021:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
1022: @*/
1023: PetscErrorCode PetscViewerBinaryReadStringArray(PetscViewer viewer,char ***data)
1024: {
1026:   PetscInt       i,n,*sizes,N = 0;

1028:   PetscViewerSetUp(viewer);
1029:   /* count number of strings */
1030:   PetscViewerBinaryRead(viewer,&n,1,NULL,PETSC_INT);
1031:   PetscMalloc1(n,&sizes);
1032:   PetscViewerBinaryRead(viewer,sizes,n,NULL,PETSC_INT);
1033:   for (i=0; i<n; i++) N += sizes[i];
1034:   PetscMalloc((n+1)*sizeof(char*) + N*sizeof(char),data);
1035:   (*data)[0] = (char*)((*data) + n + 1);
1036:   for (i=1; i<n; i++) (*data)[i] = (*data)[i-1] + sizes[i-1];
1037:   PetscViewerBinaryRead(viewer,(*data)[0],N,NULL,PETSC_CHAR);

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

1041:   PetscFree(sizes);
1042:   return(0);
1043: }

1047: PetscErrorCode PetscViewerFileGetName_Binary(PetscViewer viewer,const char **name)
1048: {
1049:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

1052:   *name = vbinary->filename;
1053:   return(0);
1054: }

1058: /*@C
1059:      PetscViewerFileGetMode - Gets the type of file to be open

1061:     Not Collective

1063:   Input Parameter:
1064: .  viewer - the PetscViewer; must be a binary, MATLAB, hdf, or netcdf PetscViewer

1066:   Output Parameter:
1067: .  type - type of file
1068: $    FILE_MODE_WRITE - create new file for binary output
1069: $    FILE_MODE_READ - open existing file for binary input
1070: $    FILE_MODE_APPEND - open existing file for binary output

1072:   Level: advanced

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

1076: @*/
1077: PetscErrorCode PetscViewerFileGetMode(PetscViewer viewer,PetscFileMode *type)
1078: {

1084:   PetscUseMethod(viewer,"PetscViewerFileGetMode_C",(PetscViewer,PetscFileMode*),(viewer,type));
1085:   return(0);
1086: }

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

1094:     Logically Collective on PetscViewer

1096:     Input Parameters:
1097: +   viewer - the PetscViewer; must be a binary
1098: -   flg - PETSC_TRUE means MPI-IO will be used

1100:     Options Database:
1101:     -viewer_binary_mpiio : Flag for using MPI-IO

1103:     Level: advanced

1105: .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen(),
1106:           PetscViewerBinaryGetUseMPIIO()

1108: @*/
1109: PetscErrorCode PetscViewerBinarySetUseMPIIO(PetscViewer viewer,PetscBool flg)
1110: {

1115:   PetscTryMethod(viewer,"PetscViewerBinarySetUseMPIIO_C",(PetscViewer,PetscBool),(viewer,flg));
1116:   return(0);
1117: }

1121: /*@C
1122:      PetscViewerFileSetMode - Sets the type of file to be open

1124:     Logically Collective on PetscViewer

1126:   Input Parameters:
1127: +  viewer - the PetscViewer; must be a binary, Matlab, hdf, or netcdf PetscViewer
1128: -  type - type of file
1129: $    FILE_MODE_WRITE - create new file for binary output
1130: $    FILE_MODE_READ - open existing file for binary input
1131: $    FILE_MODE_APPEND - open existing file for binary output

1133:   Level: advanced

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

1137: @*/
1138: PetscErrorCode PetscViewerFileSetMode(PetscViewer viewer,PetscFileMode type)
1139: {

1145:   PetscTryMethod(viewer,"PetscViewerFileSetMode_C",(PetscViewer,PetscFileMode),(viewer,type));
1146:   return(0);
1147: }

1151: PetscErrorCode PetscViewerFileGetMode_Binary(PetscViewer viewer,PetscFileMode *type)
1152: {
1153:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

1156:   *type = vbinary->btype;
1157:   return(0);
1158: }

1162: PetscErrorCode PetscViewerFileSetMode_Binary(PetscViewer viewer,PetscFileMode type)
1163: {
1164:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;

1167:   vbinary->btype = type;
1168:   return(0);
1169: }

1173: PetscErrorCode PetscViewerFileSetName_Binary(PetscViewer viewer,const char name[])
1174: {
1175:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1176:   PetscErrorCode     ierr;
1177: 
1179:   if (vbinary->filename) { PetscFree(vbinary->filename); }
1180:   PetscStrallocpy(name,&vbinary->filename);
1181:   return(0);
1182: }
1183: /*
1184:         Actually opens the file
1185: */
1188: static PetscErrorCode PetscViewerFileSetUp_Binary(PetscViewer viewer)
1189: {
1190:   PetscMPIInt        rank;
1191:   PetscErrorCode     ierr;
1192:   size_t             len;
1193:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1194:   const char         *fname;
1195:   char               bname[PETSC_MAX_PATH_LEN],*gz;
1196:   PetscBool          found;
1197:   PetscFileMode      type = vbinary->btype;

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

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

1206:   /* if ends in .gz strip that off and note user wants file compressed */
1207:   vbinary->storecompressed = PETSC_FALSE;
1208:   if (!rank && type == FILE_MODE_WRITE) {
1209:     /* remove .gz if it ends library name */
1210:     PetscStrstr(vbinary->filename,".gz",&gz);
1211:     if (gz) {
1212:       PetscStrlen(gz,&len);
1213:       if (len == 3) {
1214:         *gz = 0;
1215:         vbinary->storecompressed = PETSC_TRUE;
1216:       }
1217:     }
1218:   }

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

1223:     if (type == FILE_MODE_READ) {
1224:       /* possibly get the file from remote site or compressed file */
1225:       PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),vbinary->filename,bname,PETSC_MAX_PATH_LEN,&found);
1226:       fname = bname;
1227:       if (!rank && !found) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot locate file: %s on node zero",vbinary->filename);
1228:       else if (!found) {
1229:         PetscInfo(viewer,"Nonzero processor did not locate readonly file\n");
1230:         fname = 0;
1231:       }
1232:     } else fname = vbinary->filename;

1234: #if defined(PETSC_HAVE_O_BINARY)
1235:     if (type == FILE_MODE_WRITE) {
1236:       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);
1237:     } else if (type == FILE_MODE_READ && fname) {
1238:       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);
1239:     } else if (type == FILE_MODE_APPEND) {
1240:       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);
1241:     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
1242: #else
1243:     if (type == FILE_MODE_WRITE) {
1244:       if ((vbinary->fdes = creat(fname,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname);
1245:     } else if (type == FILE_MODE_READ && fname) {
1246:       if ((vbinary->fdes = open(fname,O_RDONLY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s for reading",fname);
1247:     } else if (type == FILE_MODE_APPEND) {
1248:       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);
1249:     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
1250: #endif
1251:   } else vbinary->fdes = -1;

1253:   /*
1254:       try to open info file: all processors open this file if read only
1255:   */
1256:   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
1257:     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];

1259:     PetscStrcpy(infoname,vbinary->filename);
1260:     /* remove .gz if it ends library name */
1261:     PetscStrstr(infoname,".gz",&gz);
1262:     if (gz) {
1263:       PetscStrlen(gz,&len);
1264:       if (len == 3) *gz = 0;
1265:     }

1267:     PetscStrcat(infoname,".info");
1268:     PetscFixFilename(infoname,iname);
1269:     if (type == FILE_MODE_READ) {
1270:       PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);
1271:       PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),infoname,PETSC_FALSE);
1272:     } else {
1273:       vbinary->fdes_info = fopen(infoname,"w");
1274:       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
1275:     }
1276:   }
1277: #if defined(PETSC_USE_LOG)
1278:   PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename);
1279: #endif
1280:   return(0);
1281: }

1283: #if defined(PETSC_HAVE_MPIIO)
1286: static PetscErrorCode PetscViewerFileSetUp_BinaryMPIIO(PetscViewer viewer)
1287: {
1288:   PetscMPIInt        rank;
1289:   PetscErrorCode     ierr;
1290:   size_t             len;
1291:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1292:   char               *gz;
1293:   PetscBool          found;
1294:   PetscFileMode      type = vbinary->btype;

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

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

1303:   vbinary->storecompressed = PETSC_FALSE;

1305:   /* only first processor opens file if writeable */
1306:   if (type == FILE_MODE_READ) {
1307:     MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_RDONLY,MPI_INFO_NULL,&vbinary->mfdes);
1308:   } else if (type == FILE_MODE_WRITE) {
1309:     MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_WRONLY | MPI_MODE_CREATE,MPI_INFO_NULL,&vbinary->mfdes);
1310:   }

1312:   /*
1313:       try to open info file: all processors open this file if read only

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

1320:     PetscStrcpy(infoname,vbinary->filename);
1321:     /* remove .gz if it ends library name */
1322:     PetscStrstr(infoname,".gz",&gz);
1323:     if (gz) {
1324:       PetscStrlen(gz,&len);
1325:       if (len == 3) *gz = 0;
1326:     }

1328:     PetscStrcat(infoname,".info");
1329:     PetscFixFilename(infoname,iname);
1330:     if (type == FILE_MODE_READ) {
1331:       PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);
1332:       PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),infoname,PETSC_FALSE);
1333:     } else {
1334:       vbinary->fdes_info = fopen(infoname,"w");
1335:       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
1336:     }
1337:   }
1338: #if defined(PETSC_USE_LOG)
1339:   PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename);
1340: #endif
1341:   return(0);
1342: }

1346: PetscErrorCode PetscViewerBinarySetUseMPIIO_Binary(PetscViewer viewer,PetscBool flg)
1347: {
1348:   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1350:   vbinary->usempiio = flg;
1351:   return(0);
1352: }
1353: #endif

1357: PetscErrorCode PetscViewerView_Binary(PetscViewer v,PetscViewer viewer)
1358: {
1359:   PetscErrorCode     ierr;
1360:   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;

1363:   if (binary->filename) {
1364:     PetscViewerASCIIPrintf(viewer,"Filename: %s\n",binary->filename);
1365:   }
1366:   return(0);
1367: }

1371: static PetscErrorCode PetscViewerSetUp_Binary(PetscViewer v)
1372: {
1373:   PetscErrorCode     ierr;
1374:   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;

1377:   if (!binary->setfromoptionscalled) { PetscViewerSetFromOptions(v); }
1378: 
1379: #if defined(PETSC_HAVE_MPIIO)
1380:   if (binary->usempiio) {
1381:     PetscViewerFileSetUp_BinaryMPIIO(v);
1382:   } else {
1383: #endif
1384:     PetscViewerFileSetUp_Binary(v);
1385: #if defined(PETSC_HAVE_MPIIO)
1386:   }
1387: #endif
1388:   return(0);
1389: }

1393: static PetscErrorCode PetscViewerSetFromOptions_Binary(PetscOptions *PetscOptionsObject,PetscViewer v)
1394: {
1395:   PetscErrorCode     ierr;
1396:   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;
1397:   char               defaultname[PETSC_MAX_PATH_LEN];
1398:   PetscBool          flg;

1401:   PetscOptionsHead(PetscOptionsObject,"Binary PetscViewer Options");
1402:   PetscSNPrintf(defaultname,PETSC_MAX_PATH_LEN-1,"binaryoutput");
1403:   PetscOptionsString("-viewer_binary_filename","Specify filename","PetscViewerFileSetName",defaultname,defaultname,PETSC_MAX_PATH_LEN-1,&flg);
1404:   if (flg) { PetscViewerFileSetName_Binary(v,defaultname); }
1405:   PetscOptionsBool("-viewer_binary_skip_info","Skip writing/reading .info file","PetscViewerBinarySetSkipInfo",PETSC_FALSE,&binary->skipinfo,&flg);
1406:   PetscOptionsBool("-viewer_binary_skip_options","Skip parsing vec load options","PetscViewerBinarySetSkipOptions",PETSC_TRUE,&binary->skipoptions,&flg);
1407:   PetscOptionsBool("-viewer_binary_skip_header","Skip writing/reading header information","PetscViewerBinarySetSkipHeader",PETSC_FALSE,&binary->skipheader,&flg);
1408: #if defined(PETSC_HAVE_MPIIO)
1409:   PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file","PetscViewerBinarySetUseMPIIO",PETSC_FALSE,&binary->usempiio,&flg);
1410: #endif
1411:   PetscOptionsTail();
1412:   binary->setfromoptionscalled = PETSC_TRUE;
1413:   return(0);
1414: }

1418: PETSC_EXTERN PetscErrorCode PetscViewerCreate_Binary(PetscViewer v)
1419: {
1420:   PetscErrorCode     ierr;
1421:   PetscViewer_Binary *vbinary;

1424:   PetscNewLog(v,&vbinary);
1425:   v->data                  = (void*)vbinary;
1426:   v->ops->setfromoptions   = PetscViewerSetFromOptions_Binary;
1427:   v->ops->destroy          = PetscViewerDestroy_Binary;
1428:   v->ops->view             = PetscViewerView_Binary;
1429:   v->ops->setup            = PetscViewerSetUp_Binary;
1430:   v->ops->flush            = NULL;
1431:   vbinary->fdes_info       = 0;
1432:   vbinary->fdes            = 0;
1433:   vbinary->skipinfo        = PETSC_FALSE;
1434:   vbinary->skipoptions     = PETSC_TRUE;
1435:   vbinary->skipheader      = PETSC_FALSE;
1436:   vbinary->setfromoptionscalled = PETSC_FALSE;
1437:   v->ops->getsingleton     = PetscViewerGetSingleton_Binary;
1438:   v->ops->restoresingleton = PetscViewerRestoreSingleton_Binary;
1439:   v->ops->read             = PetscViewerBinaryRead;
1440:   vbinary->btype           = (PetscFileMode) -1;
1441:   vbinary->storecompressed = PETSC_FALSE;
1442:   vbinary->filename        = 0;
1443:   vbinary->flowcontrol     = 256; /* seems a good number for Cray XT-5 */

1445:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",PetscViewerBinaryGetFlowControl_Binary);
1446:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetFlowControl_C",PetscViewerBinarySetFlowControl_Binary);
1447:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",PetscViewerBinarySetSkipHeader_Binary);
1448:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",PetscViewerBinaryGetSkipHeader_Binary);
1449:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipOptions_C",PetscViewerBinaryGetSkipOptions_Binary);
1450:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipOptions_C",PetscViewerBinarySetSkipOptions_Binary);
1451:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipInfo_C",PetscViewerBinaryGetSkipInfo_Binary);
1452:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipInfo_C",PetscViewerBinarySetSkipInfo_Binary);
1453:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetInfoPointer_C",PetscViewerBinaryGetInfoPointer_Binary);
1454:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_Binary);
1455:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_Binary);
1456:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_Binary);
1457:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_Binary);
1458: #if defined(PETSC_HAVE_MPIIO)
1459:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetUseMPIIO_C",PetscViewerBinaryGetUseMPIIO_Binary);
1460:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetUseMPIIO_C",PetscViewerBinarySetUseMPIIO_Binary);
1461: #endif
1462:   return(0);
1463: }

1465: /* ---------------------------------------------------------------------*/
1466: /*
1467:     The variable Petsc_Viewer_Binary_keyval is used to indicate an MPI attribute that
1468:   is attached to a communicator, in this case the attribute is a PetscViewer.
1469: */
1470: static int Petsc_Viewer_Binary_keyval = MPI_KEYVAL_INVALID;

1474: /*@C
1475:      PETSC_VIEWER_BINARY_ - Creates a binary PetscViewer shared by all processors
1476:                      in a communicator.

1478:      Collective on MPI_Comm

1480:      Input Parameter:
1481: .    comm - the MPI communicator to share the binary PetscViewer

1483:      Level: intermediate

1485:    Options Database Keys:
1486: +    -viewer_binary_filename <name>
1487: .    -viewer_binary_skip_info
1488: .    -viewer_binary_skip_options
1489: .    -viewer_binary_skip_header
1490: -    -viewer_binary_mpiio

1492:    Environmental variables:
1493: -   PETSC_VIEWER_BINARY_FILENAME

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

1500: .seealso: PETSC_VIEWER_BINARY_WORLD, PETSC_VIEWER_BINARY_SELF, PetscViewerBinaryOpen(), PetscViewerCreate(),
1501:           PetscViewerDestroy()
1502: @*/
1503: PetscViewer PETSC_VIEWER_BINARY_(MPI_Comm comm)
1504: {
1506:   PetscBool      flg;
1507:   PetscViewer    viewer;
1508:   char           fname[PETSC_MAX_PATH_LEN];
1509:   MPI_Comm       ncomm;

1512:   PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1513:   if (Petsc_Viewer_Binary_keyval == MPI_KEYVAL_INVALID) {
1514:     MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Binary_keyval,0);
1515:     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1516:   }
1517:   MPI_Attr_get(ncomm,Petsc_Viewer_Binary_keyval,(void**)&viewer,(int*)&flg);
1518:   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1519:   if (!flg) { /* PetscViewer not yet created */
1520:     PetscOptionsGetenv(ncomm,"PETSC_VIEWER_BINARY_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
1521:     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1522:     if (!flg) {
1523:       PetscStrcpy(fname,"binaryoutput");
1524:       if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1525:     }
1526:     PetscViewerBinaryOpen(ncomm,fname,FILE_MODE_WRITE,&viewer);
1527:     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1528:     PetscObjectRegisterDestroy((PetscObject)viewer);
1529:     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1530:     MPI_Attr_put(ncomm,Petsc_Viewer_Binary_keyval,(void*)viewer);
1531:     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1532:   }
1533:   PetscCommDestroy(&ncomm);
1534:   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
1535:   PetscFunctionReturn(viewer);
1536: }