Re: [eigen] Tensor Module: Index mapping support (PATCH)

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Here is the patch I came up with today to incorporate the CustomIndex type

I used SFINAE for the new template function to be choosen correctly.
( needed some is_base_of meta, which I dont know if this is good)

but as far as I understood is the standart indices type -> array<Index,NumIndices>
(I checked the compiler fully optimizes out the additional conversion: http://pastebin.com/n1pnbAUf

I only added this to the Tensor class, not the TensorBase,
I thought before adapting also the other stuff, it would be good if you would look over it and check if that "try" is of any use :-)
I am not new to Eigen, but not aware of all the little nitty gritty details :-)

Please have a look at the patch and the added test : cxx11_tensor_customIndex.cpp

Is that appropriate? Should the conversion (with SFINAE) also be applied to all other stuff in TensorBase
like reduce , stride .... etc...
some functions are already tempalted on a generic IndexType or Dimension ... ?

Have a look at the comment on line 433 in Tensor.h , is this overload needed?

Thanks!

BR Gabriel








On 10/08/2015 07:49 PM, Benoit Steiner wrote:
It is currently not possible to use custom index types to extract coefficients from a tensor. If you want to add the functionality I'll be happy to review a patch/pull request.

On Thu, Oct 8, 2015 at 6:52 AM, Gabriel <gnuetzi@xxxxxxxxx> wrote:
If the user of the tensor component in eigen
uses its own indices lets say

Eigen::Array<int,3> idx(1,2,3);

this is currently not possible:

Eigen::Tensor<double,3> t(4,4,4)
t( idx ) =  5;


We could implement this maybe with the following wrappers
(here for the resize function and CXX11 support of course as example):

                template<typename IndexType>
            void resize( const IndexType & dimensions ){

                resize(dimensions, std::make_index_sequence<NTensorIndices>{} );

            }

            template<typename IndexType, std::size_t... Is>
            void resize(const IndexType & dimensions, std::index_sequence<Is...> )
            {
                m_tensor.resize( dimensions(Is)... );
            }



Or is this already provided in the tensor module such that one can use own index types ?


Thanks a lot :-)

BR Gabriel





# HG changeset patch
# User Gabriel Nützi <gnuetzi@xxxxxx>
# Date 1444410608 -7200
#      Fri Oct 09 19:10:08 2015 +0200
# Node ID d12a137840741d6eae7eea1ce419c36ee8db1e7a
# Parent  da340856f99bca9b46b6e6c3fca46f4a0b31e884
name changes 2
user: Gabriel Nützi <gnuetzi@xxxxxx>
branch 'default'
changed unsupported/Eigen/CXX11/src/Tensor/Tensor.h
changed unsupported/Eigen/CXX11/src/Tensor/TensorMeta.h

diff --git a/unsupported/Eigen/CXX11/src/Tensor/Tensor.h b/unsupported/Eigen/CXX11/src/Tensor/Tensor.h
--- a/unsupported/Eigen/CXX11/src/Tensor/Tensor.h
+++ b/unsupported/Eigen/CXX11/src/Tensor/Tensor.h
@@ -129,17 +129,17 @@ class Tensor : public TensorBase<Tensor<
     }
 
     // custom indices
     template<typename CustomIndices,
              EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
     >
     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(const CustomIndices & indices) const
     {
-        return coeff(internal::CustomIndices2Array<Index,NumIndices>(indices));
+        return coeff(internal::customIndices2Array<Index,NumIndices>(indices));
     }
 
 
 
     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& coeff(Index index) const
     {
       eigen_internal_assert(index >= 0 && index < size());
       return m_storage.data()[index];
@@ -163,17 +163,17 @@ class Tensor : public TensorBase<Tensor<
     }
 
     // custom indices
     template<typename CustomIndices,
              EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
              >
     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(const CustomIndices & indices)
     {
-        return coeffRef(internal::CustomIndices2Array<Index,NumIndices>(indices));
+        return coeffRef(internal::customIndices2Array<Index,NumIndices>(indices));
     }
 
 
     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
     {
       eigen_internal_assert(index >= 0 && index < size());
       return m_storage.data()[index];
     }
