This brief contains information on compiling, debugging and executing XL FORTRAN Version 2 programs in the AIX operating environment of Tulane's IBM RISC System/6000 workstation cluster. AIX is IBM's implementation of the UNIX operating system. Section 5 of this brief covers special features of Fortran 90. For more information on AIX, please consult related TCS documentation. For information on the FORTRAN programming language itself refer to the XL FORTRAN manuals listed below.

The XL FORTRAN Version 2 compiler is a full implementation of the American National Standards Institute (ANSI) standard for FORTRAN 77 with selected VS FORTRAN and FORTRAN 90 features. The XL FORTRAN compiler uses the ANSI/IEEE binary floating-point representation. As a special feature, XL FORTRAN enables inter-language calls, which permit you to call subroutines written in other programming languages. This brief does not cover topics related to different FORTRAN versions and inter-language calls. Please contact TCS for further information on these features.

Section 1: Compiling and Running FORTRAN Programs

File naming conventions
Compiling and running FORTRAN programs is a multi-step process. During this process the XL FORTRAN compiler takes FORTRAN source files, object files, libraries and assembler files as input. It can produce executable files, source listings, object files and modified (preprocessed) source files.

The file type in AIX is usually expressed by the filename extension. The following file naming conventions are recognized by the XL FORTRAN compiler:

Filename extensionExample filename
FORTRAN source files.ftest.f
Object files.otest.o
Assembler files.stest.s
Source listings.lsttest.lst
Default executable filenamen/aa.out

Invoking the compiler
The multi-step compilation process starts with the creation or modification of your FORTRAN source file. You can compile and run your program in four steps: preprocessing (this is optional), compiling, linking, and running. The preprocessing, compiling, and linking phases can be combined into one step. To compile the source program, use the xlf or f77 commands. Note that the xlf and f77 commands can be used interchangeably. To invoke FORTRAN 90, use the xlf90command.

xlf [option(s)] [filename(s)] [option(s)]
If neither option(s) nor filename(s) is specified then the online help information on the XL FORTRAN compiler will be displayed. By default, the compiler automatically calls the loader and creates an executable file.

In the simplest case you can invoke the XL FORTRAN compiler and the loader without specifying any compiler options. For example, if the name of the FORTRAN file is example.f the following command will compile the code and store the executable in the default a.out file:

xlf example.f
Running the program
You can run the program by typing in the name of the executable file. For example, if your executable has the default filename you can run it by entering
Compiler options
XL FORTRAN provides compiler options to change any of the compiler's default settings. There are two methods for specifying compiler options on the command line: -qoption and -option_flag. The available compiler options include optimization features, library access information, language compatibility, and compiler listing control features. Some of the most commonly used options and their meanings are:

-o nameNames the resulting executable name instead of a.out.
-OOptimizes code generated by the compiler.
-O3Performs the -O level and other optimizations.
-cCompiles without calling the loader, and creates .o object files.
-llibrarySearches the specified library to resolve external references.
-LdirectorySearches the directory for the library specified by the -llibrary option.
-IdirectorySearches the directory for include files.
-gGenerates debugging information.
-pGenerates profiling support code.
-vDisplays verbose information on the progress of the compiler.
-qfipsTurns on full ANSI FORTRAN compatibility mode.
-qF77Turns on FORTRAN 77 compatibility mode.
-qsourceProduces source listing in the compiler listing file.
-qattrProduces attribute listing in the compiler listing file.
-qxrefProduces cross reference listing in the compiler listing file.
-qdpcPromotes single-precision constants to double precision.
-qautodbl=dblpadPromotes single- and double-precision values to double and extended precision, respectively.
-qrecurAllows recursive subroutine calls.
-qfreeAllows free format FORTRAN source file.
-P [-Wp,options]Runs the Pv preprocessor before the compiler with the specified options.

For the complete list of compiler options please refer to the AIX XL FORTRAN Compiler/6000 User's Guide.

Example compiler commands
If your FORTRAN source file is stored in the example.f file, use the following compiler command to create the example executable file with the -o compiler flag. During the compilation the O3 level optimization is turned on.

xlf -o example -O3 example.f
The following compiler command will create a compiler listing and store it with the filename of example.lst. The listing file will include the FORTRAN source code, diagnostic and error messages, attribute listing, and the cross reference table for FORTRAN variables. The name of the resulting executable file is a.out.

xlf -qsource -qattr -qxref example.f

The FORTRAN preprocessor performs a number of tasks, including the optimization of expressions and loops, data dependence analysis, memory management, inlining of subprograms, and generation of optimized library calls. The following example shows you how to turn on the most frequently requested optimizations with the -ew preprocessor flag, generate a preprocessor listing with the -l option, and store the translated FORTRAN program in a file using the -o preprocessor flag.
xlf -P -Wp,-ew,-lpreout.lst,-opreout.f sample.f
Note that there are no spaces between the preprocessor options specified by the -W option.

