-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for `xt::xtensor`, both row and column major. We're missing `xt::xarray` and views.
- Loading branch information
Showing
4 changed files
with
205 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#pragma once | ||
|
||
#include "bits/H5Inspector_decl.hpp" | ||
#include "H5Exception.hpp" | ||
|
||
#include <xtensor/xtensor.hpp> | ||
#include <xtensor/xarray.hpp> | ||
#include <xtensor/xadapt.hpp> | ||
|
||
namespace HighFive { | ||
namespace details { | ||
|
||
template <class XTensorType, bool IsConstExprRowMajor> | ||
struct xtensor_inspector { | ||
using type = XTensorType; | ||
using value_type = typename type::value_type; | ||
using base_type = typename inspector<value_type>::base_type; | ||
using hdf5_type = base_type; | ||
|
||
// TODO prevent non-trivial elements. | ||
|
||
static constexpr size_t ndim = type::rank; | ||
static constexpr size_t recursive_ndim = ndim + inspector<value_type>::recursive_ndim; | ||
static constexpr bool is_trivially_copyable = IsConstExprRowMajor && | ||
std::is_trivially_copyable<value_type>::value && | ||
inspector<value_type>::is_trivially_copyable; | ||
|
||
static std::vector<size_t> getDimensions(const type& val) { | ||
std::array<size_t, ndim> shape = val.shape(); | ||
std::vector<size_t> sizes(shape.begin(), shape.end()); | ||
return sizes; | ||
} | ||
|
||
static void prepare(type& val, const std::vector<size_t>& dims) { | ||
auto shape = std::array<size_t, ndim>{}; | ||
std::copy(dims.begin(), dims.begin() + ndim, shape.begin()); | ||
val.resize(shape); | ||
} | ||
|
||
static hdf5_type* data(type& val) { | ||
if (!is_trivially_copyable) { | ||
throw DataSetException("Invalid used of `inspector<xtensor>::data`."); | ||
} | ||
|
||
if (val.size() == 0) { | ||
throw DataSetException("Invalid use of `inspector<xtensor>::data` for empty array."); | ||
} | ||
|
||
return inspector<value_type>::data(*val.data()); | ||
} | ||
|
||
static const hdf5_type* data(const type& val) { | ||
if (!is_trivially_copyable) { | ||
throw DataSetException("Invalid used of `inspector<xtensor>::data`."); | ||
} | ||
|
||
if (val.size() == 0) { | ||
throw DataSetException("Invalid use of `inspector<xtensor>::data` for empty array."); | ||
} | ||
|
||
return inspector<value_type>::data(*val.data()); | ||
} | ||
|
||
static void serialize(const type& val, const std::vector<size_t>& dims, hdf5_type* m) { | ||
auto shape = std::array<size_t, ndim>{}; | ||
std::copy(dims.begin(), dims.begin() + ndim, shape.begin()); | ||
size_t size = compute_total_size(dims); | ||
xt::adapt(m, size, xt::no_ownership(), shape) = val; | ||
} | ||
|
||
static void unserialize(const hdf5_type* vec_align, | ||
const std::vector<size_t>& dims, | ||
type& val) { | ||
std::array<size_t, ndim> shape; | ||
std::copy(dims.begin(), dims.begin() + ndim, shape.begin()); | ||
size_t size = compute_total_size(dims); | ||
val = xt::adapt(vec_align, size, xt::no_ownership(), shape); | ||
} | ||
}; | ||
|
||
template <typename T, size_t N> | ||
struct inspector<xt::xtensor<T, N>>: public xtensor_inspector<xt::xtensor<T, N>, true> { | ||
private: | ||
using super = xtensor_inspector<xt::xtensor<T, N>, true>; | ||
|
||
public: | ||
using type = typename super::type; | ||
using value_type = typename super::value_type; | ||
using base_type = typename super::base_type; | ||
using hdf5_type = typename super::hdf5_type; | ||
}; | ||
|
||
template <typename T, size_t N, xt::layout_type L> | ||
struct inspector<xt::xtensor<T, N, L>>: public xtensor_inspector<xt::xtensor<T, N, L>, false> { | ||
private: | ||
using super = xtensor_inspector<xt::xtensor<T, N, L>, false>; | ||
|
||
public: | ||
using type = typename super::type; | ||
using value_type = typename super::value_type; | ||
using base_type = typename super::base_type; | ||
using hdf5_type = typename super::hdf5_type; | ||
}; | ||
|
||
} // namespace details | ||
} // namespace HighFive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters