Using KML in the Eigen Library
Introduction to the Eigen Library
The Eigen library is a C++ template library for linear algebra operations. It provides high-performance matrix operations and linear algebra operations, and is widely used in scientific computing, machine learning, and computer graphics. For details, visit the Eigen official website. The Eigen library provides the method of enabling open source BLAS and LAPACK libraries. It can also use the BLAS and LAPACK libraries in the KML, which provides matrix computing interfaces with higher performance compared with the native Eigen library. In addition to standard BLAS and LAPACK interfaces, the KML provides additional BLAS interfaces for Eigen. For details, see KML_BLAS-like Extensions.
Installing the Eigen Library
- Download the latest Eigen library (Eigen-3.4.0).
- Decompress the package. Then the installation is successful.
Using the Eigen Library
Method 1: Because the Eigen library contains only header files, you only need to place the extracted folder in the project directory and reference the required header files.
Method 2: Go to the extracted folder and perform compilation.
- Compile and install Eigen.
mkdir build cd build cmake .. sudo make install
- Check whether the installation is successful by compiling the following sample code.
- Create a hello_eigen.cpp file.
vi hello_eigen.cpp
- Press i to enter the insert mode and add the following content to the file:
// hello_eigen.cpp #include <iostream> #include <eigen3/Eigen/Dense> using Eigen::MatrixXd; int main(){ MatrixXd m(2,2); m(0,0) = 3; m(1,0) = 2.5; m(0,1) = -1; m(1,1) = 1; std::cout << m << std::endl; } - Press Esc, type :wq!, and press Enter to save the file and exit.
- Perform compilation.
g++ hello_eigen.cpp -o hello_eigen
After the compilation is successful, the following information is displayed:3 -1 2.5 1
- Create a hello_eigen.cpp file.
Enabling KML_BLAS in the Eigen Library
- Obtain the eigen_kml.patch file.
- Save the obtained patch file to the extracted folder and apply the patch.
patch -p1 <eigen_kml.patch
- Run the compile command after the patch is successfully applied.
g++ -I./ your_app.cpp -O2 -o your_app -L/usr/local/kml/lib/kblas/nolocking/ -lkblas -lklapack_full -fopenmp -DKUNPENG_USE