Using the IMSL subroutine library
The IMSL MATH/LIBRARY and STAT/LIBRARY provides a large collection of FORTRAN subprograms to solve mathematical and statistical problems. The IMSL MATH/LIBRARY, IMSL STAT/LIBRARY and the IMSL MATH/LIBRARY Special Functions manuals contain descriptions of the subroutines. You can access the online information database by executing the following two commands:

cd /usr/local/doc/imsl.idf
The IMSL subroutines can be accessed by linking your FORTRAN program to the libimsl.a library with the -limsl compiler option. For example, the following compiler command will link the imsltest.f program to the IMSL library:
xlf imsltest.f -limsl
Using the OSL subroutine library
The Optimization Subroutine Library (OSL) is a collection of mathematical subroutines for solving Linear Programming optimization problems. The Optimization Subroutine Library, Guide and Reference contains detailed information on the syntax and semantics of OSL subroutines.

The following example compiler command will compile the exrow.f file and link it with the OSL. The library is specified with the -losl command line option. The -I compiler flag will include all needed header files in the specified directory location. The executable is stored as exrow.

xlf -I/usr/lpp/osl/ekkincf exrow.f -losl -oexrow
FORTRAN input/output devices
The XL FORTRAN compiler supports the following input/output file formats:

You must specify, in an OPEN statement, the record length (RECL) of the direct file. Each record in a direct access file has a record number. The record number is an integer value that must be specified with the REC specifier when the record is read or written.

FORTRAN programs refer to external files by the unit numbers specified in input/output statements. The association of a unit with an external file is called a connection. Connection can occur by preconnection (prior to running the program) or by an OPEN statement. The following two paragraphs explain preconnection in more detail. An example of using an OPEN statement for connecting unit 9 with the test.output file is as follows:

OPEN(9, FILE='test.output')
Units 0, 5, and 6 are preconnected to the AIX standard error, standard input, and standard output devices, respectively. By default, the standard input device is the keyboard and the standard output device is the screen. You can use AIX input/output redirection to change the standard I/O devices. For example, if you are using the C shell you can redirect the standard output of your program into a file in the following manner:
sample > output.file
All other unit numbers are preconnected to AIX files. The general rule is that unit n is preconnected to file fort.n. For example, unit 8 is preconnected to the fort.8 file. To change this default setting you can either specify your filename for that unit in an OPEN statement or you can create a symbolic link prior to executing the program, as shown below.
ln -s myfile fort.10
This command creates a symbolic link between the two filenames. Any subsequent WRITE to unit 10 will cause the output to be written to myfile.

Section 2: FORTRAN-related AIX Commands

The script command
The script command records everything displayed on your terminal during a terminal session and saves it into the file named typescript in your current AIX directory. You can start and stop capturing your terminal session by entering the following UNIX commands:

<terminal session>
To record your session into a particular file, specify a filename as an option of the script command. Use the -a option to append the typescript to the file:
script -a log1
Printing files and typescripts
The lpr command allows you to print FORTRAN source files, input and output files, and typescripts. For example, to print your typescript file on the default printer enter:
lpr typescript
The fsplit utility
The fsplit command takes as input a file containing FORTRAN source code and splits it into separate subprogram files of the form name.f, where name is the name of the program unit (for example, function, subroutine, block data, or program). If any of the files already exists, fsplit will generate a new filename. The original FORTRAN file is not effected by fsplit

. By splitting up one large FORTRAN file into multiple subprogram files you can greatly decrease the compilation time. Doing this can also make it easier to debug your code. The following example shows how to split up the example.f FORTRAN file:

fsplit example.f
Running Batch jobs
The most efficient way to run FORTRAN programs that use large amounts of CPU time is to submit them to the Distributed Queuing System (DQS). DQS is a batch system designed to distribute workload among workstations on the RS/6000 cluster. For more information on DQS refer to the DQS on the RS/6000 Cluster (FB 18) brief.

Section 3: Advanced Features

Dealing with run time errors: debugging
The dbx command provides an environment to debug and run FORTRAN programs. You can carry out operations such as examining core files resulting from erroneous program execution, controlled execution of a program, setting breakpoints at selected statements, and displaying and modifying symbolic variables.

