diff --git a/IO/MPIImage/vtkPNrrdReader.cxx b/IO/MPIImage/vtkPNrrdReader.cxx index f9c4d879abf5e1a13c248a7fe824bc30f0fe776c..a78a9da7f374392796148ade8959efd3b6e89143 100644 --- a/IO/MPIImage/vtkPNrrdReader.cxx +++ b/IO/MPIImage/vtkPNrrdReader.cxx @@ -26,6 +26,7 @@ #include "vtkObjectFactory.h" #include "vtkStringArray.h" +#include #include #include #include @@ -308,16 +309,25 @@ void vtkPNrrdReader::ReadSlice(int slice, const int extent[6], void *buffer) length *= extent[3]-extent[2]+1; if (this->GetFileDimensionality() == 3) length *= extent[5]-extent[4]+1; - if (length > VTK_INT_MAX) + vtkIdType pos = 0; + while (length > pos) { - vtkErrorMacro(<< "Cannot read more than " << VTK_INT_MAX - << " bytes at a time."); + MPI_Status stat; + // we know this will fit in an int because it can't exceed VTK_INT_MAX. + const int remaining = std::min(static_cast(length - pos), + static_cast(VTK_INT_MAX)); + MPICall(MPI_File_read(file.Handle, (static_cast(buffer)) + pos, remaining, + MPI_BYTE, &stat)); + int rd = 0; + MPICall(MPI_Get_elements(&stat, MPI_BYTE, &rd)); + if (MPI_UNDEFINED == rd) + { + vtkErrorMacro("Error obtaining number of values read in " << remaining << + "-byte read."); + } + pos += static_cast(rd); } - // Do the read. This is a coordinated parallel operation for efficiency. - MPICall(MPI_File_read_all(file.Handle, buffer, static_cast(length), - MPI_BYTE, MPI_STATUS_IGNORE)); - MPICall(MPI_File_close(&file.Handle)); } #else // VTK_USE_MPI_IO