To exploit cache locality when looping over the elements of a multidimensional array in Julia (or in Matlab for that matter), you should generally let the inner loop go over the first dimension, the loop around that go over the second dimension, and so on, with the outermost loop going over the last dimension. This will access the elements in the same order that they are stored in memory. For efficiency, it's most important to follow this for the innermost dimensions; swapping the order of the fourth and fifth dimension will probably not make much of a difference.
However, this straightforward prescription is based on the assumption that you are only accessing one element at a time. I guess that your actual access pattern is more complex? Maybe you access not only the current position, but at the same time also some neighboring positions where one or several of the indices have changed by a small amount? Such access patterns have a profound influence on which iteration order is efficient, and more importantly, how the computation can be restructured. If you want to exploit cache locality, you often end up having to restructure your data. Maybe your code can be more efficient with some of the dimensions permuted, or maybe some of them should be mixed through some kind of tiling? Without knowing more about your problem, I cannot tell. It could also be that cache locality isn't even an issue. On Tuesday, 7 October 2014 13:34:15 UTC+2, Nils Gudat wrote: > > I'm currently trying to optimize a piece of code I've written (at present > it takes around 85 hours to finish, so there's some way to go...) > and have hence checked out the Fast Numeric Computation > <http://julialang.org/blog/2013/09/fast-numeric/> post on the Julia blog. > One issue that was new to me as a former Matlab > user was the importance of the order of calculations (row vs. column > order) when working with large matrices. > > I was wondering how this applies to higher dimensional arrays. In my > problem, I am working on an Array{Float64,5}, the dimensions > of which are around [12, 11, 10, 1000, 40]. Due to the nature of my > problem, I have to work through this backwards on the last dimension, > from 40 to 1, but for each of the Array{Float64,4}'s [12, 11, 10, 1200, i] > I could work through the elements in any order. > > Would it be preferable to loop over the "first" dimension first, or the > largest dimension first, or do something else entirely? > > >
