Skip to content

Commit

Permalink
Merge 1cbab87 into 17d3991
Browse files Browse the repository at this point in the history
  • Loading branch information
gicmo committed Jan 26, 2015
2 parents 17d3991 + 1cbab87 commit 3c824fa
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 326 deletions.
65 changes: 64 additions & 1 deletion include/nix/hdf5/BaseHDF5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,76 @@
#include <nix/hdf5/hdf5include.hpp>
#include <nix/hdf5/DataSpace.hpp>
#include <nix/hdf5/DataTypeHDF5.hpp>
#include <nix/hdf5/DataSetHDF5.hpp>


namespace nix {
namespace hdf5 {


class StringWriter {
public:
typedef std::string value_type;
typedef value_type *pointer;
typedef char *data_type;
typedef data_type *data_ptr;


StringWriter(const NDSize &size, pointer stringdata)
: nelms(size.nelms()), data(stringdata) {
buffer = new data_type[nelms];
}

data_ptr operator*() {
return buffer;
}

void finish() {
for (size_t i = 0; i < nelms; i++) {
data[i] = buffer[i];
}
}

~StringWriter() {
delete[] buffer;
}

private:
size_t nelms;
pointer data;
data_ptr buffer;
};

class StringReader {
public:
typedef const std::string value_type;
typedef value_type *pointer;
typedef const char *data_type;
typedef data_type *data_ptr;


StringReader(const NDSize &size, pointer stringdata)
: nelms(size.nelms()), data(stringdata) {
buffer = new data_type[nelms];
for (size_t i = 0; i < nelms; i++) {
buffer[i] = data[i].c_str();
}
}

data_ptr operator*() {
return buffer;
}

~StringReader() {
delete[] buffer;
}

private:
size_t nelms;
pointer data;
data_ptr buffer;
};


class NIXAPI BaseHDF5 {

protected:
Expand Down
177 changes: 9 additions & 168 deletions include/nix/hdf5/DataSetHDF5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <nix/hdf5/Selection.hpp>
#include <nix/hdf5/DataSpace.hpp>
#include <nix/hdf5/DataTypeHDF5.hpp>
#include <nix/hdf5/BaseHDF5.hpp>
#include <nix/Hydra.hpp>
#include <nix/Value.hpp>

Expand All @@ -21,81 +22,18 @@
namespace nix {
namespace hdf5 {

class StringWriter {
public:
typedef std::string value_type;
typedef value_type *pointer;
typedef char *data_type;
typedef data_type *data_ptr;


StringWriter(const NDSize &size, pointer stringdata)
: nelms(size.nelms()), data(stringdata) {
buffer = new data_type[nelms];
}

data_ptr operator*() {
return buffer;
}

void finish() {
for (size_t i = 0; i < nelms; i++) {
data[i] = buffer[i];
}
}
class NIXAPI DataSet : public BaseHDF5 {

~StringWriter() {
delete[] buffer;
}

private:
size_t nelms;
pointer data;
data_ptr buffer;
};

class StringReader {
public:
typedef const std::string value_type;
typedef value_type *pointer;
typedef const char *data_type;
typedef data_type *data_ptr;


StringReader(const NDSize &size, pointer stringdata)
: nelms(size.nelms()), data(stringdata) {
buffer = new data_type[nelms];
for (size_t i = 0; i < nelms; i++) {
buffer[i] = data[i].c_str();
}
}

data_ptr operator*() {
return buffer;
}

~StringReader() {
delete[] buffer;
}

private:
size_t nelms;
pointer data;
data_ptr buffer;
};


class NIXAPI DataSet {
DataSet() : BaseHDF5() { };

private:
DataSet(hid_t hid);

H5::DataSet h5dset;
DataSet(const DataSet &other);

public:
DataSet() { }
explicit DataSet(H5::DataSet dset);

DataSet& operator=(const DataSet &other) {h5dset = other.h5dset; return *this;}
void read(hid_t memType, void *data) const;
void write(hid_t memType, const void *data);

void read(DataType dtype, const NDSize &size, void *data) const;
void write(DataType dtype, const NDSize &size, const void *data);
Expand All @@ -114,16 +52,6 @@ class NIXAPI DataSet {
template<typename T> void write(const T &value, const Selection &fileSel);
template<typename T> void write(const T &value, const Selection &fileSel, const Selection &memSel);


static DataSet create(const H5::CommonFG &parent, const std::string &name, DataType dtype, const NDSize &size);

template<typename T>
static DataSet create(const H5::CommonFG &parent, const std::string &name, const T &value);

static DataSet create(const H5::CommonFG &parent, const std::string &name, const H5::DataType &fileType,
const NDSize &size, const NDSize &maxsize = {}, const NDSize &chunks = {},
bool maxSizeUnlimited = true, bool guessChunks = true);

static NDSize guessChunking(NDSize dims, DataType dtype);

static NDSize guessChunking(NDSize dims, size_t element_size);
Expand All @@ -132,103 +60,16 @@ class NIXAPI DataSet {
Selection createSelection() const;
NDSize size() const;

void vlenReclaim(DataType mem_type, void *data, H5::DataSpace *dspace = nullptr) const;
void vlenReclaim(H5::DataType mem_type, void *data, H5::DataSpace *dspace = nullptr) const;

static H5::DataType fileTypeForValue(DataType dtype);
static H5::DataType memTypeForValue(DataType dtype);

DataType dataType(void) const;

bool hasAttr(const std::string &name) const;
void removeAttr(const std::string &name) const;

template <typename T>
void setAttr(const std::string &name, const T &value) const;

template <typename T>
bool getAttr(const std::string &name, T &value) const;


private:
static void readAttr(const H5::Attribute &attr, H5::DataType mem_type, const NDSize &size, void *data);
static void readAttr(const H5::Attribute &attr, H5::DataType mem_type, const NDSize &size, std::string *data);

static void writeAttr(const H5::Attribute &attr, H5::DataType mem_type, const NDSize &size, const void *data);
static void writeAttr(const H5::Attribute &attr, H5::DataType mem_type, const NDSize &size, const std::string *data);

hid_t getSpace() const;
};

//template functions

template<typename T>
void DataSet::setAttr(const std::string &name, const T &value) const
{
typedef Hydra<const T> hydra_t;

const hydra_t hydra(value);
DataType dtype = hydra.element_data_type();
NDSize shape = hydra.shape();

H5::Attribute attr;

if (hasAttr(name)) {
attr = h5dset.openAttribute(name);
} else {
H5::DataType fileType = data_type_to_h5_filetype(dtype);
H5::DataSpace fileSpace = DataSpace::create(shape, false);
attr = h5dset.createAttribute(name, fileType, fileSpace);
}

writeAttr(attr, data_type_to_h5_memtype(dtype), shape, hydra.data());
}


template<typename T> bool DataSet::getAttr(const std::string &name, T &value) const
{
if (!hasAttr(name)) {
return false;
}

Hydra<T> hydra(value);

//determine attr's size and resize value accordingly
H5::Attribute attr = h5dset.openAttribute(name);
H5::DataSpace space = attr.getSpace();
int rank = space.getSimpleExtentNdims();
NDSize dims(static_cast<size_t>(rank));
space.getSimpleExtentDims (dims.data(), nullptr);
hydra.resize(dims);

DataType dtype = hydra.element_data_type();
H5::DataType mem_type = data_type_to_h5_memtype(dtype);

readAttr(attr, mem_type, dims, hydra.data());

return true;
}


template<typename T>
DataSet DataSet::create(const H5::CommonFG &parent, const std::string &name, const T &data)
{
const Hydra<const T> hydra(data);

DataType dtype = hydra.element_data_type();
H5::DataType fileType = data_type_to_h5_filetype(dtype);

NDSize shape = hydra.shape();
NDSize maxsize(shape.size(), H5S_UNLIMITED);

NDSize chunks = guessChunking(shape, dtype);
H5::DSetCreatPropList plcreate;
int rank = static_cast<int>(chunks.size());
plcreate.setChunk(rank, chunks.data());

H5::DataSpace space = DataSpace::create(shape, maxsize);
H5::DataSet dset = parent.createDataSet(name, fileType, space, plcreate);
return DataSet(dset);
}



/**
Expand Down
8 changes: 7 additions & 1 deletion include/nix/hdf5/Group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class NIXAPI Group : public BaseHDF5 {
DataSet createData(const std::string &name,
const H5::DataType &fileType,
const H5::DataSpace &fileSpace, const H5::DSetCreatPropList &cpList) const;
DataSet createData(const std::string &name, DataType dtype, const NDSize &size) const;

DataSet createData(const std::string &name, const H5::DataType &fileType,
const NDSize &size, const NDSize &maxsize = {}, const NDSize &chunks = {},
bool maxSizeUnlimited = true, bool guessChunks = true) const;

DataSet openData(const std::string &name) const;
void removeData(const std::string &name);

Expand Down Expand Up @@ -190,7 +196,7 @@ void Group::setData(const std::string &name, const T &value)

DataSet ds;
if (!hasData(name)) {
ds = DataSet::create(h5Group(), name, dtype, shape);
ds = createData(name, dtype, shape);
} else {
ds = openData(name);
ds.setExtent(shape);
Expand Down
7 changes: 4 additions & 3 deletions src/hdf5/DataArrayHDF5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void DataArrayHDF5::polynomCoefficients(const vector<double> &coefficients) {
ds = group().openData("polynom_coefficients");
ds.setExtent({coefficients.size()});
} else {
ds = DataSet::create(group().h5Group(), "polynom_coefficients", coefficients);
ds = group().createData("polynom_coefficients", DataType::Double, {coefficients.size()});
}
ds.write(coefficients);
forceUpdatedAt();
Expand Down Expand Up @@ -260,7 +260,7 @@ void DataArrayHDF5::createData(DataType dtype, const NDSize &size) {
throw new std::runtime_error("DataArray alread exists"); //TODO: FIXME, better exception
}

DataSet::create(group().h5Group(), "data", dtype, size);
group().createData("data", dtype, size); //FIXME: check if this 2-step creation is needed
DataSet ds = group().openData("data");
}

Expand All @@ -272,7 +272,8 @@ void DataArrayHDF5::write(DataType dtype, const void *data, const NDSize &count,
DataSet ds;

if (!group().hasData("data")) {
ds = DataSet::create(group().h5Group(), "data", dtype, count);
//FIXME: this case should actually never be possible, replace with exception?
ds = group().createData("data", dtype, count);
} else {
ds = group().openData("data");
}
Expand Down

0 comments on commit 3c824fa

Please sign in to comment.