// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2009 Gael Guennebaud // // Eigen is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 3 of the License, or (at your option) any later version. // // Alternatively, you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of // the License, or (at your option) any later version. // // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the // GNU General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License and a copy of the GNU General Public License along with // Eigen. If not, see . #ifndef EIGEN_TRIANGULAR_SOLVER_MATRIX_H #define EIGEN_TRIANGULAR_SOLVER_MATRIX_H namespace internal { // if the rhs is row major, let's transpose the product template struct triangular_solve_matrix { static EIGEN_DONT_INLINE void run( Index size, Index cols, const Scalar* tri, Index triStride, Scalar* _other, Index otherStride) { triangular_solve_matrix< Scalar, Index, Side==OnTheLeft?OnTheRight:OnTheLeft, (Mode&UnitDiag) | ((Mode&Upper) ? Lower : Upper), NumTraits::IsComplex && Conjugate, TriStorageOrder==RowMajor ? ColMajor : RowMajor, ColMajor> ::run(size, cols, tri, triStride, _other, otherStride); } }; /* Optimized triangular solver with multiple right hand side and the triangular matrix on the left */ template struct triangular_solve_matrix { static EIGEN_DONT_INLINE void run( Index size, Index otherSize, const Scalar* _tri, Index triStride, Scalar* _other, Index otherStride) { Index cols = otherSize; const_blas_data_mapper tri(_tri,triStride); blas_data_mapper other(_other,otherStride); typedef gebp_traits Traits; enum { SmallPanelWidth = EIGEN_PLAIN_ENUM_MAX(Traits::mr,Traits::nr), IsLower = (Mode&Lower) == Lower }; Index kc = size; // cache block size along the K direction Index mc = size; // cache block size along the M direction Index nc = cols; // cache block size along the N direction computeProductBlockingSizes(kc, mc, nc); Scalar* blockA = ei_aligned_stack_new(Scalar, kc*mc); std::size_t sizeW = kc*Traits::WorkSpaceFactor; std::size_t sizeB = sizeW + kc*cols; Scalar* allocatedBlockB = ei_aligned_stack_new(Scalar, sizeB); Scalar* blockB = allocatedBlockB + sizeW; conj_if conj; gebp_kernel gebp_kernel; gemm_pack_lhs pack_lhs; gemm_pack_rhs pack_rhs; for(Index k2=IsLower ? 0 : size; IsLower ? k20; IsLower ? k2+=kc : k2-=kc) { const Index actual_kc = std::min(IsLower ? size-k2 : k2, kc); // We have selected and packed a big horizontal panel R1 of rhs. Let B be the packed copy of this panel, // and R2 the remaining part of rhs. The corresponding vertical panel of lhs is split into // A11 (the triangular part) and A21 the remaining rectangular part. // Then the high level algorithm is: // - B = R1 => general block copy (done during the next step) // - R1 = L1^-1 B => tricky part // - update B from the new R1 => actually this has to be performed continuously during the above step // - R2 = L2 * B => GEPP // The tricky part: compute R1 = L1^-1 B while updating B from R1 // The idea is to split L1 into multiple small vertical panels. // Each panel can be split into a small triangular part A1 which is processed without optimization, // and the remaining small part A2 which is processed using gebp with appropriate block strides { // for each small vertical panels of lhs for (Index k1=0; k1(actual_kc-k1, SmallPanelWidth); // tr solve for (Index k=0; k0) { Index startTarget = IsLower ? k2+k1+actualPanelWidth : k2-actual_kc; pack_lhs(blockA, &tri(startTarget,startBlock), triStride, actualPanelWidth, lengthTarget); gebp_kernel(_other+startTarget, otherStride, blockA, blockB, lengthTarget, actualPanelWidth, cols, Scalar(-1), actualPanelWidth, actual_kc, 0, blockBOffset); } } } // R2 = A2 * B => GEPP { Index start = IsLower ? k2+kc : 0; Index end = IsLower ? size : k2-kc; for(Index i2=start; i20) { pack_lhs(blockA, &tri(i2, IsLower ? k2 : k2-kc), triStride, actual_kc, actual_mc); gebp_kernel(_other+i2, otherStride, blockA, blockB, actual_mc, actual_kc, cols, Scalar(-1)); } } } } ei_aligned_stack_delete(Scalar, blockA, kc*mc); ei_aligned_stack_delete(Scalar, allocatedBlockB, sizeB); } }; /* Optimized triangular solver with multiple left hand sides and the trinagular matrix on the right */ template struct triangular_solve_matrix { static EIGEN_DONT_INLINE void run( Index size, Index otherSize, const Scalar* _tri, Index triStride, Scalar* _other, Index otherStride) { Index rows = otherSize; const_blas_data_mapper rhs(_tri,triStride); blas_data_mapper lhs(_other,otherStride); typedef gebp_traits Traits; enum { RhsStorageOrder = TriStorageOrder, SmallPanelWidth = EIGEN_PLAIN_ENUM_MAX(Traits::mr,Traits::nr), IsLower = (Mode&Lower) == Lower }; // Index kc = std::min(Traits::Max_kc/4,size); // cache block size along the K direction // Index mc = std::min(Traits::Max_mc,size); // cache block size along the M direction // check that !!!! Index kc = size; // cache block size along the K direction Index mc = size; // cache block size along the M direction Index nc = rows; // cache block size along the N direction computeProductBlockingSizes(kc, mc, nc); Scalar* blockA = ei_aligned_stack_new(Scalar, kc*mc); std::size_t sizeW = kc*Traits::WorkSpaceFactor; std::size_t sizeB = sizeW + kc*size; Scalar* allocatedBlockB = ei_aligned_stack_new(Scalar, sizeB); Scalar* blockB = allocatedBlockB + sizeW; conj_if conj; gebp_kernel gebp_kernel; gemm_pack_rhs pack_rhs; gemm_pack_rhs pack_rhs_panel; gemm_pack_lhs pack_lhs_panel; for(Index k2=IsLower ? size : 0; IsLower ? k2>0 : k20) pack_rhs(geb, &rhs(actual_k2,startPanel), triStride, actual_kc, rs); // triangular packing (we only pack the panels off the diagonal, // neglecting the blocks overlapping the diagonal { for (Index j2=0; j2(actual_kc-j2, SmallPanelWidth); Index actual_j2 = actual_k2 + j2; Index panelOffset = IsLower ? j2+actualPanelWidth : 0; Index panelLength = IsLower ? actual_kc-j2-actualPanelWidth : j2; if (panelLength>0) pack_rhs_panel(blockB+j2*actual_kc, &rhs(actual_k2+panelOffset, actual_j2), triStride, panelLength, actualPanelWidth, actual_kc, panelOffset); } } for(Index i2=0; i2 vertical panels of rhs) for (Index j2 = IsLower ? (actual_kc - ((actual_kc%SmallPanelWidth) ? Index(actual_kc%SmallPanelWidth) : Index(SmallPanelWidth))) : 0; IsLower ? j2>=0 : j2(actual_kc-j2, SmallPanelWidth); Index absolute_j2 = actual_k2 + j2; Index panelOffset = IsLower ? j2+actualPanelWidth : 0; Index panelLength = IsLower ? actual_kc - j2 - actualPanelWidth : j2; // GEBP if(panelLength>0) { gebp_kernel(&lhs(i2,absolute_j2), otherStride, blockA, blockB+j2*actual_kc, actual_mc, panelLength, actualPanelWidth, Scalar(-1), actual_kc, actual_kc, // strides panelOffset, panelOffset, // offsets allocatedBlockB); // workspace } // unblocked triangular solve for (Index k=0; k0) gebp_kernel(_other+i2+startPanel*otherStride, otherStride, blockA, geb, actual_mc, actual_kc, rs, Scalar(-1), -1, -1, 0, 0, allocatedBlockB); } } ei_aligned_stack_delete(Scalar, blockA, kc*mc); ei_aligned_stack_delete(Scalar, allocatedBlockB, sizeB); } }; } // end namespace internal #endif // EIGEN_TRIANGULAR_SOLVER_MATRIX_H