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