C H A P T E R 4 |
Fortran 95 Features and Differences |
This appendix shows some of the major features differences between standard Fortran 95 and the Fortran 95 compiler, f95.
The Fortran 95 compiler provides the following source langauge features and extensions to the Fortran 95 standard.
f95 allows 99 continuation lines (1 initial and 99 continuation lines). Standard Fortran 95 allows 19 for fixed-form and 39 for free-form.
In fixed-form source, lines can be longer than 72 characters, but everything beyond column 73 is ignored. Standard Fortran 95 only allows 72-character lines.
Tabs in f95 force the rest of the line to be padded out to column 72. This may cause unexpected results if the tab appears within a character string that is continued onto the next line:
^Iprint *, "Tab on next line ^I1this continuation line starts with a tab." ^Iend Tab on next line this continuation line starts with a tab. |
The source form assumed by f95 depends on options, directives, and suffixes.
Files with a .f or .F suffix are assumed to be in fixed format. Files with a .f90, .f95, .F90, or .F95 suffix are assumed to be in free format.
If the -free or -fixed option is used, it overrides the file name suffix. If either a !DIR$ FREE or !DIR$ FIXED directive is used, it overrides the option and file name suffix.
Some mixing of source forms is allowed.
Sun Fortran 95 is case insensitive by default. That means that a variable AbcDeF is treated as if it were spelled abcdef. Compile with the -U option to have the compiler treat upper and lower case as unique.
This section describes features and extensions to the Fortran 95 data types.
f95 supports constants and expressions of Boolean type. However, there are no Boolean variables or arrays, and there is no Boolean type statement.
f95 allows a Boolean constant (octal, hexadecimal, or Hollerith) in the following alternate forms (no binary). Variables cannot be declared Boolean. Standard Fortran does not allow these forms.
ddddddB, where d is any octal digit
Within an I/O format specification, the letter B indicates binary digits; elsewhere it indicates octal digits.
X'ddd' or X"ddd", where d is any hexadecimal digit
Accepted forms for Hollerith data are:
Above, "..." is a string of characters and n is the character count.
Examples: Octal and hexadecimal constants.
Examples: Octal and hexadecimal in assignment statements.
Use of an octal or hexadecimal constant in an arithmetic expression can produce undefined results and do not generate syntax errors.
f95 allows BOZ constants in the places other than DATA statements.
If these are assigned to a real variable, no type conversion occurs.
Standard Fortran allows these only in DATA statements.
f95 allows the following nonstandard type declaration forms in declaration statements, function statements, and IMPLICIT statements. The form in column one is nonstandard Fortran 95, though in common use. The kind numbers in column two can vary by vendor.
Storage and alignment are always given in bytes. Values that can fit into a single byte are byte-aligned.
The size and alignment of types depends on various compiler options and platforms, and how variables are declared. The default maximum alignment in COMMON blocks is to 4-byte boundaries.
Default data alignment and storage allocation can be changed by compiling with special options, such as -aligncommon, -f, -dalign, -dbl_align_all, -xmemalign,, and -xtypemap. The default descriptions in this manual assume that these options are not in force.
There is additional information in Chapter 11 of the Fortran Programming Guide regarding special cases of data types and alignment on certain platforms.
The following table summarizes the default size and alignment, ignoring other aspects of types and options.
Options -f or -dalign force alignment of all 8, 16, or 32-byte data onto 8-byte boundaries. Option -dbl_align_all causes all data to be aligned on 8-byte boundaries. Programs that depend on the use of these options may not be portable.
A Cray pointer is a variable whose value is the address of another entity, called the pointee.
f95 supports Cray pointers; Standard Fortran 95 does not.
The Cray POINTER statement has the following format:
Where pointer_name, pointee_name, and array_spec are as follows:
Example: Declare Cray pointers to two pointees.
The above example declares Cray pointer p and its pointee b, and Cray pointer q and its pointee c.
Example: Declare a Cray pointer to an array.
The above example declares Cray pointer ix and its pointee x; and declares x to be an array of dimensions n by m+1.
You can use pointers to access user-managed storage by dynamically associating variables to particular locations in a block of storage.
Cray pointers allow accessing absolute memory locations.
Cray pointers are declared as follows:
POINTER ( pointer_name, pointee_name [array_spec] )
Fortran 95 pointers are declared as follows:
The two kinds of pointers cannot be mixed.
Its array declarator can appear in:
Cray pointers can be assigned values as follows:
Example: Use Cray pointers as described above.
Remarks about the above example:
To aid the migration of programs from f77, f95 accepts VAX Fortran STRUCTURE and UNION statements, a precursor to the "derived types" in Fortran 95. For syntax details see the FORTRAN 77 Language Reference manual.
The field declarations within a STRUCTURE can be one of the following:
As with f77, a POINTER statement cannot be used as a field declaration.
The Fortran 95 compiler accepts a new data type, UNSIGNED, as an extension to the language. Four KIND parameter values are accepted with UNSIGNED: 1, 2, 4, and 8, corresponding to 1-, 2-, 4-, and 8-byte unsigned integers, respectively.
The form of an unsigned integer constant is a digit-string followed by the upper or lower case letter U, optionally followed by an underscore and kind parameter. The following examples show the maximum values for unsigned integer constants:
Expressed without a kind parameter (12345U), the default is the same as for default integer. This is U_4 but can be changed by the -xtypemap option, which will change the kind type for default unsigned integers.
Declare an unsigned integer variable or array with the UNSIGNED type specifier:
Signed and unsigned integer operands may be compared using intrinsic relational operations. The result is based on the unaltered value of the operands.
A number of features proposed in the Fortran 2003 draft standard appear in this release of the f95 compiler.
The new draft standard for Fortran provides:
The ISO_C_BINDING module provides access to named constants that are kind type parameters representing data that is compatible with C types.
The draft standard also introduces the BIND(C) attribute. A Fortran derived type is interoperable with C if it has the BIND attribute.
This release of the Fortran 95 compiler implements these features as described in the chapter 15 of the draft standard. Fortran also provides facilities for defining derived types, enumerations, and type aliases that correspond to C types, as described in chapter 4 of the draft standard.
New intrinsic modules IEEE_ARITHMETIC, and IEEE_FEATURES provide support for exceptions and IEEE arithmetic in the Fortran language. Full support of these features is provided by:
USE, INTRINSIC :: IEEE_ARITHMETIC
USE, INTRINSIC :: IEEE_FEATURES
These modules define a set of derived types, constants, rounding modes, inquiry functions, elemental functions, kind functions, and elemental and non-elemental subroutines. The details are contained in Chapter 14 of the draft standard for Fortran 2000.
The Fortran 2003 draft standard introduces three new intrinsics for processing command-line arguments and environment variables. These are:
Returns in command the entire command line that invoked the program.
Returns a command-line argument in value.
Return the value of an environment variable.
The Fortran 95 compiler now accepts the Fortran 2003 PROTECTED attribute. PROTECTED imposes limitations on the usage of module entities. Objects with the PROTECTED attribute are only definable within the module that declares them.
The compiler recognizes the ASYNCHRONOUS specifier on I/O statements:
This syntax is as proposed in the Fortran 2000 draft standard, Chapter 9. In combination with the WAIT statement it allows the programmer to specify I/O processes that may be overlapped with computation. While the compiler recognizes ASYNCHRONOUS='YES', the draft standard does not require actual asynchronous I/O. In this release of the compiler, I/O is always synchronous.
Recent decisions by the Fortran 95 standards organizations have extended the data entities allowed for the ALLOCATABLE attribute. Previously this attribute was limited to locally stored array variables. It is now allowed with:
Allocatable entities remain forbidden in all places where they may be storage-associated: COMMON blocks and EQUIVALENCE statements. Allocatable array components may appear in SEQUENCE types, but objects of such types are then prohibited from COMMON and EQUIVALENCE.
The f95 compiler recognizes the VALUE type declaration attribute. This attribute has been proposed for the Fortran 2003 standard.
Specifying a subprogram dummy input argument with this attribute indicates that the actual argument is passed "by value". The following example demonstrates the use of the VALUE attribute with a C main program calling a Fortran 95 subprogram with a literal value as an argument:
C code: #include <stdlib.h> int main(int ac, char *av[]) { to_fortran(2); } Fortran code: subroutine to_fortran(i) integer, value :: i print *, i end |
A new "stream" I/O scheme has been proposed as part of the Fortran 2003 draft standard. Stream I/O access treats a data file as a continuous sequence of bytes, addressable by a positive integer starting from 1. Declare a stream I/O file with the ACCESS='STREAM' specifier on the OPEN statement. File positioning to a byte address requires a POS=scalar_integer_expression specifier on a READ or WRITE statement. The INQUIRE statement accepts ACCESS='STREAM', a specifier STREAM=scalar_character_variable, and POS=scalar_integer_variable.
Three new Fortran 2003 formatted I/O specifiers have been implemented in f95. They may appear on OPEN, READ, WRITE, PRINT, and INQUIRE statements:
Change the default decimal editing mode. The default uses a period to separate the whole number and decimal parts of a floating-point number formatted with D, E, EN, ES, F, and G editing. 'COMMA' changes the default to use comma instead of a period, to print, for example, 123,456. The default is 'POINT', which uses a period, to print, for example, 123.456.
Set the default rounding mode for formatted I/O D, E, EN, ES, F, and G editing. With 'COMPATIBLE', the value resulting from data conversion is the one closer to the two nearest represetnations, or the value away from zero if the value is halfway between them. With 'PROCESSOR_DEFINED', the rounding mode is dependent on the processor's default mode, and is the compiler default if ROUND is not specified.
As an example, WRITE(*,'(f11.4)') 0.11115 prints 0.1111 in default mode, and 0.1112 in 'COMPATIBLE' mode.
Returns an error message as a string in the specified character variable. This is the same error message that would appear on standard output. Users should allocated a character buffer large enough to hold the longest message. (CHARACTER*256 should be sufficient.)
When used in INQUIRE statements, these specifiers declare a character variable for returning the current values.
New edit descriptors DP, DC, RP, and RC change the defaults within a single FORMAT statement to decimal point, decimal comma, processor-defined rounding, and compatible rounding respectively. For example:
prints a comma instead of a period in the F10.3 output item.
See also the -iorounding compiler command-line option for changing the floating-point rounding modes on formatted I/O. (Section , -iorounding[={compatible|processor-defined}].)
The section describes extensions to Fortran 95 Input/Output handling that are accepted by the f95 compiler that are not part of the Fortran 2003 draft standard. Some are I/O extensions that appeared in the Fortran 77 compiler, f77, and are now part of the Fortran 95 compiler.
Two new functions enable the user to specify their own error handling routine for formatted input on a logical unit. When a formatting error is detected, the runtime I/O library calls the specified user-supplied handler routine with data pointing at the character in the input line causing the error. The handler routine can supply a new character and allow the I/O operation to continue at the point where the error was detected using the new character; or take the default Fortran error handling.
The new routines, SET_IO_ERR_HANDLER(3f) and GET_IO_ERR_HANDLER(3f), are module subroutines and require USE SUN_IO_HANDLERS in the routine that calls them. See the man pages for these routines for details.
Fortran 77 allowed any integer constant in a format to be replaced by an arbitrary expression enclosed in angle brackets:
Variable format expressions cannot appear as the n in an nH... edit descriptor, in a FORMAT statement that is referenced by an ASSIGN statement, or in a FORMAT statement within a parallel region.
This feature is enabled natively in f95, and does not require the -f77 compatibility option flag.
Opening a file with FORM='BINARY' has roughly the same effect as FORM='UNFORMATTED', except that no record lengths are embedded in the file. Without this data, there is no way to tell where one record begins, or ends. Thus, it is impossible to BACKSPACE a FORM='BINARY' file, because there is no way of telling where to backspace to. A READ on a 'BINARY' file will read as much data as needed to fill the variables on the input list.
FORM="BINARY"
ACCESS="SEQUENTIAL"
DIRECT="NO"
FORMATTED="NO"
UNFORMATTED="YES"
RECL= AND NEXTREC= are undefined
write(*,'(a)',ADVANCE='NO') 'n= '
read(*,*) n
A compiler directive directs the compiler to do some special action. Directives are also called pragmas.
A compiler directive is inserted into the source program as one or more lines of text. Each line looks like a comment, but has additional characters that identify it as more than a comment for this compiler. For most other compilers, it is treated as a comment, so there is some code portability.
Sun-style parallelization directives are the default with f95 -explicitpar. To switch to Cray-style directives, use the -mp=cray compiler command-line flag. Explicit parallelization with OpenMP directives requires compiling with -openmp.
A complete summary of Fortran directives appears in Appendix D.
f95 recognizes its own special directives in addition to those described in Chapter 2. These have the following syntax:
The !DIR$ characters are the first nonblank characters in the line (actually, non-whitespace).
Thus, !DIR$ in columns 1 through 5 works for both free-form source and fixed-form source.
These directives specify the source form of lines following the directive line.
They apply to the rest of the file in which they appear, or until the next FREE or FIXED directive is encountered.
A parallelization directive is a special comment that directs the compiler to attempt to parallelize the next DO loop. These are summarized in Appendix D and described in the chapter on parallelization in the Fortran Programming Guide. f95 recognizes both Sun and Cray style parallelization directives, as well as the OpenMP Fortran API directives. OpenMP parallelization is described in the OpenMP API User's Guide.
Compiling a file containing a Fortran 95 MODULE generates a module interface file (.mod file) for every MODULE encountered in the source. The file name is derived from the name of the MODULE; file xyz.mod (all lowercase) will be created for MODULE xyz.
Compilation also generates a .o module implementation object file for the source file containing the MODULE statements. Link with the module implementation object file along with the all other object files to create an executable.
The compiler creates module interface files and implementation object files in the directory specified by the -moddir=dir flag or the MODDIR evironment variable. If not specified, the compiler writes .mod files in the current working directory.
The compiler looks in the current working directory for the interface files when compiling USE modulename statements. The -Mpath option allows you to give the compiler an additional path to search. Module implementation object files must be listed explicitly on the command line for the link step.
Typically, programmers define one MODULE per file and assign the same name to the MODULE and the source file containing it. However, this is not a requirement.
In this example, all the files are compiled at once. The module source files appear first before their use in the main program.
demo% cat mod_one.f90 MODULE one ... END MODULE demo% cat mod_two.f90 MODULE two ... END MODULE demo% cat main.f90 USE one USE two ... END demo% f95 -o main mod_one.f90 mod_two.f90 main.f90 |
Compilation creates the files:
main
main.o
one.mod
mod_one.o
two.mod
mod_two.o
The next example compiles each unit separately and links them together.
demo% f95 -c mod_one.f90 mod_two.f90 demo% f95 -c main.f90 demo% f95 -o main main.o mod_one.o mod_two.o |
When compiling main.f90, the compiler searches the current directory for one.mod and two.mod. These must be compiled before compiling any files that reference the modules on USE statements. The link step requires the module implementation object files mod_one.o and mod_two.o appear along with all other object files to create the executable.
With the release of the Sun ONE Studio 7 Fortran 95 compiler, .mod files can be stored into an archive (.a) file. An archive must be explicitly specified in a -Mpath flag on the command line for it to be searched for modules. The compiler does not search archive files by default.
Only .mod files with the same names that appear on USE statements will be searched. For example, the Fortran 95 statement USE mymod causes the compiler to search for the module file mymod.mod by default.
While searching, the compiler gives higher priority to the directory where the module files are being written. This can be controlled by the -moddir=dir option flag and the MODDIR environment variable. This implies that if only the -Mpath option is specified, the current directory will be searched for modules first, before the directories and files listed on the -M flag.
The -use=list flag forces one or more implicit USE statements into each subprogram or module subprogram compiled with this flag. By using the flag, it is not necessary to modify source programs when a module or module file is required for some feature of a library or application.
Compiling with -use=module_name has the effect of adding a USE module_name to each subprogram or module being compiled. Compiling with -use=module_file_name has the effect of adding a USE module_name for each of the modules contained in the module_file_name file.
Use the fdumpmod(1) command to display information about the contents of a compiled module information file.
demo% fdumpmod x.mod group.mod x 1.0 v8,i4,r4,d8,n16,a4 x.mod group 1.0 v8,i4,r4,d8,n16,a4 group.mod |
The fdumpmod command will display information about modules in a single .mod file, files formed by concatenating .mod files, and in .a archives of .mod files. The display includes the name of the module, a version number, the target architecture, and flags indicating compilation options with which the module is compatible. See the fdumpmod(1) man page for details.
f95 supports some intrinsic procedures that are extensions beyond the standard.
See the Fortran Library Reference for a more complete discussion of intrinsics, including those from Fortran 77 that are recognized by the Fortran 95 compiler.
Future releases of f95 are intended to be source code compatible with this release.
Module information files generated by this release of f95 are not guaranteed to be compatible with future releases.
On Solaris systems, routines written in C can be combined with Fortran programs, since these languages have common calling conventions.See the C-Fortran Interface chapter in the Fortran Programming Guide for details on how to interoperate between C and Fortran routines.
Copyright © 2004, Sun Microsystems, Inc. All rights reserved.