@@ -211,17 +211,17 @@ class Tensor : public TensorBase<Tensor<
 
     // custom indices
     template<typename CustomIndices,
              EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
     >
     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(const CustomIndices & indices) const
     {
         //eigen_assert(checkIndexRange(indices)); /* already in coeff */
-        return coeff(internal::CustomIndices2Array<Index,NumIndices>(indices));
+        return coeff(internal::customIndices2Array<Index,NumIndices>(indices));
     }
 
     // normal indices
     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar& operator()(const array<Index, NumIndices>& indices) const
     {
       //eigen_assert(checkIndexRange(indices)); /* already in coeff */
       return coeff(indices);
     }
@@ -279,17 +279,17 @@ class Tensor : public TensorBase<Tensor<
 
     // custom indices
     template<typename CustomIndices,
              EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomIndices>::value) )
     >
     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(const CustomIndices & indices)
     {
       //eigen_assert(checkIndexRange(indices)); /* already in coeff */
-      return coeffRef(internal::CustomIndices2Array<Index,NumIndices>(indices));
+      return coeffRef(internal::customIndices2Array<Index,NumIndices>(indices));
     }
 
     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar& operator()(Index index)
     {
       eigen_assert(index >= 0 && index < size());
       return coeffRef(index);
     }
 
@@ -354,17 +354,17 @@ class Tensor : public TensorBase<Tensor<
     {
       EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
     }
 
     /** Custom Dimension (delegating constructor c++11) */
     template<typename CustomDimension,
              EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomDimension>::value) )
     >
-    inline explicit Tensor(const CustomDimension & dimensions) : Tensor(internal::CustomIndices2Array<Index,NumIndices>(dimensions))
+    inline explicit Tensor(const CustomDimension & dimensions) : Tensor(internal::customIndices2Array<Index,NumIndices>(dimensions))
     {}
 
     template<typename OtherDerived>
     EIGEN_DEVICE_FUNC
     EIGEN_STRONG_INLINE Tensor(const TensorBase<OtherDerived, ReadOnlyAccessors>& other)
     {
       typedef TensorAssignOp<Tensor, const OtherDerived> Assign;
       Assign assign(*this, other.derived());
@@ -441,17 +441,17 @@ class Tensor : public TensorBase<Tensor<
 
     /** Custom Dimension */
     template<typename CustomDimension,
              EIGEN_SFINAE_ENABLE_IF( !(isOfNormalIndex<CustomDimension>::value) )
     >
     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(const CustomDimension & dimensions)
     {
       //eigen_assert(checkIndexRange(indices)); /* already in coeff */
-      return coeffRef(internal::CustomIndices2Array<Index,NumIndices>(dimensions));
+      return coeffRef(internal::customIndices2Array<Index,NumIndices>(dimensions));
     }
 
 
 #ifndef EIGEN_EMULATE_CXX11_META_H
     template <typename std::ptrdiff_t... Indices>
     EIGEN_DEVICE_FUNC
     void resize(const Sizes<Indices...>& dimensions) {
       array<Index, NumIndices> dims;
diff --git a/unsupported/Eigen/CXX11/src/Tensor/TensorMeta.h b/unsupported/Eigen/CXX11/src/Tensor/TensorMeta.h
--- a/unsupported/Eigen/CXX11/src/Tensor/TensorMeta.h
+++ b/unsupported/Eigen/CXX11/src/Tensor/TensorMeta.h
@@ -88,25 +88,25 @@ bool operator!=(const Tuple<U, V>& x, co
 
 
 
 
 namespace internal{
 
   template<typename IndexType, Index... Is>
   EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
-  array<Index,sizeof...(Is)> customIndex2Array(const IndexType & idx, numeric_list<Index,Is...>) {
+  array<Index,sizeof...(Is)> customIndices2Array(const IndexType & idx, numeric_list<Index,Is...>) {
     return { idx(Is)... };
   }
 
   /** Make an array (for index/dimensions) out of a custom index */
   template<typename Index, int NumIndices, typename IndexType>
   EIGEN_CONSTEXPR EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
-  array<Index,NumIndices> customIndex2Array(const IndexType & idx) {
-    return customIndex2Array(idx, typename gen_numeric_list<Index,NumIndices>::type{});
+  array<Index,NumIndices> customIndices2Array(const IndexType & idx) {
+    return customIndices2Array(idx, typename gen_numeric_list<Index,NumIndices>::type{});
   }
 
 
   template <typename B, typename D>
   struct is_base_of
   {
 
     typedef char (&yes)[1];


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/