首页 > 解决方案 > 特征中的向量加法和点不通过 mkl 加速

问题描述

这是我使用的代码:

#define EIGEN_USE_MKL_ALL

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

using namespace std;
using namespace Eigen;

int main(int argc, char *argv[])
{
    VectorXf a = VectorXf::Random(100000000); 
    VectorXf b = VectorXf::Random(100000000);

    double start = clock();
    VectorXf c = a+b;
    float d = a.dot(b);
    double endd = clock();
    double thisTime = (double)(endd - start) / CLOCKS_PER_SEC;

    cout << thisTime << endl;

    return 0;
}

使用 mkl 编译:

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

去掉第一行代码,不用mkl编译:

g++ mkl_test.cpp -Ieigen

时间几乎一样。

但是可以加速矩阵计算。将代码更改为下面我可以看到速度。

    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;

标签: c++eigeneigen3intel-mkleigenvector

解决方案


从启用 mkl的特征页面:

EIGEN_USE_BLAS 允许使用外部 BLAS 2 级和 3 级例程

Eigen 不会在这里使用外部例程,因为向量加法和点积是 1 级 blas 例程。


推荐阅读