Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorMap.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2014 Benoit Steiner <benoit.steiner.goog@gmail.com>
0005 //
0006 // This Source Code Form is subject to the terms of the Mozilla
0007 // Public License v. 2.0. If a copy of the MPL was not distributed
0008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0009 
0010 #ifndef EIGEN_CXX11_TENSOR_TENSOR_MAP_H
0011 #define EIGEN_CXX11_TENSOR_TENSOR_MAP_H
0012 
0013 namespace Eigen {
0014 
0015 // FIXME use proper doxygen documentation (e.g. \tparam MakePointer_)
0016 
0017 /** \class TensorMap
0018   * \ingroup CXX11_Tensor_Module
0019   *
0020   * \brief A tensor expression mapping an existing array of data.
0021   *
0022   */
0023 /// `template <class> class MakePointer_` is added to convert the host pointer to the device pointer.
0024 /// It is added due to the fact that for our device compiler `T*` is not allowed.
0025 /// If we wanted to use the same Evaluator functions we have to convert that type to our pointer `T`.
0026 /// This is done through our `MakePointer_` class. By default the Type in the `MakePointer_<T>` is `T*` .
0027 /// Therefore, by adding the default value, we managed to convert the type and it does not break any
0028 /// existing code as its default value is `T*`.
0029 template<typename PlainObjectType, int Options_, template <class> class MakePointer_> class TensorMap : public TensorBase<TensorMap<PlainObjectType, Options_, MakePointer_> >
0030 {
0031   public:
0032     typedef TensorMap<PlainObjectType, Options_, MakePointer_> Self;
0033     typedef TensorBase<TensorMap<PlainObjectType, Options_, MakePointer_> > Base;
0034   #ifdef EIGEN_USE_SYCL
0035     typedef  typename Eigen::internal::remove_reference<typename Eigen::internal::nested<Self>::type>::type Nested;
0036   #else
0037      typedef typename Eigen::internal::nested<Self>::type Nested;
0038   #endif
0039    typedef typename internal::traits<PlainObjectType>::StorageKind StorageKind;
0040     typedef typename internal::traits<PlainObjectType>::Index Index;
0041     typedef typename internal::traits<PlainObjectType>::Scalar Scalar;
0042     typedef typename NumTraits<Scalar>::Real RealScalar;
0043     typedef typename PlainObjectType::Base::CoeffReturnType CoeffReturnType;
0044 
0045     typedef typename MakePointer_<Scalar>::Type PointerType;
0046     typedef typename MakePointer_<Scalar>::ConstType PointerConstType;
0047 
0048     // WARN: PointerType still can be a pointer to const (const Scalar*), for
0049     // example in TensorMap<Tensor<const Scalar, ...>> expression. This type of
0050     // expression should be illegal, but adding this restriction is not possible
0051     // in practice (see https://bitbucket.org/eigen/eigen/pull-requests/488).
0052     typedef typename internal::conditional<
0053         bool(internal::is_lvalue<PlainObjectType>::value),
0054         PointerType,      // use simple pointer in lvalue expressions
0055         PointerConstType  // use const pointer in rvalue expressions
0056         >::type StoragePointerType;
0057 
0058     // If TensorMap was constructed over rvalue expression (e.g. const Tensor),
0059     // we should return a reference to const from operator() (and others), even
0060     // if TensorMap itself is not const.
0061     typedef typename internal::conditional<
0062         bool(internal::is_lvalue<PlainObjectType>::value),
0063         Scalar&,
0064         const Scalar&
0065         >::type StorageRefType;
0066 
0067     static const int Options = Options_;
0068 
0069     static const Index NumIndices = PlainObjectType::NumIndices;
0070     typedef typename PlainObjectType::Dimensions Dimensions;
0071 
0072     enum {
0073       IsAligned = ((int(Options_)&Aligned)==Aligned),
0074       Layout = PlainObjectType::Layout,
0075       CoordAccess = true,
0076       RawAccess = true
0077     };
0078 
0079     EIGEN_DEVICE_FUNC
0080     EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr) : m_data(dataPtr), m_dimensions() {
0081       // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
0082       EIGEN_STATIC_ASSERT((0 == NumIndices || NumIndices == Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
0083     }
0084 
0085 #if EIGEN_HAS_VARIADIC_TEMPLATES
0086     template<typename... IndexTypes> EIGEN_DEVICE_FUNC
0087     EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index firstDimension, IndexTypes... otherDimensions) : m_data(dataPtr), m_dimensions(firstDimension, otherDimensions...) {
0088       // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
0089       EIGEN_STATIC_ASSERT((sizeof...(otherDimensions) + 1 == NumIndices || NumIndices == Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
0090     }
0091 #else
0092     EIGEN_DEVICE_FUNC
0093     EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index firstDimension) : m_data(dataPtr), m_dimensions(firstDimension) {
0094       // The number of dimensions used to construct a tensor must be equal to the rank of the tensor.
0095       EIGEN_STATIC_ASSERT((1 == NumIndices || NumIndices == Dynamic), YOU_MADE_A_PROGRAMMING_MISTAKE)
0096     }
0097     EIGEN_DEVICE_FUNC
0098     EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2) : m_data(dataPtr), m_dimensions(dim1, dim2) {
0099       EIGEN_STATIC_ASSERT(2 == NumIndices || NumIndices == Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
0100     }
0101     EIGEN_DEVICE_FUNC
0102     EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3) : m_data(dataPtr), m_dimensions(dim1, dim2, dim3) {
0103       EIGEN_STATIC_ASSERT(3 == NumIndices || NumIndices == Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
0104     }
0105     EIGEN_DEVICE_FUNC
0106     EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3, Index dim4) : m_data(dataPtr), m_dimensions(dim1, dim2, dim3, dim4) {
0107       EIGEN_STATIC_ASSERT(4 == NumIndices || NumIndices == Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
0108     }
0109     EIGEN_DEVICE_FUNC
0110     EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, Index dim1, Index dim2, Index dim3, Index dim4, Index dim5) : m_data(dataPtr), m_dimensions(dim1, dim2, dim3, dim4, dim5) {
0111       EIGEN_STATIC_ASSERT(5 == NumIndices || NumIndices == Dynamic, YOU_MADE_A_PROGRAMMING_MISTAKE)
0112     }
0113 #endif
0114 
0115    EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, const array<Index, NumIndices>& dimensions)
0116       : m_data(dataPtr), m_dimensions(dimensions)
0117     { }
0118 
0119     template <typename Dimensions>
0120     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(StoragePointerType dataPtr, const Dimensions& dimensions)
0121       : m_data(dataPtr), m_dimensions(dimensions)
0122     { }
0123 
0124     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorMap(PlainObjectType& tensor)
0125       : m_data(tensor.data()), m_dimensions(tensor.dimensions())
0126     { }
0127 
0128     EIGEN_DEVICE_FUNC
0129     EIGEN_STRONG_INLINE Index rank() const { return m_dimensions.rank(); }
0130     EIGEN_DEVICE_FUNC
0131     EIGEN_STRONG_INLINE Index dimension(Index n) const { return m_dimensions[n]; }
0132     EIGEN_DEVICE_FUNC
0133     EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
0134     EIGEN_DEVICE_FUNC
0135     EIGEN_STRONG_INLINE Index size() const { return m_dimensions.TotalSize(); }
0136     EIGEN_DEVICE_FUNC
0137     EIGEN_STRONG_INLINE StoragePointerType data() { return m_data; }
0138     EIGEN_DEVICE_FUNC
0139     EIGEN_STRONG_INLINE StoragePointerType data() const { return m_data; }
0140 
0141     EIGEN_DEVICE_FUNC
0142     EIGEN_STRONG_INLINE StorageRefType operator()(const array<Index, NumIndices>& indices) const
0143     {
0144       //      eigen_assert(checkIndexRange(indices));
0145       if (PlainObjectType::Options&RowMajor) {
0146         const Index index = m_dimensions.IndexOfRowMajor(indices);
0147         return m_data[index];
0148       } else {
0149         const Index index = m_dimensions.IndexOfColMajor(indices);
0150         return m_data[index];
0151       }
0152     }
0153 
0154     EIGEN_DEVICE_FUNC
0155     EIGEN_STRONG_INLINE StorageRefType operator()() const
0156     {
0157       EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
0158       return m_data[0];
0159     }
0160 
0161     EIGEN_DEVICE_FUNC
0162     EIGEN_STRONG_INLINE StorageRefType operator()(Index index) const
0163     {
0164       eigen_internal_assert(index >= 0 && index < size());
0165       return m_data[index];
0166     }
0167 
0168 #if EIGEN_HAS_VARIADIC_TEMPLATES
0169     template<typename... IndexTypes> EIGEN_DEVICE_FUNC
0170     EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices) const
0171     {
0172       EIGEN_STATIC_ASSERT(sizeof...(otherIndices) + 2 == NumIndices, YOU_MADE_A_PROGRAMMING_MISTAKE)
0173       eigen_assert(internal::all((Eigen::NumTraits<Index>::highest() >= otherIndices)...));
0174       if (PlainObjectType::Options&RowMajor) {
0175         const Index index = m_dimensions.IndexOfRowMajor(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
0176         return m_data[index];
0177       } else {
0178         const Index index = m_dimensions.IndexOfColMajor(array<Index, NumIndices>{{firstIndex, secondIndex, otherIndices...}});
0179         return m_data[index];
0180       }
0181     }
0182 #else
0183     EIGEN_DEVICE_FUNC
0184     EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1) const
0185     {
0186       if (PlainObjectType::Options&RowMajor) {
0187         const Index index = i1 + i0 * m_dimensions[1];
0188         return m_data[index];
0189       } else {
0190         const Index index = i0 + i1 * m_dimensions[0];
0191         return m_data[index];
0192       }
0193     }
0194     EIGEN_DEVICE_FUNC
0195     EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2) const
0196     {
0197       if (PlainObjectType::Options&RowMajor) {
0198          const Index index = i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0);
0199          return m_data[index];
0200       } else {
0201          const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * i2);
0202         return m_data[index];
0203       }
0204     }
0205     EIGEN_DEVICE_FUNC
0206     EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3) const
0207     {
0208       if (PlainObjectType::Options&RowMajor) {
0209         const Index index = i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0));
0210         return m_data[index];
0211       } else {
0212         const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * i3));
0213         return m_data[index];
0214       }
0215     }
0216     EIGEN_DEVICE_FUNC
0217     EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3, Index i4) const
0218     {
0219       if (PlainObjectType::Options&RowMajor) {
0220         const Index index = i4 + m_dimensions[4] * (i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0)));
0221         return m_data[index];
0222       } else {
0223         const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * (i3 + m_dimensions[3] * i4)));
0224         return m_data[index];
0225       }
0226     }
0227 #endif
0228 
0229     EIGEN_DEVICE_FUNC
0230     EIGEN_STRONG_INLINE StorageRefType operator()(const array<Index, NumIndices>& indices)
0231     {
0232       //      eigen_assert(checkIndexRange(indices));
0233       if (PlainObjectType::Options&RowMajor) {
0234         const Index index = m_dimensions.IndexOfRowMajor(indices);
0235         return m_data[index];
0236       } else {
0237         const Index index = m_dimensions.IndexOfColMajor(indices);
0238         return m_data[index];
0239       }
0240     }
0241 
0242     EIGEN_DEVICE_FUNC
0243     EIGEN_STRONG_INLINE StorageRefType operator()()
0244     {
0245       EIGEN_STATIC_ASSERT(NumIndices == 0, YOU_MADE_A_PROGRAMMING_MISTAKE)
0246       return m_data[0];
0247     }
0248 
0249     EIGEN_DEVICE_FUNC
0250     EIGEN_STRONG_INLINE StorageRefType operator()(Index index)
0251     {
0252       eigen_internal_assert(index >= 0 && index < size());
0253       return m_data[index];
0254     }
0255 
0256 #if EIGEN_HAS_VARIADIC_TEMPLATES
0257     template<typename... IndexTypes> EIGEN_DEVICE_FUNC
0258     EIGEN_STRONG_INLINE StorageRefType operator()(Index firstIndex, Index secondIndex, IndexTypes... otherIndices)
0259     {
0260       static_assert(sizeof...(otherIndices) + 2 == NumIndices || NumIndices == Dynamic, "Number of indices used to access a tensor coefficient must be equal to the rank of the tensor.");
0261        eigen_assert(internal::all((Eigen::NumTraits<Index>::highest() >= otherIndices)...));
0262       const std::size_t NumDims = sizeof...(otherIndices) + 2;
0263       if (PlainObjectType::Options&RowMajor) {
0264         const Index index = m_dimensions.IndexOfRowMajor(array<Index, NumDims>{{firstIndex, secondIndex, otherIndices...}});
0265         return m_data[index];
0266       } else {
0267         const Index index = m_dimensions.IndexOfColMajor(array<Index, NumDims>{{firstIndex, secondIndex, otherIndices...}});
0268         return m_data[index];
0269       }
0270     }
0271 #else
0272     EIGEN_DEVICE_FUNC
0273     EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1)
0274     {
0275        if (PlainObjectType::Options&RowMajor) {
0276          const Index index = i1 + i0 * m_dimensions[1];
0277         return m_data[index];
0278       } else {
0279         const Index index = i0 + i1 * m_dimensions[0];
0280         return m_data[index];
0281       }
0282     }
0283     EIGEN_DEVICE_FUNC
0284     EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2)
0285     {
0286        if (PlainObjectType::Options&RowMajor) {
0287          const Index index = i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0);
0288         return m_data[index];
0289       } else {
0290          const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * i2);
0291         return m_data[index];
0292       }
0293     }
0294     EIGEN_DEVICE_FUNC
0295     EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3)
0296     {
0297       if (PlainObjectType::Options&RowMajor) {
0298         const Index index = i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0));
0299         return m_data[index];
0300       } else {
0301         const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * i3));
0302         return m_data[index];
0303       }
0304     }
0305     EIGEN_DEVICE_FUNC
0306     EIGEN_STRONG_INLINE StorageRefType operator()(Index i0, Index i1, Index i2, Index i3, Index i4)
0307     {
0308       if (PlainObjectType::Options&RowMajor) {
0309         const Index index = i4 + m_dimensions[4] * (i3 + m_dimensions[3] * (i2 + m_dimensions[2] * (i1 + m_dimensions[1] * i0)));
0310         return m_data[index];
0311       } else {
0312         const Index index = i0 + m_dimensions[0] * (i1 + m_dimensions[1] * (i2 + m_dimensions[2] * (i3 + m_dimensions[3] * i4)));
0313         return m_data[index];
0314       }
0315     }
0316 #endif
0317 
0318     EIGEN_TENSOR_INHERIT_ASSIGNMENT_OPERATORS(TensorMap)
0319 
0320   private:
0321     StoragePointerType m_data;
0322     Dimensions m_dimensions;
0323 };
0324 
0325 } // end namespace Eigen
0326 
0327 #endif // EIGEN_CXX11_TENSOR_TENSOR_MAP_H