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.
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 extension||Example filename|
|FORTRAN source files||.f||test.f|
|Default executable filename||n/a||a.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.fRunning the program
|-o name||Names the resulting executable name instead of a.out.|
|-O||Optimizes code generated by the compiler.|
|-O3||Performs the -O level and other optimizations.|
|-c||Compiles without calling the loader, and creates .o object files.|
|-llibrary||Searches the specified library to resolve external references.|
|-Ldirectory||Searches the directory for the library specified by the -llibrary option.|
|-Idirectory||Searches the directory for include files.|
|-g||Generates debugging information.|
|-p||Generates profiling support code.|
|-v||Displays verbose information on the progress of the compiler.|
|-qfips||Turns on full ANSI FORTRAN compatibility mode.|
|-qF77||Turns on FORTRAN 77 compatibility mode.|
|-qsource||Produces source listing in the compiler listing file.|
|-qattr||Produces attribute listing in the compiler listing file.|
|-qxref||Produces cross reference listing in the compiler listing file.|
|-qdpc||Promotes single-precision constants to double precision.|
|-qautodbl=dblpad||Promotes single- and double-precision values to double and extended precision, respectively.|
|-qrecur||Allows recursive subroutine calls.|
|-qfree||Allows 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.fThe 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.fThe 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.fNote 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.idfThe 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 -limslUsing the OSL subroutine library
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 -oexrowFORTRAN input/output devices
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.fileAll 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.10This command creates a symbolic link between the two filenames. Any subsequent WRITE to unit 10 will cause the output to be written to myfile.
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:
scriptTo 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 log1Printing files and typescripts
lpr typescriptThe fsplit utility
. 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.fRunning Batch jobs
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:
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.oThe 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.
<Tab> xlf -o example example.o sub1.o sub2.o
After you have created the Makefile, you can compile, link, and execute your program by running make and the executable:
make exampleFine tuning your program
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.fBuilding 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.
prof | more
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.fCompiler directives
ar q libtest.a sub1.o sub2.o
xlf -o example example.f libtest.a
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:
@PROCESS OPT, SOURCE, XREFDynamic and static linking
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
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 testYou can compile the program and run the a.out executable with the following AIX commands:
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,*) number10 continue
write(9, *) number
sum = sum + number
write (9, *) "sum = ", sum ! save and print the result
print *, "sum = ", sum
What's new in FORTRAN 90 Refer to FORTRAN 90 language reference for more information.