22

I'm trying to optimize some C++ (RK4) by using

__builtin_prefetch

I can't figure out how to prefetch a whole structure.

I don't understand how much of the const void *addr is read. I want to have the next values of from and to loaded.

for (int i = from; i < to; i++)
{
    double kv = myLinks[i].kv;
    particle* from = con[i].Pfrom;
    particle* to = con[i].Pto;
    //Prefetch values at con[i++].Pfrom & con[i].Pto;
    double pos = to->px- from->px;
    double delta = from->r + to->r - pos;
    double k1 = axcel(kv, delta, from->mass) * dt; //axcel is an inlined function
    double k2 = axcel(kv, delta + 0.5 * k1, from->mass) * dt;
    double k3 = axcel(kv, delta + 0.5 * k2, from->mass) * dt;
    double k4 = axcel(kv, delta + k3, from->mass) * dt;
    #define likely(x)       __builtin_expect((x),1)
    if (likely(!from->bc))
    {
            from->x += (( k1 + 2 * k2 + 2 * k3 + k4) / 6);
    }
}

Link: http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/

Mikhail
  • 7,749
  • 11
  • 62
  • 136

2 Answers2

30

I think it just emit one FETCH machine instruction, which basically fetches a line cache, whose size is processor specific.

And you could use __builtin_prefetch (con[i+3].Pfrom) for instance. By my (small) experience, in such a loop, it is better to prefetch several elements in advance.

Don't use __builtin_prefetch too often (i.e. don't put a lot of them inside a loop). Measure the performance gain if you need them, and use GCC optimization (at least -O2). If you are very lucky, manual __builtin_prefetch could increase the performance of your loop by 10 or 20% (but it could also hurt it).

If such a loop is crucial to you, you might consider running it on GPUs with OpenCL or CUDA (but that requires recoding some routines in OpenCL or CUDA language, and tuning them to your particular hardware).

Use also a recent GCC compiler (the latest release is 4.6.2) because it is making a lot of progress on these areas.


(added in january 2018:)

Both hardware (processors) and compilers have made a lot of progress regarding caches, so it seems that using __builtin_prefetch is less useful today (in 2018). Be sure to benchmarck.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 7
    Yes: if you are going to do this make sure you benchmark your loop before and after. There is a chance you might make it worse or no difference. – 111111 Dec 10 '11 at 23:05
  • 6
    Thanks for your post. I did 3 benchmarks: no optimization = 100%, reading ahead [i+3] 100% and with [i+10] 200%+. All were done with -03 and fast math – Mikhail Dec 11 '11 at 04:56
18

It reads a cache line. Cache line size may vary, but it is most likely to be 64 bytes on modern CPUs. If you need to read multiple cache lines, check out prefetch_range.