diff --git a/include/ttl/bits/low_rank_tensor.hpp b/include/ttl/bits/low_rank_tensor.hpp new file mode 100644 index 0000000..28793bd --- /dev/null +++ b/include/ttl/bits/low_rank_tensor.hpp @@ -0,0 +1,98 @@ +#pragma once +#include +#include +#include + +#include +#include +#include +#include + +namespace ttl +{ +namespace internal +{ +template +class basic_tensor, host_memory, owner> +{ + protected: + using S = basic_static_shape; + using data_t = std::array; + + data_t data_; + + template + static void pointwise(const F &f, const data_t &x, data_t &y) + { + std::transform(x.begin(), x.end(), y.begin(), f); + } + + template + static void pointwise(const F &f, const data_t &x, const data_t &y, + data_t &z) + { + std::transform(x.begin(), x.end(), y.begin(), z.begin(), f); + } + + public: + using shape_type = S; + using dimension_type = Dim; + + static constexpr auto rank = 1; + static constexpr Dim dim = S::dim; + + basic_tensor(void *) + { + // no initialization + } + + basic_tensor() { std::fill(data_.begin(), data_.end(), static_cast(0)); } + + explicit basic_tensor(data_t data) : data_(std::move(data)) {} + + template + basic_tensor(const C &... c) : data_({static_cast(c)...}) + { + static_assert(sizeof...(C) == dim, ""); + } + + R *data() { return data_.data(); } + + const R *data() const { return data_.data(); } + + const R &operator[](int i) const { return data_[i]; } + + basic_tensor operator-() const + { + data_t y; + pointwise(std::negate(), data_, y); + return basic_tensor(std::move(y)); + } + + basic_tensor operator*(R x) const + { + data_t y; + pointwise([x = x](R a) { return a * x; }, data_, y); + return basic_tensor(std::move(y)); + } + + template + basic_tensor + operator+(const basic_tensor &y) const + { + data_t z; + pointwise(std::plus(), data_, y.data_, z); + return basic_tensor(std::move(z)); + } + + template + basic_tensor + operator-(const basic_tensor &y) const + { + data_t z; + pointwise(std::minus(), data_, y.data_, z); + return basic_tensor(std::move(z)); + } +}; +} // namespace internal +} // namespace ttl diff --git a/include/ttl/bits/std_static_shape.hpp b/include/ttl/bits/std_static_shape.hpp new file mode 100644 index 0000000..d1f38c1 --- /dev/null +++ b/include/ttl/bits/std_static_shape.hpp @@ -0,0 +1,29 @@ +#pragma once +#include + +namespace ttl +{ +namespace internal +{ +template +struct int_seq_prod; + +template +struct int_seq_prod { + static constexpr D value = 1; +}; + +template +struct int_seq_prod { + static constexpr D value = d0 * int_seq_prod::value; +}; + +template +class basic_static_shape : public std::integer_sequence +{ + public: + static constexpr auto rank = sizeof...(ds); + static constexpr D dim = int_seq_prod::value; +}; +} // namespace internal +} // namespace ttl diff --git a/include/ttl/experimental/low_rank_tensor b/include/ttl/experimental/low_rank_tensor new file mode 100644 index 0000000..631392e --- /dev/null +++ b/include/ttl/experimental/low_rank_tensor @@ -0,0 +1,42 @@ +// -*- mode: c++ -*- +#pragma once +#include +#include + +namespace ttl +{ +namespace internal +{ +template +using basic_fixed_vector = + basic_tensor, host_memory, owner>; + +template +using basic_fixed_matrix = + basic_tensor, host_memory, owner>; +} // namespace internal + +template +using vec = internal::basic_fixed_vector; + +template +using vec2 = vec; + +template +using vec3 = vec; + +template +using vec4 = vec; + +template +using mat = internal::basic_fixed_matrix; + +template +using mat2 = mat; + +template +using mat3 = mat; + +template +using mat4 = mat; +} // namespace ttl diff --git a/tests/test_include_ttl_experimental_low_rank_tensor.cpp b/tests/test_include_ttl_experimental_low_rank_tensor.cpp new file mode 100644 index 0000000..0d18311 --- /dev/null +++ b/tests/test_include_ttl_experimental_low_rank_tensor.cpp @@ -0,0 +1,55 @@ +#include "testing.hpp" + +#include + +template +void test_vec() +{ + using ttl::vec; + static_assert(sizeof(vec) == sizeof(R) * n, ""); +} + +template +void test_mat() +{ + using ttl::mat; + static_assert(sizeof(mat) == sizeof(R) * m * n, ""); +} + +TEST(ttl_low_rank_tensor_include_test, test_vec) +{ + // test_vec(); + test_vec(); + test_vec(); + test_vec(); +} + +TEST(ttl_low_rank_tensor_include_test, test_mat) +{ + // test_mat(); + test_mat(); + test_mat(); + test_mat(); + + test_mat(); + test_mat(); + test_mat(); + + test_mat(); + test_mat(); + test_mat(); +} + +TEST(ttl_low_rank_tensor_include_test, test_op) +{ + using ttl::vec; + vec x; + { + vec y = -x; + static_assert(sizeof(y) > 0, ""); + } + { + vec y = x * 2; + static_assert(sizeof(y) > 0, ""); + } +}