Thursday, November 02, 2006

Why we use Fortran and Python

From Mark Chu-Carroll, a post on why C/C++ aren't always fastest, which is of course well known in (large parts of) the scientific computing community: Fortran compilers can perform much better optimisation than C compilers, because Fortran has true arrays and loop constructs, as opposed to C's sugar-coated assembler. C is a great language to develop an operating system or a device driver, but not to write computationally intensive code that could benefit from parallelisation, where Fortran beats it easily. And what about C++? The object-oriented features of C++ are nice for scientific applications, sure; you can have matrix, vector and spinor types with overloaded operators and such. But Fortran 95 has those things, too, but doesn't suffer from the problems that C++'s C-heritage brings. And Fortran 95 has even nicer features, such as elemental functions; that's something that no C-based language can give you because of C's poor support for arrays. And in case there is some object-oriented feature that you feel is missing in Fortran 95, just wait for Fortran 2003, which includes those as well.

But what about developing graphical user interfaces? Fortran doesn't have good support for those, now, does it? No, it doesn't, but that's besides the point; Fortran ("FORmula TRANslation") is meant as a number-crunching language. I wouldn't want to write a graphical user interface for my code in either Fortran or C/C++. For these kinds of tasks, I use Python, because it is the most convenient language for them; the user interface is not computationally intensive, so speed isn't crucial, and for the number crunching, the front end calls the fast Fortran program, getting you the best of both worlds -- and without using any C anywhere (other than the fact that the Python interpreter, and probably the Fortran compiler, were written in C, which is the right language for those kinds of tasks).

Most lattice people I have personally worked with use Fortran, and a few use Python for non-numerical tasks. Of course there are large groups that use C or C++ exclusively, and depending on what they are doing, it may make sense, especially if there is legacy C or assembly code that needs to be linked with. But by and large, computational physicists are Fortran users -- not because they are boring old guys, but because they are too smart to fall for the traps that the cool C++ kids run into. (Oh, and did I mention that Fortran 95 code is a lot easier to read and debug than C++ code? I have debugged both, and the difference is something like an hour versus a day to find a well-hidden off-by-one bug.)