From f863a38c26f6021ba6dcbe58e9611abfa5454915 Mon Sep 17 00:00:00 2001 From: lg Date: Tue, 4 Feb 2020 23:22:16 +0000 Subject: [PATCH 01/12] new API :: device_type --- include/ttl/bits/flat_tensor_mixin.hpp | 1 + include/ttl/bits/std_tensor_mixin.hpp | 2 ++ tests/_test_loc.cpp | 35 +++++++++++++++++++++ tests/test_tensor.cpp | 42 ++++++++++++++++++-------- 4 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 tests/_test_loc.cpp diff --git a/include/ttl/bits/flat_tensor_mixin.hpp b/include/ttl/bits/flat_tensor_mixin.hpp index 1ce11bd..96991fe 100644 --- a/include/ttl/bits/flat_tensor_mixin.hpp +++ b/include/ttl/bits/flat_tensor_mixin.hpp @@ -44,6 +44,7 @@ class flat_tensor_mixin public: using value_type = R; using shape_type = S; + using device_type = D; size_t data_size() const { return shape_.size() * sizeof(R); } diff --git a/include/ttl/bits/std_tensor_mixin.hpp b/include/ttl/bits/std_tensor_mixin.hpp index eca1d9e..7d7dfd8 100644 --- a/include/ttl/bits/std_tensor_mixin.hpp +++ b/include/ttl/bits/std_tensor_mixin.hpp @@ -30,6 +30,7 @@ class basic_scalar_mixin public: using value_type = R; using shape_type = S; + using device_type = D; static constexpr auto rank = S::rank; // == 0 @@ -112,6 +113,7 @@ class basic_tensor_mixin public: using value_type = R; using shape_type = S; + using device_type = D; using slice_type = basic_tensor; diff --git a/tests/_test_loc.cpp b/tests/_test_loc.cpp new file mode 100644 index 0000000..34aa019 --- /dev/null +++ b/tests/_test_loc.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +#include "testing.hpp" + +namespace fs = std::filesystem; + +int loc(const char *filename) +{ + FILE *fp = std::fopen(filename, "r"); + if (fp == nullptr) { return 0; } + constexpr int max_line = 1 << 16; + char line[max_line]; + int ln = 0; + while (std::fgets(line, max_line - 1, fp)) { ++ln; } + std::fclose(fp); + return ln; +} + +TEST(test_loc, test1) +{ + std::string path = "/path/to/directory"; + int tot = 0; + int n = 0; + for (const auto &entry : fs::directory_iterator("include/ttl/bits")) { + const int ln = loc(entry.path().c_str()); + printf("%4d %s\n", ln, entry.path().c_str()); + ASSERT_TRUE(ln <= 200); + tot += ln; + ++n; + } + printf("total: %d lines in %d files\n", tot, n); + ASSERT_TRUE(tot <= 2000); +} diff --git a/tests/test_tensor.cpp b/tests/test_tensor.cpp index f569d9c..e7890d4 100644 --- a/tests/test_tensor.cpp +++ b/tests/test_tensor.cpp @@ -40,20 +40,24 @@ TEST(tensor_test, test1) ASSERT_EQ(sum, n * (n + 1) / 2); } -template struct test_assign_ { +template +struct test_assign_ { void operator()(T &x, int v) { x = v; } }; -template struct test_assign_ { +template +struct test_assign_ { void operator()(T &x, int v) {} }; -template void test_assign(T &&x, int v) +template +void test_assign(T &&x, int v) { test_assign_()(x, v); } -template struct test_5d_array { +template +struct test_5d_array { void operator()(const T &t) { using R = typename T::value_type; @@ -118,9 +122,13 @@ TEST(tensor_test, test3) test_5d_array()(v); } -template void ref_func(const tensor_ref &x) {} +template +void ref_func(const tensor_ref &x) +{ +} -template void test_auto_ref() +template +void test_auto_ref() { static_assert(std::is_convertible, tensor_ref>::value, "can't convert to ref"); @@ -142,9 +150,13 @@ TEST(tensor_test, auto_ref) // f(t); // NOT possible } -template void view_func(const tensor_view &x) {} +template +void view_func(const tensor_view &x) +{ +} -template void test_auto_view() +template +void test_auto_view() { static_assert(std::is_convertible, tensor_view>::value, "can't convert to view"); @@ -177,7 +189,8 @@ auto create_tensor_func() TEST(tensor_test, return_tensor) { auto t = create_tensor_func(); } -template R read_tensor_func(const tensor &t, int i, int j) +template +R read_tensor_func(const tensor &t, int i, int j) { const R x = t.at(i, j); return x; @@ -256,6 +269,8 @@ void test_static_properties(const ttl::internal::basic_tensor &x) using T = ttl::internal::basic_tensor; static_assert(std::is_same::value, "invalid value_type"); + static_assert(std::is_same::value, + "invalid device_type"); static_assert(T::rank == r, "invalid rank"); auto x_shape = x.shape(); static_assert(decltype(x_shape)::rank == r, "invalid rank of shape"); @@ -316,7 +331,8 @@ TEST(tensor_test, test_const_properties) ""); } -template void test_slice_57_52_53_slice_19_38(const T &t) +template +void test_slice_57_52_53_slice_19_38(const T &t) { const auto t1 = t.slice(0, 19); const auto t2 = t.slice(19, 57); @@ -349,7 +365,8 @@ TEST(tensor_test, test_slice) } } -template void test_data_end(const T &t) +template +void test_data_end(const T &t) { ASSERT_EQ(t.data_end(), t.data() + t.shape().size()); { @@ -359,7 +376,8 @@ template void test_data_end(const T &t) } } -template void test_data_end_all() +template +void test_data_end_all() { // using ttl::experimental::raw_ref; // using ttl::experimental::raw_view; From aa81e1a4a2630a7d850bc3f0e6910fc337674cff Mon Sep 17 00:00:00 2001 From: lg Date: Wed, 5 Feb 2020 18:47:01 +0000 Subject: [PATCH 02/12] more constructors --- include/ttl/bits/std_tensor.hpp | 4 ++++ tests/test_scalar.cpp | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tests/test_scalar.cpp diff --git a/include/ttl/bits/std_tensor.hpp b/include/ttl/bits/std_tensor.hpp index 0873656..2f7c416 100644 --- a/include/ttl/bits/std_tensor.hpp +++ b/include/ttl/bits/std_tensor.hpp @@ -41,6 +41,8 @@ class basic_tensor, D, readwrite> using mixin::mixin; public: + basic_tensor(R *data) : mixin(data) {} + basic_tensor(const basic_tensor, D, owner> &t) : mixin(t.data()) { @@ -67,6 +69,8 @@ class basic_tensor, D, readonly> using mixin::mixin; public: + basic_tensor(const R *data) : mixin(data) {} + basic_tensor(const basic_tensor, D, owner> &t) : mixin(t.data()) { diff --git a/tests/test_scalar.cpp b/tests/test_scalar.cpp new file mode 100644 index 0000000..9c86c78 --- /dev/null +++ b/tests/test_scalar.cpp @@ -0,0 +1,17 @@ +#include "testing.hpp" + +#include + +TEST(scalar_test, test_constructor) +{ + { + ttl::tensor x; + ttl::tensor_ref r(x); + ttl::tensor_view v(x); + } + { + int value = 0; + ttl::tensor_ref r(&value); + ttl::tensor_view v(&value); + } +} From 5be2782405179f0e2667d6c98cf56a35d54658e1 Mon Sep 17 00:00:00 2001 From: lg Date: Sat, 8 Feb 2020 15:32:41 +0000 Subject: [PATCH 03/12] chebyshev_distenace --- include/ttl/algorithm | 1 + include/ttl/bits/std_host_tensor_algo.hpp | 13 +++++++ tests/test_algo.cpp | 47 +++++++++++++++-------- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/include/ttl/algorithm b/include/ttl/algorithm index f86bcfc..0e3ef98 100644 --- a/include/ttl/algorithm +++ b/include/ttl/algorithm @@ -6,6 +6,7 @@ namespace ttl { using ttl::internal::argmax; using ttl::internal::cast; +using ttl::internal::chebyshev_distenace; using ttl::internal::fill; using ttl::internal::hamming_distance; using ttl::internal::max; diff --git a/include/ttl/bits/std_host_tensor_algo.hpp b/include/ttl/bits/std_host_tensor_algo.hpp index 78aa35b..970c80a 100644 --- a/include/ttl/bits/std_host_tensor_algo.hpp +++ b/include/ttl/bits/std_host_tensor_algo.hpp @@ -37,6 +37,19 @@ Dim hamming_distance(const basic_host_tensor_view &x, std::not_equal_to()); } +template +R chebyshev_distenace(const basic_host_tensor_view &x, + const basic_host_tensor_view &y) +{ + return std::inner_product( + x.data(), x.data_end(), y.data(), static_cast(0), + [](R a, R d) { return std::max(a, d); }, + [](R x, R y) { + // FIXME: make sure it is commutative for floats + return x > y ? x - y : y - x; + }); +} + template R max(const basic_host_tensor_view &t) { diff --git a/tests/test_algo.cpp b/tests/test_algo.cpp index ec28758..f6e12af 100644 --- a/tests/test_algo.cpp +++ b/tests/test_algo.cpp @@ -8,7 +8,7 @@ TEST(tensor_algo_test, test_argmax) using R = float; ttl::tensor t(10); std::iota(t.data(), t.data_end(), 1); - ASSERT_EQ(static_cast(9), ttl::argmax(view(t))); + ASSERT_EQ(static_cast(9), ttl::argmax(ttl::view(t))); } TEST(tensor_algo_test, test_cast) @@ -23,9 +23,9 @@ TEST(tensor_algo_test, test_cast) }); ttl::tensor y(n); - ttl::cast(view(x), ref(y)); + ttl::cast(ttl::view(x), ttl::ref(y)); - ASSERT_EQ(5, ttl::sum(view(y))); + ASSERT_EQ(5, ttl::sum(ttl::view(y))); } TEST(tensor_algo_test, test_fill) @@ -33,12 +33,12 @@ TEST(tensor_algo_test, test_fill) { using R = int; ttl::tensor t(10); - ttl::fill(ref(t), 1); + ttl::fill(ttl::ref(t), 1); } { using R = float; ttl::tensor t(10); - ttl::fill(ref(t), static_cast(1.1)); + ttl::fill(ttl::ref(t), static_cast(1.1)); } } @@ -47,11 +47,26 @@ TEST(tensor_algo_test, test_hamming_distance) using R = int; int n = 0xffff; ttl::tensor x(n); - ttl::fill(ref(x), -1); + ttl::fill(ttl::ref(x), -1); ttl::tensor y(n); - ttl::fill(ref(y), 1); + ttl::fill(ttl::ref(y), 1); ASSERT_EQ(static_cast(n), - ttl::hamming_distance(view(x), view(y))); + ttl::hamming_distance(ttl::view(x), ttl::view(y))); +} + +TEST(tensor_algo_test, chebyshev_distenace) +{ + using R = int; + int n = 0xffff; + ttl::tensor x(n); + ttl::tensor y(n); + std::iota(x.data(), x.data_end(), 1); + std::iota(y.data(), y.data_end(), 1); + ASSERT_EQ(static_cast(0), + ttl::chebyshev_distenace(ttl::view(x), ttl::view(y))); + std::reverse(y.data(), y.data_end()); + ASSERT_EQ(static_cast(n - 1), + ttl::chebyshev_distenace(ttl::view(x), ttl::view(y))); } TEST(tensor_algo_test, test_summaries_int) @@ -60,10 +75,10 @@ TEST(tensor_algo_test, test_summaries_int) const int n = 10; ttl::tensor x(n); std::iota(x.data(), x.data_end(), -5); - ASSERT_EQ(-5, ttl::min(view(x))); - ASSERT_EQ(4, ttl::max(view(x))); - ASSERT_EQ(-5, ttl::sum(view(x))); - ASSERT_EQ(0, ttl::mean(view(x))); + ASSERT_EQ(-5, ttl::min(ttl::view(x))); + ASSERT_EQ(4, ttl::max(ttl::view(x))); + ASSERT_EQ(-5, ttl::sum(ttl::view(x))); + ASSERT_EQ(0, ttl::mean(ttl::view(x))); } TEST(tensor_algo_test, test_summaries_float) @@ -72,8 +87,8 @@ TEST(tensor_algo_test, test_summaries_float) const int n = 10; ttl::tensor x(n); std::iota(x.data(), x.data_end(), -5); - ASSERT_EQ(-5, ttl::min(view(x))); - ASSERT_EQ(4, ttl::max(view(x))); - ASSERT_EQ(-5, ttl::sum(view(x))); - ASSERT_EQ(-0.5, ttl::mean(view(x))); + ASSERT_EQ(-5, ttl::min(ttl::view(x))); + ASSERT_EQ(4, ttl::max(ttl::view(x))); + ASSERT_EQ(-5, ttl::sum(ttl::view(x))); + ASSERT_EQ(-0.5, ttl::mean(ttl::view(x))); } From 2dfb50d8b42efd8c26f8610a1010608597f5b073 Mon Sep 17 00:00:00 2001 From: lg Date: Sat, 15 Feb 2020 13:53:30 +0000 Subject: [PATCH 04/12] stdc++fs --- CMakeLists.txt | 6 ++++++ cmake/tests.cmake | 11 +++++------ tests/include/ttl/filesystem | 21 +++++++++++++++++++++ tests/{_test_loc.cpp => test_loc.cpp} | 3 +-- 4 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 tests/include/ttl/filesystem rename tests/{_test_loc.cpp => test_loc.cpp} (92%) diff --git a/CMakeLists.txt b/CMakeLists.txt index f0927c4..5bcd83d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,12 @@ ELSE() ADD_DEFINITIONS(-DUSE_FAKE_CUDA_RUNTIME) ENDIF() +IF(APPLE) + ADD_DEFINITIONS(-DHAVE_STD_CPP_FS) +ELSE() + LINK_LIBRARIES(stdc++fs) +ENDIF() + IF(BUILD_TESTS) ENABLE_TESTING() INCLUDE(cmake/tests.cmake) diff --git a/cmake/tests.cmake b/cmake/tests.cmake index 93e847c..cc5d497 100644 --- a/cmake/tests.cmake +++ b/cmake/tests.cmake @@ -1,8 +1,10 @@ INCLUDE(${CMAKE_SOURCE_DIR}/cmake/gtest.cmake) -FUNCTION(ADD_GTEST target) +FUNCTION(ADD_UNIT_TEST target) ADD_EXECUTABLE(${target} ${ARGN} tests/main.cpp) TARGET_USE_GTEST(${target}) + TARGET_INCLUDE_DIRECTORIES(${target} + PRIVATE ${CMAKE_SOURCE_DIR}/tests/include) TARGET_LINK_LIBRARIES(${target} stdtensor) IF(HAVE_CUDA) TARGET_LINK_LIBRARIES(${target} cudart) @@ -13,9 +15,6 @@ ENDFUNCTION() FILE(GLOB tests tests/test_*.cpp) FOREACH(t ${tests}) GET_FILENAME_COMPONENT(name ${t} NAME_WE) - STRING(REPLACE "_" - "-" - name - ${name}) - ADD_GTEST(${name} ${t}) + STRING(REPLACE "_" "-" name ${name}) + ADD_UNIT_TEST(${name} ${t}) ENDFOREACH() diff --git a/tests/include/ttl/filesystem b/tests/include/ttl/filesystem new file mode 100644 index 0000000..0c61203 --- /dev/null +++ b/tests/include/ttl/filesystem @@ -0,0 +1,21 @@ +// -*- mode: c++ -*- +#pragma once + +#ifdef HAVE_STD_CPP_FS +#include + +namespace std +{ +namespace filesystem = std::__fs::filesystem; +} + +#else + +#include + +namespace std +{ +namespace filesystem = std::experimental::filesystem; +} + +#endif diff --git a/tests/_test_loc.cpp b/tests/test_loc.cpp similarity index 92% rename from tests/_test_loc.cpp rename to tests/test_loc.cpp index 34aa019..ef5b00f 100644 --- a/tests/_test_loc.cpp +++ b/tests/test_loc.cpp @@ -1,6 +1,6 @@ #include -#include #include +#include #include "testing.hpp" @@ -20,7 +20,6 @@ int loc(const char *filename) TEST(test_loc, test1) { - std::string path = "/path/to/directory"; int tot = 0; int n = 0; for (const auto &entry : fs::directory_iterator("include/ttl/bits")) { From d5293dffae58fd6958a0f73ce244154086837415 Mon Sep 17 00:00:00 2001 From: lg Date: Sat, 15 Feb 2020 14:14:50 +0000 Subject: [PATCH 05/12] COC --- CODE_OF_CONDUCT | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 CODE_OF_CONDUCT diff --git a/CODE_OF_CONDUCT b/CODE_OF_CONDUCT new file mode 100644 index 0000000..a889595 --- /dev/null +++ b/CODE_OF_CONDUCT @@ -0,0 +1,8 @@ +"All things were made of numbers." +Pythagoras (c. 570 BC ~ c. 495 BC) + +"Beauty is the first test: there is no permanent place in the world for ugly mathematics." +G. H. Hardy (1877 - 1947) + +"I, in any case, am convinced that He does not play dice." +Albert Einstein (1879 - 1955) From 5b38c99d03765a77f7492227db4ecfa0b91509f1 Mon Sep 17 00:00:00 2001 From: lg Date: Sat, 15 Feb 2020 17:38:13 +0000 Subject: [PATCH 06/12] deprecate --- include/stdtensor | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 include/stdtensor diff --git a/include/stdtensor b/include/stdtensor deleted file mode 100644 index 907b016..0000000 --- a/include/stdtensor +++ /dev/null @@ -1,3 +0,0 @@ -// For backward compatibility -#pragma once -#include From 86a024a394cea988c3ed569474bc69e216c9e493 Mon Sep 17 00:00:00 2001 From: lg Date: Sat, 15 Feb 2020 17:40:37 +0000 Subject: [PATCH 07/12] fix test --- tests/{test_include.cpp => test_include_ttl_tensor.cpp} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/{test_include.cpp => test_include_ttl_tensor.cpp} (86%) diff --git a/tests/test_include.cpp b/tests/test_include_ttl_tensor.cpp similarity index 86% rename from tests/test_include.cpp rename to tests/test_include_ttl_tensor.cpp index 6af1611..8744fe1 100644 --- a/tests/test_include.cpp +++ b/tests/test_include_ttl_tensor.cpp @@ -1,6 +1,6 @@ #include "testing.hpp" -#include +#include using ttl::tensor; using ttl::tensor_ref; From 7957cce9bfdbdd70570b06f148ee148c6ba4d0fe Mon Sep 17 00:00:00 2001 From: lg Date: Sat, 15 Feb 2020 17:55:45 +0000 Subject: [PATCH 08/12] support .at() for rank 0 tensor --- include/ttl/bits/std_tensor_mixin.hpp | 6 +++ tests/test_tensor.cpp | 69 ++++++++++++++++----------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/include/ttl/bits/std_tensor_mixin.hpp b/include/ttl/bits/std_tensor_mixin.hpp index 7d7dfd8..0d98fd1 100644 --- a/include/ttl/bits/std_tensor_mixin.hpp +++ b/include/ttl/bits/std_tensor_mixin.hpp @@ -47,6 +47,12 @@ class basic_scalar_mixin data_ptr data_end() const { return data_.get() + 1; } S shape() const { return S(); } + + data_ref at() const + { // FIXME: support other devices + static_assert(std::is_same::value, ""); + return data_.get()[0]; + } }; template diff --git a/tests/test_tensor.cpp b/tests/test_tensor.cpp index e7890d4..69b01f9 100644 --- a/tests/test_tensor.cpp +++ b/tests/test_tensor.cpp @@ -189,52 +189,67 @@ auto create_tensor_func() TEST(tensor_test, return_tensor) { auto t = create_tensor_func(); } -template -R read_tensor_func(const tensor &t, int i, int j) +template +R read_tensor_func(const tensor &t, I... is) { - const R x = t.at(i, j); + const R x = t.at(is...); return x; } -template -R read_tensor_ref_func(const tensor_ref &t, int i, int j) +template +R read_tensor_ref_func(const tensor_ref &t, I... is) { - const R x = t.at(i, j); + const R x = t.at(is...); return x; } -template -R read_tensor_view_func(const tensor_view &t, int i, int j) +template +R read_tensor_view_func(const tensor_view &t, I... is) { - const R x = t.at(i, j); + const R x = t.at(is...); return x; } TEST(tensor_test, test_read_access) { - tensor t(2, 2); + { + tensor t; + t.data()[0] = 1; + ASSERT_EQ(t.at(), 1); - t[0][0] = 1; - ASSERT_EQ(1, read_tensor_func(t, 0, 0)); + t.at() = 2; + tensor_ref r = ttl::ref(t); + ASSERT_EQ(r.at(), 2); - t[0][0] = 2; - ASSERT_EQ(2, read_tensor_ref_func(ref(t), 0, 0)); + r.at() = 3; + tensor_view v = ttl::view(t); + ASSERT_EQ(v.at(), 3); + } + { + tensor t(2, 2); - t[0][0] = 3; - ASSERT_EQ(3, read_tensor_view_func(view(t), 0, 0)); + t[0][0] = 1; + ASSERT_EQ(1, read_tensor_func(t, 0, 0)); - { - auto v = view(t); - auto p = v.at(0, 0); - p += 1; - UNUSED(p); + t[0][0] = 2; + ASSERT_EQ(2, read_tensor_ref_func(ref(t), 0, 0)); + + t[0][0] = 3; ASSERT_EQ(3, read_tensor_view_func(view(t), 0, 0)); - } - { - auto &p = t.at(0, 0); - p += 1; - UNUSED(p); - ASSERT_EQ(4, read_tensor_view_func(view(t), 0, 0)); + + { + auto v = view(t); + auto p = v.at(0, 0); + p += 1; + UNUSED(p); + ASSERT_EQ(3, read_tensor_view_func(view(t), 0, 0)); + } + { + auto &p = t.at(0, 0); + p += 1; + UNUSED(p); + ASSERT_EQ(4, read_tensor_view_func(view(t), 0, 0)); + } } } From 3cb51466db276c032eaa3883571e80702abd05c5 Mon Sep 17 00:00:00 2001 From: lg Date: Sat, 15 Feb 2020 18:39:58 +0000 Subject: [PATCH 09/12] raw to flat --- include/ttl/bits/raw_tensor_mixin.hpp | 9 ++++++ include/ttl/bits/std_access_traits.hpp | 29 ++++++++++++++++++++ include/ttl/bits/std_tensor_traits.hpp | 7 ++--- tests/test_raw_tensor.cpp | 38 +++++++++++++++++++++++++- 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 include/ttl/bits/std_access_traits.hpp diff --git a/include/ttl/bits/raw_tensor_mixin.hpp b/include/ttl/bits/raw_tensor_mixin.hpp index 1f21b00..b7578c0 100644 --- a/include/ttl/bits/raw_tensor_mixin.hpp +++ b/include/ttl/bits/raw_tensor_mixin.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include #include @@ -86,6 +87,14 @@ class raw_tensor_mixin return reinterpret_cast(data_.get()); } + template + auto typed() const + { + using Access = typename basic_access_traits::type; + using T = basic_tensor, D, Access>; + return T(data(), shape_); + } + template basic_tensor, D, A1> ranked_as() const { diff --git a/include/ttl/bits/std_access_traits.hpp b/include/ttl/bits/std_access_traits.hpp new file mode 100644 index 0000000..16fefaf --- /dev/null +++ b/include/ttl/bits/std_access_traits.hpp @@ -0,0 +1,29 @@ +#pragma once + +namespace ttl +{ +namespace internal +{ +struct owner; +struct readwrite; +struct readonly; + +template +struct basic_access_traits; + +template <> +struct basic_access_traits { + using type = readwrite; +}; + +template <> +struct basic_access_traits { + using type = readwrite; +}; + +template <> +struct basic_access_traits { + using type = readonly; +}; +} // namespace internal +} // namespace ttl diff --git a/include/ttl/bits/std_tensor_traits.hpp b/include/ttl/bits/std_tensor_traits.hpp index ba2fa57..2f93ca8 100644 --- a/include/ttl/bits/std_tensor_traits.hpp +++ b/include/ttl/bits/std_tensor_traits.hpp @@ -1,15 +1,12 @@ #pragma once #include +#include #include namespace ttl { namespace internal { -struct owner; -struct readwrite; -struct readonly; - template using own_ptr = std::unique_ptr>; @@ -45,7 +42,7 @@ struct basic_tensor_traits { using ref_type = R &; using Data = own_ptr; - using Access = readwrite; + using Access = readwrite; // FIXME: use basic_access_traits }; template diff --git a/tests/test_raw_tensor.cpp b/tests/test_raw_tensor.cpp index 3e9638d..c3cc21a 100644 --- a/tests/test_raw_tensor.cpp +++ b/tests/test_raw_tensor.cpp @@ -93,7 +93,8 @@ TEST(raw_tensor_test, test_convert) } } -template void test_raw_accessors(const T &t) +template +void test_raw_accessors(const T &t) { t.shape(); t.value_type(); @@ -124,3 +125,38 @@ TEST(raw_tensor_test, test_data) test_raw_accessors(rr); test_raw_accessors(rv); } + +#include + +TEST(raw_tensor_test, test_convert_to_flat) +{ + using ttl::experimental::raw_tensor; + using ttl::experimental::raw_tensor_ref; + using ttl::experimental::raw_tensor_view; + using encoder = raw_tensor::encoder_type; + using raw_shape = raw_tensor::shape_type; + + raw_tensor rt(encoder::value(), 1, 2, 3); + { + static_assert( + std::is_same()), + ttl::experimental::flat_tensor_ref>::value, + ""); + ttl::experimental::flat_tensor_ref ft = rt.typed(); + ASSERT_EQ(ft.shape().size(), static_cast(6)); + } + { + const raw_tensor_ref rtr(rt); + static_assert( + std::is_same()), + ttl::experimental::flat_tensor_ref>::value, + ""); + } + { + const raw_tensor_view rtv(rt); + static_assert( + std::is_same()), + ttl::experimental::flat_tensor_view>::value, + ""); + } +} From 1bbeb336e51dd62b17fc53973ee758617dd2577c Mon Sep 17 00:00:00 2001 From: lg Date: Sat, 15 Feb 2020 18:51:14 +0000 Subject: [PATCH 10/12] flat_tensor::{rank,size,dims} --- include/ttl/bits/flat_tensor_mixin.hpp | 6 ++++++ tests/test_raw_tensor.cpp | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/ttl/bits/flat_tensor_mixin.hpp b/include/ttl/bits/flat_tensor_mixin.hpp index 96991fe..26c4a42 100644 --- a/include/ttl/bits/flat_tensor_mixin.hpp +++ b/include/ttl/bits/flat_tensor_mixin.hpp @@ -46,6 +46,12 @@ class flat_tensor_mixin using shape_type = S; using device_type = D; + rank_t rank() const { return shape_.rank(); } + + Dim size() const { return shape_.size(); } + + const auto &dims() const { return shape_.dims(); } + size_t data_size() const { return shape_.size() * sizeof(R); } const S &shape() const { return shape_; } diff --git a/tests/test_raw_tensor.cpp b/tests/test_raw_tensor.cpp index c3cc21a..674dcdb 100644 --- a/tests/test_raw_tensor.cpp +++ b/tests/test_raw_tensor.cpp @@ -143,7 +143,7 @@ TEST(raw_tensor_test, test_convert_to_flat) ttl::experimental::flat_tensor_ref>::value, ""); ttl::experimental::flat_tensor_ref ft = rt.typed(); - ASSERT_EQ(ft.shape().size(), static_cast(6)); + ASSERT_EQ(ft.size(), static_cast(6)); } { const raw_tensor_ref rtr(rt); From 7dd250be23f919be8251889bc86a9aebdd6b0eb0 Mon Sep 17 00:00:00 2001 From: lg Date: Tue, 18 Feb 2020 15:50:46 +0000 Subject: [PATCH 11/12] simplify opencv example --- .gitignore | 2 ++ examples/example_opencv.cpp | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 8974617..b22c20f 100644 --- a/.gitignore +++ b/.gitignore @@ -47,3 +47,5 @@ compile_commands.json CTestTestfile.cmake install_manifest.txt Makefile + +*.png diff --git a/examples/example_opencv.cpp b/examples/example_opencv.cpp index 08bb6e7..1543c2e 100644 --- a/examples/example_opencv.cpp +++ b/examples/example_opencv.cpp @@ -8,8 +8,10 @@ using bmp_t = ttl::matrix; void save_bmp(const bmp_t &bmp) { - const cv::Mat img(cv::Size(bmp.shape().dims()[1], bmp.shape().dims()[0]), - CV_8UC(3), (void *)bmp.data()); + uint32_t h, w; + std::tie(h, w) = bmp.dims(); + // const auto [h, w] = bmp.dims(); // c++17 + const cv::Mat img(cv::Size(w, h), CV_8UC(3), bmp.data()); cv::imwrite("i.png", img); } From bebf814591ece76223f945af5383eb2dfc8f0129 Mon Sep 17 00:00:00 2001 From: lg Date: Tue, 18 Feb 2020 22:51:08 +0000 Subject: [PATCH 12/12] fix test for gcc --- tests/test_shape.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_shape.cpp b/tests/test_shape.cpp index 2464542..16b54c0 100644 --- a/tests/test_shape.cpp +++ b/tests/test_shape.cpp @@ -17,8 +17,11 @@ void test_shape(dim_t h, dim_t w) for (dim_t j = 0; j < w; ++j) { ASSERT_EQ(s.offset(i, j), k); { - dim_t u, v; - std::tie(u, v) = s.expand(k); + // dim_t u, v; + // std::tie(u, v) = s.expand(k); + const auto coords = s.expand(k); + const auto u = std::get<0>(coords); + const auto v = std::get<1>(coords); ASSERT_EQ(i, u); ASSERT_EQ(j, v); }