GeneralMatrixMatrix.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
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 /* Specialization for a row-major destination matrix => simple transposition of the product */
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     // transpose the product such that the result is column major
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 /*  Specialization for a col-major destination matrix
00059  *    => Blocking algorithm following Goto's paper */
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();                 // cache block size along the K direction
00081   Index mc = std::min(rows,blocking.mc());  // cache block size along the M direction
00082   //Index nc = blocking.nc(); // cache block size along the N direction
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     // this is the parallel version!
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     // For each horizontal panel of the rhs, and corresponding vertical panel of the lhs...
00104     for(Index k=0; k<depth; k+=kc)
00105     {
00106       const Index actual_kc = std::min(k+kc,depth)-k; // => rows of B', and cols of the A'
00107 
00108       // In order to reduce the chance that a thread has to wait for the other,
00109       // let's start by packing A'.
00110       pack_lhs(blockA, &lhs(0,k), lhsStride, actual_kc, mc);
00111 
00112       // Pack B_k to B' in a parallel fashion:
00113       // each thread packs the sub block B_k,j to B'_j where j is the thread id.
00114 
00115       // However, before copying to B'_j, we have to make sure that no other thread is still using it,
00116       // i.e., we test that info[tid].users equals 0.
00117       // Then, we set info[tid].users to the number of threads to mark that all other threads are going to use it.
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       // Notify the other threads that the part B'_j is ready to go.
00124       info[tid].sync = k;
00125 
00126       // Computes C_i += A' * B' per B'_j
00127       for(Index shift=0; shift<threads; ++shift)
00128       {
00129         Index j = (tid+shift)%threads;
00130 
00131         // At this point we have to make sure that B'_j has been updated by the thread j,
00132         // we use testAndSetOrdered to mimic a volatile access.
00133         // However, no need to wait for the B' part which has been updated by the current thread!
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       // Then keep going as usual with the remaining A'
00141       for(Index i=mc; i<rows; i+=mc)
00142       {
00143         const Index actual_mc = std::min(i+mc,rows)-i;
00144 
00145         // pack A_i,k to A'
00146         pack_lhs(blockA, &lhs(i,k), lhsStride, actual_kc, actual_mc);
00147 
00148         // C_i += A' * B'
00149         gebp(res+i, resStride, blockA, blockB, actual_mc, actual_kc, cols, alpha, -1,-1,0,0, w);
00150       }
00151 
00152       // Release all the sub blocks B'_j of B' for the current thread,
00153       // i.e., we simply decrement the number of users by 1
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     // this is the sequential version!
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     // For each horizontal panel of the rhs, and corresponding panel of the lhs...
00174     // (==GEMM_VAR1)
00175     for(Index k2=0; k2<depth; k2+=kc)
00176     {
00177       const Index actual_kc = std::min(k2+kc,depth)-k2;
00178 
00179       // OK, here we have selected one horizontal panel of rhs and one vertical panel of lhs.
00180       // => Pack rhs's panel into a sequential chunk of memory (L2 caching)
00181       // Note that this panel will be read as many times as the number of blocks in the lhs's
00182       // vertical panel which is, in practice, a very low number.
00183       pack_rhs(blockB, &rhs(k2,0), rhsStride, actual_kc, cols);
00184 
00185 
00186       // For each mc x kc block of the lhs's vertical panel...
00187       // (==GEPP_VAR1)
00188       for(Index i2=0; i2<rows; i2+=mc)
00189       {
00190         const Index actual_mc = std::min(i2+mc,rows)-i2;
00191 
00192         // We pack the lhs's block into a sequential chunk of memory (L1 caching)
00193         // Note that this block will be read a very high number of times, which is equal to the number of
00194         // micro vertical panel of the large rhs's panel (e.g., cols/4 times).
00195         pack_lhs(blockA, &lhs(i2,k2), lhsStride, actual_kc, actual_mc);
00196 
00197         // Everything is packed, we can now call the block * panel kernel:
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 *  Specialization of GeneralProduct<> for "large" GEMM, i.e.,
00209 *  implementation of the high level wrapper to general_matrix_matrix_product
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               /*(const Scalar*)*/&m_lhs.coeffRef(row,0), m_lhs.outerStride(),
00237               /*(const Scalar*)*/&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 /*rows*/, DenseIndex /*cols*/, DenseIndex /*depth*/)
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 } // end namespace internal
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


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:15