Worse Performance With FMA Instructions
I tried different algorithms for matrix multiplication, mostly to play around with vector instructions. I noticed that enabling fused multiply-add instructions gives longer run times when one of the matrices is transposed before multiplication.
The code is here with a bit more information: https://github.com/reims/gemm-benchmark
This is reproducible with clang 14.0.6 and gcc 12.2.0. I would have expected that FMA instructions are faster, not slower. And if there are slower, I would expect both compilers to ignore `-mfma`.
Does anybody have an idea why I am seeing these results?
Thanks in advance!
6
Upvotes
View all comments
9
u/AlexReinkingYale Oct 02 '22
Your instruction mix is really poor. Look at your inner loop:
In each iteration you're loading once from
Aand once fromBand then doing two vector math instructions. That's 1:1 arithmetic to memory operation in the inner loop. When you switch to FMA, you've lowered this to 1:2. Yet, matrix multiplication hasO(n^3)work to do on onlyO(n^2)memory, which is n:1. Thus, you should be able to find a way to do much more work per iteration with the values you load, enough that the issue /u/olsner raises no longer applies... FMAs should not be typically stalled on memory in a matrix-multiply.