Commit 568a1a24 authored by Lukas Riedel's avatar Lukas Riedel 📝

Rework H5File and remove unused headers

* Change signature of 'read_dataset'
* Throw more exceptions
* Update docstrings
parent 7c46f34b
......@@ -309,10 +309,11 @@ private:
H5File file(file_path, _verbose);
// Read the data
file.read_dataset(element_index_map,
cells,
dataset_name,
H5T_NATIVE_INT);
file.read_dataset(dataset_name,
H5T_NATIVE_INT,
element_index_map,
cells
);
// check if cells has correct size and extensions
if (cells.size() != Grid::dimension) {
......
......@@ -5,11 +5,8 @@
#include <cassert>
#include <iostream>
#include <vector>
#include <array>
#include <exception>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <hdf5.h>
namespace Dune {
......@@ -20,8 +17,11 @@ class H5File
{
private:
//! Verbosity of this class
const int _verbose;
//! ID of the H5 file
hid_t _file_id;
//! ID of the currently opened group
hid_t _group_id;
public:
......@@ -79,18 +79,29 @@ public:
}
}
/** Read data set from multi-dim. array in sequential mode from HDF5 file.
/// Read data from a multi-dimensional dataset.
/** Data is read collectively from all parallel processes.
* The output data arrays are resized automatically.
*
* \param local_data is a 'return value' which gets the data that belongs to the current processor (current hyperslab)
* \param local_cells is a container for storing the number of cells in each dimension
* \param dataset_name is the group and name of the dataset to be read
* \param data_type The H5 data type to read.
* \tparam data_t The data type to read the H5 array entries into.
* Must implicitly convert to the data H5 data type.
* \tparam ext_t The data type for array extensions.
* Must implicitly convert to hid_t.
* \param[in] dataset_path The full internal path of the dataset.
* May contain subgroups.
* \param[in] data_type The H5 internal data type of the array to read.
* \param[out] data The vector for reading the data into. Is resized
* automatically.
* \param[out] shape The vector containing the extensions of the dataset.
* This shape is the reverse information of the H5 internal shape.
* It indicated directions x, y[, z].
*/
template<typename VEC, typename CELLVEC>
void read_dataset(VEC& local_data,
CELLVEC& local_cells,
const std::string& dataset_path,
const hid_t data_type)
template<typename data_t, typename ext_t>
void read_dataset(const std::string& dataset_path,
const hid_t data_type,
std::vector<data_t>& data,
std::vector<ext_t>& shape
)
{
if(_verbose > 1) {
std::cout << "Reading H5 parameter field " << dataset_path
......@@ -104,7 +115,9 @@ public:
hid_t dataset_id = H5Dopen(_group_id,
dataset_name.c_str(),
H5P_DEFAULT);
assert(dataset_id > -1);
if (dataset_id < 0) {
throw std::runtime_error("Cannot open dataset " + dataset_path);
}
// get the dataspace
hid_t dataspace_id = H5Dget_space(dataset_id);
......@@ -127,38 +140,44 @@ public:
const std::vector<hsize_t> offset(arr_dim, 0);
// reverse order of dimensions!
local_cells.resize(arr_dim);
std::copy(dims.rbegin(), dims.rend(), local_cells.begin());
shape.resize(arr_dim);
std::copy(dims.rbegin(), dims.rend(), shape.begin());
// create the memory space
hid_t memspace_id = H5Screate_simple(arr_dim, dims.data(), NULL);
//select the hyperslab
status = H5Sselect_hyperslab(memspace_id, H5S_SELECT_SET, offset.data(), NULL, dims.data(), NULL);
assert(status>-1);
assert(status > -1);
//resize the return data
local_data.resize(local_size);
data.resize(local_size);
// set up the collective transfer properties list
hid_t h5_plist_id = H5Pcreate(H5P_DATASET_XFER);
H5Pset_dxpl_mpio(h5_plist_id, H5FD_MPIO_COLLECTIVE);
assert(h5_plist_id > -1);
status = H5Dread(dataset_id
, data_type
, memspace_id
, dataspace_id
, h5_plist_id
, local_data.data()
// read the actual data
status = H5Dread(dataset_id,
data_type,
memspace_id,
dataspace_id,
h5_plist_id,
data.data()
);
assert(status > -1);
if (status < 0) {
throw std::runtime_error("Error reading data from dataset "
+ dataset_path);
}
// close the identifiers
H5Dclose(dataset_id);
H5Sclose(dataspace_id);
H5Sclose(memspace_id);
H5Pclose(h5_plist_id);
status = H5Dclose(dataset_id);
status = H5Sclose(dataspace_id);
status = H5Sclose(memspace_id);
status = H5Pclose(h5_plist_id);
assert(status > -1);
}
private:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment