1 /* 2 Copyright (c) 2011, Intel Corporation. All rights reserved. 3 4 Redistribution and use in source and binary forms, with or without modification, 5 are permitted provided that the following conditions are met: 6 7 * Redistributions of source code must retain the above copyright notice, this 8 list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above copyright notice, 10 this list of conditions and the following disclaimer in the documentation 11 and/or other materials provided with the distribution. 12 * Neither the name of Intel Corporation nor the names of its contributors may 13 be used to endorse or promote products derived from this software without 14 specific prior written permission. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 20 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 27 ******************************************************************************** 28 * Content : Eigen bindings to BLAS F77 29 * Triangular matrix * matrix product functionality based on ?TRMM. 30 ******************************************************************************** 31 */ 32 33 #ifndef EIGEN_TRIANGULAR_SOLVER_MATRIX_BLAS_H 34 #define EIGEN_TRIANGULAR_SOLVER_MATRIX_BLAS_H 35 36 namespace Eigen { 37 38 namespace internal { 39 40 // implements LeftSide op(triangular)^-1 * general 41 #define EIGEN_BLAS_TRSM_L(EIGTYPE, BLASTYPE, BLASFUNC) \ 42 template <typename Index, int Mode, bool Conjugate, int TriStorageOrder> \ 43 struct triangular_solve_matrix<EIGTYPE,Index,OnTheLeft,Mode,Conjugate,TriStorageOrder,ColMajor,1> \ 44 { \ 45 enum { \ 46 IsLower = (Mode&Lower) == Lower, \ 47 IsUnitDiag = (Mode&UnitDiag) ? 1 : 0, \ 48 IsZeroDiag = (Mode&ZeroDiag) ? 1 : 0, \ 49 conjA = ((TriStorageOrder==ColMajor) && Conjugate) ? 1 : 0 \ 50 }; \ 51 static void run( \ 52 Index size, Index otherSize, \ 53 const EIGTYPE* _tri, Index triStride, \ 54 EIGTYPE* _other, Index otherIncr, Index otherStride, level3_blocking<EIGTYPE,EIGTYPE>& /*blocking*/) \ 55 { \ 56 EIGEN_ONLY_USED_FOR_DEBUG(otherIncr); \ 57 eigen_assert(otherIncr == 1); \ 58 BlasIndex m = convert_index<BlasIndex>(size), n = convert_index<BlasIndex>(otherSize), lda, ldb; \ 59 char side = 'L', uplo, diag='N', transa; \ 60 /* Set alpha_ */ \ 61 EIGTYPE alpha(1); \ 62 ldb = convert_index<BlasIndex>(otherStride);\ 63 \ 64 const EIGTYPE *a; \ 65 /* Set trans */ \ 66 transa = (TriStorageOrder==RowMajor) ? ((Conjugate) ? 'C' : 'T') : 'N'; \ 67 /* Set uplo */ \ 68 uplo = IsLower ? 'L' : 'U'; \ 69 if (TriStorageOrder==RowMajor) uplo = (uplo == 'L') ? 'U' : 'L'; \ 70 /* Set a, lda */ \ 71 typedef Matrix<EIGTYPE, Dynamic, Dynamic, TriStorageOrder> MatrixTri; \ 72 Map<const MatrixTri, 0, OuterStride<> > tri(_tri,size,size,OuterStride<>(triStride)); \ 73 MatrixTri a_tmp; \ 74 \ 75 if (conjA) { \ 76 a_tmp = tri.conjugate(); \ 77 a = a_tmp.data(); \ 78 lda = convert_index<BlasIndex>(a_tmp.outerStride()); \ 79 } else { \ 80 a = _tri; \ 81 lda = convert_index<BlasIndex>(triStride); \ 82 } \ 83 if (IsUnitDiag) diag='U'; \ 84 /* call ?trsm*/ \ 85 BLASFUNC(&side, &uplo, &transa, &diag, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (BLASTYPE*)_other, &ldb); \ 86 } \ 87 }; 88 89 #ifdef EIGEN_USE_MKL 90 EIGEN_BLAS_TRSM_L(double, double, dtrsm) 91 EIGEN_BLAS_TRSM_L(dcomplex, MKL_Complex16, ztrsm) 92 EIGEN_BLAS_TRSM_L(float, float, strsm) 93 EIGEN_BLAS_TRSM_L(scomplex, MKL_Complex8, ctrsm) 94 #else 95 EIGEN_BLAS_TRSM_L(double, double, dtrsm_) 96 EIGEN_BLAS_TRSM_L(dcomplex, double, ztrsm_) 97 EIGEN_BLAS_TRSM_L(float, float, strsm_) 98 EIGEN_BLAS_TRSM_L(scomplex, float, ctrsm_) 99 #endif 100 101 // implements RightSide general * op(triangular)^-1 102 #define EIGEN_BLAS_TRSM_R(EIGTYPE, BLASTYPE, BLASFUNC) \ 103 template <typename Index, int Mode, bool Conjugate, int TriStorageOrder> \ 104 struct triangular_solve_matrix<EIGTYPE,Index,OnTheRight,Mode,Conjugate,TriStorageOrder,ColMajor,1> \ 105 { \ 106 enum { \ 107 IsLower = (Mode&Lower) == Lower, \ 108 IsUnitDiag = (Mode&UnitDiag) ? 1 : 0, \ 109 IsZeroDiag = (Mode&ZeroDiag) ? 1 : 0, \ 110 conjA = ((TriStorageOrder==ColMajor) && Conjugate) ? 1 : 0 \ 111 }; \ 112 static void run( \ 113 Index size, Index otherSize, \ 114 const EIGTYPE* _tri, Index triStride, \ 115 EIGTYPE* _other, Index otherIncr, Index otherStride, level3_blocking<EIGTYPE,EIGTYPE>& /*blocking*/) \ 116 { \ 117 EIGEN_ONLY_USED_FOR_DEBUG(otherIncr); \ 118 eigen_assert(otherIncr == 1); \ 119 BlasIndex m = convert_index<BlasIndex>(otherSize), n = convert_index<BlasIndex>(size), lda, ldb; \ 120 char side = 'R', uplo, diag='N', transa; \ 121 /* Set alpha_ */ \ 122 EIGTYPE alpha(1); \ 123 ldb = convert_index<BlasIndex>(otherStride);\ 124 \ 125 const EIGTYPE *a; \ 126 /* Set trans */ \ 127 transa = (TriStorageOrder==RowMajor) ? ((Conjugate) ? 'C' : 'T') : 'N'; \ 128 /* Set uplo */ \ 129 uplo = IsLower ? 'L' : 'U'; \ 130 if (TriStorageOrder==RowMajor) uplo = (uplo == 'L') ? 'U' : 'L'; \ 131 /* Set a, lda */ \ 132 typedef Matrix<EIGTYPE, Dynamic, Dynamic, TriStorageOrder> MatrixTri; \ 133 Map<const MatrixTri, 0, OuterStride<> > tri(_tri,size,size,OuterStride<>(triStride)); \ 134 MatrixTri a_tmp; \ 135 \ 136 if (conjA) { \ 137 a_tmp = tri.conjugate(); \ 138 a = a_tmp.data(); \ 139 lda = convert_index<BlasIndex>(a_tmp.outerStride()); \ 140 } else { \ 141 a = _tri; \ 142 lda = convert_index<BlasIndex>(triStride); \ 143 } \ 144 if (IsUnitDiag) diag='U'; \ 145 /* call ?trsm*/ \ 146 BLASFUNC(&side, &uplo, &transa, &diag, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (BLASTYPE*)_other, &ldb); \ 147 /*std::cout << "TRMS_L specialization!\n";*/ \ 148 } \ 149 }; 150 151 #ifdef EIGEN_USE_MKL 152 EIGEN_BLAS_TRSM_R(double, double, dtrsm) 153 EIGEN_BLAS_TRSM_R(dcomplex, MKL_Complex16, ztrsm) 154 EIGEN_BLAS_TRSM_R(float, float, strsm) 155 EIGEN_BLAS_TRSM_R(scomplex, MKL_Complex8, ctrsm) 156 #else 157 EIGEN_BLAS_TRSM_R(double, double, dtrsm_) 158 EIGEN_BLAS_TRSM_R(dcomplex, double, ztrsm_) 159 EIGEN_BLAS_TRSM_R(float, float, strsm_) 160 EIGEN_BLAS_TRSM_R(scomplex, float, ctrsm_) 161 #endif 162 163 } // end namespace internal 164 165 } // end namespace Eigen 166 167 #endif // EIGEN_TRIANGULAR_SOLVER_MATRIX_BLAS_H 168