Compare commits
2
Commits
3a27df500e
..
math
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a8b00b32d2
|
||
|
|
f98615fea4
|
@@ -5,12 +5,18 @@ project(cenet CXX)
|
||||
option(CENET_BUILD_TESTS "Compile cenet tests" OFF)
|
||||
option(CENET_BUILD_EXAMPLES "Compile cenet examples" ON)
|
||||
|
||||
option(TULIP_DISABLE_SIMD "Disable SIMD backend for tulip" OFF)
|
||||
|
||||
file(GLOB_RECURSE TULIP_HEADERS CONFIGURE_DEPENDS "include/tulip/*.hpp")
|
||||
add_library(tulip INTERFACE ${TULIP_HEADERS})
|
||||
target_include_directories(tulip INTERFACE include)
|
||||
target_compile_features(tulip INTERFACE cxx_std_17)
|
||||
add_library(cenet::tulip ALIAS tulip)
|
||||
|
||||
if(TULIP_DISABLE_SIMD)
|
||||
target_compile_definitions(tulip INTERFACE TULIP_DISABLE_SIMD)
|
||||
endif()
|
||||
|
||||
file(GLOB_RECURSE CENET_HEADERS CONFIGURE_DEPENDS "include/cenet/*.hpp")
|
||||
add_library(cenet INTERFACE ${CENET_HEADERS})
|
||||
target_link_libraries(cenet INTERFACE tulip)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef TULIP_DISPATCH_HPP
|
||||
#define TULIP_DISPATCH_HPP
|
||||
|
||||
#include "./kernels/elementwise.hpp"
|
||||
#include "./simd/simd.hpp"
|
||||
#include "type_traits.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace tulip {
|
||||
namespace detail {
|
||||
namespace dispatch {
|
||||
|
||||
template <typename Expr, typename Isa, typename = void>
|
||||
struct is_simd_evaluable : std::false_type {};
|
||||
|
||||
template <typename Expr, typename Isa>
|
||||
struct is_simd_evaluable<Expr,
|
||||
Isa,
|
||||
std::void_t<decltype(std::declval<const remove_cvref_t<Expr>&>().template eval_pack<Isa>(std::size_t{}))>>
|
||||
: std::true_type {};
|
||||
|
||||
template <typename Expr, typename Isa>
|
||||
inline constexpr auto is_simd_evaluable_v = is_simd_evaluable<Expr, Isa>::value;
|
||||
|
||||
template <typename T, typename Expr>
|
||||
void elementwise(T* dst, const Expr& expr, std::size_t size) {
|
||||
using value_type = std::remove_cv_t<T>;
|
||||
using expr_value_type = remove_cvref_t<decltype(expr.coeff(std::size_t{}))>;
|
||||
|
||||
using isa = simd::detail::preferred_isa_t<value_type>;
|
||||
|
||||
if constexpr (std::is_same_v<value_type, expr_value_type> && is_simd_evaluable_v<Expr, isa>) {
|
||||
kernels::elementwise<isa>(dst, expr, size);
|
||||
} else {
|
||||
kernels::elementwise_scalar(dst, expr, size);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace dispatch
|
||||
} // namespace detail
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_DISPATCH_HPP
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef TULIP_EVALUATE_HPP
|
||||
#define TULIP_EVALUATE_HPP
|
||||
|
||||
#include "./dispatch.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace tulip {
|
||||
namespace detail {
|
||||
|
||||
template <typename T, typename Expr>
|
||||
void evaluate_elementwise(T* dst, const Expr& expr, std::size_t size) {
|
||||
dispatch::elementwise(dst, expr, size);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_EVALUATE_HPP
|
||||
@@ -158,6 +158,37 @@ struct operand_storage {
|
||||
template <typename T>
|
||||
using operand_storage_t = typename operand_storage<T>::type;
|
||||
|
||||
template <typename T, typename Isa, typename Operand, typename = void>
|
||||
struct is_pack_operand : std::bool_constant<std::is_arithmetic_v<remove_cvref_t<Operand>>> {};
|
||||
|
||||
template <typename T, typename Isa, typename Operand>
|
||||
struct is_pack_operand<T,
|
||||
Isa,
|
||||
Operand,
|
||||
std::void_t<decltype(std::declval<const remove_cvref_t<Operand>&>().template eval_pack<Isa>(std::size_t{}))>>
|
||||
: std::is_same<decltype(std::declval<const remove_cvref_t<Operand>&>().template eval_pack<Isa>(std::size_t{})),
|
||||
simd::detail::pack_t<T, Isa>> {};
|
||||
|
||||
template <typename Op, typename T, typename Isa, typename = void>
|
||||
struct has_pack_apply : std::false_type {};
|
||||
|
||||
template <typename Op, typename T, typename Isa>
|
||||
struct has_pack_apply<Op,
|
||||
T,
|
||||
Isa,
|
||||
std::void_t<decltype(std::declval<const Op&>().template apply_pack<T, Isa>(
|
||||
std::declval<simd::detail::pack_t<T, Isa>>(),
|
||||
std::declval<simd::detail::pack_t<T, Isa>>()))>> : std::true_type {};
|
||||
|
||||
template <typename T, typename Isa, typename Operand>
|
||||
auto eval_pack_operand(const Operand& operand, std::size_t index) {
|
||||
if constexpr (std::is_arithmetic_v<remove_cvref_t<Operand>>) {
|
||||
return simd::detail::broadcast<T, Isa>(static_cast<T>(operand));
|
||||
} else {
|
||||
return operand.template eval_pack<Isa>(index);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename E, typename Op>
|
||||
@@ -195,6 +226,17 @@ struct binary_expression : expression_node_tag, ops_provider<binary_expression<L
|
||||
constexpr auto shape() const { return Op::shape(lhs, rhs); }
|
||||
|
||||
auto coeff(std::size_t index) const { return op(index, std::as_const(lhs), std::as_const(rhs)); }
|
||||
|
||||
using value_type = remove_cvref_t<decltype(std::declval<const binary_expression&>().coeff(std::size_t{}))>;
|
||||
|
||||
template <typename Isa,
|
||||
typename = std::enable_if_t<detail::is_pack_operand<value_type, Isa, lhs_type>::value &&
|
||||
detail::is_pack_operand<value_type, Isa, rhs_type>::value &&
|
||||
detail::has_pack_apply<Op, value_type, Isa>::value>>
|
||||
auto eval_pack(std::size_t index) const {
|
||||
return op.template apply_pack<value_type, Isa>(detail::eval_pack_operand<value_type, Isa>(lhs, index),
|
||||
detail::eval_pack_operand<value_type, Isa>(rhs, index));
|
||||
}
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef TULIP_KERNELS_ELEMENTWISE_HPP
|
||||
#define TULIP_KERNELS_ELEMENTWISE_HPP
|
||||
|
||||
#include "../simd/simd.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace tulip {
|
||||
namespace kernels {
|
||||
|
||||
template <typename T, typename Expr>
|
||||
void elementwise_scalar(T* dst, const Expr& expr, std::size_t size) {
|
||||
for (std::size_t idx = 0; idx < size; ++idx) {
|
||||
dst[idx] = expr.coeff(idx);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Isa, typename T, typename Expr>
|
||||
void elementwise(T* dst, const Expr& expr, std::size_t size) {
|
||||
std::size_t idx = 0;
|
||||
|
||||
constexpr std::size_t width = simd::detail::width_v<T, Isa>;
|
||||
|
||||
for (; idx + width <= size; idx += width) {
|
||||
auto value = expr.template eval_pack<Isa>(idx);
|
||||
simd::detail::store<T, Isa>(dst + idx, value);
|
||||
}
|
||||
|
||||
for (; idx < size; ++idx) {
|
||||
dst[idx] = expr.coeff(idx);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace kernels
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_KERNELS_ELEMENTWISE_HPP
|
||||
+28
-4
@@ -1,6 +1,7 @@
|
||||
#ifndef TULIP_OPS_HPP
|
||||
#define TULIP_OPS_HPP
|
||||
|
||||
#include "./simd/traits.hpp"
|
||||
#include "./tensor_fwd.hpp"
|
||||
#include "./tensor_shape.hpp"
|
||||
#include "./type_traits.hpp"
|
||||
@@ -67,10 +68,33 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
struct plus : generic_binary_op<std::plus<>> {};
|
||||
struct minus : generic_binary_op<std::minus<>> {};
|
||||
struct multiply : generic_binary_op<std::multiplies<>> {};
|
||||
struct divide : generic_binary_op<std::divides<>> {};
|
||||
struct plus : generic_binary_op<std::plus<>> {
|
||||
template <typename T, typename Isa>
|
||||
static auto apply_pack(simd::detail::pack_t<T, Isa> lhs, simd::detail::pack_t<T, Isa> rhs) noexcept {
|
||||
return simd::detail::add<T, Isa>(lhs, rhs);
|
||||
}
|
||||
};
|
||||
|
||||
struct minus : generic_binary_op<std::minus<>> {
|
||||
template <typename T, typename Isa>
|
||||
static auto apply_pack(simd::detail::pack_t<T, Isa> lhs, simd::detail::pack_t<T, Isa> rhs) noexcept {
|
||||
return simd::detail::sub<T, Isa>(lhs, rhs);
|
||||
}
|
||||
};
|
||||
|
||||
struct multiply : generic_binary_op<std::multiplies<>> {
|
||||
template <typename T, typename Isa>
|
||||
static auto apply_pack(simd::detail::pack_t<T, Isa> lhs, simd::detail::pack_t<T, Isa> rhs) noexcept {
|
||||
return simd::detail::mul<T, Isa>(lhs, rhs);
|
||||
}
|
||||
};
|
||||
|
||||
struct divide : generic_binary_op<std::divides<>> {
|
||||
template <typename T, typename Isa>
|
||||
static auto apply_pack(simd::detail::pack_t<T, Isa> lhs, simd::detail::pack_t<T, Isa> rhs) noexcept {
|
||||
return simd::detail::div<T, Isa>(lhs, rhs);
|
||||
}
|
||||
};
|
||||
|
||||
struct matmul {
|
||||
template <typename L, typename R>
|
||||
|
||||
@@ -14,10 +14,15 @@ struct traits<float, avx_isa> {
|
||||
|
||||
static constexpr std::size_t width = 4;
|
||||
|
||||
static pack_type load(const float* ptr) noexcept { return _mm128_loadu_ps(ptr); }
|
||||
static void store(float* ptr, pack_type value) noexcept { _mm128_storeu_ps(ptr, value); }
|
||||
static pack_type load(const float* ptr) noexcept { return _mm_loadu_ps(ptr); }
|
||||
static void store(float* ptr, pack_type value) noexcept { _mm_storeu_ps(ptr, value); }
|
||||
|
||||
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm128_add_ps(lhs, rhs); }
|
||||
static pack_type broadcast(float value) noexcept { return _mm_set1_ps(value); }
|
||||
|
||||
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm_add_ps(lhs, rhs); }
|
||||
static pack_type sub(pack_type lhs, pack_type rhs) noexcept { return _mm_sub_ps(lhs, rhs); }
|
||||
static pack_type mul(pack_type lhs, pack_type rhs) noexcept { return _mm_mul_ps(lhs, rhs); }
|
||||
static pack_type div(pack_type lhs, pack_type rhs) noexcept { return _mm_div_ps(lhs, rhs); }
|
||||
};
|
||||
#endif // TULIP_AVX_SUPPORTED
|
||||
|
||||
|
||||
@@ -17,7 +17,12 @@ struct traits<float, avx2_isa> {
|
||||
static pack_type load(const float* ptr) noexcept { return _mm256_loadu_ps(ptr); }
|
||||
static void store(float* ptr, pack_type value) noexcept { _mm256_storeu_ps(ptr, value); }
|
||||
|
||||
static pack_type broadcast(float value) noexcept { return _mm256_set1_ps(value); }
|
||||
|
||||
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm256_add_ps(lhs, rhs); }
|
||||
static pack_type sub(pack_type lhs, pack_type rhs) noexcept { return _mm256_sub_ps(lhs, rhs); }
|
||||
static pack_type mul(pack_type lhs, pack_type rhs) noexcept { return _mm256_mul_ps(lhs, rhs); }
|
||||
static pack_type div(pack_type lhs, pack_type rhs) noexcept { return _mm256_div_ps(lhs, rhs); }
|
||||
};
|
||||
|
||||
template <>
|
||||
@@ -26,10 +31,15 @@ struct traits<double, avx2_isa> {
|
||||
|
||||
static constexpr std::size_t width = 4;
|
||||
|
||||
static pack_type load(const double* ptr) noexcept { return _mm256d_loadu_ps(ptr); }
|
||||
static void store(double* ptr, pack_type value) noexcept { _mm256d_storeu_ps(ptr, value); }
|
||||
static pack_type load(const double* ptr) noexcept { return _mm256_loadu_pd(ptr); }
|
||||
static void store(double* ptr, pack_type value) noexcept { _mm256_storeu_pd(ptr, value); }
|
||||
|
||||
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm256d_add_ps(lhs, rhs); }
|
||||
static pack_type broadcast(float value) noexcept { return _mm256_set1_pd(value); }
|
||||
|
||||
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return _mm256_add_pd(lhs, rhs); }
|
||||
static pack_type sub(pack_type lhs, pack_type rhs) noexcept { return _mm256_sub_pd(lhs, rhs); }
|
||||
static pack_type mul(pack_type lhs, pack_type rhs) noexcept { return _mm256_mul_pd(lhs, rhs); }
|
||||
static pack_type div(pack_type lhs, pack_type rhs) noexcept { return _mm256_div_pd(lhs, rhs); }
|
||||
};
|
||||
#endif // TULIP_AVX2_SUPPORTED
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#ifndef TULIP_SIMD_CONFIG_HPP
|
||||
#define TULIP_SIMD_CONFIG_HPP
|
||||
|
||||
#if !defined(TULIP_DISABLE_SIMD) && (defined(__AVX__) || defined(__AVX2__))
|
||||
#include <immintrin.h>
|
||||
#endif
|
||||
|
||||
namespace tulip {
|
||||
namespace simd {
|
||||
|
||||
@@ -8,15 +12,13 @@ struct scalar_isa {};
|
||||
struct avx_isa {};
|
||||
struct avx2_isa {};
|
||||
|
||||
#ifdef __AVX__
|
||||
#if defined(__AVX__) && !defined(TULIP_DISABLE_SIMD)
|
||||
#define TULIP_AVX_SUPPORTED
|
||||
#include <immintrin.h>
|
||||
#endif // __AVX__
|
||||
#endif
|
||||
|
||||
#ifdef __AVX2__
|
||||
#if defined(__AVX2__) && !defined(TULIP_DISABLE_SIMD)
|
||||
#define TULIP_AVX2_SUPPORTED
|
||||
#include <immintrin.h>
|
||||
#endif // __AVX2__
|
||||
#endif
|
||||
|
||||
} // namespace simd
|
||||
} // namespace tulip
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef TULIP_SIMD_DISPATCH_HPP
|
||||
#define TULIP_SIMD_DISPATCH_HPP
|
||||
|
||||
#include "./config.hpp"
|
||||
|
||||
namespace tulip {
|
||||
namespace simd {
|
||||
namespace detail {
|
||||
|
||||
template <typename T>
|
||||
struct preferred_isa {
|
||||
using type = simd::scalar_isa;
|
||||
};
|
||||
|
||||
#ifdef TULIP_AVX2_SUPPORT
|
||||
template <>
|
||||
struct preferrpreferred_isa<float> {
|
||||
using type = simd::avx2_isa;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct preferrpreferred_isa<double> {
|
||||
using type = simd::avx2_isa;
|
||||
};
|
||||
#elif defined(TULIP_AVX_SUPPORT)
|
||||
template <>
|
||||
struct preferrpreferred_isa<float> {
|
||||
using type = simd::avx_isa;
|
||||
};
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
using preferred_isa_t = typename preferred_isa<T>::type;
|
||||
|
||||
} // namespace detail
|
||||
} // namespace simd
|
||||
} // namespace tulip
|
||||
|
||||
#endif // TULIP_SIMD_DISPATCH_HPP
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef TULIP_SIMD_HPP
|
||||
#define TULIP_SIMD_HPP
|
||||
|
||||
#include "./avx.hpp" // IWYU pragma: export
|
||||
#include "./avx2.hpp" // IWYU pragma: export
|
||||
#include "./config.hpp" // IWYU pragma: export
|
||||
#include "./dispatch.hpp" // IWYU pragma: export
|
||||
#include "./traits.hpp" // IWYU pragma: export
|
||||
|
||||
#endif // TULIP_SIMD_HPP
|
||||
@@ -21,7 +21,12 @@ struct traits<T, scalar_isa> {
|
||||
static pack_type load(const T* ptr) noexcept { return *ptr; }
|
||||
static void store(T* ptr, pack_type value) noexcept { *ptr = value; }
|
||||
|
||||
static pack_type broadcast(T value) noexcept { return value; }
|
||||
|
||||
static pack_type add(pack_type lhs, pack_type rhs) noexcept { return lhs + rhs; }
|
||||
static pack_type sub(pack_type lhs, pack_type rhs) noexcept { return lhs - rhs; }
|
||||
static pack_type mul(pack_type lhs, pack_type rhs) noexcept { return lhs * rhs; }
|
||||
static pack_type div(pack_type lhs, pack_type rhs) noexcept { return lhs / rhs; }
|
||||
};
|
||||
|
||||
template <typename T, typename Isa>
|
||||
@@ -36,15 +41,35 @@ static auto load(const T* ptr) noexcept {
|
||||
}
|
||||
|
||||
template <typename T, typename Isa>
|
||||
static void store(const T* ptr, pack_t<T, Isa> value) noexcept {
|
||||
static void store(T* ptr, pack_t<T, Isa> value) noexcept {
|
||||
traits<T, Isa>::store(ptr, value);
|
||||
}
|
||||
|
||||
template <typename T, typename Isa>
|
||||
static auto broadcast(T value) noexcept {
|
||||
return traits<T, Isa>::broadcast(value);
|
||||
}
|
||||
|
||||
template <typename T, typename Isa>
|
||||
static auto add(pack_t<T, Isa> lhs, pack_t<T, Isa> rhs) noexcept {
|
||||
return traits<T, Isa>::add(lhs, rhs);
|
||||
}
|
||||
|
||||
template <typename T, typename Isa>
|
||||
static auto sub(pack_t<T, Isa> lhs, pack_t<T, Isa> rhs) noexcept {
|
||||
return traits<T, Isa>::sub(lhs, rhs);
|
||||
}
|
||||
|
||||
template <typename T, typename Isa>
|
||||
static auto mul(pack_t<T, Isa> lhs, pack_t<T, Isa> rhs) noexcept {
|
||||
return traits<T, Isa>::mul(lhs, rhs);
|
||||
}
|
||||
|
||||
template <typename T, typename Isa>
|
||||
static auto div(pack_t<T, Isa> lhs, pack_t<T, Isa> rhs) noexcept {
|
||||
return traits<T, Isa>::div(lhs, rhs);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace simd
|
||||
} // namespace tulip
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "./tensor_shape.hpp"
|
||||
#include "./tensor_traits.hpp"
|
||||
#include "./tensor_view.hpp"
|
||||
#include "tulip/evaluate.hpp"
|
||||
#include "tulip/simd/traits.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
@@ -93,20 +95,14 @@ public:
|
||||
tensor(E&& exprFwd) { // NOLINT
|
||||
set_dimensions(exprFwd.shape());
|
||||
|
||||
E expr = std::forward<E>(exprFwd);
|
||||
for (std::size_t i = 0; i < size(); ++i) {
|
||||
m_items[i] = expr.coeff(i);
|
||||
}
|
||||
detail::evaluate_elementwise(m_items, std::forward<E>(exprFwd), size());
|
||||
}
|
||||
|
||||
template <typename E, typename = std::enable_if_t<detail::is_expression_v<E>>>
|
||||
tensor& operator=(E&& exprFwd) {
|
||||
set_dimensions(exprFwd.shape());
|
||||
if (m_shape != exprFwd.shape()) set_dimensions(exprFwd.shape());
|
||||
|
||||
E expr = std::forward<E>(exprFwd);
|
||||
for (std::size_t i = 0; i < view().size(); ++i) {
|
||||
m_items[i] = expr.coeff(i);
|
||||
}
|
||||
detail::evaluate_elementwise(m_items, std::forward<E>(exprFwd), size());
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -225,6 +221,13 @@ public:
|
||||
const value_type* data() const { return m_items; }
|
||||
|
||||
const shape_type& shape() const { return m_shape; }
|
||||
public:
|
||||
// WARN: eval_pack does not perform bounds checks!
|
||||
template <typename Isa>
|
||||
auto eval_pack(std::size_t idx) noexcept {
|
||||
using scalar_type = std::remove_cv_t<value_type>;
|
||||
return simd::detail::load<scalar_type, Isa>(m_items + idx);
|
||||
}
|
||||
private:
|
||||
template <typename... RtDims, std::size_t... Is>
|
||||
constexpr bool valid_dims(std::index_sequence<Is...>, RtDims... dimensions) const {
|
||||
|
||||
@@ -104,6 +104,13 @@ public:
|
||||
const value_type* data() const { return m_view; }
|
||||
|
||||
const shape_type& shape() const { return m_shape; }
|
||||
public:
|
||||
// WARN: eval_pack does not perform bounds checks!
|
||||
template <typename Isa>
|
||||
auto eval_pack(std::size_t idx) noexcept {
|
||||
using scalar_type = std::remove_cv_t<value_type>;
|
||||
return simd::detail::load<scalar_type, Isa>(m_view + idx);
|
||||
}
|
||||
private:
|
||||
template <typename InT, std::size_t Off, std::size_t First, std::size_t... Rest>
|
||||
auto inner_view(value_type* view, std::index_sequence<First, Rest...>) {
|
||||
|
||||
@@ -124,3 +124,47 @@ TEST(Tensor, Offsets2) {
|
||||
EXPECT_EQ(shape.calculate_offset(std::make_tuple(1, 2)), 20);
|
||||
EXPECT_EQ(shape.calculate_offset(std::make_tuple(1, 2, 3)), 23);
|
||||
}
|
||||
|
||||
TEST(Tensor, ElementwiseSimd) {
|
||||
constexpr std::size_t count = 13;
|
||||
|
||||
tulip::tensor<float, count> lhs;
|
||||
tulip::tensor<float, count> rhs;
|
||||
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
lhs.coeff(i) = static_cast<float>(i + 1);
|
||||
rhs.coeff(i) = static_cast<float>(i + 2);
|
||||
}
|
||||
|
||||
tulip::tensor<float, count> add = lhs + rhs;
|
||||
tulip::tensor<float, count> sub = lhs - rhs;
|
||||
tulip::tensor<float, count> mul = lhs * rhs;
|
||||
tulip::tensor<float, count> div = lhs / rhs;
|
||||
|
||||
// Tensor-scalar
|
||||
tulip::tensor<float, count> ts = lhs * 2.0F;
|
||||
|
||||
// Scalar-tensor
|
||||
tulip::tensor<float, count> st = 100.0F - lhs;
|
||||
tulip::tensor<float, count> sd = 120.0F / lhs;
|
||||
|
||||
// Nested expression
|
||||
tulip::tensor<float, count> nested = (lhs + rhs) * 2.0F;
|
||||
|
||||
for (std::size_t i = 0; i < count; ++i) {
|
||||
const float a = lhs.coeff(i);
|
||||
const float b = rhs.coeff(i);
|
||||
|
||||
EXPECT_FLOAT_EQ(add.coeff(i), a + b);
|
||||
EXPECT_FLOAT_EQ(sub.coeff(i), a - b);
|
||||
EXPECT_FLOAT_EQ(mul.coeff(i), a * b);
|
||||
EXPECT_FLOAT_EQ(div.coeff(i), a / b);
|
||||
|
||||
EXPECT_FLOAT_EQ(ts.coeff(i), a * 2.0F);
|
||||
|
||||
EXPECT_FLOAT_EQ(st.coeff(i), 100.0F - a);
|
||||
EXPECT_FLOAT_EQ(sd.coeff(i), 120.0F / a);
|
||||
|
||||
EXPECT_FLOAT_EQ(nested.coeff(i), (a + b) * 2.0F);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user