Cache friendlier way to access 2D array?
Members - Reputation: 1000
Posted 17 February 2013 - 02:29 PM
A normal array is not very cache friendly if iterating through the different "layers" of the array (not the direction of the array elements in memory)
The access is done using x*XSize + y
Does there exist an access "equation" which has the elements spatially close to each other in 2d space likely to also be close to each other in memory, regardless of axis?
Id imagibe it would work like a quadtree without the upper levels (built in depth first order)
Assuming we use quadtree like indexing, would it work for non-POT grid sizes?
Members - Reputation: 1397
Posted 17 February 2013 - 02:37 PM
This is commonly done in graphics hardware for textures, where it's called tiling or swizzling. You could apply the same process to arrays.
http://fgiesen.wordpress.com/2011/01/17/texture-tiling-and-swizzling/ explains the process well.
Moderators - Reputation: 7545
Posted 17 February 2013 - 08:02 PM
The tiling pattern assumes you are operating as a kernel scanning across an image. That is very often true for certain post-processing effects.
Your goal is to have roughly linear data access along the array. If your algorithms are linear across the array than you don't need to do anything special. If your algorithms require scanning several lines at once, such as the 4 lines mentioned in the article, then it would make more sense to have them tiled as it describes.
If your access pattern is something else entirely, you will want a layout that mimics whatever access pattern you are using.
Crossbones+ - Reputation: 1906
Posted 18 February 2013 - 02:17 AM
Everything stated so far is of course correct but misses an obvious item. Frob makes the proper suggestion which is that access pattern is key and all the swizzling in the world won't make a difference if the access is out of expectations. More importantly, a "bug" in the given equation needs to be brought up: "x*XSize+y". That's likely a typo. Should be: "x+y*XSize". Ok, why is that important? As Frob pointed out, your access pattern is critical and the initial, incorrect?, equation would need to have a memory layout biased in vertical bars which could actually be mostly solved by storing the image rotated 90 degree's and exchanging x/y without any fancy swizzles.
Sorry folks, I just figured it's proper to ask if the standard strided image format formula was misstyped before suggesting complicated solutions.
Assuming the equation was a typo, I disagree with the original hypothesis that "iteration" is inefficient. Again, as Frob mentions, this depends on access patterns. Simple iterations over the pixels, you will likely slow things down using other packing methods. Performing bi-linear operations are also likely to slow down with alternative layouts using AMD/Intel CPU's due to the number of lines of associativity they maintain in the cache. I.e. they maintain all three lines of data in separate cache lines without purging and reloading them constantly and each cache line is likely to contain at least say 4 pixels (likely more unless you are on a REALLY dated CPU) so the predictor is busy loading the next section from all three memory areas while the CPU is actively processing the loaded data.
Getting back on track. As Frob points out, it's all about what you want to do with the data. If you wanted to perform a gaussian blur on the image you might think swizzling is the way to go. You'd be wrong in that case. Execute the gaussian blur only in left to right so you get linear memory access on multiple lines, rotate the original image 90 degree's in memory, apply the blur again, unrotate and then average the images. Mathematically identical results, baring rounding errors. But MUCH faster than accessing memory beyond cache line association limits. (NOTE: Think that's the correct fast method, been a while.. And there are more tricks to reduce memory access top to bottom to keep it cache efficient just about no matter what filter size you want.)
So, accessing arrays/memory is all about how you need to access it and what you intend to do with it. I use the gaussian blur because it is a big "window" of access, but also you can break the math into several stages and that's what allows you to optimize your memory access. Truly random access, forget it, linear is best.
Members - Reputation: 1000
Posted 18 February 2013 - 05:07 AM
Basically i was wondefing if there is another array indexing equation which doesnt favor either axis.
I was thinking an access pattern where you might pick a cell and then need to access the cells near it, in any direction. Not something like image processing.
I guess simple tiling into chunks of some.size is the simplest way and also effective...
Crossbones+ - Reputation: 1328
Posted 18 February 2013 - 11:50 AM
Are you thinking of something like a Hilbert curve?
You actually could use a Hilbert curve for a layout of a 2D array and it would have the property that (x,y)'s that are near each other in the Euclidean sense would tend to be near each other in the array; there would be exceptions but generally this would be the case.
Would only work with power of two sized arrays however. Also not sure about the time efficiency of going from index -> (x,y) and (x,y)->index; might be log(n) over the size of the array but not sure.
Edited by jwezorek, 19 February 2013 - 01:14 PM.