Fixes for the forced evaluation of tensor expressions

More tests
This commit is contained in:
Benoit Steiner 2014-10-02 10:39:36 -07:00
parent 5cc23199be
commit 8b2afe33a1
4 changed files with 110 additions and 8 deletions

View File

@ -87,31 +87,28 @@ struct TensorEvaluator<const TensorForcedEvalOp<ArgType>, Device>
enum {
IsAligned = true,
PacketAccess = true,
PacketAccess = (internal::packet_traits<Scalar>::size > 1),
};
EIGEN_DEVICE_FUNC TensorEvaluator(const XprType& op, const Device& device)
: m_impl(op.expression(), device), m_op(op.expression()), m_device(device), m_buffer(NULL)
{ }
EIGEN_DEVICE_FUNC ~TensorEvaluator() {
eigen_assert(!m_buffer);
}
typedef typename XprType::Index Index;
typedef typename XprType::CoeffReturnType CoeffReturnType;
typedef typename XprType::PacketReturnType PacketReturnType;
EIGEN_DEVICE_FUNC const Dimensions& dimensions() const { return m_impl.dimensions(); }
EIGEN_STRONG_INLINE void evalSubExprsIfNeeded() {
m_impl.evalSubExprsIfNeeded();
EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar*) {
m_impl.evalSubExprsIfNeeded(NULL);
m_buffer = (Scalar*)m_device.allocate(m_impl.dimensions().TotalSize() * sizeof(Scalar));
typedef TensorEvalToOp<const ArgType> EvalTo;
EvalTo evalToTmp(m_buffer, m_op);
internal::TensorExecutor<const EvalTo, Device, TensorEvaluator<ArgType, Device>::PacketAccess>::run(evalToTmp, m_device);
m_impl.cleanup();
return true;
}
EIGEN_STRONG_INLINE void cleanup() {
m_device.deallocate(m_buffer);
@ -129,6 +126,8 @@ struct TensorEvaluator<const TensorForcedEvalOp<ArgType>, Device>
return internal::ploadt<Packet, LoadMode>(m_buffer + index);
}
Scalar* data() const { return m_buffer; }
private:
TensorEvaluator<ArgType, Device> m_impl;
const ArgType m_op;

View File

@ -101,10 +101,12 @@ if(EIGEN_TEST_CXX11)
ei_add_test(cxx11_tensor_simple "-std=c++0x")
ei_add_test(cxx11_tensor_symmetry "-std=c++0x")
# ei_add_test(cxx11_tensor_assign "-std=c++0x")
# ei_add_test(cxx11_tensor_dimension "-std=c++0x")
ei_add_test(cxx11_tensor_comparisons "-std=c++0x")
ei_add_test(cxx11_tensor_contraction "-std=c++0x")
ei_add_test(cxx11_tensor_convolution "-std=c++0x")
ei_add_test(cxx11_tensor_expr "-std=c++0x")
ei_add_test(cxx11_tensor_forced_eval "-std=c++0x")
# ei_add_test(cxx11_tensor_fixed_size "-std=c++0x")
ei_add_test(cxx11_tensor_const "-std=c++0x")
ei_add_test(cxx11_tensor_of_const_values "-std=c++0x")
@ -120,6 +122,5 @@ if(EIGEN_TEST_CXX11)
# ei_add_test(cxx11_tensor_shuffling "-std=c++0x")
ei_add_test(cxx11_tensor_striding "-std=c++0x")
# ei_add_test(cxx11_tensor_device "-std=c++0x")
# ei_add_test(cxx11_tensor_fixed_size "-std=c++0x")
# ei_add_test(cxx11_tensor_thread_pool "-std=c++0x")
endif()

View File

@ -0,0 +1,51 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include "main.h"
#include <Eigen/CXX11/Tensor>
using Eigen::Tensor;
static void test_dynamic_size()
{
Eigen::DSizes<int, 3> dimensions(Eigen::array<int, 3>(2,3,7));
VERIFY_IS_EQUAL((int)Eigen::internal::array_get<0>(dimensions), 2);
VERIFY_IS_EQUAL((int)Eigen::internal::array_get<1>(dimensions), 3);
VERIFY_IS_EQUAL((int)Eigen::internal::array_get<2>(dimensions), 7);
VERIFY_IS_EQUAL(dimensions.TotalSize(), (size_t)2*3*7);
}
static void test_fixed_size()
{
Eigen::Sizes<2,3,7> dimensions;
VERIFY_IS_EQUAL((int)Eigen::internal::array_get<0>(dimensions), 2);
VERIFY_IS_EQUAL((int)Eigen::internal::array_get<1>(dimensions), 3);
VERIFY_IS_EQUAL((int)Eigen::internal::array_get<2>(dimensions), 7);
VERIFY_IS_EQUAL(dimensions.TotalSize(), (size_t)2*3*7);
}
static void test_match()
{
Eigen::DSizes<int, 3> dyn(Eigen::array<int, 3>(2,3,7));
Eigen::Sizes<2,3,7> stat;
VERIFY_IS_EQUAL(Eigen::internal::dimensions_match(dyn, stat), true);
}
void test_cxx11_tensor_dimension()
{
CALL_SUBTEST(test_dynamic_size());
CALL_SUBTEST(test_fixed_size());
CALL_SUBTEST(test_match());
}

View File

@ -0,0 +1,51 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include "main.h"
#include <Eigen/Core>
#include <Eigen/CXX11/Tensor>
using Eigen::MatrixXf;
using Eigen::Tensor;
static void test_simple()
{
MatrixXf m1(3,3);
MatrixXf m2(3,3);
m1.setRandom();
m2.setRandom();
TensorMap<Tensor<float, 2>> mat1(m1.data(), 3,3);
TensorMap<Tensor<float, 2>> mat2(m2.data(), 3,3);
Tensor<float, 2> mat3(3,3);
mat3 = mat1;
typedef Tensor<float, 1>::DimensionPair DimPair;
Eigen::array<DimPair, 1> dims({{DimPair(1, 0)}});
mat3 = mat3.contract(mat2, dims).eval();
VERIFY_IS_APPROX(mat3(0, 0), (m1*m2).eval()(0,0));
VERIFY_IS_APPROX(mat3(0, 1), (m1*m2).eval()(0,1));
VERIFY_IS_APPROX(mat3(0, 2), (m1*m2).eval()(0,2));
VERIFY_IS_APPROX(mat3(1, 0), (m1*m2).eval()(1,0));
VERIFY_IS_APPROX(mat3(1, 1), (m1*m2).eval()(1,1));
VERIFY_IS_APPROX(mat3(1, 2), (m1*m2).eval()(1,2));
VERIFY_IS_APPROX(mat3(2, 0), (m1*m2).eval()(2,0));
VERIFY_IS_APPROX(mat3(2, 1), (m1*m2).eval()(2,1));
VERIFY_IS_APPROX(mat3(2, 2), (m1*m2).eval()(2,2));
}
void test_cxx11_tensor_forced_eval()
{
CALL_SUBTEST(test_simple());
}