Skip to content

[WIP] geometry #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions include/ttl/bits/low_rank_tensor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#pragma once
#include <algorithm>
#include <array>
#include <utility>

#include <ttl/bits/std_access_traits.hpp>
#include <ttl/bits/std_device.hpp>
#include <ttl/bits/std_static_shape.hpp>
#include <ttl/bits/std_tensor_fwd.hpp>

namespace ttl
{
namespace internal
{
template <typename R, typename Dim, Dim... ds>
class basic_tensor<R, basic_static_shape<Dim, ds...>, host_memory, owner>
{
protected:
using S = basic_static_shape<Dim, ds...>;
using data_t = std::array<R, S::dim>;

data_t data_;

template <typename F>
static void pointwise(const F &f, const data_t &x, data_t &y)
{
std::transform(x.begin(), x.end(), y.begin(), f);
}

template <typename F>
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<R>(0)); }

explicit basic_tensor(data_t data) : data_(std::move(data)) {}

template <typename... C>
basic_tensor(const C &... c) : data_({static_cast<R>(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<R, S, host_memory, owner> operator-() const
{
data_t y;
pointwise(std::negate<R>(), data_, y);
return basic_tensor<R, S, host_memory, owner>(std::move(y));
}

basic_tensor<R, S, host_memory, owner> operator*(R x) const
{
data_t y;
pointwise([x = x](R a) { return a * x; }, data_, y);
return basic_tensor<R, S, host_memory, owner>(std::move(y));
}

template <typename A>
basic_tensor<R, S, host_memory, owner>
operator+(const basic_tensor<R, S, host_memory, A> &y) const
{
data_t z;
pointwise(std::plus<R>(), data_, y.data_, z);
return basic_tensor<R, S, host_memory, owner>(std::move(z));
}

template <typename A>
basic_tensor<R, S, host_memory, owner>
operator-(const basic_tensor<R, S, host_memory, A> &y) const
{
data_t z;
pointwise(std::minus<R>(), data_, y.data_, z);
return basic_tensor<R, S, host_memory, owner>(std::move(z));
}
};
} // namespace internal
} // namespace ttl
29 changes: 29 additions & 0 deletions include/ttl/bits/std_static_shape.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include <utility>

namespace ttl
{
namespace internal
{
template <typename D, D... ds>
struct int_seq_prod;

template <typename D>
struct int_seq_prod<D> {
static constexpr D value = 1;
};

template <typename D, D d0, D... ds>
struct int_seq_prod<D, d0, ds...> {
static constexpr D value = d0 * int_seq_prod<D, ds...>::value;
};

template <typename D, D... ds>
class basic_static_shape : public std::integer_sequence<D, ds...>
{
public:
static constexpr auto rank = sizeof...(ds);
static constexpr D dim = int_seq_prod<D, ds...>::value;
};
} // namespace internal
} // namespace ttl
42 changes: 42 additions & 0 deletions include/ttl/experimental/low_rank_tensor
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// -*- mode: c++ -*-
#pragma once
#include <ttl/bits/low_rank_tensor.hpp>
#include <ttl/bits/std_access_traits.hpp>

namespace ttl
{
namespace internal
{
template <typename R, uint32_t n>
using basic_fixed_vector =
basic_tensor<R, basic_static_shape<uint32_t, n>, host_memory, owner>;

template <typename R, uint32_t m, uint32_t n>
using basic_fixed_matrix =
basic_tensor<R, basic_static_shape<uint32_t, m, n>, host_memory, owner>;
} // namespace internal

template <typename R, uint32_t n>
using vec = internal::basic_fixed_vector<R, n>;

template <typename R>
using vec2 = vec<R, 2>;

template <typename R>
using vec3 = vec<R, 3>;

template <typename R>
using vec4 = vec<R, 4>;

template <typename R, uint32_t m, uint32_t n = m>
using mat = internal::basic_fixed_matrix<R, m, n>;

template <typename R>
using mat2 = mat<R, 2>;

template <typename R>
using mat3 = mat<R, 3>;

template <typename R>
using mat4 = mat<R, 4>;
} // namespace ttl
55 changes: 55 additions & 0 deletions tests/test_include_ttl_experimental_low_rank_tensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "testing.hpp"

#include <ttl/experimental/low_rank_tensor>

template <typename R, int n>
void test_vec()
{
using ttl::vec;
static_assert(sizeof(vec<R, n>) == sizeof(R) * n, "");
}

template <typename R, int m, int n = m>
void test_mat()
{
using ttl::mat;
static_assert(sizeof(mat<R, m, n>) == sizeof(R) * m * n, "");
}

TEST(ttl_low_rank_tensor_include_test, test_vec)
{
// test_vec<int, 0>();
test_vec<int, 1>();
test_vec<int, 2>();
test_vec<int, 3>();
}

TEST(ttl_low_rank_tensor_include_test, test_mat)
{
// test_mat<int, 0>();
test_mat<int, 1>();
test_mat<int, 2>();
test_mat<int, 3>();

test_mat<int, 1, 2>();
test_mat<int, 1, 3>();
test_mat<int, 1, 4>();

test_mat<int, 2, 3>();
test_mat<int, 2, 4>();
test_mat<int, 3, 4>();
}

TEST(ttl_low_rank_tensor_include_test, test_op)
{
using ttl::vec;
vec<int, 2> x;
{
vec<int, 2> y = -x;
static_assert(sizeof(y) > 0, "");
}
{
vec<int, 2> y = x * 2;
static_assert(sizeof(y) > 0, "");
}
}