Using BLAS/LAPACK from Eigen

Since Eigen version 3.3 and later, any F77 compatible BLAS or LAPACK libraries can be used as backends for dense matrix products and dense matrix decompositions. For instance, one can use IntelĀ® MKL, Apple's Accelerate framework on OSX, OpenBLAS, Netlib LAPACK, etc.

Do not miss this page for further discussions on the specific use of IntelĀ® MKL (also includes VML, PARDISO, etc.)

In order to use an external BLAS and/or LAPACK library, you must link you own application to the respective libraries and their dependencies. For LAPACK, you must also link to the standard Lapacke library, which is used as a convenient think layer between Eigen's C++ code and LAPACK F77 interface. Then you must activate their usage by defining one or multiple of the following macros (before including any Eigen's header):

Note
For Mac users, in order to use the lapack version shipped with the Accelerate framework, you also need the lapacke library. Using MacPorts, this is as easy as:
sudo port install lapack
and then use the following link flags: -framework Accelerate /opt/local/lib/lapack/liblapacke.dylib
EIGEN_USE_BLAS Enables the use of external BLAS level 2 and 3 routines (compatible with any F77 BLAS interface)
EIGEN_USE_LAPACKE Enables the use of external Lapack routines via the Lapacke C interface to Lapack (compatible with any F77 LAPACK interface)
EIGEN_USE_LAPACKE_STRICT Same as EIGEN_USE_LAPACKE but algorithms of lower numerical robustness are disabled.
This currently concerns only JacobiSVD which otherwise would be replaced by gesvd that is less robust than Jacobi rotations.

When doing so, a number of Eigen's algorithms are silently substituted with calls to BLAS or LAPACK routines. These substitutions apply only for Dynamic or large enough objects with one of the following four standard scalar types: float, double, complex<float>, and complex<double>. Operations on other scalar types or mixing reals and complexes will continue to use the built-in algorithms.

The breadth of Eigen functionality that can be substituted is listed in the table below.

Functional domainCode exampleBLAS/LAPACK routines
Matrix-matrix operations
EIGEN_USE_BLAS
m1*m2.transpose();
m1.selfadjointView<Lower>()*m2;
m1*m2.triangularView<Upper>();
m1.selfadjointView<Lower>().rankUpdate(m2,1.0);
Matrix-vector operations
EIGEN_USE_BLAS
m1.adjoint()*b;
m1.selfadjointView<Lower>()*b;
m1.triangularView<Upper>()*b;
LU decomposition
EIGEN_USE_LAPACKE
EIGEN_USE_LAPACKE_STRICT
v1 = m1.lu().solve(v2);
?getrf
Cholesky decomposition
EIGEN_USE_LAPACKE
EIGEN_USE_LAPACKE_STRICT
v1 = m2.selfadjointView<Upper>().llt().solve(v2);
?potrf
QR decomposition
EIGEN_USE_LAPACKE
EIGEN_USE_LAPACKE_STRICT
m1.householderQr();
m1.colPivHouseholderQr();
?geqrf
?geqp3
Singular value decomposition
EIGEN_USE_LAPACKE
JacobiSVD<MatrixXd> svd;
svd.compute(m1, ComputeThinV);
?gesvd
Eigen-value decompositions
EIGEN_USE_LAPACKE
EIGEN_USE_LAPACKE_STRICT
EigenSolver<MatrixXd> es(m1);
ComplexEigenSolver<MatrixXcd> ces(m1);
SelfAdjointEigenSolver<MatrixXd> saes(m1+m1.transpose());
GeneralizedSelfAdjointEigenSolver<MatrixXd>
gsaes(m1+m1.transpose(),m2+m2.transpose());
?gees
?gees
?syev/?heev
?syev/?heev,
?potrf
Schur decomposition
EIGEN_USE_LAPACKE
EIGEN_USE_LAPACKE_STRICT
RealSchur<MatrixXd> schurR(m1);
ComplexSchur<MatrixXcd> schurC(m1);
?gees

In the examples, m1 and m2 are dense matrices and v1 and v2 are dense vectors.

es
EigenSolver< MatrixXf > es
Definition: EigenSolver_compute.cpp:1
gemv
EIGEN_DONT_INLINE void gemv(const Mat &A, const Vec &B, Vec &C)
Definition: gemv.cpp:4
llt
EIGEN_DONT_INLINE void llt(const Mat &A, const Mat &B, Mat &C)
Definition: llt.cpp:5
ces
cout<< "Here is a random 4x4 matrix, A:"<< endl<< A<< endl<< endl;ComplexEigenSolver< MatrixXcf > ces
Definition: ComplexEigenSolver_compute.cpp:4
symm
int EIGEN_BLAS_FUNC() symm(const char *side, const char *uplo, const int *m, const int *n, const RealScalar *palpha, const RealScalar *pa, const int *lda, const RealScalar *pb, const int *ldb, const RealScalar *pbeta, RealScalar *pc, const int *ldc)
Definition: level3_impl.h:287
b
Scalar * b
Definition: benchVecAdd.cpp:17
Eigen::Upper
@ Upper
Definition: Constants.h:211
m1
Matrix3d m1
Definition: IOFormat.cpp:2
svd
cout<< "Here is the matrix m:"<< endl<< m<< endl;JacobiSVD< MatrixXf > svd(m, ComputeThinU|ComputeThinV)
symv
int EIGEN_BLAS_FUNC() symv(const char *uplo, const int *n, const RealScalar *palpha, const RealScalar *pa, const int *lda, const RealScalar *px, const int *incx, const RealScalar *pbeta, RealScalar *py, const int *incy)
Definition: level2_real_impl.h:13
hemv
int EIGEN_BLAS_FUNC() hemv(const char *uplo, const int *n, const RealScalar *palpha, const RealScalar *pa, const int *lda, const RealScalar *px, const int *incx, const RealScalar *pbeta, RealScalar *py, const int *incy)
Definition: level2_cplx_impl.h:19
m2
MatrixType m2(n_dims)
trmm
int EIGEN_BLAS_FUNC() trmm(const char *side, const char *uplo, const char *opa, const char *diag, const int *m, const int *n, const RealScalar *palpha, const RealScalar *pa, const int *lda, RealScalar *pb, const int *ldb)
Definition: level3_impl.h:183
gemm
EIGEN_DONT_INLINE void gemm(const A &a, const B &b, C &c)
Definition: bench_gemm.cpp:162
Eigen::Lower
@ Lower
Definition: Constants.h:209
ssyrk
int BLASFUNC() ssyrk(char *, char *, int *, int *, float *, float *, int *, float *, float *, int *)
Eigen::ComputeThinV
@ ComputeThinV
Definition: Constants.h:399
trmv
EIGEN_DONT_INLINE void trmv(const Mat &A, const Vec &B, Vec &C)
Definition: trmv_lo.cpp:4
v2
Vector v2
Definition: testSerializationBase.cpp:39
v1
Vector v1
Definition: testSerializationBase.cpp:38
dsyrk
int BLASFUNC() dsyrk(char *, char *, int *, int *, double *, double *, int *, double *, double *, int *)


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:09:56