LifeV
HDF5IO.cpp
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 #include <lifev/core/filter/HDF5IO.hpp>
37 
38 #ifdef LIFEV_HAS_HDF5
39 #ifdef HAVE_MPI
40 
41 // ===================================================
42 // Constructor
43 // ===================================================
44 
45 LifeV::HDF5IO::HDF5IO (const std::string& fileName, const commPtr_Type& comm,
46  const bool& existing)
47 {
48  openFile (fileName, comm, existing);
49 }
50 
51 // ===================================================
52 // Public Methods
53 // ===================================================
54 
55 void LifeV::HDF5IO::openFile (const std::string& fileName,
56  const commPtr_Type& comm,
57  const bool& existing)
58 {
59  hid_t plistId;
60  MPI_Comm mpiComm = comm->Comm();
61  MPI_Info info = MPI_INFO_NULL;
62 
63  // Set up file access property list with parallel I/O access
64  plistId = H5Pcreate (H5P_FILE_ACCESS);
65  H5Pset_fapl_mpio (plistId, mpiComm, info);
66 
67  // Create/open a file collectively and release property list identifier.
68  if (existing)
69  {
70  M_fileId = H5Fopen (fileName.c_str(), H5F_ACC_RDONLY, plistId);
71  }
72  else
73  {
74  M_fileId = H5Fcreate (fileName.c_str(), H5F_ACC_TRUNC,
75  H5P_DEFAULT, plistId);
76  }
77  H5Pclose (plistId);
78 }
79 
80 void LifeV::HDF5IO::createTable (const std::string& tableName,
81  hid_t& fileDataType,
82  hsize_t tableDimensions[])
83 {
84  tableHandle& currentTable = M_tableList[tableName];
85 
86  currentTable.filespace = H5Screate_simple (2, tableDimensions,
87  tableDimensions);
88 #ifdef H5_USE_16_API
89  currentTable.dataset = H5Dcreate (M_fileId, tableName.c_str(), fileDataType,
90  currentTable.filespace, H5P_DEFAULT);
91 #else
92  currentTable.dataset = H5Dcreate (M_fileId, tableName.c_str(), fileDataType,
93  currentTable.filespace, H5P_DEFAULT,
94  H5P_DEFAULT, H5P_DEFAULT);
95 #endif
96  currentTable.plist = H5Pcreate (H5P_DATASET_XFER);
97  H5Pset_dxpl_mpio (currentTable.plist, H5FD_MPIO_COLLECTIVE);
98 }
99 
100 void LifeV::HDF5IO::openTable (const std::string& tableName,
101  hsize_t tableDimensions[])
102 {
103  tableHandle& currentTable = M_tableList[tableName];
104 
105 #ifdef H5_USE_16_API
106  currentTable.dataset = H5Dopen (M_fileId, tableName.c_str() );
107 #else
108  currentTable.dataset = H5Dopen (M_fileId, tableName.c_str(), H5P_DEFAULT);
109 #endif
110  currentTable.filespace = H5Dget_space (currentTable.dataset);
111  H5Sget_simple_extent_dims (currentTable.filespace, tableDimensions, NULL);
112  currentTable.plist = H5Pcreate (H5P_DATASET_XFER);
113  H5Pset_dxpl_mpio (currentTable.plist, H5FD_MPIO_COLLECTIVE);
114 }
115 
116 void LifeV::HDF5IO::write (const std::string& tableName,
117  hid_t& memDataType, hsize_t currentCount[],
118  hsize_t currentOffset[], void* buffer)
119 {
120  tableHandle& currentTable = M_tableList[tableName];
121 
122  hid_t memspace = H5Screate_simple (2, currentCount, currentCount);
123 
124  H5Sselect_hyperslab (currentTable.filespace, H5S_SELECT_SET, currentOffset,
125  NULL, currentCount, NULL);
126  H5Dwrite (currentTable.dataset, memDataType, memspace,
127  currentTable.filespace, currentTable.plist, buffer);
128 
129  H5Sclose (memspace);
130 }
131 
132 void LifeV::HDF5IO::read (const std::string& tableName,
133  hid_t& memDataType, hsize_t currentCount[],
134  hsize_t currentOffset[], void* buffer)
135 {
136  tableHandle& currentTable = M_tableList[tableName];
137 
138  hid_t memspace = H5Screate_simple (2, currentCount, currentCount);
139 
140  H5Sselect_hyperslab (currentTable.filespace, H5S_SELECT_SET, currentOffset,
141  NULL, currentCount, NULL);
142  H5Dread (currentTable.dataset, memDataType, memspace,
143  currentTable.filespace, currentTable.plist,
144  buffer);
145 
146  H5Sclose (memspace);
147 }
148 
149 void LifeV::HDF5IO::closeTable (const std::string& tableName)
150 {
151  tableHandle& currentTable = M_tableList[tableName];
152  H5Sclose (currentTable.filespace);
153  H5Dclose (currentTable.dataset);
154  H5Pclose (currentTable.plist);
155  M_tableList.erase (tableName);
156 }
157 
158 void LifeV::HDF5IO::closeFile()
159 {
160  H5Fclose (M_fileId);
161 }
162 
163 #endif /* HAVE_MPI */
164 #endif /* LIFEV_HAS_HDF5 */