Ref.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2012 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #ifndef EIGEN_REF_H
  10. #define EIGEN_REF_H
  11. namespace Eigen {
  12. namespace internal {
  13. template<typename _PlainObjectType, int _Options, typename _StrideType>
  14. struct traits<Ref<_PlainObjectType, _Options, _StrideType> >
  15. : public traits<Map<_PlainObjectType, _Options, _StrideType> >
  16. {
  17. typedef _PlainObjectType PlainObjectType;
  18. typedef _StrideType StrideType;
  19. enum {
  20. Options = _Options,
  21. Flags = traits<Map<_PlainObjectType, _Options, _StrideType> >::Flags | NestByRefBit,
  22. Alignment = traits<Map<_PlainObjectType, _Options, _StrideType> >::Alignment
  23. };
  24. template<typename Derived> struct match {
  25. enum {
  26. IsVectorAtCompileTime = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime,
  27. HasDirectAccess = internal::has_direct_access<Derived>::ret,
  28. StorageOrderMatch = IsVectorAtCompileTime || ((PlainObjectType::Flags&RowMajorBit)==(Derived::Flags&RowMajorBit)),
  29. InnerStrideMatch = int(StrideType::InnerStrideAtCompileTime)==int(Dynamic)
  30. || int(StrideType::InnerStrideAtCompileTime)==int(Derived::InnerStrideAtCompileTime)
  31. || (int(StrideType::InnerStrideAtCompileTime)==0 && int(Derived::InnerStrideAtCompileTime)==1),
  32. OuterStrideMatch = IsVectorAtCompileTime
  33. || int(StrideType::OuterStrideAtCompileTime)==int(Dynamic) || int(StrideType::OuterStrideAtCompileTime)==int(Derived::OuterStrideAtCompileTime),
  34. // NOTE, this indirection of evaluator<Derived>::Alignment is needed
  35. // to workaround a very strange bug in MSVC related to the instantiation
  36. // of has_*ary_operator in evaluator<CwiseNullaryOp>.
  37. // This line is surprisingly very sensitive. For instance, simply adding parenthesis
  38. // as "DerivedAlignment = (int(evaluator<Derived>::Alignment))," will make MSVC fail...
  39. DerivedAlignment = int(evaluator<Derived>::Alignment),
  40. AlignmentMatch = (int(traits<PlainObjectType>::Alignment)==int(Unaligned)) || (DerivedAlignment >= int(Alignment)), // FIXME the first condition is not very clear, it should be replaced by the required alignment
  41. ScalarTypeMatch = internal::is_same<typename PlainObjectType::Scalar, typename Derived::Scalar>::value,
  42. MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch && ScalarTypeMatch
  43. };
  44. typedef typename internal::conditional<MatchAtCompileTime,internal::true_type,internal::false_type>::type type;
  45. };
  46. };
  47. template<typename Derived>
  48. struct traits<RefBase<Derived> > : public traits<Derived> {};
  49. }
  50. template<typename Derived> class RefBase
  51. : public MapBase<Derived>
  52. {
  53. typedef typename internal::traits<Derived>::PlainObjectType PlainObjectType;
  54. typedef typename internal::traits<Derived>::StrideType StrideType;
  55. public:
  56. typedef MapBase<Derived> Base;
  57. EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
  58. EIGEN_DEVICE_FUNC inline Index innerStride() const
  59. {
  60. return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
  61. }
  62. EIGEN_DEVICE_FUNC inline Index outerStride() const
  63. {
  64. return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
  65. : IsVectorAtCompileTime ? this->size()
  66. : int(Flags)&RowMajorBit ? this->cols()
  67. : this->rows();
  68. }
  69. EIGEN_DEVICE_FUNC RefBase()
  70. : Base(0,RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime),
  71. // Stride<> does not allow default ctor for Dynamic strides, so let' initialize it with dummy values:
  72. m_stride(StrideType::OuterStrideAtCompileTime==Dynamic?0:StrideType::OuterStrideAtCompileTime,
  73. StrideType::InnerStrideAtCompileTime==Dynamic?0:StrideType::InnerStrideAtCompileTime)
  74. {}
  75. EIGEN_INHERIT_ASSIGNMENT_OPERATORS(RefBase)
  76. protected:
  77. typedef Stride<StrideType::OuterStrideAtCompileTime,StrideType::InnerStrideAtCompileTime> StrideBase;
  78. template<typename Expression>
  79. EIGEN_DEVICE_FUNC void construct(Expression& expr)
  80. {
  81. EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(PlainObjectType,Expression);
  82. if(PlainObjectType::RowsAtCompileTime==1)
  83. {
  84. eigen_assert(expr.rows()==1 || expr.cols()==1);
  85. ::new (static_cast<Base*>(this)) Base(expr.data(), 1, expr.size());
  86. }
  87. else if(PlainObjectType::ColsAtCompileTime==1)
  88. {
  89. eigen_assert(expr.rows()==1 || expr.cols()==1);
  90. ::new (static_cast<Base*>(this)) Base(expr.data(), expr.size(), 1);
  91. }
  92. else
  93. ::new (static_cast<Base*>(this)) Base(expr.data(), expr.rows(), expr.cols());
  94. if(Expression::IsVectorAtCompileTime && (!PlainObjectType::IsVectorAtCompileTime) && ((Expression::Flags&RowMajorBit)!=(PlainObjectType::Flags&RowMajorBit)))
  95. ::new (&m_stride) StrideBase(expr.innerStride(), StrideType::InnerStrideAtCompileTime==0?0:1);
  96. else
  97. ::new (&m_stride) StrideBase(StrideType::OuterStrideAtCompileTime==0?0:expr.outerStride(),
  98. StrideType::InnerStrideAtCompileTime==0?0:expr.innerStride());
  99. }
  100. StrideBase m_stride;
  101. };
  102. /** \class Ref
  103. * \ingroup Core_Module
  104. *
  105. * \brief A matrix or vector expression mapping an existing expression
  106. *
  107. * \tparam PlainObjectType the equivalent matrix type of the mapped data
  108. * \tparam Options specifies the pointer alignment in bytes. It can be: \c #Aligned128, , \c #Aligned64, \c #Aligned32, \c #Aligned16, \c #Aligned8 or \c #Unaligned.
  109. * The default is \c #Unaligned.
  110. * \tparam StrideType optionally specifies strides. By default, Ref implies a contiguous storage along the inner dimension (inner stride==1),
  111. * but accepts a variable outer stride (leading dimension).
  112. * This can be overridden by specifying strides.
  113. * The type passed here must be a specialization of the Stride template, see examples below.
  114. *
  115. * This class provides a way to write non-template functions taking Eigen objects as parameters while limiting the number of copies.
  116. * A Ref<> object can represent either a const expression or a l-value:
  117. * \code
  118. * // in-out argument:
  119. * void foo1(Ref<VectorXf> x);
  120. *
  121. * // read-only const argument:
  122. * void foo2(const Ref<const VectorXf>& x);
  123. * \endcode
  124. *
  125. * In the in-out case, the input argument must satisfy the constraints of the actual Ref<> type, otherwise a compilation issue will be triggered.
  126. * By default, a Ref<VectorXf> can reference any dense vector expression of float having a contiguous memory layout.
  127. * Likewise, a Ref<MatrixXf> can reference any column-major dense matrix expression of float whose column's elements are contiguously stored with
  128. * the possibility to have a constant space in-between each column, i.e. the inner stride must be equal to 1, but the outer stride (or leading dimension)
  129. * can be greater than the number of rows.
  130. *
  131. * In the const case, if the input expression does not match the above requirement, then it is evaluated into a temporary before being passed to the function.
  132. * Here are some examples:
  133. * \code
  134. * MatrixXf A;
  135. * VectorXf a;
  136. * foo1(a.head()); // OK
  137. * foo1(A.col()); // OK
  138. * foo1(A.row()); // Compilation error because here innerstride!=1
  139. * foo2(A.row()); // Compilation error because A.row() is a 1xN object while foo2 is expecting a Nx1 object
  140. * foo2(A.row().transpose()); // The row is copied into a contiguous temporary
  141. * foo2(2*a); // The expression is evaluated into a temporary
  142. * foo2(A.col().segment(2,4)); // No temporary
  143. * \endcode
  144. *
  145. * The range of inputs that can be referenced without temporary can be enlarged using the last two template parameters.
  146. * Here is an example accepting an innerstride!=1:
  147. * \code
  148. * // in-out argument:
  149. * void foo3(Ref<VectorXf,0,InnerStride<> > x);
  150. * foo3(A.row()); // OK
  151. * \endcode
  152. * The downside here is that the function foo3 might be significantly slower than foo1 because it won't be able to exploit vectorization, and will involve more
  153. * expensive address computations even if the input is contiguously stored in memory. To overcome this issue, one might propose to overload internally calling a
  154. * template function, e.g.:
  155. * \code
  156. * // in the .h:
  157. * void foo(const Ref<MatrixXf>& A);
  158. * void foo(const Ref<MatrixXf,0,Stride<> >& A);
  159. *
  160. * // in the .cpp:
  161. * template<typename TypeOfA> void foo_impl(const TypeOfA& A) {
  162. * ... // crazy code goes here
  163. * }
  164. * void foo(const Ref<MatrixXf>& A) { foo_impl(A); }
  165. * void foo(const Ref<MatrixXf,0,Stride<> >& A) { foo_impl(A); }
  166. * \endcode
  167. *
  168. *
  169. * \sa PlainObjectBase::Map(), \ref TopicStorageOrders
  170. */
  171. template<typename PlainObjectType, int Options, typename StrideType> class Ref
  172. : public RefBase<Ref<PlainObjectType, Options, StrideType> >
  173. {
  174. private:
  175. typedef internal::traits<Ref> Traits;
  176. template<typename Derived>
  177. EIGEN_DEVICE_FUNC inline Ref(const PlainObjectBase<Derived>& expr,
  178. typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0);
  179. public:
  180. typedef RefBase<Ref> Base;
  181. EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
  182. #ifndef EIGEN_PARSED_BY_DOXYGEN
  183. template<typename Derived>
  184. EIGEN_DEVICE_FUNC inline Ref(PlainObjectBase<Derived>& expr,
  185. typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
  186. {
  187. EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
  188. Base::construct(expr.derived());
  189. }
  190. template<typename Derived>
  191. EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
  192. typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
  193. #else
  194. /** Implicit constructor from any dense expression */
  195. template<typename Derived>
  196. inline Ref(DenseBase<Derived>& expr)
  197. #endif
  198. {
  199. EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
  200. EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
  201. EIGEN_STATIC_ASSERT(!Derived::IsPlainObjectBase,THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
  202. Base::construct(expr.const_cast_derived());
  203. }
  204. EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Ref)
  205. };
  206. // this is the const ref version
  207. template<typename TPlainObjectType, int Options, typename StrideType> class Ref<const TPlainObjectType, Options, StrideType>
  208. : public RefBase<Ref<const TPlainObjectType, Options, StrideType> >
  209. {
  210. typedef internal::traits<Ref> Traits;
  211. public:
  212. typedef RefBase<Ref> Base;
  213. EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
  214. template<typename Derived>
  215. EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
  216. typename internal::enable_if<bool(Traits::template match<Derived>::ScalarTypeMatch),Derived>::type* = 0)
  217. {
  218. // std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n";
  219. // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n";
  220. // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n";
  221. construct(expr.derived(), typename Traits::template match<Derived>::type());
  222. }
  223. EIGEN_DEVICE_FUNC inline Ref(const Ref& other) : Base(other) {
  224. // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
  225. }
  226. template<typename OtherRef>
  227. EIGEN_DEVICE_FUNC inline Ref(const RefBase<OtherRef>& other) {
  228. construct(other.derived(), typename Traits::template match<OtherRef>::type());
  229. }
  230. protected:
  231. template<typename Expression>
  232. EIGEN_DEVICE_FUNC void construct(const Expression& expr,internal::true_type)
  233. {
  234. Base::construct(expr);
  235. }
  236. template<typename Expression>
  237. EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type)
  238. {
  239. internal::call_assignment_no_alias(m_object,expr,internal::assign_op<Scalar,Scalar>());
  240. Base::construct(m_object);
  241. }
  242. protected:
  243. TPlainObjectType m_object;
  244. };
  245. } // end namespace Eigen
  246. #endif // EIGEN_REF_H