TriangularMatrixMatrix_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_MATRIX_MATRIX_BLAS_H
34 #define EIGEN_TRIANGULAR_MATRIX_MATRIX_BLAS_H
35 
36 namespace Eigen {
37 
38 namespace internal {
39 
40 
41 template <typename Scalar, typename Index,
42  int Mode, bool LhsIsTriangular,
43  int LhsStorageOrder, bool ConjugateLhs,
44  int RhsStorageOrder, bool ConjugateRhs,
45  int ResStorageOrder>
47  product_triangular_matrix_matrix<Scalar,Index,Mode,
48  LhsIsTriangular,LhsStorageOrder,ConjugateLhs,
49  RhsStorageOrder, ConjugateRhs, ResStorageOrder, BuiltIn> {};
50 
51 
52 // try to go to BLAS specialization
53 #define EIGEN_BLAS_TRMM_SPECIALIZE(Scalar, LhsIsTriangular) \
54 template <typename Index, int Mode, \
55  int LhsStorageOrder, bool ConjugateLhs, \
56  int RhsStorageOrder, bool ConjugateRhs> \
57 struct product_triangular_matrix_matrix<Scalar,Index, Mode, LhsIsTriangular, \
58  LhsStorageOrder,ConjugateLhs, RhsStorageOrder,ConjugateRhs,ColMajor,Specialized> { \
59  static inline void run(Index _rows, Index _cols, Index _depth, const Scalar* _lhs, Index lhsStride,\
60  const Scalar* _rhs, Index rhsStride, Scalar* res, Index resStride, Scalar alpha, level3_blocking<Scalar,Scalar>& blocking) { \
61  product_triangular_matrix_matrix_trmm<Scalar,Index,Mode, \
62  LhsIsTriangular,LhsStorageOrder,ConjugateLhs, \
63  RhsStorageOrder, ConjugateRhs, ColMajor>::run( \
64  _rows, _cols, _depth, _lhs, lhsStride, _rhs, rhsStride, res, resStride, alpha, blocking); \
65  } \
66 };
67 
68 EIGEN_BLAS_TRMM_SPECIALIZE(double, true)
69 EIGEN_BLAS_TRMM_SPECIALIZE(double, false)
71 EIGEN_BLAS_TRMM_SPECIALIZE(dcomplex, false)
72 EIGEN_BLAS_TRMM_SPECIALIZE(float, true)
73 EIGEN_BLAS_TRMM_SPECIALIZE(float, false)
75 EIGEN_BLAS_TRMM_SPECIALIZE(scomplex, false)
76 
77 // implements col-major += alpha * op(triangular) * op(general)
78 #define EIGEN_BLAS_TRMM_L(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC) \
79 template <typename Index, int Mode, \
80  int LhsStorageOrder, bool ConjugateLhs, \
81  int RhsStorageOrder, bool ConjugateRhs> \
82 struct product_triangular_matrix_matrix_trmm<EIGTYPE,Index,Mode,true, \
83  LhsStorageOrder,ConjugateLhs,RhsStorageOrder,ConjugateRhs,ColMajor> \
84 { \
85  enum { \
86  IsLower = (Mode&Lower) == Lower, \
87  SetDiag = (Mode&(ZeroDiag|UnitDiag)) ? 0 : 1, \
88  IsUnitDiag = (Mode&UnitDiag) ? 1 : 0, \
89  IsZeroDiag = (Mode&ZeroDiag) ? 1 : 0, \
90  LowUp = IsLower ? Lower : Upper, \
91  conjA = ((LhsStorageOrder==ColMajor) && ConjugateLhs) ? 1 : 0 \
92  }; \
93 \
94  static void run( \
95  Index _rows, Index _cols, Index _depth, \
96  const EIGTYPE* _lhs, Index lhsStride, \
97  const EIGTYPE* _rhs, Index rhsStride, \
98  EIGTYPE* res, Index resStride, \
99  EIGTYPE alpha, level3_blocking<EIGTYPE,EIGTYPE>& blocking) \
100  { \
101  Index diagSize = (std::min)(_rows,_depth); \
102  Index rows = IsLower ? _rows : diagSize; \
103  Index depth = IsLower ? diagSize : _depth; \
104  Index cols = _cols; \
105 \
106  typedef Matrix<EIGTYPE, Dynamic, Dynamic, LhsStorageOrder> MatrixLhs; \
107  typedef Matrix<EIGTYPE, Dynamic, Dynamic, RhsStorageOrder> MatrixRhs; \
108 \
109 /* Non-square case - doesn't fit to BLAS ?TRMM. Fall to default triangular product or call BLAS ?GEMM*/ \
110  if (rows != depth) { \
111 \
112  /* FIXME handle mkl_domain_get_max_threads */ \
113  /*int nthr = mkl_domain_get_max_threads(EIGEN_BLAS_DOMAIN_BLAS);*/ int nthr = 1;\
114 \
115  if (((nthr==1) && (((std::max)(rows,depth)-diagSize)/(double)diagSize < 0.5))) { \
116  /* Most likely no benefit to call TRMM or GEMM from BLAS */ \
117  product_triangular_matrix_matrix<EIGTYPE,Index,Mode,true, \
118  LhsStorageOrder,ConjugateLhs, RhsStorageOrder, ConjugateRhs, ColMajor, BuiltIn>::run( \
119  _rows, _cols, _depth, _lhs, lhsStride, _rhs, rhsStride, res, resStride, alpha, blocking); \
120  /*std::cout << "TRMM_L: A is not square! Go to Eigen TRMM implementation!\n";*/ \
121  } else { \
122  /* Make sense to call GEMM */ \
123  Map<const MatrixLhs, 0, OuterStride<> > lhsMap(_lhs,rows,depth,OuterStride<>(lhsStride)); \
124  MatrixLhs aa_tmp=lhsMap.template triangularView<Mode>(); \
125  BlasIndex aStride = convert_index<BlasIndex>(aa_tmp.outerStride()); \
126  gemm_blocking_space<ColMajor,EIGTYPE,EIGTYPE,Dynamic,Dynamic,Dynamic> gemm_blocking(_rows,_cols,_depth, 1, true); \
127  general_matrix_matrix_product<Index,EIGTYPE,LhsStorageOrder,ConjugateLhs,EIGTYPE,RhsStorageOrder,ConjugateRhs,ColMajor>::run( \
128  rows, cols, depth, aa_tmp.data(), aStride, _rhs, rhsStride, res, resStride, alpha, gemm_blocking, 0); \
129 \
130  /*std::cout << "TRMM_L: A is not square! Go to BLAS GEMM implementation! " << nthr<<" \n";*/ \
131  } \
132  return; \
133  } \
134  char side = 'L', transa, uplo, diag = 'N'; \
135  EIGTYPE *b; \
136  const EIGTYPE *a; \
137  BlasIndex m, n, lda, ldb; \
138 \
139 /* Set m, n */ \
140  m = convert_index<BlasIndex>(diagSize); \
141  n = convert_index<BlasIndex>(cols); \
142 \
143 /* Set trans */ \
144  transa = (LhsStorageOrder==RowMajor) ? ((ConjugateLhs) ? 'C' : 'T') : 'N'; \
145 \
146 /* Set b, ldb */ \
147  Map<const MatrixRhs, 0, OuterStride<> > rhs(_rhs,depth,cols,OuterStride<>(rhsStride)); \
148  MatrixX##EIGPREFIX b_tmp; \
149 \
150  if (ConjugateRhs) b_tmp = rhs.conjugate(); else b_tmp = rhs; \
151  b = b_tmp.data(); \
152  ldb = convert_index<BlasIndex>(b_tmp.outerStride()); \
153 \
154 /* Set uplo */ \
155  uplo = IsLower ? 'L' : 'U'; \
156  if (LhsStorageOrder==RowMajor) uplo = (uplo == 'L') ? 'U' : 'L'; \
157 /* Set a, lda */ \
158  Map<const MatrixLhs, 0, OuterStride<> > lhs(_lhs,rows,depth,OuterStride<>(lhsStride)); \
159  MatrixLhs a_tmp; \
160 \
161  if ((conjA!=0) || (SetDiag==0)) { \
162  if (conjA) a_tmp = lhs.conjugate(); else a_tmp = lhs; \
163  if (IsZeroDiag) \
164  a_tmp.diagonal().setZero(); \
165  else if (IsUnitDiag) \
166  a_tmp.diagonal().setOnes();\
167  a = a_tmp.data(); \
168  lda = convert_index<BlasIndex>(a_tmp.outerStride()); \
169  } else { \
170  a = _lhs; \
171  lda = convert_index<BlasIndex>(lhsStride); \
172  } \
173  /*std::cout << "TRMM_L: A is square! Go to BLAS TRMM implementation! \n";*/ \
174 /* call ?trmm*/ \
175  BLASFUNC(&side, &uplo, &transa, &diag, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (BLASTYPE*)b, &ldb); \
176 \
177 /* Add op(a_triangular)*b into res*/ \
178  Map<MatrixX##EIGPREFIX, 0, OuterStride<> > res_tmp(res,rows,cols,OuterStride<>(resStride)); \
179  res_tmp=res_tmp+b_tmp; \
180  } \
181 };
182 
183 #ifdef EIGEN_USE_MKL
184 EIGEN_BLAS_TRMM_L(double, double, d, dtrmm)
185 EIGEN_BLAS_TRMM_L(dcomplex, MKL_Complex16, cd, ztrmm)
186 EIGEN_BLAS_TRMM_L(float, float, f, strmm)
187 EIGEN_BLAS_TRMM_L(scomplex, MKL_Complex8, cf, ctrmm)
188 #else
189 EIGEN_BLAS_TRMM_L(double, double, d, dtrmm_)
190 EIGEN_BLAS_TRMM_L(dcomplex, double, cd, ztrmm_)
191 EIGEN_BLAS_TRMM_L(float, float, f, strmm_)
192 EIGEN_BLAS_TRMM_L(scomplex, float, cf, ctrmm_)
193 #endif
194 
195 // implements col-major += alpha * op(general) * op(triangular)
196 #define EIGEN_BLAS_TRMM_R(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC) \
197 template <typename Index, int Mode, \
198  int LhsStorageOrder, bool ConjugateLhs, \
199  int RhsStorageOrder, bool ConjugateRhs> \
200 struct product_triangular_matrix_matrix_trmm<EIGTYPE,Index,Mode,false, \
201  LhsStorageOrder,ConjugateLhs,RhsStorageOrder,ConjugateRhs,ColMajor> \
202 { \
203  enum { \
204  IsLower = (Mode&Lower) == Lower, \
205  SetDiag = (Mode&(ZeroDiag|UnitDiag)) ? 0 : 1, \
206  IsUnitDiag = (Mode&UnitDiag) ? 1 : 0, \
207  IsZeroDiag = (Mode&ZeroDiag) ? 1 : 0, \
208  LowUp = IsLower ? Lower : Upper, \
209  conjA = ((RhsStorageOrder==ColMajor) && ConjugateRhs) ? 1 : 0 \
210  }; \
211 \
212  static void run( \
213  Index _rows, Index _cols, Index _depth, \
214  const EIGTYPE* _lhs, Index lhsStride, \
215  const EIGTYPE* _rhs, Index rhsStride, \
216  EIGTYPE* res, Index resStride, \
217  EIGTYPE alpha, level3_blocking<EIGTYPE,EIGTYPE>& blocking) \
218  { \
219  Index diagSize = (std::min)(_cols,_depth); \
220  Index rows = _rows; \
221  Index depth = IsLower ? _depth : diagSize; \
222  Index cols = IsLower ? diagSize : _cols; \
223 \
224  typedef Matrix<EIGTYPE, Dynamic, Dynamic, LhsStorageOrder> MatrixLhs; \
225  typedef Matrix<EIGTYPE, Dynamic, Dynamic, RhsStorageOrder> MatrixRhs; \
226 \
227 /* Non-square case - doesn't fit to BLAS ?TRMM. Fall to default triangular product or call BLAS ?GEMM*/ \
228  if (cols != depth) { \
229 \
230  int nthr = 1 /*mkl_domain_get_max_threads(EIGEN_BLAS_DOMAIN_BLAS)*/; \
231 \
232  if ((nthr==1) && (((std::max)(cols,depth)-diagSize)/(double)diagSize < 0.5)) { \
233  /* Most likely no benefit to call TRMM or GEMM from BLAS*/ \
234  product_triangular_matrix_matrix<EIGTYPE,Index,Mode,false, \
235  LhsStorageOrder,ConjugateLhs, RhsStorageOrder, ConjugateRhs, ColMajor, BuiltIn>::run( \
236  _rows, _cols, _depth, _lhs, lhsStride, _rhs, rhsStride, res, resStride, alpha, blocking); \
237  /*std::cout << "TRMM_R: A is not square! Go to Eigen TRMM implementation!\n";*/ \
238  } else { \
239  /* Make sense to call GEMM */ \
240  Map<const MatrixRhs, 0, OuterStride<> > rhsMap(_rhs,depth,cols, OuterStride<>(rhsStride)); \
241  MatrixRhs aa_tmp=rhsMap.template triangularView<Mode>(); \
242  BlasIndex aStride = convert_index<BlasIndex>(aa_tmp.outerStride()); \
243  gemm_blocking_space<ColMajor,EIGTYPE,EIGTYPE,Dynamic,Dynamic,Dynamic> gemm_blocking(_rows,_cols,_depth, 1, true); \
244  general_matrix_matrix_product<Index,EIGTYPE,LhsStorageOrder,ConjugateLhs,EIGTYPE,RhsStorageOrder,ConjugateRhs,ColMajor>::run( \
245  rows, cols, depth, _lhs, lhsStride, aa_tmp.data(), aStride, res, resStride, alpha, gemm_blocking, 0); \
246 \
247  /*std::cout << "TRMM_R: A is not square! Go to BLAS GEMM implementation! " << nthr<<" \n";*/ \
248  } \
249  return; \
250  } \
251  char side = 'R', transa, uplo, diag = 'N'; \
252  EIGTYPE *b; \
253  const EIGTYPE *a; \
254  BlasIndex m, n, lda, ldb; \
255 \
256 /* Set m, n */ \
257  m = convert_index<BlasIndex>(rows); \
258  n = convert_index<BlasIndex>(diagSize); \
259 \
260 /* Set trans */ \
261  transa = (RhsStorageOrder==RowMajor) ? ((ConjugateRhs) ? 'C' : 'T') : 'N'; \
262 \
263 /* Set b, ldb */ \
264  Map<const MatrixLhs, 0, OuterStride<> > lhs(_lhs,rows,depth,OuterStride<>(lhsStride)); \
265  MatrixX##EIGPREFIX b_tmp; \
266 \
267  if (ConjugateLhs) b_tmp = lhs.conjugate(); else b_tmp = lhs; \
268  b = b_tmp.data(); \
269  ldb = convert_index<BlasIndex>(b_tmp.outerStride()); \
270 \
271 /* Set uplo */ \
272  uplo = IsLower ? 'L' : 'U'; \
273  if (RhsStorageOrder==RowMajor) uplo = (uplo == 'L') ? 'U' : 'L'; \
274 /* Set a, lda */ \
275  Map<const MatrixRhs, 0, OuterStride<> > rhs(_rhs,depth,cols, OuterStride<>(rhsStride)); \
276  MatrixRhs a_tmp; \
277 \
278  if ((conjA!=0) || (SetDiag==0)) { \
279  if (conjA) a_tmp = rhs.conjugate(); else a_tmp = rhs; \
280  if (IsZeroDiag) \
281  a_tmp.diagonal().setZero(); \
282  else if (IsUnitDiag) \
283  a_tmp.diagonal().setOnes();\
284  a = a_tmp.data(); \
285  lda = convert_index<BlasIndex>(a_tmp.outerStride()); \
286  } else { \
287  a = _rhs; \
288  lda = convert_index<BlasIndex>(rhsStride); \
289  } \
290  /*std::cout << "TRMM_R: A is square! Go to BLAS TRMM implementation! \n";*/ \
291 /* call ?trmm*/ \
292  BLASFUNC(&side, &uplo, &transa, &diag, &m, &n, (const BLASTYPE*)&numext::real_ref(alpha), (const BLASTYPE*)a, &lda, (BLASTYPE*)b, &ldb); \
293 \
294 /* Add op(a_triangular)*b into res*/ \
295  Map<MatrixX##EIGPREFIX, 0, OuterStride<> > res_tmp(res,rows,cols,OuterStride<>(resStride)); \
296  res_tmp=res_tmp+b_tmp; \
297  } \
298 };
299 
300 #ifdef EIGEN_USE_MKL
301 EIGEN_BLAS_TRMM_R(double, double, d, dtrmm)
302 EIGEN_BLAS_TRMM_R(dcomplex, MKL_Complex16, cd, ztrmm)
303 EIGEN_BLAS_TRMM_R(float, float, f, strmm)
304 EIGEN_BLAS_TRMM_R(scomplex, MKL_Complex8, cf, ctrmm)
305 #else
306 EIGEN_BLAS_TRMM_R(double, double, d, dtrmm_)
307 EIGEN_BLAS_TRMM_R(dcomplex, double, cd, ztrmm_)
308 EIGEN_BLAS_TRMM_R(float, float, f, strmm_)
309 EIGEN_BLAS_TRMM_R(scomplex, float, cf, ctrmm_)
310 #endif
311 } // end namespace internal
312 
313 } // end namespace Eigen
314 
315 #endif // EIGEN_TRIANGULAR_MATRIX_MATRIX_BLAS_H
int BLASFUNC() strmm(char *, char *, char *, char *, int *, int *, float *, float *, int *, float *, int *)
SCALAR Scalar
Definition: bench_gemm.cpp:33
int BLASFUNC() ztrmm(char *, char *, char *, char *, int *, int *, double *, double *, int *, double *, int *)
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
#define EIGEN_BLAS_TRMM_R(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC)
std::complex< float > scomplex
Definition: MKL_support.h:119
std::complex< double > dcomplex
Definition: MKL_support.h:118
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
int BLASFUNC() dtrmm(char *, char *, char *, char *, int *, int *, double *, double *, int *, double *, int *)
#define EIGEN_BLAS_TRMM_L(EIGTYPE, BLASTYPE, EIGPREFIX, BLASFUNC)
int BLASFUNC() ctrmm(char *, char *, char *, char *, int *, int *, float *, float *, int *, float *, int *)
#define EIGEN_BLAS_TRMM_SPECIALIZE(Scalar, LhsIsTriangular)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:51:15