[Nov 23, 2009 14:09] Web access to Microsoft Live@edu accounts now works.
Table of Contents
- Introduction
- Additional Documentation
- Creating the Source File
- Compiling Your Program
- Linking Your Program
- Running Your Program
- Debugging Your Program
- Optimization
- FORTRAN 90
Introduction
- This document introduces you to the IBM AIX XL FORTRAN compiler running on the RS/6000 Unix machines. We discuss the steps to create, compile, and run a FORTRAN program, as well as debugging tools.
- In the section on optimization, alternate commands are given for calling the compiler that activate the optimization feature. The default for the basic compiler call xlf is not to use optimization. Once your program successfully compiles, you are strongly encouraged to use optimization, either with one of the aliased names for the compiler or by turning on the feature manually, since this usually results in greatly reduced run time.
- Utilities for debugging FORTRAN programs are discussed and the compiler for the FORTRAN 90 language extension is introduced.
Additional Documentation
The following IBM manuals are available, for reference only, in the Unix lab at Thornton Hall E225. Personal copies may be ordered from the local IBM Marketing Representative. Copies have also been placed in the Science and Engineering Library (Clark Hall).
| Document No. | Title |
|---|---|
| SC09-1354 | AIX XL FORTRAN Compiler/6000 User's Guide |
| SC09-1353 | AIX XL FORTRAN Compiler/6000 Language Reference |
| SC09-1545 | Optimization and Tuning Guide for the XL FORTRAN and XL C Compilers |
| SC23-2199 | AIX Commands Reference for IBM RISC System/6000 |
| SC23-2198 | AIX Calls and Subroutines Reference, Vols. 1 & 2 |
Manuals can be viewed on-line using the InfoExplorer by entering the command:
info
and then selecting Programming followed by Languages to get a list of available manuals. The AIX FORTRAN manuals can be accessed by invoking the InfoExplorer with the command:
info -l xlf
Visit the IBM home page for AIX FORTRAN >>
Creating the Source File
A source file is the file that contains your program. Use an editor, e.g. Jove or Vi, to type your program into the source file or to edit it. The file name must have the file extension .f. It should have the attributes particular to FORTRAN, namely, statements must appear between character positions 7 and 72, inclusive. A tab character placed anywhere in columns 1 through 6 will tab to column 7. The text of statements except the END statement, and the EJECT, INCLUDE, and @PROCESS compiler directives can continue to the following line by placing blanks in columns 1 through 5 and a character other than blank or zero in column 6.
Compiling Your Program
The xlf command invokes the XL FORTRAN compiler, which then translates FORTRAN source code statements into object code, sends .s files to the assembler, and links the resulting object files with object files and libraries specified on the command line in the order shown, producing a single executable file called a.out by default. The -o flag may be used to rename the resulting executable file. For this document, commands you are instructed to enter are terminated by pressing the Return (aka Enter) key.
To compile a source program and/or subprograms on disk, type the following command:
xlf cmd_line_opts input_files
where cmd_line_opts refers to compiler options. See the XL FORTRAN User's Guide for information on the options you can use. input_files are source files (.f), object files (.o), or assembler files (.s)
For example, to compile a FORTRAN program whose source is in source file prog.f you would enter the following command:
xlf prog.f
After the xlf command completes, you will see a new executable file in your directory called a.out.
The -O option instructs the compiler to optimize the instructions produced. As previously mentioned, using this feature greatly reduces run time in most cases. Please refer to Optimization section of this document for more details about this option, including several alias names we have provided for the compiler.
The following examples illustrate the syntax required for some of the more commonly used options. Note that several options can be used concurrently:
| Command String | Operation Performed |
|---|---|
| xlf -C prog.f | perform runtime checking of arrays bound |
| xlf -O prog.f | compile with optimization |
| xlf -qsource prog.f | produce a listing file "prog.lst", along with a.out |
| xlf -o runprog prog.f | produce an executable named "runprog" instead of a.out |
| xlf -lkey prog.f | selects the FORTRAN library file named libkey.a |
| xlf -qextchek prog.f | performs procedure interface checking as well as detection of mismatched common blocks |
Another useful tool for compiling FORTRAN programs is the make command generator. Make is especially useful in object oriented programming. It has the ability to decide whether parts of a file need to be compiled (i.e. which source files have been modified, or have missing object files). After compiling a part of the program, it can also be told to link the object files together.
MIT provides an excellent source of information about makefiles.
Linking Your Program
If you specify -c as a compiler option, XL FORTRAN only compiles the source program, producing an object file whose default name is that of the program with a .o extension. Before running the program, you must proceed with the linkage editor phase. Either invoke the linker using the ld command or issue the xlf command a second time without the -c option, using the desired object file (.o) names.
For example, you may compile a subprogram second.f and then use it in your main program prog.f, with the following sequence of commands:
xlf -c second.f
xlf prog.f second.o
See the AIX Commands Reference Manual for further information on the linkage editor and the link-edit flags.
Running Your Program
After your program compiles without error you may run it. Standard Unix redirection may be used with the executable a.out as follows:
a.out < infile > outfile
where infileis the input file and outfile is the output file.
Debugging Your Program
Segmentation errors often occur when arrays are indexed beyond their dimensioned bounds. A commonly used compiler option for performing runtime checking of array bounds is:
xlf -C prog.f
Two other tools are available for detecting and correcting errors in a FORTRAN program. The first is the dbx symbolic debugging package which is discussed in the ITC document U-029, The dbx Debugger on the RS/6000. In order to use the dbx debugger, compilation should be done with the -g option and no optimization.
xlf -g prog.f
The executable a.out can then be run from within the dbx environment. An X-window version of the debugger, xdbx, is also available.
The second debugging tool is the FORTRAN program checker ftnchek. This utility is run directly on the source code to detect semantic errors that the compiler might miss.
ftnchek [-option, -option ...] prog.f
Further information can be accessed online by typing
man ftnchek
at the command line.
Optimization
Several commands have been aliased on the RS/6000 to invoke the compiler with the -O option, which activates the optimization feature. The commands are as follows:
- xlf -O
- f77
- ftn
- fortran.
Additional options, as outlined above, may be used with these commands. Note that, although the time to compile may be longer, the savings in run time should be worth it. You are advised not to use optimization when debugging, using instead the basic xlf command until the program compiles and runs.
Programs that appear to work correctly when compiled with no optimization will occasionally fail when optimized. Most often this is caused by program variables that have not been initialized. More virtual storage, system page space, and longer compilation times are required for optimization. Further information on program optimization can be accessed through the InfoExplorer utility by typing:
info -l xlf
and selecting the Optimization and Tuning Guide.
The following general programming guidelines will help you to take advantage of the optimization feature:
- Use unformatted input/output to use less time and storage.
- Since each reference to a common block variable requires that the address of the common block be in a register: Minimize the number of common blocks. Group concurrently referenced variables into the same common block. Place scalar variables before arrays in a common block. Place small arrays before large ones in a common block. Assign frequently referenced scalar variables in a common block to a local variable. Remember to assign these values back to the common block before exiting the subprogram. REAL*8 values are handled more efficiently than REAL*4 values. Make certain that all REAL*8 variables begin at REAL*8 intervals in a common block. Use padding if necessary to ensure this.
- Define constant operands as local variables.
- Make the adjustable dimensions of an array the high-order dimensions.
- Initialize large arrays using a DO loop rather than a DATA statement.
- Use parentheses to group duplicated expressions.
- If your code contains a short, heavily-referenced loop, consider expanding the loop into a straight sequence of statements.
- Avoid forcing the compiler to convert numbers between integer and floating point internal representation.
- Change simple divisions to multiplications.
- Use a block or logical IF rather than arithmetic IF statements. In IF statements involving multiple logical comparisons, put the simplest condition and/or the tests most likely to be decisive in the leftmost position.
There are three tools available on the RS/6000s that can assist you with optimization. The simplest is the timex command, which reports in seconds, the elapsed time, user time, and system execution time for a command. With specified flags, the timex command lists or summarizes process accounting data for a command (executable file) and all of its children.
timex a.out
The on-line manual page can be accessed by typing
man timex
at the command line.
The other two tools are the Unix utilities prof and gprof, which generate execution profiles. The profile for prof breaks down the execution time to the subroutine and functional level. The profile for gprof also includes which routines call which. Manual pages are available online for both utilities (man prof, man gprof). To enable profiling, relink the programs object modules with one of the following commands to enable prof and gprof, respectively:
xlf -p prog.f
xlf -pg prog.f
After running the executable and the program completes, type the name of the profiler you selected and the information about time spent in subroutines and functions will appear. Concentrate your efforts on optimizing the code in the one or two routines where most of the time is spent.
FORTRAN 90
The FORTRAN 90 programming language is an extension of the ANSI standard FORTRAN 77 (f77, xlf) language and includes many features similar to C++ and object oriented programming. The list of features include memory allocation, array operations, automatic arrays, pointers, defined operators, derived types, overloaded operations, generic interfaces, optional arguments, and modules. The IBM AIX version of the FORTRAN 90 compiler is xlf90 and the compilation is of the form:
xlf90 cmd_line_opts input_files
where cmd line_opts and input_files are defined as for the xlf compiler. Further information on the xlf compiler can be accessed through InfoExplorer by typing:
info -l xlf
and performing a search on xlf90.