【发布时间】:2012-04-09 23:24:53
【问题描述】:
我在 C++ 中使用 eigen3 线性代数库已经有一段时间了,我一直在尝试利用向量化的性能优势。今天,我决定测试一下矢量化在多大程度上真正加快了我的程序。所以,我编写了以下测试程序:
--- eigentest.cpp ---
#include <eigen3/Eigen/Dense>
using namespace Eigen;
#include <iostream>
int main() {
Matrix4d accumulator=Matrix4d::Zero();
Matrix4d randMat = Matrix4d::Random();
Matrix4d constMat = Matrix4d::Constant(2);
for(int i=0; i<1000000; i++) {
randMat+=constMat;
accumulator+=randMat*randMat;
}
std::cout<<accumulator(0,0)<<"\n"; // To avoid optimizing everything away
return 0;
}
然后我在使用不同的编译器选项编译后运行了这个程序:(结果不是一次性的,多次运行给出相似的结果)
$ g++ eigentest.cpp -o eigentest -DNDEBUG -std=c++0x -march=native
$ time ./eigentest
5.33334e+18
real 0m4.409s
user 0m4.404s
sys 0m0.000s
$ g++ eigentest.cpp -o eigentest -DNDEBUG -std=c++0x
$ time ./eigentest
5.33334e+18
real 0m4.085s
user 0m4.040s
sys 0m0.000s
$ g++ eigentest.cpp -o eigentest -DNDEBUG -std=c++0x -march=native -O3
$ time ./eigentest
5.33334e+18
real 0m0.147s
user 0m0.136s
sys 0m0.000s
$ g++ eigentest.cpp -o eigentest -DNDEBUG -std=c++0x -O3
$time ./eigentest
5.33334e+18
real 0m0.025s
user 0m0.024s
sys 0m0.000s
这是我的相关cpu信息:
model name : AMD Athlon(tm) 64 X2 Dual Core Processor 5600+
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm 3dnowext 3dnow extd_apicid pni cx16 lahf_lm cmp_legacy svm extapic cr8_legacy 3dn
我知道当我不使用编译器选项-march=native 时不会进行向量化,因为当我不使用它时,我永远不会因为向量化而出现分段错误或错误结果,而不是我使用它的情况(-NDEBUG)。
这些结果让我相信,至少在我使用 eigen3 进行 CPU 向量化时会导致执行速度变慢。我该怪谁?我的 CPU,eigen3 还是 gcc?
编辑:为了消除任何疑虑,我现在尝试添加-DEIGEN_DONT_ALIGN 编译器选项,以防我尝试测量无向量化情况的性能,结果是相同的。此外,当我添加-DEIGEN_DONT_ALIGN 和-march=native 时,结果变得非常接近没有-march=native 的情况。
【问题讨论】:
-
你用的是哪个g++版本?
-
在我的平台(英特尔 Q9550)上,无论我是否使用 March=native,我都能获得相同的速度。但是,-O3 会导致激进的内联、使用 SSE 和展开。添加 -mss3 会导致组装略有不同,但运行时性能完全相同。我真的不明白你对段错误的看法,你有一组编译器标志会导致你的程序崩溃吗?
-
@KillianDS 我的 gcc 版本:
gcc --version:gcc (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 -
@Bob 我已经尝试通过在
Vector2d成员之前定义一个带有char成员的类来故意设置一个矢量化问题,没有EIGEN_MAKE_ALIGNED_OPERATOR_NEW然后创建一个带有 new 并编译的对象-NDEBUG绕过断言。如果没有-march=native,我会从计算中得到正确的结果,并且有了它,我会遇到分段错误。这告诉我,没有 -march 就不会使用矢量化。
标签: c++ performance gcc vectorization eigen