10 ms·
Rewriting Fortran Software in Rust
- historyremade 6y agoC++ IS KING. FUCK RUST RETARDS!! YOU MIGHT WANT TO REWRITE WINDOWS IN RUST!!
- saagarjha 6y agoIt’s nice to see an article that makes assumptions and then realizing that they didn’t really work out and walking back on them. It’s often good to try rewriting things, but often the thing you think is old and crusty (FORTRAN) is actually quite good, perhaps in ways you weren’t aware of. So it’s important to be able to say, “yes I tried some nice new thing, it didn’t really work out how I had expected, also some of my assumptions I started off with were not really correct either and I might not have made a fair comparison”.
- gautamcgoel 6y agoThis is a really cool post, but I wish the author had profiled his code. He makes several statements like "I think the performance issue is XYZ" but never bothers to check if his guess is correct. As Walter Bright liked to say, Use the profiler, Luke!
- melling 6y ago“ I’m guessing that FORTRAN is faster on small inputs due to the threading overheads, but faster on large inputs because of contention between threads for cache lines and memory bandwidth" I think the author meant FORTRAN was slower on small inputs?
- estebank 6y agoLook at the graph (or the chart on the left, I had to to understand it): the green and blue lines cross twice, first the FORTRAN version is faster, then the Rust one, then the FORTRAN version again.
- estebank 6y agoTL;DR: wanted to leverage GPUs for computation (but couldn't), the ergonomics of arrays in Rust for scientific computing without the use of ndarray are not good, had memory bandwidth issues to keep cache lines fed, performance of the two implementations is comparable One of the things I find interesting is how the Fortran version is faster both with small and huge inputs, while the Rust version is faster for input sizes in the middle.
- ghostwriter 6y ago> Having written parallel software in both C and Rust, the memory safety guarantees and easy parallelisation with Rayon offered by Rust contrasts quite sharply with my poor experience using OpenMP While I appreciate the strive for safety, and the effort being put into the rewrite, I believe in that particular case it would be more beneficial to rewrite it in ATS with safe formally-proved C rewrites/known to be safe inlined C calls.
- phkahler 6y agoIt seemed like this one area was a definite plus for the rewrite in Rust. I recently added some OpenMP goodness to some C++ code. Making the loops run parallel was almost trivial. The challenge was going over a couple thousand lines of code checking for safety. There were data structures being created and shared in various was but the code was written in a very functional style so it worked in the end. With Rust, all that analysis is done by the compiler. So for that code migrating to Rust is probably a good idea.
- zozbot234 6y agoATS is not readily usable for large programs. Among the "popular" programming languages Rust is perhaps most likely to get some support for interactive formal proof down the road, given that the safe subset of Rust basically enforces the rules of functional programming (and the unsafe subset is self-contained in a way that's not necessarily true elsewhere). Work along these lines is already happening as part of RustBelt.
- codezero 6y agoI used to maintain a large FORTRAN codebase - it seemed like every time new grad students showed up, everyone got a wise idea to re-build everything in a modern language. This question is revisited a lot, but the general consensus has been that FORTRAN is fast, simple, easy to understand and code, and the compilers used are super optimized (Intel's FORTRAN compiler was really a gem - it managed to do a ton of automatic parallelization on modern hardware) I'll see if I can find it, but I remember attending a conference talk at AGU in ~'08-10 called "Do-over or make-do" [1] that analyzed how many people-hours it would take to update all the climate modeling software (earth and space sciences are really huge on legacy FORTRAN code), and the conclusion of that talk was: use modern code/tools for glue, keep your climate models in FORTRAN. There's a ton relating to precision - we had a huge ordeal converting our 32-bit space weather model to 64-bit because the change in precision changed our results, so we couldn't publish (in good conscious) without making sure the results weren't within a good margin of error. Anyhow. This is always a fun thing to revisit now that I'm really far away from having to maintain any FORTRAN code any more :) edit found it! [1]: https://www.easterbrook.ca/steve/2010/12/agu-session-on-software-engineering-for-climate-modeling/ https://www.easterbrook.ca/steve/2010/12/agu-session-on-soft... [2]: slides http://www.cs.toronto.edu/~sme/presentations/Easterbrook-AGU-fall2010.pdf http://www.cs.toronto.edu/~sme/presentations/Easterbrook-AGU... Also randomly found this: http://www.moreisdifferent.com/2015/07/16/why-physicsts-still-use-fortran/ http://www.moreisdifferent.com/2015/07/16/why-physicsts-stil...
- classified 6y agoI came here to ask whether your typical Fortran program would carry the coding security risks that Rust is designed to mitigate; I couldn't think of any. Your comment seems to confirm my doubts.
- codezero 6y agoIt probably wouldn't have affected my work at all. We were running our code as a monolith - it was the only thing that ran on any given machine, and its output was stored on a large filesystem on NFS. That's not to say there isn't someone out there with this concern :)
- atrettel 6y agoThis is an interesting article. I have often wondered if there would be benefits to porting scientific simulation codes to more modern languages. In the end I've always reluctantly stayed in C or Fortran for scientific simulations. The article touched on the some of the issues (and the ever present issue of "If it isn't broke, don't fix it"). But I do think that the article ignored what I think is the biggest issue. To me the biggest issue is that some simulations just need to run on 1000s or hundreds of 1000s of cores, and the primary technology that lets this happen is MPI (Message Passing Interface). I've been writing new Fortran code only because I want the simulation to be able to scale to this kind of size. The issue is that MPI only has official bindings for C and Fortran, so the only real choice I have is to keep programing in C or Fortran if I want to use MPI. Any thoughts on this? Personally I would love to move towards more modern languages but without the proper libraries it is still a tough decision.
- zozbot234 6y agoRust can bind to C libraries, as can many other languages.
- atrettel 6y agoI have looked into this a bit, but the issue appears to be that MPI is not a library per se but a standard interface to more underlying function calls. Different implementations of MPI are in effect different libraries using the same API, and because of that it is difficult to write general bindings rather than just bindings to a particular version of an MPI library that is implemented on a particular supercomputer. In short, it's too site specific and fragile, unfortunately. If you change the system, the program might no longer compile. I appreciate the comment and feedback, though. This Stack Overflow page comments more on this issue: https://stackoverflow.com/questions/22949462/rust-on-grid-computing/24913447 https://stackoverflow.com/questions/22949462/rust-on-grid-co...
- TheRealKing 6y agoMPI officially supports only C and Fortran. Anything else for any other language can be only considered as adventures of some open-source developers, that may not last forever. Take a look at the fate of the C++ bindings for MPI.
- cs702 6y agoI find it remarkable that one person with just over a year of Rust experience can successfully rewrite ~6000 lines of well-tested, highly tuned Fortran in Rust and end up with similar performance on benchmarks without having in-depth expertise in the domain (simulation of fluid dynamics). The benchmarks (https://mckeogh.tech/post/shallow-water/84896735-54a97580-b09c-11ea-9e61-6b3fcd37a3cd.jpg https://mckeogh.tech/post/shallow-water/84896735-54a97580-b0...) show similar performance across all input sizes for both the old Fortran and the new Rust code, with Fortran outperforming in some cases and Rust outperforming in others.
- Someone 6y agoFTA: “The result of this is that these simple operations are being performed on some very large amounts of data which I believe is resulting in a memory bandwidth bottleneck. This hypothesis explains why SIMD did not improve performance and performance is not significantly improved by parallelisation” If that’s true, comparing system load or power usage could be a way to differentiate between the two implementations.
- tastyminerals 6y agoRust is not a very good example here considering the amount of time the op might have spent waiting for the compiler and steep learning curve of the language...
- faitswulff 6y agoActually closer to 4 years of experience: > As for my Rust experience, I’ve been using it for personal projects since ~2016 and I worked as a Rust software engineer at a startup in Berlin for a year after leaving high school. Publish date on this blog post: July 14th, 2020
- mindB 6y agoThe author says he's happy with the performance improvements he made, but--even at the size where the Rust version performs best--he gets much less than a 2x speedup. After a couple months of effort, that's not a whole lot to show. This reads like a case study in why it's probably a bad idea to rewrite even when you think you have compelling reason for it.
- pjmlp 6y agoAlso the author has not used state of the art compilers like XL, Intel and PGI.
- milancurcic 6y agoIndeed, and this depends between applications. For the WRF (Weather Research and Forecasting) model, I get 3-4x speed up with Intel Fortran compiler over gfortran. I don't see where the Author mentions the Fortran compiler used, though.
- wycy 6y ago> I get 3-4x speed up with Intel Fortran compiler over gfortran Wow, even at similar optimization levels? -O3 for each?
- milancurcic 6y agoNot quite, but at optimization levels set by default WRF configuration for each compiler (definitely not a fair comparison): gfortran: -O2 -ftree-vectorize -funroll-loops ifort: -O3 I don't have the timing results anymore. This was in 2018 on Xeon Platinum 8168. I recently tried replacing -O2 with -Ofast -ffast-math to gfortran settings, which gives about 18% speed up. So still far from Intel. I recently proposed it here [1]. [1] https://github.com/wrf-model/WRF/issues/1254 https://github.com/wrf-model/WRF/issues/1254
- RockIslandLine 6y ago
- TheRealKing 6y agowhy? Why would you do so? Did you gain any significant improvement in your final results compared to the original software? Why would rewrite a software that already works fine, other than not having the will to learn a bit of a new language? Why???
- aw1621107 6y agoAll your questions are addressed in the article. It does turn out that some of the author's beliefs were incorrect (e.g., the state of Fortran GPU compute libraries), though, so it's possible that they would have come to a different conclusion had they known that.
- pjmlp 6y agoAlready mentioned on the Reddit thread, the author apparently isn't aware of modern Fortran capabilities, IDE tooling support, NVidia and Intel offerings for GPGPU programming or HPC.
- efxhoy 6y agoNice post! I have written several versions of a scientific simulation program in python, first using MPI (single machine) and then native multiprocessing. Using numpy and Pandas for all the heavy data operations. I really valued the ergonomics of python and native multiprocessing and found the MPI code to be not very human friendly. However, I ran into some very strange bugs relating to MKL and multiprocessing that just hung the program on large input sizes with MKL_NUM_THREADS above 1. I also had to bounce the input data to the worker processes to files due to mp not being able to pickle data when it got too large. As soon as Apache arrow support in rust gets more feature complete I'll probably try to redo it there.
- danmg 6y agoPGI's Fortran compiler, bought by NVidia a few years ago, has excellent GPU extensions. You set an attribute on an array, move stuff on there, do your computation, move stuff back. It was the easiest GPU coding experience I've had.
- milancurcic 6y agoNice article. I'm curious which Fortran compiler did you use? One key difference that I see in the context of this article is that Fortran is natively parallel (both shared and distributed memory) via coarrays, teams, and events, whereas Rust (and Rayon), as far as I can tell, is shared-memory only. For applications that need to run on 100s or 1000s of cores, Rust needs to bind with C MPI. Is there any other approach to distributed-memory parallelism in Rust? Good points on the lack of certain features and ergonomic libraries. We're working hard on making this much better for Fortran [1] by developing a community-driven stdlib, package manager, and similar tools. In fact, we'll have our monthly community call tomorrow [2] where we'll discuss the best way forward for a nice strings API in Fortran stdlib. I welcome everybody interested to join the call. [1]: https://fortran-lang.org https://fortran-lang.org [2]: https://fortran-lang.discourse.group/t/fortran-monthly-call-july-2020/195 https://fortran-lang.discourse.group/t/fortran-monthly-call-...
- schultzer 6y agoThis is interesting, I did write BLAS in Rust https://github.com/schultzer/libblas https://github.com/schultzer/libblas it's only single threaded at the moment. I feel people forget to ask a more important question before they start a full rewrite and that is: Where is the hardware trends going and, is dedicated GPU, CUDA etc. even the future. I feel it would be wasteful to spend a lot of time writing GPU specific code, Its not really maintainable on these scientific project (where only a select few people contribute, usually people that do the research).
- morty_s 6y agoIn my experience... Reinventing (or rewriting) the wheel is a fun exercise. It’s even more fun to write it in rust (biased). Occasionally, the new wheel is better. Mostly, I learn how some of my assumptions were wrong and/or how I didn’t even account for “this, that, and the other.” That said, I enjoyed reading through and finding “Edit: I was wrong about X.” Learning experiences all around! Also, might have been mentioned but is this a typo? > I’m guessing that FORTRAN is faster on small inputs due to the threading overheads, but faster on large inputs Anyways, thanks! Enjoyed the article!
- adultSwim 6y agoNew engineer thinking, want to do everything themself