Field3D

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
 
hid_t m_location
 
int m_occupiedBlocks
 
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 70 of file SparseDataReader.h.

Constructor & Destructor Documentation

◆ SparseDataReader()

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.

109  : m_location(location),
110  m_valuesPerBlock(valuesPerBlock),
111  m_occupiedBlocks(occupiedBlocks),
112  k_dataStr("data")
113 {
114 
115 }

Member Function Documentation

◆ readBlock()

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 120 of file SparseDataReader.h.

121 {
122  using namespace Hdf5Util;
123  using namespace Exc;
124 
125  GlobalLock lock(g_hdf5Mutex);
126 
127  Hdf5Util::H5ScopedDopen dataSet;
128  Hdf5Util::H5ScopedDget_space fileDataSpace;
130  Hdf5Util::H5ScopedScreate memDataSpace;
131 
132  hsize_t dims[2];
133  hsize_t memDims[1];
134 
135  // Open the data set
136  dataSet.open(m_location, k_dataStr, H5P_DEFAULT);
137  if (dataSet.id() < 0)
138  throw OpenDataSetException("Couldn't open data set: " + k_dataStr);
139 
140  // Get the space and type
141  fileDataSpace.open(dataSet.id());
142  dataType.open(dataSet.id());
143  if (fileDataSpace.id() < 0)
144  throw GetDataSpaceException("Couldn't get data space");
145  if (dataType.id() < 0)
146  throw GetDataTypeException("Couldn't get data type");
147 
148  // Make the memory data space
149  memDims[0] = m_valuesPerBlock;
150  memDataSpace.create(H5S_SIMPLE);
151  H5Sset_extent_simple(memDataSpace.id(), 1, memDims, NULL);
152 
153  // Get the dimensions and check they match
154  H5Sget_simple_extent_dims(fileDataSpace.id(), dims, NULL);
155  if (dims[1] != static_cast<hsize_t>(m_valuesPerBlock)) {
156  throw FileIntegrityException("Block length mismatch in "
157  "SparseDataReader");
158  }
159  if (dims[0] != static_cast<hsize_t>(m_occupiedBlocks))
160  throw FileIntegrityException("Block count mismatch in "
161  "SparseDataReader");
162 
163  hsize_t offset[2];
164  hsize_t count[2];
165  herr_t status;
166 
167  offset[0] = idx; // Index of block
168  offset[1] = 0; // Index of first data in block. Always 0
169  count[0] = 1; // Number of columns to read. Always 1
170  count[1] = m_valuesPerBlock; // Number of values in one column
171 
172  status = H5Sselect_hyperslab(fileDataSpace.id(), H5S_SELECT_SET,
173  offset, NULL, count, NULL);
174 
175  if (status < 0) {
176  throw ReadHyperSlabException("Couldn't select slab in readBlock(): " +
177  boost::lexical_cast<std::string>(idx));
178  }
179 
180  status = H5Dread(dataSet.id(), DataTypeTraits<Data_T>::h5type(),
181  memDataSpace.id(), fileDataSpace.id(),
182  H5P_DEFAULT, &result);
183 }

References Hdf5Util::H5ScopedScreate::create(), g_hdf5Mutex, Hdf5Util::H5Base::id(), Hdf5Util::H5ScopedDget_space::open(), Hdf5Util::H5ScopedDget_type::open(), and Hdf5Util::H5ScopedDopen::open().

◆ readBlockList()

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 188 of file SparseDataReader.h.

