2

I want to have an array with 32 elements of 64bit numbers like this : long int arr[32];

however my cache line size is 64 bytes. Does it mean my array will not go at all into the cache system or rather some of elements will do?

Would it help to fit cache if I split my array into two-dimensional like this : long int arr[4][8]; ?

Tunga
  • 93
  • 4
  • 1
    It doesn't matter how you split it up, you can't fit 256 bytes into 64 bytes. – Barmar Jun 01 '16 at 21:12
  • 1
    All that matters is that you access the array sequentially, that will minimize cache misses. – Barmar Jun 01 '16 at 21:14
  • maybe I should have 4 arrays arr1[8], arr2[8], arr3[8], arr4[8] where all fit perfectly into cache line? I want to avoid any access to memory but access the arrays may not be sequentially. – Tunga Jun 01 '16 at 21:28
  • You will have to access memory if you go from `arr1` to `arr2`, because they can't both be in the cache line at the same time. – Barmar Jun 01 '16 at 21:30
  • The array already has 4 blocks that fit into cache lines (assuming it is 64-aligned), giving the blocks different names makes no difference. – harold Jun 01 '16 at 21:30
  • Also, there's no guarantee that any variable will be aligned at a cache line boundary. – Barmar Jun 01 '16 at 21:30
  • but my cache L2 has 256kb and L3 has 6Mb so there should be a space to store those arrays. – Tunga Jun 01 '16 at 21:33
  • In a normal program, you have no control of how memory are cached, so attempt to fit data into cache is useless. 2-D array has the same performance as 1-D array as they are both contiguous memory blocks. Splitting a large array to multiple swap-size arrays at best gives the same performance as the 1 large array, at worst gives worse performance since you might not have contiguous data. – Harvey Pham Jun 01 '16 at 22:31

1 Answers1

1
  1. Your array is 256 bytes, so it will not fit in one 64 byte cache line.
  2. Splitting your array won't decrease its size, so #1 still applies.
  3. Your CPU has multiple cache lines, so it's very likely that 256 bytes will fit in whatever cache you are worried about.
MSN
  • 53,214
  • 7
  • 75
  • 105
  • Would add "4. The only thing you can do that's actually useful is to ensure the start of the array is aligned on a cache line boundary; so that it takes 4 full cache lines, and doesn't take (e.g.) half a cache line then 3 full cache lines then another half a cache line." – Brendan Jun 02 '16 at 01:35
  • Brendan - how can i do that (to ensure)? – Tunga Jun 02 '16 at 08:25