Eigen  5.0.1
 
Loading...
Searching...
No Matches
Using Intel® MKL from Eigen

Since Eigen version 3.1 and later, users can benefit from built-in Intel® Math Kernel Library (MKL) optimizations with an installed copy of Intel MKL 10.3 (or later).

Intel MKL (now distributed as Intel® oneAPI Math Kernel Library, oneMKL) provides highly optimized multi-threaded mathematical routines for x86-compatible architectures. Current oneMKL releases are available on Linux and Windows; macOS support was removed in oneMKL 2024.0 and is only present in earlier releases.

Note
Intel® oneMKL is closed source, but it is distributed under the Intel Simplified Software License: there is no license to buy and no community-license registration to complete, and the terms grant royalty-free use and redistribution with no limit on the number of copies. See the oneMKL license FAQ for the terms that apply to your own product.

Using Intel MKL through Eigen is easy:

  1. define the EIGEN_USE_MKL_ALL macro before including any Eigen's header
  2. link your program to MKL libraries (see the MKL link line advisor)
  3. to use the ILP64 interface rather than LP64, define EIGEN_64BIT_BLAS alongside MKL_ILP64 (see The 64-bit integer (ILP64) interface); Eigen checks at compile time that the two agree

When doing so, a number of Eigen's algorithms are silently substituted with calls to Intel MKL 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.

In addition you can choose which parts will be substituted by defining one or multiple of the following macros:

EIGEN_USE_BLAS Enables the use of external BLAS level 2 and 3 routines
EIGEN_USE_LAPACKE Enables the use of external Lapack routines via the Lapacke C interface to Lapack
EIGEN_USE_LAPACKE_STRICT Same as EIGEN_USE_LAPACKE but algorithm of lower robustness are disabled.
This currently concerns only JacobiSVD which otherwise would be replaced by gesvd that is less robust than Jacobi rotations.
EIGEN_USE_MKL_VML Enables the use of Intel VML (vector operations)
EIGEN_USE_MKL_ALL Defines EIGEN_USE_BLAS, EIGEN_USE_LAPACKE, and EIGEN_USE_MKL_VML

The EIGEN_USE_BLAS and EIGEN_USE_LAPACKE* macros can be combined with EIGEN_USE_MKL to explicitly tell Eigen that the underlying BLAS/Lapack implementation is Intel MKL. The main effect is to enable MKL direct call feature (MKL_DIRECT_CALL). This may help to increase performance of some MKL BLAS (?GEMM, ?GEMV, ?TRSM, ?AXPY and ?DOT) and LAPACK (LU, Cholesky and QR) routines for very small matrices. MKL direct call can be disabled by defining EIGEN_MKL_NO_DIRECT_CALL.

Note that the BLAS and LAPACKE backends can be enabled for any F77 compatible BLAS and LAPACK libraries. See this page for the details.

Finally, the PARDISO sparse solver shipped with Intel MKL can be used through the PardisoLU, PardisoLLT and PardisoLDLT classes of the PardisoSupport module.

The following table summarizes the list of functions covered by EIGEN_USE_MKL_VML:

Code exampleMKL routines
v2=v1.array().sin();
v2=v1.array().asin();
v2=v1.array().cos();
v2=v1.array().acos();
v2=v1.array().tan();
v2=v1.array().exp();
v2=v1.array().log();
v2=v1.array().sqrt();
v2=v1.array().square();
v2=v1.array().pow(1.5);
v?Sin
v?Asin
v?Cos
v?Acos
v?Tan
v?Exp
v?Ln
v?Sqrt
v?Sqr
v?Powx

In the examples, v1 and v2 are dense vectors.

Links

  • Intel MKL can be downloaded from the Intel oneMKL page, either standalone or as part of the Intel oneAPI Base Toolkit.