190 {
191  using namespace Hdf5Util;
192  using namespace Exc;
193 
194  GlobalLock lock(g_hdf5Mutex);
195 
196  Hdf5Util::H5ScopedDopen dataSet;
197  Hdf5Util::H5ScopedDget_space fileDataSpace;
199  Hdf5Util::H5ScopedScreate memDataSpace;
200 
201  hsize_t dims[2];
202  hsize_t memDims[1];
203 
204  // Open the data set
205  dataSet.open(m_location, k_dataStr, H5P_DEFAULT);
206  if (dataSet.id() < 0)
207  throw OpenDataSetException("Couldn't open data set: " + k_dataStr);
208 
209  // Get the space and type
210  fileDataSpace.open(dataSet.id());
211  dataType.open(dataSet.id());
212  if (fileDataSpace.id() < 0)
213  throw GetDataSpaceException("Couldn't get data space");
214  if (dataType.id() < 0)
215  throw GetDataTypeException("Couldn't get data type");
216 
217  // Make the memory data space
218  memDims[0] = m_valuesPerBlock;
219  memDataSpace.create(H5S_SIMPLE);
220  H5Sset_extent_simple(memDataSpace.id(), 1, memDims, NULL);
221 
222  // Get the dimensions and check they match
223  H5Sget_simple_extent_dims(fileDataSpace.id(), dims, NULL);
224  if (dims[1] != static_cast<hsize_t>(m_valuesPerBlock)) {
225  throw FileIntegrityException("Block length mismatch in "
226  "SparseDataReader");
227  }
228  if (dims[0] != static_cast<hsize_t>(m_occupiedBlocks))
229  throw FileIntegrityException("Block count mismatch in "
230  "SparseDataReader");
231 
232  hsize_t offset[2];
233  hsize_t count[2];
234  herr_t status;
235 
236  offset[0] = idxLo; // Index of block
237  offset[1] = 0; // Index of first data in block. Always 0
238  count[0] = memoryList.size(); // Number of columns to read.
239  count[1] = m_valuesPerBlock; // Number of values in one column
240 
241  status = H5Sselect_hyperslab(fileDataSpace.id(), H5S_SELECT_SET,
242  offset, NULL, count, NULL);
243  if (status < 0) {
244  throw ReadHyperSlabException("Couldn't select slab in readBlockList():" +
245  boost::lexical_cast<std::string>(idxLo));
246  }
247 
248  // Make the memory data space ---
249 
250  Hdf5Util::H5ScopedScreate localMemDataSpace;
251  hsize_t fileDims[2];
252  fileDims[0] = memoryList.size();
253  fileDims[1] = m_valuesPerBlock;
254  localMemDataSpace.create(H5S_SIMPLE);
255  H5Sset_extent_simple(localMemDataSpace.id(), 2, fileDims, NULL);
256 
257  // Setup the temporary memory region ---
258 
259  int bytesPerValue = 0;
260  {
261  hid_t t = DataTypeTraits<Data_T>::h5type();
262  if (t == H5T_NATIVE_CHAR)
263  bytesPerValue = 1;
264  else if (t == H5T_NATIVE_SHORT)
265  bytesPerValue = 2;
266  else if (t == H5T_NATIVE_FLOAT)
267  bytesPerValue = 4;
268  else if (t == H5T_NATIVE_DOUBLE)
269  bytesPerValue = 8;
270  }
271 
272  int dim = sizeof(Data_T) / bytesPerValue;
273  std::vector<Data_T> bigblock(memoryList.size() * m_valuesPerBlock/dim);
274 
275  status = H5Dread(dataSet.id(),
277  localMemDataSpace.id(),
278  fileDataSpace.id(),
279  H5P_DEFAULT, &bigblock[0]);
280 
281  if (status < 0) {
282  throw Hdf5DataReadException("Couldn't read slab " +
283  boost::lexical_cast<std::string>(idxLo));
284  }
285 
286  // Distribute block data into memory slots ---
287  for (size_t i = 0; i < memoryList.size(); ++i) {
288  memcpy(memoryList[i],
289  &bigblock[i * m_valuesPerBlock / dim],
290  bytesPerValue * m_valuesPerBlock);
291  }
292 }

References Hdf5Util::H5ScopedScreate::create(), g_hdf5Mutex, DataTypeTraits< T >::h5type(), Hdf5Util::H5Base::id(), Hdf5Util::H5ScopedDget_space::open(), Hdf5Util::H5ScopedDget_type::open(), and Hdf5Util::H5ScopedDopen::open().

Member Data Documentation

◆ m_location

template<class Data_T >
hid_t SparseDataReader< Data_T >::m_location
private

Definition at line 94 of file SparseDataReader.h.

◆ m_valuesPerBlock

template<class Data_T >
int SparseDataReader< Data_T >::m_valuesPerBlock
private

Definition at line 96 of file SparseDataReader.h.

◆ m_occupiedBlocks

template<class Data_T >
int SparseDataReader< Data_T >::m_occupiedBlocks
private

Definition at line 97 of file SparseDataReader.h.

◆ k_dataStr

template<class Data_T >
const std::string SparseDataReader< Data_T >::k_dataStr
private

Definition at line 99 of file SparseDataReader.h.


The documentation for this class was generated from the following file:
SparseDataReader::m_occupiedBlocks
int m_occupiedBlocks
Definition: SparseDataReader.h:97
SparseDataReader::k_dataStr
const std::string k_dataStr
Definition: SparseDataReader.h:99
DataTypeTraits::h5type
static hid_t h5type()
Hdf5Util::H5ScopedScreate::create
void create(H5S_class_t type)
Definition: Hdf5Util.h:246
Hdf5Util::H5Base::id
hid_t id() const
Query the hid_t value.
Definition: Hdf5Util.h:100
SparseDataReader::m_location
hid_t m_location
Definition: SparseDataReader.h:94
Hdf5Util::H5ScopedDopen
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:357
Hdf5Util::H5ScopedScreate
Scoped object - creates a dataspace on creation and closes it on destruction.
Definition: Hdf5Util.h:235
g_hdf5Mutex
FIELD3D_NAMESPACE_OPEN FIELD3D_API boost::recursive_mutex g_hdf5Mutex
Definition: Hdf5Util.cpp:67
Hdf5Util::H5ScopedDget_type::open
void open(hid_t dataset_id)
Definition: Hdf5Util.h:428
Exc
Namespace for Exception objects.
Definition: Exception.h:57
Hdf5Util::H5ScopedDget_space::open
void open(hid_t dataset_id)
Definition: Hdf5Util.h:398
GlobalLock
boost::recursive_mutex::scoped_lock GlobalLock
Definition: Hdf5Util.h:78
SparseDataReader::m_valuesPerBlock
int m_valuesPerBlock
Definition: SparseDataReader.h:96
Hdf5Util
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
DataTypeTraits
Definition: Traits.h:266
Hdf5Util::H5ScopedDopen::open
void open(hid_t parentLocation, const std::string &name, hid_t dapl_id)
Definition: Hdf5Util.h:368
Hdf5Util::H5ScopedDget_type
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:417
Hdf5Util::H5ScopedDget_space
Scoped object - opens a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:387