Using Sun Fortran Compilers |
2 |
![]() |
The principal use of any compiler is to transform a program written in a procedural language like Fortran into a data file that is executable by the target computer hardware. As part of its job, the compiler may also automatically invoke a system linker to generate the executable file.
The Sun Fortran 77 and Fortran 90 compilers can also be used to:
-pg)
The very basic steps to running a Fortran application involve using an editor to create a Fortran source file with a .f, .for, .f90, .F, or .F90 filename suffix; invoking the compiler to produce an executable; and finally, launching the program into execution by typing the name of the file:
Note - The command line examples in this chapter show f77 usages. Except where noted, equivalent usages of f90 are similarly valid; however, the printed output may be slightly different.
Example: This program displays a message on the screen:
demo% cat greetings.f PROGRAM GREETINGS PRINT *, 'Real programmers write Fortran!' END demo% f77 greetings.f greetings.f: MAIN greetings: demo% a.out Real programmers write Fortran! demo% |
In the example, f77 compiles source file greetings.f and compiles the executable program onto the file, a.out, by default. To launch our program, the name of the executable file, a.out, is typed at the command prompt.
demo% f77 -o greetings greetings.f greetings.f: MAIN greetings: demo% |
In the preceeding example, to -o option tells the compiler to write the executable code to the file greetings. (By convention, executable files usually are given the same name as the main source file, but without an extension.)
demo% greetings Real programmers write Fortran! demo% |
The next sections of this chapter discuss the conventions used by the f77 and f90 commands, compiler source line directives, and other issues concerning the use of these compilers. The next chapter describes the command-line syntax and all the options in detail.
Invoking the Compiler
The syntax of a simple compiler command is as follows:
f77 [options] sfn ... invokes the Fortran 77 compiler |
f90 [options] sfn ... invokes the Fortran 90 compiler |
Here sfn is a Fortran source file name that ends in .f, .F, .f90, .F90, or .for; options is one or more of the compiler options. (Files with names ending in a .f90 or .F90 extension are "free-format" Fortran 90 source files recognized only by the f90 compiler.)
demo% f90 -g -o growth growth.f fft.f90 |
Compile-Link Sequence
In the previous example, the compiler will automatically generate the loader object files, growth.o and fft.o, and then invoke the system linker to create the executable program on the file growth. Command-Line File Name Conventions
The suffix extension attached to file names appearing on the command-line determine how the compiler will process the file. File names with a suffix extension other than one of those listed below, or without an extension, are passed to the linker.
Fortran 90 free-format is described in Appendix C of this manual.
Source Files
The Fortran compilers will accept multiple source files on the command line. A set of source files compiled together by a single compiler command are often referred to as a compilation unit. A single source file may contain any number of procedures (main program, subroutine, function, block data, module, and so on). There are advantages for organizing an application with one procedure per file, as there are for gathering procedures that work together into a single file. Some of these are described in the Sun Fortran Programmer's Guide. Source File Preprocessors
Both f77 and f90 support two source file preprocessors, fpp and cpp. Either can be invoked by the compiler to expand source code "macros" and symbolic definitions prior to compilation. The compilers will use fpp by default; the -xpp=cpp option changes the default from fpp to cpp. (See also the discussion of the -Dname option). Separate Compiling and Linking
You can compile and link in separate steps. The -c option compiles source files and generates .o object files, but does not create an executable. Without the -c option the compiler will invoke the linker. By splitting the compile and link steps in this manner, a complete recompilation is not needed just to fix one file, as shown in the following example:
demo% f77 -c file1.f (Make new object file) demo% f77 -o prgrm file1.o file2.o file3.o (Make executable file) |
Be sure that the link step lists all the object files needed to make the complete program. If any object files are missing from this step, the link will fail with undefined external reference errors (missing routines).
Consistent Compiling and Linking
If you do compile and link in separate steps, consistent compiling and linking is critical when using certain compiler options:
-dbl_align_all, -explicitpar, -f, -fast, -misalign, -p, -parallel, -pg, -r8, -xarch=a, -xcache=c, -xchip=c, xprofile=p,
demo% f77 -c -dbl sbr.f demo% f77 -c smain.f demo% f77 -dbl sbr.o smain.o {pass -dbl to the linker}
|
If you compile any subprogram with any of these options, be sure to link with the same options as well.
Linking Mixed Fortran 90 and Fortran 77 Compilations
As a general rule, if any of the object files that make up a program were compiled with f90, then the final link step must be done with f90. Use f77 to produce the executable file only if none of the .o object files were compiled with f90. Unrecognized Command-Line Arguments
Any arguments on the command-line that the compiler does not recognize are interpreted as being possibly linker options, object program file names, or library names.
Note that in the first example,
-bit is not recognized by f77 and the option is passed on to the linker (ld), who tries to interpret it. Because single letter ld options may be strung together, the linker sees -bit as -b -i -t, which are all legitimate ld options! This may (or may not) be what the user expects, or intended. -fast but neglected the leading dash. The compiler again passes the argument to the linker which, in turn, interprets it as a file name. Modules (Fortran 90)
f90 automatically creates module files for each MODULE declaration encountered in the source files, and searches for modules referenced by a USE statement. All the modules appearing in a source file are compiled into a single file with the primary name of the source file and .M suffix. For example, the modules on mysrc.f90 would be compiled to mysrc.M by f90. USE statements. More directories can be added to the search path with the f90 -M command-line option. However, .M files cannot be specified directly on the command line.
Directives
Use a source code directive, a form of Fortran comment, to pass specific information to the compiler regarding special optimization or parallelization choices. Compiler directives are also called pragmas.
Note - Directives are not part of any Fortran standard.
General Directives (f77)
The various forms of an f77 general directive are:
C$PRAGMA keyword C$PRAGMA keyword ( a [ , a ] ... ) [ , keyword ( a [ , a ] ... ) ] ,... C$PRAGMA SUN keyword |
The variable keyword identifies the specific directive; the a's are arguments.
C(...) -- The listed arguments are external functions written in C.
UNROLL=n -- Requests the optimizer to attempt loop unrolling to depth n.
WEAK (name1[=name2]) -- Define weak symbol bindings.
The C() directive for a particular function should appear before the first reference to that function in each subprogram that contains such a reference.
Example - compiling ABC and XYZ for C:
EXTERNAL ABC, XYZ C$PRAGMA C(ABC, XYZ) |
The UNROLL Directive (
The UNROLL directive requires that you specify SUN after C$PRAGMA.f77)
Example - unrolling loops two times:
C$PRAGMA SUN UNROLL=2 |
The
The WEAK Directive (f77)
WEAK directive defines a symbol to have less precedence than an earlier definition of the same symbol. This pragma is used mainly in sources files for building libraries. The linker does not produce an error message if it is unable to resolve a weak symbol.
C$PRAGMA WEAK (name1 [=name2]) |
WEAK (name1) defines name1 to be a weak symbol. The linker does not produce an error message if it does not find a definition for name1.WEAK (name1=name2) defines name1 to be a weak symbol and an alias for name2. Parallelization Directives (f77)
Parallelization directives explicitly request the compiler attempt to parallelize the DO loop that follows the directive. The syntax differs from general directives. Parallelization directives are only recognized when compilation options -parallel or -explicitpar are used. (f90 parallelization directives are described in Appendix C; detailed information on Fortran parallelization can be found in the Fortran Programmer's Guide.)
$PAR, no blanks, either upper or lower case.
DOALL, DOSERIAL, and DOSERIAL*
Example: Specifying a loop with a shared variable:
C$PAR DOALL SHARED(yvalue) |
See the Fortran Programmer's Guide for details about parallelization.
Compiler Usage Notes
The next sections suggest a number of ways to use the Sun Fortran compilers efficiently. A complete compiler options reference follows in the next chapter. Determining Floating-Point Hardware
Some compiler options are specific to particular hardware options. The utility command fpversion tells which floating-point hardware is installed:
It may take a number of seconds before fpversion responds while it dynamically calculates apparent hardware clock rates of the CPU and FPU. (The values printed depend on the load on the system at the moment fpversion is called.)
Simplifying Options
You can simplify complicated compiler commands by defining special shell aliases or using the $FFLAGS environment variable. Using Aliases (C Shell)
Example: Define an alias for a command with frequently used options:
demo% alias f77fx "f77 -silent -fast -Xlist" |
Example: Using the alias f77fx:
demo% f77fx any.f |
The command f77fx is now the same as:
Either FFLAGS or OPTIONS can be used explicitly in the command line. When you are using make files implicit compilation rules, FFLAGS is used automatically by the make program.
Example: Set FFLAGS: (C Shell)
demo% setenv FFLAGS '-silent -fast -Xlist' |
demo% f77 $FFLAGS any.f |
A workstation should have at least 24 megabytes of memory; 32 megabytes are recommended. Memory usage depends on the size of each procedure, the level of optimization, the limits set for virtual memory, the size of the disk swap file, and various other parameters.
Compiling a single source file containing many routines could cause the compiler to run out of memory or swap space.
If the compiler runs out of memory, try reducing the level of optimization, or split multiple-routine source files into files with one routine per file, using fsplit(1).
Example: Use the swap command:
demo% swap -s total: 40236k bytes allocated + 7280k reserved = 47516k used, 1058708k available |
To determine the actual real memory, use the following command:
demo% /usr/sbin/dmesg | grep mem mem = 655360K (0x28000000) avail mem = 602476544 |
Increasing Swap Space
Use mkfile(1M) and swap (1M) to increase the size of the swap space on a workstation. You must become superuser to do this. mkfile creates a file of a specific size, and swap -a adds the file to the system swap space:
demo# mkfile -v 90m /home/swapfile /home/swapfile 94317840 bytes demo# /usr/sbin/swap -a /home/swapfile |
Control of Virtual Memory
Compiling very large routines (thousands of lines of code in a single procedure) at -O3 or higher, may require an unreasonable amount of memory. In such cases, performance of the system may degrade. You can control this by limiting the amount of virtual memory available to a single process.
demo$ ulimit -d 16000 |
demo% limit datasize 16M |
This limit cannot be greater than the system's total available swap space and, in practice, must be small enough to permit normal use of the system while a large compilation is in progress.
Be sure that no compilation consumes more than half the space.
Example: With 32 Mbytes of swap space, use the following commands:
demo$ ulimit -d 1600 |
demo% limit datasize 16M |