8

Intel helpfully provides a prefetch pragma; for example

#pragma prefetch a
for(i=0; i<m; i++)
  a[i]=b[i]+1;

will prefetch a a certain number of loop cycles ahead, as determined by the compiler.

But what if a is not an array but a class with [] overridden? If operator[] does a simple array access, can prefetch still be used in this way?

(Presumably the question applies to std::vectors as well).

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
Sideshow Bob
  • 4,566
  • 5
  • 42
  • 79

1 Answers1

2

One way to find out is to try it and look at the assembly. And if anything else, just benchmark it with and without the pragma. However, I'm not sure if the prefetch pragma is what you want:

The prefetch pragma is supported by Intel® Itanium® processors only.

http://software.intel.com/sites/products/documentation/studio/composer/en-us/2011/compiler_c/cref_cls/common/cppref_pragma_prefetch_noprefetch.htm

Are you really writing this for an Itanium?

On x86/x64 systems, simple loops like that with sequential memory access are already well handled by the hardware prefetcher. So it may not help at all to do manual prefetching.

See here for a prefetching example: Prefetching Examples?

Community
  • 1
  • 1
Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • I'm compiling for both x86 and itanium - and assuming anyone worried about performance will be using the latter (on a grid). Thanks for pointing out what the hardware prefetcher does though. I hadn't realised. – Sideshow Bob Sep 14 '11 at 13:31
  • In 2018, This pragma applies only to Intel® MIC Architecture and Intel® Advanced Vector Extensions 512 (Intel® AVX-512). – Olsonist Feb 07 '18 at 23:21
  • And prefetching works on x86 in general but not with the prefetch pragma. Instead, use the -qopt-prefetch flags. – Olsonist Feb 08 '18 at 01:26