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