6 ms·
Understanding Fast-Math
- deleted 5y ago[deleted]
- bee_rider 5y agoNitpicky, but saying > It turns out that, like with almost anything else relating to IEEE floating-point math, it’s a rabbit hole full of surprising behaviors. immediately before describing that they disabled IEEE floating-point math is a bit funny. The standard isn't surprising, floating point numbers (arguably) are. The whole point of standardizing floating point path was to reduce this surprisingness. Can't complain about IEEE floating point numbers if you tell the compiler not to use them.
- sampo 5y agoIt takes some time to learn how floating point and numerical calculations work. Not too long, but more than one evening. If you take a numerical analysis course in a university, the first 2 or 3 lectures might be about floating point, error propagation and error analysis. Or the first chapter of a numerical analysis textbook. But almost nobody spends this much effort to familiarize themselves with the floating point system. So it keeps surprising people.
- josefx 5y agoAt least it should be a bit less surprising now that every CPU has SSE instructions where a 32 bit float is actually stored in a 32 bit vector register instead of a 80 bit FPU register that may be dropped to a 32 bit memory location whenever your compiler feels like it. Makes the results a lot more consistent.
- optimalsolver 5y ago"-fno-math-errno" and "-fno-signed-zeros" can be turned on without any problems. I got a four times speedup on <cmath> functions with no loss in accuracy. See also "Why Standard C++ Math Functions Are Slow": https://medium.com/@ryan.burn/why-standard-c-math-functions-are-slow-d10d02554e33 https://medium.com/@ryan.burn/why-standard-c-math-functions-...
- iamcreasy 5y agoSorry, if it's a basic question - but does recompiling C/C++ code(with/without flags) produce more efficient code most of the time? For example - let say I am using a binary that was compiled on a processor that didn't have support for SIMD. Assuming the program is capable of taking advantage of SIMD instructions, and also assuming my processor support SIMD - would it make sense to recompile the C/C++ code on my system again hoping the newer binary would run faster?
- bee_rider 5y agogcc has the ability to target different architectures (look up the -march and -mtune flags for example). Linux distributions are typically set to be compatible with a pretty wide range of devices, so they often don't take advantage of recent instructions. Compiling a big program can be a bit of a pain, though, so it is probably only worthwhile if you have a program that you use very frequently. Also compilers aren't magic, the bottleneck in the program you want to run could be various things: CPU stuff, memory bandwidth, weird memory access patterns, disk access, network access, etc. The compiler mostly just helps with the first one. Also, note that some libraries, like Intel's MKL, are able to check what processor you are using and just dispatch the appropriate code (your mileage may vary, they sometimes don't keep up with changes in AMD processors, causing great annoyance).
- optimalsolver 5y agoThe more CPU-bound your program, the more benefit you'll see from the optimization flags. If your program is constantly waiting around for input on a network channel, then it may not help as much. My currently used optimization flags are: -O3 -fno-math-errno -fno-signed-zeros -march=native -flto Only use -march=native if the program is only intended to run on your own machine. It carries out architecture-specific optimizations that make the program non-portable. Also look into profile-guided optimization, where you compile and run your program, automatically generate a statistical report, then recompile using that generated information. It can result in some dramatic speedups. https://ddmler.github.io/compiler/2018/06/29/profile-guided-optimization.html https://ddmler.github.io/compiler/2018/06/29/profile-guided-...
- bla3 5y agoRecent post on the same topic that gets the same information across with fewer words: https://kristerw.github.io/2021/10/19/fast-math/ https://kristerw.github.io/2021/10/19/fast-math/
- kristofferc 5y agohttps://simonbyrne.github.io/notes/fastmath/ https://simonbyrne.github.io/notes/fastmath/ also has a nice discussion about the possible pitfalls of "fast"-math.
- Symmetry 5y agoI sort of assume the main benefit of assuming no NaNs is being able to replace `a == a` with `true`? The handling of isnan is certainly a big question. I can see wanting to respect that, but I can also see littering your code with assertions that isnan is false and compiling with normal optimizations and then hoping that later recompiling with fast-math and all its attendant "fun, safe optimizations" will let you avoid any performance penalty for all those asserts.
- jcranmer 5y agoThere's also several cases where handling NaNs "correctly" may require a fair amount of extra work (C99's behavior for complex multiplication is far more complicated when NaNs are involved).
- assbuttbuttass 5y ago> To my surprise, there were no measurable differences outside of the standard deviation we already see when repeating tests multiple times. This should be the main takeaway. Don't enable ffast-math if floating point calculations aren't a bottleneck, and especially not if you don't understand all the other optimizations it enables.
- nickelpro 5y agoCounter argument: Always enable fast-math unless your application has a demonstrable need for deterministically consistent floating point math. If you're just trying to get a "good enough" calculation (which is the vast majority of floating point work, physics and rendering calculations for 3D graphics), there's no reason to leave the performance on the floor.
- josefx 5y ago> Always enable fast-math unless your application has a demonstrable need for deterministically consistent floating point math. As far as I remember fast math also breaks things like NaN and infinity, which makes filtering out invalid values before they hit something important fun since they will obviously still exist and mess up your results but you can no longer check for them.
- nickelpro 5y agoNo tight rendering loop building a transform matrix to ship off to the a graphics card has an isnan() in it to begin with. If your code cares about isnan() or infinities, that would be an example of a demonstrable need to not use fast-math
- deleted 5y ago[deleted]
- usefulcat 5y agoIf the compiler is going to assume that all FP values and calculations are finite, then you really need to be able to check for any non-finite input values, which you can’t do with -ffast-math because it silently breaks isnan and isinf. WTF. Also, there are other uses of FP besides games..
- gumby 5y agoI am shocked by the plethora of posts by people surprised when -ffast-math breaks their code (no insult to the pspdfkit folks intended). If it were a harmless flag you'd think it would be enabled by default. That's a clue that you should look before you leap. We have a few files in our code base that compile with -ffast-math but the rest don't. Those files were written with that flag in mind.
- asveikau 5y agoPeople sometimes have unreasonable faith in compilers or libraries. It's a common adage to look at bugs in application code before suspecting these lower layers. That is not the same thing as the lower layers never producing unexpected results. So when people get evidence that a compiler feature may be rough around the edges, they resist it.
- gumby 5y agoFrom Cygnus it felt like the opposite: more than half the reports were from people blaming the compiler when it was their own code that was at fault. But that perspective aside, I agree: generally people correctly assume that their tools are OK and their code is at fault. I wouldn't consider -ffast-math a bug: it's a sharp tool that should only be used by experienced users. If there's a bug at all it's that, in retrospect, the flag should have had a different name.
- Sharlin 5y agoNo, it’s easy to assume (even after reading the documentation) that -ffast-math just makes some calculations less precise or otherwise not strictly IEEE 754 compliant. That would be plenty reason enough not to enable it by default, but make it available for those who desire maximum performance at the expense of precision (or accuracy), as may be a perfectly reasonable tradeoff in cases like computer graphics. It’s very understandable that people are surprised by the fact that -ffast-math can actually break code in unintuitive ways.
- usefulcat 5y ago
- toolslive 5y agoOnce spent 2 days trying to find out why a python prototype yielded different results on a different platform (in casu Solaris). Eventually discovered that an unnamed culprit implemented the cunning plan to compile the interpreter with `-ffast-math`. Fun times
- dahart 5y agoI’ve long wondered if changing the name to something like -ffast_inaccurate_math might help stem the tide of surprises and mistakes. Having only “fast” in the name makes it sounds like a good thing, rather than a tradeoff to consider.