News for hackers. Stuff that matters.
Re:Rewriting Fortran Software in Rust by codezero on Wednesday July 15, 2020 @12:25PM (#23847527)
I 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-soft... [2]: slides http://www.cs.toronto.edu/~sme/presentations/Easterbrook-AGU... Also randomly found this: http://www.moreisdifferent.com/2015/07/16/why-physicsts-stil...

Re:Rewriting Fortran Software in Rust by nahuel0x on Wednesday July 15, 2020 @02:44PM (#23849751)
Julia is taking some inroads in climate modelling: "when the coder-climatologists of the Climate Modeling Alliance (CliMA) — a coalition of US-based scientists, engineers and mathematicians — set out to build a model from the ground up, they opted for a language that could handle their needs. They opted for Julia."

https://www.nature.com/articles/d41586-019-02310-3

https://clima.caltech.edu/

Re:Rewriting Fortran Software in Rust by cjhanks on Wednesday July 15, 2020 @07:12PM (#23853568)
It probably helps that Julia and Fortran are both column major.

When interfacing with C languages, that can be a big penalty (depending on the complexity of the algorithm).

It will probably take two generations of developers to make this happen, but well worth it. When I was studying atmospheric science I tried to understand OpenWRF (weather research forecasting) and the barrier was too high... And I had programming experience, imagine those with none!

An open source project is only as useful as it is accessible to it's average developer base.

Re:Rewriting Fortran Software in Rust by codezero on Wednesday July 15, 2020 @03:30PM (#23850441)
That's awesome, and not at all a surprise! It makes a lot of sense when building from the ground up to choose a great tool, and Julia has had some serious adoption in academia.
Re:Rewriting Fortran Software in Rust by Certhas on Wednesday July 15, 2020 @05:27PM (#23852120)
I think long term it has enormous potential to take over as _the_ language for modelling.

The reason is that for every hyper optimized climate model there are a thousand small or medium models. For these Julia offers sufficient performance and excellent developer experience.

Re:Rewriting Fortran Software in Rust by xioxox on Wednesday July 15, 2020 @01:55PM (#23848972)
Although Fortran compilers may produce fast code (they have to - there's no chance to play with SIMD intrinsics or templates unless you want to use C++), I don't find them very good. I've personally hit several bugs in gfortran. The code I maintain now has some extremely ugly hacks to work around unfixed compiler bugs. I recently tried compiling it with Intel Fortran and got a compiler crash on one input file. Maybe they work well with Fortran 77 level code, but the Fortran 2003 level support seemed rather buggy. I suppose this is the problem with a minority language.

I won't recommend Fortran unless the code has to be written by scientists who know nothing else. It has horrible string support, no data structure or algorithm library (like STL), no templates, few non-numeric libraries (e.g. http, json...) and (as above) buggy compilers. Furthermore, C++ gives you much more control over numerical speed when you need it.

Re:Rewriting Fortran Software in Rust by codezero on Wednesday July 15, 2020 @02:36PM (#23849619)
We could never use gfortran, and other open source compilers were not really great for us either. We used Intel's fortran compiler, and it stood out as a pure gem. At the time it was free for educational/research purposes, which is why we used it, but if we had to pay (I think the license was like $1k/yr) we would have done it in a heartbeat.

Our code was entirely FORTRAN 66 and 77, so that may explain why it worked so well for us.

Re:Rewriting Fortran Software in Rust by wycy on Wednesday July 15, 2020 @05:46PM (#23852431)
Ditto this, I've run into some strange compiler bugs I've had to work around. Intel Fortran 2019 actually fails to compile a code at work that's worked for ~40 years, Intel Fortran 2018 compiles it but produces numerical errors when the code runs. Intel Fortran 2016 actually works perfectly, as do several earlier versions.

With gfortran I run into issues primarily when using the "newer" features of Fortran 2003/2018.

Re:Rewriting Fortran Software in Rust by pantalaimon on Wednesday July 15, 2020 @05:29PM (#23852152)
Have you tried Flang? It’s LLVM's fortan compiler and was written from the ground up to support FORTRAN-2018
Re:Rewriting Fortran Software in Rust by codezero on Wednesday July 15, 2020 @05:33PM (#23852205)
Nope, I left the field in 2012 so haven’t kept up to date at all, sounds cool though!
Re:Rewriting Fortran Software in Rust by m463 on Wednesday July 15, 2020 @07:31PM (#23853766)
I wonder if when global warming starts eating coastal cities will the government have to seek out centenarian fortran programmers to analyze the climate models?

(like the new jersey unemployment system in cobol)

:)

Re:Rewriting Fortran Software in Rust by codezero on Wednesday July 15, 2020 @10:41PM (#23855152)
I'll do the same thing they did and ask for a shit ton more money :)

Though the beauty of fortran to me is really how easy it is to learn and make working code. That said - it's not elegant at all, probably a feature.

Re:Rewriting Fortran Software in Rust by TheRealKing on Wednesday July 15, 2020 @02:11PM (#23849238)
"It has horrible string support", this is untrue. Learn Fortran 2003, 2008, 2018.
Re:Rewriting Fortran Software in Rust by xioxox on Wednesday July 15, 2020 @02:18PM (#23849353)
What? Where is this good string support in Fortran 2003, 2008 and 2018? I must have missed it in the manual. Now you can allocate a string of a non fixed size - simply amazing technology! Actually, one of my compiler crashes was trying to use this revolutionary feature.
Re:Rewriting Fortran Software in Rust by pantalaimon on Wednesday July 15, 2020 @05:30PM (#23852175)
> Now you can allocate a string of a non fixed size - simply amazing technology!

That’s more than what C gives you out of the box ;)

Re:Rewriting Fortran Software in Rust by smabie on Wednesday July 15, 2020 @08:50PM (#23854437)
Many languages forbid mutable strings. They aren't really a very good idea, imo. Even less of a good idea are extensible strings. More complex data structures are required for efficient extensible strings, and this complexity shouldn't be hidden in a simple interface. Better to have a Buffer type or something that is distinct from the String type so programmers are made aware of the performance/allocation characteristics.

Though, this is kind of ridiculous to even talk about, since the CPU and compiler itself are responsible for the greatest sins of hiding something unimaginably complex behind a simple interface.

Ah, the joys of the 6502 and the Commodore 64!

Re:Rewriting Fortran Software in Rust by benibela on Friday July 17, 2020 @07:22AM (#23869386)
Delphi had mutable xor aliased strings. That is really great. Each function can change the string as fast as a mutable string, but once it shared with other functions, they can be certain that it will not be changed randomly
Re:Rewriting Fortran Software in Rust by m463 on Wednesday July 15, 2020 @07:32PM (#23853773)
Or just bite the bullet and learn how format statements really work. ;)
Re:Rewriting Fortran Software in Rust by _bxg1 on Wednesday July 15, 2020 @01:50PM (#23848902)
> 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

This is a really interesting dimension to the transition that I've never considered before. An actual change in correctness, not just compatibility. I would have thought for floats it would just add more decimal points

Re:Rewriting Fortran Software in Rust by giardini on Wednesday July 15, 2020 @04:54PM (#23851637)
One of our numerical analysis(NA) professors had worked at NASA, at IBM and had enormous real-world experience in NA. Several weeks into the class he challenged us to write a correct square root routine in FORTRAN. Over the next two weeks submissions were presented in class, where our professsor showed how each had multiple critically fatal errors. He let it stew awhile: two weeks before semester's end he presented and explicated to us a correct implementation of the square root, which turned out to be far more lengthy and involved than we had imagined. He said there was always the possibility of more bugs being present. That was really an eye-opening class.
Re:Rewriting Fortran Software in Rust by Animats on Wednesday July 15, 2020 @05:55PM (#23852586)
I saw that done years ago in assembler for UNIVAC mainframes. Tear apart the exponent and mantissa. Shift mantissa if exponent is odd. Shift exponent right one bit to get square root of exponent. Look up starting guess from table using a few high bits of mantissa. Run two iterations of Newton's method. Reassemble mantissa and exponent. Normalize. Done.
Re:Rewriting Fortran Software in Rust by saagarjha on Wednesday July 15, 2020 @09:23PM (#23854680)
Is it possible to keep ulp precision in all cases after doing all that separation, normalization, and recombination?
Re:Rewriting Fortran Software in Rust by blackrock on Wednesday July 15, 2020 @05:43PM (#23852383)
Post the code here, and educate us.
Re:Rewriting Fortran Software in Rust by zokier on Wednesday July 15, 2020 @06:19PM (#23852944)
musl sqrt is here, about hundred lines of code with nice explanation on top. Considering that it has 1993 copyright stamp, I'd imagine it is fairly canonical implementation

https://git.musl-libc.org/cgit/musl/tree/src/math/sqrt.c

Re:Rewriting Fortran Software in Rust by codezero on Wednesday July 15, 2020 @02:33PM (#23849578)
FWIW, I didn't bother getting too deep into an analysis (I was an undergrad and not really savvy enough to deep dive, but maybe I could now if I went back :) )

Our model was a space weather model of the solar wind speed and density - it was very iterative, and I think that iterative nature is what contributed to quirks in the transition - think of the issues when summing say, 0.1 ten times in Python (or other languages) - ultimately, we didn't need to do any real work to show that our converged model was within a margin of error and still presented the same results in terms of predicting solar flares - but it was still a lingering curiosity that the numbers weren't exactly the same (as you say!) - most of the work was done in finding the right compiler parameters to not break existing code, while taking advantage of the larger memory space.

Re:Rewriting Fortran Software in Rust by _bxg1 on Wednesday July 15, 2020 @02:48PM (#23849830)
Ah that makes sense
Re:Rewriting Fortran Software in Rust by classified on Wednesday July 15, 2020 @12:34PM (#23847642)
I 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.
Re:Rewriting Fortran Software in Rust by codezero on Wednesday July 15, 2020 @12:35PM (#23847669)
It 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 :)

Re:Rewriting Fortran Software in Rust by mumblemumble on Wednesday July 15, 2020 @12:57PM (#23848000)
https://www.fortran.io/

For example.

More seriously, I could imagine a security flaw in numpy wreaking all sorts of havok. But it's also somewhat difficult for me to imagine that there are any security vulnerabilities left in BLAS. But then I remember Heartbleed.

Re:Rewriting Fortran Software in Rust by veddox on Wednesday July 15, 2020 @05:51PM (#23852500)
I think „coding security risks“ referred to language properties that increase the likelihood of introducing bugs in a general sense, not in terms of cyber security/hacking.
Re:Rewriting Fortran Software in Rust by bsder on Wednesday July 15, 2020 @06:04PM (#23852726)
Memory errors also affect correctness.

Forgetting to initialize that last entry or initializing one too many entries by accident can cause random, very hard to track down, issues.

Security? Perhaps if your code is pulling chunks of data from a cloud source?

Re:Rewriting Fortran Software in Rust by classified on Friday July 17, 2020 @05:35AM (#23868844)
You are right, I forgot to mention correctness.
Re:Rewriting Fortran Software in Rust by lmm on Wednesday July 15, 2020 @09:12PM (#23854596)
> 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.

Had you done some kind of external validation of the original results? If not, surely the new results are just as valid to publish as the original ones - any bugs are as much or more likely to be in the 32-bit than the 64-bit version.

Re:Rewriting Fortran Software in Rust by codezero on Thursday July 16, 2020 @12:12AM (#23855695)
Yep. Our model was run by other organizations and incorporated into amalgamated models that had further downstream error bars that they needed.

Not that our model contributed much but we were one tiny part of the early warning system for solar flares for satellites and astronauts.

We admittedly weren’t super formal about the transition but wanted to make sure the differences wouldn’t be meaningful or propagate into other models we contributed to.

Also fwiw, we had no control or say over how others ran our model so they could have easily done their own 64 bit build of our code but I think it’s unlikely.

Re:Rewriting Fortran Software in Rust by MaxBarraclough on Wednesday July 15, 2020 @08:14PM (#23854142)
> 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.

That sounds like a question of numerical stability of the algorithm, rather than anything to do with the programming language, no?

Re:Rewriting Fortran Software in Rust by codezero on Thursday July 16, 2020 @12:14AM (#23855707)
Yep. Fair point. The folks I worked with were very pragmatic so didn’t get into the internals and just treated the code as the instructions. Many of them came from an earlier era where the code was a lot more specific and the concept of an R8 is not quite as specific on x86_84 as it was on a VAX because the modern CPU does a lot of tricks under your nose.
Re:Rewriting Fortran Software in Rust by Uhhrrr on Wednesday July 15, 2020 @03:59PM (#23850849)
"in good conscience"
Re:Rewriting Fortran Software in Rust by codezero on Wednesday July 15, 2020 @05:35PM (#23852240)
Nice catch, thanks!