Before you start the debugger, compile the source file using the -g compiler option. The following example shows how to prepare for and start the debugging environment: < blockquote> xlf -g -o example example.f
dbx example
Once you have entered the debugging environment you have access to the debugger commands. You can display your program using the list command, stop the execution at a specified line with the stop at line command, and run the program. The program execution will stop at the specified line and you can display the content of variables using the print variable dbx command. Resume the run by typing in the continue command. To exit the debugger enter quit.

Note that you can use the xde command if your display device is compatible with the X Window System. xde provides an easy-to-use graphical user interface to dbx.

Automating compilation with the make command
In the code development cycle you modify your FORTRAN code, then recompile, relink, run, and debug your program. The make command simplifies the recompiling and relinking of programs.

The make command is an especially useful tool for maintaining up-to-date versions of executable files when a number of FORTRAN source files are involved. If your FORTRAN subroutines are contained in one source file, use the fsplit command to create one file for each subroutine, as described above.

The make command executes the instructions defined by the Makefile in your current directory. The following example shows a simple Makefile instructing make to recompile the subroutine source files and relink the object modules to create the executable:

<Tab> xlf -c $<

example: example.o sub1.o sub2.o
<Tab> xlf -o example example.o sub1.o sub2.o
The first two lines contain instructions for the recompiling of all changed FORTRAN files in the current directory. The -c option forces the creation of a .o object file for all .f FORTRAN files. In this example the $< symbol substitutes for all FORTRAN files that have been modified after the last compilation. The last two lines link the resulting object files to create the executable.

After you have created the Makefile, you can compile, link, and execute your program by running make and the executable:

make example
Fine tuning your program
When you have finished developing your FORTRAN program you may wish to fine tune the code. Profiling refers to the process of measuring the execution performance of programs and subroutines. You can use this information to pinpoint CPU time-intensive sections of your program and to optimize the execution time of these sections. Frequently used optimization techniques include inlining, strength reduction, common subexpression elimination, strip mining, cache size tuning with stride minimization, and loop unrolling.

To run the prof profiler recompile your FORTRAN program with the -p option, and run the executable. During the run the profile monitor accumulates performance data in the mon.out file. You can read the profile data by entering the prof command, as in:

xlf -p example.f
prof | more
Building your own FORTRAN library You can archive a collection of subroutines into an indexed library using the ar AIX command. This is usually the last phase of the code development cycle. The members of the library are object modules generated by the FORTRAN compiler with the -c option turned on. You can reference the subroutines stored in the library using the -l and -L compiler options or by including the library name in the compilation command. The naming convention for archive libraries is libname.a, where name is the name of the library.

The following example shows how to generate object files with the -c compiler option and how to create an indexed library from these object files. You can reference the subroutines stored in this library by including the library name at the end of the compiler command.

xlf -c sub1.f sub2.f
ar q libtest.a sub1.o sub2.o
xlf -o example example.f libtest.a
Compiler directives
You can specify compiler options in the FORTRAN source file using the @PROCESS compiler directive. The options specified after the directive are typed in uppercase letters. Compiler directives override command line options and compiler default settings. In most cases, the @PROCESS directive starts in the first column of the first line of the FORTRAN file.

For example, you can turn on compiler optimization, source listing, and cross reference table generation if you include the following line at the top of your FORTRAN source file:

Dynamic and static linking
The AIX operating system provides facilities for both dynamic and static linking by the loader. Dynamic linking is the default and preferred method. When dynamically linking to shared libraries the size of the resulting executable file is much smaller than the statically linked executables.

Static linking does offer advantages, however, since all external references to AIX libraries are resolved by "bundling" the appropriate object segment into the executable file. This method can be useful when you are making environment independent, portable code. Static linking also has a slight performance edge over dynamic linking in most cases. The following example shows how to create statically linked executable files using two -b loader options:

xlf -bnso -bI:/lib/syscalls.exp sample.f

Section 4: Example

The following section provides a short FORTRAN program and example UNIX commands to compile and run the code. Use one of the available UNIX editors to create the FORTRAN file.

The test.f FORTRAN program listed below reads 10 integer numbers from the keyboard (unit 5), computes the sum, and displays it on the screen. The numbers and the sum are stored in the test.output file (unit 9).

program test
integer i, number, sum
open(9, file='test.output') ! open the output file
sum = 0 ! initialize sum to zero
print *, 'Enter 10 numbers:'
do 10 I=1,10 ! main loop to read and write>
read(5,*) number
write(9, *) number
sum = sum + number
10 continue
write (9, *) "sum = ", sum ! save and print the result
print *, "sum = ", sum
You can compile the program and run the a.out executable with the following AIX commands:
xlf test.f

Section 5: Special Features of Fortran

To compile and link:
% xlf90 file.f
What's new in FORTRAN 90 Refer to FORTRAN 90 language reference for more information.
SB78 version 2.0 - August 1997