近期开展方程组求解过程中,发现求解速度随着节点数量的增加快速降低,还没有确定出具体原因在哪,于是就先对使用的Eigen库进行了研究,Eigen是一个开源的矩阵计算库,使用C++语言,大量应用模板,同时有BLAS、MKL、LAPACKE等高性能库的接口。
测试源程序如下:
#include
#include “Eigen/Dense”
#include <time.h>
#include <stdio.h>
using namespace Eigen;
using namespace std;
int main(int argc,char *argv[])
{
//Eigen::initParallel();
int dim;
if(argc>1)
{
dim = atoi(argv[1]);
}
else{
dim = 10;
}
MatrixXd A = MatrixXd::Random(dim,dim);
MatrixXd B = MatrixXd::Random(dim,dim);
//cout << "A =" << endl << A << endl;
VectorXd b = VectorXd::Random(dim);
//cout << "b =" << endl << b << endl;
VectorXd x(dim);
time_t begin_t = clock();
x = A.colPivHouseholderQr().solve(b);
//cout << "The solution is:\n" << x << endl;
//A= A*B;
time_t finish_t = clock();
cout<<"it cost " << (double )(finish_t - begin_t )/CLOCKS_PER_SEC <<" s"<<endl;
return 0;
}
直接编译:
g++ test_eigen.cpp
运行[1000*1000]后
./a.out 1000
计算时间为19.0214 s
编译器优化编译后:
g++ -Ofast test_eigen.cpp
计算时间为0.628668 s
增加-mavx -mfma参数后
g++ -Ofast -mavx -mfma test_eigen.cpp
计算时间为0.554892 s,略有提升。
后期采用自动化测试工具进行测试,对不同的矩阵大小和优化参数的效果进行测试。
上述Ofast可能会有一些不安全的问题,具体进一步参考https://www.zhihu.com/question/27090458/answer/137944410