安装mkl
https://software.intel.com/en-us/get-started-with-mkl-for-linux

把eigen放到代码文件平行目录

使用mkl编译:
mkl_test.cpp

#define EIGEN_USE_MKL_ALL

#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <time.h>

using namespace std;
using namespace Eigen;

// 使用Eigen+Intel MKL
int main(int argc, char *argv[])
{
    MatrixXd a = MatrixXd::Random(1000, 1000);  // 随机初始化矩阵
    MatrixXd b = MatrixXd::Random(1000, 1000);

    double start = clock();
    MatrixXd c = a * b;    // 乘法好简洁
    double endd = clock();
    double thisTime = (double)(endd - start) / CLOCKS_PER_SEC;

    cout << thisTime << endl;

    //system("PAUSE");
    return 0;
}

编译指令:
g++ mkl_test.cpp /home/tong.guo/intel/mkl/lib/intel64/libmkl_rt.so -Ieigen -Wl,–no-as-needed -lpthread -lm -ldl -m64 -I/home/tong.guo/intel/mkl/include

然后运行./a.out
打印 0.23

对比不编译mkl:
mkl_test2.cpp

#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <time.h>

using namespace std;
using namespace Eigen;

int main(int argc, char *argv[])
{
    MatrixXd a = MatrixXd::Random(1000, 1000);  // 随机初始化矩阵
    MatrixXd b = MatrixXd::Random(1000, 1000);

    double start = clock();
    MatrixXd c = a * b;    // 乘法好简洁
    double endd = clock();
    double thisTime = (double)(endd - start) / CLOCKS_PER_SEC;

    cout << thisTime << endl;

    //system("PAUSE");
    return 0;
}

编译指令:g++ mkl_test2.cpp -o b.out -Ieigen
运行./b.out
打印11.94

Logo

更多推荐