OpenGM  2.3.x
Discrete Graphical Model Library
marray_hdf5.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef MARRAY_HDF5_HXX
3 #define MARRAY_HDF5_HXX
4 
5 // compat fix for buggy hdf5 1.8 versions
6 #include <H5version.h>
7 #if (H5_VERS_MAJOR == 1 && H5_VERS_MINOR >= 8 && defined(H5_USE_16_API_DEFAULT))
8 #define H5Gcreate_vers 2
9 #define H5Gopen_vers 2
10 #define H5Dopen_vers 2
11 #define H5Dcreate_vers 2
12 #define H5Acreate_vers 2
13 #endif
14 
15 #include <cstring>
16 
17 #include "marray.hxx"
18 #include "hdf5.h"
19 
20 namespace marray {
22 namespace hdf5 {
23 
24 // assertion testing
25 
26 // \cond suppress doxygen
27 template<bool B> class HandleCheck;
28 template<> class HandleCheck<false> {
29 public:
30  HandleCheck()
31  { counter_ = H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL); }
32  void check()
33  { marray_detail::Assert( counter_ == H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL)); }
34 private:
35  ssize_t counter_;
36 };
37 template<> class HandleCheck<true> {
38 public:
39  void check() {}
40 };
41 // \endcond
42 
43 // namespace variables
44 
45 const char reverseShapeAttributeName[14] = "reverse-shape";
46 
47 // prototypes
48 
51 
52 inline hid_t createFile(const std::string&, HDF5Version = DEFAULT_HDF5_VERSION);
53 inline hid_t openFile(const std::string&, FileAccessMode = READ_ONLY, HDF5Version = DEFAULT_HDF5_VERSION);
54 inline void closeFile(const hid_t&);
55 
56 inline hid_t createGroup(const hid_t&, const std::string&);
57 inline hid_t openGroup(const hid_t&, const std::string&);
58 inline void closeGroup(const hid_t&);
59 
60 template<class T>
61  void save(const hid_t&, const std::string&, const Marray<T>&);
62 template<class T, bool isConst>
63  void save(const hid_t&, const std::string&, const View<T, isConst>&);
64 template<class T>
65  void save(const hid_t&, const std::string&, const std::vector<T>&);
66 template<class T, class BaseIterator, class ShapeIterator>
67  void saveHyperslab(const hid_t&, const std::string&,
68  BaseIterator, BaseIterator, ShapeIterator, const Marray<T>&);
69 template<class T, class ShapeIterator>
70  void create(const hid_t&, const std::string&, ShapeIterator,
71  ShapeIterator, CoordinateOrder);
72 
73 template<class T>
74  void load(const hid_t&, const std::string&, Marray<T>&);
75 template<class T>
76  void loadShape(const hid_t&, const std::string&, Vector<T>&);
77 template<class T>
78  void loadVec(const hid_t&, const std::string&, std::vector<T>&);
79 template<class T, class BaseIterator, class ShapeIterator>
80  void loadHyperslab(const hid_t&, const std::string&,
81  BaseIterator, BaseIterator, ShapeIterator, Marray<T>&);
82 
83 // type conversion from C++ to HDF5
84 
85 // \cond suppress doxygen
86 template<class T>
87 inline hid_t uintTypeHelper() {
88  switch(sizeof(T)) {
89  case 1:
90  return H5T_STD_U8LE;
91  case 2:
92  return H5T_STD_U16LE;
93  case 4:
94  return H5T_STD_U32LE;
95  case 8:
96  return H5T_STD_U64LE;
97  default:
98  throw std::runtime_error("No matching HDF5 type.");
99  }
100 }
101 
102 template<class T>
103 inline hid_t intTypeHelper() {
104  switch(sizeof(T)) {
105  case 1:
106  return H5T_STD_I8LE;
107  case 2:
108  return H5T_STD_I16LE;
109  case 4:
110  return H5T_STD_I32LE;
111  case 8:
112  return H5T_STD_I64LE;
113  default:
114  throw std::runtime_error("No matching HDF5 type.");
115  }
116 }
117 
118 template<class T>
119 inline hid_t floatingTypeHelper() {
120  switch(sizeof(T)) {
121  case 4:
122  return H5T_IEEE_F32LE;
123  case 8:
124  return H5T_IEEE_F64LE;
125  default:
126  throw std::runtime_error("No matching HDF5 type.");
127  }
128 }
129 
130 template<class T>
131 inline hid_t hdf5Type();
132 
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>(); }
143 
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>(); }
156 
157 template<> inline hid_t hdf5Type<float>()
158  { return floatingTypeHelper<float>(); }
159 template<> inline hid_t hdf5Type<double>()
160  { return floatingTypeHelper<double>(); }
161 // \endcond
162 
163 // implementation
164 
175 template<class T, class ShapeIterator>
176 void create(
177  const hid_t& groupHandle,
178  const std::string& datasetName,
179  ShapeIterator begin,
180  ShapeIterator end,
181  CoordinateOrder coordinateOrder
182 ) {
183  marray_detail::Assert(MARRAY_NO_ARG_TEST || groupHandle >= 0);
184  HandleCheck<MARRAY_NO_DEBUG> handleCheck;
185 
186  // build dataspace
187  hid_t datatype = H5Tcopy(hdf5Type<T>());
188  size_t dimension = std::distance(begin, end);
189  Vector<hsize_t> shape((size_t)(dimension));
190  if(coordinateOrder == FirstMajorOrder) {
191  // copy shape as is
192  for(size_t j=0; j<dimension; ++j) {
193  shape[j] = hsize_t(*begin);
194  ++begin;
195  }
196  }
197  else {
198  // reverse shape
199  for(size_t j=0; j<dimension; ++j) {
200  shape[dimension-j-1] = hsize_t(*begin);
201  ++begin;
202  }
203  }
204  hid_t dataspace = H5Screate_simple(dimension, &shape[0], NULL);
205  if(dataspace < 0) {
206  H5Tclose(datatype);
207  throw std::runtime_error("Marray cannot create dataspace.");
208  }
209 
210 
211  // create new dataset
212  hid_t dataset = H5Dcreate(groupHandle, datasetName.c_str(), datatype,
213  dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
214  if(dataset < 0) {
215  H5Sclose(dataspace);
216  H5Tclose(datatype);
217  throw std::runtime_error("Marray cannot create dataset.");
218  }
219 
220  // write attribute to indicate whether shape is reversed
221  if(coordinateOrder == LastMajorOrder) {
222  hsize_t attributeShape[1] = {1};
223  hid_t attributeDataspace = H5Screate_simple(1, attributeShape, NULL);
224  if(attributeDataspace < 0) {
225  H5Dclose(dataset);
226  H5Sclose(dataspace);
227  H5Tclose(datatype);
228  throw std::runtime_error("Marray cannot create dataspace.");
229  }
230  hid_t attribute = H5Acreate(dataset, reverseShapeAttributeName,
231  H5T_STD_U8LE, attributeDataspace, H5P_DEFAULT, H5P_DEFAULT);
232  if(attribute < 0) {
233  H5Sclose(attributeDataspace);
234  H5Dclose(dataset);
235  H5Sclose(dataspace);
236  H5Tclose(datatype);
237  throw std::runtime_error("Marray cannot create attribute.");
238  }
239  unsigned int data = 1;
240  herr_t err = H5Awrite(attribute, H5T_STD_U8LE, &data);
241  H5Aclose(attribute);
242  H5Sclose(attributeDataspace);
243  if(err < 0) {
244  H5Dclose(dataset);
245  H5Sclose(dataspace);
246  H5Tclose(datatype);
247  throw std::runtime_error("Marray cannot create write to attribute.");
248  }
249  }
250 
251  // clean up
252  H5Dclose(dataset);
253  H5Sclose(dataspace);
254  H5Tclose(datatype);
255  handleCheck.check();
256 }
257 
266 template<class T>
267 void save(
268  const hid_t& groupHandle,
269  const std::string& datasetName,
270  const Marray<T>& in
271 ) {
272  marray_detail::Assert(MARRAY_NO_ARG_TEST || groupHandle >= 0);
273  HandleCheck<MARRAY_NO_DEBUG> handleCheck;
274 
275  // build dataspace
276  hid_t datatype = H5Tcopy(hdf5Type<T>());
277  Vector<hsize_t> shape(in.dimension());
278  if(in.coordinateOrder() == FirstMajorOrder) {
279  // copy shape as is
280  for(size_t j=0; j<in.dimension(); ++j) {
281  shape[j] = hsize_t(in.shape(j));
282  }
283  }
284  else {
285  // reverse shape
286  for(size_t j=0; j<in.dimension(); ++j) {
287  shape[size_t(in.dimension()-j-1)] = hsize_t(in.shape(j));
288  }
289  }
290  hid_t dataspace = H5Screate_simple(in.dimension(), &shape[0], NULL);
291  if(dataspace < 0) {
292  H5Tclose(datatype);
293  throw std::runtime_error("Marray cannot create dataspace.");
294  }
295 
296  // create new dataset
297  hid_t dataset = H5Dcreate(groupHandle, datasetName.c_str(), datatype,
298  dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
299  if(dataset < 0) {
300  H5Sclose(dataspace);
301  H5Tclose(datatype);
302  throw std::runtime_error("Marray cannot create dataset.");
303  }
304 
305  // write attribute to indicate whether shape is reversed
306  if(in.coordinateOrder() == LastMajorOrder) {
307  hsize_t attributeShape[1] = {1};
308  hid_t attributeDataspace = H5Screate_simple(1, attributeShape, NULL);
309  if(attributeDataspace < 0) {
310  H5Dclose(dataset);
311  H5Sclose(dataspace);
312  H5Tclose(datatype);
313  throw std::runtime_error("Marray cannot create dataspace.");
314  }
315  hid_t attribute = H5Acreate(dataset, reverseShapeAttributeName,
316  H5T_STD_U8LE, attributeDataspace, H5P_DEFAULT, H5P_DEFAULT);
317  if(attribute < 0) {
318  H5Sclose(attributeDataspace);
319  H5Dclose(dataset);
320  H5Sclose(dataspace);
321  H5Tclose(datatype);
322  throw std::runtime_error("Marray cannot create attribute.");
323  }
324  unsigned int data = 1;
325  herr_t err = H5Awrite(attribute, H5T_STD_U8LE, &data);
326  H5Aclose(attribute);
327  H5Sclose(attributeDataspace);
328  if(err < 0) {
329  H5Dclose(dataset);
330  H5Sclose(dataspace);
331  H5Tclose(datatype);
332  throw std::runtime_error("Marray cannot create write to attribute.");
333  }
334  }
335 
336  // write
337  herr_t status = H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL,
338  H5P_DEFAULT, &in(0));
339  H5Dclose(dataset);
340  H5Sclose(dataspace);
341  H5Tclose(datatype);
342  if(status < 0) {
343  throw std::runtime_error("Marray cannot write to dataset.");
344  }
345 
346  handleCheck.check();
347 }
348 
357 template<class T, bool isConst>
358 inline void save(
359  const hid_t& groupHandle,
360  const std::string& datasetName,
361  const View<T, isConst>& in
362 ) {
363  Marray<T> m = in;
364  save(groupHandle, datasetName, m);
365 }
366 
375 template<class T>
376 void save(
377  const hid_t& groupHandle,
378  const std::string& datasetName,
379  const std::vector<T>& in
380 )
381 {
382  marray::Vector<T> v(in.size());
383  for(size_t j=0; j<in.size(); ++j) {
384  v[j] = in[j];
385  }
386  save(groupHandle, datasetName, v);
387 }
388 
397 template<class T>
398 void load(
399  const hid_t& groupHandle,
400  const std::string& datasetName,
401  Marray<T>& out
402 ) {
403  marray_detail::Assert(MARRAY_NO_ARG_TEST || groupHandle >= 0);
404  HandleCheck<MARRAY_NO_DEBUG> handleCheck;
405 
406  hid_t dataset = H5Dopen(groupHandle, datasetName.c_str(), H5P_DEFAULT);
407  if(dataset < 0) {
408  throw std::runtime_error("Marray cannot open dataset.");
409  }
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>())) {
414  H5Dclose(dataset);
415  H5Tclose(nativeType);
416  H5Tclose(type);
417  H5Sclose(filespace);
418  throw std::runtime_error("Data types not equal error.");
419  }
420  int dimension = H5Sget_simple_extent_ndims(filespace);
421  Vector<hsize_t> shape(dimension);
422  herr_t status = H5Sget_simple_extent_dims(filespace, &shape[0], NULL);
423  if(status < 0) {
424  H5Dclose(dataset);
425  H5Tclose(nativeType);
426  H5Tclose(type);
427  H5Sclose(filespace);
428  throw std::runtime_error("H5Sget_simple_extent_dims error.");
429  }
430  hid_t memspace = H5Screate_simple(dimension, &shape[0], NULL);
431 
432  // resize marray
433  marray::Vector<size_t> newShape((size_t)(dimension));
434  for(size_t j=0; j<newShape.size(); ++j) {
435  newShape(j) = (size_t)(shape[j]);
436  }
437  if(H5Aexists(dataset, reverseShapeAttributeName) > 0) {
438  // reverse shape
439  out = Marray<T>(SkipInitialization, newShape.rbegin(),
440  newShape.rend(), LastMajorOrder);
441  }
442  else {
443  // don't reverse shape
444  out = Marray<T>(SkipInitialization, newShape.begin(),
445  newShape.end(), FirstMajorOrder);
446  }
447 
448  // read
449  status = H5Dread(dataset, nativeType, memspace, filespace,
450  H5P_DEFAULT, &out(0));
451  H5Dclose(dataset);
452  H5Tclose(nativeType);
453  H5Tclose(type);
454  H5Sclose(memspace);
455  H5Sclose(filespace);
456  if(status < 0) {
457  throw std::runtime_error("Marray cannot read from dataset.");
458  }
459 
460  handleCheck.check();
461 }
462 
471 template<class T>
473  const hid_t& groupHandle,
474  const std::string& datasetName,
475  Vector<T>& out
476 ) {
477  marray_detail::Assert(MARRAY_NO_ARG_TEST || groupHandle >= 0);
478  HandleCheck<MARRAY_NO_DEBUG> handleCheck;
479 
480  // load shape from HDF5 file
481  hid_t dataset = H5Dopen(groupHandle, datasetName.c_str(), H5P_DEFAULT);
482  if(dataset < 0) {
483  throw std::runtime_error("Marray cannot open dataset.");
484  }
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);
489  if(status < 0) {
490  H5Dclose(dataset);
491  H5Sclose(filespace);
492  delete[] shape;
493  throw std::runtime_error("Marray cannot get extension of dataset.");
494  }
495 
496  // write shape to out
497  out = Vector<T>((size_t)(dimension));
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]);
501  }
502  }
503  else {
504  for(size_t j=0; j<out.size(); ++j) {
505  out[j] = T(shape[j]);
506  }
507  }
508 
509  // clean up
510  delete[] shape;
511  H5Dclose(dataset);
512  H5Sclose(filespace);
513  handleCheck.check();
514 }
515 
524 template<class T>
525 void loadVec(
526  const hid_t& groupHandle,
527  const std::string& datasetName,
528  std::vector<T>& out
529 )
530 {
532  load( groupHandle,datasetName,v);
533  out.resize(v.size());
534  for(size_t j=0; j<v.size(); ++j) {
535  out[j] = v(j);
536  }
537 }
538 
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,
557  Marray<T>& out
558 ) {
559  marray_detail::Assert(MARRAY_NO_ARG_TEST || groupHandle >= 0);
560  HandleCheck<MARRAY_NO_DEBUG> handleCheck;
561 
562  // open dataset
563  hid_t dataset = H5Dopen(groupHandle, datasetName.c_str(), H5P_DEFAULT);
564  if(dataset < 0) {
565  throw std::runtime_error("Marray cannot open dataset.");
566  }
567 
568  // determine shape of hyperslab and array
569  size_t size = std::distance(baseBegin, baseEnd);
570  Vector<hsize_t> offset(size);
571  Vector<hsize_t> slabShape(size);
572  Vector<hsize_t> marrayShape(size);
573  CoordinateOrder coordinateOrder;
574  if(H5Aexists(dataset, reverseShapeAttributeName) > 0) {
575  // reverse base and shape
576  coordinateOrder = LastMajorOrder;
577  size_t j = size-1;
578  size_t k = 0;
579  for(;;) {
580  offset[j] = hsize_t(*baseBegin);
581  slabShape[j] = hsize_t(*shapeBegin);
582  marrayShape[k] = slabShape[j];
583  if(j == 0) {
584  break;
585  }
586  else {
587  ++baseBegin;
588  ++shapeBegin;
589  ++k;
590  --j;
591  }
592  }
593  }
594  else {
595  // don't reverse base and shape
596  coordinateOrder = FirstMajorOrder;
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];
601  ++baseBegin;
602  ++shapeBegin;
603  }
604  }
605 
606  // select dataspace hyperslab
607  hid_t datatype = H5Dget_type(dataset);
608 
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");
611  }
612 
613  hid_t dataspace = H5Dget_space(dataset);
614  herr_t status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET,
615  &offset[0], NULL, &slabShape[0], NULL);
616  if(status < 0) {
617  H5Tclose(datatype);
618  H5Sclose(dataspace);
619  H5Dclose(dataset);
620  throw std::runtime_error("Marray cannot select hyperslab. Check offset and shape!");
621  }
622 
623  // select memspace hyperslab
624  hid_t memspace = H5Screate_simple(int(size), &marrayShape[0], NULL);
625  Vector<hsize_t> offsetOut(size, 0); // no offset
626  status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, &offsetOut[0],
627  NULL, &marrayShape[0], NULL);
628  if(status < 0) {
629  H5Sclose(memspace);
630  H5Tclose(datatype);
631  H5Sclose(dataspace);
632  H5Dclose(dataset);
633  throw std::runtime_error("Marray cannot select hyperslab. Check offset and shape!");
634  }
635 
636  // read from dataspace into memspace
637  out = Marray<T>(SkipInitialization, &marrayShape[0],
638  (&marrayShape[0])+size, coordinateOrder);
639  status = H5Dread(dataset, datatype, memspace, dataspace,
640  H5P_DEFAULT, &(out(0)));
641 
642  // clean up
643  H5Sclose(memspace);
644  H5Tclose(datatype);
645  H5Sclose(dataspace);
646  H5Dclose(dataset);
647  if(status < 0) {
648  throw std::runtime_error("Marray cannot read from dataset.");
649  }
650  handleCheck.check();
651 }
652 
664 template<class T, class BaseIterator, class ShapeIterator>
665 void
667  const hid_t& groupHandle,
668  const std::string& datasetName,
669  BaseIterator baseBegin,
670  BaseIterator baseEnd,
671  ShapeIterator shapeBegin,
672  const Marray<T>& in
673 ) {
674  marray_detail::Assert(MARRAY_NO_ARG_TEST || groupHandle >= 0);
675  HandleCheck<MARRAY_NO_DEBUG> handleCheck;
676 
677  // open dataset
678  hid_t dataset = H5Dopen(groupHandle, datasetName.c_str(), H5P_DEFAULT);
679  if(dataset < 0) {
680  throw std::runtime_error("Marray cannot open dataset.");
681  }
682 
683  // determine hyperslab shape
684  Vector<hsize_t> memoryShape(in.dimension());
685  for(size_t j=0; j<in.dimension(); ++j) {
686  memoryShape[j] = in.shape(j);
687  }
688  size_t size = std::distance(baseBegin, baseEnd);
689  Vector<hsize_t> offset(size);
690  Vector<hsize_t> slabShape(size);
691  bool reverseShapeAttribute =
692  (H5Aexists(dataset, reverseShapeAttributeName) > 0);
693  if(reverseShapeAttribute && in.coordinateOrder() == LastMajorOrder) {
694  // reverse base and shape
695  size_t j = size-1;
696  for(;;) {
697  offset[j] = hsize_t(*baseBegin);
698  slabShape[j] = hsize_t(*shapeBegin);
699  if(j == 0) {
700  break;
701  }
702  else {
703  ++baseBegin;
704  ++shapeBegin;
705  --j;
706  }
707  }
708  }
709  else if(!reverseShapeAttribute && in.coordinateOrder() == FirstMajorOrder) {
710  for(size_t j=0; j<size; ++j) {
711  offset[j] = hsize_t(*baseBegin);
712  slabShape[j] = hsize_t(*shapeBegin);
713  ++baseBegin;
714  ++shapeBegin;
715  }
716  }
717  else {
718  H5Dclose(dataset);
719  throw std::runtime_error("Marray cannot write to HDF5 file. A different order was used when the file was created.");
720  }
721 
722  // select dataspace hyperslab
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);
727  if(status < 0) {
728  H5Tclose(datatype);
729  H5Sclose(dataspace);
730  H5Dclose(dataset);
731  throw std::runtime_error("Marray cannot select hyperslab. Check offset and shape!");
732  }
733 
734  // select memspace hyperslab
735  hid_t memspace = H5Screate_simple(int(in.dimension()), &memoryShape[0], NULL);
736  Vector<hsize_t> memoryOffset(int(in.dimension()), 0); // no offset
737  status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, &memoryOffset[0], NULL,
738  &memoryShape[0], NULL);
739  if(status < 0) {
740  H5Sclose(memspace);
741  H5Tclose(datatype);
742  H5Sclose(dataspace);
743  H5Dclose(dataset);
744  throw std::runtime_error("Marray cannot select hyperslab. Check offset and shape!");
745  }
746 
747  // write from memspace to dataspace
748  status = H5Dwrite(dataset, datatype, memspace, dataspace, H5P_DEFAULT, &(in(0)));
749 
750  // clean up
751  H5Sclose(memspace);
752  H5Tclose(datatype);
753  H5Sclose(dataspace);
754  H5Dclose(dataset);
755  if(status < 0) {
756  throw std::runtime_error("Marray cannot write to dataset.");
757  }
758  handleCheck.check();
759 }
760 
770 inline hid_t
772 (
773  const std::string& filename,
774  HDF5Version hdf5version
775 )
776 {
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);
781  }
782 
783  hid_t fileHandle = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, version);
784  if(fileHandle < 0) {
785  throw std::runtime_error("Could not create HDF5 file: " + filename);
786  }
787 
788  return fileHandle;
789 }
790 
801 inline hid_t
802 openFile
803 (
804  const std::string& filename,
805  FileAccessMode fileAccessMode,
806  HDF5Version hdf5version
807 )
808 {
809  hid_t access = H5F_ACC_RDONLY;
810  if(fileAccessMode == READ_WRITE) {
811  access = H5F_ACC_RDWR;
812  }
813 
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);
818  }
819 
820  hid_t fileHandle = H5Fopen(filename.c_str(), access, version);
821  if(fileHandle < 0) {
822  throw std::runtime_error("Could not open HDF5 file: " + filename);
823  }
824 
825  return fileHandle;
826 }
827 
834 inline void closeFile
835 (
836  const hid_t& handle
837 )
838 {
839  H5Fclose(handle);
840 }
841 
850 inline hid_t
852 (
853  const hid_t& parentHandle,
854  const std::string& groupName
855 )
856 {
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.");
861  }
862  return groupHandle;
863 }
864 
873 inline hid_t
874 openGroup
875 (
876  const hid_t& parentHandle,
877  const std::string& groupName
878 )
879 {
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.");
883  }
884  return groupHandle;
885 }
886 
893 inline void
895 (
896  const hid_t& handle
897 )
898 {
899  H5Gclose(handle);
900 }
901 
902 } // namespace hdf5
903 } // namespace marray
904 
905 #endif // #ifndef MARRAY_HDF5_HXX
906 
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]
Definition: marray_hdf5.hxx:45
Runtime-flexible multi-dimensional views and arrays.
Definition: marray.hxx:20
const size_t shape(const size_t) const
Get the shape in one dimension.
Definition: marray.hxx:1725
reverse_iterator rbegin()
Array-Interface to an interval of memory.
Definition: marray.hxx:44
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.
Definition: marray.hxx:1711
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.
Definition: marray.hxx:60
void loadShape(const hid_t &, const std::string &, Vector< T > &)
Load the shape of an HDF5 dataset.
CoordinateOrder
Definition: marray.hxx:23
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.
reverse_iterator rend()
static const InitializationSkipping SkipInitialization
Flag to indicate initialization skipping.
Definition: marray.hxx:29
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.
Definition: marray.hxx:1815
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.