vtkMPIMultiBlockPLOT3DReader.cxx 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkMPIMultiBlockPLOT3DReader.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkMPIMultiBlockPLOT3DReader.h"

#include "vtkByteSwap.h"
#include "vtkDataArrayDispatcher.h"
#include "vtkErrorCode.h"
#include "vtkMPICommunicator.h"
#include "vtkMPIController.h"
#include "vtkMPI.h"
#include "vtkMultiBlockPLOT3DReaderInternals.h"
#include "vtkObjectFactory.h"
#include "vtkStructuredData.h"

#include <exception>
#include <cassert>

#define DEFINE_MPI_TYPE(ctype, mpitype) \
  template <> struct mpi_type<ctype> { static MPI_Datatype type() { return mpitype; }  };

namespace
{
  template <class T> struct mpi_type {};
  DEFINE_MPI_TYPE(char, MPI_CHAR);
  DEFINE_MPI_TYPE(signed char, MPI_SIGNED_CHAR);
  DEFINE_MPI_TYPE(unsigned char, MPI_UNSIGNED_CHAR);
  DEFINE_MPI_TYPE(short, MPI_SHORT);
  DEFINE_MPI_TYPE(unsigned short, MPI_UNSIGNED_SHORT);
  DEFINE_MPI_TYPE(int, MPI_INT);
  DEFINE_MPI_TYPE(unsigned int, MPI_UNSIGNED);
  DEFINE_MPI_TYPE(long, MPI_LONG);
  DEFINE_MPI_TYPE(unsigned long, MPI_UNSIGNED_LONG);
  DEFINE_MPI_TYPE(float, MPI_FLOAT);
  DEFINE_MPI_TYPE(double, MPI_DOUBLE);
#if defined(VTK_TYPE_USE_LONG_LONG)
  DEFINE_MPI_TYPE(long long, MPI_LONG_LONG);
  DEFINE_MPI_TYPE(unsigned long long, MPI_UNSIGNED_LONG_LONG);
#endif
#if defined(VTK_TYPE_USE___INT64)
# if VTK_SIZEOF_LONG == 8
  DEFINE_MPI_TYPE(__int64, MPI_LONG);
  DEFINE_MPI_TYPE(unsigned __int64, MPI_UNSIGNED_LONG);
# elif defined(MPI_LONG_LONG)
  DEFINE_MPI_TYPE(__int64, MPI_LONG_LONG);
  DEFINE_MPI_TYPE(__int64, MPI_UNSIGNED_LONG_LONG);
# endif
#endif

  class MPIPlot3DException : public std::exception
  {
  };

  class SetupFileViewFunctor
  {
    vtkMPICommunicatorOpaqueComm& Communicator;
    vtkMPIOpaqueFileHandle& Handle;
    int Extent[6];
    int WExtent[6];
    vtkTypeUInt64 Offset;
    int ByteOrder;

  public:
    SetupFileViewFunctor(
      vtkMPICommunicatorOpaqueComm& comm,
      vtkMPIOpaqueFileHandle& handle,
        const int extent[6], const int wextent[6], vtkTypeUInt64 offset, int byteOrder)
      : Communicator(comm), Handle(handle), Offset(offset), ByteOrder(byteOrder)
    {
    std::copy(extent, extent+6, this->Extent);
    std::copy(wextent, wextent+6, this->WExtent);
    }

