Field3D

#include <SparseFieldIO.h>

Inheritance diagram for SparseFieldIO:
FieldIO RefBase

Public Types

typedef SparseFieldIO class_type
 
typedef boost::intrusive_ptr< SparseFieldIOPtr
 
- Public Types inherited from FieldIO
typedef FieldIO class_type
 
typedef boost::intrusive_ptr< FieldIOPtr
 
- Public Types inherited from RefBase
typedef boost::intrusive_ptr< RefBasePtr
 
typedef boost::weak_ptr< RefBaseWeakPtr
 

Public Member Functions

virtual std::string className () const
 Returns the class name. More...
 
virtual FieldBase::Ptr read (hid_t layerGroup, const std::string &filename, const std::string &layerPath, DataTypeEnum typeEnum)
 Reads the field at the given location and tries to create a SparseField object from it. More...
 
 SparseFieldIO ()
 Ctor. More...
 
const char * staticClassType () const
 
virtual bool write (hid_t layerGroup, FieldBase::Ptr field)
 Writes the given field to disk. More...
 
virtual ~SparseFieldIO ()
 Dtor. More...
 
- Public Member Functions inherited from FieldIO
 FieldIO ()
 Ctor. More...
 
virtual ~FieldIO ()
 Dtor. More...
 
- Public Member Functions inherited from RefBase
void ref () const
 Used by boost::intrusive_pointer. More...
 
size_t refcnt ()
 Used by boost::intrusive_pointer. More...
 
void unref () const
 Used by boost::intrusive_pointer. More...
 
WeakPtr weakPtr () const
 
 RefBase ()
 
 RefBase (const RefBase &)
 Copy constructor. More...
 
RefBaseoperator= (const RefBase &)
 Assignment operator. More...
 
virtual ~RefBase ()
 Destructor. More...
 
virtual bool checkRTTI (const char *typenameStr)=0
 This function is only implemented by concrete classes and triggers the actual RTTI check through matchRTTI();. More...
 
bool matchRTTI (const char *typenameStr)
 Performs a check to see if the given typename string matches this class' This needs to be implemented in -all- subclasses, even abstract ones. More...
 

Static Public Member Functions

static FieldIO::Ptr create ()
 
- Static Public Member Functions inherited from FieldIO
static const char * staticClassType ()
 
- Static Public Member Functions inherited from RefBase
static const char * staticClassType ()
 

Public Attributes

 DEFINE_FIELD_RTTI_CONCRETE_CLASS
 
- Public Attributes inherited from FieldIO
 DEFINE_FIELD_RTTI_ABSTRACT_CLASS
 

Private Types

typedef FieldIO base
 Convenience typedef for referring to base class. More...
 

Private Member Functions

template<class Data_T >
bool readData (hid_t location, int numBlocks, const std::string &filename, const std::string &layerPath, typename SparseField< Data_T >::Ptr result)
 Reads the data that is dependent on the data type on disk. More...
 
template<class Data_T >
bool writeInternal (hid_t layerGroup, typename SparseField< Data_T >::Ptr field)
 This call writes all the attributes and sets up the data space. More...
 

Static Private Attributes

static const std::string k_bitsPerComponentStr
 
static const std::string k_blockOrderStr
 
static const std::string k_blockResStr
 
static const std::string k_componentsStr
 
static const std::string k_dataStr
 
static const std::string k_dataWindowStr
 
static const std::string k_extentsStr
 
static const std::string k_numBlocksStr
 
static const std::string k_numOccupiedBlocksStr
 
static const std::string k_versionAttrName
 
static const int k_versionNumber
 

Detailed Description

Defines the IO for a SparseField object

Definition at line 79 of file SparseFieldIO.h.

Member Typedef Documentation

typedef boost::intrusive_ptr<SparseFieldIO> SparseFieldIO::Ptr

Definition at line 86 of file SparseFieldIO.h.

