简体   繁体   English

C ++ eigen3线性代数库,性能奇怪

[英]C++ eigen3 linear algebra library, odd performance results

I've been using eigen3 linear algebra library in c++ for a while, and I've always tried to take advantage of the vectorization performance benefits. 我已经在c ++中使用了eigen3线性代数库一段时间了,我一直试图利用矢量化性能优势。 Today, I've decided to test how much vectorization really speeds my programs up. 今天,我决定测试多少矢量化真正加速我的程序。 So, I've written the following test program: 所以,我写了以下测试程序:

--- eigentest.cpp --- --- 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;
}

Then I've run this program after compiling it with different compiler options: (The results aren't one-time, many runs give similar results) 然后我用不同的编译器选项编译后运行这个程序:(结果不是一次性的,很多运行给出类似的结果)

$ 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

And here's my relevant cpu information: 这是我的相关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

I know that there's no vectorization going on when I don't use the compiler option -march=native because when I don't use it, I never get a segmentation fault, or wrong result due to vectorization, as opposed to the case that I use it (with -NDEBUG ). 我知道当我不使用编译器选项-march=native时没有向量化,因为当我不使用它时,我从未得到分段错误,或者由于向量化而导致错误的结果,而不是我用它(带-NDEBUG )。

These results lead me to believe that, at least on my CPU vectorization with eigen3 results in slower execution. 这些结果让我相信,至少在我的CPU上使用eigen3进行矢量化会导致执行速度变慢。 Who should I blame? 我应该责怪谁? My CPU, eigen3 or gcc? 我的CPU,eigen3还是gcc?

Edit: To remove any doubts, I've now tried to add the -DEIGEN_DONT_ALIGN compiler option in cases where I'm trying to measure the performance of the no-vectorization case, and the results are the same. 编辑:为了消除任何疑虑,我现在尝试添加-DEIGEN_DONT_ALIGN编译器选项,以便我尝试测量无向量化情况的性能,结果是相同的。 Furthermore, when I add -DEIGEN_DONT_ALIGN along with -march=native the results become very close to the case without -march=native . 此外,当我将-DEIGEN_DONT_ALIGN-march=native一起添加时,结果变得非常接近没有-march=native的情况。

It seems that the compiler is smarter than you think and still optimizes a lot of stuff away. 似乎编译器比你想象的更聪明,并且仍然优化了许多东西。

On my platform, I get about 9ms without -march=native and about 39ms with -march=native . 在我的平台上,我得到大约9毫秒没有-march=native和大约39ms with -march=native However, if I replace the line above the return by 但是,如果我将返回上方的行替换为

std::cout<<accumulator<<"\n";

then the timings change to 78ms without -march=native and about 39ms with -march=native . 然后时间变为78毫秒,没有-march=native ,大约39ms, -march=native

Thus, it seems that without vectorization, the compiler realizes that you only use the (0,0) element of the matrix and so it only computes that element. 因此,似乎没有矢量化,编译器意识到您只使用矩阵的(0,0)元素,因此它只计算该元素。 However, it can't do that optimization if vectorization is enabled. 但是,如果启用了矢量化,则无法执行该优化。

If you output the whole matrix, thus forcing the compiler to compute all the entries, then vectorization speeds up the program with a factor 2, as expected (though I'm surprised to see that it is exactly a factor 2 in my timings). 如果你输出整个矩阵,从而迫使编译器计算所有条目,那么矢量化就像预期的那样以2倍加速程序(虽然我很惊讶地看到它在我的时间中恰好是2)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM