Field3D
SparseDataReader< Data_T > Class Template Reference

This class gets used by SparseFieldIO and SparseFileManager to read the block data. On creation it will open the data set and not close it until the object is destroyed. More...

#include <SparseDataReader.h>

Public Member Functions

void readBlock (int idx, Data_T &result)
 Reads a block, storing the data in result, which is assumed to contain enough room for m_valuesPerBlock entries. More...
 
void readBlockList (int idx, const std::vector< Data_T * > &memoryList)
 Reads a series of blocks, storing each block of data in memoryList, which is assumed to contain enough room for m_valuesPerBlock entries. More...
 
 SparseDataReader (hid_t location, int valuesPerBlock, int occupiedBlocks)
 Constructor. Requires knowledge of the Hdf5 location where data is stored. More...
 

Private Attributes

const std::string k_dataStr
 
Hdf5Util::H5ScopedDopen m_dataSet
 
Hdf5Util::H5ScopedDget_type m_dataType
 
Hdf5Util::H5ScopedDget_space m_fileDataSpace
 
Hdf5Util::H5ScopedScreate m_memDataSpace
 
int m_valuesPerBlock
 

Detailed Description

template<class Data_T>
class SparseDataReader< Data_T >

This class gets used by SparseFieldIO and SparseFileManager to read the block data. On creation it will open the data set and not close it until the object is destroyed.

Definition at line 68 of file SparseDataReader.h.

Constructor & Destructor Documentation

template<class Data_T >
SparseDataReader< Data_T >::SparseDataReader ( hid_t  location,
int  valuesPerBlock,
int  occupiedBlocks 
)

Constructor. Requires knowledge of the Hdf5 location where data is stored.

Definition at line 107 of file SparseDataReader.h.

References Hdf5Util::H5ScopedScreate::create(), Hdf5Util::H5Base::id(), SparseDataReader< Data_T >::k_dataStr, SparseDataReader< Data_T >::m_dataSet, SparseDataReader< Data_T >::m_dataType, SparseDataReader< Data_T >::m_fileDataSpace, SparseDataReader< Data_T >::m_memDataSpace, SparseDataReader< Data_T >::m_valuesPerBlock, Hdf5Util::H5ScopedDopen::open(), Hdf5Util::H5ScopedDget_space::open(), and Hdf5Util::H5ScopedDget_type::open().

109  : m_valuesPerBlock(valuesPerBlock),
110  k_dataStr("data")
111 {
112  using namespace Hdf5Util;
113  using namespace Exc;
114 
115  hsize_t dims[2];
116  hsize_t memDims[1];
117 
118  // Open the data set
119  m_dataSet.open(location, k_dataStr, H5P_DEFAULT);
120  if (m_dataSet.id() < 0)
121  throw OpenDataSetException("Couldn't open data set: " + k_dataStr);
122 
123  // Get the space and type
126  if (m_fileDataSpace.id() < 0)
127  throw GetDataSpaceException("Couldn't get data space");
128  if (m_dataType.id() < 0)
129  throw GetDataTypeException("Couldn't get data type");
130 
131  // Make the memory data space
132  memDims[0] = m_valuesPerBlock;
133  m_memDataSpace.create(H5S_SIMPLE);
134  H5Sset_extent_simple(m_memDataSpace.id(), 1, memDims, NULL);
135 
136  // Get the dimensions and check they match
137  H5Sget_simple_extent_dims(m_fileDataSpace.id(), dims, NULL);
138  if (dims[1] != static_cast<hsize_t>(m_valuesPerBlock)) {
139  throw FileIntegrityException("Block length mismatch in "
140  "SparseDataReader");
141  }
142  if (dims[0] != static_cast<hsize_t>(occupiedBlocks))
143  throw FileIntegrityException("Block count mismatch in "
144  "SparseDataReader");
145 }
void open(hid_t dataset_id)
Definition: Hdf5Util.h:365
Hdf5Util::H5ScopedScreate m_memDataSpace
Hdf5Util::H5ScopedDget_space m_fileDataSpace
void open(hid_t dataset_id)
Definition: Hdf5Util.h:393
const std::string k_dataStr
void open(hid_t parentLocation, const std::string &name, hid_t dapl_id)
Definition: Hdf5Util.h:337
Hdf5Util::H5ScopedDget_type m_dataType
Hdf5Util::H5ScopedDopen m_dataSet
void create(H5S_class_t type)
Definition: Hdf5Util.h:225
hid_t id() const
Query the hid_t value.
Definition: Hdf5Util.h:90

Member Function Documentation

template<class Data_T >
void SparseDataReader< Data_T >::readBlock ( int  idx,
Data_T &  result 
)

Reads a block, storing the data in result, which is assumed to contain enough room for m_valuesPerBlock entries.

Definition at line 150 of file SparseDataReader.h.

References DataTypeTraits< T >::h5type(), Hdf5Util::H5Base::id(), SparseDataReader< Data_T >::m_dataSet, SparseDataReader< Data_T >::m_fileDataSpace, SparseDataReader< Data_T >::m_memDataSpace, and SparseDataReader< Data_T >::m_valuesPerBlock.

