Next Up Previous Contents
Next: 2.4.1 Fortran 77
Up: 2 Computing
Previous: 2.3 Numerical analysis
[ID index][Keyword index]

2.4 Programming languages

I speak Spanish to God, Italian to women, French to men, and German to my horse. Emperor Charles V (attr.)

There is no One True Programming Language, nor even One True Compromise.

The language you use to write code is as much a function of your background, environment and personal tastes, as it is a function of technical merit. At some level or another, all programming languages are equivalent, but just as you'd look askance at anyone who wrote a stellar atmosphere code in TeX[Note 4], it's clear that some languages are more suited for some tasks than others.

The obvious issue at this point is the choice between Fortran and C. Let me state first of all that, for general scientific computing, I do not believe the difference between the two is substantial enough, or the advantages of each are unqualified enough, that it warrants abandoning the language you're comfortable with and learning the other.

The languages' respective strengths only become significant when you are deep within their `native territories': for Fortran this territory is that of numerically intensive codes with week-long runtimes, for C it is intricate manipulation of data structures. Away from these extremes, the choice is at the level of which to choose for a particular application, given that you know both, and an ideal application might be said to be one with a Fortran heart whirring away in a C harness.

Though Fortran and C are both simple languages, Fortran is simple in ways that an optimizing compiler can exploit. This means that for a numerically intensive application, where processing speed is very important, Fortran is the only suitable language.

Why is this? The reason can be illustrated by the two languages' loop semantics. Fortran's loop is very simple:

   do 10, i=1,n
C  process array element a(i)
10 continue
The equivalent construction in C is

    for (i=0; i<n; i++) {
       /* process array element a[i] */
The difference is what the two language compilers can say about the loop. The Fortran compiler can know that the loop will be performed exactly n times, since neither i nor n may be changed within the loop; also the array a(i) cannot overlap with any other array within the loop body. On the other hand, in C, the loop for (init; test; increment) { body } is defined to be equivalent to the loop init; while (test) { body; increment; }, so that both i and n could change within the loop, and since a could be a pointer, it could point anywhere. The Fortran compiler could rewrite the first loop as

      do 10, i=1,n,4
C     process array element a(i)
C     process array element a(i+1)
C     process array element a(i+2)
C     process array element a(i+3)
   10 continue
cutting the number end-of-loop tests by a factor of four (this is a simple example of `loop unrolling', and presumes that n is divisible by 4). The loop semantics could also make the elements of the loop candidates for parallelization, if the compiler and hardware support that[Note 5]. The C compiler has to do a lot more investigative work before it can make any similar rearrangements.

Even if there were no such fundamental differences, the fact would remain that Fortran vendors have always sold their products on the strength of their optimizers, so that it is both easier and profitable for Fortran compilers to produce very fast code.

Though C's loop semantics are troublesome, it is really the pointer type which causes a lot of the trouble -- since a pointer is little more than an address, the compiler can have little clue what the programmer is aiming to do with it, and so has little choice but to make a literal translation of the C code into assembler.

The pointer type is redeemed, however, because it is this which allows C to code complicated data structures so naturally: not only aggregate types, but linked lists, queues, and all the other ways of holding, reading, parsing and generally juggling data, so beloved of Computer Science 1.

Fortran 90/95 goes some way towards addressing Fortran's weaknesses by introducing pointers (though the fact that they're living in a Fortran code doesn't make them any less troublesome to an optimizer), as well as proper data structures and some modularisation. At the same time as Fortran does that, however, C++ uses object-orientation to further enhance C's advantages in data manipulation, going so far as to make functions auxiliary features of data structures.

For general discussions of the issues involved in high-performance computing, see the book High Performance Computing [dowd].

Starlink ensures that C, C++, Fortran 77 and Fortran 90/95 compilers are available at all sites.

Next Up Previous Contents
Next: 2.4.1 Fortran 77
Up: 2 Computing
Previous: 2.3 Numerical analysis
[ID index][Keyword index]
Theory and Modelling Resources Cookbook
Starlink Cookbook 13
Norman Gray
2 December 2001. Release 2-5. Last updated 10 March 2003