Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
TriangularSolverMatrix_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 * Triangular matrix-matrix solve functionality based on ?TRSM.
30 ********************************************************************************
31*/
32// SPDX-License-Identifier: BSD-3-Clause
33
34#ifndef EIGEN_TRIANGULAR_SOLVER_MATRIX_BLAS_H
35#define EIGEN_TRIANGULAR_SOLVER_MATRIX_BLAS_H
36
37// IWYU pragma: private
38#include "../InternalHeaderCheck.h"
39
40namespace Eigen {
41
42namespace internal {
43
44// implements LeftSide op(triangular)^-1 * general
45#define EIGEN_BLAS_TRSM_L(EIGTYPE, BLASTYPE, BLASFUNC) \
46 template <typename Index, int Mode, bool Conjugate, int TriStorageOrder> \
47 struct triangular_solve_matrix<EIGTYPE, Index, OnTheLeft, Mode, Conjugate, TriStorageOrder, ColMajor, 1> { \
48 using Scalar = EIGTYPE; \
49 enum { \
50 IsLower = (Mode & Lower) == Lower, \
51 IsUnitDiag = (Mode & UnitDiag) ? 1 : 0, \
52 IsZeroDiag = (Mode & ZeroDiag) ? 1 : 0, \
53 conjA = ((TriStorageOrder == ColMajor) && Conjugate) ? 1 : 0 \
54 }; \
55 static void run(Index size, Index otherSize, const EIGTYPE* _tri, Index triStride, Scalar* _other, \
56 Index otherIncr, Index otherStride, level3_blocking<EIGTYPE, EIGTYPE>& blocking) { \
57 if (size == 0 || otherSize == 0) return; \
58 if (!blas_compatible_stride<TriStorageOrder>(size, size, triStride) || \
59 !blas_compatible_stride<ColMajor>(size, otherSize, otherStride)) { \
60 triangular_solve_matrix<EIGTYPE, Index, OnTheLeft, Mode, Conjugate, TriStorageOrder, ColMajor, Dynamic>::run( \
61 size, otherSize, _tri, triStride, _other, otherIncr, otherStride, blocking); \
62 return; \
63 } \
64 EIGEN_ONLY_USED_FOR_DEBUG(otherIncr); \
65 eigen_assert(otherIncr == 1); \
66 BlasIndex m = convert_index<BlasIndex>(size), n = convert_index<BlasIndex>(otherSize), lda, ldb; \
67 char side = 'L', uplo, diag = 'N', transa; \
68 /* Set alpha_ */ \
69 EIGTYPE alpha(1); \
70 ldb = blas_leading_dim<ColMajor>(size, otherSize, otherStride); \
71 \
72 const EIGTYPE* a; \
73 /* Set trans */ \
74 transa = (TriStorageOrder == RowMajor) ? ((Conjugate) ? 'C' : 'T') : 'N'; \
75 /* Set uplo */ \
76 uplo = IsLower ? 'L' : 'U'; \
77 EIGEN_IF_CONSTEXPR (TriStorageOrder == RowMajor) { \
78 uplo = (uplo == 'L') ? 'U' : 'L'; \
79 } \
80 /* Set a, lda */ \
81 using MatrixTri = Matrix<EIGTYPE, Dynamic, Dynamic, TriStorageOrder>; \
82 Map<const MatrixTri, 0, OuterStride<> > tri(_tri, size, size, OuterStride<>(triStride)); \
83 MatrixTri a_tmp; \
84 \
85 EIGEN_IF_CONSTEXPR (conjA) { \
86 a_tmp = tri.conjugate(); \
87 a = a_tmp.data(); \
88 lda = convert_index<BlasIndex>(a_tmp.outerStride()); \
89 } else { \
90 a = _tri; \
91 lda = blas_leading_dim<TriStorageOrder>(size, size, triStride); \
92 } \
93 EIGEN_IF_CONSTEXPR (IsUnitDiag) { \
94 diag = 'U'; \
95 } \
96 /* call ?trsm*/ \
97 BLASFUNC(&side, &uplo, &transa, &diag, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, \
98 &lda, (BLASTYPE*)_other, &ldb); \
99 } \
100 };
101
102#ifdef EIGEN_USE_MKL
103EIGEN_BLAS_TRSM_L(double, double, dtrsm)
104EIGEN_BLAS_TRSM_L(dcomplex, MKL_Complex16, ztrsm)
105EIGEN_BLAS_TRSM_L(float, float, strsm)
106EIGEN_BLAS_TRSM_L(scomplex, MKL_Complex8, ctrsm)
107#else
108EIGEN_BLAS_TRSM_L(double, double, EIGEN_BLAS_SYM(dtrsm))
109EIGEN_BLAS_TRSM_L(dcomplex, double, EIGEN_BLAS_SYM(ztrsm))
110EIGEN_BLAS_TRSM_L(float, float, EIGEN_BLAS_SYM(strsm))
111EIGEN_BLAS_TRSM_L(scomplex, float, EIGEN_BLAS_SYM(ctrsm))
112#endif
113
114// implements RightSide general * op(triangular)^-1
115#define EIGEN_BLAS_TRSM_R(EIGTYPE, BLASTYPE, BLASFUNC) \
116 template <typename Index, int Mode, bool Conjugate, int TriStorageOrder> \
117 struct triangular_solve_matrix<EIGTYPE, Index, OnTheRight, Mode, Conjugate, TriStorageOrder, ColMajor, 1> { \
118 using Scalar = EIGTYPE; \
119 enum { \
120 IsLower = (Mode & Lower) == Lower, \
121 IsUnitDiag = (Mode & UnitDiag) ? 1 : 0, \
122 IsZeroDiag = (Mode & ZeroDiag) ? 1 : 0, \
123 conjA = ((TriStorageOrder == ColMajor) && Conjugate) ? 1 : 0 \
124 }; \
125 static void run(Index size, Index otherSize, const EIGTYPE* _tri, Index triStride, Scalar* _other, \
126 Index otherIncr, Index otherStride, level3_blocking<EIGTYPE, EIGTYPE>& blocking) { \
127 if (size == 0 || otherSize == 0) return; \
128 if (!blas_compatible_stride<TriStorageOrder>(size, size, triStride) || \
129 !blas_compatible_stride<ColMajor>(otherSize, size, otherStride)) { \
130 triangular_solve_matrix<EIGTYPE, Index, OnTheRight, Mode, Conjugate, TriStorageOrder, ColMajor, Dynamic>::run( \
131 size, otherSize, _tri, triStride, _other, otherIncr, otherStride, blocking); \
132 return; \
133 } \
134 EIGEN_ONLY_USED_FOR_DEBUG(otherIncr); \
135 eigen_assert(otherIncr == 1); \
136 BlasIndex m = convert_index<BlasIndex>(otherSize), n = convert_index<BlasIndex>(size), lda, ldb; \
137 char side = 'R', uplo, diag = 'N', transa; \
138 /* Set alpha_ */ \
139 EIGTYPE alpha(1); \
140 ldb = blas_leading_dim<ColMajor>(otherSize, size, otherStride); \
141 \
142 const EIGTYPE* a; \
143 /* Set trans */ \
144 transa = (TriStorageOrder == RowMajor) ? ((Conjugate) ? 'C' : 'T') : 'N'; \
145 /* Set uplo */ \
146 uplo = IsLower ? 'L' : 'U'; \
147 EIGEN_IF_CONSTEXPR (TriStorageOrder == RowMajor) { \
148 uplo = (uplo == 'L') ? 'U' : 'L'; \
149 } \
150 /* Set a, lda */ \
151 using MatrixTri = Matrix<EIGTYPE, Dynamic, Dynamic, TriStorageOrder>; \
152 Map<const MatrixTri, 0, OuterStride<> > tri(_tri, size, size, OuterStride<>(triStride)); \
153 MatrixTri a_tmp; \
154 \
155 EIGEN_IF_CONSTEXPR (conjA) { \
156 a_tmp = tri.conjugate(); \
157 a = a_tmp.data(); \
158 lda = convert_index<BlasIndex>(a_tmp.outerStride()); \
159 } else { \
160 a = _tri; \
161 lda = blas_leading_dim<TriStorageOrder>(size, size, triStride); \
162 } \
163 EIGEN_IF_CONSTEXPR (IsUnitDiag) { \
164 diag = 'U'; \
165 } \
166 /* call ?trsm*/ \
167 BLASFUNC(&side, &uplo, &transa, &diag, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, \
168 &lda, (BLASTYPE*)_other, &ldb); \
169 /*std::cout << "TRMS_L specialization!\n";*/ \
170 } \
171 };
172
173#ifdef EIGEN_USE_MKL
174EIGEN_BLAS_TRSM_R(double, double, dtrsm)
175EIGEN_BLAS_TRSM_R(dcomplex, MKL_Complex16, ztrsm)
176EIGEN_BLAS_TRSM_R(float, float, strsm)
177EIGEN_BLAS_TRSM_R(scomplex, MKL_Complex8, ctrsm)
178#else
179EIGEN_BLAS_TRSM_R(double, double, EIGEN_BLAS_SYM(dtrsm))
180EIGEN_BLAS_TRSM_R(dcomplex, double, EIGEN_BLAS_SYM(ztrsm))
181EIGEN_BLAS_TRSM_R(float, float, EIGEN_BLAS_SYM(strsm))
182EIGEN_BLAS_TRSM_R(scomplex, float, EIGEN_BLAS_SYM(ctrsm))
183#endif
184
185#undef EIGEN_BLAS_TRSM_R
186#undef EIGEN_BLAS_TRSM_L
187} // end namespace internal
188
189} // end namespace Eigen
190
191#endif // EIGEN_TRIANGULAR_SOLVER_MATRIX_BLAS_H