151 {
152  using namespace Hdf5Util;
153  using namespace Exc;
154 
155  hsize_t offset[2];
156  hsize_t count[2];
157  herr_t status;
158 
159  offset[0] = idx; // Index of block
160  offset[1] = 0; // Index of first data in block. Always 0
161  count[0] = 1; // Number of columns to read. Always 1
162  count[1] = m_valuesPerBlock; // Number of values in one column
163 
164  status = H5Sselect_hyperslab(m_fileDataSpace.id(), H5S_SELECT_SET,
165  offset, NULL, count, NULL);
166  if (status < 0) {
167  throw ReadHyperSlabException("Couldn't select slab " +
168  boost::lexical_cast<std::string>(idx));
169  }
170 
171  status = H5Dread(m_dataSet.id(), DataTypeTraits<Data_T>::h5type(),
173  H5P_DEFAULT, &result);
174 }
Hdf5Util::H5ScopedScreate m_memDataSpace
Hdf5Util::H5ScopedDget_space m_fileDataSpace
static hid_t h5type()
Hdf5Util::H5ScopedDopen m_dataSet
hid_t id() const
Query the hid_t value.
Definition: Hdf5Util.h:90
template<class Data_T >
void SparseDataReader< Data_T >::readBlockList ( int  idx,
const std::vector< Data_T * > &  memoryList 
)

Reads a series of blocks, storing each block of data in memoryList, which is assumed to contain enough room for m_valuesPerBlock entries.

Definition at line 180 of file SparseDataReader.h.

References DataTypeTraits< T >::h5type(), Hdf5Util::H5Base::id(), SparseDataReader< Data_T >::m_dataSet, SparseDataReader< Data_T >::m_fileDataSpace, and SparseDataReader< Data_T >::m_valuesPerBlock.

Referenced by SparseFieldIO::readData().

181 {
182  using namespace Hdf5Util;
183  using namespace Exc;
184 
185  hsize_t offset[2];
186  hsize_t count[2];
187  herr_t status;
188 
189  offset[0] = idxLo; // Index of block
190  offset[1] = 0; // Index of first data in block. Always 0
191  count[0] = memoryList.size(); // Number of columns to read.
192  count[1] = m_valuesPerBlock; // Number of values in one column
193 
194  status = H5Sselect_hyperslab(m_fileDataSpace.id(), H5S_SELECT_SET,
195  offset, NULL, count, NULL);
196  if (status < 0) {
197  throw ReadHyperSlabException("Couldn't select slab " +
198  boost::lexical_cast<std::string>(idxLo));
199  }
200 
201  // Make the memory data space ---
202 
203  Hdf5Util::H5ScopedScreate localMemDataSpace;
204  hsize_t memDims[2];
205  memDims[0] = memoryList.size();
206  memDims[1] = m_valuesPerBlock;
207  localMemDataSpace.create(H5S_SIMPLE);
208  H5Sset_extent_simple(localMemDataSpace.id(), 2, memDims, NULL);
209 
210  // Setup the temporary memory region ---
211 
212  int bytesPerValue = 0;
213  {
214  hid_t t = DataTypeTraits<Data_T>::h5type();
215  if (t == H5T_NATIVE_CHAR)
216  bytesPerValue = 1;
217  else if (t == H5T_NATIVE_SHORT)
218  bytesPerValue = 2;
219  else if (t == H5T_NATIVE_FLOAT)
220  bytesPerValue = 4;
221  else if (t == H5T_NATIVE_DOUBLE)
222  bytesPerValue = 8;
223  }
224 
225  int dim = sizeof(Data_T) / bytesPerValue;
226  std::vector<Data_T> bigblock(memoryList.size() * m_valuesPerBlock/dim);
227 
228  status = H5Dread(m_dataSet.id(),
230  localMemDataSpace.id(),
231  m_fileDataSpace.id(),
232  H5P_DEFAULT, &bigblock[0]);
233 
234  if (status < 0) {
235  throw Hdf5DataReadException("Couldn't read slab " +
236  boost::lexical_cast<std::string>(idxLo));
237  }
238 
239  // Distribute block data into memory slots ---
240  for (size_t i = 0; i < memoryList.size(); ++i) {
241  memcpy(memoryList[i],
242  &bigblock[i * m_valuesPerBlock / dim],
243  bytesPerValue * m_valuesPerBlock);
244  }
245 }
Hdf5Util::H5ScopedDget_space m_fileDataSpace
static hid_t h5type()
Scoped object - creates a dataspace on creation and closes it on destruction.
Definition: Hdf5Util.h:213
Hdf5Util::H5ScopedDopen m_dataSet
hid_t id() const
Query the hid_t value.
Definition: Hdf5Util.h:90

Member Data Documentation

template<class Data_T>
Hdf5Util::H5ScopedDget_type SparseDataReader< Data_T >::m_dataType
private

Definition at line 94 of file SparseDataReader.h.

Referenced by SparseDataReader< Data_T >::SparseDataReader().

template<class Data_T>
Hdf5Util::H5ScopedScreate SparseDataReader< Data_T >::m_memDataSpace
private
template<class Data_T>
int SparseDataReader< Data_T >::m_valuesPerBlock
private
template<class Data_T>
const std::string SparseDataReader< Data_T >::k_dataStr
private

Definition at line 99 of file SparseDataReader.h.

Referenced by SparseDataReader< Data_T >::SparseDataReader().


The documentation for this class was generated from the following file: