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