diff --git a/include/cenet/math/tensor.hpp b/include/cenet/math/tensor.hpp new file mode 100644 index 0000000..1ff5a05 --- /dev/null +++ b/include/cenet/math/tensor.hpp @@ -0,0 +1,135 @@ +#ifndef CENET_MATH_TENSOR_HPP +#define CENET_MATH_TENSOR_HPP + +#include "tensor_fwd.hpp" +#include "tensor_traits.hpp" + +#include +#include +#include +#include +#include + +namespace cenet { +namespace math { + +template +class tensor_view { + template + friend class tensor; +public: + using traits = tensor_traits>; + 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 +class tensor { +public: + using traits = tensor_traits; + 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 && ...))>> + 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{}); + dyn_copy(std::make_index_sequence{}); + + return *this; + } +public: + template && ...))>> + void set_dimensions(RtDims... dimensions) { + if constexpr (!valid_dims(std::make_index_sequence{}, dimensions...)) + throw std::runtime_error("invalid dynamic tensor dimensions"); + store_dims(std::make_index_sequence{}, dimensions...); + + release(); + m_items = new value_type[(dimensions * ... * 1)]; + std::memset(m_items, 0, sizeof(*m_items) * (dimensions * ... * 1)); + } +private: + template + constexpr bool valid_dims(std::index_sequence, RtDims... dimensions) const { + return (valid_dim(dimensions) && ...); + } + + template + constexpr bool valid_dim(RtDim dim) const { + return traits::dimensions[I] == dynamic_extent || traits::dimensions[I] == dim; + } +private: + template + constexpr void store_dims(std::index_sequence, RtDims... dimensions) const { + return ((m_dimensions[Is] = dimensions) && ...); + } +private: + template + void dyn_alloc(std::index_sequence) { + m_items = new value_type[(m_dimensions[Is] * ...)]; + } + + template + void dyn_copy(const value_type* src, std::index_sequence) { + 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 m_dimensions; + std::enable_if_t m_strides; +}; + +template +using matrix = tensor; + +} // namespace math +} // namespace cenet + +#endif // CENET_MATH_TENSOR_HPP diff --git a/include/cenet/math/tensor_fwd.hpp b/include/cenet/math/tensor_fwd.hpp new file mode 100644 index 0000000..9e3a92c --- /dev/null +++ b/include/cenet/math/tensor_fwd.hpp @@ -0,0 +1,29 @@ +#ifndef CENET_MATH_FWD_HPP +#define CENET_MATH_FWD_HPP + +#include +#include + +namespace cenet { +namespace math { + +inline constexpr std::size_t dynamic_extent = std::numeric_limits::max(); + +template +struct inline_storage { + using value_type = T; +}; + +template +class tensor_view; + +template +class tensor; + +template +using matrix = tensor; + +} // namespace math +} // namespace cenet + +#endif // CENET_MATH_FWD_HPP diff --git a/include/cenet/math/tensor_traits.hpp b/include/cenet/math/tensor_traits.hpp new file mode 100644 index 0000000..2cd1d5d --- /dev/null +++ b/include/cenet/math/tensor_traits.hpp @@ -0,0 +1,86 @@ +#ifndef CENET_MATH_TENSOR_TRAITS_HPP +#define CENET_MATH_TENSOR_TRAITS_HPP + +#include "tensor_fwd.hpp" + +#include + +namespace cenet { +namespace math { + +template +struct tensor_storage {}; + +template +struct tensor_storage> { + static_assert(std::is_arithmetic_v); + + using value_type = T; + using storage_type = T*; +}; + +template +struct tensor_storage, Dims...>> { + static_assert(std::is_arithmetic_v); + + using value_type = T; + using storage_type = T[(Dims * ... * 1)]; +}; + +template +struct is_storage_inline : std::false_type {}; + +template +struct is_storage_inline> : std::true_type {}; + +template +static constexpr auto is_storage_inline_v = is_storage_inline::value; + +template +struct is_tensor : std::false_type {}; + +template +struct is_tensor> : std::true_type {}; + +template +static constexpr auto is_tensor_v = is_tensor::value; + +template +struct is_tensor_dynamic : std::false_type {}; + +template +struct is_tensor_dynamic> : std::false_type {}; + +template +struct is_tensor_dynamic> : is_tensor_dynamic> {}; + +template +struct is_tensor_dynamic> : std::true_type {}; + +template +static constexpr auto is_tensor_dynamic_v = is_tensor_dynamic::value; + +template +struct tensor_traits {}; + +template +struct tensor_traits> { + using tensor_type = tensor; + using value_type = tensor_storage::value_type; + using storage_type = tensor_storage::storage_type; + + static constexpr auto rank = sizeof...(Dims); + static constexpr auto dynamic = is_tensor_dynamic_v; + + static constexpr auto inline_storage = is_storage_inline_v; + 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