为什么 GCC __builtin_prefetch 不能提高性能? [英] why does GCC __builtin_prefetch not improve performance?

查看:29
本文介绍了为什么 GCC __builtin_prefetch 不能提高性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来分析社交网络图.这意味着程序需要大量的随机内存访问.在我看来,预取应该有帮助.这是从顶点的邻居读取值的一小段代码.

for (size_t i = 0; i 

我将上面的代码转换为下面的代码并预取顶点的邻居的值.

int *neigh_vals = new int[num_vertices];for (size_t i = 0; i 

在这个 C++ 代码中,我没有覆盖任何运算符.

不幸的是,代码并没有真正提高性能.我想知道为什么.显然,在这种情况下,硬件预取不起作用,因为硬件无法预测内存位置.

不知道是不是GCC优化造成的.当我编译代码时,我启用了 -O3.我真的希望即使启用 -O3 预取也可以进一步提高性能.在这种情况下 -O3 优化是否融合了两个循环?这种情况下 -O3 可以默认开启预取吗?

我使用 gcc 版本 4.6.3,程序在 Intel Xeon E5-4620 上运行.

谢谢,哒

解决方案

是的,一些最新版本的 GCC(例如 2015 年 3 月的 4.9)在使用 -O3 优化时能够发出一些 PREFETCH 指令(即使没有任何显式的 __builtin_prefetch)

我们不知道get_neighbor在做什么,vneigh_val的类型是什么.

而且预取并不总是有利可图的.添加显式 __builtin_prefetch 可以减慢你的代码.你需要衡量.

正如 Retired Ninja 评论的那样,在一个循环中预取并希望数据会缓存在下一个循环中(进一步在您的源代码中)是错误的.

你或许可以试试

for (size_t i = 0; i 

你可以根据经验用任何合适的常量替换4.

但我猜上面的 __builtin_prefetch 没用(因为编译器可能能够自己添加它)并且它可能会损害(甚至使程序崩溃,当计算其参数时给出未定义的行为,例如,如果 v.get_neighbor(i+4) 未定义;但是预取地址空间之外的地址不会造成伤害 - 但可能会减慢您的程序速度).请进行基准测试.

请参阅相关问题的此答案.

注意在C++中所有的[]get_neighbor都可能被重载,变成非常复杂的操作,所以我们无法猜测!

在某些情况下,硬件会限制性能,无论您添加什么__builtin_prefetch(添加它们可能损害性能)

顺便说一句,您可以通过 -O3 -mtune=native -fdump-tree-ssa -S -fverbose-asm 来了解更多编译器在做什么(并查看生成的转储文件和汇编程序文件);此外,-O3 生成的代码确实比 -O2 给出的代码慢一些.

您可以考虑显式多线程OpenMPOpenCL 如果你有时间浪费在优化上.请记住,过早的优化是有害的.您是否进行了基准测试,是否对整个应用程序进行了概要分析?

I'm writing a program to analyze a graph of social network. It means the program needs a lot of random memory accesses. It seems to me prefetch should help. Here is a small piece of the code of reading values from neighbors of a vertex.

for (size_t i = 0; i < v.get_num_edges(); i++) {
    unsigned int id = v.neighbors[i];
    res += neigh_vals[id];
}

I transform the code above to the one as below and prefetch the values of the neighbors of a vertex.

int *neigh_vals = new int[num_vertices];

for (size_t i = 0; i < v.get_num_edges(); i += 128) {
    size_t this_end = std::min(v.get_num_edges(), i + 128);
    for (size_t j = i; j < this_end; j++) {
        unsigned int id = v.neighbors[j];
        __builtin_prefetch(&neigh_vals[id], 0, 2);
    }
    for (size_t j = i; j < this_end; j++) {
        unsigned int id = v.neighbors[j];
        res += neigh_vals[id];
    }
}

In this C++ code, I didn't override any operators.

Unfortunately, the code doesn't really improve the performance. I wonder why. Apparently, hardware prefetch doesn't work in this case because the hardware can't predict the memory location.

I wonder if it's caused by GCC optimization. When I compile the code, I enable -O3. I really hope prefetch can further improve performance even when -O3 is enabled. Does -O3 optimization fuse the two loops in this case? Can -O3 enable prefetch in this case by default?

I use gcc version 4.6.3 and the program runs on Intel Xeon E5-4620.

Thanks, Da

解决方案

Yes, some recent versions of GCC (e.g. 4.9 in march 2015) are able to issue some PREFETCH instruction when optimizing with -O3 (even without any explicit __builtin_prefetch)

We don't know what get_neighbor is doing, and what are the types of v and neigh_val.

And prefetching is not always profitable. Adding explicit __builtin_prefetch can slow down your code. You need to measure.

As Retired Ninja commented, prefetching in one loop and hoping data would be cached in the following loop (further down in your source code) is wrong.

You might perhaps try instead

for (size_t i = 0; i < v.get_num_edges(); i++) {
  fg::vertex_id_t id = v.get_neighbor(i);
  __builtin_prefetch (neigh_val[v.get_neighbor(i+4)]);
  res += neigh_vals[id];
}

You could empirically replace the 4 with whatever appropriate constant is the best.

But I guess that the __builtin_prefetch above is useless (since the compiler is probably able to add it by itself) and it could harm (or even crash the program, when computing its argument gives undefined behavior, e.g. if v.get_neighbor(i+4) is undefined; however prefetching an address outside of your address space won't harm -but could slow down your program). Please benchmark.

See this answer to a related question.

Notice that in C++ all of [], get_neighbor could be overloaded and becomes very complex operations, so we cannot guess!

And there are cases where the hardware is limiting performance, whatever __builtin_prefetch you add (and adding them could hurt performance)

BTW, you might pass -O3 -mtune=native -fdump-tree-ssa -S -fverbose-asm to understand more what the compiler is doing (and look inside generated dump files and assembler files); also, it does happen that -O3 produces slightly slower code than what -O2 gives.

You could consider explicit multithreading, OpenMP, OpenCL if you have time to waste on optimization. Remember that premature optimization is evil. Did you benchmark, did you profile your entire application?

这篇关于为什么 GCC __builtin_prefetch 不能提高性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