Most efficient way to loop through an Eigen matrix

后端 未结 4 1205
栀梦
栀梦 2021-02-02 01:37

I\'m creating some functions to do things like the \"separated sum\" of negative and positive number, kahan, pairwise and other stuff where it doesn\'t matter the order I take t

4条回答
  •  醉梦人生
    2021-02-02 02:06

    Eigen allocates matrices in column-major (Fortran) order by default (documentation).

    The fastest way to iterate over a matrix is in storage order, doing it the wrong way round will increase the number of cache misses (which if your matrix doesn't fit in L1 will dominate your computation time, so read increase your computation time) by a factor of cacheline/elemsize (probably 64/8=8).

    If your matrix fits into L1 cache this won't make a difference, but a good compiler should be able to vectorise the loop, which with AVX enabled (on a shiny new core i7) could give you a speedup of as much as 4 times. (256 bits / 64 bits).

    Finally don't expect any of Eigen's built-in functions to give you a speed-up (I don't think there are iterators anyhow, but I may be mistaken), they're just going to give you the same (very simple) code.

    TLDR: Swap your order of iteration, you need to vary the row index most quickly.

提交回复
热议问题