    template <class T>
    void operator()(vtkDataArrayDispatcherPointer<T> array) const
    {
    int array_of_sizes[3];
    int array_of_subsizes[3];
    int array_of_starts[3];
    for (int cc=0; cc < 3; ++cc)
      {
      array_of_sizes[cc]    = this->WExtent[cc*2+1] - this->WExtent[cc*2] + 1;
      array_of_subsizes[cc] = this->Extent[cc*2+1] - this->Extent[cc*2] + 1;
      array_of_starts[cc]   = this->Extent[cc*2];
      }

    // Define the file type for this process.
    MPI_Datatype filetype;
    if (MPI_Type_create_subarray(3,
        array_of_sizes,
        array_of_subsizes,
        array_of_starts,
        MPI_ORDER_FORTRAN,
        mpi_type<T>::type(),
        &filetype) != MPI_SUCCESS)
      {
      throw MPIPlot3DException();
      }
    MPI_Type_commit(&filetype);

    // For each component.
    const vtkIdType valuesToRead = vtkStructuredData::GetNumberOfPoints(
      const_cast<int*>(this->Extent));
    const vtkIdType valuesTotal = vtkStructuredData::GetNumberOfPoints(
      const_cast<int*>(this->WExtent));

    const int _INT_MAX = 2e9 / sizeof(T); /// XXX: arbitrary limit that seems
                                          /// to work when reading large files.

    int numIterations = static_cast<int>(valuesToRead/_INT_MAX) + 1;
    int maxNumIterations;
123
124
125
126
127
    if (MPI_Allreduce(&numIterations, &maxNumIterations, 1, mpi_type<int>::type(),
        MPI_MAX, *this->Communicator.GetHandle()) != MPI_SUCCESS)
      {
      throw MPIPlot3DException();
      }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

    vtkTypeUInt64 byteOffset = this->Offset;
    // simply use the VTK array for scalars.
    T *buffer = array.NumberOfComponents == 1? array.RawPointer : new T[valuesToRead];
    try
      {
      for (int comp=0; comp < array.NumberOfComponents; ++comp)
        {
        // Define the view.
        if (MPI_File_set_view(this->Handle.Handle, byteOffset, mpi_type<T>::type(),
            filetype, const_cast<char*>("native"), MPI_INFO_NULL) != MPI_SUCCESS)
          {
          throw MPIPlot3DException();
          }

        // Read contents from the file in 2GB chunks.
        vtkIdType valuesRead = 0;
        for (int iteration = 0; iteration < maxNumIterations; ++iteration)
          {
          vtkIdType valuesToReadInIteration =
            std::min((valuesToRead - valuesRead), static_cast<vtkIdType>(_INT_MAX));
          if (MPI_File_read_all(this->Handle.Handle, buffer + valuesRead,
              static_cast<int>(valuesToReadInIteration),
              mpi_type<T>::type(), MPI_STATUS_IGNORE) != MPI_SUCCESS)
            {
            throw MPIPlot3DException();
            }
          valuesRead += valuesToReadInIteration;
          }
        if (buffer != array.RawPointer)
          {
          for (vtkIdType i=0; i < valuesToRead; ++i)
            {
            array.RawPointer[array.NumberOfComponents*i + comp] = buffer[i];
            }
          }
        byteOffset += valuesTotal * sizeof(T);
        }
      }
    catch (MPIPlot3DException)
      {
      if (buffer != array.RawPointer)
        {
        delete [] buffer;
        }
      MPI_Type_free(&filetype);
      throw MPIPlot3DException();
      }
    if (buffer != array.RawPointer)
      {
      delete [] buffer;
      }
    MPI_Type_free(&filetype);
    // finally, handle byte order.
    if (this->ByteOrder == vtkMultiBlockPLOT3DReader::FILE_LITTLE_ENDIAN)
      {
      vtkByteSwap::SwapLERange(array.RawPointer, array.NumberOfComponents*array.NumberOfTuples);
      }
    else
      {
      vtkByteSwap::SwapBERange(array.RawPointer, array.NumberOfComponents*array.NumberOfTuples);
      }
    }
  };
}

vtkStandardNewMacro(vtkMPIMultiBlockPLOT3DReader);
//----------------------------------------------------------------------------
vtkMPIMultiBlockPLOT3DReader::vtkMPIMultiBlockPLOT3DReader()
{
  this->UseMPIIO = true;
}

//----------------------------------------------------------------------------
vtkMPIMultiBlockPLOT3DReader::~vtkMPIMultiBlockPLOT3DReader()
{
}

//----------------------------------------------------------------------------
bool vtkMPIMultiBlockPLOT3DReader::CanUseMPIIO()
{
  return (this->UseMPIIO && this->BinaryFile &&
    this->Internal->Settings.NumberOfDimensions == 3 &&
    vtkMPIController::SafeDownCast(this->Controller) != NULL);
}

