feat(math): begin work on the tensor/math library
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
#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 {
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
class tensor_view {
|
||||
template <typename, std::size_t, std::size_t...>
|
||||
friend class tensor;
|
||||
public:
|
||||
using traits = tensor_traits<tensor<T, Dims...>>;
|
||||
using value_type = traits::value_type;
|
||||
public:
|
||||
private:
|
||||
value_type* m_view;
|
||||
|
||||
std::enable_if_t<(sizeof...(Dims) > 0), std::size_t[sizeof...(Dims)]> m_strides;
|
||||
};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
class tensor {
|
||||
public:
|
||||
using traits = tensor_traits<tensor>;
|
||||
using value_type = traits::value_type;
|
||||
public:
|
||||
tensor() {
|
||||
if constexpr (traits::dynamic) {
|
||||
m_items = nullptr;
|
||||
} else if constexpr (traits::inplace_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 { *this = std::move(other); }
|
||||
tensor& operator=(tensor&& other) noexcept {
|
||||
if (this == &other) return *this;
|
||||
release();
|
||||
m_items = other.m_items;
|
||||
std::memcpy(m_dimensions, other.m_dimensions, sizeof(m_dimensions));
|
||||
std::memcpy(m_strides, other.m_strides, sizeof(m_strides));
|
||||
|
||||
other.m_items = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tensor(const tensor& other) { *this = other; }
|
||||
tensor& operator=(const tensor& other) {
|
||||
if (this == &other) return *this;
|
||||
release();
|
||||
std::memcpy(m_dimensions, other.m_dimensions, sizeof(m_dimensions));
|
||||
std::memcpy(m_strides, other.m_strides, sizeof(m_strides));
|
||||
dyn_alloc(std::make_index_sequence<sizeof...(Dims)>{});
|
||||
dyn_copy(std::make_index_sequence<sizeof...(Dims)>{});
|
||||
|
||||
return *this;
|
||||
}
|
||||
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 constexpr (!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) const {
|
||||
return ((m_dimensions[Is] = dimensions) && ...);
|
||||
}
|
||||
private:
|
||||
template <std::size_t... Is>
|
||||
void dyn_alloc(std::index_sequence<Is...>) {
|
||||
m_items = new value_type[(m_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_dimensions[Is] * ... * 1));
|
||||
}
|
||||
|
||||
void release() {
|
||||
if constexpr (!traits::inline_storage) {
|
||||
delete[] m_items;
|
||||
}
|
||||
}
|
||||
private:
|
||||
traits::storage_type m_items;
|
||||
|
||||
std::enable_if_t<traits::dynamic, std::size_t[sizeof...(Dims)]> m_dimensions;
|
||||
std::enable_if_t<traits::dynamic, std::size_t[sizeof...(Dims)]> m_strides;
|
||||
};
|
||||
|
||||
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
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef CENET_MATH_FWD_HPP
|
||||
#define CENET_MATH_FWD_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <limits>
|
||||
|
||||
namespace cenet {
|
||||
namespace math {
|
||||
|
||||
inline constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max();
|
||||
|
||||
template <typename T>
|
||||
struct inline_storage {
|
||||
using value_type = T;
|
||||
};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
class tensor_view;
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
class tensor;
|
||||
|
||||
template <typename T, std::size_t M, std::size_t N>
|
||||
using matrix = tensor<T, M, N>;
|
||||
|
||||
} // namespace math
|
||||
} // namespace cenet
|
||||
|
||||
#endif // CENET_MATH_FWD_HPP
|
||||
@@ -0,0 +1,86 @@
|
||||
#ifndef CENET_MATH_TENSOR_TRAITS_HPP
|
||||
#define CENET_MATH_TENSOR_TRAITS_HPP
|
||||
|
||||
#include "tensor_fwd.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace cenet {
|
||||
namespace math {
|
||||
|
||||
template <typename T>
|
||||
struct tensor_storage {};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct tensor_storage<tensor<T, Dims...>> {
|
||||
static_assert(std::is_arithmetic_v<T>);
|
||||
|
||||
using value_type = T;
|
||||
using storage_type = T*;
|
||||
};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct tensor_storage<tensor<inline_storage<T>, Dims...>> {
|
||||
static_assert(std::is_arithmetic_v<T>);
|
||||
|
||||
using value_type = T;
|
||||
using storage_type = T[(Dims * ... * 1)];
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct is_storage_inline : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_storage_inline<inline_storage<T>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
static constexpr auto is_storage_inline_v = is_storage_inline<T>::value;
|
||||
|
||||
template <typename T>
|
||||
struct is_tensor : std::false_type {};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct is_tensor<tensor<T, Dims...>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
static constexpr auto is_tensor_v = is_tensor<T>::value;
|
||||
|
||||
template <typename T>
|
||||
struct is_tensor_dynamic : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_tensor_dynamic<tensor<T>> : std::false_type {};
|
||||
|
||||
template <typename T, std::size_t First, std::size_t... Rest>
|
||||
struct is_tensor_dynamic<tensor<T, First, Rest...>> : is_tensor_dynamic<tensor<T, Rest...>> {};
|
||||
|
||||
template <typename T, std::size_t... Rest>
|
||||
struct is_tensor_dynamic<tensor<T, dynamic_extent, Rest...>> : std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
static constexpr auto is_tensor_dynamic_v = is_tensor_dynamic<T>::value;
|
||||
|
||||
template <typename T>
|
||||
struct tensor_traits {};
|
||||
|
||||
template <typename T, std::size_t... Dims>
|
||||
struct tensor_traits<tensor<T, Dims...>> {
|
||||
using tensor_type = tensor<T, Dims...>;
|
||||
using value_type = tensor_storage<tensor_type>::value_type;
|
||||
using storage_type = tensor_storage<tensor_type>::storage_type;
|
||||
|
||||
static constexpr auto rank = sizeof...(Dims);
|
||||
static constexpr auto dynamic = is_tensor_dynamic_v<tensor_type>;
|
||||
|
||||
static constexpr auto inline_storage = is_storage_inline_v<T>;
|
||||
static_assert(!inline_storage || !dynamic, "Tensor storage set to inline with dynamic extents!");
|
||||
|
||||
static constexpr std::enable_if_t<(sizeof...(Dims) > 0), std::size_t[sizeof...(Dims)]> dimensions = { Dims... };
|
||||
|
||||
static constexpr std::size_t size = dynamic ? dynamic_extent : (Dims * ... * 1);
|
||||
};
|
||||
|
||||
} // namespace math
|
||||
} // namespace cenet
|
||||
|
||||
#endif // CENET_MATH_TENSOR_TRAITS_HPP
|
||||
Reference in New Issue
Block a user