开发者

How do I prefetch a pointer in C, targeting AMD Opteron 6168?

开发者 https://www.devze.com 2023-01-20 08:41 出处:网络
I am writing a multi-threaded program in C where one core periodically grabs an item from the head of a linked list while other cores append items to the back of the list (using CAS magic for thread s

I am writing a multi-threaded program in C where one core periodically grabs an item from the head of a linked list while other cores append items to the back of the list (using CAS magic for thread safety, someone else provided that for me). It appears that my program will run faster if the core taking an item from the head of the list simply initiates a prefetch for the next item, which is sure to be in another core's cache.

Currently I am targeting an AMD Opteron 6168, compiling with gcc on Debian Linux: I've tried to find documentation for this but I am in unfamiliar waters. All I ca开发者_JAVA技巧n find is using -O3 to enable compiler-inserted prefetching (I think for loops) and some mentions of the AMD prefetch instruction names like PREFETCHW.

I do not know how to find the reference for what I'm after, or how to insert a statement like that into C, maybe as a block of assembly?


gcc comes with some builtin functions for that. You can do

__builtin_prefetch(&yourData);


Check the intel architecture docs.

In VC, you should be able to do something like this

asm
{
  prefetch POINTER_NAME
}

In GCC -

asm("prefetch %0", POINTER_NAME); //May have syntax slightly off

I've looked this over before.

0

精彩评论

暂无评论...
验证码 换一张
取 消