Skip to content
Snippets Groups Projects
Commit 27e7c0c1 authored by Elias Pipping's avatar Elias Pipping
Browse files

HDF5: Proper files names, namespace

parent d4ec6925
Branches
Tags
No related merge requests found
install(FILES
attributes.hh
file.hh
frombuffer.hh
hdf5-attributes.hh
hdf5-sequence-io.hh
hdf5-singleton-writer.hh
hdf5file.hh
hdf5typetraits.hh
sequenceio.hh
singletonwriter.hh
tobuffer.hh
typetraits.hh
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/dune/fufem/hdf5)
#ifndef DUNE_FUFEM_HDF5_HDF5_ATTRIBUTES_HH
#define DUNE_FUFEM_HDF5_HDF5_ATTRIBUTES_HH
#ifndef DUNE_FUFEM_HDF5_ATTRIBUTES_HH
#define DUNE_FUFEM_HDF5_ATTRIBUTES_HH
#include <string>
#include <hdf5.h>
#include "hdf5file.hh"
#include "file.hh"
class HDF5Attribute {
namespace HDF5 {
class Attribute {
public:
HDF5Attribute(HDF5Grouplike &grouplike, std::string key)
Attribute(Grouplike &grouplike, std::string key)
: grouplike_(grouplike), key_(key) {}
bool exists() { return H5Aexists(grouplike_.c_obj(), key_.c_str()); }
......@@ -56,7 +57,8 @@ public:
}
private:
HDF5Grouplike &grouplike_;
Grouplike &grouplike_;
std::string key_;
};
}
#endif
#ifndef DUNE_FUFEM_HDF5_HDF5FILE_HH
#define DUNE_FUFEM_HDF5_HDF5FILE_HH
#ifndef DUNE_FUFEM_HDF5_FILE_HH
#define DUNE_FUFEM_HDF5_FILE_HH
#include <sys/stat.h>
#include <string>
#include <hdf5.h>
class HDF5Grouplike {
#include <dune/common/exceptions.hh>
namespace HDF5 {
class Grouplike {
public:
bool hasDataset(std::string name) {
return H5Lexists(c_obj(), name.c_str(), H5P_DEFAULT);
......@@ -25,9 +28,9 @@ public:
virtual hid_t c_obj() = 0;
};
class HDF5File : public HDF5Grouplike {
class File : public Grouplike {
public:
HDF5File(std::string filename, bool swmr = false) {
File(std::string filename, bool swmr = false) {
struct stat buffer;
/* Use the latest HDF5 format.
......@@ -60,13 +63,11 @@ public:
void flush() { H5Fflush(file_, H5F_SCOPE_LOCAL); }
~HDF5File() { H5Fclose(file_); }
~File() { H5Fclose(file_); }
hid_t c_obj() override { return file_; }
bool swmrEnabled() {
return swmrEnabled_;
}
bool swmrEnabled() { return swmrEnabled_; }
void swmrEnable() {
if (swmrEnabled_)
return;
......@@ -86,21 +87,22 @@ private:
bool swmrEnabled_ = false;
};
class HDF5Group : public HDF5Grouplike {
class Group : public Grouplike {
public:
HDF5Group(HDF5Grouplike &parent, std::string groupname) : parent_(parent) {
Group(Grouplike &parent, std::string groupname) : parent_(parent) {
group_ = H5Lexists(parent_.c_obj(), groupname.c_str(), H5P_DEFAULT)
? H5Gopen(parent_.c_obj(), groupname.c_str(), H5P_DEFAULT)
: H5Gcreate(parent_.c_obj(), groupname.c_str(), H5P_DEFAULT,
H5P_DEFAULT, H5P_DEFAULT);
}
~HDF5Group() { H5Gclose(group_); }
~Group() { H5Gclose(group_); }
hid_t c_obj() override { return group_; }
private:
HDF5Grouplike &parent_;
Grouplike &parent_;
hid_t group_;
};
}
#endif
#ifndef DUNE_FUFEM_HDF5_HDF5_SEQUENCE_IO_HH
#define DUNE_FUFEM_HDF5_HDF5_SEQUENCE_IO_HH
#ifndef DUNE_FUFEM_HDF5_SEQUENCEIO_HH
#define DUNE_FUFEM_HDF5_SEQUENCEIO_HH
#include <numeric>
......@@ -10,19 +10,20 @@
#include <dune/istl/bvector.hh>
#include <dune/istl/matrix.hh>
#include "hdf5file.hh"
#include "hdf5typetraits.hh"
#include "file.hh"
#include "frombuffer.hh"
#include "tobuffer.hh"
#include "typetraits.hh"
namespace HDF5 {
template <int spatialDimensions, typename ctype = double, typename T = hsize_t>
class HDF5SequenceIO {
class SequenceIO {
int static const dimensions = 1 + spatialDimensions;
public:
template <typename... Args>
HDF5SequenceIO(HDF5Grouplike &file, std::string datasetname, Args... args)
: file_(file), capacity_{ { T(args)... } } {
SequenceIO(Grouplike &file, std::string datasetname, Args... args)
: file_(file), capacity_{{T(args)...}} {
static_assert(sizeof...(args) == spatialDimensions,
"wrong number of arguments");
......@@ -43,14 +44,14 @@ public:
H5Pset_chunk(plist, dimensions, chunk_dims.data());
dset_ = file_.createDataset(datasetname, file_space, plist,
HDF5TypeTraits<ctype>::getType());
TypeTraits<ctype>::getType());
H5Pclose(plist);
H5Sclose(file_space);
}
}
~HDF5SequenceIO() { H5Dclose(dset_); }
~SequenceIO() { H5Dclose(dset_); }
void add(size_t timeStep, std::vector<ctype> const &buffer) {
if (buffer.size() != entrySize())
......@@ -71,7 +72,7 @@ public:
if (buffer.size() != entrySize())
DUNE_THROW(Dune::Exception, "buffer size incorrect");
H5DOappend(dset_, H5P_DEFAULT, skip, 1, HDF5TypeTraits<ctype>::getType(),
H5DOappend(dset_, H5P_DEFAULT, skip, 1, TypeTraits<ctype>::getType(),
buffer.data());
}
#endif
......@@ -93,7 +94,7 @@ public:
count.data(), NULL);
hid_t mem_space = H5Screate_simple(dimensions, count.data(), NULL);
H5Dread(dset_, HDF5TypeTraits<ctype>::getType(), mem_space, file_space,
H5Dread(dset_, TypeTraits<ctype>::getType(), mem_space, file_space,
H5P_DEFAULT, buffer.data());
H5Sclose(mem_space);
......@@ -147,7 +148,7 @@ private:
}
void checkDatatype() {
if (!H5Tequal(H5Dget_type(dset_), HDF5TypeTraits<ctype>::getType()))
if (!H5Tequal(H5Dget_type(dset_), TypeTraits<ctype>::getType()))
DUNE_THROW(Dune::Exception, "unexpected data type");
}
......@@ -165,15 +166,15 @@ private:
return ret;
}
HDF5Grouplike &file_;
Grouplike &file_;
std::array<T, spatialDimensions> const capacity_;
hid_t dset_;
};
template <int spatialDimensions, typename ctype, typename T, class Data>
void addEntry(HDF5SequenceIO<spatialDimensions, ctype, T> &writer,
size_t timeStep, Data const &data) {
void addEntry(SequenceIO<spatialDimensions, ctype, T> &writer, size_t timeStep,
Data const &data) {
std::vector<ctype> buffer;
toBuffer(data, buffer);
writer.add(timeStep, buffer);
......@@ -181,7 +182,7 @@ void addEntry(HDF5SequenceIO<spatialDimensions, ctype, T> &writer,
#if H5_VERSION_GE(1, 10, 0)
template <int spatialDimensions, typename ctype, typename T, class Data>
void appendEntry(HDF5SequenceIO<spatialDimensions, ctype, T> &writer,
void appendEntry(SequenceIO<spatialDimensions, ctype, T> &writer,
Data const &data) {
std::vector<ctype> buffer;
toBuffer(data, buffer);
......@@ -190,11 +191,12 @@ void appendEntry(HDF5SequenceIO<spatialDimensions, ctype, T> &writer,
#endif
template <int spatialDimensions, typename ctype, typename T, class Data>
void readEntry(HDF5SequenceIO<spatialDimensions, ctype, T> &reader,
size_t timeStep, Data &data) {
void readEntry(SequenceIO<spatialDimensions, ctype, T> &reader, size_t timeStep,
Data &data) {
std::vector<ctype> buffer;
std::array<T, spatialDimensions> dimensions;
reader.read(timeStep, buffer, dimensions);
fromBuffer(buffer, dimensions, data);
}
}
#endif
#ifndef DUNE_FUFEM_HDF5_HDF5_SINGLETON_WRITER_HH
#define DUNE_FUFEM_HDF5_HDF5_SINGLETON_WRITER_HH
#ifndef DUNE_FUFEM_HDF5_SINGLETONWRITER_HH
#define DUNE_FUFEM_HDF5_SINGLETONWRITER_HH
#include <numeric>
......@@ -9,20 +9,20 @@
#include <dune/istl/bvector.hh>
#include <dune/istl/matrix.hh>
#include "hdf5file.hh"
#include "hdf5typetraits.hh"
#include "file.hh"
#include "tobuffer.hh"
#include "typetraits.hh"
namespace HDF5 {
template <int spatialDimensions, typename ctype = double, typename T = hsize_t>
class HDF5SingletonWriter {
class SingletonWriter {
private:
int static const dimensions = spatialDimensions;
public:
template <typename... Args>
HDF5SingletonWriter(HDF5Grouplike &file, std::string datasetname,
Args... args)
: file_(file), capacity_{ { T(args)... } } {
SingletonWriter(Grouplike &file, std::string datasetname, Args... args)
: file_(file), capacity_{{T(args)...}} {
static_assert(sizeof...(args) == spatialDimensions,
"wrong number of arguments");
......@@ -40,14 +40,14 @@ public:
hid_t plist = H5Pcreate(H5P_DATASET_CREATE);
H5Pset_layout(plist, H5D_CONTIGUOUS);
dset_ = file_.createDataset(datasetname, file_space, plist,
HDF5TypeTraits<ctype>::getType());
TypeTraits<ctype>::getType());
H5Pclose(plist);
H5Sclose(file_space);
}
}
~HDF5SingletonWriter() { H5Dclose(dset_); }
~SingletonWriter() { H5Dclose(dset_); }
void set(std::vector<ctype> const &buffer) {
if (buffer.size() != entrySize())
......@@ -60,7 +60,7 @@ public:
count.data(), NULL);
hid_t mem_space = H5Screate_simple(dimensions, count.data(), NULL);
H5Dwrite(dset_, HDF5TypeTraits<ctype>::getType(), mem_space, file_space,
H5Dwrite(dset_, TypeTraits<ctype>::getType(), mem_space, file_space,
H5P_DEFAULT, buffer.data());
H5Sclose(mem_space);
H5Sclose(file_space);
......@@ -95,11 +95,10 @@ private:
}
void checkDatatype() {
if (!H5Tequal(H5Dget_type(dset_), HDF5TypeTraits<ctype>::getType()))
if (!H5Tequal(H5Dget_type(dset_), TypeTraits<ctype>::getType()))
DUNE_THROW(Dune::Exception, "unexpected data type");
}
std::array<T, dimensions> minExtent() {
std::array<T, dimensions> ret;
std::fill(ret.begin(), ret.end(), 0);
......@@ -108,17 +107,18 @@ private:
std::array<T, dimensions> maxExtent() { return capacity_; }
HDF5Grouplike &file_;
Grouplike &file_;
std::array<T, spatialDimensions> const capacity_;
hid_t dset_;
};
template <int spatialDimensions, typename ctype, typename T, class Data>
void setEntry(HDF5SingletonWriter<spatialDimensions, ctype, T> &writer,
void setEntry(SingletonWriter<spatialDimensions, ctype, T> &writer,
Data const &data) {
std::vector<ctype> buffer;
toBuffer(data, buffer);
writer.set(buffer);
}
}
#endif
#ifndef DUNE_FUFEM_HDF5_HDF5TYPETRAITS_HH
#define DUNE_FUFEM_HDF5_HDF5TYPETRAITS_HH
#ifndef DUNE_FUFEM_HDF5_TYPETRAITS_HH
#define DUNE_FUFEM_HDF5_TYPETRAITS_HH
template <typename ctype> struct HDF5TypeTraits {
namespace HDF5 {
template <typename ctype>
struct TypeTraits {
static hid_t getType() {
DUNE_THROW(Dune::Exception, "Not a recognised type.");
}
};
template <> struct HDF5TypeTraits<float> {
template <>
struct TypeTraits<float> {
static hid_t getType() { return H5T_NATIVE_FLOAT; }
};
template <> struct HDF5TypeTraits<double> {
template <>
struct TypeTraits<double> {
static hid_t getType() { return H5T_NATIVE_DOUBLE; }
};
template <> struct HDF5TypeTraits<long double> {
template <>
struct TypeTraits<long double> {
static hid_t getType() { return H5T_NATIVE_LDOUBLE; }
};
template <> struct HDF5TypeTraits<char> {
template <>
struct TypeTraits<char> {
static hid_t getType() { return H5T_NATIVE_CHAR; }
};
template <> struct HDF5TypeTraits<signed char> {
template <>
struct TypeTraits<signed char> {
static hid_t getType() { return H5T_NATIVE_SCHAR; }
};
template <> struct HDF5TypeTraits<unsigned char> {
template <>
struct TypeTraits<unsigned char> {
static hid_t getType() { return H5T_NATIVE_UCHAR; }
};
template <> struct HDF5TypeTraits<short> {
template <>
struct TypeTraits<short> {
static hid_t getType() { return H5T_NATIVE_SHORT; }
};
template <> struct HDF5TypeTraits<unsigned short> {
template <>
struct TypeTraits<unsigned short> {
static hid_t getType() { return H5T_NATIVE_USHORT; }
};
template <> struct HDF5TypeTraits<int> {
template <>
struct TypeTraits<int> {
static hid_t getType() { return H5T_NATIVE_INT; }
};
template <> struct HDF5TypeTraits<unsigned int> {
template <>
struct TypeTraits<unsigned int> {
static hid_t getType() { return H5T_NATIVE_UINT; }
};
template <> struct HDF5TypeTraits<long> {
template <>
struct TypeTraits<long> {
static hid_t getType() { return H5T_NATIVE_LONG; }
};
template <> struct HDF5TypeTraits<unsigned long> {
template <>
struct TypeTraits<unsigned long> {
static hid_t getType() { return H5T_NATIVE_ULONG; }
};
template <> struct HDF5TypeTraits<long long> {
template <>
struct TypeTraits<long long> {
static hid_t getType() { return H5T_NATIVE_LLONG; }
};
template <> struct HDF5TypeTraits<unsigned long long> {
template <>
struct TypeTraits<unsigned long long> {
static hid_t getType() { return H5T_NATIVE_ULLONG; }
};
}
#endif
......@@ -7,9 +7,9 @@
#include <dune/istl/bvector.hh>
#include <dune/fufem/hdf5/hdf5file.hh>
#include <dune/fufem/hdf5/hdf5-sequence-io.hh>
#include <dune/fufem/hdf5/hdf5-singleton-writer.hh>
#include <dune/fufem/hdf5/file.hh>
#include <dune/fufem/hdf5/sequenceio.hh>
#include <dune/fufem/hdf5/singletonwriter.hh>
void deleteIfNecessary(std::string filename) {
struct stat buffer;
......@@ -19,16 +19,14 @@ void deleteIfNecessary(std::string filename) {
}
}
template <class Vector>
void checkAddSequence(Vector const &written0, Vector const &written1) {
using ft = typename Vector::block_type::field_type;
std::string const filename("out-sequence.h5");
{
deleteIfNecessary(filename);
HDF5File file(filename.c_str());
HDF5SequenceIO<2, ft> h5writer(file, "data", 2, 3);
HDF5::File file(filename.c_str());
HDF5::SequenceIO<2, ft> h5writer(file, "data", 2, 3);
addEntry(h5writer, 1, written1);
addEntry(h5writer, 0, written0);
}
......@@ -39,8 +37,8 @@ void checkAddSequence(Vector const &written0, Vector const &written1) {
Vector read0;
Vector read1;
{
HDF5File file(filename.c_str());
HDF5SequenceIO<2, ft> h5reader(file, "data", 2, 3);
HDF5::File file(filename.c_str());
HDF5::SequenceIO<2, ft> h5reader(file, "data", 2, 3);
readEntry(h5reader, 0, read0);
readEntry(h5reader, 1, read1);
}
......@@ -61,8 +59,8 @@ void checkAppendSequence(Vector const &written0, Vector const &written1) {
std::string const filename("out-sequence.h5");
{
deleteIfNecessary(filename);
HDF5File file(filename.c_str());
HDF5SequenceIO<2, ft> h5writer(file, "data", 2, 3);
HDF5::File file(filename.c_str());
HDF5::SequenceIO<2, ft> h5writer(file, "data", 2, 3);
appendEntry(h5writer, written0);
appendEntry(h5writer, written1);
}
......@@ -73,8 +71,8 @@ void checkAppendSequence(Vector const &written0, Vector const &written1) {
Vector read0;
Vector read1;
{
HDF5File file(filename.c_str());
HDF5SequenceIO<2, ft> h5reader(file, "data", 2, 3);
HDF5::File file(filename.c_str());
HDF5::SequenceIO<2, ft> h5reader(file, "data", 2, 3);
readEntry(h5reader, 0, read0);
readEntry(h5reader, 1, read1);
}
......@@ -95,8 +93,8 @@ void checkSetSingleton(Vector const &written0) {
std::string const filename = "out-singleton.h5";
{
deleteIfNecessary(filename);
HDF5File file(filename);
HDF5SingletonWriter<2, ft> h5writer(file, "data", 2, 3);
HDF5::File file(filename);
HDF5::SingletonWriter<2, ft> h5writer(file, "data", 2, 3);
setEntry(h5writer, written0);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment