How to tell VS Code to use the fast math option?

Sorry for the somewhat epistemological question, but how did they know without a test suite?

2 Likes

We could just leave it, but it’s a hard sell to implement the @nofastmath feature just to support a global flag that we probably shouldn’t have in the first place. I do think that users deserve some warning when they use an option that could quite possible render their computations into total nonsense in very unexpected ways. So maybe @Tamas_Papp’s banner should be added.

2 Likes

I must insist on the cow’s eyes being red.

4 Likes

The option --math-mode=fast is “dangerous” but what about “-O3”? Is it also dangerous to use it?

No. (also since SLP is included in O2 I haven’t seen any benchmark where O3 is better than the default)

1 Like

How can I know what option is being used from within my Julia session?
What optimization option is used if I launch Julia from VS Code?

Base.JLOptions().opt_level

2 Likes

OK, thanks, VS Code says it’s using -O 2.

That’s the default.

Based on this thread and others, I have a feeling you may be fixating a bit too much on compiler flags. Julia already defaults to good performance—you should not need to mess around with optimization flags unless you are doing something pretty unusual. Frankly, the --math-mode=fast flag is a bad idea and you shouldn’t use it at all (sorry, @antoine-levitt, but that’s my honest opinion) and -O3 is basically the same as -O2 which is already the default. These options are not some magic wand to make your code faster—even if they do, it will only be by a very small margin. If your code is not fast enough, instead of messing with compiler flags, you should: profile it, look at allocation stats, make sure it’s type stable, make sure you don’t use non-constant globals, and put @inline and @simd annotations where appropriate. Or better still, use better algorithms—that’s where the most dramatic speed improvements come from. In all my time doing Julia consulting work helping people speed up their code, I have never once changed any compiler flags.

4 Likes

FWIW, I almost completely agree: --math-mode=fast is the last resort of the last resort, after thinking about the problem, the algorithm, the structure of the code, the code itself, and then the micro optimizations, all that with copious profiling. It’s just that sometimes, you’ve done all that (or decided you’ve done enough and further improvements would be too much of a bother), you play around with flags for 5 minutes (either of the safe variety like O3 or of the frankly dangerous variety with math-mode), you’ve made sure it doesn’t significantly change the result and it gives you an extra 10% with no modification to your code. Seems a shame to let that go.