10 #ifndef EIGEN_GENERAL_MATRIX_MATRIX_H
11 #define EIGEN_GENERAL_MATRIX_MATRIX_H
17 template<
typename _LhsScalar,
typename _RhsScalar>
class level3_blocking;
22 typename LhsScalar,
int LhsStorageOrder,
bool ConjugateLhs,
23 typename RhsScalar,
int RhsStorageOrder,
bool ConjugateRhs>
24 struct general_matrix_matrix_product<Index,LhsScalar,LhsStorageOrder,ConjugateLhs,RhsScalar,RhsStorageOrder,ConjugateRhs,
RowMajor>
26 typedef gebp_traits<RhsScalar,LhsScalar> Traits;
28 typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType ResScalar;
29 static EIGEN_STRONG_INLINE
void run(
30 Index rows, Index cols, Index depth,
31 const LhsScalar* lhs, Index lhsStride,
32 const RhsScalar* rhs, Index rhsStride,
33 ResScalar* res, Index resStride,
35 level3_blocking<RhsScalar,LhsScalar>& blocking,
36 GemmParallelInfo<Index>* info = 0)
39 general_matrix_matrix_product<Index,
43 ::run(cols,rows,depth,rhs,rhsStride,lhs,lhsStride,res,resStride,alpha,blocking,info);
51 typename LhsScalar,
int LhsStorageOrder,
bool ConjugateLhs,
52 typename RhsScalar,
int RhsStorageOrder,
bool ConjugateRhs>
53 struct general_matrix_matrix_product<Index,LhsScalar,LhsStorageOrder,ConjugateLhs,RhsScalar,RhsStorageOrder,ConjugateRhs,
ColMajor>
56 typedef gebp_traits<LhsScalar,RhsScalar> Traits;
58 typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType ResScalar;
59 static void run(Index rows, Index cols, Index depth,
60 const LhsScalar* _lhs, Index lhsStride,
61 const RhsScalar* _rhs, Index rhsStride,
62 ResScalar* _res, Index resStride,
64 level3_blocking<LhsScalar,RhsScalar>& blocking,
65 GemmParallelInfo<Index>* info = 0)
67 typedef const_blas_data_mapper<LhsScalar, Index, LhsStorageOrder> LhsMapper;
68 typedef const_blas_data_mapper<RhsScalar, Index, RhsStorageOrder> RhsMapper;
69 typedef blas_data_mapper<typename Traits::ResScalar, Index, ColMajor> ResMapper;
70 LhsMapper lhs(_lhs,lhsStride);
71 RhsMapper rhs(_rhs,rhsStride);
72 ResMapper res(_res, resStride);
74 Index kc = blocking.kc();
75 Index mc = (std::min)(rows,blocking.mc());
76 Index nc = (std::min)(cols,blocking.nc());
78 gemm_pack_lhs<LhsScalar, Index, LhsMapper, Traits::mr, Traits::LhsProgress, LhsStorageOrder> pack_lhs;
79 gemm_pack_rhs<RhsScalar, Index, RhsMapper, Traits::nr, RhsStorageOrder> pack_rhs;
80 gebp_kernel<LhsScalar, RhsScalar, Index, ResMapper, Traits::mr, Traits::nr, ConjugateLhs, ConjugateRhs> gebp;
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;
102 pack_rhs(blockB, rhs.getSubMapper(k,0), actual_kc, nc);
110 while(info[tid].users!=0) {}
111 info[tid].users += threads;
113 pack_lhs(blockA+info[tid].lhs_start*actual_kc, lhs.getSubMapper(info[tid].lhs_start,k), actual_kc, info[tid].lhs_length);
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)
137 const Index actual_nc = (std::min)(j+nc,cols)-j;
140 pack_rhs(blockB, rhs.getSubMapper(k,j), actual_kc, actual_nc);
143 gebp(res.getSubMapper(0, j), blockA, blockB, rows, actual_kc, actual_nc, alpha);
150 for(Index i=0; i<threads; ++i)
157 #endif // EIGEN_HAS_OPENMP
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)
173 const Index actual_mc = (std::min)(i2+mc,rows)-i2;
175 for(Index k2=0; k2<depth; k2+=kc)
177 const Index actual_kc = (std::min)(k2+kc,depth)-k2;
183 pack_lhs(blockA, lhs.getSubMapper(i2,k2), actual_kc, actual_mc);
186 for(Index j2=0; j2<cols; j2+=nc)
188 const Index actual_nc = (std::min)(j2+nc,cols)-j2;
193 if((!pack_rhs_once) || i2==0)
194 pack_rhs(blockB, rhs.getSubMapper(k2,j2), actual_kc, actual_nc);
197 gebp(res.getSubMapper(i2, j2), blockA, blockB, actual_mc, actual_kc, actual_nc, alpha);
211 template<
typename Scalar,
typename Index,
typename Gemm,
typename Lhs,
typename Rhs,
typename Dest,
typename BlockingType>
214 gemm_functor(
const Lhs& lhs,
const Rhs& rhs, Dest& dest,
const Scalar& actualAlpha, BlockingType& blocking)
215 : m_lhs(lhs), m_rhs(rhs), m_dest(dest), m_actualAlpha(actualAlpha), m_blocking(blocking)
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();
224 void operator() (Index row, Index rows, Index col=0, Index cols=-1, GemmParallelInfo<Index>* info=0)
const
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;
243 BlockingType& m_blocking;
246 template<
int StorageOrder,
typename LhsScalar,
typename RhsScalar,
int MaxRows,
int MaxCols,
int MaxDepth,
int KcFactor=1,
247 bool FiniteAtCompileTime = MaxRows!=Dynamic && MaxCols!=Dynamic && MaxDepth != Dynamic>
class gemm_blocking_space;
249 template<
typename _LhsScalar,
typename _RhsScalar>
250 class level3_blocking
252 typedef _LhsScalar LhsScalar;
253 typedef _RhsScalar 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; }
277 template<
int StorageOrder,
typename _LhsScalar,
typename _RhsScalar,
int MaxRows,
int MaxCols,
int MaxDepth,
int KcFactor>
278 class gemm_blocking_space<StorageOrder,_LhsScalar,_RhsScalar,MaxRows, MaxCols, MaxDepth, KcFactor, true >
279 :
public level3_blocking<
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
288 typedef typename conditional<Transpose,_RhsScalar,_LhsScalar>::type LhsScalar;
289 typedef typename conditional<Transpose,_LhsScalar,_RhsScalar>::type RhsScalar;
290 typedef gebp_traits<LhsScalar,RhsScalar> Traits;
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];
306 gemm_blocking_space(Index , Index , Index , Index ,
bool )
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() {}
328 template<
int StorageOrder,
typename _LhsScalar,
typename _RhsScalar,
int MaxRows,
int MaxCols,
int MaxDepth,
int KcFactor>
329 class gemm_blocking_space<StorageOrder,_LhsScalar,_RhsScalar,MaxRows, MaxCols, MaxDepth, KcFactor, false>
330 :
public level3_blocking<
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;
339 typedef gebp_traits<LhsScalar,RhsScalar> Traits;
346 gemm_blocking_space(Index rows, Index cols, Index depth, Index num_threads,
bool l3_blocking)
348 this->m_mc = Transpose ? cols : rows;
349 this->m_nc = Transpose ? rows : cols;
354 computeProductBlockingSizes<LhsScalar,RhsScalar,KcFactor>(this->m_kc, this->m_mc, this->m_nc, num_threads);
358 Index m = this->m_mc;
359 Index n = this->m_nc;
360 computeProductBlockingSizes<LhsScalar,RhsScalar,KcFactor>(this->m_kc, m, n, num_threads);
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)
369 this->m_mc = Transpose ? cols : rows;
370 this->m_nc = Transpose ? rows : cols;
373 eigen_internal_assert(this->m_blockA==0 && this->m_blockB==0);
374 Index m = this->m_mc;
375 computeProductBlockingSizes<LhsScalar,RhsScalar,KcFactor>(this->m_kc, m, this->m_nc, num_threads);
376 m_sizeA = this->m_mc * this->m_kc;
377 m_sizeB = this->m_kc * this->m_nc;
382 if(this->m_blockA==0)
383 this->m_blockA = aligned_new<LhsScalar>(m_sizeA);
388 if(this->m_blockB==0)
389 this->m_blockB = aligned_new<RhsScalar>(m_sizeB);
398 ~gemm_blocking_space()
400 aligned_delete(this->m_blockA, m_sizeA);
401 aligned_delete(this->m_blockB, m_sizeB);
409 template<
typename Lhs,
typename Rhs>
410 struct generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,GemmProduct>
411 : generic_product_impl_base<Lhs,Rhs,generic_product_impl<Lhs,Rhs,DenseShape,DenseShape,GemmProduct> >
413 typedef typename Product<Lhs,Rhs>::Scalar Scalar;
414 typedef typename Lhs::Scalar LhsScalar;
415 typedef typename Rhs::Scalar RhsScalar;
417 typedef internal::blas_traits<Lhs> LhsBlasTraits;
418 typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
419 typedef typename internal::remove_all<ActualLhsType>::type ActualLhsTypeCleaned;
421 typedef internal::blas_traits<Rhs> RhsBlasTraits;
422 typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
423 typedef typename internal::remove_all<ActualRhsType>::type ActualRhsTypeCleaned;
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<
481 LhsScalar, (ActualLhsTypeCleaned::Flags&
RowMajorBit) ?
RowMajor : ColMajor,
bool(LhsBlasTraits::NeedToConjugate),
482 RhsScalar, (ActualRhsTypeCleaned::Flags&
RowMajorBit) ?
RowMajor : ColMajor,
bool(RhsBlasTraits::NeedToConjugate),
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);
496 #endif // EIGEN_GENERAL_MATRIX_MATRIX_H
Definition: Constants.h:320
Definition: StdDeque.h:58
const unsigned int RowMajorBit
Definition: Constants.h:61
Definition: Constants.h:322
Definition: Eigen_Colamd.h:54