为什么 GCC __builtin_prefetch 不能提高性能?

本文介绍了为什么 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 不能提高性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1127

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1032

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:775

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:866

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:903

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:848

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:834

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:809

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1619

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1262