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