SelfadjointMatrixMatrix_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  * Self adjoint matrix * matrix product functionality based on ?SYMM/?HEMM.
30  ********************************************************************************
31 */
32 
33 #ifndef EIGEN_SELFADJOINT_MATRIX_MATRIX_BLAS_H
34 #define EIGEN_SELFADJOINT_MATRIX_MATRIX_BLAS_H
35 
36 #include "../InternalHeaderCheck.h"
37 
38 namespace Eigen {
39 
40 namespace internal {
41 
42 
43 /* Optimized selfadjoint matrix * matrix (?SYMM/?HEMM) product */
44 
45 #define EIGEN_BLAS_SYMM_L(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC) \
46 template <typename Index, \
47  int LhsStorageOrder, bool ConjugateLhs, \
48  int RhsStorageOrder, bool ConjugateRhs> \
49 struct product_selfadjoint_matrix<EIGTYPE,Index,LhsStorageOrder,true,ConjugateLhs,RhsStorageOrder,false,ConjugateRhs,ColMajor,1> \
50 {\
51 \
52  static void run( \
53  Index rows, Index cols, \
54  const EIGTYPE* _lhs, Index lhsStride, \
55  const EIGTYPE* _rhs, Index rhsStride, \
56  EIGTYPE* res, Index resIncr, Index resStride, \
57  EIGTYPE alpha, level3_blocking<EIGTYPE, EIGTYPE>& /*blocking*/) \
58  { \
59  EIGEN_ONLY_USED_FOR_DEBUG(resIncr); \
60  eigen_assert(resIncr == 1); \
61  char side='L', uplo='L'; \
62  BlasIndex m, n, lda, ldb, ldc; \
63  const EIGTYPE *a, *b; \
64  EIGTYPE beta(1); \
65  MatrixX##EIGPREFIX b_tmp; \
66 \
67 /* Set transpose options */ \
68 /* Set m, n, k */ \
69  m = convert_index<BlasIndex>(rows); \
70  n = convert_index<BlasIndex>(cols); \
71 \
72 /* Set lda, ldb, ldc */ \
73  lda = convert_index<BlasIndex>(lhsStride); \
74  ldb = convert_index<BlasIndex>(rhsStride); \
75  ldc = convert_index<BlasIndex>(resStride); \
76 \
77 /* Set a, b, c */ \
78  if (LhsStorageOrder==RowMajor) uplo='U'; \
79  a = _lhs; \
80 \
81  if (RhsStorageOrder==RowMajor) { \
82  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > rhs(_rhs,n,m,OuterStride<>(rhsStride)); \
83  b_tmp = rhs.adjoint(); \
84  b = b_tmp.data(); \
85  ldb = convert_index<BlasIndex>(b_tmp.outerStride()); \
86  } else b = _rhs; \
87 \
88  BLASFUNC(&side, &uplo, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (const BLASTYPE*)b, &ldb, (const BLASTYPE*)&numext::real_ref(beta), (BLASTYPE*)res, &ldc); \
89 \
90  } \
91 };
92 
93 
94 #define EIGEN_BLAS_HEMM_L(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC) \
95 template <typename Index, \
96  int LhsStorageOrder, bool ConjugateLhs, \
97  int RhsStorageOrder, bool ConjugateRhs> \
98 struct product_selfadjoint_matrix<EIGTYPE,Index,LhsStorageOrder,true,ConjugateLhs,RhsStorageOrder,false,ConjugateRhs,ColMajor,1> \
99 {\
100  static void run( \
101  Index rows, Index cols, \
102  const EIGTYPE* _lhs, Index lhsStride, \
103  const EIGTYPE* _rhs, Index rhsStride, \
104  EIGTYPE* res, Index resIncr, Index resStride, \
105  EIGTYPE alpha, level3_blocking<EIGTYPE, EIGTYPE>& /*blocking*/) \
106  { \
107  EIGEN_ONLY_USED_FOR_DEBUG(resIncr); \
108  eigen_assert(resIncr == 1); \
109  char side='L', uplo='L'; \
110  BlasIndex m, n, lda, ldb, ldc; \
111  const EIGTYPE *a, *b; \
112  EIGTYPE beta(1); \
113  MatrixX##EIGPREFIX b_tmp; \
114  Matrix<EIGTYPE, Dynamic, Dynamic, LhsStorageOrder> a_tmp; \
115 \
116 /* Set transpose options */ \
117 /* Set m, n, k */ \
118  m = convert_index<BlasIndex>(rows); \
119  n = convert_index<BlasIndex>(cols); \
120 \
121 /* Set lda, ldb, ldc */ \
122  lda = convert_index<BlasIndex>(lhsStride); \
123  ldb = convert_index<BlasIndex>(rhsStride); \
124  ldc = convert_index<BlasIndex>(resStride); \
125 \
126 /* Set a, b, c */ \
127  if (((LhsStorageOrder==ColMajor) && ConjugateLhs) || ((LhsStorageOrder==RowMajor) && (!ConjugateLhs))) { \
128  Map<const Matrix<EIGTYPE, Dynamic, Dynamic, LhsStorageOrder>, 0, OuterStride<> > lhs(_lhs,m,m,OuterStride<>(lhsStride)); \
129  a_tmp = lhs.conjugate(); \
130  a = a_tmp.data(); \
131  lda = convert_index<BlasIndex>(a_tmp.outerStride()); \
132  } else a = _lhs; \
133  if (LhsStorageOrder==RowMajor) uplo='U'; \
134 \
135  if (RhsStorageOrder==ColMajor && (!ConjugateRhs)) { \
136  b = _rhs; } \
137  else { \
138  if (RhsStorageOrder==ColMajor && ConjugateRhs) { \
139  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > rhs(_rhs,m,n,OuterStride<>(rhsStride)); \
140  b_tmp = rhs.conjugate(); \
141  } else \
142  if (ConjugateRhs) { \
143  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > rhs(_rhs,n,m,OuterStride<>(rhsStride)); \
144  b_tmp = rhs.adjoint(); \
145  } else { \
146  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > rhs(_rhs,n,m,OuterStride<>(rhsStride)); \
147  b_tmp = rhs.transpose(); \
148  } \
149  b = b_tmp.data(); \
150  ldb = convert_index<BlasIndex>(b_tmp.outerStride()); \
151  } \
152 \
153  BLASFUNC(&side, &uplo, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (const BLASTYPE*)b, &ldb, (const BLASTYPE*)&numext::real_ref(beta), (BLASTYPE*)res, &ldc); \
154 \
155  } \
156 };
157 
158 #ifdef EIGEN_USE_MKL
159 EIGEN_BLAS_SYMM_L(double, double, d, dsymm)
160 EIGEN_BLAS_SYMM_L(float, float, f, ssymm)
161 EIGEN_BLAS_HEMM_L(dcomplex, MKL_Complex16, cd, zhemm)
162 EIGEN_BLAS_HEMM_L(scomplex, MKL_Complex8, cf, chemm)
163 #else
164 EIGEN_BLAS_SYMM_L(double, double, d, dsymm_)
165 EIGEN_BLAS_SYMM_L(float, float, f, ssymm_)
166 EIGEN_BLAS_HEMM_L(dcomplex, double, cd, zhemm_)
167 EIGEN_BLAS_HEMM_L(scomplex, float, cf, chemm_)
168 #endif
169 
170 /* Optimized matrix * selfadjoint matrix (?SYMM/?HEMM) product */
171 
172 #define EIGEN_BLAS_SYMM_R(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC) \
173 template <typename Index, \
174  int LhsStorageOrder, bool ConjugateLhs, \
175  int RhsStorageOrder, bool ConjugateRhs> \
176 struct product_selfadjoint_matrix<EIGTYPE,Index,LhsStorageOrder,false,ConjugateLhs,RhsStorageOrder,true,ConjugateRhs,ColMajor,1> \
177 {\
178 \
179  static void run( \
180  Index rows, Index cols, \
181  const EIGTYPE* _lhs, Index lhsStride, \
182  const EIGTYPE* _rhs, Index rhsStride, \
183  EIGTYPE* res, Index resIncr, Index resStride, \
184  EIGTYPE alpha, level3_blocking<EIGTYPE, EIGTYPE>& /*blocking*/) \
185  { \
186  EIGEN_ONLY_USED_FOR_DEBUG(resIncr); \
187  eigen_assert(resIncr == 1); \
188  char side='R', uplo='L'; \
189  BlasIndex m, n, lda, ldb, ldc; \
190  const EIGTYPE *a, *b; \
191  EIGTYPE beta(1); \
192  MatrixX##EIGPREFIX b_tmp; \
193 \
194 /* Set m, n, k */ \
195  m = convert_index<BlasIndex>(rows); \
196  n = convert_index<BlasIndex>(cols); \
197 \
198 /* Set lda, ldb, ldc */ \
199  lda = convert_index<BlasIndex>(rhsStride); \
200  ldb = convert_index<BlasIndex>(lhsStride); \
201  ldc = convert_index<BlasIndex>(resStride); \
202 \
203 /* Set a, b, c */ \
204  if (RhsStorageOrder==RowMajor) uplo='U'; \
205  a = _rhs; \
206 \
207  if (LhsStorageOrder==RowMajor) { \
208  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > lhs(_lhs,n,m,OuterStride<>(rhsStride)); \
209  b_tmp = lhs.adjoint(); \
210  b = b_tmp.data(); \
211  ldb = convert_index<BlasIndex>(b_tmp.outerStride()); \
212  } else b = _lhs; \
213 \
214  BLASFUNC(&side, &uplo, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (const BLASTYPE*)b, &ldb, (const BLASTYPE*)&numext::real_ref(beta), (BLASTYPE*)res, &ldc); \
215 \
216  } \
217 };
218 
219 
220 #define EIGEN_BLAS_HEMM_R(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC) \
221 template <typename Index, \
222  int LhsStorageOrder, bool ConjugateLhs, \
223  int RhsStorageOrder, bool ConjugateRhs> \
224 struct product_selfadjoint_matrix<EIGTYPE,Index,LhsStorageOrder,false,ConjugateLhs,RhsStorageOrder,true,ConjugateRhs,ColMajor,1> \
225 {\
226  static void run( \
227  Index rows, Index cols, \
228  const EIGTYPE* _lhs, Index lhsStride, \
229  const EIGTYPE* _rhs, Index rhsStride, \
230  EIGTYPE* res, Index resIncr, Index resStride, \
231  EIGTYPE alpha, level3_blocking<EIGTYPE, EIGTYPE>& /*blocking*/) \
232  { \
233  EIGEN_ONLY_USED_FOR_DEBUG(resIncr); \
234  eigen_assert(resIncr == 1); \
235  char side='R', uplo='L'; \
236  BlasIndex m, n, lda, ldb, ldc; \
237  const EIGTYPE *a, *b; \
238  EIGTYPE beta(1); \
239  MatrixX##EIGPREFIX b_tmp; \
240  Matrix<EIGTYPE, Dynamic, Dynamic, RhsStorageOrder> a_tmp; \
241 \
242 /* Set m, n, k */ \
243  m = convert_index<BlasIndex>(rows); \
244  n = convert_index<BlasIndex>(cols); \
245 \
246 /* Set lda, ldb, ldc */ \
247  lda = convert_index<BlasIndex>(rhsStride); \
248  ldb = convert_index<BlasIndex>(lhsStride); \
249  ldc = convert_index<BlasIndex>(resStride); \
250 \
251 /* Set a, b, c */ \
252  if (((RhsStorageOrder==ColMajor) && ConjugateRhs) || ((RhsStorageOrder==RowMajor) && (!ConjugateRhs))) { \
253  Map<const Matrix<EIGTYPE, Dynamic, Dynamic, RhsStorageOrder>, 0, OuterStride<> > rhs(_rhs,n,n,OuterStride<>(rhsStride)); \
254  a_tmp = rhs.conjugate(); \
255  a = a_tmp.data(); \
256  lda = convert_index<BlasIndex>(a_tmp.outerStride()); \
257  } else a = _rhs; \
258  if (RhsStorageOrder==RowMajor) uplo='U'; \
259 \
260  if (LhsStorageOrder==ColMajor && (!ConjugateLhs)) { \
261  b = _lhs; } \
262  else { \
263  if (LhsStorageOrder==ColMajor && ConjugateLhs) { \
264  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > lhs(_lhs,m,n,OuterStride<>(lhsStride)); \
265  b_tmp = lhs.conjugate(); \
266  } else \
267  if (ConjugateLhs) { \
268  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > lhs(_lhs,n,m,OuterStride<>(lhsStride)); \
269  b_tmp = lhs.adjoint(); \
270  } else { \
271  Map<const MatrixX##EIGPREFIX, 0, OuterStride<> > lhs(_lhs,n,m,OuterStride<>(lhsStride)); \
272  b_tmp = lhs.transpose(); \
273  } \
274  b = b_tmp.data(); \
275  ldb = convert_index<BlasIndex>(b_tmp.outerStride()); \
276  } \
277 \
278  BLASFUNC(&side, &uplo, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (const BLASTYPE*)b, &ldb, (const BLASTYPE*)&numext::real_ref(beta), (BLASTYPE*)res, &ldc); \
279  } \
280 };
281 
282 #ifdef EIGEN_USE_MKL
283 EIGEN_BLAS_SYMM_R(double, double, d, dsymm)
284 EIGEN_BLAS_SYMM_R(float, float, f, ssymm)
285 EIGEN_BLAS_HEMM_R(dcomplex, MKL_Complex16, cd, zhemm)
286 EIGEN_BLAS_HEMM_R(scomplex, MKL_Complex8, cf, chemm)
287 #else
288 EIGEN_BLAS_SYMM_R(double, double, d, dsymm_)
289 EIGEN_BLAS_SYMM_R(float, float, f, ssymm_)
290 EIGEN_BLAS_HEMM_R(dcomplex, double, cd, zhemm_)
291 EIGEN_BLAS_HEMM_R(scomplex, float, cf, chemm_)
292 #endif
293 } // end namespace internal
294 
295 } // end namespace Eigen
296 
297 #endif // EIGEN_SELFADJOINT_MATRIX_MATRIX_BLAS_H
#define EIGEN_BLAS_HEMM_L(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC)
#define EIGEN_BLAS_HEMM_R(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC)
#define EIGEN_BLAS_SYMM_R(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC)
#define EIGEN_BLAS_SYMM_L(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC)
int BLASFUNC() chemm(const char *, const char *, const int *, const int *, const float *, const float *, const int *, const float *, const int *, const float *, float *, const int *)
int BLASFUNC() zhemm(const char *, const char *, const int *, const int *, const double *, const double *, const int *, const double *, const int *, const double *, double *, const int *)
int BLASFUNC() dsymm(const char *, const char *, const int *, const int *, const double *, const double *, const int *, const double *, const int *, const double *, double *, const int *)
int BLASFUNC() ssymm(const char *, const char *, const int *, const int *, const float *, const float *, const int *, const float *, const int *, const float *, float *, const int *)
: InteropHeaders
Definition: Core:139
std::complex< double > dcomplex
Definition: MKL_support.h:127
std::complex< float > scomplex
Definition: MKL_support.h:128