What's New in Fortran 2008 by hcw25539


									             What’s New in Fortran 2008
                         Van Snyder
 Jet Propulsion Laboratory, California Institute of Technology

                                July 23, 2009
A “What’s new in Fortran since the Fortran 77 your professor told you is
 obsolete” seminar would be much longer – Fortran 2008 is the fourth
                       revision after Fortran 77

See ftp://ftp.nag.co.uk/sc22wg5/N1701-N1750/N1735.pdf for
                  a more complete discussion
   c 2009 California Institute of Technology. Government Sponsorship Acknowledged
                 Fortran Committees
Fortran development is divided between two committees.
A working group of a subcommittee of a joint ISO/IEC technical
committee, officially designated ISO/IEC JTC1/SC22/WG5, is
responsible for setting the requirements for Fortran, monitoring
progress toward implementing those requirements in a revision of
the standard, and maintaining the current standard (publishing
WG5 has appointed ANSI to perform the development work.
ANSI has in turn contracted with a subcommittee of the In-
ternational Committee for Information Technology Standards,
officially designated ANSI/INCITS/PL22.3, to carry out the de-
velopment work.
(The former contractor was the Computer and Business Equip-
ment Manufacturers Association; the previous committee’s des-
ignation was ANSI/CBEMA/X3J3.)
                                       WVS July 23, 2009 – Page 1 of 23
• Submodules

• Performance enhancement

• Data enhancement

• Input/Output enhancement

• Execution control enhancement

• Procedure enhancement

• New intrinsic procedures

• More access to the computing environment

                                  WVS July 23, 2009 – Page 2 of 23
Submodules are program units that can extend a module or a
submodule. A module or submodule can have several submod-
ules. A submodule is very much like an Ada private child unit.

The primary use is to put the interfaces of module procedures
in the module, and their bodies in submodules, similar to the
distinction in Modula-2 between definition and implementation
modules, or in Ada between package spec and package body.

This prevents compilation cascades, and allows to publish a mod-
ule as definitive documentation of its interface without publishing
trade secrets embodied in its implementation.

Submodules were provided as an amendment to Fortran 2003 by
ISO Type 2 Technical Report number 19767.

                                        WVS July 23, 2009 – Page 3 of 23
                 Submodule example
Declaration of the interface of a procedure in a module

module Quadrature
  public :: Integrate
  module subroutine Integrate ( A, B, Func, Tolerance, &
    &                           Error_Estimate )
    real, intent(in) :: A, B, Tolerance
      real function Func ( X )
        real, intent(in) :: X
      end function Func
    end interface
    real, intent(out) :: Error_Estimate
  end subroutine
end module Quadrature
                                      WVS July 23, 2009 – Page 4 of 23
           Submodule example (cont.)
Definition of the procedure body in a submodule

submodule ( Quadrature ) Quadrature_body
  module procedure Integrate ! The interface can optionally
                             ! be repeated
    ! The body of the procedure is here. The parent module
    ! can be published as definitive documentation of the
    ! interface without publishing the trade secrets
    ! embodied in the submodule.

    ! The submodule can be compiled without causing a
    ! compilation cascade, since it depends upon the module,
    ! not vice-versa.
  end procedure Integrate
end submodule Quadrature_body
                                     WVS July 23, 2009 – Page 5 of 23
          Performance enhancements
• DO CONCURRENT construct allows iterations over its
  block to be executed concurrently.

• CONTIGUOUS attribute tells the compiler that array ele-
  ments of pointers or assumed-shape dummy arguments are

• Simply contiguous arrays don’t need the contiguous attribute
  to be specified explicitly.
  In addition to knowing the stride between array elements at
  compile time, performance is improved by not needing copy
  in / copy out or a check for contiguity when a contiguous
  array is an actual argument associated with a contiguous
  dummy argument.

                                     WVS July 23, 2009 – Page 6 of 23
                Data enhancements
• The maximum number of array dimensions has been in-
  creased to 15. In the case of coarrays the limit applies to
  rank + corank.

• Processors are required to provide at least one kind of integer
  with at least 18 decimal digits.

• The type of an allocatable component can be defined after
  the type containing the component. This allows mutually
  recursive types connected by allocatable components instead
  of pointers. Allocatable objects have semantics that are more
  favorable for optimization than the semantics of pointers.

• A polymorphic allocatable variable on the left side of an in-
  trinsic assignment statement is reallocated with the correct
  type, length type parameters, and shape, if necessary.
                                       WVS July 23, 2009 – Page 7 of 23
          Data enhancements (cont.)
• A polymorphic variable’s type can be taken during allocation
  from another polymorphic variable, without copying the value
  (this could be counted as a performance enhancement).

• The shape of a parameter array can be taken from its ini-
  tialization, as could be done for the length of a character
  parameter since 1990. Compare:
  character(len=*), parameter :: heading = "Input data"
  integer, parameter :: order(0:*) = [0, 1, 2, 3]

• Pointers can be initialized to other than NULL(). This ap-
  plies both to data pointers and procedure pointers.

• The kind of a FORALL index can be specified in the forall-
  header, e.g.,
  forall(integer(selected int kind(12)) :: i=1,1000000000).
                                     WVS July 23, 2009 – Page 8 of 23
              Accessing data objects
• Real and imaginary parts of complex numbers can be ac-
  cessed using a component-like syntax. This allows to update
  the parts independently, and to form array sections of parts.
  complex :: x(n), y(n)
  x%re = -2.0*y(n:1:-1)%im ! instead of
  ! x = cmplx(-2.0*aimag(y(n:1:-1),aimag(x))

• A reference to a pointer function is a variable and is permitted
  in any variable definition context. Other languages call this
  a left-hand function. This allows, for example, to calculate
  where to store data depending upon a key, e.g.,
  storage ( value%key ) = value
  where storage is a function returning a pointer of the same
  type as value.
                                       WVS July 23, 2009 – Page 9 of 23
                   Input / Output

• A NEWUNIT= specifier in an OPEN statement automati-
  cally selects a unit number that is not the same as the unit
  number of any existing connection.

• The G0 edit descriptor allows the processor automatically to
  select a suitable field width for output of any numeric type,
  as could be done for integers using the I0 format descriptor
  since 1990.

                                    WVS July 23, 2009 – Page 10 of 23
              Input / Output (cont.)

• A list of edit descriptors enclosed by parentheses can be pre-
  ceded by an asterisk, which has the effect of repeating the
  list indefinitely, as if it were preceded by an arbitrarily large
  integer, e.g.,
  write ( 10, ’("iarray = ", *(i0, :, ", "))’) iarray
  doesn’t need to be changed if the size of iarray changes.

• A function that is referenced in an input/output list can per-
  form input or output, so long as it is not on the same unit as
  a data transfer that is in progress. This is useful for tracing
  and diagnostic purposes.

                                      WVS July 23, 2009 – Page 11 of 23
                   Execution control

The BLOCK construct allows declarations within its scope, e.g.,

    integer :: i
    real :: a(n)
    do i = 1, n
      a(i) = i
    end do
  end block

ensures that the loop index i and the automatic array a(n) are
separate from any other variables with the same names.

                                     WVS July 23, 2009 – Page 12 of 23
            Execution control (cont.)

The EXIT statement can apply to any construct, not just a DO
construct, e.g.,

 outer: block
   do i = 1, num_in_set
     if ( x == a(i) ) exit outer
   end do
   print *, ’The element ’, x, ’ is not in the set’
 end block outer

Without this extension of EXIT, such computations required GO
TO or redundant tests.

                                    WVS July 23, 2009 – Page 13 of 23
             Execution control (cont.)

The optional stop code in a STOP statement is extended from
a string of one to five digits or a character constant to any
constant expression of type default integer or default character.
It is recommended that

1. the processor display it using formatted output on the unit
   identified by the named constant ERROR UNIT of the in-
   trinsic module ISO FORTRAN ENV, and

2. if it is an integer and the system supports command line
   execution it be made available as the exit status value.

                                       WVS July 23, 2009 – Page 14 of 23
           Programs and procedures
• The CONTAINS part of a program unit can be empty. This
  facilitates writing stubs during prototyping (and allows com-
  piler writers to get rid of a silly error message).

• An internal procedure can be an actual argument. When
  invoked by way of a dummy argument, its host instance is the
  one in effect when its name appeared as an actual argument,
  which might be different from the one when it is invoked if
  it is passed through a recursive invocation of its host.

• An internal procedure can be a procedure pointer target. The
  host instance when invoked by way of the pointer is the one
  in effect when its name was the proc-target in a procedure
  pointer assignment statement.

                                     WVS July 23, 2009 – Page 15 of 23
      Programs and procedures (cont.)
• Two specific procedures in a generic interface can be dis-
  tinguished if one has a pointer dummy argument that cor-
  responds to an allocatable dummy argument of the other

• When a null pointer actual argument corresponds to an op-
  tional nonpointer dummy argument the dummy argument is
  interpreted to be absent.

• An elemental procedure can be explicitly declared not to be
  pure, which means it operates in array element order, and it
  is allowed to have side effects prohibited to pure ones.

• The ENTRY statement has become obsolescent, which means
  it might be deleted sometime in the 22nd century.

                                    WVS July 23, 2009 – Page 16 of 23
   Intrinsic procedures for bit processing
• Bitwise comparison (bge, bgt, ble, blt) of integers treated
  as bit sequences.

• Counting bits (leadz, popcnt, poppar, trailz).

• Constructing masks of left- or right-adjusted contiguous se-
  quences of 1 bits (maskl, maskr).

• Shifting bits (shifta, shiftl, shiftr).

• Combined shifting of pairs of integers treated as bit se-
  quences (dshiftl, dshiftr).

• Merging bits (merge bits).

• Bit transformations (iall, iany, iparity).
                                     WVS July 23, 2009 – Page 17 of 23
    Other intrinsic procedure additions
• STORAGE SIZE ( A ) returns the storage size in bits of a
  scalar of the dynamic type of A, which may be a scalar or
  array of any type.

• An optional RADIX argument has been added to SE-

• PARITY ( LOGICAL ARRAY ) tests for the number of true
  values being odd.

• ATAN may have one or two arguments; with two arguments
  it is the same as ATAN2.

• Inverse trigonometric functions ACOS, ASIN, one-argument
  ATAN and hyperbolic intrinsic functions COSH, SINH and
  TANH may have complex arguments. Inverse hyperbolic
  functions ACOSH, ASINH and ATANH have been added.
                                  WVS July 23, 2009 – Page 18 of 23
Other intrinsic procedure additions (cont.)
• Elemental Bessel functions BESSEL J0 ( X ), BESSEL J1
  ( X ), BESSEL JN ( N, X ), BESSEL Y0 ( X ), BESSEL Y1
  ( X ), and BESSEL YN ( N, X ), have been added.
  Transformational functions BESSEL JN ( N1, N2, X) and
  BESSEL YN ( N1, N2, X ) overload BESSEL JN ( N, X )
  and BESSEL YN ( N, X ).

• Error functions ERF ( X ), ERFC ( X ) and ERFC SCALED
  ( X ) = exp(X 2) erfc(X) have been added.

• Gamma functions GAMMA ( X ) =            ∞ X−1 e−tdt and
                                           0 t
  LOG GAMMA ( X ) have been added.

• The FINDLOC function finds the location in an array of a
  specified value.

                                 WVS July 23, 2009 – Page 19 of 23
Other intrinsic procedure additions (cont.)
• NORM2 computes Euclidean vector norms, Frobenius matrix
  norms, and their generalizations for arrays of higher dimen-
  sions, without intermediate overflow or underflow unless the
  result overflows or underflows.                 2
                                     i,j,...,k xi,j,...,k .

• HYPOT ( X, Y ) computes the Euclidean distance X 2 + Y 2
  without intermediate overflow or underflow unless the result
  overflows or underflows (it’s really just the same as CABS).

• An EXECUTE COMMAND LINE              subroutine      has    been

• The MAXLOC and MINLOC functions now have an optional
  BACK argument to report the location of the last instead of
  the first of several equal maximum (minimum) values.
                                    WVS July 23, 2009 – Page 20 of 23
New named constants in ISO FORTRAN ENV

  INT64, REAL32, REAL64 and REAL128 are kind type pa-
  rameter values. The first four are arrays whose sizes are the
  numbers of kinds available. The rest are scalars.

  IOSTAT= value returned by an INQUIRE statement if the
  unit number identifies an internal unit (i.e., a character vari-
  able), which can only happen during defined input/output.

                                      WVS July 23, 2009 – Page 21 of 23
    New procedures in intrinsic modules

  FORTRAN ENV are functions that return the compiler op-
  tions and compiler version, respectively, of the compiler used
  to compile the program unit from which they are referenced.

• C SIZEOF ( X ) is a function in the intrinsic module ISO -
  C BINDING that returns the result of applying the C sizeof
  operator to an object of a C type that interoperates with the
  type and type parameters of X. If X is an array the result
  is the number of elements times the value that would be
  returned for a scalar of the same type and kind as X.

                                     WVS July 23, 2009 – Page 22 of 23
A comprehensive mechanism for single-program multiple-data
parallel programming, called coarrays, has been added. This
is by far the largest project in Fortran 2008.

In brief, a program is a collection of images that execute con-
currently. Each one might (or might not) execute on a different
processor from the others.

Data objects of any type can be declared that spread across the
images. Values can be accessed from one image to another by
adding cosubscripts enclosed in square brackets.

Synchronization facilities are provided.

See ftp://ftp.nag.co.uk/sc22wg5/N1751-N1800/N1762.pdf for
a more complete discussion.

                                       WVS July 23, 2009 – Page 23 of 23

To top