我们不知道get_neighbor在做什么,v和neigh_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 给出的代码慢一些.您可以考虑显式多线程、OpenMP、OpenCL 如果你有时间浪费在优化上.请记住,过早的优化是有害的.您是否进行了基准测试,是否对整个应用程序进行了概要分析?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 insteadfor (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 不能提高性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!