Interesting. One question. How would I take advantage of this ? Are there special flags for the compiler or special memory allocator that I would need ? Do libraries like BLAS already account for this ?
How misaligning data can increase performance 12x by reducing cache misses
21–30 of 31 posts
Re: How misaligning data can increase performance 12x by reducing cache misses
#22Why would you want to make an in-memory data structure page-aligned instead of cache-line-aligned?
Either avoid walking the elements, or do whatever it takes to ensure that you don't end up getting stuck behind the N.
Hardware is inescapable.
It's also worth noting that you can use this to your advantage, ensuring that accesses to certain elements does not push much else out of the associative cache.
Re: How misaligning data can increase performance 12x by reducing cache misses
#23To avoid them in code, it's often sufficient to round to convenient decimal numbers when allocating arrays, instead of powers-of-2. That is, allocate an array of 1000, instead of an array of 1024.
Re: How misaligning data can increase performance 12x by reducing cache misses
#24Great example. I looked briefly at the source, and wasn't sure whether "pointer_chase" was on or off in your graphs. Or maybe it didn't make a difference?
Page-aligned accesses like these also make compulsory misses worse, because prefetchers won’t prefetch beyond a page boundary. But if you have enough data that you’re aligning things to page boundaries, you probably can’t do much about that anyway.
To the contrary, I think this is one of the relatively rare cases that explicit prefetching can help you. But maybe this helps only once your sets are too large for L3?
I wrote a little a few months ago on my attempts to speed up a Stream benchmark for Sandy Bridge that might have some overlap with your post: http://software.intel.com/en-us/forums/topic/480004#comment-...
Re: How misaligning data can increase performance 12x by reducing cache misses
#25As a not-systems c++ programmer, it seems to reinforce the usual lesson: don't optimize your structures initially for anything but readability, once you have your system running and can pinpoint the bottlenecks, then try things like aligning structures to various things. But naturally always have a real-world-like test-suite to verify you are improving things.
Sorry if this is boring.
Re: How misaligning data can increase performance 12x by reducing cache misses
#26Why would you want to make an in-memory data structure page-aligned instead of cache-line-aligned?
It's not that you would want to. It's that certain data structures (particularly in kernel land) naturally form around powers-of-2 sizing. Since the hashing in an n-way associative cache is done by masking away bits, you can get into this nasty situation where multiple elements of your data structure end up hashing to the same location set (the N in an N-way associative cache). You don't want that if you are going to…
Re: How misaligning data can increase performance 12x by reducing cache misses
#27Re: How misaligning data can increase performance 12x by reducing cache misses
#28The actual discussion is good, too.
Re: How misaligning data can increase performance 12x by reducing cache misses
#29Why would you want to make an in-memory data structure page-aligned instead of cache-line-aligned?
You can see an example of this technique here: https://github.com/scotts/streamflow/blob/master/streamflow....
This bit of code executes when a call to malloc() could not find any free memory, and it has to request more memory from the operating system. The function supermap() allocates a large chunk of memory from the kernel. We then write the meta-information to keep track of that chunk of memory directly to itself - that "meta-information" is the pageblock_t struct. In malloc(), we then use that chunk of memory to find a sub-chunk inside of it to return to the user.
Note that internal to malloc(), we will maintain lists of these pageblock_t structures, and they will all be page-aligned. So, anytime we want to search for free memory among the managed pages, we're going to access page-aligned structures.
Re: How misaligning data can increase performance 12x by reducing cache misses
#30Why would you want to make an in-memory data structure page-aligned instead of cache-line-aligned?