Definition at line 90 of file SparseFieldIO.h.

typedef FieldIO SparseFieldIO::base
private

Convenience typedef for referring to base class.

Definition at line 163 of file SparseFieldIO.h.

Constructor & Destructor Documentation

SparseFieldIO::SparseFieldIO ( )
inline

Ctor.

Definition at line 101 of file SparseFieldIO.h.

102  : FieldIO()
103  { }
FieldIO()
Ctor.
Definition: FieldIO.h:105
virtual SparseFieldIO::~SparseFieldIO ( )
inlinevirtual

Dtor.

Definition at line 106 of file SparseFieldIO.h.

107  { /* Empty */ }

Member Function Documentation

const char* SparseFieldIO::staticClassType ( ) const
inline

Definition at line 93 of file SparseFieldIO.h.

94  {
95  return "SparseFieldIO";
96  }
static FieldIO::Ptr SparseFieldIO::create ( )
inlinestatic

Definition at line 110 of file SparseFieldIO.h.

References read(), and write().

Referenced by initIO().

111  { return Ptr(new SparseFieldIO); }
boost::intrusive_ptr< SparseFieldIO > Ptr
Definition: SparseFieldIO.h:86
FieldBase::Ptr SparseFieldIO::read ( hid_t  layerGroup,
const std::string &  filename,
const std::string &  layerPath,
DataTypeEnum  typeEnum 
)
virtual

Reads the field at the given location and tries to create a SparseField object from it.

Returns
Null if no object was read

Implements FieldIO.

Definition at line 84 of file SparseFieldIO.cpp.

References DataTypeDouble, DataTypeFloat, DataTypeHalf, DataTypeVecDouble, DataTypeVecFloat, DataTypeVecHalf, Msg::print(), Hdf5Util::readAttribute(), SparseField< Data_T >::setBlockOrder(), ResizableField< Data_T >::setSize(), and Msg::SevWarning.

Referenced by create().

