hannorein / rebound

💫 An open-source multi-purpose N-body code.
https://rebound.readthedocs.io/
GNU General Public License v3.0
824 stars 218 forks source link

Consider safe-to-use compiler math optimizations #643

Closed alchzh closed 10 months ago

alchzh commented 1 year ago

I was modifying rebound this summer and played around with compiler flags to speed up our long running simulations.

-ffast-math is too aggressive and breaks some simulations completely, but I think some other flags are totally safe to put in Makefile.defs. In particular, rebound shouldn't be affected by -fno-trapping-math (there's no code that catches signals) and -fno-math-errno (errno is never checked), so I think they can be enabled for small but "free" performance gains of a few percent.

There are also some flags that seem to not break rebound but could change results slightly (e.g. -ffp-contrast=fast)

I can do some benchmarking after this AAS is over-- my overheating prone laptop can't record consistent times.

(I see that there's a note explicitly disabling -march=native)

hannorein commented 1 year ago

Thanks for looking into this! I think you are probably right.

If it's just a few percent, say less than 10%, I'm not sure if it's worth the trouble. In any case, the important thing is that the simulations remain bit-wise identical with these optimizations. So no floating-point operations should be rearranged.

hannorein commented 10 months ago

Some compilers have started to turn on fp-contract by default (clang). That breaks reproducibility and some unit tests. I've added a new flag to setup.py to turn off fp-contract manually at least while running the unit tests.

I think it should be save to turn on most optimization flags, but I'll close this issue for now. If you want to look into it more, feel free to reopen it once you have done some benchmarking.