//----------------------------------------------------------------------------
int vtkMPIMultiBlockPLOT3DReader::OpenFileForDataRead(void*& vfp, const char* fname)
{
  if (!this->CanUseMPIIO())
    {
    return this->Superclass::OpenFileForDataRead(vfp, fname);
    }

  vtkMPICommunicator* mpiComm = vtkMPICommunicator::SafeDownCast(
    this->Controller->GetCommunicator());
  assert(mpiComm);

  vtkMPIOpaqueFileHandle* handle = new vtkMPIOpaqueFileHandle();
  try
    {
    if (MPI_File_open(*mpiComm->GetMPIComm()->GetHandle(),
                      const_cast<char*>(fname), MPI_MODE_RDONLY,
                      MPI_INFO_NULL, &handle->Handle) != MPI_SUCCESS)
      {
      this->SetErrorCode(vtkErrorCode::FileNotFoundError);
      vtkErrorMacro("File: " << fname << " not found.");
      throw MPIPlot3DException();
      }
    }
  catch (MPIPlot3DException)
    {
    delete handle;
    vfp = NULL;
    return VTK_ERROR;
    }
  vfp = handle;
  return VTK_OK;
}

//----------------------------------------------------------------------------
void vtkMPIMultiBlockPLOT3DReader::CloseFile(void* vfp)
{
  if (!this->CanUseMPIIO())
    {
    this->Superclass::CloseFile(vfp);
    return;
    }

  vtkMPIOpaqueFileHandle* handle = reinterpret_cast<vtkMPIOpaqueFileHandle*>(vfp);
  assert(handle);
  if (MPI_File_close(&handle->Handle) != MPI_SUCCESS)
    {
    vtkErrorMacro("Failed to close file!");
    }
}

//----------------------------------------------------------------------------
int vtkMPIMultiBlockPLOT3DReader::ReadIntScalar(
  void* vfp, int extent[6], int wextent[6],
  vtkDataArray* scalar, vtkTypeUInt64 offset)
{
  if (!this->CanUseMPIIO())
    {
272
    return this->Superclass::ReadIntScalar(vfp, extent, wextent, scalar, offset);
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
    }

  return this->ReadScalar(vfp, extent, wextent, scalar, offset);
}

//----------------------------------------------------------------------------
int vtkMPIMultiBlockPLOT3DReader::ReadScalar(
    void* vfp,
    int extent[6], int wextent[6],
    vtkDataArray* scalar, vtkTypeUInt64 offset)
{
  if (!this->CanUseMPIIO())
    {
    return this->Superclass::ReadScalar(vfp, extent, wextent, scalar, offset);
    }

  vtkMPIOpaqueFileHandle* handle = reinterpret_cast<vtkMPIOpaqueFileHandle*>(vfp);
  assert(handle);

  vtkMPICommunicatorOpaqueComm* comm =
    vtkMPICommunicator::SafeDownCast(this->Controller->GetCommunicator())->GetMPIComm();
  assert(comm);

  SetupFileViewFunctor work(*comm, *handle, extent, wextent, offset, this->Internal->Settings.ByteOrder);
  vtkDataArrayDispatcher<SetupFileViewFunctor> dispatcher(work);
  try
    {
    dispatcher.Go(scalar);
    }
  catch (MPIPlot3DException)
    {
    return 0;
    }
  return 1;
}

//----------------------------------------------------------------------------
int vtkMPIMultiBlockPLOT3DReader::ReadVector(
  void* vfp,
  int extent[6], int wextent[6],
  int numDims, vtkDataArray* vector, vtkTypeUInt64 offset)
{
  if (!this->CanUseMPIIO())
    {
    return this->Superclass::ReadVector(vfp, extent, wextent, numDims, vector, offset);
    }

  vtkMPIOpaqueFileHandle* handle = reinterpret_cast<vtkMPIOpaqueFileHandle*>(vfp);
  assert(handle);

  vtkMPICommunicatorOpaqueComm* comm =
    vtkMPICommunicator::SafeDownCast(this->Controller->GetCommunicator())->GetMPIComm();
  assert(comm);

  SetupFileViewFunctor work(*comm, *handle, extent, wextent, offset, this->Internal->Settings.ByteOrder);
  vtkDataArrayDispatcher<SetupFileViewFunctor> dispatcher(work);
  try
    {
    dispatcher.Go(vector);
    }
  catch (MPIPlot3DException)
    {
    return 0;
    }
  return 1;
}

//----------------------------------------------------------------------------
void vtkMPIMultiBlockPLOT3DReader::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "UseMPIIO: " << this->UseMPIIO << endl;
}