87 {
88  Box3i extents, dataW;
89  int components;
90  int blockOrder;
91  int numBlocks;
92  V3i blockRes;
93 
94  if (layerGroup == -1) {
95  Msg::print(Msg::SevWarning, "Bad layerGroup.");
96  return FieldBase::Ptr();
97  }
98 
99  int version;
100  if (!readAttribute(layerGroup, k_versionAttrName, 1, version))
101  throw MissingAttributeException("Couldn't find attribute: " +
103 
104  if (version != k_versionNumber)
105  throw UnsupportedVersionException("SparseField version not supported: " +
106  lexical_cast<std::string>(version));
107 
108  if (!readAttribute(layerGroup, k_extentsStr, 6, extents.min.x))
109  throw MissingAttributeException("Couldn't find attribute: " +
110  k_extentsStr);
111 
112  if (!readAttribute(layerGroup, k_dataWindowStr, 6, dataW.min.x))
113  throw MissingAttributeException("Couldn't find attribute: " +
115 
116  if (!readAttribute(layerGroup, k_componentsStr, 1, components))
117  throw MissingAttributeException("Couldn't find attribute: " +
119 
120  // Read block order
121  if (!readAttribute(layerGroup, k_blockOrderStr, 1, blockOrder))
122  throw MissingAttributeException("Couldn't find attribute: " +
124 
125  // Read number of blocks total
126  if (!readAttribute(layerGroup, k_numBlocksStr, 1, numBlocks))
127  throw MissingAttributeException("Couldn't find attribute: " +
129 
130  // Read block resolution in each dimension
131  if (!readAttribute(layerGroup, k_blockResStr, 3, blockRes.x))
132  throw MissingAttributeException("Couldn't find attribute: " +
133  k_blockResStr);
134 
135  // ... Check that it matches the # reported by summing the active blocks
136 
137  int numCalculatedBlocks = blockRes.x * blockRes.y * blockRes.z;
138  if (numCalculatedBlocks != numBlocks)
139  throw FileIntegrityException("Incorrect block count in SparseFieldIO::read");
140 
141  // Call the appropriate read function based on the data type ---
142 
143  FieldBase::Ptr result;
144 
145  int occupiedBlocks;
146  if (!readAttribute(layerGroup, k_numOccupiedBlocksStr, 1, occupiedBlocks))
147  throw MissingAttributeException("Couldn't find attribute: " +
149 
150  // Check the data type ---
151 
152  int bits;
153  if (!readAttribute(layerGroup, k_bitsPerComponentStr, 1, bits))
154  throw MissingAttributeException("Couldn't find attribute: " +
156 
157  bool isHalf = false;
158  bool isFloat = false;
159  bool isDouble = false;
160 
161  switch (bits) {
162  case 16:
163  isHalf = true;
164  break;
165  case 64:
166  isDouble = true;
167  break;
168  case 32:
169  default:
170  isFloat = true;
171  }
172 
173  // Finally, read the data ---
174 
175  if (components == 1) {
176  if (isHalf && typeEnum == DataTypeHalf) {
178  field->setSize(extents, dataW);
179  field->setBlockOrder(blockOrder);
180  readData<half>(layerGroup, numBlocks, filename, layerPath, field);
181  result = field;
182  } else if (isFloat && typeEnum == DataTypeFloat) {
184  field->setSize(extents, dataW);
185  field->setBlockOrder(blockOrder);
186  readData<float>(layerGroup, numBlocks, filename, layerPath, field);
187  result = field;
188  } else if (isDouble && typeEnum == DataTypeDouble) {
190  field->setSize(extents, dataW);
191  field->setBlockOrder(blockOrder);
192  readData<double>(layerGroup, numBlocks, filename, layerPath, field);
193  result = field;
194  }
195  } else if (components == 3) {
196  if (isHalf && typeEnum == DataTypeVecHalf) {
198  field->setSize(extents, dataW);
199  field->setBlockOrder(blockOrder);
200  readData<V3h>(layerGroup, numBlocks, filename, layerPath, field);
201  result = field;
202  } else if (isFloat && typeEnum == DataTypeVecFloat) {
204  field->setSize(extents, dataW);
205  field->setBlockOrder(blockOrder);
206  readData<V3f>(layerGroup, numBlocks, filename, layerPath, field);
207  result = field;
208  } else if (isDouble && typeEnum == DataTypeVecDouble) {
210  field->setSize(extents, dataW);
211  field->setBlockOrder(blockOrder);
212  readData<V3d>(layerGroup, numBlocks, filename, layerPath, field);
213  result = field;
214  }
215  }
216 
217  return result;
218 }
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
static const std::string k_versionAttrName
static const std::string k_dataWindowStr
FIELD3D_API bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
boost::intrusive_ptr< FieldBase > Ptr
Definition: Field.h:97
static const std::string k_numOccupiedBlocksStr
static const std::string k_extentsStr
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
boost::intrusive_ptr< SparseField > Ptr
Definition: SparseField.h:357
static const int k_versionNumber
Imath::V3i V3i
Definition: SpiMathLib.h:71
static const std::string k_blockOrderStr
static const std::string k_numBlocksStr
static const std::string k_componentsStr
static const std::string k_blockResStr
static const std::string k_bitsPerComponentStr
This Field subclass stores voxel data in block-allocated arrays.
Definition: SparseField.h:350
bool SparseFieldIO::write ( hid_t  layerGroup,
FieldBase::Ptr  field 
)
virtual

Writes the given field to disk.

Returns
true if successful, otherwise false

Implements FieldIO.

Definition at line 223 of file SparseFieldIO.cpp.

References FIELD3D_NAMESPACE_SOURCE_CLOSE, field_dynamic_cast(), Msg::print(), Msg::SevWarning, and Hdf5Util::writeAttribute().

Referenced by create().

224 {
225  if (layerGroup == -1) {
226  Msg::print(Msg::SevWarning, "Bad layerGroup.");
227  return false;
228  }
229 
230  // Add version attribute
231  if (!writeAttribute(layerGroup, k_versionAttrName,
232  1, k_versionNumber)) {
233  Msg::print(Msg::SevWarning, "Error adding version attribute.");
234  return false;
235  }
236 
237  SparseField<half>::Ptr halfField =
239  SparseField<float>::Ptr floatField =
241  SparseField<double>::Ptr doubleField =
243  SparseField<V3h>::Ptr vecHalfField =
245  SparseField<V3f>::Ptr vecFloatField =
247  SparseField<V3d>::Ptr vecDoubleField =
249 
250  bool success = true;
251  if (halfField) {
252  success = writeInternal<half>(layerGroup, halfField);
253  } else if (floatField) {
254  success = writeInternal<float>(layerGroup, floatField);
255  } else if (doubleField) {
256  success = writeInternal<double>(layerGroup, doubleField);
257  } else if (vecHalfField) {
258  success = writeInternal<V3h>(layerGroup, vecHalfField);
259  } else if (vecFloatField) {
260  success = writeInternal<V3f>(layerGroup, vecFloatField);
261  } else if (vecDoubleField) {
262  success = writeInternal<V3d>(layerGroup, vecDoubleField);
263  } else {
264  throw WriteLayerException("SparseFieldIO::write does not support the given "
265  "SparseField template parameter");
266  }
267 
268  return success;
269 }
Field_T::Ptr field_dynamic_cast(RefBase::Ptr field)
Dynamic cast that uses string-comparison in order to be safe even after an object crosses a shared li...
Definition: RefCount.h:256
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
static const std::string k_versionAttrName
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
boost::intrusive_ptr< SparseField > Ptr
Definition: SparseField.h:357
static const int k_versionNumber
This Field subclass stores voxel data in block-allocated arrays.
Definition: SparseField.h:350
virtual std::string SparseFieldIO::className ( ) const
inlinevirtual

Returns the class name.

Implements FieldIO.

Definition at line 127 of file SparseFieldIO.h.

References readData(), and writeInternal().

128  { return "SparseField"; }
template<class Data_T >
bool SparseFieldIO::writeInternal ( hid_t  layerGroup,
typename SparseField< Data_T >::Ptr  field 
)
private

This call writes all the attributes and sets up the data space.

Todo:
Tune the chunk size of the gzip call

Definition at line 172 of file SparseFieldIO.h.

References Hdf5Util::checkHdf5Gzip(), Sparse::SparseBlock< Data_T >::data, FieldTraits< Data_T >::dataDims(), FieldRes::dataWindow(), Sparse::SparseBlock< Data_T >::emptyValue, FieldRes::extents(), g_hdf5Mutex, DataTypeTraits< T >::h5bits(), DataTypeTraits< T >::h5type(), Hdf5Util::H5Base::id(), Sparse::SparseBlock< Data_T >::isAllocated, k_bitsPerComponentStr, k_blockOrderStr, k_blockResStr, k_componentsStr, k_dataStr, k_dataWindowStr, k_extentsStr, k_numBlocksStr, k_numOccupiedBlocksStr, SparseField< Data_T >::m_blockOrder, SparseField< Data_T >::m_blockRes, SparseField< Data_T >::m_blocks, Msg::print(), Msg::SevWarning, and Hdf5Util::writeAttribute().

Referenced by className().

174 {
175  using namespace std;
176  using namespace Exc;
177  using namespace Hdf5Util;
178  using namespace Sparse;
179 
180  GlobalLock lock(g_hdf5Mutex);
181 
182  Box3i ext(field->extents()), dw(field->dataWindow());
183 
184  int components = FieldTraits<Data_T>::dataDims();
185 
186  int valuesPerBlock = (1 << (field->m_blockOrder * 3)) * components;
187 
188  // Add extents attribute ---
189 
190  int extents[6] =
191  { ext.min.x, ext.min.y, ext.min.z, ext.max.x, ext.max.y, ext.max.z };
192 
193  if (!writeAttribute(layerGroup, k_extentsStr, 6, extents[0])) {
194  Msg::print(Msg::SevWarning, "Error adding size attribute.");
195  return false;
196  }
197 
198  // Add data window attribute ---
199 
200  int dataWindow[6] =
201  { dw.min.x, dw.min.y, dw.min.z, dw.max.x, dw.max.y, dw.max.z };
202 
203  if (!writeAttribute(layerGroup, k_dataWindowStr, 6, dataWindow[0])) {
204  Msg::print(Msg::SevWarning, "Error adding size attribute.");
205  return false;
206  }
207 
208  // Add components attribute ---
209 
210  if (!writeAttribute(layerGroup, k_componentsStr, 1, components)) {
211  Msg::print(Msg::SevWarning, "Error adding components attribute.");
212  return false;
213  }
214 
215  // Add block order attribute ---
216 
217  int blockOrder = field->m_blockOrder;
218 
219  if (!writeAttribute(layerGroup, k_blockOrderStr, 1, blockOrder)) {
220  Msg::print(Msg::SevWarning, "Error adding block order attribute.");
221  return false;
222  }
223 
224  // Add number of blocks attribute ---
225 
226  V3i &blockRes = field->m_blockRes;
227  int numBlocks = blockRes.x * blockRes.y * blockRes.z;
228 
229  if (!writeAttribute(layerGroup, k_numBlocksStr, 1, numBlocks)) {
230  Msg::print(Msg::SevWarning, "Error adding number of blocks attribute.");
231  return false;
232  }
233 
234  // Add block resolution in each dimension ---
235 
236  if (!writeAttribute(layerGroup, k_blockResStr, 3, blockRes.x)) {
237  Msg::print(Msg::SevWarning, "Error adding block res attribute.");
238  return false;
239  }
240 
241  // Add the bits per component attribute ---
242 
243  int bits = DataTypeTraits<Data_T>::h5bits();
244  if (!writeAttribute(layerGroup, k_bitsPerComponentStr, 1, bits)) {
245  Msg::print(Msg::SevWarning, "Error adding bits per component attribute.");
246  return false;
247  }
248 
249  // Write the block info data sets ---
250 
251  SparseBlock<Data_T> *blocks = field->m_blocks;
252 
253  // ... Write the isAllocated array
254  {
255  vector<char> isAllocated(numBlocks);
256  for (int i = 0; i < numBlocks; ++i) {
257  isAllocated[i] = static_cast<char>(blocks[i].isAllocated);
258  }
259  writeSimpleData<char>(layerGroup, "block_is_allocated_data", isAllocated);
260  }
261 
262  // ... Write the emptyValue array
263  {
264  vector<Data_T> emptyValue(numBlocks);
265  for (int i = 0; i < numBlocks; ++i) {
266  emptyValue[i] = static_cast<Data_T>(blocks[i].emptyValue);
267  }
268  writeSimpleData<Data_T>(layerGroup, "block_empty_value_data", emptyValue);
269  }
270 
271  // Count the number of occupied blocks ---
272  int occupiedBlocks = 0;
273  for (int i = 0; i < numBlocks; ++i) {
274  if (blocks[i].isAllocated) {
275  occupiedBlocks++;
276  }
277  }
278 
279  if (!writeAttribute(layerGroup, k_numOccupiedBlocksStr, 1, occupiedBlocks)) {
280  throw WriteAttributeException("Couldn't add attribute " +
282  }
283 
284  if (occupiedBlocks > 0) {
285 
286  // Make the memory data space
287  hsize_t memDims[1];
288  memDims[0] = valuesPerBlock;
289  H5ScopedScreate memDataSpace(H5S_SIMPLE);
290  H5Sset_extent_simple(memDataSpace.id(), 1, memDims, NULL);
291 
292  // Make the file data space
293  hsize_t fileDims[2];
294  fileDims[0] = occupiedBlocks;
295  fileDims[1] = valuesPerBlock;
296  H5ScopedScreate fileDataSpace(H5S_SIMPLE);
297  H5Sset_extent_simple(fileDataSpace.id(), 2, fileDims, NULL);
298 
299  // Set up gzip property list
300  bool gzipAvailable = checkHdf5Gzip();
301  hid_t dcpl = H5Pcreate(H5P_DATASET_CREATE);
302  hsize_t chunkSize[2];
303  chunkSize[0] = 1;
304  chunkSize[1] = valuesPerBlock;
305  if (gzipAvailable) {
306  herr_t status = H5Pset_deflate(dcpl, 9);
307  if (status < 0) {
308  return false;
309  }
310  status = H5Pset_chunk(dcpl, 2, chunkSize);
311  if (status < 0) {
312  return false;
313  }
314  }
315 
316  // Add the data set
317  H5ScopedDcreate dataSet(layerGroup, k_dataStr,
319  fileDataSpace.id(),
320  H5P_DEFAULT, dcpl, H5P_DEFAULT);
321  if (dataSet.id() < 0)
322  throw CreateDataSetException("Couldn't create data set in "
323  "SparseFieldIO::writeInternal");
324 
325  // For each allocated block ---
326 
327  int nextBlockIdx = 0;
328  hsize_t offset[2];
329  hsize_t count[2];
330  herr_t status;
331 
332  for (int i = 0; i < numBlocks; ++i) {
333  if (blocks[i].isAllocated) {
334  offset[0] = nextBlockIdx; // Index of next block
335  offset[1] = 0; // Index of first data in block. Always 0
336  count[0] = 1; // Number of columns to read. Always 1
337  count[1] = valuesPerBlock; // Number of values in one column
338  status = H5Sselect_hyperslab(fileDataSpace.id(), H5S_SELECT_SET,
339  offset, NULL, count, NULL);
340  if (status < 0) {
341  throw WriteHyperSlabException(
342  "Couldn't select slab " +
343  boost::lexical_cast<std::string>(nextBlockIdx));
344  }
345  Data_T *data = field->m_blocks[i].data;
346  status = H5Dwrite(dataSet.id(), DataTypeTraits<Data_T>::h5type(),
347  memDataSpace.id(),
348  fileDataSpace.id(), H5P_DEFAULT, data);
349  if (status < 0) {
350  throw WriteHyperSlabException(
351  "Couldn't write slab " +
352  boost::lexical_cast<std::string>(nextBlockIdx));
353  }
354  // Increment nextBlockIdx
355  nextBlockIdx++;
356  }
357  }
358 
359  } // if occupiedBlocks > 0
360 
361  return true;
362 
363 }
Imath::Box3i Box3i
Definition: SpiMathLib.h:77
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
FIELD3D_API bool writeAttribute(hid_t location, const std::string &attrName, const std::string &value)
Writes a string attribute.
static int dataDims()
Dimensions of the given data type. i.e. 3 for V3f, 1 for float.
Namespace for Exception objects.
Definition: Exception.h:57
static const std::string k_dataStr
static const std::string k_dataWindowStr
static const std::string k_numOccupiedBlocksStr
static const std::string k_extentsStr
boost::recursive_mutex::scoped_lock GlobalLock
Definition: Hdf5Util.h:78
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
Data_T emptyValue
The value to use if the block isn&#39;t allocated. We allow setting this per block so that we for example...
Definition: SparseField.h:308
Imath::V3i V3i
Definition: SpiMathLib.h:71
FIELD3D_API bool checkHdf5Gzip()
Checks whether gzip is available in the current hdf5 library.
Definition: Hdf5Util.cpp:722
static const std::string k_blockOrderStr
static hid_t h5type()
Namespace for sparse field specifics.
Definition: SparseField.h:221
static const std::string k_numBlocksStr
static const std::string k_componentsStr
static const std::string k_blockResStr
static int h5bits()
bool isAllocated
Whether the block is allocated or not.
Definition: SparseField.h:303
Storage for one individual block of a SparseField.
Definition: SparseField.h:227
Scoped object - creates a dataspace on creation and closes it on destruction.
Definition: Hdf5Util.h:234
FIELD3D_NAMESPACE_OPEN FIELD3D_API boost::recursive_mutex g_hdf5Mutex
Definition: Hdf5Util.cpp:67
static const std::string k_bitsPerComponentStr
Scoped object - creates a dataset on creation and closes it on destruction.
Definition: Hdf5Util.h:263
template<class Data_T >
bool SparseFieldIO::readData ( hid_t  location,
int  numBlocks,
const std::string &  filename,
const std::string &  layerPath,
typename SparseField< Data_T >::Ptr  result 
)
private

Reads the data that is dependent on the data type on disk.

Definition at line 368 of file SparseFieldIO.h.

References SparseField< Data_T >::addReference(), FieldTraits< Data_T >::dataDims(), SparseFileManager::doLimitMemUse(), Sparse::SparseBlock< Data_T >::emptyValue, FIELD3D_NAMESPACE_HEADER_CLOSE, Sparse::SparseBlock< Data_T >::isAllocated, k_numOccupiedBlocksStr, SparseField< Data_T >::m_blockOrder, SparseField< Data_T >::m_blocks, Hdf5Util::readAttribute(), SparseDataReader< Data_T >::readBlockList(), Sparse::SparseBlock< Data_T >::resize(), SparseField< Data_T >::setupReferenceBlocks(), and SparseFileManager::singleton().

Referenced by className().

373 {
374  using namespace std;
375  using namespace Exc;
376  using namespace Hdf5Util;
377  using namespace Sparse;
378 
379  int occupiedBlocks;
380 
381  bool dynamicLoading = SparseFileManager::singleton().doLimitMemUse();
382 
383  int components = FieldTraits<Data_T>::dataDims();
384  int valuesPerBlock = (1 << (result->m_blockOrder * 3)) * components;
385 
386  // Read the number of occupied blocks ---
387 
388  if (!readAttribute(location, k_numOccupiedBlocksStr, 1, occupiedBlocks))
389  throw MissingAttributeException("Couldn't find attribute: " +
391 
392  // Set up the dynamic read info ---
393 
394  if (dynamicLoading) {
395  // Set up the field reference
396  result->addReference(filename, layerPath,
397  valuesPerBlock,
398  occupiedBlocks);
399  }
400 
401  // Read the block info data sets ---
402 
403  SparseBlock<Data_T> *blocks = result->m_blocks;
404 
405  // ... Read the isAllocated array
406 
407  {
408  vector<char> isAllocated(numBlocks);
409  readSimpleData<char>(location, "block_is_allocated_data", isAllocated);
410  for (int i = 0; i < numBlocks; ++i) {
411  blocks[i].isAllocated = isAllocated[i];
412  if (!dynamicLoading && isAllocated[i]) {
413  blocks[i].resize(valuesPerBlock);
414  }
415  }
416  }
417 
418  // ... Read the emptyValue array ---
419 
420  {
421  vector<Data_T> emptyValue(numBlocks);
422  readSimpleData<Data_T>(location, "block_empty_value_data", emptyValue);
423  for (int i = 0; i < numBlocks; ++i) {
424  blocks[i].emptyValue = emptyValue[i];
425  }
426  }
427 
428  // Read the data ---
429 
430  if (occupiedBlocks > 0) {
431 
432  if (dynamicLoading) {
433 
434  result->setupReferenceBlocks();
435 
436  } else {
437 
438  size_t b = 0, bend = b + numBlocks;
439 
440  SparseDataReader<Data_T> reader(location, valuesPerBlock, occupiedBlocks);
441 
442  // We'll read at most 50meg at a time
443  static const long maxMemPerPass = 50*1024*1024;
444 
445  for (int nextBlockIdx = 0;;) {
446 
447  long mem = 0;
448  std::vector<Data_T*> memoryList;
449 
450  for (; b != bend && mem < maxMemPerPass; ++b) {
451  if (blocks[b].isAllocated) {
452  mem += sizeof(Data_T)*valuesPerBlock;
453  memoryList.push_back(blocks[b].data);
454  }
455  }
456 
457  // all done.
458  if (!memoryList.size()) {
459  break;
460  }
461 
462  reader.readBlockList(nextBlockIdx, memoryList);
463  nextBlockIdx += memoryList.size();
464  }
465 
466  }
467 
468  } // if occupiedBlocks > 0
469 
470  return true;
471 
472 }
Contains utility functions and classes for Hdf5 files.
Definition: Hdf5Util.h:86
static int dataDims()
Dimensions of the given data type. i.e. 3 for V3f, 1 for float.
Namespace for Exception objects.
Definition: Exception.h:57
FIELD3D_API bool readAttribute(hid_t location, const std::string &attrName, std::string &value)
Reads a string attribute.
static const std::string k_numOccupiedBlocksStr
Data_T emptyValue
The value to use if the block isn&#39;t allocated. We allow setting this per block so that we for example...
Definition: SparseField.h:308
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.
Namespace for sparse field specifics.
Definition: SparseField.h:221
void resize(int n)
Alloc data.
Definition: SparseField.h:259
bool isAllocated
Whether the block is allocated or not.
Definition: SparseField.h:303
bool doLimitMemUse() const
Returns whether to limit memory usage and do dynamic loading for sparse fields.
Definition: SparseFile.cpp:80
Storage for one individual block of a SparseField.
Definition: SparseField.h:227
static SparseFileManager & singleton()
Returns a reference to the singleton instance.
Definition: SparseFile.cpp:63

Member Data Documentation

SparseFieldIO::DEFINE_FIELD_RTTI_CONCRETE_CLASS

Definition at line 91 of file SparseFieldIO.h.

const int SparseFieldIO::k_versionNumber
staticprivate

Definition at line 148 of file SparseFieldIO.h.

const std::string SparseFieldIO::k_versionAttrName
staticprivate

Definition at line 149 of file SparseFieldIO.h.

const std::string SparseFieldIO::k_extentsStr
staticprivate

Definition at line 150 of file SparseFieldIO.h.

Referenced by writeInternal().

const std::string SparseFieldIO::k_dataWindowStr
staticprivate

Definition at line 151 of file SparseFieldIO.h.

Referenced by writeInternal().

const std::string SparseFieldIO::k_componentsStr
staticprivate

Definition at line 152 of file SparseFieldIO.h.

Referenced by writeInternal().

const std::string SparseFieldIO::k_blockOrderStr
staticprivate

Definition at line 153 of file SparseFieldIO.h.

Referenced by writeInternal().

const std::string SparseFieldIO::k_numBlocksStr
staticprivate

Definition at line 154 of file SparseFieldIO.h.

Referenced by writeInternal().

const std::string SparseFieldIO::k_blockResStr
staticprivate

Definition at line 155 of file SparseFieldIO.h.

Referenced by writeInternal().

const std::string SparseFieldIO::k_bitsPerComponentStr
staticprivate

Definition at line 156 of file SparseFieldIO.h.

Referenced by writeInternal().

const std::string SparseFieldIO::k_numOccupiedBlocksStr
staticprivate

Definition at line 157 of file SparseFieldIO.h.

Referenced by readData(), and writeInternal().

const std::string SparseFieldIO::k_dataStr
staticprivate

Definition at line 158 of file SparseFieldIO.h.

Referenced by writeInternal().


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