10#ifndef EIGEN_GENERAL_MATRIX_MATRIX_H
11#define EIGEN_GENERAL_MATRIX_MATRIX_H
17template<
typename _LhsScalar,
typename _RhsScalar>
class level3_blocking;
22 typename LhsScalar,
int LhsStorageOrder,
bool ConjugateLhs,
23 typename RhsScalar,
int RhsStorageOrder,
bool ConjugateRhs>
29 static EIGEN_STRONG_INLINE
void run(
30 Index rows, Index cols, Index depth,
43 ::run(cols,rows,depth,rhs,
rhsStride,lhs,
lhsStride,res,
resStride,alpha,
blocking,info);
59static void run(Index rows, Index cols, Index depth,
75 Index mc = (std::min)(rows,
blocking.mc());
76 Index nc = (std::min)(cols,
blocking.nc());
82#ifdef EIGEN_HAS_OPENMP
86 Index
tid = omp_get_thread_num();
87 Index threads = omp_get_num_threads();
89 LhsScalar* blockA =
blocking.blockA();
90 eigen_internal_assert(blockA!=0);
92 std::size_t
sizeB = kc*nc;
93 ei_declare_aligned_stack_constructed_variable(RhsScalar, blockB,
sizeB, 0);
96 for(Index k=0; k<depth; k+=kc)
98 const Index
actual_kc = (std::min)(k+kc,depth)-k;
110 while(info[
tid].users!=0) {}
111 info[
tid].users += threads;
119 for(Index shift=0; shift<threads; ++shift)
121 Index i = (
tid+shift)%threads;
127 while(info[i].sync!=k) {
131 gebp(res.getSubMapper(info[i].lhs_start, 0), blockA+info[i].lhs_start*
actual_kc, blockB, info[i].lhs_length,
actual_kc, nc, alpha);
135 for(Index
j=nc;
j<cols;
j+=nc)
150 for(Index i=0; i<threads; ++i)
159 EIGEN_UNUSED_VARIABLE(info);
162 std::size_t
sizeA = kc*mc;
163 std::size_t
sizeB = kc*nc;
165 ei_declare_aligned_stack_constructed_variable(LhsScalar, blockA,
sizeA,
blocking.blockA());
166 ei_declare_aligned_stack_constructed_variable(RhsScalar, blockB,
sizeB,
blocking.blockB());
168 const bool pack_rhs_once = mc!=rows && kc==depth && nc==cols;
171 for(Index
i2=0;
i2<rows;
i2+=mc)
175 for(Index
k2=0;
k2<depth;
k2+=kc)
186 for(Index
j2=0;
j2<cols;
j2+=nc)
211template<
typename Scalar,
typename Index,
typename Gemm,
typename Lhs,
typename Rhs,
typename Dest,
typename BlockingType>
218 void initParallelSession(Index num_threads)
const
220 m_blocking.initParallel(m_lhs.rows(), m_rhs.cols(), m_lhs.cols(), num_threads);
221 m_blocking.allocateA();
229 Gemm::run(rows, cols, m_lhs.cols(),
230 &m_lhs.coeffRef(row,0), m_lhs.outerStride(),
231 &m_rhs.coeffRef(0,col), m_rhs.outerStride(),
232 (Scalar*)&(m_dest.coeffRef(row,col)), m_dest.outerStride(),
233 m_actualAlpha, m_blocking, info);
236 typedef typename Gemm::Traits Traits;
242 Scalar m_actualAlpha;
246template<
int StorageOrder,
typename LhsScalar,
typename RhsScalar,
int MaxRows,
int MaxCols,
int MaxDepth,
int KcFactor=1,
249template<
typename _LhsScalar,
typename _RhsScalar>
266 : m_blockA(0), m_blockB(0), m_mc(0), m_nc(0), m_kc(0)
269 inline Index mc()
const {
return m_mc; }
270 inline Index nc()
const {
return m_nc; }
271 inline Index kc()
const {
return m_kc; }
273 inline LhsScalar* blockA() {
return m_blockA; }
274 inline RhsScalar* blockB() {
return m_blockB; }
277template<
int StorageOrder,
typename _LhsScalar,
typename _RhsScalar,
int MaxRows,
int MaxCols,
int MaxDepth,
int KcFactor>
280 typename conditional<StorageOrder==RowMajor,_RhsScalar,_LhsScalar>::type,
281 typename conditional<StorageOrder==RowMajor,_LhsScalar,_RhsScalar>::type>
285 ActualRows =
Transpose ? MaxCols : MaxRows,
286 ActualCols =
Transpose ? MaxRows : MaxCols
292 SizeA = ActualRows * MaxDepth,
293 SizeB = ActualCols * MaxDepth
296#if EIGEN_MAX_STATIC_ALIGN_BYTES >= EIGEN_DEFAULT_ALIGN_BYTES
297 EIGEN_ALIGN_MAX
LhsScalar m_staticA[SizeA];
298 EIGEN_ALIGN_MAX
RhsScalar m_staticB[SizeB];
300 EIGEN_ALIGN_MAX
char m_staticA[SizeA *
sizeof(
LhsScalar) + EIGEN_DEFAULT_ALIGN_BYTES-1];
301 EIGEN_ALIGN_MAX
char m_staticB[SizeB *
sizeof(
RhsScalar) + EIGEN_DEFAULT_ALIGN_BYTES-1];
308 this->m_mc = ActualRows;
309 this->m_nc = ActualCols;
310 this->m_kc = MaxDepth;
311#if EIGEN_MAX_STATIC_ALIGN_BYTES >= EIGEN_DEFAULT_ALIGN_BYTES
312 this->m_blockA = m_staticA;
313 this->m_blockB = m_staticB;
315 this->m_blockA =
reinterpret_cast<LhsScalar*
>((std::size_t(m_staticA) + (EIGEN_DEFAULT_ALIGN_BYTES-1)) &
~std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1));
316 this->m_blockB =
reinterpret_cast<RhsScalar*
>((std::size_t(m_staticB) + (EIGEN_DEFAULT_ALIGN_BYTES-1)) &
~std::size_t(EIGEN_DEFAULT_ALIGN_BYTES-1));
320 void initParallel(Index, Index, Index, Index)
323 inline void allocateA() {}
324 inline void allocateB() {}
325 inline void allocateAll() {}
328template<
int StorageOrder,
typename _LhsScalar,
typename _RhsScalar,
int MaxRows,
int MaxCols,
int MaxDepth,
int KcFactor>
331 typename conditional<StorageOrder==RowMajor,_RhsScalar,_LhsScalar>::type,
332 typename conditional<StorageOrder==RowMajor,_LhsScalar,_RhsScalar>::type>
337 typedef typename conditional<Transpose,_RhsScalar,_LhsScalar>::type LhsScalar;
338 typedef typename conditional<Transpose,_LhsScalar,_RhsScalar>::type RhsScalar;
358 Index m = this->m_mc;
359 Index n = this->m_nc;
363 m_sizeA = this->m_mc * this->m_kc;
364 m_sizeB = this->m_kc * this->m_nc;
367 void initParallel(Index rows, Index cols, Index depth, Index num_threads)
373 eigen_internal_assert(this->m_blockA==0 && this->m_blockB==0);
374 Index m = this->m_mc;
376 m_sizeA = this->m_mc * this->m_kc;
377 m_sizeB = this->m_kc * this->m_nc;
382 if(this->m_blockA==0)
388 if(this->m_blockB==0)
400 aligned_delete(this->m_blockA, m_sizeA);
401 aligned_delete(this->m_blockB, m_sizeB);
409template<
typename Lhs,
typename Rhs>
411 : generic_product_impl_base<Lhs,Rhs,generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,GemmProduct> >
414 typedef typename Lhs::Scalar LhsScalar;
415 typedef typename Rhs::Scalar RhsScalar;
418 typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
422 typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
426 MaxDepthAtCompileTime = EIGEN_SIZE_MIN_PREFER_FIXED(Lhs::MaxColsAtCompileTime,Rhs::MaxRowsAtCompileTime)
429 typedef generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,CoeffBasedProductMode> lazyproduct;
431 template<
typename Dst>
432 static void evalTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs)
434 if((rhs.rows()+dst.rows()+dst.cols())<20 && rhs.rows()>0)
435 lazyproduct::evalTo(dst, lhs, rhs);
439 scaleAndAddTo(dst, lhs, rhs, Scalar(1));
443 template<
typename Dst>
444 static void addTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs)
446 if((rhs.rows()+dst.rows()+dst.cols())<20 && rhs.rows()>0)
447 lazyproduct::addTo(dst, lhs, rhs);
449 scaleAndAddTo(dst,lhs, rhs, Scalar(1));
452 template<
typename Dst>
453 static void subTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs)
455 if((rhs.rows()+dst.rows()+dst.cols())<20 && rhs.rows()>0)
456 lazyproduct::subTo(dst, lhs, rhs);
458 scaleAndAddTo(dst, lhs, rhs, Scalar(-1));
461 template<
typename Dest>
462 static void scaleAndAddTo(Dest& dst,
const Lhs& a_lhs,
const Rhs& a_rhs,
const Scalar& alpha)
464 eigen_assert(dst.rows()==a_lhs.rows() && dst.cols()==a_rhs.cols());
465 if(a_lhs.cols()==0 || a_lhs.rows()==0 || a_rhs.cols()==0)
468 typename internal::add_const_on_value_type<ActualLhsType>::type lhs = LhsBlasTraits::extract(a_lhs);
469 typename internal::add_const_on_value_type<ActualRhsType>::type rhs = RhsBlasTraits::extract(a_rhs);
471 Scalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(a_lhs)
472 * RhsBlasTraits::extractScalarFactor(a_rhs);
475 Dest::MaxRowsAtCompileTime,Dest::MaxColsAtCompileTime,MaxDepthAtCompileTime> BlockingType;
477 typedef internal::gemm_functor<
479 internal::general_matrix_matrix_product<
484 ActualLhsTypeCleaned, ActualRhsTypeCleaned, Dest, BlockingType> GemmFunctor;
486 BlockingType blocking(dst.rows(), dst.cols(), lhs.cols(), 1,
true);
487 internal::parallelize_gemm<(Dest::MaxRowsAtCompileTime>32 || Dest::MaxRowsAtCompileTime==Dynamic)>
488 (GemmFunctor(lhs, rhs, dst, actualAlpha, blocking), a_lhs.rows(), a_rhs.cols(), Dest::Flags&
RowMajorBit);
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:111
Pseudo expression representing a solving operation.
Definition Solve.h:63
Expression of the transpose of a matrix.
Definition Transpose.h:55
Definition GeneralMatrixMatrix.h:247
Definition GeneralMatrixMatrix.h:251
@ ColMajor
Storage order is column major (see TopicStorageOrders).
Definition Constants.h:320
@ RowMajor
Storage order is row major (see TopicStorageOrders).
Definition Constants.h:322
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
Definition Constants.h:511
Definition BlasUtil.h:257
Definition GeneralMatrixMatrix.h:213