Back to CFM home             Brown University

Programming in Fortran

Fortran is one of the oldest programming languages around. It has survived and thrived through the years, despite its disadvantages due to:

Still, Fortran in its most popular language level, Fortran 77, suffers from several disadvantages, the most important of which for numerical programming are:

Still, the language is by no means a dead language, having progressed from Fortran IV, to Fortran 66 to Fortran 77. These days more and more programmers are shifting to Fortran 90, the new language standard that was agreed upon in 1990. There is an even more recent standard, Fortran 95 (no compiler fully implements this, as it is still in the revisions stage). Both Fortran 90 & 95 are backwards compatible with Fortran 77 code but add several new language constructs and capabilities. Only very old Fortran IV & 66 constructs have become obsolete with these new standards. Among the Fortran 90 advantages are dynamic memory allocation (at last), pointers (though more restricted than those of C), and some object oriented concepts. Moreover a new concise array operation syntax is introduced that gets rid of a lot of loops, making the code more readable & mathematical. Unfortunately, Fortran 90 compilers are not yet as capable as their Fortran 77 ancestors and produce less optimized code, but the gap should be closing. You are encouraged to start converting (or start from) Fortran 90 as this will also make parallelizing your code easier using HPF. High Performance Fortran is a superset of Fortran 90 with special constructs to specify a data parallel mode of parallelism. A program written in HPF should work both as serial code and as parallel code depending on the compiler/platform it is run on. Of course, hand optimizing an HPF program is something one would almost certainly need to do, as currently HPF compilers are still in their early steps and don't produce high quality optimized code easily. Both academia and the industry however is rapidly moving in that direction.

  1. Compiling & Linking
  2. To compile a Fortran program in file fortran_program.f (note Fortran programs on most systems need to be in files with a .f extension - for Fortran 90 it is usually, with the exception of the IBMs, .f90. Other extensions include .for, .ftn and .F - check the man pages of the compilers for more information, including information on how to deal with the source code format, be it free or fixed):
    	% f77 fortran_program.f -o executable_name
    If you omit -o executable_name the executable binary will be called a.out by default.
    If your program is split over more than one file, you can either compile them all on the same line:
    	% f77 fortran_program1.f fortran_program2.f -o executable_name
    or compile them separately and then link the resulting object files (".o" files) together:
    	% f77 -c fortran_program1.f
    	% f77 -c fortran_program2.f
    	% f77 fortran_program1.f fortran_program2.f -o executable_name
    If some of the include files that you require (by # include <include_file.h> (you can use cpp style includes in Fortran) or include 'include_file' statements in your Fortran source code) are not in the standard include file search paths the C preprocessor cpp (called by the Fortran compiler in case it meets any include statements) searches in, you can specify them by the -Iinclude_search_path flag:
    	% f77 -I/usr/local/mpich/include -c mpi.f 
    Similarly, if some of the library functions that your program uses are not to be found among the standard libraries the linker looks in for a Fortran program, you have to specify them yourself, sometimes including the path to the library if it not in the standard directory the linker looks in:
    	% f77 mathfortran_program1.o mathfortran_program2.o -o executable_name -lblas
    	% f77 mpi.o -o mpi-test -L/usr/local/mpich/lib/IRIX/ch_shmem -lmpi
    If you are using a library whose filename is libblas.a, you specify it as -lblas.

  3. Compilers
  4. f77 is the standard name for a Fortran 77 compiler and f90 for Fortran 90 one, but not there are other options:

  5. Optimization flags
  6. Please make sure that once you've developed and debugged a code you compile it optimized for any production runs you make. Running unoptimized production code is a waste of your time as well as well as CFM computing resources. One only needs to take care when using optimization flags as high optimization levels can alter the semantics of your code and produce significantly different and hence erroneous results. You should check the respective man pages for each compiler to see which optimization flags pose such a threat. In that case it is necessary to test your optimized code by comparing the results of one or more of its runs with those of the code compiled with no optimization. If the difference in the results is small (machine or algorithmic accuracy) then go ahead and use the optimized code. If the difference is large enough for the results to be wrong, choose a lower optimization level and try again. Despite the extra trouble you may have to go through, please try and compile your code optimized, you may be very surprised by how much the time it takes to run (especially if it is well written) decreases! And of course always remember that usually the best optimization is a better algorithm. It is suggested that you look up the man pages for the compiler you plan to use for the best results. (Note that sometimes even supposedly safe optimization options could cause problems due to bugs in the optimizer.) Suggestions for optimization flags for the Fortran compilers on our systems are as follows:

  • For more Fortran related information