2
我使用Eigen 3的Cholesky模块来求解线性方程组。 Eigen文档指出,使用LDLT
而不是LLT
可能会更快,但我的基准测试结果显示出不同的结果。Eigen LDLT比LLT慢吗?
我使用下面的代码为标杆:
#include <iostream>
#include <chrono>
#include <Eigen/Core>
#include <Eigen/Cholesky>
using namespace std;
using namespace std::chrono;
using namespace Eigen;
int main()
{
MatrixXf cov = MatrixXf::Random(4200, 4200);
cov = (cov + cov.transpose()) + 1000 * MatrixXf::Identity(4200, 4200);
VectorXf b = VectorXf::Random(4200), r1, r2;
r1 = b;
LLT<MatrixXf> llt;
auto start = high_resolution_clock::now();
llt.compute(cov);
if (llt.info() != Success)
{
cout << "Error on LLT!" << endl;
return 1;
}
auto middle = high_resolution_clock::now();
llt.solveInPlace(r1);
auto stop = high_resolution_clock::now();
cout << "LLT decomposition & solving in " << duration_cast<milliseconds>(middle - start).count()
<< " + " << duration_cast<milliseconds>(stop - middle).count() << " ms." << endl;
r2 = b;
LDLT<MatrixXf> ldlt;
start = high_resolution_clock::now();
ldlt.compute(cov);
if (ldlt.info() != Success)
{
cout << "Error on LDLT!" << endl;
return 1;
}
middle = high_resolution_clock::now();
ldlt.solveInPlace(r2);
stop = high_resolution_clock::now();
cout << "LDLT decomposition & solving in " << duration_cast<milliseconds>(stop - start).count()
<< " + " << duration_cast<milliseconds>(stop - middle).count() << " ms." << endl;
cout << "Total result difference: " << (r2 - r1).cwiseAbs().sum() << endl;
return 0;
}
我和g++ -std=c++11 -O2 -o llt.exe llt.cc
编译它在Windows上,这就是我得到:
LLT decomposition & solving in 6515 + 15 ms.
LDLT decomposition & solving in 8562 + 15 ms.
Total result difference: 1.27354e-006
那么,为什么是活体肝移植比LLT慢?我做错了什么或者我误解了文档?
你需要一个正定矩阵cholesky。所以我认为错误是'cov =(cov + cov.transpose())'加法应该是一个乘法。 – typ1232
@ typ1232:用'cov =(cov * cov.transpose())+ 1000 * MatrixXf :: Identity(4200,4200);'试过了,但是这并没有改变测量的性能。 – Callidior