61

I've read that Fortran is still heavily used for scientific computing. For code already heavily invested in Fortran this makes sense to me.

But is there a reason to use Fortran over other modern languages for a new project? Are there language design decisions in Fortran that makes it much more suitable for scientific computing compared to say the more popular languages (C++, Java, Python, Ruby, etc.)? For example, are there specific language features of Fortran that maybe allow numeric optimization in compilers to a much higher degree compared to other languages I mentioned?

13
  • 2
    Just a quick straw poll: does anyone here actually use Fortran in the wild (rather than at school)? Does anyone here know anyone (first hand, not a friend of a bloke you met at the pub last year) who uses it?
    – paxdiablo
    Jan 25, 2012 at 2:22
  • 18
    @pawdiablo : I have written, I write and I will write Fortran codes ! Therefore, you cannot say anymore that you don't know anybody using Fortran ;-) Jan 25, 2012 at 5:02
  • 5
    @pawdiablo Yes. I'm part of a research group (one of many groups at a university) of a few tens of scientists who all use Fortran daily.
    – Chris
    Jan 25, 2012 at 9:47
  • 4
    I assume "in the wild" means coding in underwear while having Cheetos and diet dr. Pepper. Jan 25, 2012 at 13:58
  • 4
    @paxdiablo - Yes, using it "in the wild" (in a company with no relations to school or academia).
    – Rook
    Jan 25, 2012 at 14:05

5 Answers 5

92

Fortran is, for better or worse, the only major language out there specifically designed for scientific numerical computing. It's array handling is nice, with succinct array operations on both whole arrays and on slices, comparable with matlab or numpy but super fast. The language is carefully designed to make it very difficult to accidentally write slow code -- pointers are restricted in such a way that it's immediately obvious if there might be aliasing, as the standard example -- and so the optimizer can go to town on your code. Current incarnations have things like coarray fortran, and do concurrent and forall built into the language, allowing distributed memory and shared memory parallelism, and vectorization.

The downsides of Fortran are mainly the flip side of one of the upsides mentioned; Fortran has a huge long history. Upside: tonnes of great libraries. Downsides: tonnes of historical baggage.

If you have to do a lot of number crunching, Fortran remains one of the top choices, which is why many of the most sophisticated simulation codes run at supercomputing centres around the world are written in it. But of course it would be a terrible, terrible, language to write a web browser in. To each task its tool.

3
  • 8
    FORTRAN is traditionally used for scientific/engineering numerical computing for reasons of inertia as much as anything else, as well as having lots of specialized libraries. FORTRAN, like C, is a "super assembler" that lets you get close to the metal without adding a lot of fluff to make programming easier, or protect you from yourself -- Programming for Real Men(tm). It has a very long history of compiler optimization work, but there's no reason that other languages can't use most of those optimizations. It was designed before there was real language/compiler science, and shows its age.
    – Phil Perry
    May 9, 2014 at 15:38
  • I've not been doing in any other language what I had been doing in Fortran for 3 years (CFD); in addition, I don't feel expert of any "major" language (Fortran/C/C++, for instance); however, based on the bits I know for each of them, it's so hard for me to think that all those advantages that you mention would be so hard to be made available, for instance, in C++ through a well written library. In conclusion it looks to me that scientific numerical computing community is mostly stuck in Fortran just because there's little sheer will to rebuild those features in a more modern language.
    – Enlico
    Feb 16, 2020 at 12:12
  • The first sentence ("Fortran is, for better or worse, the only major language out there specifically designed for scientific numerical computing.") appears to no longer be true with the advent of Julia (and maybe other languages). It would be bewildering to me if a new programmer looking for a language to do scientific numerical computing chose Fortran over something like Julia in 2020. That said, Julia still does use Fortran under the hood for some of its functionality... for now :)
    – Grayscale
    Jun 12, 2020 at 4:10
18

The main reason for me is the nice array notation, and many other design decisions that make writing and debugging scientific code easier. The fact that it is usually the best choice in terms of performance on the relevant tasks (array operations) does not hurt either :)

Honestly, I would not consider most the languages cited as real competitors for Fortran -- Java and Ruby are far, far behind in terms of both convenience and performance, while C++ is much too complex and tricky a language to recommend to anyone whose main job for the last few years has been anything other than daily programming in C++. Python with numpy could be an option though. I am personally not a huge fan of the language, but I know a number of people who use numpy regularly and seem quite happy with it.

Real competition I see is not from these, but from Matlab, R, and similar languages, that offer similar convenience, combined with many standard libraries. Luckily, it is usually possible to start a project in R or Matlab, and write performance-critical parts in Fortran later.

