Fortran Programming Guide | ![]() ![]() ![]() ![]() ![]() |
Porting
This chapter discusses the porting of programs from other dialects of Fortran to Sun compilers. VAX VMS Fortran programs compile almost exactly as is with Sun
f77
; this is discussed further in the chapter on VMS extensions in the FORTRAN 77 Language Reference Manual.
Note Porting issues bear mostly upon Fortran 77 programs. The Sun WorkShop Fortran 95 compiler,f95
, incorporates few nonstandard extensions, and these are described in the Fortran User's Guide.
Time and Date Functions
Library functions that return the time of day or elapsed CPU time vary from system to system.
The following time functions are not supported directly in the Sun Fortran libraries, but you can write subroutines to duplicate their functions:
- Time-of-day in
10h
format- Date in
A10
format- Milliseconds of job CPU time
- Julian date in ASCII
The time functions supported in the Sun Fortran library are listed in the following table:
For details, see Fortran Library Reference Manual or the individual man pages for these functions.
The routines listed in the following table provide compatibility with VMS Fortran system routines
idate
andtime
. To use these routines, you must include the-lV77
option on thef77
command line, in which case you also get these VMS versions instead of the standardf77
versions.
TABLE 7-2 Summary: Nonstandard VMS Fortran System Routines idate
Date as day, month, year call idate( d, m, y )
integer
time
Current time as hhmmss call time( t )
character*8
Note Thedate
(3F) routine and the VMS version ofidate
(3F) cannot be Year 2000 safe because they return 2-digit values for the year. Programs that compute time duration by subtracting dates returned by these routines will compute erroneous results after December 31, 1999. The Fortran 95 routinedate_and_time
(3F) is available for both FORTRAN 77 and Fortran 95 programs, and should be used instead. See the Fortran Library Reference Manual for details.
The error condition subroutine
errsns
is not provided, because it is totally specific to the VMS operating system.Here is a simple example of the use of these time functions (
TestTim.f
):
Running this program produces the following results:
demo%TimeTest
Hello, Time Now Is: Mon Feb 12 11:53:54 1996See how long 'sleep 4' takes, in secondsElapsed time for sleep 4 was: 5 secondsatan(q) 1000 times took: 2.26550E-03 secondsdemo%Formats
Some
f77
andf95
format edit descriptors can behave differently on other systems. Here are some format specifiers thatf77
treats differently than some other implementations:
A
- Alphanumeric conversion. Used with character type data elements. In FORTRAN 77, this specifier worked with any variable type.f77
supports the older usage, up to four characters to a word.$
- Suppresses newline character output.R
- Sets an arbitrary radix for theI
formats that follow in the descriptor.SU
- Selects unsigned output for followingI
formats. For example, you can convert output to either hexadecimal or octal with the following formats, instead of using theZ
orO
edit descriptors:
10 FORMAT( SU, 16R, I4 )20 FORMAT( SU, 8R, I4 )Carriage-Control
Fortran carriage-control grew out of the capabilities of the equipment used when Fortran was originally developed. For similar historical reasons, an operating system derived from the UNIX operating system, does not have Fortran carriage control, but you can simulate it in two ways.
- Use the
asa
filter to transform Fortran carriage-control conventions into the UNIX carriage-control format (see theasa
(1) man page) before printing files with thelpr
command.f77:
For simple jobs, useOPEN(N, FORM='PRINT')
to enable single or double spacing, formfeed, and stripping off of column one. It is legal to reopen unit 6 to change the form parameter to
OPEN( 6, FORM='PRINT')
- You can use
lp
(1) to print a file that is opened in this manner.Working With Files
Early Fortran systems did not use named files, but did provide a command line mechanism to equate actual file names with internal unit numbers. This facility can be emulated in a number of ways, including standard UNIX redirection.
Example: Redirecting
stdin
toredir.data
(usingcsh
(1)):
Porting From Scientific Mainframes
If the application code was originally developed for 64-bit (or 60-bit) mainframes such as CRAY or CDC, you might want to compile these codes with the following
options when porting to an UltraSPARC-II platform, for example:
-fast -xarch=v9a -xchip=ultra2 \ -xtypemap=real:64,double:64,integer:64
These options automatically promote all default
REAL
variables and constants toREAL*8
, andCOMPLEX
toCOMPLEX*16
. Only undeclared variables or variables declared as simplyREAL
orCOMPLEX
are promoted; variables declared explicitly (for example,REAL*4
) are not promoted. All single-precisionREAL
constants are also promoted toREAL*8
. (Set-xarch
and-xchip
appropriately for the target platform.) To also promote defaultDOUBLE
PRECISION
data toREAL*16
, change thedouble:64
todouble:128
in the-xtypemap
example.The
-xtypemap
option, is preferred over-dbl
and-r8
and-i2
. See the Fortran User's Guide and thef77
(1) orf95
(1) man pages for details.To further recreate the original mainframe environment, it is probably preferable to stop on overflows, division by zero, and invalid operations. Compile the main program with
-ftrap=common
to ensure this.Data Representation
The FORTRAN 77 Language Reference Manual, Fortran User's Guide, and the Sun Numerical Computation Guide discuss in detail the hardware representation of data objects in Fortran. Differences between data representations across systems and hardware platforms usually generate the most significant portability problems.
The following issues should be noted:
- Sun adheres to the IEEE Standard 754 for floating-point arithmetic. Therefore, the first four bytes in a
REAL*8
are not the same as in aREAL*4
.- The default sizes for reals, integers, and logicals are described in the FORTRAN 77 standard, except when these default sizes are changed by the
-xtypemap=
option (or by-i2,
-dbl,
or-r8).
- Character variables can be freely mixed and equivalenced to variables of other types, but be careful of potential alignment problems.
f77
IEEE floating-point arithmetic does raise exceptions on overflow or divide by zero but does not signalSIGFPE
or trap by default. It does deliver IEEE indeterminate forms in cases where exceptions would otherwise be signaled. This is explained in the Floating Point Arithmetic chapter of this Guide.- The extreme finite, normalized values can be determined. See
libm_single
(3F) andlibm_double
(3F). The indeterminate forms can be written and read, using formatted and list-directed I/O statements.Hollerith Data
Many "dusty-deck" Fortran applications store Hollerith ASCII data into numerical data objects. With the 1977 Fortran standard (and Fortran 95), the
CHARACTER
data type was provided for this purpose and its use is recommended. You can still initialize variables with the older Fortran Hollerith (nH
) feature, but this is not standard practice. The following table indicates the maximum number of characters that will fit into certain data types. (In this table, boldfaced data types indicate default types subject to promotion by thef77
command-line flags-dbl
,-r8
, or-xtypemap=
).
When storing standard ASCII characters with normal Fortran:
- With
-r8
, unspecified sizeINTEGER
andLOGICAL
do not hold double.- With
-dbl
, unspecified sizeINTEGER
andLOGICAL
do hold double.The storage is allocated with both options, but it is unavailable in normal Fortran with
-r8
.Options
-i2
,-r8
, and-dbl
are now considered obsolete; use-xtypemap
instead.Example: Initialize variables with Hollerith:
If you pass Hollerith constants as arguments, or if you use them in expressions or comparisons, they are interpreted as character-type expressions.
If needed, you can initialize a data item of a compatible type with a Hollerith and then pass it to other routines.
Example:
Nonstandard Coding Practices
As a general rule, porting an application program from one system and compiler to another can be made easier by eliminating any nonstandard coding. Optimizations or work-arounds that were successful on one system might only obscure and confuse compilers on other systems. In particular, optimized hand-tuning for one particular architecture can cause degradations in performance elsewhere. This is discussed later in the chapters on performance and tuning. However, the following issues are worth considering with regards to porting in general.
Uninitialized Variables
Some systems automatically initialize local and COMMON variables to zero or some "not-a-number" (NaN) value. However, there is no standard practice, and programs should not make assumptions regarding the initial value of any variable. To assure maximum portability, a program should initialize all variables.
Aliasing Across Calls
Aliasing occurs when the same storage address is referenced by more than one name. This happens when actual arguments to a subprogram overlap between themselves or between COMMON variables within the subprogram. For example, arguments X and Z refer to the same storage locations, as do B and H:
COMMON /INS/B(100)REAL S(100), T(100)...CALL SUB(S,T,S,B,100)...SUBROUTINE SUB(X,Y,Z,H,N)REAL X(N),Y(N),Z(N),H(N)COMMON /INS/B(100)...Avoid aliasing in this manner in all portable code. The results on some systems and with higher optimization levels could be unpredictable.
Obscure Optimizations
Legacy codes may contain source-code restructurings of ordinary computational DO loops intended to cause older vectorizing compilers to generate optimal code for a particular architecture. In most cases, these restructurings are no longer needed and may degrade the portability of a program. Two common restructurings are strip-mining and loop unrolling.
Strip-Mining
Fixed-length vector registers on some architectures led programmers to manually "strip-mine" the array computations in a loop into segments:
REAL TX(0:63)...DO IOUTER = 1,NX,64DO IINNER = 0,63TX(IINNER) = AX(IOUTER+IINNER) * BX(IOUTER+IINNER)/2.QX(IOUTER+IINNER) = TX(IINNER)**2END DOEND DOStrip-mining is no longer appropriate with modern compilers; the loop can be written much less obscurely as:
DO IX = 1,NTX = AX(I)*BX(I)/2.QX(I) = TX**2END DOLoop Unrolling
Unrolling loops by hand was a typical source-code optimization technique before compilers were available that could perform this restructuring automatically. A loop written as:
should be rewritten the way it was originally intended:
DO K = 1,NDO J = 1,NDO I = 1,NA(I,J) = A(I,J) + B(I,K) * C(K,J)END DOEND DOEND DOTroubleshooting
Here are a few suggestions for what to try when programs ported to Sun Fortran do not run as expected.
Results Are Close, but Not Close Enough
- Pay attention to the size and the engineering units. Numbers very close to zero can appear to be different, but the difference is not significant, especially if this number is the difference between two large numbers, such as the distance across the continent in feet, as calculated on two different computers. For example, 1.9999999e-30 is very near -9.9992112e-33, even though they differ in sign.
- VAX math is not as good as IEEE math, and even different IEEE processors may differ. This is especially true if the mathematics involves many trigonometric functions. These functions are much more complicated than one might think, and the standard defines only the basic arithmetic functions. There can be subtle differences, even between IEEE machines. Review the Floating-Point Arithmetic chapter in this Guide.
- Try running with a
call nonstandard_arithmetic()
. Doing so can also improve performance considerably, and make your Sun workstation behave more like a VAX system. If you have access to a VAX or some other system, run it there also. It is quite common for many numerical applications to produce slightly different results on each floating-point implementation.- Check for NaN, +Inf, and other signs of probable errors. See the Floating-Point Arithmetic chapter in this Guide, or the man page
ieee_handler
(3m) for instructions on how to trap the various exceptions. On most machines, these exceptions simply abort the run.- Two numbers can differ by 6 x 1029 and still have the same floating-point form. Here is an example of different numbers, with the same representation:
real*4 x,yx=99999990e+29y=99999996e+29write (*,10) x, x10 format('99,999,990 x 10^29 = ', e14.8, ' = ', z8)write(*,20) y, y20 format('99,999,996 x 10^29 = ', e14.8, ' = ', z8)end
- The output is:
99,999,990 x 10^29 = 0.99999993E+37 = 7CF0BDC199,999,996 x 10^29 = 0.99999993E+37 = 7CF0BDC1- In this example, the difference is 6 x 1029. The reason for this indistinguishable, wide gap is that in IEEE single-precision arithmetic, you are guaranteed only six decimal digits for any one decimal-to-binary conversion. You may be able to convert seven or eight digits correctly, but it depends on the number.
Program Fails Without Warning
If the program fails without warning and runs different lengths of time between failures, then:
- Compile with minimal optimization (
-O1
). If the program then works, compile only selective routines with higher optimization levels.- Understand that optimizers must make assumptions about the program. Nonstandard coding or constructs can cause problems. Almost no optimizer handles all programs at all levels of optimization.
Sun Microsystems, Inc. Copyright information. All rights reserved. Feedback |
Library | Contents | Previous | Next | Index |