From 4b8824944bc8e286bbdb613277e080f718ef7c9e Mon Sep 17 00:00:00 2001 From: lg Date: Tue, 7 Apr 2020 15:46:18 +0100 Subject: [PATCH 1/2] vec and mat --- include/ttl/bits/low_rank_tensor.hpp | 76 +++++++++++++++++++ include/ttl/bits/std_static_shape.hpp | 29 +++++++ include/ttl/experimental/low_rank_tensor | 42 ++++++++++ ...clude_ttl_experimental_low_rank_tensor.cpp | 41 ++++++++++ tests/test_loc.cpp | 2 +- 5 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 include/ttl/bits/low_rank_tensor.hpp create mode 100644 include/ttl/bits/std_static_shape.hpp create mode 100644 include/ttl/experimental/low_rank_tensor create mode 100644 tests/test_include_ttl_experimental_low_rank_tensor.cpp diff --git a/include/ttl/bits/low_rank_tensor.hpp b/include/ttl/bits/low_rank_tensor.hpp new file mode 100644 index 0000000..336ba23 --- /dev/null +++ b/include/ttl/bits/low_rank_tensor.hpp @@ -0,0 +1,76 @@ +#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, 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() const { return data_.data(); } + + const R &operator[](int i) const { return data_[i]; } + + 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..3f41ab2 --- /dev/null +++ b/tests/test_include_ttl_experimental_low_rank_tensor.cpp @@ -0,0 +1,41 @@ +#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(); +} diff --git a/tests/test_loc.cpp b/tests/test_loc.cpp index f5f499a..ea78c07 100644 --- a/tests/test_loc.cpp +++ b/tests/test_loc.cpp @@ -30,5 +30,5 @@ TEST(test_loc, test1) ++n; } printf("total: %d lines in %d files\n", tot, n); - ASSERT_TRUE(tot <= 2100); + ASSERT_TRUE(tot <= 2300); } From cc1bbfba2e09b52fe8870a64a745f012de6548a6 Mon Sep 17 00:00:00 2001 From: lg Date: Wed, 8 Apr 2020 23:01:23 +0100 Subject: [PATCH 2/2] scaler vector operations --- include/ttl/bits/low_rank_tensor.hpp | 24 ++++++++++++++++++- ...clude_ttl_experimental_low_rank_tensor.cpp | 14 +++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/include/ttl/bits/low_rank_tensor.hpp b/include/ttl/bits/low_rank_tensor.hpp index 336ba23..28793bd 100644 --- a/include/ttl/bits/low_rank_tensor.hpp +++ b/include/ttl/bits/low_rank_tensor.hpp @@ -21,6 +21,12 @@ class basic_tensor, host_memory, owner> 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) @@ -50,10 +56,26 @@ class basic_tensor, host_memory, owner> static_assert(sizeof...(C) == dim, ""); } - R *data() const { return data_.data(); } + 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 diff --git a/tests/test_include_ttl_experimental_low_rank_tensor.cpp b/tests/test_include_ttl_experimental_low_rank_tensor.cpp index 3f41ab2..0d18311 100644 --- a/tests/test_include_ttl_experimental_low_rank_tensor.cpp +++ b/tests/test_include_ttl_experimental_low_rank_tensor.cpp @@ -39,3 +39,17 @@ TEST(ttl_low_rank_tensor_include_test, 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, ""); + } +}