6 ms·
A few years ago, I rewrote an `R` script into Fortran, for better than order-of-magnitude speed increase. The script optimized the placement of samplers in a b
by dmlorenzetti 9y ago
A few years ago, I rewrote an `R` script into Fortran, for better than order-of-magnitude speed increase.
The script optimized the placement of samplers in a building, in order to maximize the probability of detecting airborne pollutants, or to minimize the expected time required to detect. The rewrite cut the runtime down from 2-3 days to sub-hour.
Some of the speedup was intrinsic to the interpreted/compiled divide. However most of the speedup came from the greater control Fortran gave over how data got mapped in memory. This made it easier for the code to be explicit about memory re-use, which was a big help when we were iterating over millions of networks.
Re-using memory was helpful in two ways, I think. First, it avoided wanton creation and destruction of objects. Second, and more importantly, it allowed bootstrapping the work already invested in evaluating network `N` when it came time to evaluate a nearly-identical network `N+1`. Of course, I could have made the same algorithms work in R, but languages like C or Fortran, which put you more in the driver's seat, make it a little easier to think through the machine-level consequences of coding decisions.
That experience actually taught me something interesting about user expectations. When the Fortran version was done, my users were so accustomed to waiting a few days to get their results, that they didn't run their old problems faster. Instead, they greatly expanded the size of the problems they were willing to tackle (the size of the building, the number of uncertain parameters, and the number of samplers to place).
- fhood 9y agoThis is the first time that I have ever encountered someone who used Fortran for something other than earth sciences related modeling. Thank you, now I have a second example the nest time someone claims "nobody used fortran anymore".
- azag0 9y agoI’ll say it again. If the natural language of your problem domain is linear algebra and/or matrices (tensors), Fortran is the perfect tool.
- kutkloon7 9y agoWhy is it better than C(++) for this?
- gjjrfcbugxbhf 9y agoBecause for example Fortran has multi dimensional arrays as first class entities. C does not. Yes you can get the same speed in C - yes you can do the same things in C but you can do them with simpler code easier to reason about code in Fortran.
- atemerev 9y agoModern Fortran is high-level enough to pass for MATLAB, has full vectorized operations support, but in many cases faster than C. What's not to like?
- shoo 9y agoHistorically i believe Fortran language had stronger assumptions that variables arent aliases for the same memory. This means that the compiler can be more aggressive (than e.g. C) in terms of the generated code as it doesnt have to behave correctly if e.g. memory for unrelated arrays overlap. See https://en.wikipedia.org/wiki/Pointer_aliasing https://en.wikipedia.org/wiki/Pointer_aliasing I am not sure if this performance advantage is still the case as the wikipedia page on pointer aliasing notes that C99 added a "restrict" keyword so that the C programmer can tell the C compiler where similar strong assumptions can hold.
- gravypod 9y agoBecause scientists already know fortran and they're too lazy to learn something else even if there would be significantly improved results.
- atemerev 9y agoLike what?
- 9y ago
- Elrac 9y agoSorry about the "me too" but I'm another example of someone who programs non-science in FORTRAN. I work on a crew management system for a major airline. Most of this and related applications engage in data shuffling rather than number crunching, but FORTRAN used to be one way of creating code that ran at nearly assembly language speed and size in (historically) memory constrained environments. That said, the application (and I) are nearing retirement.
- shoo 9y agoi haven't been doing any numerical computing for the last 5 or so years, but previously I was using python's numpy + scipy pretty heavily for data-science / applied-maths stuff, and under the hood quite a lot of the numerical optimisation algorithms in scipy are wrappers around old fortran libraries. so at that stage it still gets used but perhaps most people can pretend it doesnt exist. similarly, the backend of the original "random forest" machine learning algorithm for R was written in fortran, and then wrapped as an R library, but as an R user you could largely pretend that fortran didnt exist. R was a pitifully slow language so this was a good move.
- deleted 9y ago[deleted]
- cbcoutinho 9y agoFortran is an excellent language. I enjoy the fact that people think it's too old and/or difficult to learn, because neither of those are true. It's a great fit especially when the problem domain is heavily numerical in nature.
- na85 9y agoMy big problem with Fortran is finding a modern tutorial written for an audience that doesn't already know some previous version of Fortran.
- cbcoutinho 9y agoThat's a fair criticism. One of the disadvantages of Fortran is that it caters to a very non-sexy segment of the programming population, by that I mean that UX/UI is not of great concern - a lot of learning materials I come across reflect that. Unless you find a textbook, you'll probably end up looking through undergraduate/graduate engineering lectures, or some presentation from a national lab (see below). First question, do you have access to a compiler? On all Linux distros I'm aware of, you can get the gnu fortran compiler fairly easily (some variation of gcc-fortran, gfortran, etc.). On Windows you can either boot up a VM, or use something like the MinGW toolchain (https://sourceforge.net/projects/mingw-w64/ https://sourceforge.net/projects/mingw-w64/), which is a port of gcc compilers to windows. I have no experience with Mac and/or BSD. Some learning materials I just came across on Google, which don't seem to cater to people with prior Fortran experience: http://www.hpc.lsu.edu/training/weekly-materials/2014-Spring/Fortran-Spring-2014.pdf http://www.hpc.lsu.edu/training/weekly-materials/2014-Spring... https://www.tacc.utexas.edu/documents/13601/162125/fortran_class.pdf https://www.tacc.utexas.edu/documents/13601/162125/fortran_c... http://www.dsf.unica.it/~fiore/f03t.pdf http://www.dsf.unica.it/~fiore/f03t.pdf Some more advanced topics: http://people.ds.cam.ac.uk/nmm1/fortran/paper_10.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_10.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_11.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_11.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_12.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_12.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_13.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_13.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_14.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_14.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_15.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_15.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_16.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_16.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_17.pdf http://people.ds.cam.ac.uk/nmm1/fortran/paper_17.pdf