Eigen  3.3.0
GeneralProduct.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_GENERAL_PRODUCT_H
12 #define EIGEN_GENERAL_PRODUCT_H
13 
14 namespace Eigen {
15 
16 enum {
17  Large = 2,
18  Small = 3
19 };
20 
21 namespace internal {
22 
23 template<int Rows, int Cols, int Depth> struct product_type_selector;
24 
25 template<int Size, int MaxSize> struct product_size_category
26 {
27  enum { is_large = MaxSize == Dynamic ||
28  Size >= EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD,
29  value = is_large ? Large
30  : Size == 1 ? 1
31  : Small
32  };
33 };
34 
35 template<typename Lhs, typename Rhs> struct product_type
36 {
37  typedef typename remove_all<Lhs>::type _Lhs;
38  typedef typename remove_all<Rhs>::type _Rhs;
39  enum {
40  MaxRows = traits<_Lhs>::MaxRowsAtCompileTime,
41  Rows = traits<_Lhs>::RowsAtCompileTime,
42  MaxCols = traits<_Rhs>::MaxColsAtCompileTime,
43  Cols = traits<_Rhs>::ColsAtCompileTime,
44  MaxDepth = EIGEN_SIZE_MIN_PREFER_FIXED(traits<_Lhs>::MaxColsAtCompileTime,
45  traits<_Rhs>::MaxRowsAtCompileTime),
46  Depth = EIGEN_SIZE_MIN_PREFER_FIXED(traits<_Lhs>::ColsAtCompileTime,
47  traits<_Rhs>::RowsAtCompileTime)
48  };
49 
50  // the splitting into different lines of code here, introducing the _select enums and the typedef below,
51  // is to work around an internal compiler error with gcc 4.1 and 4.2.
52 private:
53  enum {
54  rows_select = product_size_category<Rows,MaxRows>::value,
55  cols_select = product_size_category<Cols,MaxCols>::value,
56  depth_select = product_size_category<Depth,MaxDepth>::value
57  };
58  typedef product_type_selector<rows_select, cols_select, depth_select> selector;
59 
60 public:
61  enum {
62  value = selector::ret,
63  ret = selector::ret
64  };
65 #ifdef EIGEN_DEBUG_PRODUCT
66  static void debug()
67  {
68  EIGEN_DEBUG_VAR(Rows);
69  EIGEN_DEBUG_VAR(Cols);
70  EIGEN_DEBUG_VAR(Depth);
71  EIGEN_DEBUG_VAR(rows_select);
72  EIGEN_DEBUG_VAR(cols_select);
73  EIGEN_DEBUG_VAR(depth_select);
74  EIGEN_DEBUG_VAR(value);
75  }
76 #endif
77 };
78 
79 /* The following allows to select the kind of product at compile time
80  * based on the three dimensions of the product.
81  * This is a compile time mapping from {1,Small,Large}^3 -> {product types} */
82 // FIXME I'm not sure the current mapping is the ideal one.
83 template<int M, int N> struct product_type_selector<M,N,1> { enum { ret = OuterProduct }; };
84 template<int M> struct product_type_selector<M, 1, 1> { enum { ret = LazyCoeffBasedProductMode }; };
85 template<int N> struct product_type_selector<1, N, 1> { enum { ret = LazyCoeffBasedProductMode }; };
86 template<int Depth> struct product_type_selector<1, 1, Depth> { enum { ret = InnerProduct }; };
87 template<> struct product_type_selector<1, 1, 1> { enum { ret = InnerProduct }; };
88 template<> struct product_type_selector<Small,1, Small> { enum { ret = CoeffBasedProductMode }; };
89 template<> struct product_type_selector<1, Small,Small> { enum { ret = CoeffBasedProductMode }; };
90 template<> struct product_type_selector<Small,Small,Small> { enum { ret = CoeffBasedProductMode }; };
91 template<> struct product_type_selector<Small, Small, 1> { enum { ret = LazyCoeffBasedProductMode }; };
92 template<> struct product_type_selector<Small, Large, 1> { enum { ret = LazyCoeffBasedProductMode }; };
93 template<> struct product_type_selector<Large, Small, 1> { enum { ret = LazyCoeffBasedProductMode }; };
94 template<> struct product_type_selector<1, Large,Small> { enum { ret = CoeffBasedProductMode }; };
95 template<> struct product_type_selector<1, Large,Large> { enum { ret = GemvProduct }; };
96 template<> struct product_type_selector<1, Small,Large> { enum { ret = CoeffBasedProductMode }; };
97 template<> struct product_type_selector<Large,1, Small> { enum { ret = CoeffBasedProductMode }; };
98 template<> struct product_type_selector<Large,1, Large> { enum { ret = GemvProduct }; };
99 template<> struct product_type_selector<Small,1, Large> { enum { ret = CoeffBasedProductMode }; };
100 template<> struct product_type_selector<Small,Small,Large> { enum { ret = GemmProduct }; };
101 template<> struct product_type_selector<Large,Small,Large> { enum { ret = GemmProduct }; };
102 template<> struct product_type_selector<Small,Large,Large> { enum { ret = GemmProduct }; };
103 template<> struct product_type_selector<Large,Large,Large> { enum { ret = GemmProduct }; };
104 template<> struct product_type_selector<Large,Small,Small> { enum { ret = CoeffBasedProductMode }; };
105 template<> struct product_type_selector<Small,Large,Small> { enum { ret = CoeffBasedProductMode }; };
106 template<> struct product_type_selector<Large,Large,Small> { enum { ret = GemmProduct }; };
107 
108 } // end namespace internal
109 
110 /***********************************************************************
111 * Implementation of Inner Vector Vector Product
112 ***********************************************************************/
113 
114 // FIXME : maybe the "inner product" could return a Scalar
115 // instead of a 1x1 matrix ??
116 // Pro: more natural for the user
117 // Cons: this could be a problem if in a meta unrolled algorithm a matrix-matrix
118 // product ends up to a row-vector times col-vector product... To tackle this use
119 // case, we could have a specialization for Block<MatrixType,1,1> with: operator=(Scalar x);
120 
121 /***********************************************************************
122 * Implementation of Outer Vector Vector Product
123 ***********************************************************************/
124 
125 /***********************************************************************
126 * Implementation of General Matrix Vector Product
127 ***********************************************************************/
128 
129 /* According to the shape/flags of the matrix we have to distinghish 3 different cases:
130  * 1 - the matrix is col-major, BLAS compatible and M is large => call fast BLAS-like colmajor routine
131  * 2 - the matrix is row-major, BLAS compatible and N is large => call fast BLAS-like rowmajor routine
132  * 3 - all other cases are handled using a simple loop along the outer-storage direction.
133  * Therefore we need a lower level meta selector.
134  * Furthermore, if the matrix is the rhs, then the product has to be transposed.
135  */
136 namespace internal {
137 
138 template<int Side, int StorageOrder, bool BlasCompatible>
139 struct gemv_dense_selector;
140 
141 } // end namespace internal
142 
143 namespace internal {
144 
145 template<typename Scalar,int Size,int MaxSize,bool Cond> struct gemv_static_vector_if;
146 
147 template<typename Scalar,int Size,int MaxSize>
148 struct gemv_static_vector_if<Scalar,Size,MaxSize,false>
149 {
150  EIGEN_STRONG_INLINE Scalar* data() { eigen_internal_assert(false && "should never be called"); return 0; }
151 };
152 
153 template<typename Scalar,int Size>
154 struct gemv_static_vector_if<Scalar,Size,Dynamic,true>
155 {
156  EIGEN_STRONG_INLINE Scalar* data() { return 0; }
157 };
158 
159 template<typename Scalar,int Size,int MaxSize>
160 struct gemv_static_vector_if<Scalar,Size,MaxSize,true>
161 {
162  enum {
163  ForceAlignment = internal::packet_traits<Scalar>::Vectorizable,
164  PacketSize = internal::packet_traits<Scalar>::size
165  };
166  #if EIGEN_MAX_STATIC_ALIGN_BYTES!=0
167  internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize),0,EIGEN_PLAIN_ENUM_MIN(AlignedMax,PacketSize)> m_data;
168  EIGEN_STRONG_INLINE Scalar* data() { return m_data.array; }
169  #else
170  // Some architectures cannot align on the stack,
171  // => let's manually enforce alignment by allocating more data and return the address of the first aligned element.
172  internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize)+(ForceAlignment?EIGEN_MAX_ALIGN_BYTES:0),0> m_data;
173  EIGEN_STRONG_INLINE Scalar* data() {
174  return ForceAlignment
175  ? reinterpret_cast<Scalar*>((internal::UIntPtr(m_data.array) & ~(std::size_t(EIGEN_MAX_ALIGN_BYTES-1))) + EIGEN_MAX_ALIGN_BYTES)
176  : m_data.array;
177  }
178  #endif
179 };
180 
181 // The vector is on the left => transposition
182 template<int StorageOrder, bool BlasCompatible>
183 struct gemv_dense_selector<OnTheLeft,StorageOrder,BlasCompatible>
184 {
185  template<typename Lhs, typename Rhs, typename Dest>
186  static void run(const Lhs &lhs, const Rhs &rhs, Dest& dest, const typename Dest::Scalar& alpha)
187  {
188  Transpose<Dest> destT(dest);
189  enum { OtherStorageOrder = StorageOrder == RowMajor ? ColMajor : RowMajor };
190  gemv_dense_selector<OnTheRight,OtherStorageOrder,BlasCompatible>
191  ::run(rhs.transpose(), lhs.transpose(), destT, alpha);
192  }
193 };
194 
195 template<> struct gemv_dense_selector<OnTheRight,ColMajor,true>
196 {
197  template<typename Lhs, typename Rhs, typename Dest>
198  static inline void run(const Lhs &lhs, const Rhs &rhs, Dest& dest, const typename Dest::Scalar& alpha)
199  {
200  typedef typename Lhs::Scalar LhsScalar;
201  typedef typename Rhs::Scalar RhsScalar;
202  typedef typename Dest::Scalar ResScalar;
203  typedef typename Dest::RealScalar RealScalar;
204 
205  typedef internal::blas_traits<Lhs> LhsBlasTraits;
206  typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
207  typedef internal::blas_traits<Rhs> RhsBlasTraits;
208  typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
209 
210  typedef Map<Matrix<ResScalar,Dynamic,1>, EIGEN_PLAIN_ENUM_MIN(AlignedMax,internal::packet_traits<ResScalar>::size)> MappedDest;
211 
212  ActualLhsType actualLhs = LhsBlasTraits::extract(lhs);
213  ActualRhsType actualRhs = RhsBlasTraits::extract(rhs);
214 
215  ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(lhs)
216  * RhsBlasTraits::extractScalarFactor(rhs);
217 
218  // make sure Dest is a compile-time vector type (bug 1166)
219  typedef typename conditional<Dest::IsVectorAtCompileTime, Dest, typename Dest::ColXpr>::type ActualDest;
220 
221  enum {
222  // FIXME find a way to allow an inner stride on the result if packet_traits<Scalar>::size==1
223  // on, the other hand it is good for the cache to pack the vector anyways...
224  EvalToDestAtCompileTime = (ActualDest::InnerStrideAtCompileTime==1),
225  ComplexByReal = (NumTraits<LhsScalar>::IsComplex) && (!NumTraits<RhsScalar>::IsComplex),
226  MightCannotUseDest = (ActualDest::InnerStrideAtCompileTime!=1) || ComplexByReal
227  };
228 
229  gemv_static_vector_if<ResScalar,ActualDest::SizeAtCompileTime,ActualDest::MaxSizeAtCompileTime,MightCannotUseDest> static_dest;
230 
231  const bool alphaIsCompatible = (!ComplexByReal) || (numext::imag(actualAlpha)==RealScalar(0));
232  const bool evalToDest = EvalToDestAtCompileTime && alphaIsCompatible;
233 
234  RhsScalar compatibleAlpha = get_factor<ResScalar,RhsScalar>::run(actualAlpha);
235 
236  ei_declare_aligned_stack_constructed_variable(ResScalar,actualDestPtr,dest.size(),
237  evalToDest ? dest.data() : static_dest.data());
238 
239  if(!evalToDest)
240  {
241  #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
242  Index size = dest.size();
243  EIGEN_DENSE_STORAGE_CTOR_PLUGIN
244  #endif
245  if(!alphaIsCompatible)
246  {
247  MappedDest(actualDestPtr, dest.size()).setZero();
248  compatibleAlpha = RhsScalar(1);
249  }
250  else
251  MappedDest(actualDestPtr, dest.size()) = dest;
252  }
253 
254  typedef const_blas_data_mapper<LhsScalar,Index,ColMajor> LhsMapper;
255  typedef const_blas_data_mapper<RhsScalar,Index,RowMajor> RhsMapper;
256  general_matrix_vector_product
257  <Index,LhsScalar,LhsMapper,ColMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsMapper,RhsBlasTraits::NeedToConjugate>::run(
258  actualLhs.rows(), actualLhs.cols(),
259  LhsMapper(actualLhs.data(), actualLhs.outerStride()),
260  RhsMapper(actualRhs.data(), actualRhs.innerStride()),
261  actualDestPtr, 1,
262  compatibleAlpha);
263 
264  if (!evalToDest)
265  {
266  if(!alphaIsCompatible)
267  dest += actualAlpha * MappedDest(actualDestPtr, dest.size());
268  else
269  dest = MappedDest(actualDestPtr, dest.size());
270  }
271  }
272 };
273 
274 template<> struct gemv_dense_selector<OnTheRight,RowMajor,true>
275 {
276  template<typename Lhs, typename Rhs, typename Dest>
277  static void run(const Lhs &lhs, const Rhs &rhs, Dest& dest, const typename Dest::Scalar& alpha)
278  {
279  typedef typename Lhs::Scalar LhsScalar;
280  typedef typename Rhs::Scalar RhsScalar;
281  typedef typename Dest::Scalar ResScalar;
282 
283  typedef internal::blas_traits<Lhs> LhsBlasTraits;
284  typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
285  typedef internal::blas_traits<Rhs> RhsBlasTraits;
286  typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
287  typedef typename internal::remove_all<ActualRhsType>::type ActualRhsTypeCleaned;
288 
289  typename add_const<ActualLhsType>::type actualLhs = LhsBlasTraits::extract(lhs);
290  typename add_const<ActualRhsType>::type actualRhs = RhsBlasTraits::extract(rhs);
291 
292  ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(lhs)
293  * RhsBlasTraits::extractScalarFactor(rhs);
294 
295  enum {
296  // FIXME find a way to allow an inner stride on the result if packet_traits<Scalar>::size==1
297  // on, the other hand it is good for the cache to pack the vector anyways...
298  DirectlyUseRhs = ActualRhsTypeCleaned::InnerStrideAtCompileTime==1
299  };
300 
301  gemv_static_vector_if<RhsScalar,ActualRhsTypeCleaned::SizeAtCompileTime,ActualRhsTypeCleaned::MaxSizeAtCompileTime,!DirectlyUseRhs> static_rhs;
302 
303  ei_declare_aligned_stack_constructed_variable(RhsScalar,actualRhsPtr,actualRhs.size(),
304  DirectlyUseRhs ? const_cast<RhsScalar*>(actualRhs.data()) : static_rhs.data());
305 
306  if(!DirectlyUseRhs)
307  {
308  #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
309  Index size = actualRhs.size();
310  EIGEN_DENSE_STORAGE_CTOR_PLUGIN
311  #endif
312  Map<typename ActualRhsTypeCleaned::PlainObject>(actualRhsPtr, actualRhs.size()) = actualRhs;
313  }
314 
315  typedef const_blas_data_mapper<LhsScalar,Index,RowMajor> LhsMapper;
316  typedef const_blas_data_mapper<RhsScalar,Index,ColMajor> RhsMapper;
317  general_matrix_vector_product
318  <Index,LhsScalar,LhsMapper,RowMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsMapper,RhsBlasTraits::NeedToConjugate>::run(
319  actualLhs.rows(), actualLhs.cols(),
320  LhsMapper(actualLhs.data(), actualLhs.outerStride()),
321  RhsMapper(actualRhsPtr, 1),
322  dest.data(), dest.col(0).innerStride(), //NOTE if dest is not a vector at compile-time, then dest.innerStride() might be wrong. (bug 1166)
323  actualAlpha);
324  }
325 };
326 
327 template<> struct gemv_dense_selector<OnTheRight,ColMajor,false>
328 {
329  template<typename Lhs, typename Rhs, typename Dest>
330  static void run(const Lhs &lhs, const Rhs &rhs, Dest& dest, const typename Dest::Scalar& alpha)
331  {
332  // TODO if rhs is large enough it might be beneficial to make sure that dest is sequentially stored in memory, otherwise use a temp
333  typename nested_eval<Rhs,1>::type actual_rhs(rhs);
334  const Index size = rhs.rows();
335  for(Index k=0; k<size; ++k)
336  dest += (alpha*actual_rhs.coeff(k)) * lhs.col(k);
337  }
338 };
339 
340 template<> struct gemv_dense_selector<OnTheRight,RowMajor,false>
341 {
342  template<typename Lhs, typename Rhs, typename Dest>
343  static void run(const Lhs &lhs, const Rhs &rhs, Dest& dest, const typename Dest::Scalar& alpha)
344  {
345  typename nested_eval<Rhs,Lhs::RowsAtCompileTime>::type actual_rhs(rhs);
346  const Index rows = dest.rows();
347  for(Index i=0; i<rows; ++i)
348  dest.coeffRef(i) += alpha * (lhs.row(i).cwiseProduct(actual_rhs.transpose())).sum();
349  }
350 };
351 
352 } // end namespace internal
353 
354 /***************************************************************************
355 * Implementation of matrix base methods
356 ***************************************************************************/
357 
364 #ifndef __CUDACC__
365 
366 template<typename Derived>
367 template<typename OtherDerived>
368 inline const Product<Derived, OtherDerived>
370 {
371  // A note regarding the function declaration: In MSVC, this function will sometimes
372  // not be inlined since DenseStorage is an unwindable object for dynamic
373  // matrices and product types are holding a member to store the result.
374  // Thus it does not help tagging this function with EIGEN_STRONG_INLINE.
375  enum {
376  ProductIsValid = Derived::ColsAtCompileTime==Dynamic
377  || OtherDerived::RowsAtCompileTime==Dynamic
378  || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
379  AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
380  SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
381  };
382  // note to the lost user:
383  // * for a dot product use: v1.dot(v2)
384  // * for a coeff-wise product use: v1.cwiseProduct(v2)
385  EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
386  INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
387  EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
388  INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
389  EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
390 #ifdef EIGEN_DEBUG_PRODUCT
391  internal::product_type<Derived,OtherDerived>::debug();
392 #endif
393 
394  return Product<Derived, OtherDerived>(derived(), other.derived());
395 }
396 
397 #endif // __CUDACC__
398 
410 template<typename Derived>
411 template<typename OtherDerived>
414 {
415  enum {
416  ProductIsValid = Derived::ColsAtCompileTime==Dynamic
417  || OtherDerived::RowsAtCompileTime==Dynamic
418  || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
419  AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
420  SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
421  };
422  // note to the lost user:
423  // * for a dot product use: v1.dot(v2)
424  // * for a coeff-wise product use: v1.cwiseProduct(v2)
425  EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
426  INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
427  EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
428  INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
429  EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
430 
431  return Product<Derived,OtherDerived,LazyProduct>(derived(), other.derived());
432 }
433 
434 } // end namespace Eigen
435 
436 #endif // EIGEN_PRODUCT_H
Definition: Constants.h:320
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
Definition: Constants.h:335
Namespace containing all symbols from the Eigen library.
Definition: Core:287
Derived & derived()
Definition: EigenBase.h:44
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: Constants.h:333
Definition: Eigen_Colamd.h:50
Definition: Constants.h:322
const int Dynamic
Definition: Constants.h:21
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const CwiseBinaryOp< internal::scalar_product_op< Scalar, T >, Derived, Constant< T > > operator*(const T &scalar) const
const Product< Derived, OtherDerived, LazyProduct > lazyProduct(const MatrixBase< OtherDerived > &other) const
Definition: GeneralProduct.h:413