/src/Geometry_Eigen/Eigen/src/Core/products/TriangularMatrixMatrix.h

http://github.com/Akranar/daguerreo · C Header · 403 lines · 277 code · 51 blank · 75 comment · 27 complexity · 5727edaa2fbe4675f68639107b5bb574 MD5 · raw file

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // Eigen is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU Lesser General Public
  8. // License as published by the Free Software Foundation; either
  9. // version 3 of the License, or (at your option) any later version.
  10. //
  11. // Alternatively, you can redistribute it and/or
  12. // modify it under the terms of the GNU General Public License as
  13. // published by the Free Software Foundation; either version 2 of
  14. // the License, or (at your option) any later version.
  15. //
  16. // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
  17. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  18. // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU Lesser General Public
  22. // License and a copy of the GNU General Public License along with
  23. // Eigen. If not, see <http://www.gnu.org/licenses/>.
  24. #ifndef EIGEN_TRIANGULAR_MATRIX_MATRIX_H
  25. #define EIGEN_TRIANGULAR_MATRIX_MATRIX_H
  26. namespace internal {
  27. // template<typename Scalar, int mr, int StorageOrder, bool Conjugate, int Mode>
  28. // struct gemm_pack_lhs_triangular
  29. // {
  30. // Matrix<Scalar,mr,mr,
  31. // void operator()(Scalar* blockA, const EIGEN_RESTRICT Scalar* _lhs, int lhsStride, int depth, int rows)
  32. // {
  33. // conj_if<NumTraits<Scalar>::IsComplex && Conjugate> cj;
  34. // const_blas_data_mapper<Scalar, StorageOrder> lhs(_lhs,lhsStride);
  35. // int count = 0;
  36. // const int peeled_mc = (rows/mr)*mr;
  37. // for(int i=0; i<peeled_mc; i+=mr)
  38. // {
  39. // for(int k=0; k<depth; k++)
  40. // for(int w=0; w<mr; w++)
  41. // blockA[count++] = cj(lhs(i+w, k));
  42. // }
  43. // for(int i=peeled_mc; i<rows; i++)
  44. // {
  45. // for(int k=0; k<depth; k++)
  46. // blockA[count++] = cj(lhs(i, k));
  47. // }
  48. // }
  49. // };
  50. /* Optimized triangular matrix * matrix (_TRMM++) product built on top of
  51. * the general matrix matrix product.
  52. */
  53. template <typename Scalar, typename Index,
  54. int Mode, bool LhsIsTriangular,
  55. int LhsStorageOrder, bool ConjugateLhs,
  56. int RhsStorageOrder, bool ConjugateRhs,
  57. int ResStorageOrder>
  58. struct product_triangular_matrix_matrix;
  59. template <typename Scalar, typename Index,
  60. int Mode, bool LhsIsTriangular,
  61. int LhsStorageOrder, bool ConjugateLhs,
  62. int RhsStorageOrder, bool ConjugateRhs>
  63. struct product_triangular_matrix_matrix<Scalar,Index,Mode,LhsIsTriangular,
  64. LhsStorageOrder,ConjugateLhs,
  65. RhsStorageOrder,ConjugateRhs,RowMajor>
  66. {
  67. static EIGEN_STRONG_INLINE void run(
  68. Index rows, Index cols, Index depth,
  69. const Scalar* lhs, Index lhsStride,
  70. const Scalar* rhs, Index rhsStride,
  71. Scalar* res, Index resStride,
  72. Scalar alpha)
  73. {
  74. product_triangular_matrix_matrix<Scalar, Index,
  75. (Mode&(UnitDiag|ZeroDiag)) | ((Mode&Upper) ? Lower : Upper),
  76. (!LhsIsTriangular),
  77. RhsStorageOrder==RowMajor ? ColMajor : RowMajor,
  78. ConjugateRhs,
  79. LhsStorageOrder==RowMajor ? ColMajor : RowMajor,
  80. ConjugateLhs,
  81. ColMajor>
  82. ::run(cols, rows, depth, rhs, rhsStride, lhs, lhsStride, res, resStride, alpha);
  83. }
  84. };
  85. // implements col-major += alpha * op(triangular) * op(general)
  86. template <typename Scalar, typename Index, int Mode,
  87. int LhsStorageOrder, bool ConjugateLhs,
  88. int RhsStorageOrder, bool ConjugateRhs>
  89. struct product_triangular_matrix_matrix<Scalar,Index,Mode,true,
  90. LhsStorageOrder,ConjugateLhs,
  91. RhsStorageOrder,ConjugateRhs,ColMajor>
  92. {
  93. typedef gebp_traits<Scalar,Scalar> Traits;
  94. enum {
  95. SmallPanelWidth = EIGEN_PLAIN_ENUM_MAX(Traits::mr,Traits::nr),
  96. IsLower = (Mode&Lower) == Lower,
  97. SetDiag = (Mode&(ZeroDiag|UnitDiag)) ? 0 : 1
  98. };
  99. static EIGEN_DONT_INLINE void run(
  100. Index _rows, Index _cols, Index _depth,
  101. const Scalar* _lhs, Index lhsStride,
  102. const Scalar* _rhs, Index rhsStride,
  103. Scalar* res, Index resStride,
  104. Scalar alpha)
  105. {
  106. // strip zeros
  107. Index diagSize = (std::min)(_rows,_depth);
  108. Index rows = IsLower ? _rows : diagSize;
  109. Index depth = IsLower ? diagSize : _depth;
  110. Index cols = _cols;
  111. const_blas_data_mapper<Scalar, Index, LhsStorageOrder> lhs(_lhs,lhsStride);
  112. const_blas_data_mapper<Scalar, Index, RhsStorageOrder> rhs(_rhs,rhsStride);
  113. Index kc = depth; // cache block size along the K direction
  114. Index mc = rows; // cache block size along the M direction
  115. Index nc = cols; // cache block size along the N direction
  116. computeProductBlockingSizes<Scalar,Scalar,4>(kc, mc, nc);
  117. std::size_t sizeW = kc*Traits::WorkSpaceFactor;
  118. std::size_t sizeB = sizeW + kc*cols;
  119. ei_declare_aligned_stack_constructed_variable(Scalar, blockA, kc*mc, 0);
  120. ei_declare_aligned_stack_constructed_variable(Scalar, allocatedBlockB, sizeB, 0);
  121. Scalar* blockB = allocatedBlockB + sizeW;
  122. Matrix<Scalar,SmallPanelWidth,SmallPanelWidth,LhsStorageOrder> triangularBuffer;
  123. triangularBuffer.setZero();
  124. if((Mode&ZeroDiag)==ZeroDiag)
  125. triangularBuffer.diagonal().setZero();
  126. else
  127. triangularBuffer.diagonal().setOnes();
  128. gebp_kernel<Scalar, Scalar, Index, Traits::mr, Traits::nr, ConjugateLhs, ConjugateRhs> gebp_kernel;
  129. gemm_pack_lhs<Scalar, Index, Traits::mr, Traits::LhsProgress, LhsStorageOrder> pack_lhs;
  130. gemm_pack_rhs<Scalar, Index, Traits::nr,RhsStorageOrder> pack_rhs;
  131. for(Index k2=IsLower ? depth : 0;
  132. IsLower ? k2>0 : k2<depth;
  133. IsLower ? k2-=kc : k2+=kc)
  134. {
  135. Index actual_kc = (std::min)(IsLower ? k2 : depth-k2, kc);
  136. Index actual_k2 = IsLower ? k2-actual_kc : k2;
  137. // align blocks with the end of the triangular part for trapezoidal lhs
  138. if((!IsLower)&&(k2<rows)&&(k2+actual_kc>rows))
  139. {
  140. actual_kc = rows-k2;
  141. k2 = k2+actual_kc-kc;
  142. }
  143. pack_rhs(blockB, &rhs(actual_k2,0), rhsStride, actual_kc, cols);
  144. // the selected lhs's panel has to be split in three different parts:
  145. // 1 - the part which is zero => skip it
  146. // 2 - the diagonal block => special kernel
  147. // 3 - the dense panel below (lower case) or above (upper case) the diagonal block => GEPP
  148. // the block diagonal, if any:
  149. if(IsLower || actual_k2<rows)
  150. {
  151. // for each small vertical panels of lhs
  152. for (Index k1=0; k1<actual_kc; k1+=SmallPanelWidth)
  153. {
  154. Index actualPanelWidth = std::min<Index>(actual_kc-k1, SmallPanelWidth);
  155. Index lengthTarget = IsLower ? actual_kc-k1-actualPanelWidth : k1;
  156. Index startBlock = actual_k2+k1;
  157. Index blockBOffset = k1;
  158. // => GEBP with the micro triangular block
  159. // The trick is to pack this micro block while filling the opposite triangular part with zeros.
  160. // To this end we do an extra triangular copy to a small temporary buffer
  161. for (Index k=0;k<actualPanelWidth;++k)
  162. {
  163. if (SetDiag)
  164. triangularBuffer.coeffRef(k,k) = lhs(startBlock+k,startBlock+k);
  165. for (Index i=IsLower ? k+1 : 0; IsLower ? i<actualPanelWidth : i<k; ++i)
  166. triangularBuffer.coeffRef(i,k) = lhs(startBlock+i,startBlock+k);
  167. }
  168. pack_lhs(blockA, triangularBuffer.data(), triangularBuffer.outerStride(), actualPanelWidth, actualPanelWidth);
  169. gebp_kernel(res+startBlock, resStride, blockA, blockB, actualPanelWidth, actualPanelWidth, cols, alpha,
  170. actualPanelWidth, actual_kc, 0, blockBOffset);
  171. // GEBP with remaining micro panel
  172. if (lengthTarget>0)
  173. {
  174. Index startTarget = IsLower ? actual_k2+k1+actualPanelWidth : actual_k2;
  175. pack_lhs(blockA, &lhs(startTarget,startBlock), lhsStride, actualPanelWidth, lengthTarget);
  176. gebp_kernel(res+startTarget, resStride, blockA, blockB, lengthTarget, actualPanelWidth, cols, alpha,
  177. actualPanelWidth, actual_kc, 0, blockBOffset);
  178. }
  179. }
  180. }
  181. // the part below (lower case) or above (upper case) the diagonal => GEPP
  182. {
  183. Index start = IsLower ? k2 : 0;
  184. Index end = IsLower ? rows : (std::min)(actual_k2,rows);
  185. for(Index i2=start; i2<end; i2+=mc)
  186. {
  187. const Index actual_mc = (std::min)(i2+mc,end)-i2;
  188. gemm_pack_lhs<Scalar, Index, Traits::mr,Traits::LhsProgress, LhsStorageOrder,false>()
  189. (blockA, &lhs(i2, actual_k2), lhsStride, actual_kc, actual_mc);
  190. gebp_kernel(res+i2, resStride, blockA, blockB, actual_mc, actual_kc, cols, alpha);
  191. }
  192. }
  193. }
  194. }
  195. };
  196. // implements col-major += alpha * op(general) * op(triangular)
  197. template <typename Scalar, typename Index, int Mode,
  198. int LhsStorageOrder, bool ConjugateLhs,
  199. int RhsStorageOrder, bool ConjugateRhs>
  200. struct product_triangular_matrix_matrix<Scalar,Index,Mode,false,
  201. LhsStorageOrder,ConjugateLhs,
  202. RhsStorageOrder,ConjugateRhs,ColMajor>
  203. {
  204. typedef gebp_traits<Scalar,Scalar> Traits;
  205. enum {
  206. SmallPanelWidth = EIGEN_PLAIN_ENUM_MAX(Traits::mr,Traits::nr),
  207. IsLower = (Mode&Lower) == Lower,
  208. SetDiag = (Mode&(ZeroDiag|UnitDiag)) ? 0 : 1
  209. };
  210. static EIGEN_DONT_INLINE void run(
  211. Index _rows, Index _cols, Index _depth,
  212. const Scalar* _lhs, Index lhsStride,
  213. const Scalar* _rhs, Index rhsStride,
  214. Scalar* res, Index resStride,
  215. Scalar alpha)
  216. {
  217. // strip zeros
  218. Index diagSize = (std::min)(_cols,_depth);
  219. Index rows = _rows;
  220. Index depth = IsLower ? _depth : diagSize;
  221. Index cols = IsLower ? diagSize : _cols;
  222. const_blas_data_mapper<Scalar, Index, LhsStorageOrder> lhs(_lhs,lhsStride);
  223. const_blas_data_mapper<Scalar, Index, RhsStorageOrder> rhs(_rhs,rhsStride);
  224. Index kc = depth; // cache block size along the K direction
  225. Index mc = rows; // cache block size along the M direction
  226. Index nc = cols; // cache block size along the N direction
  227. computeProductBlockingSizes<Scalar,Scalar,4>(kc, mc, nc);
  228. std::size_t sizeW = kc*Traits::WorkSpaceFactor;
  229. std::size_t sizeB = sizeW + kc*cols;
  230. ei_declare_aligned_stack_constructed_variable(Scalar, blockA, kc*mc, 0);
  231. ei_declare_aligned_stack_constructed_variable(Scalar, allocatedBlockB, sizeB, 0);
  232. Scalar* blockB = allocatedBlockB + sizeW;
  233. Matrix<Scalar,SmallPanelWidth,SmallPanelWidth,RhsStorageOrder> triangularBuffer;
  234. triangularBuffer.setZero();
  235. if((Mode&ZeroDiag)==ZeroDiag)
  236. triangularBuffer.diagonal().setZero();
  237. else
  238. triangularBuffer.diagonal().setOnes();
  239. gebp_kernel<Scalar, Scalar, Index, Traits::mr, Traits::nr, ConjugateLhs, ConjugateRhs> gebp_kernel;
  240. gemm_pack_lhs<Scalar, Index, Traits::mr, Traits::LhsProgress, LhsStorageOrder> pack_lhs;
  241. gemm_pack_rhs<Scalar, Index, Traits::nr,RhsStorageOrder> pack_rhs;
  242. gemm_pack_rhs<Scalar, Index, Traits::nr,RhsStorageOrder,false,true> pack_rhs_panel;
  243. for(Index k2=IsLower ? 0 : depth;
  244. IsLower ? k2<depth : k2>0;
  245. IsLower ? k2+=kc : k2-=kc)
  246. {
  247. Index actual_kc = (std::min)(IsLower ? depth-k2 : k2, kc);
  248. Index actual_k2 = IsLower ? k2 : k2-actual_kc;
  249. // align blocks with the end of the triangular part for trapezoidal rhs
  250. if(IsLower && (k2<cols) && (actual_k2+actual_kc>cols))
  251. {
  252. actual_kc = cols-k2;
  253. k2 = actual_k2 + actual_kc - kc;
  254. }
  255. // remaining size
  256. Index rs = IsLower ? (std::min)(cols,actual_k2) : cols - k2;
  257. // size of the triangular part
  258. Index ts = (IsLower && actual_k2>=cols) ? 0 : actual_kc;
  259. Scalar* geb = blockB+ts*ts;
  260. pack_rhs(geb, &rhs(actual_k2,IsLower ? 0 : k2), rhsStride, actual_kc, rs);
  261. // pack the triangular part of the rhs padding the unrolled blocks with zeros
  262. if(ts>0)
  263. {
  264. for (Index j2=0; j2<actual_kc; j2+=SmallPanelWidth)
  265. {
  266. Index actualPanelWidth = std::min<Index>(actual_kc-j2, SmallPanelWidth);
  267. Index actual_j2 = actual_k2 + j2;
  268. Index panelOffset = IsLower ? j2+actualPanelWidth : 0;
  269. Index panelLength = IsLower ? actual_kc-j2-actualPanelWidth : j2;
  270. // general part
  271. pack_rhs_panel(blockB+j2*actual_kc,
  272. &rhs(actual_k2+panelOffset, actual_j2), rhsStride,
  273. panelLength, actualPanelWidth,
  274. actual_kc, panelOffset);
  275. // append the triangular part via a temporary buffer
  276. for (Index j=0;j<actualPanelWidth;++j)
  277. {
  278. if (SetDiag)
  279. triangularBuffer.coeffRef(j,j) = rhs(actual_j2+j,actual_j2+j);
  280. for (Index k=IsLower ? j+1 : 0; IsLower ? k<actualPanelWidth : k<j; ++k)
  281. triangularBuffer.coeffRef(k,j) = rhs(actual_j2+k,actual_j2+j);
  282. }
  283. pack_rhs_panel(blockB+j2*actual_kc,
  284. triangularBuffer.data(), triangularBuffer.outerStride(),
  285. actualPanelWidth, actualPanelWidth,
  286. actual_kc, j2);
  287. }
  288. }
  289. for (Index i2=0; i2<rows; i2+=mc)
  290. {
  291. const Index actual_mc = (std::min)(mc,rows-i2);
  292. pack_lhs(blockA, &lhs(i2, actual_k2), lhsStride, actual_kc, actual_mc);
  293. // triangular kernel
  294. if(ts>0)
  295. {
  296. for (Index j2=0; j2<actual_kc; j2+=SmallPanelWidth)
  297. {
  298. Index actualPanelWidth = std::min<Index>(actual_kc-j2, SmallPanelWidth);
  299. Index panelLength = IsLower ? actual_kc-j2 : j2+actualPanelWidth;
  300. Index blockOffset = IsLower ? j2 : 0;
  301. gebp_kernel(res+i2+(actual_k2+j2)*resStride, resStride,
  302. blockA, blockB+j2*actual_kc,
  303. actual_mc, panelLength, actualPanelWidth,
  304. alpha,
  305. actual_kc, actual_kc, // strides
  306. blockOffset, blockOffset,// offsets
  307. allocatedBlockB); // workspace
  308. }
  309. }
  310. gebp_kernel(res+i2+(IsLower ? 0 : k2)*resStride, resStride,
  311. blockA, geb, actual_mc, actual_kc, rs,
  312. alpha,
  313. -1, -1, 0, 0, allocatedBlockB);
  314. }
  315. }
  316. }
  317. };
  318. /***************************************************************************
  319. * Wrapper to product_triangular_matrix_matrix
  320. ***************************************************************************/
  321. template<int Mode, bool LhsIsTriangular, typename Lhs, typename Rhs>
  322. struct traits<TriangularProduct<Mode,LhsIsTriangular,Lhs,false,Rhs,false> >
  323. : traits<ProductBase<TriangularProduct<Mode,LhsIsTriangular,Lhs,false,Rhs,false>, Lhs, Rhs> >
  324. {};
  325. } // end namespace internal
  326. template<int Mode, bool LhsIsTriangular, typename Lhs, typename Rhs>
  327. struct TriangularProduct<Mode,LhsIsTriangular,Lhs,false,Rhs,false>
  328. : public ProductBase<TriangularProduct<Mode,LhsIsTriangular,Lhs,false,Rhs,false>, Lhs, Rhs >
  329. {
  330. EIGEN_PRODUCT_PUBLIC_INTERFACE(TriangularProduct)
  331. TriangularProduct(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs) {}
  332. template<typename Dest> void scaleAndAddTo(Dest& dst, Scalar alpha) const
  333. {
  334. const ActualLhsType lhs = LhsBlasTraits::extract(m_lhs);
  335. const ActualRhsType rhs = RhsBlasTraits::extract(m_rhs);
  336. Scalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(m_lhs)
  337. * RhsBlasTraits::extractScalarFactor(m_rhs);
  338. internal::product_triangular_matrix_matrix<Scalar, Index,
  339. Mode, LhsIsTriangular,
  340. (internal::traits<_ActualLhsType>::Flags&RowMajorBit) ? RowMajor : ColMajor, LhsBlasTraits::NeedToConjugate,
  341. (internal::traits<_ActualRhsType>::Flags&RowMajorBit) ? RowMajor : ColMajor, RhsBlasTraits::NeedToConjugate,
  342. (internal::traits<Dest >::Flags&RowMajorBit) ? RowMajor : ColMajor>
  343. ::run(
  344. lhs.rows(), rhs.cols(), lhs.cols(),// LhsIsTriangular ? rhs.cols() : lhs.rows(), // sizes
  345. &lhs.coeffRef(0,0), lhs.outerStride(), // lhs info
  346. &rhs.coeffRef(0,0), rhs.outerStride(), // rhs info
  347. &dst.coeffRef(0,0), dst.outerStride(), // result info
  348. actualAlpha // alpha
  349. );
  350. }
  351. };
  352. #endif // EIGEN_TRIANGULAR_MATRIX_MATRIX_H