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-vector product functionality based on ?TRMV. 30 ******************************************************************************** 31 */ 32 33 #ifndef EIGEN_TRIANGULAR_MATRIX_VECTOR_BLAS_H 34 #define EIGEN_TRIANGULAR_MATRIX_VECTOR_BLAS_H 35 36 namespace Eigen { 37 38 namespace internal { 39 40 /********************************************************************** 41 * This file implements triangular matrix-vector multiplication using BLAS 42 **********************************************************************/ 43 44 // trmv/hemv specialization 45 46 template<typename Index, int Mode, typename LhsScalar, bool ConjLhs, typename RhsScalar, bool ConjRhs, int StorageOrder> 47 struct triangular_matrix_vector_product_trmv : 48 triangular_matrix_vector_product<Index,Mode,LhsScalar,ConjLhs,RhsScalar,ConjRhs,StorageOrder,BuiltIn> {}; 49 50 #define EIGEN_BLAS_TRMV_SPECIALIZE(Scalar) \ 51 template<typename Index, int Mode, bool ConjLhs, bool ConjRhs> \ 52 struct triangular_matrix_vector_product<Index,Mode,Scalar,ConjLhs,Scalar,ConjRhs,ColMajor,Specialized> { \ 53 static void run(Index _rows, Index _cols, const Scalar* _lhs, Index lhsStride, \ 54 const Scalar* _rhs, Index rhsIncr, Scalar* _res, Index resIncr, Scalar alpha) { \ 55 triangular_matrix_vector_product_trmv<Index,Mode,Scalar,ConjLhs,Scalar,ConjRhs,ColMajor>::run( \ 56 _rows, _cols, _lhs, lhsStride, _rhs, rhsIncr, _res, resIncr, alpha); \ 57 } \ 58 }; \ 59 template<typename Index, int Mode, bool ConjLhs, bool ConjRhs> \ 60 struct triangular_matrix_vector_product<Index,Mode,Scalar,ConjLhs,Scalar,ConjRhs,RowMajor,Specialized> { \ 61 static void run(Index _rows, Index _cols, const Scalar* _lhs, Index lhsStride, \ 62 const Scalar* _rhs, Index rhsIncr, Scalar* _res, Index resIncr, Scalar alpha) { \ 63 triangular_matrix_vector_product_trmv<Index,Mode,Scalar,ConjLhs,Scalar,ConjRhs,RowMajor>::run( \ 64 _rows, _cols, _lhs, lhsStride, _rhs, rhsIncr, _res, resIncr, alpha); \ 65 } \ 66 }; 67 68 EIGEN_BLAS_TRMV_SPECIALIZE(double) 69 EIGEN_BLAS_TRMV_SPECIALIZE(float) 70 EIGEN_BLAS_TRMV_SPECIALIZE(dcomplex) 71 EIGEN_BLAS_TRMV_SPECIALIZE(scomplex) 72 73 // implements col-major: res += alpha * op(triangular) * vector 74 #define EIGEN_BLAS_TRMV_CM(EIGTYPE, BLASTYPE, EIGPREFIX, BLASPREFIX) \ 75 template<typename Index, int Mode, bool ConjLhs, bool ConjRhs> \ 76 struct triangular_matrix_vector_product_trmv<Index,Mode,EIGTYPE,ConjLhs,EIGTYPE,ConjRhs,ColMajor> { \ 77 enum { \ 78 IsLower = (Mode&Lower) == Lower, \ 79 SetDiag = (Mode&(ZeroDiag|UnitDiag)) ? 0 : 1, \ 80 IsUnitDiag = (Mode&UnitDiag) ? 1 : 0, \ 81 IsZeroDiag = (Mode&ZeroDiag) ? 1 : 0, \ 82 LowUp = IsLower ? Lower : Upper \ 83 }; \ 84 static void run(Index _rows, Index _cols, const EIGTYPE* _lhs, Index lhsStride, \ 85 const EIGTYPE* _rhs, Index rhsIncr, EIGTYPE* _res, Index resIncr, EIGTYPE alpha) \ 86 { \ 87 if (ConjLhs || IsZeroDiag) { \ 88 triangular_matrix_vector_product<Index,Mode,EIGTYPE,ConjLhs,EIGTYPE,ConjRhs,ColMajor,BuiltIn>::run( \ 89 _rows, _cols, _lhs, lhsStride, _rhs, rhsIncr, _res, resIncr, alpha); \ 90 return; \ 91 }\ 92 Index size = (std::min)(_rows,_cols); \ 93 Index rows = IsLower ? _rows : size; \ 94 Index cols = IsLower ? size : _cols; \ 95 \ 96 typedef VectorX##EIGPREFIX VectorRhs; \ 97 EIGTYPE *x, *y;\ 98 \ 99 /* Set x*/ \ 100 Map<const VectorRhs, 0, InnerStride<> > rhs(_rhs,cols,InnerStride<>(rhsIncr)); \ 101 VectorRhs x_tmp; \ 102 if (ConjRhs) x_tmp = rhs.conjugate(); else x_tmp = rhs; \ 103 x = x_tmp.data(); \ 104 \ 105 /* Square part handling */\ 106 \ 107 char trans, uplo, diag; \ 108 BlasIndex m, n, lda, incx, incy; \ 109 EIGTYPE const *a; \ 110 EIGTYPE beta(1); \ 111 \ 112 /* Set m, n */ \ 113 n = convert_index<BlasIndex>(size); \ 114 lda = convert_index<BlasIndex>(lhsStride); \ 115 incx = 1; \ 116 incy = convert_index<BlasIndex>(resIncr); \ 117 \ 118 /* Set uplo, trans and diag*/ \ 119 trans = 'N'; \ 120 uplo = IsLower ? 'L' : 'U'; \ 121 diag = IsUnitDiag ? 'U' : 'N'; \ 122 \ 123 /* call ?TRMV*/ \ 124 BLASPREFIX##trmv_(&uplo, &trans, &diag, &n, (const BLASTYPE*)_lhs, &lda, (BLASTYPE*)x, &incx); \ 125 \ 126 /* Add op(a_tr)rhs into res*/ \ 127 BLASPREFIX##axpy_(&n, &numext::real_ref(alpha),(const BLASTYPE*)x, &incx, (BLASTYPE*)_res, &incy); \ 128 /* Non-square case - doesn't fit to BLAS ?TRMV. Fall to default triangular product*/ \ 129 if (size<(std::max)(rows,cols)) { \ 130 if (ConjRhs) x_tmp = rhs.conjugate(); else x_tmp = rhs; \ 131 x = x_tmp.data(); \ 132 if (size<rows) { \ 133 y = _res + size*resIncr; \ 134 a = _lhs + size; \ 135 m = convert_index<BlasIndex>(rows-size); \ 136 n = convert_index<BlasIndex>(size); \ 137 } \ 138 else { \ 139 x += size; \ 140 y = _res; \ 141 a = _lhs + size*lda; \ 142 m = convert_index<BlasIndex>(size); \ 143 n = convert_index<BlasIndex>(cols-size); \ 144 } \ 145 BLASPREFIX##gemv_(&trans, &m, &n, &numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (const BLASTYPE*)x, &incx, &numext::real_ref(beta), (BLASTYPE*)y, &incy); \ 146 } \ 147 } \ 148 }; 149 150 EIGEN_BLAS_TRMV_CM(double, double, d, d) 151 EIGEN_BLAS_TRMV_CM(dcomplex, double, cd, z) 152 EIGEN_BLAS_TRMV_CM(float, float, f, s) 153 EIGEN_BLAS_TRMV_CM(scomplex, float, cf, c) 154 155 // implements row-major: res += alpha * op(triangular) * vector 156 #define EIGEN_BLAS_TRMV_RM(EIGTYPE, BLASTYPE, EIGPREFIX, BLASPREFIX) \ 157 template<typename Index, int Mode, bool ConjLhs, bool ConjRhs> \ 158 struct triangular_matrix_vector_product_trmv<Index,Mode,EIGTYPE,ConjLhs,EIGTYPE,ConjRhs,RowMajor> { \ 159 enum { \ 160 IsLower = (Mode&Lower) == Lower, \ 161 SetDiag = (Mode&(ZeroDiag|UnitDiag)) ? 0 : 1, \ 162 IsUnitDiag = (Mode&UnitDiag) ? 1 : 0, \ 163 IsZeroDiag = (Mode&ZeroDiag) ? 1 : 0, \ 164 LowUp = IsLower ? Lower : Upper \ 165 }; \ 166 static void run(Index _rows, Index _cols, const EIGTYPE* _lhs, Index lhsStride, \ 167 const EIGTYPE* _rhs, Index rhsIncr, EIGTYPE* _res, Index resIncr, EIGTYPE alpha) \ 168 { \ 169 if (IsZeroDiag) { \ 170 triangular_matrix_vector_product<Index,Mode,EIGTYPE,ConjLhs,EIGTYPE,ConjRhs,RowMajor,BuiltIn>::run( \ 171 _rows, _cols, _lhs, lhsStride, _rhs, rhsIncr, _res, resIncr, alpha); \ 172 return; \ 173 }\ 174 Index size = (std::min)(_rows,_cols); \ 175 Index rows = IsLower ? _rows : size; \ 176 Index cols = IsLower ? size : _cols; \ 177 \ 178 typedef VectorX##EIGPREFIX VectorRhs; \ 179 EIGTYPE *x, *y;\ 180 \ 181 /* Set x*/ \ 182 Map<const VectorRhs, 0, InnerStride<> > rhs(_rhs,cols,InnerStride<>(rhsIncr)); \ 183 VectorRhs x_tmp; \ 184 if (ConjRhs) x_tmp = rhs.conjugate(); else x_tmp = rhs; \ 185 x = x_tmp.data(); \ 186 \ 187 /* Square part handling */\ 188 \ 189 char trans, uplo, diag; \ 190 BlasIndex m, n, lda, incx, incy; \ 191 EIGTYPE const *a; \ 192 EIGTYPE beta(1); \ 193 \ 194 /* Set m, n */ \ 195 n = convert_index<BlasIndex>(size); \ 196 lda = convert_index<BlasIndex>(lhsStride); \ 197 incx = 1; \ 198 incy = convert_index<BlasIndex>(resIncr); \ 199 \ 200 /* Set uplo, trans and diag*/ \ 201 trans = ConjLhs ? 'C' : 'T'; \ 202 uplo = IsLower ? 'U' : 'L'; \ 203 diag = IsUnitDiag ? 'U' : 'N'; \ 204 \ 205 /* call ?TRMV*/ \ 206 BLASPREFIX##trmv_(&uplo, &trans, &diag, &n, (const BLASTYPE*)_lhs, &lda, (BLASTYPE*)x, &incx); \ 207 \ 208 /* Add op(a_tr)rhs into res*/ \ 209 BLASPREFIX##axpy_(&n, &numext::real_ref(alpha),(const BLASTYPE*)x, &incx, (BLASTYPE*)_res, &incy); \ 210 /* Non-square case - doesn't fit to BLAS ?TRMV. Fall to default triangular product*/ \ 211 if (size<(std::max)(rows,cols)) { \ 212 if (ConjRhs) x_tmp = rhs.conjugate(); else x_tmp = rhs; \ 213 x = x_tmp.data(); \ 214 if (size<rows) { \ 215 y = _res + size*resIncr; \ 216 a = _lhs + size*lda; \ 217 m = convert_index<BlasIndex>(rows-size); \ 218 n = convert_index<BlasIndex>(size); \ 219 } \ 220 else { \ 221 x += size; \ 222 y = _res; \ 223 a = _lhs + size; \ 224 m = convert_index<BlasIndex>(size); \ 225 n = convert_index<BlasIndex>(cols-size); \ 226 } \ 227 BLASPREFIX##gemv_(&trans, &n, &m, &numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (const BLASTYPE*)x, &incx, &numext::real_ref(beta), (BLASTYPE*)y, &incy); \ 228 } \ 229 } \ 230 }; 231 232 EIGEN_BLAS_TRMV_RM(double, double, d, d) 233 EIGEN_BLAS_TRMV_RM(dcomplex, double, cd, z) 234 EIGEN_BLAS_TRMV_RM(float, float, f, s) 235 EIGEN_BLAS_TRMV_RM(scomplex, float, cf, c) 236 237 } // end namespase internal 238 239 } // end namespace Eigen 240 241 #endif // EIGEN_TRIANGULAR_MATRIX_VECTOR_BLAS_H 242