2 #ifndef MARRAY_HDF5_HXX 3 #define MARRAY_HDF5_HXX 7 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR >= 8 && defined(H5_USE_16_API_DEFAULT)) 8 #define H5Gcreate_vers 2 10 #define H5Dopen_vers 2 11 #define H5Dcreate_vers 2 12 #define H5Acreate_vers 2 27 template<
bool B>
class HandleCheck;
28 template<>
class HandleCheck<false> {
31 { counter_ = H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL); }
33 { marray_detail::Assert( counter_ == H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL)); }
37 template<>
class HandleCheck<true> {
56 inline hid_t
createGroup(
const hid_t&,
const std::string&);
57 inline hid_t
openGroup(
const hid_t&,
const std::string&);
61 void save(
const hid_t&,
const std::string&,
const Marray<T>&);
62 template<
class T,
bool isConst>
65 void save(
const hid_t&,
const std::string&,
const std::vector<T>&);
66 template<
class T,
class BaseIterator,
class ShapeIterator>
68 BaseIterator, BaseIterator, ShapeIterator,
const Marray<T>&);
69 template<
class T,
class ShapeIterator>
70 void create(
const hid_t&,
const std::string&, ShapeIterator,
78 void loadVec(
const hid_t&,
const std::string&, std::vector<T>&);
79 template<
class T,
class BaseIterator,
class ShapeIterator>
81 BaseIterator, BaseIterator, ShapeIterator,
Marray<T>&);
87 inline hid_t uintTypeHelper() {
98 throw std::runtime_error(
"No matching HDF5 type.");
103 inline hid_t intTypeHelper() {
108 return H5T_STD_I16LE;
110 return H5T_STD_I32LE;
112 return H5T_STD_I64LE;
114 throw std::runtime_error(
"No matching HDF5 type.");
119 inline hid_t floatingTypeHelper() {
122 return H5T_IEEE_F32LE;
124 return H5T_IEEE_F64LE;
126 throw std::runtime_error(
"No matching HDF5 type.");
131 inline hid_t hdf5Type();
133 template<>
inline hid_t hdf5Type<unsigned char>()
134 {
return uintTypeHelper<unsigned char>(); }
135 template<>
inline hid_t hdf5Type<unsigned short>()
136 {
return uintTypeHelper<unsigned short>(); }
137 template<>
inline hid_t hdf5Type<unsigned int>()
138 {
return uintTypeHelper<unsigned int>(); }
139 template<>
inline hid_t hdf5Type<unsigned long>()
140 {
return uintTypeHelper<unsigned long>(); }
141 template<>
inline hid_t hdf5Type<unsigned long long>()
142 {
return uintTypeHelper<unsigned long long>(); }
144 template<>
inline hid_t hdf5Type<char>()
145 {
return uintTypeHelper<char>(); }
146 template<>
inline hid_t hdf5Type<signed char>()
147 {
return intTypeHelper<signed char>(); }
148 template<>
inline hid_t hdf5Type<short>()
149 {
return intTypeHelper<short>(); }
150 template<>
inline hid_t hdf5Type<int>()
151 {
return intTypeHelper<int>(); }
152 template<>
inline hid_t hdf5Type<long>()
153 {
return intTypeHelper<long>(); }
154 template<>
inline hid_t hdf5Type<long long>()
155 {
return intTypeHelper<long long>(); }
157 template<>
inline hid_t hdf5Type<float>()
158 {
return floatingTypeHelper<float>(); }
159 template<>
inline hid_t hdf5Type<double>()
160 {
return floatingTypeHelper<double>(); }
175 template<
class T,
class ShapeIterator>
177 const hid_t& groupHandle,
178 const std::string& datasetName,
184 HandleCheck<MARRAY_NO_DEBUG> handleCheck;
187 hid_t datatype = H5Tcopy(hdf5Type<T>());
188 size_t dimension = std::distance(begin, end);
192 for(
size_t j=0; j<dimension; ++j) {
193 shape[j] = hsize_t(*begin);
199 for(
size_t j=0; j<dimension; ++j) {
200 shape[dimension-j-1] = hsize_t(*begin);
204 hid_t dataspace = H5Screate_simple(dimension, &shape[0], NULL);
207 throw std::runtime_error(
"Marray cannot create dataspace.");
212 hid_t dataset = H5Dcreate(groupHandle, datasetName.c_str(), datatype,
213 dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
217 throw std::runtime_error(
"Marray cannot create dataset.");
222 hsize_t attributeShape[1] = {1};
223 hid_t attributeDataspace = H5Screate_simple(1, attributeShape, NULL);
224 if(attributeDataspace < 0) {
228 throw std::runtime_error(
"Marray cannot create dataspace.");
230 hid_t attribute = H5Acreate(dataset, reverseShapeAttributeName,
231 H5T_STD_U8LE, attributeDataspace, H5P_DEFAULT, H5P_DEFAULT);
233 H5Sclose(attributeDataspace);
237 throw std::runtime_error(
"Marray cannot create attribute.");
239 unsigned int data = 1;
240 herr_t err = H5Awrite(attribute, H5T_STD_U8LE, &data);
242 H5Sclose(attributeDataspace);
247 throw std::runtime_error(
"Marray cannot create write to attribute.");
268 const hid_t& groupHandle,
269 const std::string& datasetName,
273 HandleCheck<MARRAY_NO_DEBUG> handleCheck;
276 hid_t datatype = H5Tcopy(hdf5Type<T>());
281 shape[j] = hsize_t(in.
shape(j));
290 hid_t dataspace = H5Screate_simple(in.
dimension(), &shape[0], NULL);
293 throw std::runtime_error(
"Marray cannot create dataspace.");
297 hid_t dataset = H5Dcreate(groupHandle, datasetName.c_str(), datatype,
298 dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
302 throw std::runtime_error(
"Marray cannot create dataset.");
307 hsize_t attributeShape[1] = {1};
308 hid_t attributeDataspace = H5Screate_simple(1, attributeShape, NULL);
309 if(attributeDataspace < 0) {
313 throw std::runtime_error(
"Marray cannot create dataspace.");
315 hid_t attribute = H5Acreate(dataset, reverseShapeAttributeName,
316 H5T_STD_U8LE, attributeDataspace, H5P_DEFAULT, H5P_DEFAULT);
318 H5Sclose(attributeDataspace);
322 throw std::runtime_error(
"Marray cannot create attribute.");
324 unsigned int data = 1;
325 herr_t err = H5Awrite(attribute, H5T_STD_U8LE, &data);
327 H5Sclose(attributeDataspace);
332 throw std::runtime_error(
"Marray cannot create write to attribute.");
337 herr_t status = H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL,
338 H5P_DEFAULT, &in(0));
343 throw std::runtime_error(
"Marray cannot write to dataset.");
357 template<
class T,
bool isConst>
359 const hid_t& groupHandle,
360 const std::string& datasetName,
364 save(groupHandle, datasetName, m);
377 const hid_t& groupHandle,
378 const std::string& datasetName,
379 const std::vector<T>& in
383 for(
size_t j=0; j<in.size(); ++j) {
386 save(groupHandle, datasetName, v);
399 const hid_t& groupHandle,
400 const std::string& datasetName,
404 HandleCheck<MARRAY_NO_DEBUG> handleCheck;
406 hid_t dataset = H5Dopen(groupHandle, datasetName.c_str(), H5P_DEFAULT);
408 throw std::runtime_error(
"Marray cannot open dataset.");
410 hid_t filespace = H5Dget_space(dataset);
411 hid_t type = H5Dget_type(dataset);
412 hid_t nativeType = H5Tget_native_type(type, H5T_DIR_DESCEND);
413 if(!H5Tequal(nativeType, hdf5Type<T>())) {
415 H5Tclose(nativeType);
418 throw std::runtime_error(
"Data types not equal error.");
420 int dimension = H5Sget_simple_extent_ndims(filespace);
422 herr_t status = H5Sget_simple_extent_dims(filespace, &shape[0], NULL);
425 H5Tclose(nativeType);
428 throw std::runtime_error(
"H5Sget_simple_extent_dims error.");
430 hid_t memspace = H5Screate_simple(dimension, &shape[0], NULL);
434 for(
size_t j=0; j<newShape.
size(); ++j) {
435 newShape(j) = (
size_t)(shape[j]);
437 if(H5Aexists(dataset, reverseShapeAttributeName) > 0) {
449 status = H5Dread(dataset, nativeType, memspace, filespace,
450 H5P_DEFAULT, &out(0));
452 H5Tclose(nativeType);
457 throw std::runtime_error(
"Marray cannot read from dataset.");
473 const hid_t& groupHandle,
474 const std::string& datasetName,
478 HandleCheck<MARRAY_NO_DEBUG> handleCheck;
481 hid_t dataset = H5Dopen(groupHandle, datasetName.c_str(), H5P_DEFAULT);
483 throw std::runtime_error(
"Marray cannot open dataset.");
485 hid_t filespace = H5Dget_space(dataset);
486 hsize_t dimension = H5Sget_simple_extent_ndims(filespace);
487 hsize_t* shape =
new hsize_t[(
size_t)(dimension)];
488 herr_t status = H5Sget_simple_extent_dims(filespace, shape, NULL);
493 throw std::runtime_error(
"Marray cannot get extension of dataset.");
498 if(H5Aexists(dataset, reverseShapeAttributeName) > 0) {
499 for(
size_t j=0; j<out.
size(); ++j) {
500 out[out.
size()-j-1] = T(shape[j]);
504 for(
size_t j=0; j<out.
size(); ++j) {
505 out[j] = T(shape[j]);
526 const hid_t& groupHandle,
527 const std::string& datasetName,
532 load( groupHandle,datasetName,v);
533 out.resize(v.
size());
534 for(
size_t j=0; j<v.
size(); ++j) {
550 template<
class T,
class BaseIterator,
class ShapeIterator>
552 const hid_t& groupHandle,
553 const std::string& datasetName,
554 BaseIterator baseBegin,
555 BaseIterator baseEnd,
556 ShapeIterator shapeBegin,
560 HandleCheck<MARRAY_NO_DEBUG> handleCheck;
563 hid_t dataset = H5Dopen(groupHandle, datasetName.c_str(), H5P_DEFAULT);
565 throw std::runtime_error(
"Marray cannot open dataset.");
569 size_t size = std::distance(baseBegin, baseEnd);
574 if(H5Aexists(dataset, reverseShapeAttributeName) > 0) {
580 offset[j] = hsize_t(*baseBegin);
581 slabShape[j] = hsize_t(*shapeBegin);
582 marrayShape[k] = slabShape[j];
597 for(
size_t j=0; j<size; ++j) {
598 offset[j] = hsize_t(*baseBegin);
599 slabShape[j] = hsize_t(*shapeBegin);
600 marrayShape[j] = slabShape[j];
607 hid_t datatype = H5Dget_type(dataset);
609 if(!H5Tequal(datatype, hdf5Type<T>())) {
610 throw std::runtime_error(
"data type of stored hdf5 dataset and passed array do not match in loadHyperslab");
613 hid_t dataspace = H5Dget_space(dataset);
614 herr_t status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET,
615 &offset[0], NULL, &slabShape[0], NULL);
620 throw std::runtime_error(
"Marray cannot select hyperslab. Check offset and shape!");
624 hid_t memspace = H5Screate_simple(
int(size), &marrayShape[0], NULL);
626 status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, &offsetOut[0],
627 NULL, &marrayShape[0], NULL);
633 throw std::runtime_error(
"Marray cannot select hyperslab. Check offset and shape!");
638 (&marrayShape[0])+size, coordinateOrder);
639 status = H5Dread(dataset, datatype, memspace, dataspace,
640 H5P_DEFAULT, &(out(0)));
648 throw std::runtime_error(
"Marray cannot read from dataset.");
664 template<
class T,
class BaseIterator,
class ShapeIterator>
667 const hid_t& groupHandle,
668 const std::string& datasetName,
669 BaseIterator baseBegin,
670 BaseIterator baseEnd,
671 ShapeIterator shapeBegin,
675 HandleCheck<MARRAY_NO_DEBUG> handleCheck;
678 hid_t dataset = H5Dopen(groupHandle, datasetName.c_str(), H5P_DEFAULT);
680 throw std::runtime_error(
"Marray cannot open dataset.");
686 memoryShape[j] = in.
shape(j);
688 size_t size = std::distance(baseBegin, baseEnd);
691 bool reverseShapeAttribute =
692 (H5Aexists(dataset, reverseShapeAttributeName) > 0);
697 offset[j] = hsize_t(*baseBegin);
698 slabShape[j] = hsize_t(*shapeBegin);
710 for(
size_t j=0; j<size; ++j) {
711 offset[j] = hsize_t(*baseBegin);
712 slabShape[j] = hsize_t(*shapeBegin);
719 throw std::runtime_error(
"Marray cannot write to HDF5 file. A different order was used when the file was created.");
723 hid_t datatype = H5Dget_type(dataset);
724 hid_t dataspace = H5Dget_space(dataset);
725 herr_t status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET,
726 &offset[0], NULL, &slabShape[0], NULL);
731 throw std::runtime_error(
"Marray cannot select hyperslab. Check offset and shape!");
735 hid_t memspace = H5Screate_simple(
int(in.
dimension()), &memoryShape[0], NULL);
737 status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, &memoryOffset[0], NULL,
738 &memoryShape[0], NULL);
744 throw std::runtime_error(
"Marray cannot select hyperslab. Check offset and shape!");
748 status = H5Dwrite(dataset, datatype, memspace, dataspace, H5P_DEFAULT, &(in(0)));
756 throw std::runtime_error(
"Marray cannot write to dataset.");
773 const std::string& filename,
777 hid_t version = H5P_DEFAULT;
778 if(hdf5version == LATEST_HDF5_VERSION) {
779 version = H5Pcreate(H5P_FILE_ACCESS);
780 H5Pset_libver_bounds(version, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST);
783 hid_t fileHandle = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, version);
785 throw std::runtime_error(
"Could not create HDF5 file: " + filename);
804 const std::string& filename,
809 hid_t access = H5F_ACC_RDONLY;
810 if(fileAccessMode == READ_WRITE) {
811 access = H5F_ACC_RDWR;
814 hid_t version = H5P_DEFAULT;
815 if(hdf5version == LATEST_HDF5_VERSION) {
816 version = H5Pcreate(H5P_FILE_ACCESS);
817 H5Pset_libver_bounds(version, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST);
820 hid_t fileHandle = H5Fopen(filename.c_str(), access, version);
822 throw std::runtime_error(
"Could not open HDF5 file: " + filename);
853 const hid_t& parentHandle,
854 const std::string& groupName
857 hid_t groupHandle = H5Gcreate(parentHandle, groupName.c_str(),
858 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
859 if(groupHandle < 0) {
860 throw std::runtime_error(
"Could not create HDF5 group.");
876 const hid_t& parentHandle,
877 const std::string& groupName
880 hid_t groupHandle = H5Gopen(parentHandle, groupName.c_str(), H5P_DEFAULT);
881 if(groupHandle < 0) {
882 throw std::runtime_error(
"Could not open HDF5 group.");
905 #endif // #ifndef MARRAY_HDF5_HXX void loadVec(const hid_t &, const std::string &, std::vector< T > &)
Load a vector of an HDF5 dataset.
hid_t createFile(const std::string &, HDF5Version=DEFAULT_HDF5_VERSION)
Create an HDF5 file.
const char reverseShapeAttributeName[14]
Runtime-flexible multi-dimensional views and arrays.
const size_t shape(const size_t) const
Get the shape in one dimension.
reverse_iterator rbegin()
Array-Interface to an interval of memory.
void create(const hid_t &, const std::string &, ShapeIterator, ShapeIterator, CoordinateOrder)
Create and close an HDF5 dataset to store Marray data.
hid_t createGroup(const hid_t &, const std::string &)
Create an HDF5 group.
void load(const hid_t &, const std::string &, Marray< T > &)
Load an Marray from an HDF5 dataset.
const size_t dimension() const
Get the dimension.
void loadHyperslab(const hid_t &, const std::string &, BaseIterator, BaseIterator, ShapeIterator, Marray< T > &)
Load a hyperslab from an HDF5 dataset into an Marray.
const bool MARRAY_NO_ARG_TEST
Argument testing enabled.
void loadShape(const hid_t &, const std::string &, Vector< T > &)
Load the shape of an HDF5 dataset.
const size_t size() const
void closeFile(const hid_t &)
Close an HDF5 file.
void save(const hid_t &, const std::string &, const Marray< T > &)
Save an Marray as an HDF5 dataset.
void closeGroup(const hid_t &)
Close an HDF5 group.
static const InitializationSkipping SkipInitialization
Flag to indicate initialization skipping.
void saveHyperslab(const hid_t &, const std::string &, BaseIterator, BaseIterator, ShapeIterator, const Marray< T > &)
Save an Marray as a hyperslab into an HDF5 dataset.
const CoordinateOrder & coordinateOrder() const
Get the coordinate order used for scalar indexing and iterators.
hid_t openFile(const std::string &, FileAccessMode=READ_ONLY, HDF5Version=DEFAULT_HDF5_VERSION)
Open an HDF5 file.
hid_t openGroup(const hid_t &, const std::string &)
Open an HDF5 group.