From 35722fa0222a7f99a8179d75244177a9801ea36b Mon Sep 17 00:00:00 2001 From: Benoit Steiner Date: Mon, 30 Mar 2015 14:55:54 -0700 Subject: [PATCH] Made the index type a template parameter of the tensor class instead of encoding it in the options. --- Eigen/src/Core/util/Constants.h | 6 +- unsupported/Eigen/CXX11/src/Tensor/Tensor.h | 10 +-- .../Eigen/CXX11/src/Tensor/TensorBase.h | 6 +- .../Eigen/CXX11/src/Tensor/TensorDimensions.h | 4 +- .../Eigen/CXX11/src/Tensor/TensorFixedSize.h | 10 +-- .../src/Tensor/TensorForwardDeclarations.h | 4 +- .../Eigen/CXX11/src/Tensor/TensorStorage.h | 49 ++++--------- .../Eigen/CXX11/src/Tensor/TensorTraits.h | 71 ++++++++----------- .../test/cxx11_tensor_mixed_indices.cpp | 10 ++- 9 files changed, 66 insertions(+), 104 deletions(-) diff --git a/Eigen/src/Core/util/Constants.h b/Eigen/src/Core/util/Constants.h index fea75a004..d1855b50b 100644 --- a/Eigen/src/Core/util/Constants.h +++ b/Eigen/src/Core/util/Constants.h @@ -296,11 +296,7 @@ enum { /** Align the matrix itself if it is vectorizable fixed-size */ AutoAlign = 0, /** Don't require alignment for the matrix itself (the array of coefficients, if dynamically allocated, may still be requested to be aligned) */ // FIXME --- clarify the situation - DontAlign = 0x2, - /** Use the DenseIndex type to index the matrix/array/tensor. Unless otherwise specified by defining EIGEN_DEFAULT_DENSE_INDEX_TYPE, DenseIndex is a ptrdiff_t. */ - IndexDefault = 0, - /** Use 32bit signed integers to index the matrix/array/tensor. */ - Index32Bit = 0x4 + DontAlign = 0x2 }; /** \ingroup enums diff --git a/unsupported/Eigen/CXX11/src/Tensor/Tensor.h b/unsupported/Eigen/CXX11/src/Tensor/Tensor.h index 87ced2cce..b56cb936f 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/Tensor.h +++ b/unsupported/Eigen/CXX11/src/Tensor/Tensor.h @@ -59,12 +59,12 @@ namespace Eigen { * \ref TopicStorageOrders */ -template -class Tensor : public TensorBase > +template +class Tensor : public TensorBase > { public: - typedef Tensor Self; - typedef TensorBase > Base; + typedef Tensor Self; + typedef TensorBase > Base; typedef typename Eigen::internal::nested::type Nested; typedef typename internal::traits::StorageKind StorageKind; typedef typename internal::traits::Index Index; @@ -86,7 +86,7 @@ class Tensor : public TensorBase > typedef DSizes Dimensions; protected: - TensorStorage m_storage; + TensorStorage m_storage; public: // Metadata diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h b/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h index ca6681a1f..22fbe9b2c 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h @@ -460,8 +460,7 @@ class TensorBase } protected: - template friend class Tensor; - template friend class TensorVarDim; + template friend class Tensor; template friend class TensorBase; EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Derived& derived() const { return *static_cast(this); } @@ -477,8 +476,7 @@ class TensorBase : public TensorBase::type PacketReturnType; static const int NumDimensions = DerivedTraits::NumDimensions; - template friend class Tensor; - template friend class TensorVarDim; + template friend class Tensor; template friend class TensorBase; EIGEN_DEVICE_FUNC diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h b/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h index 474636a89..4c713af9f 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorDimensions.h @@ -184,7 +184,7 @@ template EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::size_t array_prod(const Sizes&) { return Sizes::total_size; -}; +} #endif @@ -345,7 +345,7 @@ template EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::size_t array_get(const Sizes& a) { return get::Base>::value; -}; +} #endif diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorFixedSize.h b/unsupported/Eigen/CXX11/src/Tensor/TensorFixedSize.h index 94b3f957b..76998b690 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorFixedSize.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorFixedSize.h @@ -23,12 +23,12 @@ namespace Eigen { * Eigen::TensorFixedSize> t; */ -template -class TensorFixedSize : public TensorBase > +template +class TensorFixedSize : public TensorBase > { public: - typedef TensorFixedSize Self; - typedef TensorBase > Base; + typedef TensorFixedSize Self; + typedef TensorBase > Base; typedef typename Eigen::internal::nested::type Nested; typedef typename internal::traits::StorageKind StorageKind; typedef typename internal::traits::Index Index; @@ -50,7 +50,7 @@ class TensorFixedSize : public TensorBase m_storage; + TensorStorage m_storage; public: EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rank() const { return NumIndices; } diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorForwardDeclarations.h b/unsupported/Eigen/CXX11/src/Tensor/TensorForwardDeclarations.h index 3607fe3fe..1441943c3 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorForwardDeclarations.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorForwardDeclarations.h @@ -12,8 +12,8 @@ namespace Eigen { -template class Tensor; -template class TensorFixedSize; +template class Tensor; +template class TensorFixedSize; template class TensorMap; template class TensorRef; template::value> class TensorBase; diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorStorage.h b/unsupported/Eigen/CXX11/src/Tensor/TensorStorage.h index c1d004678..f567b8c03 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorStorage.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorStorage.h @@ -2,6 +2,7 @@ // for linear algebra. // // Copyright (C) 2013 Christian Seiler +// Copyright (C) 2014-2015 Benoit Steiner // // 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 @@ -30,21 +31,22 @@ namespace Eigen { * * \sa Tensor */ -template class TensorStorage; +template class TensorStorage; // Pure fixed-size storage -template -class TensorStorage +template +class TensorStorage { private: + static const std::size_t Size = FixedDimensions::total_size; + EIGEN_ALIGN_DEFAULT T m_data[Size]; FixedDimensions m_dimensions; public: EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorStorage() { - EIGEN_STATIC_ASSERT(Size == FixedDimensions::total_size, YOU_MADE_A_PROGRAMMING_MISTAKE) } EIGEN_DEVICE_FUNC @@ -60,35 +62,14 @@ class TensorStorage }; - -// pure-dynamic, but without specification of all dimensions explicitly -template -class TensorStorage - : public TensorStorage(Options_ & Index32Bit)>::type, NumIndices_, Dynamic>::type> -{ - typedef typename internal::compute_index_type(Options_ & Index32Bit)>::type Index; - typedef DSizes Dimensions; - typedef TensorStorage::type> Base_; - - public: - EIGEN_DEVICE_FUNC TensorStorage() { } - EIGEN_DEVICE_FUNC TensorStorage(const TensorStorage& other) : Base_(other) { } - - EIGEN_DEVICE_FUNC TensorStorage(internal::constructor_without_unaligned_array_assert) : Base_(internal::constructor_without_unaligned_array_assert()) {} - EIGEN_DEVICE_FUNC TensorStorage(Index size, const array& dimensions) : Base_(size, dimensions) {} - - // TensorStorage& operator=(const TensorStorage&) = default; -}; - // pure dynamic -template -class TensorStorage(Options_ & Index32Bit)>::type, NumIndices_, Dynamic>::type> +template +class TensorStorage, Options_> { public: - typedef typename internal::compute_index_type(Options_&Index32Bit)>::type Index; - typedef DSizes Dimensions; - - typedef TensorStorage::type> Self_; + typedef IndexType Index; + typedef DSizes Dimensions; + typedef TensorStorage, Options_> Self; EIGEN_DEVICE_FUNC TensorStorage() : m_data(0), m_dimensions() {} EIGEN_DEVICE_FUNC TensorStorage(internal::constructor_without_unaligned_array_assert) @@ -97,23 +78,23 @@ class TensorStorage(size)), m_dimensions(dimensions) { EIGEN_INTERNAL_TENSOR_STORAGE_CTOR_PLUGIN } - EIGEN_DEVICE_FUNC TensorStorage(const Self_& other) + EIGEN_DEVICE_FUNC TensorStorage(const Self& other) : m_data(internal::conditional_aligned_new_auto(internal::array_prod(other.m_dimensions))) , m_dimensions(other.m_dimensions) { internal::smart_copy(other.m_data, other.m_data+internal::array_prod(other.m_dimensions), m_data); } - EIGEN_DEVICE_FUNC Self_& operator=(const Self_& other) + EIGEN_DEVICE_FUNC Self& operator=(const Self& other) { if (this != &other) { - Self_ tmp(other); + Self tmp(other); this->swap(tmp); } return *this; } EIGEN_DEVICE_FUNC ~TensorStorage() { internal::conditional_aligned_delete_auto(m_data, internal::array_prod(m_dimensions)); } - EIGEN_DEVICE_FUNC void swap(Self_& other) + EIGEN_DEVICE_FUNC void swap(Self& other) { std::swap(m_data,other.m_data); std::swap(m_dimensions,other.m_dimensions); } EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const {return m_dimensions;} diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorTraits.h b/unsupported/Eigen/CXX11/src/Tensor/TensorTraits.h index 0745b1742..ba09298c3 100644 --- a/unsupported/Eigen/CXX11/src/Tensor/TensorTraits.h +++ b/unsupported/Eigen/CXX11/src/Tensor/TensorTraits.h @@ -43,24 +43,13 @@ class compute_tensor_flags enum { ret = packet_access_bit | aligned_bit}; }; -template -struct compute_index_type { - typedef DenseIndex type; -}; -template<> -struct compute_index_type { - typedef int type; -}; - - - -template -struct traits > +template +struct traits > { typedef Scalar_ Scalar; typedef Dense StorageKind; - typedef typename compute_index_type(Options_&Index32Bit)>::type Index; + typedef IndexType_ Index; static const int NumDimensions = NumIndices_; static const int Layout = Options_ & RowMajor ? RowMajor : ColMajor; enum { @@ -70,12 +59,12 @@ struct traits > }; -template -struct traits > +template +struct traits > { typedef Scalar_ Scalar; typedef Dense StorageKind; - typedef typename compute_index_type(Options_&Index32Bit)>::type Index; + typedef IndexType_ Index; static const int NumDimensions = array_size::value; static const int Layout = Options_ & RowMajor ? RowMajor : ColMajor; enum { @@ -118,28 +107,28 @@ struct traits > }; -template -struct eval, Eigen::Dense> +template +struct eval, Eigen::Dense> { - typedef const Tensor<_Scalar, NumIndices_, Options>& type; + typedef const Tensor<_Scalar, NumIndices_, Options, IndexType_>& type; }; -template -struct eval, Eigen::Dense> +template +struct eval, Eigen::Dense> { - typedef const Tensor<_Scalar, NumIndices_, Options>& type; + typedef const Tensor<_Scalar, NumIndices_, Options, IndexType_>& type; }; -template -struct eval, Eigen::Dense> +template +struct eval, Eigen::Dense> { - typedef const TensorFixedSize& type; + typedef const TensorFixedSize& type; }; -template -struct eval, Eigen::Dense> +template +struct eval, Eigen::Dense> { - typedef const TensorFixedSize& type; + typedef const TensorFixedSize& type; }; template @@ -167,28 +156,28 @@ struct eval, Eigen::Dense> }; -template -struct nested > +template +struct nested > { - typedef const Tensor& type; + typedef const Tensor& type; }; -template -struct nested > +template +struct nested > { - typedef const Tensor& type; + typedef const Tensor& type; }; -template -struct nested > +template +struct nested > { - typedef const TensorFixedSize& type; + typedef const TensorFixedSize& type; }; -template -struct nested > +template +struct nested > { - typedef const TensorFixedSize& type; + typedef const TensorFixedSize& type; }; diff --git a/unsupported/test/cxx11_tensor_mixed_indices.cpp b/unsupported/test/cxx11_tensor_mixed_indices.cpp index 8a12f9207..72f826216 100644 --- a/unsupported/test/cxx11_tensor_mixed_indices.cpp +++ b/unsupported/test/cxx11_tensor_mixed_indices.cpp @@ -11,13 +11,11 @@ #include -using Eigen::Tensor; -using Eigen::RowMajor; static void test_simple() { - Tensor vec1({6}); - Tensor vec2({6}); + Tensor vec1({6}); + Tensor vec2({6}); vec1(0) = 4.0; vec2(0) = 0.0; vec1(1) = 8.0; vec2(1) = 1.0; @@ -27,10 +25,10 @@ static void test_simple() vec1(5) = 42.0; vec2(5) = 5.0; float data3[6]; - TensorMap> vec3(data3, 6); + TensorMap> vec3(data3, 6); vec3 = vec1.sqrt(); float data4[6]; - TensorMap> vec4(data4, 6); + TensorMap> vec4(data4, 6); vec4 = vec2.square(); VERIFY_IS_APPROX(vec3(0), sqrtf(4.0));