TensorInflation.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2015 Ke Yang <yangke@gmail.com>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
00011 #define EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
00012 
00013 namespace Eigen {
00014 
00022 namespace internal {
00023 template<typename Strides, typename XprType>
00024 struct traits<TensorInflationOp<Strides, XprType> > : public traits<XprType>
00025 {
00026   typedef typename XprType::Scalar Scalar;
00027   typedef traits<XprType> XprTraits;
00028   typedef typename XprTraits::StorageKind StorageKind;
00029   typedef typename XprTraits::Index Index;
00030   typedef typename XprType::Nested Nested;
00031   typedef typename remove_reference<Nested>::type _Nested;
00032   static const int NumDimensions = XprTraits::NumDimensions;
00033   static const int Layout = XprTraits::Layout;
00034 };
00035 
00036 template<typename Strides, typename XprType>
00037 struct eval<TensorInflationOp<Strides, XprType>, Eigen::Dense>
00038 {
00039   typedef const TensorInflationOp<Strides, XprType>& type;
00040 };
00041 
00042 template<typename Strides, typename XprType>
00043 struct nested<TensorInflationOp<Strides, XprType>, 1, typename eval<TensorInflationOp<Strides, XprType> >::type>
00044 {
00045   typedef TensorInflationOp<Strides, XprType> type;
00046 };
00047 
00048 }  // end namespace internal
00049 
00050 template<typename Strides, typename XprType>
00051 class TensorInflationOp : public TensorBase<TensorInflationOp<Strides, XprType>, ReadOnlyAccessors>
00052 {
00053   public:
00054   typedef typename Eigen::internal::traits<TensorInflationOp>::Scalar Scalar;
00055   typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
00056   typedef typename XprType::CoeffReturnType CoeffReturnType;
00057   typedef typename Eigen::internal::nested<TensorInflationOp>::type Nested;
00058   typedef typename Eigen::internal::traits<TensorInflationOp>::StorageKind StorageKind;
00059   typedef typename Eigen::internal::traits<TensorInflationOp>::Index Index;
00060 
00061   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorInflationOp(const XprType& expr, const Strides& strides)
00062       : m_xpr(expr), m_strides(strides) {}
00063 
00064     EIGEN_DEVICE_FUNC
00065     const Strides& strides() const { return m_strides; }
00066 
00067     EIGEN_DEVICE_FUNC
00068     const typename internal::remove_all<typename XprType::Nested>::type&
00069     expression() const { return m_xpr; }
00070 
00071   protected:
00072     typename XprType::Nested m_xpr;
00073     const Strides m_strides;
00074 };
00075 
00076 // Eval as rvalue
00077 template<typename Strides, typename ArgType, typename Device>
00078 struct TensorEvaluator<const TensorInflationOp<Strides, ArgType>, Device>
00079 {
00080   typedef TensorInflationOp<Strides, ArgType> XprType;
00081   typedef typename XprType::Index Index;
00082   static const int NumDims = internal::array_size<typename TensorEvaluator<ArgType, Device>::Dimensions>::value;
00083   typedef DSizes<Index, NumDims> Dimensions;
00084   typedef typename XprType::Scalar Scalar;
00085   typedef typename XprType::CoeffReturnType CoeffReturnType;
00086   typedef typename PacketType<CoeffReturnType, Device>::type PacketReturnType;
00087   static const int PacketSize = internal::unpacket_traits<PacketReturnType>::size;
00088 
00089   enum {
00090     IsAligned = /*TensorEvaluator<ArgType, Device>::IsAligned*/ false,
00091     PacketAccess = TensorEvaluator<ArgType, Device>::PacketAccess,
00092     BlockAccess = false,
00093     Layout = TensorEvaluator<ArgType, Device>::Layout,
00094     CoordAccess = false,  // to be implemented
00095     RawAccess = false
00096   };
00097 
00098   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorEvaluator(const XprType& op, const Device& device)
00099       : m_impl(op.expression(), device), m_strides(op.strides())
00100   {
00101     m_dimensions = m_impl.dimensions();
00102     // Expand each dimension to the inflated dimension.
00103     for (int i = 0; i < NumDims; ++i) {
00104       m_dimensions[i] = (m_dimensions[i] - 1) * op.strides()[i] + 1;
00105     }
00106 
00107     // Remember the strides for fast division.
00108     for (int i = 0; i < NumDims; ++i) {
00109       m_fastStrides[i] = internal::TensorIntDivisor<Index>(m_strides[i]);
00110     }
00111 
00112     const typename TensorEvaluator<ArgType, Device>::Dimensions& input_dims = m_impl.dimensions();
00113     if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
00114       m_outputStrides[0] = 1;
00115       m_inputStrides[0] = 1;
00116       for (int i = 1; i < NumDims; ++i) {
00117         m_outputStrides[i] = m_outputStrides[i-1] * m_dimensions[i-1];
00118         m_inputStrides[i] = m_inputStrides[i-1] * input_dims[i-1];
00119       }
00120     } else {  // RowMajor
00121       m_outputStrides[NumDims-1] = 1;
00122       m_inputStrides[NumDims-1] = 1;
00123       for (int i = NumDims - 2; i >= 0; --i) {
00124         m_outputStrides[i] = m_outputStrides[i+1] * m_dimensions[i+1];
00125         m_inputStrides[i] = m_inputStrides[i+1] * input_dims[i+1];
00126       }
00127     }
00128   }
00129 
00130   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Dimensions& dimensions() const { return m_dimensions; }
00131 
00132   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool evalSubExprsIfNeeded(Scalar* /*data*/) {
00133     m_impl.evalSubExprsIfNeeded(NULL);
00134     return true;
00135   }
00136   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void cleanup() {
00137     m_impl.cleanup();
00138   }
00139 
00140   // Computes the input index given the output index. Returns true if the output
00141   // index doesn't fall into a hole.
00142   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE bool getInputIndex(Index index, Index* inputIndex) const
00143   {
00144     eigen_assert(index < dimensions().TotalSize());
00145     *inputIndex = 0;
00146     if (static_cast<int>(Layout) == static_cast<int>(ColMajor)) {
00147       for (int i = NumDims - 1; i > 0; --i) {
00148         const Index idx = index / m_outputStrides[i];
00149         if (idx != idx / m_fastStrides[i] * m_strides[i]) {
00150           return false;
00151         }
00152         *inputIndex += idx / m_strides[i] * m_inputStrides[i];
00153         index -= idx * m_outputStrides[i];
00154       }
00155       if (index != index / m_fastStrides[0] * m_strides[0]) {
00156         return false;
00157       }
00158       *inputIndex += index / m_strides[0];
00159       return true;
00160     } else {
00161       for (int i = 0; i < NumDims - 1; ++i) {
00162         const Index idx = index / m_outputStrides[i];
00163         if (idx != idx / m_fastStrides[i] * m_strides[i]) {
00164           return false;
00165         }
00166         *inputIndex += idx / m_strides[i] * m_inputStrides[i];
00167         index -= idx * m_outputStrides[i];
00168       }
00169       if (index != index / m_fastStrides[NumDims-1] * m_strides[NumDims-1]) {
00170         return false;
00171       }
00172       *inputIndex += index / m_strides[NumDims - 1];
00173     }
00174     return true;
00175   }
00176 
00177   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
00178   {
00179     Index inputIndex = 0;
00180     if (getInputIndex(index, &inputIndex)) {
00181      return m_impl.coeff(inputIndex);
00182     } else {
00183      return Scalar(0);
00184     }
00185   }
00186 
00187   // TODO(yangke): optimize this function so that we can detect and produce
00188   // all-zero packets
00189   template<int LoadMode>
00190   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
00191   {
00192     EIGEN_STATIC_ASSERT((PacketSize > 1), YOU_MADE_A_PROGRAMMING_MISTAKE)
00193     eigen_assert(index+PacketSize-1 < dimensions().TotalSize());
00194 
00195     EIGEN_ALIGN_MAX typename internal::remove_const<CoeffReturnType>::type values[PacketSize];
00196     for (int i = 0; i < PacketSize; ++i) {
00197       values[i] = coeff(index+i);
00198     }
00199     PacketReturnType rslt = internal::pload<PacketReturnType>(values);
00200     return rslt;
00201   }
00202 
00203   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE TensorOpCost costPerCoeff(bool vectorized) const {
00204     const double compute_cost = NumDims * (3 * TensorOpCost::DivCost<Index>() +
00205                                            3 * TensorOpCost::MulCost<Index>() +
00206                                            2 * TensorOpCost::AddCost<Index>());
00207     const double input_size = m_impl.dimensions().TotalSize();
00208     const double output_size = m_dimensions.TotalSize();
00209     if (output_size == 0)
00210       return TensorOpCost();
00211     return m_impl.costPerCoeff(vectorized) +
00212            TensorOpCost(sizeof(CoeffReturnType) * input_size / output_size, 0,
00213                         compute_cost, vectorized, PacketSize);
00214   }
00215 
00216   EIGEN_DEVICE_FUNC Scalar* data() const { return NULL; }
00217 
00218  protected:
00219   Dimensions m_dimensions;
00220   array<Index, NumDims> m_outputStrides;
00221   array<Index, NumDims> m_inputStrides;
00222   TensorEvaluator<ArgType, Device> m_impl;
00223   const Strides m_strides;
00224   array<internal::TensorIntDivisor<Index>, NumDims> m_fastStrides;
00225 };
00226 
00227 } // end namespace Eigen
00228 
00229 #endif // EIGEN_CXX11_TENSOR_TENSOR_INFLATION_H
 All Classes Functions Variables Typedefs Enumerator