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 123 124 125 126 127 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
|
#include "vtkGenericIOUtilities.h"
// VTK includes
#include "vtkDataArray.h"
#include "vtkMPI.h"
#include "vtkMPICommunicator.h"
#include "vtkMPIController.h"
#include "vtkMultiProcessController.h"
// GenericIO includes
#include "GenericIOMPIReader.h"
#include "GenericIOPosixReader.h"
#include "GenericIOMPIWriter.h"
// C/C++ includes
#include <cassert>
// MPI
#include <mpi.h>
namespace vtkGenericIOUtilities
{
//==============================================================================
MPI_Comm GetMPICommunicator(vtkMultiProcessController *mpc)
{
assert("pre: null multiprocess controller!" && (mpc != NULL) );
MPI_Comm comm = MPI_COMM_NULL;
// STEP 0: Get the communicator from the controller
vtkCommunicator *vtkComm = mpc->GetCommunicator();
assert("pre: VTK communicator is NULL" && (vtkComm != NULL) );
// STEP 1: Safe downcast to an vtkMPICommunicator
vtkMPICommunicator* vtkMPIComm = vtkMPICommunicator::SafeDownCast(vtkComm);
assert("pre: MPI communicator is NULL" && (vtkMPIComm != NULL) );
// STEP 2: Get the opaque VTK MPI communicator
vtkMPICommunicatorOpaqueComm* mpiComm = vtkMPIComm->GetMPIComm();
assert("pre: Opaque MPI communicator is NULL" && (mpiComm != NULL) );
// STEP 3: Finally, get the MPI comm
comm = *(mpiComm->GetHandle());
return( comm );
}
//==============================================================================
vtkDataArray* GetVtkDataArray(
std::string name, int type, void* rawBuffer, int N)
{
assert("pre: cannot read from null buffer!" && (rawBuffer != NULL) );
vtkDataArray *dataArray = NULL;
size_t dataSize = 0;
switch( type )
{
case gio::GENERIC_IO_INT32_TYPE:
dataArray = vtkDataArray::CreateDataArray(VTK_TYPE_INT32);
dataSize = sizeof(vtkTypeInt32);
break;
case gio::GENERIC_IO_INT64_TYPE:
dataArray = vtkDataArray::CreateDataArray(VTK_TYPE_INT64);
// i.e., don't run this on windows
assert(sizeof(vtkTypeInt64) == sizeof(uint64_t) );
dataSize = sizeof(vtkTypeInt64);
break;
case gio::GENERIC_IO_UINT32_TYPE:
dataArray = vtkDataArray::CreateDataArray(VTK_TYPE_UINT32);
dataSize = sizeof(vtkTypeUInt32);
break;
case gio::GENERIC_IO_UINT64_TYPE:
dataArray = vtkDataArray::CreateDataArray(VTK_TYPE_UINT64);
// i.e., don't run this on windows
assert(sizeof(vtkTypeUInt64) == sizeof(uint64_t) );
dataSize = sizeof(vtkTypeUInt64);
break;
case gio::GENERIC_IO_DOUBLE_TYPE:
dataArray = vtkDataArray::CreateDataArray(VTK_DOUBLE);
dataSize = sizeof(double);
break;
case gio::GENERIC_IO_FLOAT_TYPE:
dataArray = vtkDataArray::CreateDataArray(VTK_FLOAT);
dataSize = sizeof(float);
break;
default:
dataSize = 0;
return NULL;
} // END switch
assert("pre: null data array!" && (dataArray != NULL) );
dataArray->SetNumberOfComponents(1);
dataArray->SetNumberOfTuples( N );
dataArray->SetName(name.c_str());
if (N > 0)
{
void *dataBuffer = dataArray->GetVoidPointer(0);
assert("pre: encountered NULL data buffer!" && (dataBuffer != NULL) );
memcpy(dataBuffer,rawBuffer,N*dataSize);
}
return( dataArray );
}
//==============================================================================
double GetDoubleFromRawBuffer(
const int type, void* buffer, vtkIdType buffer_idx)
{
assert("pre: cannot read from null buffer!" && (buffer != NULL) );
double dataItem = 0.0;
switch( type )
{
case gio::GENERIC_IO_INT32_TYPE:
{
int32_t *dataPtr = static_cast<int32_t*>(buffer);
dataItem = static_cast<double>(dataPtr[buffer_idx]);
}
break;
case gio::GENERIC_IO_INT64_TYPE:
{
int64_t *dataPtr = static_cast<int64_t*>(buffer);
dataItem = static_cast<double>(dataPtr[buffer_idx]);
}
break;
case gio::GENERIC_IO_UINT32_TYPE:
{
uint32_t *dataPtr = static_cast<uint32_t*>(buffer);
dataItem = static_cast<double>(dataPtr[buffer_idx]);
}
break;
case gio::GENERIC_IO_UINT64_TYPE:
{
uint64_t *dataPtr = static_cast<uint64_t*>(buffer);
dataItem = static_cast<double>(dataPtr[buffer_idx]);
}
break;
case gio::GENERIC_IO_DOUBLE_TYPE:
{
double *dataPtr = static_cast<double*>(buffer);
dataItem = dataPtr[buffer_idx];
}
break;
case gio::GENERIC_IO_FLOAT_TYPE:
{
float *dataPtr = static_cast<float*>(buffer);
dataItem = static_cast<double>(dataPtr[buffer_idx]);
}
break;
default:
assert("pre: Undefined GENERIC IO type: " && true);
} // END switch
return( dataItem );
}
//==============================================================================
vtkIdType GetIdFromRawBuffer(
const int type, void* buffer, vtkIdType buffer_idx)
{
assert("pre: cannot read from null buffer!" && (buffer != NULL) );
vtkIdType dataItem = 0;
switch( type )
{
case gio::GENERIC_IO_INT32_TYPE:
{
int32_t *dataPtr = static_cast<int32_t*>(buffer);
dataItem = static_cast<vtkIdType>(dataPtr[buffer_idx]);
}
break;
case gio::GENERIC_IO_INT64_TYPE:
{
int64_t *dataPtr = static_cast<int64_t*>(buffer);
dataItem = static_cast<vtkIdType>(dataPtr[buffer_idx]);
}
break;
case gio::GENERIC_IO_UINT32_TYPE:
{
uint32_t *dataPtr = static_cast<uint32_t*>(buffer);
dataItem = static_cast<vtkIdType>(dataPtr[buffer_idx]);
}
break;
case gio::GENERIC_IO_UINT64_TYPE:
{
uint64_t *dataPtr = static_cast<uint64_t*>(buffer);
dataItem = static_cast<vtkIdType>(dataPtr[buffer_idx]);
}
break;
case gio::GENERIC_IO_DOUBLE_TYPE:
{
double *dataPtr = static_cast<double*>(buffer);
dataItem = static_cast<vtkIdType>(dataPtr[buffer_idx]);
}
break;
case gio::GENERIC_IO_FLOAT_TYPE:
{
float *dataPtr = static_cast<float*>(buffer);
dataItem = static_cast<vtkIdType>(dataPtr[buffer_idx]);
}
break;
default:
assert("pre: Undefined GENERIC IO type: " && true);
} // END switch
return( dataItem );
}
//==============================================================================
gio::GenericIOReader* GetReader(
MPI_Comm comm, bool posix, int distribution, const std::string& fileName)
{
gio::GenericIOReader *reader = NULL;
if( posix )
{
reader = new gio::GenericIOPosixReader();
}
else
{
reader = new gio::GenericIOMPIReader();
}
assert( "pre: reader is NULL!" && (reader != NULL) );
reader->SetCommunicator(comm);
reader->SetBlockAssignmentStrategy(distribution);
reader->SetFileName(fileName);
return( reader );
}
//==============================================================================
gio::GenericIOWriter* GetWriter(MPI_Comm comm, const std::string& fileName)
{
gio::GenericIOWriter* writer = new gio::GenericIOMPIWriter(comm);
writer->SetFileName(fileName);
return writer;
}
}
|