Trying to Make a Loop Auto-Vectorize

(jsgroth.dev)

18 points | by zdw 4 days ago ago

5 comments

  • gnufx 3 minutes ago

    [delayed]

  • srean 19 minutes ago

    It has been a while, but I was quite surprised by how good gcc/g++ was at explaining why it had failed to vectorize a certain loop. At that time clang was being positioned as the better-than-gcc at optimization and error messages and it turned out that on my code it was the other way round -- hence the surprise.

    I had written a expressions template C++ helper library with sort of the same functionality as Python's itertool. This was for my own consumption. I expected very little from g++ and it had me impressed. Would be around 2008 - 2010

  • jvanderbot 44 minutes ago

    I do this. I do this a lot. My job involves processing a significant amount of weather data and flight telemetry _very quicky_.

    What I have found is that getting rust to auto-vectorize is a nightmare. The options available to me have always converged around: 1) use simd-like apis 2) frame it as matrix-vector operations.

    (1) is touched on in TFA

    (2) is way easier, and allows use of well-tested apis and crates, each of which (sensibly) call out to better-tested C libraries. Each of those can, should, might, or will use your CPU better than you will. If you can frame it as matrix-vector operations, you will go very fast, not least of which, by stacking the operations into a _big_ matrix/vector op, which your CPU will happily tear though.

    However the article proposes a third, very cool option: use algebraic ops API! Worth a read.

       Rust used to not have any reasonable way to do anything like this on stable (for my own definition of reasonable), but now it does! Rust 1.98 stabilized algebraic operators for floats. These allow you to declare per-operation that you’re okay with the compiler making optimizations that may change the result as long as the optimized code is algebraically equivalent to the original. Yes, this includes potentially reordering operations.
    • the__alchemist 38 minutes ago

      Great insight. My (much less sophisticated) mental model is: 1: I don't know how to get things to auto-vectorize nor evaluate if they did. [I should learn]; I assume they do not. Manual SIMD it is! [I have a lib for floats and vectors which mimics core::simd but on stable and x86 only)

      I will check out your insights regarding matrix-vector ops and C libs!

      • jvanderbot 34 minutes ago

        Very generally speaking, you want to view dissasembled sections.

        You can do this at the function level.

        In rust, try this: https://github.com/pacak/cargo-show-asm

        However, life is much easier now, because an agent knows how to steer through this with you. Have it teach you at first, then you know how to ask.

        This is one of those "back in my day we had to ... " stories, btw :D