Commit 5125a037 authored by Mathieu Westphal's avatar Mathieu Westphal
Browse files

Fix parallel nhdr reader

This commit allows the vtkPNrrdReader to read more than INT_MAX items,
because of int usage.
The reader will nom loop over all items while ensuring item index
do not go over INT_MAX.
parent 937d4d47
......@@ -26,6 +26,7 @@
#include "vtkObjectFactory.h"
#include "vtkStringArray.h"
#include <algorithm>
#include <string>
#include <vector>
#include <vtksys/SystemTools.hxx>
......@@ -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<size_t>(length - pos),
static_cast<size_t>(VTK_INT_MAX));
MPICall(MPI_File_read(file.Handle, (static_cast<char*>(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<vtkIdType>(rd);
}
// Do the read. This is a coordinated parallel operation for efficiency.
MPICall(MPI_File_read_all(file.Handle, buffer, static_cast<int>(length),
MPI_BYTE, MPI_STATUS_IGNORE));
MPICall(MPI_File_close(&file.Handle));
}
#else // VTK_USE_MPI_IO
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment