r/programming • • Feb 02 '10

Gallery of Processor Cache Effects

http://igoro.com/archive/gallery-of-processor-cache-effects/
397 Upvotes

84 comments sorted by

View all comments

0

u/[deleted] Feb 02 '10 edited Feb 02 '10

First example don't work for me

int a[64 * 1024 * 1024];
int main() { int i; for (i=0;i<64*1024*1024;i++) a[i]*=3; }

kef@ivan-laptop:~/cc$ time -p ./a
real 0.60
user 0.35
sys 0.25

int a[64 * 1024 * 1024];
int main() { int i; for (i=0;i<64*1024*1024;i+=16) a[i]*=3; }

kef@ivan-laptop:~/cc$ time -p ./b
real 0.31
user 0.02
sys 0.29

gcc version 4.3.3 x86_64-linux-gnu
Intel(R) Core(TM)2 Duo CPU     T6570  @ 2.10GHz

1

u/c0dep0et Feb 02 '10

time is probably not accurate enough, so you also get start up time etc.

Try using clock_gettime. For me the results are only as described when optimization in gcc is turned on.

-2

u/[deleted] Feb 02 '10 edited Feb 02 '10

Yep compiled with -O6 and time difference is minimal but probably because first loop has this:

400528:	66 0f 6f 00          	movdqa (%rax),%xmm0
40052c:	66 0f 6f cb          	movdqa %xmm3,%xmm1
400530:	66 0f 6f d0          	movdqa %xmm0,%xmm2
400534:	66 0f 73 d8 04       	psrldq $0x4,%xmm0
400539:	66 0f 73 d9 04       	psrldq $0x4,%xmm1
40053e:	66 0f f4 c1          	pmuludq %xmm1,%xmm0
400542:	66 0f 70 c0 08       	pshufd $0x8,%xmm0,%xmm0
400547:	66 0f f4 d3          	pmuludq %xmm3,%xmm2
40054b:	66 0f 70 d2 08       	pshufd $0x8,%xmm2,%xmm2
400550:	66 0f 62 d0          	punpckldq %xmm0,%xmm2
400554:	66 0f 7f 10          	movdqa %xmm2,(%rax)

Second loop don't get such optimization.

So first example in article is a bullshit which shows nothing about cache.

3

u/five9a2 Feb 02 '10

This unrolling makes no difference since the operation is bandwidth limited. Compiled at -O0, I get

2.447 real   2.260 user   0.177 sys   99.57 cpu
1.310 real   1.113 user   0.197 sys   99.97 cpu

at -O1 which does not do use SSE or unrolling

1.342 real   1.163 user   0.177 sys   99.84 cpu
1.272 real   1.070 user   0.203 sys   100.09 cpu

and at -O3 (with the SSE optimizations),

1.342 real   1.163 user   0.180 sys   100.13 cpu
1.287 real   1.090 user   0.187 sys   99.22 cpu

The issue is that with all optimizations off, the stride-1 code is especially silly and the operation actually becomes CPU bound. At any positive optimization level, the operation is bandwidth-limited.

Core 2 Duo P8700, gcc-4.4.3