Re:Rust and C++ on Floating-Point Intensive Code
by lovasoa on Friday October 25, 2019 @05:33AM
(#21353265)
I liked your trick to iterate on chunks to force the compiler to vectorize the code !
Now that the code is properly vectorized, you can add the `mul_add` function, and this time you'll see a significant speedup. I tried it on my machine and it made the code 20% faster.
See the generated assembler here: https://rust.godbolt.org/z/G5A2u0
Re:Rust and C++ on Floating-Point Intensive Code
by silentvoice on Friday October 25, 2019 @12:01PM
(#21356285)
Thanks! The chunks trick was a fairly straightforward translation of what I would do in C++ if the compiler wouldn't vectorize the reduction for some reason. These days most compilers will do it if you pass enough flags, a fact I really took for granted when doing this because Rust is more conservative.
I've tried using mul_add, but at the moment performance isn't much better. But I also noticed someone else on my machine running a big parallel build, so I'll wait a little later and run the full sweep over the problem sizes with mul_add.
So really the existence of FMA didn't have a performance implication it seems except to confirm that Rust wasn't passing "fast math" to LLVM where Clang was. It just so happens that "fast math" will also allow vectorization of reductions.
Re:Rust and C++ on Floating-Point Intensive Code
by tom_mellior on Thursday October 24, 2019 @04:26PM
(#21348796)
Great to hear that you managed another 2x speedup! Sure, feel free to link my comment if you like.
If it's OK I'll link to this comment as the inspiration.
On the iterators versus loop: for some reason when I use the raw loop _nothing_ vectorizes, not even the obvious loop. What I read online was that bounds checking happens inside the loop body because Rust doesn't know where those indices are coming from. Using iterators instead is supposed to fix this, and it did seem to in my experiments.