LifeV
HDF5IO.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5 Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6 Copyright (C) 2010, 2011, 2012 EPFL, Politecnico di Milano, Emory University
7 
8 This file is part of LifeV.
9 
10 LifeV is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14 
15 LifeV is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief Convenience wrapper for the C interface of the HDF5 library
30 
31  @date 8-06-2012
32  @author Radu Popescu <radu.popescu@epfl.ch>
33  @maintainer Radu Popescu <radu.popescu@epfl.ch>
34 */
35 
36 #ifndef HDF5_IO_H_
37 #define HDF5_IO_H_
38 
39 #include <lifev/core/LifeV.hpp>
40 
41 
42 #include <Epetra_config.h>
43 
44 #include <map>
45 #include <string>
46 
47 #ifdef LIFEV_HAS_HDF5
48 #ifdef HAVE_MPI
49 
50 #include <hdf5.h>
51 
52 #include <Epetra_MpiComm.h>
53 
54 
55 namespace LifeV
56 {
57 
58 /*!
59  @brief Convenience wrapper for the C interface of the HDF5 library
60  @author Radu Popescu <radu.popescu@epfl.ch>
61 
62  This class provides an easy way to write and read data from an HDF5 container.
63  It is designed to handle a single open file at one time, with multiple open
64  datasets simultaneously.
65  It does not make any assumptions about data types, or dataset dimensions.
66  It is a very thin wrapper on top of the HDF5 library and it is designed for
67  high performance parallel operations.
68 
69  Usage:
70  - open (or create) a file with HDF5IO::openFile
71  - create or open existing data table with HDF5IO::createTable or
72  HDF5IO::openTable (you can have multiple open tables at one time)
73  - read or write blocks of data with HDF5IO::read or HDF5IO::write
74  - close the tables after you are finished, with HDF5::closeTable
75  - close the file: HDF5::closeFile
76 */
77 class HDF5IO
78 {
79 public:
80  //! @name Public Types
81  //@{
82  typedef std::shared_ptr<Epetra_MpiComm> commPtr_Type;
83  //@}
84 
85  //! @name Constructors and Destructor
86  //@{
87  //! Default empty constructor
88  HDF5IO() {}
89 
90  //! Constructor
91  /*!
92  * Constructor
93  * \param fileName the name of the HDF5 file to be used
94  * \param comm pointer to Epetra_Comm
95  * \param existing boolean flag indicating whether the file exists already
96  * or not. If it exists, data is appended
97  */
98  HDF5IO (const std::string& fileName, const commPtr_Type& comm,
99  const bool& existing = false);
100 
101  //! Empty destructor
102  virtual ~HDF5IO() {}
103  //@}
104 
105  //! @name Public Methods
106  //@{
107  //! Open
108  /*!
109  * Create a file or open an existing file
110  * \param fileName the name of the HDF5 file to be used
111  * \param comm pointer to Epetra_Comm
112  * \param existing boolean flag indicating whether the file exists already
113  * or not. If it exists, data is appended
114  */
115  void openFile (const std::string& fileName, const commPtr_Type& comm,
116  const bool& existing);
117  //! Create a new table
118  /*!
119  * Create a new table in the open file
120  * \param tableName a string containing the table name
121  * \param fileDataType data type that is to be used in the HDF5 container
122  * should be a standard HDF5 type, not a machine native type;
123  * consult HDF5 documentation for more information
124  * \param tableDimensions array of hsize_t of size 2 which holds the
125  * dimensions of the table
126  */
127  void createTable (const std::string& tableName, hid_t& fileDataType,
128  hsize_t tableDimensions[]);
129  //! Open a new table
130  /*!
131  * Open a new table in the open file
132  * \param tableName a string containing the table name
133  * \param tableDimensions array of hsize_t of size 2 which will hold the
134  * dimensions of the table (output parameter)
135  */
136  void openTable (const std::string& tableName, hsize_t tableDimensions[]);
137  //! Write
138  /*!
139  * \param tableName a string containing the table name
140  * \param memDataType the type (described as an HDF5 machine native type)
141  * of the data in the buffer that is to be written
142  * \param currentCount an array of hsize_t of size two describing the shape
143  * of the block to be written (see HDF5 documentation)
144  * \param currentOffset an array of hsize_t of size two describing the
145  * stride of the block to be written (see HDF5 documentation)
146  * \param buffer pointer to a memory region containing the data to be
147  * written
148  */
149  void write (const std::string& tableName,
150  hid_t& memDataType, hsize_t currentCount[],
151  hsize_t currentOffset[], void* buffer);
152  //! Write
153  /*!
154  * \param tableName a string containing the table name
155  * \param memDataType the type (described as an HDF5 machine native type)
156  * of the data in the destination buffer
157  * \param currentCount an array of hsize_t of size two describing the shape
158  * of the block to be read (see HDF5 documentation)
159  * \param currentOffset an array of hsize_t of size two describing the
160  * stride of the block to be read (see HDF5 documentation)
161  * \param buffer pointer to a memory region that represents the destination
162  * of the read operation
163  */
164  void read (const std::string& tableName,
165  hid_t& memDataType, hsize_t currentCount[],
166  hsize_t currentOffset[], void* buffer);
167  //! Write
168  /*!
169  * \param tableName a string containing the table name
170  */
171  void closeTable (const std::string& tableName);
172  //! Close an open file
173  /*!
174  * Call this when finished operating with a file
175  */
176  void closeFile();
177  //@}
178 
179 private:
180  // typedef for internal use
181  typedef struct
182  {
183  hid_t filespace;
184  hid_t dataset;
185  hid_t plist;
186  } tableHandle;
187 
188  // Copy constructor and assignment operator are disabled
189  HDF5IO (const HDF5IO&);
190  HDF5IO& operator= (const HDF5IO&);
191 
192  //! Private Data Members
193  //@{
194  // HDF5 handles
195  std::map<std::string, tableHandle> M_tableList;
196  hid_t M_fileId;
197  //@}
198 }; // class HDF5IO
199 
200 } /* namespace LifeV */
201 
202 #endif /* HAVE_MPI */
203 #endif /* LIFEV_HAS_HDF5 */
204 
205 #endif /* HDF5_IO_H_ */