Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
H5.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: H5.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Minimal HDF5 wrapper
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
37 #include <boost/make_shared.hpp>
38 
39 #ifdef NEKTAR_USE_MPI
41 #endif
42 
43 namespace Nektar
44 {
45 namespace LibUtilities
46 {
47 namespace H5
48 {
49 
50 Object::Object() : m_Id(H5I_INVALID_HID)
51 {
52 }
53 Object::Object(hid_t id) : m_Id(id)
54 {
55 }
57 {
58 }
59 
60 PList::PList() : Object(H5P_DEFAULT)
61 {
62 }
63 PList::PList(hid_t cls) : Object()
64 {
65  H5_CONSTRUCT(m_Id, H5Pcreate, (cls));
66 }
68 {
69  Close();
70 }
72 {
73  H5_CALL(H5Pclose, (m_Id));
74  m_Id = H5I_INVALID_HID;
75 }
76 
77 /// Default options
79 {
80  return PListSharedPtr(new PList());
81 }
82 
83 /// Properties for object creation
85 {
86  return PListSharedPtr(new PList(H5P_OBJECT_CREATE));
87 }
88 
89 /// Properties for file creation
91 {
92  return PListSharedPtr(new PList(H5P_FILE_CREATE));
93 }
94 
95 /// Properties for file access
97 {
98  return PListSharedPtr(new PList(H5P_FILE_ACCESS));
99 }
100 
101 /// Properties for dataset creation
103 {
104  return PListSharedPtr(new PList(H5P_DATASET_CREATE));
105 }
106 
107 /// Properties for dataset access
109 {
110  return PListSharedPtr(new PList(H5P_DATASET_ACCESS));
111 }
112 
113 /// Properties for raw data transfer
115 {
116  return PListSharedPtr(new PList(H5P_DATASET_XFER));
117 }
118 
119 /// Properties for file mounting
121 {
122  return PListSharedPtr(new PList(H5P_FILE_MOUNT));
123 }
124 
125 /// Properties for group creation
127 {
128  return PListSharedPtr(new PList(H5P_GROUP_CREATE));
129 }
130 
131 /// Properties for group access
133 {
134  return PListSharedPtr(new PList(H5P_GROUP_ACCESS));
135 }
136 
137 /// Properties for datatype creation
139 {
140  return PListSharedPtr(new PList(H5P_DATATYPE_CREATE));
141 }
142 
143 /// Properties for datatype access
145 {
146  return PListSharedPtr(new PList(H5P_DATATYPE_ACCESS));
147 }
148 
149 /// Properties for character encoding when encoding strings or object names
151 {
152  return PListSharedPtr(new PList(H5P_STRING_CREATE));
153 }
154 
155 /// Properties for attribute creation
157 {
158  return PListSharedPtr(new PList(H5P_ATTRIBUTE_CREATE));
159 }
160 
161 /// Properties governing the object copying process
163 {
164  return PListSharedPtr(new PList(H5P_OBJECT_COPY));
165 }
166 
167 /// Properties governing link creation
169 {
170  return PListSharedPtr(new PList(H5P_LINK_CREATE));
171 }
172 
173 /// Properties governing link traversal when accessing objects
175 {
176  return PListSharedPtr(new PList(H5P_LINK_ACCESS));
177 }
178 void PList::SetChunk(const std::vector<hsize_t> &dims)
179 {
180  H5_CALL(H5Pset_chunk, (m_Id, dims.size(), &dims[0]));
181 }
182 void PList::SetDeflate(const unsigned level)
183 {
184  H5_CALL(H5Pset_deflate, (m_Id, level));
185 }
186 #ifdef NEKTAR_USE_MPI
188 {
189  H5_CALL(H5Pset_dxpl_mpio, (m_Id, H5FD_MPIO_COLLECTIVE));
190 }
192 {
193  H5_CALL(H5Pset_dxpl_mpio, (m_Id, H5FD_MPIO_INDEPENDENT));
194 }
195 void PList::SetMpio(CommSharedPtr comm)
196 {
197  CommMpiSharedPtr mpi_comm = boost::dynamic_pointer_cast<CommMpi>(comm);
198  ASSERTL0(mpi_comm, "Can't convert communicator to MPI communicator.")
199  // TODO: accept hints
200  MPI_Info info = MPI_INFO_NULL;
201  H5_CALL(H5Pset_fapl_mpio, (m_Id, mpi_comm->GetComm(), info));
202 }
203 #else
205 {
206  ASSERTL0(false, "Trying to use parallel HDF5 without MPI!");
207 }
209 {
210  ASSERTL0(false, "Trying to use parallel HDF5 without MPI!");
211 }
213 {
214  ASSERTL0(false, "Trying to use parallel HDF5 without MPI!");
215 }
216 #endif
217 
219  PListSharedPtr createPL,
220  PListSharedPtr accessPL)
221 {
222  hid_t grp;
223  H5_CONSTRUCT(grp, H5Gcreate2, (m_Id, name.c_str(), H5P_DEFAULT,
224  createPL->GetId(), accessPL->GetId()));
225  GroupSharedPtr ans(new Group(grp));
226  return ans;
227 }
228 
230  DataTypeSharedPtr type,
231  DataSpaceSharedPtr space,
232  PListSharedPtr createPL,
233  PListSharedPtr accessPL)
234 {
235  hid_t ds;
236  H5_CONSTRUCT(ds, H5Dcreate2,
237  (m_Id, name.c_str(), type->GetId(), space->GetId(),
238  H5P_DEFAULT, createPL->GetId(), accessPL->GetId()));
239 
240  DataSetSharedPtr ans(new DataSet(ds));
241  return ans;
242 }
243 
244 // Open an existing group.
245 // The accessPL can be omitted to use the defaults
247  PListSharedPtr accessPL) const
248 {
249  hid_t grp;
250  H5_CONSTRUCT(grp, H5Gopen2, (m_Id, name.c_str(), accessPL->GetId()));
251  GroupSharedPtr ans(new Group(grp));
252  return ans;
253 }
254 
255 // Open an existing dataset
256 // The accessPL can be omitted to use the defaults
258  const std::string &name, PListSharedPtr accessPL) const
259 {
260  hid_t ds;
261  H5_CONSTRUCT(ds, H5Dopen2, (m_Id, name.c_str(), accessPL->GetId()));
262  DataSetSharedPtr ans(new DataSet(ds));
263  return ans;
264 }
265 
267 {
268  // Have to use dynamic because of virtual inheritance
270  boost::dynamic_pointer_cast<CanHaveGroupsDataSets>(shared_from_this());
272 }
273 
275 {
276  // Have to use dynamic because of virtual inheritance
278  boost::dynamic_pointer_cast<CanHaveGroupsDataSets>(shared_from_this());
279  return CanHaveGroupsDataSets::LinkIterator(thisSh, GetNumElements());
280 }
281 
283  CanHaveGroupsDataSetsSharedPtr grp, hsize_t idx)
284  : m_grp(grp), m_idx(-1), m_next(idx), m_size(grp->GetNumElements())
285 {
286  ++*this;
287 }
288 
290 {
291  return m_currentName;
292 }
295 {
296  m_idx = m_next;
297  if (m_idx < m_size)
298  {
299  H5_CALL(H5Literate, (m_grp->GetId(), H5_INDEX_NAME, H5_ITER_NATIVE,
300  &m_next, LinkIterator::helper, this));
301  }
302  return *this;
303 }
305  const CanHaveGroupsDataSets::LinkIterator &other) const
306 {
307  return (m_grp == other.m_grp && m_idx == other.m_idx);
308 }
309 herr_t CanHaveGroupsDataSets::LinkIterator::helper(hid_t g_id, const char *name,
310  const H5L_info_t *info,
311  void *op_data)
312 {
314  static_cast<CanHaveGroupsDataSets::LinkIterator *>(op_data);
315  iter->m_currentName = name;
316  return 1;
317 }
318 
320  DataTypeSharedPtr type,
321  DataSpaceSharedPtr space)
322 {
323  return Attribute::Create(m_Id, name, type, space);
324 }
325 
327 {
328  return Attribute::Open(m_Id, name);
329 }
330 
332 {
333  H5O_info_t info;
334  H5_CALL(H5Oget_info, (m_Id, &info));
335  return info.num_attrs;
336 }
337 
339 {
340  // Have to use dynamic because of virtual inheritance
342  boost::dynamic_pointer_cast<CanHaveAttributes>(shared_from_this());
343  return CanHaveAttributes::AttrIterator(thisSh);
344 }
346 {
347  // Have to use dynamic because of virtual inheritance
349  boost::dynamic_pointer_cast<CanHaveAttributes>(shared_from_this());
350  return CanHaveAttributes::AttrIterator(thisSh, GetNumAttr());
351 }
352 
354  hsize_t idx)
355  : m_obj(obj), m_idx(-1), m_next(idx), m_size(obj->GetNumAttr())
356 {
357  ++*this;
358 }
359 
361 {
362  return m_currentName;
363 }
365 {
366  m_idx = m_next;
367  if (m_next < m_size)
368  {
369  H5_CALL(H5Aiterate2, (m_obj->GetId(), H5_INDEX_CRT_ORDER, H5_ITER_INC,
370  &m_next, AttrIterator::helper, this));
371  }
372  return *this;
373 }
375  const CanHaveAttributes::AttrIterator &other) const
376 {
377  return m_obj == other.m_obj && m_idx == other.m_idx;
378 }
379 
380 herr_t CanHaveAttributes::AttrIterator::helper(hid_t g_id, const char *name,
381  const H5A_info_t *info,
382  void *op_data)
383 {
385  static_cast<CanHaveAttributes::AttrIterator *>(op_data);
386  iter->m_currentName = name;
387  return 1;
388 }
389 
391 {
392  DataSpaceSharedPtr ans(new DataSpace);
393  H5_CONSTRUCT(ans->m_Id, H5Screate, (H5S_NULL));
394  return ans;
395 }
396 
398 {
399  DataSpaceSharedPtr ans(new DataSpace);
400  H5_CONSTRUCT(ans->m_Id, H5Screate, (H5S_SCALAR));
401  return ans;
402 }
404 {
405  DataSpaceSharedPtr ans(new DataSpace);
406  H5_CONSTRUCT(ans->m_Id, H5Screate_simple, (1, &size, NULL));
407  return ans;
408 }
409 
411 {
412 }
413 
415 {
416 }
417 
418 DataSpace::DataSpace(const std::vector<hsize_t> &dims) : Object()
419 {
420  int rank = dims.size();
421  H5_CONSTRUCT(m_Id, H5Screate_simple, (rank, &dims[0], NULL));
422 }
423 
424 DataSpace::DataSpace(const hsize_t size, const hsize_t max) : Object()
425 {
426  const hsize_t *max_p = NULL;
427  if (max != (H5S_UNLIMITED - 1))
428  max_p = &max;
429  H5_CONSTRUCT(m_Id, H5Screate_simple, (1, &size, max_p));
430 }
431 
432 DataSpace::DataSpace(const std::vector<hsize_t> &dims,
433  const std::vector<hsize_t> &max_dims)
434  : Object()
435 {
436  int rank = dims.size();
437  H5_CONSTRUCT(m_Id, H5Screate_simple, (rank, &dims[0], &max_dims[0]));
438 }
439 
441 {
442  Close();
443 }
444 
446 {
447  H5_CALL(H5Sclose, (m_Id));
448  m_Id = H5I_INVALID_HID;
449 }
450 void DataSpace::SelectRange(const hsize_t start, const hsize_t count)
451 {
452  H5_CALL(H5Sselect_hyperslab,
453  (m_Id, H5S_SELECT_SET, &start, NULL, &count, NULL));
454 }
455 
457 {
458  return H5Sget_simple_extent_npoints(m_Id);
459 }
460 
461 DataType::DataType(hid_t id) : Object(id)
462 {
463 }
465 {
467  DataTypeSharedPtr ans = s1->Copy();
468  if (len == 0)
469  {
470  len = H5T_VARIABLE;
471  }
472  H5_CALL(H5Tset_size, (ans->GetId(), len));
473  return ans;
474 }
475 
477 {
478  H5_CALL(H5Tclose, (m_Id));
479  m_Id = H5I_INVALID_HID;
480 }
481 
483 {
484  hid_t ans_id = H5I_INVALID_HID;
485  H5_CONSTRUCT(ans_id, H5Tcopy, (m_Id));
486  DataTypeSharedPtr ans(new DataType(ans_id));
487  return ans;
488 }
489 
491 {
492  return DataTypeSharedPtr(new PredefinedDataType(H5T_C_S1));
493 }
494 
496 {
497 }
498 
500 {
501  // No-op
502  m_Id = H5I_INVALID_HID;
503 }
504 
505 template <> const hid_t DataTypeTraits<char>::NativeType = H5T_NATIVE_CHAR;
506 
507 template <> const hid_t DataTypeTraits<int>::NativeType = H5T_NATIVE_INT;
508 
509 template <>
510 const hid_t DataTypeTraits<unsigned int>::NativeType = H5T_NATIVE_UINT;
511 
512 template <>
513 const hid_t DataTypeTraits<unsigned long>::NativeType = H5T_NATIVE_ULONG;
514 
515 template <>
516 const hid_t DataTypeTraits<unsigned long long>::NativeType = H5T_NATIVE_ULLONG;
517 
518 template <> const hid_t DataTypeTraits<double>::NativeType = H5T_NATIVE_DOUBLE;
519 
520 AttributeSharedPtr Attribute::Create(hid_t parent, const std::string &name,
521  DataTypeSharedPtr type,
522  DataSpaceSharedPtr space)
523 {
524  hid_t id;
525  H5_CONSTRUCT(id, H5Acreate2, (parent, name.c_str(), type->GetId(),
526  space->GetId(), H5P_DEFAULT, H5P_DEFAULT));
527  return AttributeSharedPtr(new Attribute(id));
528 }
529 
530 AttributeSharedPtr Attribute::Open(hid_t parent, const std::string &name)
531 {
532  hid_t id;
533  H5_CONSTRUCT(id, H5Aopen, (parent, name.c_str(), H5P_DEFAULT));
534  return AttributeSharedPtr(new Attribute(id));
535 }
536 
538 {
539  Close();
540 }
542 {
543  H5_CALL(H5Aclose, (m_Id));
544  m_Id = H5I_INVALID_HID;
545 }
546 
548 {
549  return DataSpaceSharedPtr(new DataSpace(H5Aget_space(m_Id)));
550 }
551 
552 File::File(hid_t id) : Object(id)
553 {
554 }
555 FileSharedPtr File::Create(const std::string &filename, unsigned mode,
556  PListSharedPtr createPL, PListSharedPtr accessPL)
557 {
558  hid_t id;
559  H5_CONSTRUCT(id, H5Fcreate, (filename.c_str(), mode, createPL->GetId(),
560  accessPL->GetId()));
561  return FileSharedPtr(new File(id));
562 }
563 FileSharedPtr File::Open(const std::string &filename, unsigned mode,
564  PListSharedPtr accessPL)
565 {
566  hid_t id;
567  H5_CONSTRUCT(id, H5Fopen, (filename.c_str(), mode, accessPL->GetId()));
568  return FileSharedPtr(new File(id));
569 }
570 
572 {
573  Close();
574 }
575 
577 {
578  H5_CALL(H5Fclose, (m_Id));
579  m_Id = H5I_INVALID_HID;
580 }
582 {
583  GroupSharedPtr root = OpenGroup("/");
584  return root->GetNumElements();
585 }
586 Group::Group(hid_t id) : Object(id)
587 {
588 }
589 
591 {
592  Close();
593 }
594 
596 {
597  H5_CALL(H5Gclose, (m_Id));
598  m_Id = H5I_INVALID_HID;
599 }
600 
602 {
603  H5G_info_t info;
604  H5_CALL(H5Gget_info, (m_Id, &info));
605  return info.nlinks;
606 }
607 
608 DataSet::DataSet(hid_t id) : Object(id)
609 {
610 }
611 
613 {
614  Close();
615 }
616 
618 {
619  H5_CALL(H5Dclose, (m_Id));
620  m_Id = H5I_INVALID_HID;
621 }
622 
624 {
625  return DataSpaceSharedPtr(new DataSpace(H5Dget_space(m_Id)));
626 }
627 }
628 }
629 }
#define H5_CALL(func, args)
Definition: H5.h:68
static PListSharedPtr DatatypeCreate()
Properties for datatype creation.
Definition: H5.cpp:138
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
boost::shared_ptr< Group > GroupSharedPtr
Definition: FieldIOHdf5.h:50
static DataTypeSharedPtr CS1()
Definition: H5.cpp:490
static FileSharedPtr Create(const std::string &filename, unsigned mode, PListSharedPtr createPL=PList::Default(), PListSharedPtr accessPL=PList::Default())
Definition: H5.cpp:555
void SelectRange(const hsize_t start, const hsize_t count)
Definition: H5.cpp:450
static AttributeSharedPtr Create(hid_t parent, const std::string &name, DataTypeSharedPtr type, DataSpaceSharedPtr space)
Definition: H5.cpp:520
HDF5 DataSpace wrapper.
Definition: H5.h:304
boost::shared_ptr< DataSet > DataSetSharedPtr
Definition: H5.h:100
#define H5_CONSTRUCT(ans, func, args)
Definition: H5.h:57
AttrIterator(CanHaveAttributesSharedPtr obj, hsize_t idx=0)
Definition: H5.cpp:353
DataSetSharedPtr CreateDataSet(const std::string &name, DataTypeSharedPtr type, DataSpaceSharedPtr space, PListSharedPtr createPL=PList::Default(), PListSharedPtr accessPL=PList::Default())
Definition: H5.cpp:229
static DataTypeSharedPtr String(size_t len=0)
Definition: H5.cpp:464
static PListSharedPtr GroupAccess()
Properties for group access.
Definition: H5.cpp:132
boost::shared_ptr< File > FileSharedPtr
Definition: H5.h:98
DataSetSharedPtr OpenDataSet(const std::string &name, PListSharedPtr accessPL=PList::Default()) const
Definition: H5.cpp:257
Mixin for objects that can have attributes (Group, DataSet, DataType)
Definition: H5.h:253
boost::shared_ptr< DataType > DataTypeSharedPtr
Definition: H5.h:86
boost::shared_ptr< PList > PListSharedPtr
Definition: H5.h:102
static PListSharedPtr DatasetCreate()
Properties for dataset creation.
Definition: H5.cpp:102
static PListSharedPtr GroupCreate()
Properties for group creation.
Definition: H5.cpp:126
Wrap and HDF5 data type object. Technically this can have attributes, but not really bothered...
Definition: H5.h:374
boost::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:55
boost::shared_ptr< DataSpace > DataSpaceSharedPtr
Definition: H5.h:88
static DataSpaceSharedPtr OneD(hsize_t size)
Definition: H5.cpp:403
static FileSharedPtr Open(const std::string &filename, unsigned mode, PListSharedPtr accessPL=PList::Default())
Definition: H5.cpp:563
boost::shared_ptr< Attribute > AttributeSharedPtr
Definition: H5.h:92
boost::shared_ptr< CommMpi > CommMpiSharedPtr
Pointer to a Communicator object.
Definition: CommMpi.h:55
void SetChunk(const std::vector< hsize_t > &dims)
Definition: H5.cpp:178
static PListSharedPtr ObjectCreate()
Properties for object creation.
Definition: H5.cpp:84
DataSpaceSharedPtr GetSpace() const
Definition: H5.cpp:547
Traits class for HDF5 data types.
Definition: H5.h:344
static DataSpaceSharedPtr Null()
Definition: H5.cpp:390
static PListSharedPtr DatasetXfer()
Properties for raw data transfer.
Definition: H5.cpp:114
static PListSharedPtr FileCreate()
Properties for file creation.
Definition: H5.cpp:90
GroupSharedPtr CreateGroup(const std::string &name, PListSharedPtr createPL=PList::Default(), PListSharedPtr accessPL=PList::Default())
Definition: H5.cpp:218
static PListSharedPtr DatatypeAccess()
Properties for datatype access.
Definition: H5.cpp:144
static DataSpaceSharedPtr Scalar()
Definition: H5.cpp:397
Mixin for objects that contain groups and datasets (Group and File)
Definition: H5.h:181
static herr_t helper(hid_t g_id, const char *name, const H5A_info_t *info, void *op_data)
Definition: H5.cpp:380
void SetMpio(CommSharedPtr comm)
Definition: H5.cpp:212
AttributeSharedPtr CreateAttribute(const std::string &name, DataTypeSharedPtr type, DataSpaceSharedPtr space)
Definition: H5.cpp:319
virtual hsize_t GetNumElements()
Definition: H5.cpp:601
HDF5 base class.
Definition: H5.h:106
DataSpaceSharedPtr GetSpace() const
Definition: H5.cpp:623
virtual hsize_t GetNumElements()
Definition: H5.cpp:581
static PListSharedPtr StringCreate()
Properties for character encoding when encoding strings or object names.
Definition: H5.cpp:150
DataTypeSharedPtr Copy() const
Definition: H5.cpp:482
static PListSharedPtr LinkCreate()
Properties governing link creation.
Definition: H5.cpp:168
boost::shared_ptr< CanHaveAttributes > CanHaveAttributesSharedPtr
Definition: H5.h:90
static PListSharedPtr DatasetAccess()
Properties for dataset access.
Definition: H5.cpp:108
static PListSharedPtr FileAccess()
Properties for file access.
Definition: H5.cpp:96
bool operator==(const AttrIterator &other) const
Definition: H5.cpp:374
static PListSharedPtr AttributeCreate()
Properties for attribute creation.
Definition: H5.cpp:156
static PListSharedPtr LinkAccess()
Properties governing link traversal when accessing objects.
Definition: H5.cpp:174
AttributeSharedPtr OpenAttribute(const std::string &name)
Definition: H5.cpp:326
void SetDeflate(const unsigned level=1)
Definition: H5.cpp:182
static PListSharedPtr ObjectCopy()
Properties governing the object copying process.
Definition: H5.cpp:162
HDF5 Group wrapper.
Definition: H5.h:441
static PListSharedPtr Default()
Default options.
Definition: H5.cpp:78
static PListSharedPtr FileMount()
Properties for file mounting.
Definition: H5.cpp:120
GroupSharedPtr OpenGroup(const std::string &name, PListSharedPtr accessPL=PList::Default()) const
Definition: H5.cpp:246
boost::shared_ptr< CanHaveGroupsDataSets > CanHaveGroupsDataSetsSharedPtr
Definition: H5.h:94
static AttributeSharedPtr Open(hid_t parent, const std::string &name)
Definition: H5.cpp:530