4
  • 1
    You argue against "competitors" with their assumed slow speed. This is not true (anymore). Some references were given in the answers here. Due to the evolution of computer architecture, it will even be less true for the future. Or can you imagine, writing a grid computing app in FORTRAN, utilizing multiple cores, SIMD instructions on its nodes - possibly even utilizing GPU power ... ? Of course, it IS possible and hence some people will do. But they will be wasting their time by doing so. Mar 4, 2012 at 21:16
  • 6
    Hmm I am not quite sure what are the presumably better alternatives you are referring to here? I doubt we are going to see java or ruby gaining popularity for HPC -- their purpose is quite different. And there are lots of grid apps being written in fortran (probably about 50%, the other half being C/C++). What do you find more suitable?PGI accelerator fortran btw is surprisingly good at programming GPU's, I'd recommend it over CUDA any time for portability.
    – laxxy
    Mar 5, 2012 at 19:50
  • 1
    Competition is slowly starting to arrive: julialang.org Jul 6, 2019 at 21:32
  • Numpy is actually mainly written in Fortran... (at least the core operations). Apr 8, 2022 at 0:36
17

Few projects are completely new projects. I'm not sure it's specific to scientific computing, but at least in this field, you tend to build your applications based on existing (scientific) models, perhaps produced by other groups/people. You will always have to deal with some amount of legacy code, whether you want it or not.

Fortran is what a lot of scientists have been taught with and what a lot of the libraries they need are implemented in. A number of them might not be computer scientists or IT people, more computational scientists. Their primary goal is rarely computing, it's their science first. While a large number of programmers would have a tendency to learn a new programming language or framework whenever they get a chance (including during their spare time), most scientists would use that time exploring new ideas regarding their science.

A domain expert who's trained in Fortran (or any language) and surrounded by people who are in a similar situation will have no incentive to move away from it. It's not just that now other languages can be as good as Fortran in terms of performance, they need to be much better: there needs to be a good reason to move away from what you have and know.

It's also a "vicious" circle to a degree. I've always found comparisons between Java and Fortran a bit difficult, simply because a number of Java scientific applications are not programmed in a Java way. Some of the Java Grande benchmark applications look clearly like Fortran programs turned into C programs, copied/pasted/tweaked into Java programs (in a method, passing the length of the array as an extra parameter next to the array itself gives a clue, if I remember well). Because of this, Java (for example) hasn't got a great reputation in the scientific community, even though its performance is getting better. A consequence of that is that there is little overlap between HPC experts and Java experts, for example. Even from the hardware vendors or libraries implementors, little demand from users leads to little support offered, which in turns deters users who would potentially be interested in moving to other languages.

Note that this doesn't preclude the same (or other) scientists from using other languages for other purposes (e.g. workflow management, data management, quicker modeling with Matlab, Numpy, ...).

2
  • 1
    +1 for the link to the paper. Here is a similar comparison for .NET: ilnumerics.net/blog/… It is not grid related but here .NET even outperforms Fortran in the kmean algorithm! Mar 4, 2012 at 21:08
  • 2
    The link to the "Current State of Java for HPC" paper is dead...I think this is the same paper: docs.huihoo.com/proactive/ProActiveJavaStatusforHPC.pdf. However, it's from 2008, and I can't find anything more recent...caveat emptor
    – gariepy
    May 2, 2016 at 16:41
3

As I understand it, there are libraries that are some of the most efficient implementations of their algorithms available, which makes Fortran popular for this kind of work in spite of the language's limitations.

5
  • 5
    And what are the limitations of Fortran please ? I know several programming languages quite well like C, Fortran, JAVA. When I need to program interactive codes with graphical parts, I choose JAVA. But when I want to program a scientific code with array manipulations, I choose Fortran. And when I want to program something which needs computer science tricks, C is usually the best choice. Jan 25, 2012 at 4:58
  • 4
    Fortran is a 1960's procedural based language with very little in the way of modern language features. Arguably it is Basic on steroids (yes I know it predates Basic) with little concept of scope, algorithmic control structures and a rudimentary I/O model. The lack of variable initialization checking for example allegedly was responsible for the loss of a US satellite (misspelled variable name).
    – nfc-uk
    Apr 28, 2013 at 11:56
  • 2
    @nfc-uk Are you comparing FORTRAN 66 with languages originating in the 90s, that should be compared with Fortran 95 and later? Fortran programming is all about scope, it has many control structures and I/O comparable with C plus to own modes in addition to the C one. Oct 14, 2014 at 22:19
  • These libraries can be wrapped in other languages, as is done in Julia or SciPy.
    – Grayscale
    Jun 12, 2020 at 4:19
  • @Grayscale: Such wrappers add a layer of indirection (however thin) which you don't want if raw performance is your goal. Jun 12, 2020 at 12:14
-5

One reason is in how the arrays were constructed. They are column major, unlike most other languages. This provides faster computation for their calculations.

9
  • 8
    Wouldn't it be easier to just switch the order of one's indices than switch to a different language? Jan 25, 2012 at 2:13
  • 3
    @jheld - Why do you think it's a pretty bad language nowadays?
    – Rook
    Jan 25, 2012 at 3:47
  • 3
    I don't see why the order of indices could have an impact on performance... if you choose the right algorithm which might of course depends on that order. Jan 25, 2012 at 5:11
  • 9
    Being column major has nothing to do with computational efficiency. Jan 25, 2012 at 6:00
  • 5
    @IRO-bot: exactly, the physical memory anyway is linear (1-dimensional) and it does not matter if matrices are stored column by column or row by row. It is, however, important to use the right, language specific order of indices within loops. Otherwise the code can get really slow...
    – alexurba
    Jan 25, 2012 at 9:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.