MOAB  4.9.3pre
GeneralMatrixMatrixTriangular.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) 2009-2010 Gael Guennebaud <[email protected]>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_GENERAL_MATRIX_MATRIX_TRIANGULAR_H
00011 #define EIGEN_GENERAL_MATRIX_MATRIX_TRIANGULAR_H
00012 
00013 namespace Eigen { 
00014 
00015 template<typename Scalar, typename Index, int StorageOrder, int UpLo, bool ConjLhs, bool ConjRhs>
00016 struct selfadjoint_rank1_update;
00017 
00018 namespace internal {
00019 
00020 /**********************************************************************
00021 * This file implements a general A * B product while
00022 * evaluating only one triangular part of the product.
00023 * This is a more general version of self adjoint product (C += A A^T)
00024 * as the level 3 SYRK Blas routine.
00025 **********************************************************************/
00026 
00027 // forward declarations (defined at the end of this file)
00028 template<typename LhsScalar, typename RhsScalar, typename Index, int mr, int nr, bool ConjLhs, bool ConjRhs, int UpLo>
00029 struct tribb_kernel;
00030   
00031 /* Optimized matrix-matrix product evaluating only one triangular half */
00032 template <typename Index,
00033           typename LhsScalar, int LhsStorageOrder, bool ConjugateLhs,
00034           typename RhsScalar, int RhsStorageOrder, bool ConjugateRhs,
00035                               int ResStorageOrder, int  UpLo, int Version = Specialized>
00036 struct general_matrix_matrix_triangular_product;
00037 
00038 // as usual if the result is row major => we transpose the product
00039 template <typename Index, typename LhsScalar, int LhsStorageOrder, bool ConjugateLhs,
00040                           typename RhsScalar, int RhsStorageOrder, bool ConjugateRhs, int  UpLo, int Version>
00041 struct general_matrix_matrix_triangular_product<Index,LhsScalar,LhsStorageOrder,ConjugateLhs,RhsScalar,RhsStorageOrder,ConjugateRhs,RowMajor,UpLo,Version>
00042 {
00043   typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType ResScalar;
00044   static EIGEN_STRONG_INLINE void run(Index size, Index depth,const LhsScalar* lhs, Index lhsStride,
00045                                       const RhsScalar* rhs, Index rhsStride, ResScalar* res, Index resStride,
00046                                       const ResScalar& alpha, level3_blocking<LhsScalar,RhsScalar>& blocking)
00047   {
00048     general_matrix_matrix_triangular_product<Index,
00049         RhsScalar, RhsStorageOrder==RowMajor ? ColMajor : RowMajor, ConjugateRhs,
00050         LhsScalar, LhsStorageOrder==RowMajor ? ColMajor : RowMajor, ConjugateLhs,
00051         ColMajor, UpLo==Lower?Upper:Lower>
00052       ::run(size,depth,rhs,rhsStride,lhs,lhsStride,res,resStride,alpha,blocking);
00053   }
00054 };
00055 
00056 template <typename Index, typename LhsScalar, int LhsStorageOrder, bool ConjugateLhs,
00057                           typename RhsScalar, int RhsStorageOrder, bool ConjugateRhs, int  UpLo, int Version>
00058 struct general_matrix_matrix_triangular_product<Index,LhsScalar,LhsStorageOrder,ConjugateLhs,RhsScalar,RhsStorageOrder,ConjugateRhs,ColMajor,UpLo,Version>
00059 {
00060   typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType ResScalar;
00061   static EIGEN_STRONG_INLINE void run(Index size, Index depth,const LhsScalar* _lhs, Index lhsStride,
00062                                       const RhsScalar* _rhs, Index rhsStride, ResScalar* _res, Index resStride,
00063                                       const ResScalar& alpha, level3_blocking<LhsScalar,RhsScalar>& blocking)
00064   {
00065     typedef gebp_traits<LhsScalar,RhsScalar> Traits;
00066 
00067     typedef const_blas_data_mapper<LhsScalar, Index, LhsStorageOrder> LhsMapper;
00068     typedef const_blas_data_mapper<RhsScalar, Index, RhsStorageOrder> RhsMapper;
00069     typedef blas_data_mapper<typename Traits::ResScalar, Index, ColMajor> ResMapper;
00070     LhsMapper lhs(_lhs,lhsStride);
00071     RhsMapper rhs(_rhs,rhsStride);
00072     ResMapper res(_res, resStride);
00073 
00074     Index kc = blocking.kc();
00075     Index mc = (std::min)(size,blocking.mc());
00076 
00077     // !!! mc must be a multiple of nr:
00078     if(mc > Traits::nr)
00079       mc = (mc/Traits::nr)*Traits::nr;
00080 
00081     std::size_t sizeA = kc*mc;
00082     std::size_t sizeB = kc*size;
00083 
00084     ei_declare_aligned_stack_constructed_variable(LhsScalar, blockA, sizeA, blocking.blockA());
00085     ei_declare_aligned_stack_constructed_variable(RhsScalar, blockB, sizeB, blocking.blockB());
00086 
00087     gemm_pack_lhs<LhsScalar, Index, LhsMapper, Traits::mr, Traits::LhsProgress, LhsStorageOrder> pack_lhs;
00088     gemm_pack_rhs<RhsScalar, Index, RhsMapper, Traits::nr, RhsStorageOrder> pack_rhs;
00089     gebp_kernel<LhsScalar, RhsScalar, Index, ResMapper, Traits::mr, Traits::nr, ConjugateLhs, ConjugateRhs> gebp;
00090     tribb_kernel<LhsScalar, RhsScalar, Index, Traits::mr, Traits::nr, ConjugateLhs, ConjugateRhs, UpLo> sybb;
00091 
00092     for(Index k2=0; k2<depth; k2+=kc)
00093     {
00094       const Index actual_kc = (std::min)(k2+kc,depth)-k2;
00095 
00096       // note that the actual rhs is the transpose/adjoint of mat
00097       pack_rhs(blockB, rhs.getSubMapper(k2,0), actual_kc, size);
00098 
00099       for(Index i2=0; i2<size; i2+=mc)
00100       {
00101         const Index actual_mc = (std::min)(i2+mc,size)-i2;
00102 
00103         pack_lhs(blockA, lhs.getSubMapper(i2, k2), actual_kc, actual_mc);
00104 
00105         // the selected actual_mc * size panel of res is split into three different part:
00106         //  1 - before the diagonal => processed with gebp or skipped
00107         //  2 - the actual_mc x actual_mc symmetric block => processed with a special kernel
00108         //  3 - after the diagonal => processed with gebp or skipped
00109         if (UpLo==Lower)
00110           gebp(res.getSubMapper(i2, 0), blockA, blockB, actual_mc, actual_kc,
00111                (std::min)(size,i2), alpha, -1, -1, 0, 0);
00112 
00113 
00114         sybb(_res+resStride*i2 + i2, resStride, blockA, blockB + actual_kc*i2, actual_mc, actual_kc, alpha);
00115 
00116         if (UpLo==Upper)
00117         {
00118           Index j2 = i2+actual_mc;
00119           gebp(res.getSubMapper(i2, j2), blockA, blockB+actual_kc*j2, actual_mc,
00120                actual_kc, (std::max)(Index(0), size-j2), alpha, -1, -1, 0, 0);
00121         }
00122       }
00123     }
00124   }
00125 };
00126 
00127 // Optimized packed Block * packed Block product kernel evaluating only one given triangular part
00128 // This kernel is built on top of the gebp kernel:
00129 // - the current destination block is processed per panel of actual_mc x BlockSize
00130 //   where BlockSize is set to the minimal value allowing gebp to be as fast as possible
00131 // - then, as usual, each panel is split into three parts along the diagonal,
00132 //   the sub blocks above and below the diagonal are processed as usual,
00133 //   while the triangular block overlapping the diagonal is evaluated into a
00134 //   small temporary buffer which is then accumulated into the result using a
00135 //   triangular traversal.
00136 template<typename LhsScalar, typename RhsScalar, typename Index, int mr, int nr, bool ConjLhs, bool ConjRhs, int UpLo>
00137 struct tribb_kernel
00138 {
00139   typedef gebp_traits<LhsScalar,RhsScalar,ConjLhs,ConjRhs> Traits;
00140   typedef typename Traits::ResScalar ResScalar;
00141 
00142   enum {
00143     BlockSize  = meta_least_common_multiple<EIGEN_PLAIN_ENUM_MAX(mr,nr),EIGEN_PLAIN_ENUM_MIN(mr,nr)>::ret
00144   };
00145   void operator()(ResScalar* _res, Index resStride, const LhsScalar* blockA, const RhsScalar* blockB, Index size, Index depth, const ResScalar& alpha)
00146   {
00147     typedef blas_data_mapper<ResScalar, Index, ColMajor> ResMapper;
00148     ResMapper res(_res, resStride);
00149     gebp_kernel<LhsScalar, RhsScalar, Index, ResMapper, mr, nr, ConjLhs, ConjRhs> gebp_kernel;
00150 
00151     Matrix<ResScalar,BlockSize,BlockSize,ColMajor> buffer;
00152 
00153     // let's process the block per panel of actual_mc x BlockSize,
00154     // again, each is split into three parts, etc.
00155     for (Index j=0; j<size; j+=BlockSize)
00156     {
00157       Index actualBlockSize = std::min<Index>(BlockSize,size - j);
00158       const RhsScalar* actual_b = blockB+j*depth;
00159 
00160       if(UpLo==Upper)
00161         gebp_kernel(res.getSubMapper(0, j), blockA, actual_b, j, depth, actualBlockSize, alpha,
00162                     -1, -1, 0, 0);
00163 
00164       // selfadjoint micro block
00165       {
00166         Index i = j;
00167         buffer.setZero();
00168         // 1 - apply the kernel on the temporary buffer
00169         gebp_kernel(ResMapper(buffer.data(), BlockSize), blockA+depth*i, actual_b, actualBlockSize, depth, actualBlockSize, alpha,
00170                     -1, -1, 0, 0);
00171         // 2 - triangular accumulation
00172         for(Index j1=0; j1<actualBlockSize; ++j1)
00173         {
00174           ResScalar* r = &res(i, j + j1);
00175           for(Index i1=UpLo==Lower ? j1 : 0;
00176               UpLo==Lower ? i1<actualBlockSize : i1<=j1; ++i1)
00177             r[i1] += buffer(i1,j1);
00178         }
00179       }
00180 
00181       if(UpLo==Lower)
00182       {
00183         Index i = j+actualBlockSize;
00184         gebp_kernel(res.getSubMapper(i, j), blockA+depth*i, actual_b, size-i, 
00185                     depth, actualBlockSize, alpha, -1, -1, 0, 0);
00186       }
00187     }
00188   }
00189 };
00190 
00191 } // end namespace internal
00192 
00193 // high level API
00194 
00195 template<typename MatrixType, typename ProductType, int UpLo, bool IsOuterProduct>
00196 struct general_product_to_triangular_selector;
00197 
00198 
00199 template<typename MatrixType, typename ProductType, int UpLo>
00200 struct general_product_to_triangular_selector<MatrixType,ProductType,UpLo,true>
00201 {
00202   static void run(MatrixType& mat, const ProductType& prod, const typename MatrixType::Scalar& alpha)
00203   {
00204     typedef typename MatrixType::Scalar Scalar;
00205     
00206     typedef typename internal::remove_all<typename ProductType::LhsNested>::type Lhs;
00207     typedef internal::blas_traits<Lhs> LhsBlasTraits;
00208     typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhs;
00209     typedef typename internal::remove_all<ActualLhs>::type _ActualLhs;
00210     typename internal::add_const_on_value_type<ActualLhs>::type actualLhs = LhsBlasTraits::extract(prod.lhs());
00211     
00212     typedef typename internal::remove_all<typename ProductType::RhsNested>::type Rhs;
00213     typedef internal::blas_traits<Rhs> RhsBlasTraits;
00214     typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhs;
00215     typedef typename internal::remove_all<ActualRhs>::type _ActualRhs;
00216     typename internal::add_const_on_value_type<ActualRhs>::type actualRhs = RhsBlasTraits::extract(prod.rhs());
00217 
00218     Scalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(prod.lhs().derived()) * RhsBlasTraits::extractScalarFactor(prod.rhs().derived());
00219 
00220     enum {
00221       StorageOrder = (internal::traits<MatrixType>::Flags&RowMajorBit) ? RowMajor : ColMajor,
00222       UseLhsDirectly = _ActualLhs::InnerStrideAtCompileTime==1,
00223       UseRhsDirectly = _ActualRhs::InnerStrideAtCompileTime==1
00224     };
00225     
00226     internal::gemv_static_vector_if<Scalar,Lhs::SizeAtCompileTime,Lhs::MaxSizeAtCompileTime,!UseLhsDirectly> static_lhs;
00227     ei_declare_aligned_stack_constructed_variable(Scalar, actualLhsPtr, actualLhs.size(),
00228       (UseLhsDirectly ? const_cast<Scalar*>(actualLhs.data()) : static_lhs.data()));
00229     if(!UseLhsDirectly) Map<typename _ActualLhs::PlainObject>(actualLhsPtr, actualLhs.size()) = actualLhs;
00230     
00231     internal::gemv_static_vector_if<Scalar,Rhs::SizeAtCompileTime,Rhs::MaxSizeAtCompileTime,!UseRhsDirectly> static_rhs;
00232     ei_declare_aligned_stack_constructed_variable(Scalar, actualRhsPtr, actualRhs.size(),
00233       (UseRhsDirectly ? const_cast<Scalar*>(actualRhs.data()) : static_rhs.data()));
00234     if(!UseRhsDirectly) Map<typename _ActualRhs::PlainObject>(actualRhsPtr, actualRhs.size()) = actualRhs;
00235     
00236     
00237     selfadjoint_rank1_update<Scalar,Index,StorageOrder,UpLo,
00238                               LhsBlasTraits::NeedToConjugate && NumTraits<Scalar>::IsComplex,
00239                               RhsBlasTraits::NeedToConjugate && NumTraits<Scalar>::IsComplex>
00240           ::run(actualLhs.size(), mat.data(), mat.outerStride(), actualLhsPtr, actualRhsPtr, actualAlpha);
00241   }
00242 };
00243 
00244 template<typename MatrixType, typename ProductType, int UpLo>
00245 struct general_product_to_triangular_selector<MatrixType,ProductType,UpLo,false>
00246 {
00247   static void run(MatrixType& mat, const ProductType& prod, const typename MatrixType::Scalar& alpha)
00248   {
00249     typedef typename internal::remove_all<typename ProductType::LhsNested>::type Lhs;
00250     typedef internal::blas_traits<Lhs> LhsBlasTraits;
00251     typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhs;
00252     typedef typename internal::remove_all<ActualLhs>::type _ActualLhs;
00253     typename internal::add_const_on_value_type<ActualLhs>::type actualLhs = LhsBlasTraits::extract(prod.lhs());
00254     
00255     typedef typename internal::remove_all<typename ProductType::RhsNested>::type Rhs;
00256     typedef internal::blas_traits<Rhs> RhsBlasTraits;
00257     typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhs;
00258     typedef typename internal::remove_all<ActualRhs>::type _ActualRhs;
00259     typename internal::add_const_on_value_type<ActualRhs>::type actualRhs = RhsBlasTraits::extract(prod.rhs());
00260 
00261     typename ProductType::Scalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(prod.lhs().derived()) * RhsBlasTraits::extractScalarFactor(prod.rhs().derived());
00262 
00263     enum {
00264       IsRowMajor = (internal::traits<MatrixType>::Flags&RowMajorBit) ? 1 : 0,
00265       LhsIsRowMajor = _ActualLhs::Flags&RowMajorBit ? 1 : 0,
00266       RhsIsRowMajor = _ActualRhs::Flags&RowMajorBit ? 1 : 0
00267     };
00268 
00269     Index size = mat.cols();
00270     Index depth = actualLhs.cols();
00271 
00272     typedef internal::gemm_blocking_space<IsRowMajor ? RowMajor : ColMajor,typename Lhs::Scalar,typename Rhs::Scalar,
00273           MatrixType::MaxColsAtCompileTime, MatrixType::MaxColsAtCompileTime, _ActualRhs::MaxColsAtCompileTime> BlockingType;
00274 
00275     BlockingType blocking(size, size, depth, 1, false);
00276 
00277     internal::general_matrix_matrix_triangular_product<Index,
00278       typename Lhs::Scalar, LhsIsRowMajor ? RowMajor : ColMajor, LhsBlasTraits::NeedToConjugate,
00279       typename Rhs::Scalar, RhsIsRowMajor ? RowMajor : ColMajor, RhsBlasTraits::NeedToConjugate,
00280       IsRowMajor ? RowMajor : ColMajor, UpLo>
00281       ::run(size, depth,
00282             &actualLhs.coeffRef(0,0), actualLhs.outerStride(), &actualRhs.coeffRef(0,0), actualRhs.outerStride(),
00283             mat.data(), mat.outerStride(), actualAlpha, blocking);
00284   }
00285 };
00286 
00287 template<typename MatrixType, unsigned int UpLo>
00288 template<typename ProductType>
00289 TriangularView<MatrixType,UpLo>& TriangularViewImpl<MatrixType,UpLo,Dense>::_assignProduct(const ProductType& prod, const Scalar& alpha)
00290 {
00291   eigen_assert(derived().nestedExpression().rows() == prod.rows() && derived().cols() == prod.cols());
00292   
00293   general_product_to_triangular_selector<MatrixType, ProductType, UpLo, internal::traits<ProductType>::InnerSize==1>::run(derived().nestedExpression().const_cast_derived(), prod, alpha);
00294   
00295   return derived();
00296 }
00297 
00298 } // end namespace Eigen
00299 
00300 #endif // EIGEN_GENERAL_MATRIX_MATRIX_TRIANGULAR_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines