I used FILE_MODE_READ. Please see attached file (the function is part of 
another file therefore all the other include statements; I've extracted it 
here). The test file only contains one number. My directory is hard-coded into 
the main function.

Thanks a lot!
Yuyun

-----Original Message-----
From: Smith, Barry F. <bsm...@mcs.anl.gov> 
Sent: Monday, February 18, 2019 6:41 PM
To: Yuyun Yang <yyan...@stanford.edu>
Cc: petsc-users@mcs.anl.gov
Subject: Re: [petsc-users] PetscBinaryRead fails


  Send program and data file if you cannot get it to work.


> On Feb 18, 2019, at 8:08 PM, Yuyun Yang via petsc-users 
> <petsc-users@mcs.anl.gov> wrote:
> 
> Hello,
>  
> I am writing a very simple function just to read in a single value from an 
> input file (which also just contains one single scalar value). Using 
> PetscBinaryRead(fd, &value, 1, PETSC_SCALAR) gives me the following error. 
> Could you help me figure out why?
>  
> [0]PETSC ERROR: --------------------- Error Message 
> --------------------------------------------------------------
> [0]PETSC ERROR: Read from file failed
> [0]PETSC ERROR: Read past end of file
> [0]PETSC ERROR: See http://www.mcs.anl.gov/petsc/documentation/faq.html for 
> trouble shooting.
> [0]PETSC ERROR: Petsc Release Version 3.10.3, unknown [0]PETSC ERROR: 
> ./output on a arch-linux2-c-debug named DESKTOP-02K82L1.stanford.edu 
> by yyy910805 Mon Feb 18 17:58:30 2019 [0]PETSC ERROR: Configure options 
> --with-cc=gcc --with-cxx=g++ --with-shared-libraries=1 --download-hdf5 
> --download-metis --download-parmetis --download-hypre --download-superlu 
> --download-mumps --download-scalapack --download-mpich 
> --download-mpich-configure-arguments="--enable-error-messages=all --enable-g"
> [0]PETSC ERROR: #1 PetscBinaryRead() line 276 in 
> /home/yyy910805/petsc/src/sys/fileio/sysio.c
> 0.000000
>  
> Thanks a lot!
> Yuyun

//#include "genFuncs.hpp"

#include <assert.h>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

#include <petscdmda.h>
#include <petscts.h>
#include <petscviewer.h>

using namespace std;

PetscErrorCode loadValueFromCheckpoint(const string outputDir, const string 
filename, PetscScalar &value) {
  PetscErrorCode ierr = 0;
  #if VERBOSE > 1
  ierr = PetscPrinf(PETSC_COMM_WORLD, "Starting loadValueFromCheckpoint in 
genFuncs.cpp.\n"); CHKERRQ(ierr);
  ierr = PetscPrintf(PETSC_COMM_WORLD, "Attempting to load %s%s\n", 
outputDir.c_str(), filename.c_str()); CHKERRQ(ierr);
  #endif

  string checkpointFile = outputDir + filename;
  bool fileExists = doesFileExist(checkpointFile);
  if (fileExists) {
    PetscPrintf(PETSC_COMM_WORLD, "Loading %s\n", filename.c_str());
    PetscViewer viewer;
    int fd;
    PetscViewerBinaryOpen(PETSC_COMM_WORLD, checkpointFile.c_str(), 
FILE_MODE_READ, &viewer);
    PetscViewerBinaryGetDescriptor(viewer, &fd);
    PetscBinaryRead(fd, &value, 1, PETSC_SCALAR);
    PetscViewerDestroy(&viewer);
  }
  else {
    PetscPrintf(PETSC_COMM_WORLD, "Warning: %s not found.\n", 
checkpointFile.c_str());
  }

  #if VERBOSE > 1
  ierr = PetscPrintf(PETSC_COMM_WORLD, "Ending loadValueFromCheckpoint in 
%s.\n", filename.c_str()); CHKERRQ(ierr);
  #endif

  return ierr;
}


int main(int argc, char **args) {
  PetscErrorCode ierr = 0;

  ierr = PetscInitialize(&argc, &args, NULL, NULL); CHKERRQ(ierr);

  // directory and filename
  const string outputDir = "/home/yyy910805/";
  const string filename = "_test";
  PetscScalar value = 0;

  loadValueFromCheckpoint(outputDir, filename, value);
  ierr = PetscPrintf(PETSC_COMM_WORLD, " %f\n", value); CHKERRQ(ierr);

  ierr = PetscFinalize(); CHKERRQ(ierr);
  
  return 0;
}

Attachment: _test
Description: _test

Reply via email to