Files
cenet/include/cenet/math/tensor.hpp
T

240 lines
8.1 KiB
C++

#ifndef CENET_MATH_TENSOR_HPP
#define CENET_MATH_TENSOR_HPP
#include "tensor_fwd.hpp"
#include "tensor_traits.hpp"
#include <cstddef>
#include <cstring>
#include <stdexcept>
#include <type_traits>
#include <utility>
namespace cenet {
namespace math {
namespace detail {
struct empty_shape {};
template <std::size_t Rank>
struct dynamic_shape {
dynamic_shape() = default;
template <std::size_t ExtRank, typename = std::enable_if_t<(ExtRank >= Rank)>>
explicit dynamic_shape(const dynamic_shape<ExtRank>& oth) {
constexpr std::size_t ext_offset = ExtRank - Rank;
for (std::size_t i = 0; i < Rank; ++i) {
dimensions[i] = oth.dimensions[ext_offset + i];
}
}
std::size_t dimensions[Rank]{};
};
} // namespace detail
template <typename T, std::size_t... Dims>
class tensor_view {
static_assert(std::is_arithmetic_v<T>);
template <typename, std::size_t...>
friend class tensor;
template <typename, std::size_t...>
friend class tensor_view;
public:
using traits = tensor_traits<tensor<T, Dims...>>;
using value_type = T;
public:
template <typename... Offs,
typename = std::enable_if_t<(
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
auto slice(Offs... offs) {
auto* ptr = m_view + calculate_offset(std::make_tuple(offs...));
if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr;
return inner_view<T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{});
}
template <typename... Offs,
typename = std::enable_if_t<(
sizeof...(Offs) > 0 && sizeof...(Offs) <= sizeof...(Dims) && (std::is_integral_v<Offs> && ...))>>
auto slice(Offs... offs) const {
const auto* ptr = m_view + calculate_offset(std::make_tuple(offs...));
if constexpr (sizeof...(Offs) == sizeof...(Dims)) return *ptr;
return inner_view<const T, sizeof...(Offs)>(ptr, std::index_sequence<Dims...>{});
}
public:
template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>>
auto operator[](Off off) {
return slice(off);
}
template <typename Off, typename = std::enable_if_t<std::is_integral_v<Off>>>
auto operator[](Off off) const {
return slice(off);
}
private:
template <typename... Offs>
std::size_t calculate_offset(const std::tuple<Offs...>& offs) const {
return calculate_offset_impl<sizeof...(Offs) - 1, sizeof...(Dims) - sizeof...(Offs)>(offs);
}
template <std::size_t I, std::size_t Start, typename... Offs>
std::size_t calculate_offset_impl(const std::tuple<Offs...>& offs) const {
if constexpr (I == 0)
return std::get<I>(offs) * dimension<Start + I>();
else
return (calculate_offset_impl<I - 1, Start>(offs) + std::get<I>(offs)) * dimension<Start + I>();
}
private:
template <typename InT, std::size_t Off, std::size_t First, std::size_t... Rest>
auto inner_view(value_type* view, std::index_sequence<First, Rest...>) {
if constexpr (Off == 0)
return tensor_view<InT, First, Rest...>{ view, m_shape };
else
return inner_view<InT, Off - 1>(view, std::index_sequence<Rest...>{});
}
private:
template <std::size_t I>
auto dimension() const {
if constexpr (traits::dynamic && traits::dimensions[I] == dynamic_extent) {
return m_shape.dimensions[I];
} else {
return traits::dimensions[I];
}
}
private:
using shape_type = std::conditional_t<traits::dynamic, detail::dynamic_shape<traits::rank>, detail::empty_shape>;
template <typename Fwd>
tensor_view(value_type* view, Fwd&& shape)
: m_view(view), m_shape(std::forward<Fwd>(shape)) {}
private:
value_type* m_view;
shape_type m_shape;
};
template <typename T, std::size_t... Dims>
class tensor {
public:
using traits = tensor_traits<tensor>;
using value_type = typename traits::value_type;
using constant_value_type = const value_type;
using view_type = tensor_view<value_type, Dims...>;
using constant_view_type = tensor_view<constant_value_type, Dims...>;
public:
tensor() {
if constexpr (traits::dynamic) {
m_items = nullptr;
m_shape = {};
} else if constexpr (traits::inline_storage) {
std::memset(m_items, 0, sizeof(m_items));
} else {
m_items = new value_type[traits::size];
}
}
template <typename... RtDims,
typename = std::enable_if_t<(
traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
explicit tensor(RtDims... dimensions) {
set_dimensions(dimensions...);
}
~tensor() { release(); }
tensor(tensor&& other) noexcept {
if constexpr (!traits::inline_storage) m_items = nullptr;
*this = std::move(other);
}
tensor& operator=(tensor&& other) noexcept {
if (this == &other) return *this;
release();
m_items = other.m_items;
std::memcpy(m_shape.dimensions, other.m_shape.dimensions, sizeof(m_shape.dimensions));
std::memcpy(m_shape.strides, other.m_shape.strides, sizeof(m_shape.strides));
if constexpr (!traits::inline_storage) other.m_items = nullptr;
return *this;
}
tensor(const tensor& other) {
if constexpr (!traits::inline_storage) m_items = nullptr;
*this = other;
}
tensor& operator=(const tensor& other) {
if (this == &other) return *this;
release();
std::memcpy(m_shape.dimensions, other.m_shape.dimensions, sizeof(m_shape.dimensions));
std::memcpy(m_shape.strides, other.m_shape.strides, sizeof(m_shape.strides));
dyn_alloc(std::make_index_sequence<sizeof...(Dims)>{});
dyn_copy(std::make_index_sequence<sizeof...(Dims)>{});
return *this;
}
public:
view_type view() { return { m_items, m_shape }; }
constant_view_type view() const { return { m_items, m_shape }; }
public:
template <typename... RtDims,
typename = std::enable_if_t<(
traits::dynamic && sizeof...(RtDims) == sizeof...(Dims) && (std::is_integral_v<RtDims> && ...))>>
void set_dimensions(RtDims... dimensions) {
if (!valid_dims(std::make_index_sequence<sizeof...(Dims)>{}, dimensions...))
throw std::runtime_error("invalid dynamic tensor dimensions");
store_dims(std::make_index_sequence<sizeof...(Dims)>{}, dimensions...);
release();
m_items = new value_type[(dimensions * ... * 1)];
std::memset(m_items, 0, sizeof(*m_items) * (dimensions * ... * 1));
}
private:
template <typename... RtDims, std::size_t... Is>
constexpr bool valid_dims(std::index_sequence<Is...>, RtDims... dimensions) const {
return (valid_dim<Is>(dimensions) && ...);
}
template <typename RtDim, std::size_t I>
constexpr bool valid_dim(RtDim dim) const {
return traits::dimensions[I] == dynamic_extent || traits::dimensions[I] == dim;
}
private:
template <typename... RtDims, std::size_t... Is>
constexpr void store_dims(std::index_sequence<Is...>, RtDims... dimensions) {
((m_shape.dimensions[Is] = static_cast<std::size_t>(dimensions)), ...);
}
private:
template <std::size_t... Is>
void dyn_alloc(std::index_sequence<Is...>) {
m_items = new value_type[(m_shape.dimensions[Is] * ...)];
}
template <std::size_t... Is>
void dyn_copy(const value_type* src, std::index_sequence<Is...>) {
std::memcpy(m_items, src, sizeof(*m_items) * (m_shape.dimensions[Is] * ... * 1));
}
void release() {
if constexpr (!traits::inline_storage) {
delete[] m_items;
m_items = nullptr;
}
}
private:
using shape_type = std::conditional_t<traits::dynamic, detail::dynamic_shape<traits::rank>, detail::empty_shape>;
private:
typename traits::storage_type m_items;
shape_type m_shape;
};
template <typename T, std::size_t M, std::size_t N>
using matrix = tensor<T, M, N>;
} // namespace math
} // namespace cenet
#endif // CENET_MATH_TENSOR_HPP