00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef EIGEN_GENERAL_MATRIX_MATRIX_H
00026 #define EIGEN_GENERAL_MATRIX_MATRIX_H
00027
00028 namespace internal {
00029
00030 template<typename _LhsScalar, typename _RhsScalar> class level3_blocking;
00031
00032
00033 template<
00034 typename Index,
00035 typename LhsScalar, int LhsStorageOrder, bool ConjugateLhs,
00036 typename RhsScalar, int RhsStorageOrder, bool ConjugateRhs>
00037 struct general_matrix_matrix_product<Index,LhsScalar,LhsStorageOrder,ConjugateLhs,RhsScalar,RhsStorageOrder,ConjugateRhs,RowMajor>
00038 {
00039 typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType ResScalar;
00040 static EIGEN_STRONG_INLINE void run(
00041 Index rows, Index cols, Index depth,
00042 const LhsScalar* lhs, Index lhsStride,
00043 const RhsScalar* rhs, Index rhsStride,
00044 ResScalar* res, Index resStride,
00045 ResScalar alpha,
00046 level3_blocking<RhsScalar,LhsScalar>& blocking,
00047 GemmParallelInfo<Index>* info = 0)
00048 {
00049
00050 general_matrix_matrix_product<Index,
00051 RhsScalar, RhsStorageOrder==RowMajor ? ColMajor : RowMajor, ConjugateRhs,
00052 LhsScalar, LhsStorageOrder==RowMajor ? ColMajor : RowMajor, ConjugateLhs,
00053 ColMajor>
00054 ::run(cols,rows,depth,rhs,rhsStride,lhs,lhsStride,res,resStride,alpha,blocking,info);
00055 }
00056 };
00057
00058
00059
00060 template<
00061 typename Index,
00062 typename LhsScalar, int LhsStorageOrder, bool ConjugateLhs,
00063 typename RhsScalar, int RhsStorageOrder, bool ConjugateRhs>
00064 struct general_matrix_matrix_product<Index,LhsScalar,LhsStorageOrder,ConjugateLhs,RhsScalar,RhsStorageOrder,ConjugateRhs,ColMajor>
00065 {
00066 typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType ResScalar;
00067 static void run(Index rows, Index cols, Index depth,
00068 const LhsScalar* _lhs, Index lhsStride,
00069 const RhsScalar* _rhs, Index rhsStride,
00070 ResScalar* res, Index resStride,
00071 ResScalar alpha,
00072 level3_blocking<LhsScalar,RhsScalar>& blocking,
00073 GemmParallelInfo<Index>* info = 0)
00074 {
00075 const_blas_data_mapper<LhsScalar, Index, LhsStorageOrder> lhs(_lhs,lhsStride);
00076 const_blas_data_mapper<RhsScalar, Index, RhsStorageOrder> rhs(_rhs,rhsStride);
00077
00078 typedef gebp_traits<LhsScalar,RhsScalar> Traits;
00079
00080 Index kc = blocking.kc();
00081 Index mc = (std::min)(rows,blocking.mc());
00082
00083
00084 gemm_pack_lhs<LhsScalar, Index, Traits::mr, Traits::LhsProgress, LhsStorageOrder> pack_lhs;
00085 gemm_pack_rhs<RhsScalar, Index, Traits::nr, RhsStorageOrder> pack_rhs;
00086 gebp_kernel<LhsScalar, RhsScalar, Index, Traits::mr, Traits::nr, ConjugateLhs, ConjugateRhs> gebp;
00087
00088 #ifdef EIGEN_HAS_OPENMP
00089 if(info)
00090 {
00091
00092 Index tid = omp_get_thread_num();
00093 Index threads = omp_get_num_threads();
00094
00095 std::size_t sizeA = kc*mc;
00096 std::size_t sizeW = kc*Traits::WorkSpaceFactor;
00097 ei_declare_aligned_stack_constructed_variable(LhsScalar, blockA, sizeA, 0);
00098 ei_declare_aligned_stack_constructed_variable(RhsScalar, w, sizeW, 0);
00099
00100 RhsScalar* blockB = blocking.blockB();
00101 eigen_internal_assert(blockB!=0);
00102
00103
00104 for(Index k=0; k<depth; k+=kc)
00105 {
00106 const Index actual_kc = (std::min)(k+kc,depth)-k;
00107
00108
00109
00110 pack_lhs(blockA, &lhs(0,k), lhsStride, actual_kc, mc);
00111
00112
00113
00114
00115
00116
00117
00118 while(info[tid].users!=0) {}
00119 info[tid].users += threads;
00120
00121 pack_rhs(blockB+info[tid].rhs_start*actual_kc, &rhs(k,info[tid].rhs_start), rhsStride, actual_kc, info[tid].rhs_length);
00122
00123
00124 info[tid].sync = k;
00125
00126
00127 for(Index shift=0; shift<threads; ++shift)
00128 {
00129 Index j = (tid+shift)%threads;
00130
00131
00132
00133
00134 if(shift>0)
00135 while(info[j].sync!=k) {}
00136
00137 gebp(res+info[j].rhs_start*resStride, resStride, blockA, blockB+info[j].rhs_start*actual_kc, mc, actual_kc, info[j].rhs_length, alpha, -1,-1,0,0, w);
00138 }
00139
00140
00141 for(Index i=mc; i<rows; i+=mc)
00142 {
00143 const Index actual_mc = (std::min)(i+mc,rows)-i;
00144
00145
00146 pack_lhs(blockA, &lhs(i,k), lhsStride, actual_kc, actual_mc);
00147
00148
00149 gebp(res+i, resStride, blockA, blockB, actual_mc, actual_kc, cols, alpha, -1,-1,0,0, w);
00150 }
00151
00152
00153
00154 for(Index j=0; j<threads; ++j)
00155 #pragma omp atomic
00156 --(info[j].users);
00157 }
00158 }
00159 else
00160 #endif // EIGEN_HAS_OPENMP
00161 {
00162 EIGEN_UNUSED_VARIABLE(info);
00163
00164
00165 std::size_t sizeA = kc*mc;
00166 std::size_t sizeB = kc*cols;
00167 std::size_t sizeW = kc*Traits::WorkSpaceFactor;
00168
00169 ei_declare_aligned_stack_constructed_variable(LhsScalar, blockA, sizeA, blocking.blockA());
00170 ei_declare_aligned_stack_constructed_variable(RhsScalar, blockB, sizeB, blocking.blockB());
00171 ei_declare_aligned_stack_constructed_variable(RhsScalar, blockW, sizeW, blocking.blockW());
00172
00173
00174
00175 for(Index k2=0; k2<depth; k2+=kc)
00176 {
00177 const Index actual_kc = (std::min)(k2+kc,depth)-k2;
00178
00179
00180
00181
00182
00183 pack_rhs(blockB, &rhs(k2,0), rhsStride, actual_kc, cols);
00184
00185
00186
00187
00188 for(Index i2=0; i2<rows; i2+=mc)
00189 {
00190 const Index actual_mc = (std::min)(i2+mc,rows)-i2;
00191
00192
00193
00194
00195 pack_lhs(blockA, &lhs(i2,k2), lhsStride, actual_kc, actual_mc);
00196
00197
00198 gebp(res+i2, resStride, blockA, blockB, actual_mc, actual_kc, cols, alpha, -1, -1, 0, 0, blockW);
00199
00200 }
00201 }
00202 }
00203 }
00204
00205 };
00206
00207
00208
00209
00210
00211
00212 template<typename Lhs, typename Rhs>
00213 struct traits<GeneralProduct<Lhs,Rhs,GemmProduct> >
00214 : traits<ProductBase<GeneralProduct<Lhs,Rhs,GemmProduct>, Lhs, Rhs> >
00215 {};
00216
00217 template<typename Scalar, typename Index, typename Gemm, typename Lhs, typename Rhs, typename Dest, typename BlockingType>
00218 struct gemm_functor
00219 {
00220 gemm_functor(const Lhs& lhs, const Rhs& rhs, Dest& dest, Scalar actualAlpha,
00221 BlockingType& blocking)
00222 : m_lhs(lhs), m_rhs(rhs), m_dest(dest), m_actualAlpha(actualAlpha), m_blocking(blocking)
00223 {}
00224
00225 void initParallelSession() const
00226 {
00227 m_blocking.allocateB();
00228 }
00229
00230 void operator() (Index row, Index rows, Index col=0, Index cols=-1, GemmParallelInfo<Index>* info=0) const
00231 {
00232 if(cols==-1)
00233 cols = m_rhs.cols();
00234
00235 Gemm::run(rows, cols, m_lhs.cols(),
00236 &m_lhs.coeffRef(row,0), m_lhs.outerStride(),
00237 &m_rhs.coeffRef(0,col), m_rhs.outerStride(),
00238 (Scalar*)&(m_dest.coeffRef(row,col)), m_dest.outerStride(),
00239 m_actualAlpha, m_blocking, info);
00240 }
00241
00242 protected:
00243 const Lhs& m_lhs;
00244 const Rhs& m_rhs;
00245 Dest& m_dest;
00246 Scalar m_actualAlpha;
00247 BlockingType& m_blocking;
00248 };
00249
00250 template<int StorageOrder, typename LhsScalar, typename RhsScalar, int MaxRows, int MaxCols, int MaxDepth,
00251 bool FiniteAtCompileTime = MaxRows!=Dynamic && MaxCols!=Dynamic && MaxDepth != Dynamic> class gemm_blocking_space;
00252
00253 template<typename _LhsScalar, typename _RhsScalar>
00254 class level3_blocking
00255 {
00256 typedef _LhsScalar LhsScalar;
00257 typedef _RhsScalar RhsScalar;
00258
00259 protected:
00260 LhsScalar* m_blockA;
00261 RhsScalar* m_blockB;
00262 RhsScalar* m_blockW;
00263
00264 DenseIndex m_mc;
00265 DenseIndex m_nc;
00266 DenseIndex m_kc;
00267
00268 public:
00269
00270 level3_blocking()
00271 : m_blockA(0), m_blockB(0), m_blockW(0), m_mc(0), m_nc(0), m_kc(0)
00272 {}
00273
00274 inline DenseIndex mc() const { return m_mc; }
00275 inline DenseIndex nc() const { return m_nc; }
00276 inline DenseIndex kc() const { return m_kc; }
00277
00278 inline LhsScalar* blockA() { return m_blockA; }
00279 inline RhsScalar* blockB() { return m_blockB; }
00280 inline RhsScalar* blockW() { return m_blockW; }
00281 };
00282
00283 template<int StorageOrder, typename _LhsScalar, typename _RhsScalar, int MaxRows, int MaxCols, int MaxDepth>
00284 class gemm_blocking_space<StorageOrder,_LhsScalar,_RhsScalar,MaxRows, MaxCols, MaxDepth, true>
00285 : public level3_blocking<
00286 typename conditional<StorageOrder==RowMajor,_RhsScalar,_LhsScalar>::type,
00287 typename conditional<StorageOrder==RowMajor,_LhsScalar,_RhsScalar>::type>
00288 {
00289 enum {
00290 Transpose = StorageOrder==RowMajor,
00291 ActualRows = Transpose ? MaxCols : MaxRows,
00292 ActualCols = Transpose ? MaxRows : MaxCols
00293 };
00294 typedef typename conditional<Transpose,_RhsScalar,_LhsScalar>::type LhsScalar;
00295 typedef typename conditional<Transpose,_LhsScalar,_RhsScalar>::type RhsScalar;
00296 typedef gebp_traits<LhsScalar,RhsScalar> Traits;
00297 enum {
00298 SizeA = ActualRows * MaxDepth,
00299 SizeB = ActualCols * MaxDepth,
00300 SizeW = MaxDepth * Traits::WorkSpaceFactor
00301 };
00302
00303 EIGEN_ALIGN16 LhsScalar m_staticA[SizeA];
00304 EIGEN_ALIGN16 RhsScalar m_staticB[SizeB];
00305 EIGEN_ALIGN16 RhsScalar m_staticW[SizeW];
00306
00307 public:
00308
00309 gemm_blocking_space(DenseIndex , DenseIndex , DenseIndex )
00310 {
00311 this->m_mc = ActualRows;
00312 this->m_nc = ActualCols;
00313 this->m_kc = MaxDepth;
00314 this->m_blockA = m_staticA;
00315 this->m_blockB = m_staticB;
00316 this->m_blockW = m_staticW;
00317 }
00318
00319 inline void allocateA() {}
00320 inline void allocateB() {}
00321 inline void allocateW() {}
00322 inline void allocateAll() {}
00323 };
00324
00325 template<int StorageOrder, typename _LhsScalar, typename _RhsScalar, int MaxRows, int MaxCols, int MaxDepth>
00326 class gemm_blocking_space<StorageOrder,_LhsScalar,_RhsScalar,MaxRows, MaxCols, MaxDepth, false>
00327 : public level3_blocking<
00328 typename conditional<StorageOrder==RowMajor,_RhsScalar,_LhsScalar>::type,
00329 typename conditional<StorageOrder==RowMajor,_LhsScalar,_RhsScalar>::type>
00330 {
00331 enum {
00332 Transpose = StorageOrder==RowMajor
00333 };
00334 typedef typename conditional<Transpose,_RhsScalar,_LhsScalar>::type LhsScalar;
00335 typedef typename conditional<Transpose,_LhsScalar,_RhsScalar>::type RhsScalar;
00336 typedef gebp_traits<LhsScalar,RhsScalar> Traits;
00337
00338 DenseIndex m_sizeA;
00339 DenseIndex m_sizeB;
00340 DenseIndex m_sizeW;
00341
00342 public:
00343
00344 gemm_blocking_space(DenseIndex rows, DenseIndex cols, DenseIndex depth)
00345 {
00346 this->m_mc = Transpose ? cols : rows;
00347 this->m_nc = Transpose ? rows : cols;
00348 this->m_kc = depth;
00349
00350 computeProductBlockingSizes<LhsScalar,RhsScalar>(this->m_kc, this->m_mc, this->m_nc);
00351 m_sizeA = this->m_mc * this->m_kc;
00352 m_sizeB = this->m_kc * this->m_nc;
00353 m_sizeW = this->m_kc*Traits::WorkSpaceFactor;
00354 }
00355
00356 void allocateA()
00357 {
00358 if(this->m_blockA==0)
00359 this->m_blockA = aligned_new<LhsScalar>(m_sizeA);
00360 }
00361
00362 void allocateB()
00363 {
00364 if(this->m_blockB==0)
00365 this->m_blockB = aligned_new<RhsScalar>(m_sizeB);
00366 }
00367
00368 void allocateW()
00369 {
00370 if(this->m_blockW==0)
00371 this->m_blockW = aligned_new<RhsScalar>(m_sizeW);
00372 }
00373
00374 void allocateAll()
00375 {
00376 allocateA();
00377 allocateB();
00378 allocateW();
00379 }
00380
00381 ~gemm_blocking_space()
00382 {
00383 aligned_delete(this->m_blockA, m_sizeA);
00384 aligned_delete(this->m_blockB, m_sizeB);
00385 aligned_delete(this->m_blockW, m_sizeW);
00386 }
00387 };
00388
00389 }
00390
00391 template<typename Lhs, typename Rhs>
00392 class GeneralProduct<Lhs, Rhs, GemmProduct>
00393 : public ProductBase<GeneralProduct<Lhs,Rhs,GemmProduct>, Lhs, Rhs>
00394 {
00395 enum {
00396 MaxDepthAtCompileTime = EIGEN_SIZE_MIN_PREFER_FIXED(Lhs::MaxColsAtCompileTime,Rhs::MaxRowsAtCompileTime)
00397 };
00398 public:
00399 EIGEN_PRODUCT_PUBLIC_INTERFACE(GeneralProduct)
00400
00401 typedef typename Lhs::Scalar LhsScalar;
00402 typedef typename Rhs::Scalar RhsScalar;
00403 typedef Scalar ResScalar;
00404
00405 GeneralProduct(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs)
00406 {
00407 typedef internal::scalar_product_op<LhsScalar,RhsScalar> BinOp;
00408 EIGEN_CHECK_BINARY_COMPATIBILIY(BinOp,LhsScalar,RhsScalar);
00409 }
00410
00411 template<typename Dest> void scaleAndAddTo(Dest& dst, Scalar alpha) const
00412 {
00413 eigen_assert(dst.rows()==m_lhs.rows() && dst.cols()==m_rhs.cols());
00414
00415 const ActualLhsType lhs = LhsBlasTraits::extract(m_lhs);
00416 const ActualRhsType rhs = RhsBlasTraits::extract(m_rhs);
00417
00418 Scalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(m_lhs)
00419 * RhsBlasTraits::extractScalarFactor(m_rhs);
00420
00421 typedef internal::gemm_blocking_space<(Dest::Flags&RowMajorBit) ? RowMajor : ColMajor,LhsScalar,RhsScalar,
00422 Dest::MaxRowsAtCompileTime,Dest::MaxColsAtCompileTime,MaxDepthAtCompileTime> BlockingType;
00423
00424 typedef internal::gemm_functor<
00425 Scalar, Index,
00426 internal::general_matrix_matrix_product<
00427 Index,
00428 LhsScalar, (_ActualLhsType::Flags&RowMajorBit) ? RowMajor : ColMajor, bool(LhsBlasTraits::NeedToConjugate),
00429 RhsScalar, (_ActualRhsType::Flags&RowMajorBit) ? RowMajor : ColMajor, bool(RhsBlasTraits::NeedToConjugate),
00430 (Dest::Flags&RowMajorBit) ? RowMajor : ColMajor>,
00431 _ActualLhsType, _ActualRhsType, Dest, BlockingType> GemmFunctor;
00432
00433 BlockingType blocking(dst.rows(), dst.cols(), lhs.cols());
00434
00435 internal::parallelize_gemm<(Dest::MaxRowsAtCompileTime>32 || Dest::MaxRowsAtCompileTime==Dynamic)>(GemmFunctor(lhs, rhs, dst, actualAlpha, blocking), this->rows(), this->cols(), Dest::Flags&RowMajorBit);
00436 }
00437 };
00438
00439 #endif // EIGEN_GENERAL_MATRIX_MATRIX_H