Go to Page Main Content
NASA - Goddard Institute for Space Studies
FIND IT @ NASA
NASA Homepage Goddard Institute for Space Studies

RESEARCH

The GISS GCM Good Programming Guide

Version 1.0 February 1999
Gavin Schmidt, Jeff Jonas, Jean Lerner & Ruedy Reto

The GISS GCM (and all attendant offshoots) has developed into a large distributed effort. This guide is an effort to help integrate GISS-wide good coding practices that improve the efficiency of the code, make it more transparent and hopefully (in the long term) lead to some degree of homogenization.

This guide will be split into a number of sections. Firstly, we will highlight some of the common examples of 'bad' code and indicate some better alternatives. These should become common sense programming habits. The next section deals with the ways of getting rid of unnecessary GO TO statements, and discusses more structured approaches. We then outline some of the more useful elements of FORTRAN 90which can be used to enhance the readability of the code. The first appendix describes tools that are under-utilised but can be very helpful. The other appendix contains some of the issues that arise in porting the model code to the SGI machines. Examples are given where we feel the correct behavior is not obvious.

This is not intended to be a comprehensive guide to FORTRAN (or to the GISS GCM). It is intended only to highlight some of the more common problems that occur in the model and should be seen as a useful reference for the programmers and scientists in the building.

Please let us know about any features that could be added to this document. They will appear in the next version (contact gavin@giss.nasa.gov).

What to definitely avoid

Further information on optimization is available in the ``XL Fortran: Optimization Guide'' handbook or the IRIX Fortran manual.

NASA GISS: Unstructured habits

Unstructured habits

Much of the GCM code (and some of the programmers!) dates back to the punch-card days of yore. In contrast to the current fashion for 1960's nostalgia, we believe that the code really should reflect some of the advances made by FORTRAN 77 (and maybe even FORTRAN 90!). Many of the most confusing examples are driven by a desire to write compact code. While this was an issue with punch-cards, it is no-longer so important. However, if the desire for compact code is still present, we recommend you use the new features of FORTRAN 90 which allow extremely compact code to be written in clear and unambiguous ways (see next section).

New structures from FORTRAN 90

Many of the new features in FORTRAN 90 significantly reduce the amount of coding needed and makes the code much more readable. There is much more to FORTRAN 90 then we can do justice to here, but we particularly want to highlight the array processing facilities. To use FORTRAN 90constructs, you need only change f77 to f90 in your compilation scripts. There is one major caveat: the GCM II' as a whole does not run if compiled with f90. Some parts of it (the radiation?) cannot be compiled, but others have included f90 features with no problems. Gary's model can be run with f90. Please ensure that any routines you modify will compile with f90 prior to including any new constructs. Please report any problems (and solutions!) you uncover.

One last thing...

COMMENTS ARE GOOD! Please, please, please comment any code you write. It doesn't need to be very verbose, but important calculations, break points, branches etc. should be flagged. At some stage, other people will try to work through it - please think of them! In particular, messages printed out before a stop due to unrealistic conditions should be verbose rather than cryptic.

Appendix I: Helpful tools

The dbx debugger

dbx is a powerful debugging utility that can be used quickly to diagnose some common problems. If you have a core dump, then go to the run directory and type

dbx program.exe core

At the prompt, typing 'where' will give the routine and line number of where it died. Compiling with the -g option increases the amount of information available, i.e. 'print variable' will display the value of the variable at the time of the core dump.

The KAP preprocessor

A Preprocessor rewrites your fortran code before sending it to the compiler and can be very useful in identifying bottlenecks and inefficiencies in your code. Using the preprocessor can lead to significant improvements in time (average 10-15% faster for Gary's model). It is called via an option to the fortran compiler but is not available on all machines.

Appendix II: Modifications necessary for SGI computers

  1. All FORTRAN and C utilities have to be recompiled.
  2. Most ksh scripts are ok, some have to be modified - test them. Please be sure to add #!/bin/ksh at the top of all scripts, so that those who have different login shells can use them.
  3. xlf .... has to be replaced by f77 .... (or f90) There has to be a space between -o and name-of-executable-file
      OPTIONS:
      in model routine compilations the options -n32 -static or
      -64 -mips4 -static are needed (the 2nd gives better performance).
      -static guarantees that local variables in a subroutine are saved after returns, i.e. IFIRST-constructions should work.
      -old_rl should be used with direct-access files to make it compatible with the IBMs
      -OPT:reorg_commonoff is needed with the new release
      -mp (in the link step) will allow large jobs to run much faster, if your .profile contains the line:
      export PAGESIZE_DATA64; export PAGESIZE_STACK64; export PAGESIZE_TEXT64
      -Wl,-woff,134 -Wl,-woff,15 arguements for f77/ f90 used when linking (in setup or mkexe) removes the pointless warnings, but leaves any important ones.
  4. Optimizations ( -O3 cannot be used for individual subroutines for -n32 ) what worked so far is:
    f77 -n32 -static -O2 -c xyz.f (-> xyz.o) or
    f77 -64 -static -O3 -c xyz.f (-> xyz.o)
    for more speed:
    f77 -64 -mips4 -static -O3 -OPT:fold_arith_limit1409
    -O2 is almost as fast and safer than -O3 (optimization level)
    link options:
    -mips4 lfastm (uses library of fast math.routines)
  5. Differences in FORTRAN (missing/obsolete features etc):
    • READ(...,NUMnbytes) is unavailable
    • BLOCK DATA have to be named
    • 'open' cannot be the name of a COMMON BLOCK
    • T,F cannot be used to initialize logical variables, unless
      PARAMETER (T.TRUE.,F.FALSE.) is added
    • Use STOP rather than RETURN in MAIN.
    • The function ERFC only takes real*4 args, use DERFC for real*8 args.
    • The compiler warns if you use CALL SUBR(A,..) where A is a variable whereas A is an array in the subroutine (using of course only A(1))
    • If the arrays and their dimensions are passed to a subroutine, the dimensions have to be passed also in each entry that passes the arrays (the original HNTRP does not work on the SGIs)
    • The following construction does NOT work (with or w/o -static)
      SUBROUTINE ABC (v1,...)
      (no v1... statement)
      ENTRY XYZ (v1 is NOT an argument)
      expression involving v1          (result unpredictable if
                                        reached after CALL XYZ)
      
      Quick (but ugly) fix:
      SUBROUTINE ABC (v1xxx,...)
      v1v1xxx
      
  6. NAMELIST READ('string',NML...) is unavailable ; namelists have to be read from external files: back to 66-version (using unit 8 instead of 14)
  7. System calls to save rsf, acc files did not work reliably. Use simple OPEN instead as in model II' ( MB112M9.S).
  8. Model-specific changes:
    1. remove all READ(....NUM...) instances ( INPUT,readt in UTIL..)
    2. replace internal NAMELIST reads by reads from unit 8 ( INPUT,2x)
    3. name all BLOCK DATA (diagnostics, rad.routines, input,soils)
    4. replace RANDU-subroutine (add RANDSGI)


gavin@giss.nasa.gov
USA.gov

End of Page