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