Fortran 90, 95, and 2000. Fortran 90 ● Generalities: format changes, portable numerics ● Arrays: syntax, classes of arrays, dynamic storage ● Structures.

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Advertisements

Programming Languages and Paradigms The C Programming Language.
PL/SQL.
Names and Bindings.
Chapter 7: User-Defined Functions II
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
The new features of Fortran 2003 David Muxworthy BSI Fortran Convenor.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Chapter 9 Imperative and object-oriented languages 1.
Fortran: Array Features Session Five ICoCSIS. Outline 1.Zero-sized Array 2.Assumed-shaped Array 3.Automatic Objects 4.Allocation of Data 5.Elemental Operations.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Chapter 10.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Arrays Array Terminology Declarations Visualisation of Arrays Array Conformance Array Element Ordering Array Syntax Whole Array Expressions Array Sections.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ISBN Chapter 6 Data Types: Structured types.
Introduction to Fortran Fortran Evolution Drawbacks of FORTRAN 77 Fortran 90 New features Advantages of Additions.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Fortran 9x HTML version. New F90 features Free source form Modules User-defined data types and operators Generic user-defined procedures Interface blocks.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Chapter 8 ARRAYS. 2 Array subscript expressions  Each subscript in an array element designator is an expression of integer type. It need not be a constant.
Pointers Applications
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Fortran: Specification Statements Session Six ICoCSIS.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Fortran: Program Units and Procedures Session Four ICoCSIS.
Multi-Dimensional Arrays
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
FORTRAN FORmula TRANslator -Anand Trivedi. HISTORY  Designed and written from scratch in by an IBM team lead by John W. Backus as the first.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Module and Data Sharing. Programming in the Large Software, in general, is large having multiple units Multiple units designed and developed independently.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Ch. 5 Ch. 51 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (more notes) Dr. Carter Tiernan.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
An introduction to arrays WEEK 7 Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values,
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Compilation Technology SCINET compiler workshop | February 17-18, 2009 © 2009 IBM Corporation Software Group Fortran 2003 Jim Xia IBM Toronto Lab
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
FORTRAN 90+ Yetmen Wang Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Names, Scope, and Bindings Programming Languages and Paradigms.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
APPENDIX a WRITING SUBROUTINES IN C
Array processing and Matrix manipulation
Pointers and References
Data Structures and Abstract Data Types
By Deniz Savas, CiCS, Shef. Univ., 2015
Objectives You should be able to describe: Addresses and Pointers
Java Programming Language
Programming Languages and Paradigms
Array processing and Matrix manipulation
Presentation transcript:

Fortran 90, 95, and 2000

Fortran 90 ● Generalities: format changes, portable numerics ● Arrays: syntax, classes of arrays, dynamic storage ● Structures ● Derived types ● Pointers (not like C/C++ pointers, more like aliases) ● Type parameterization ● Modules and interfaces

Fortran-C++ Translations ● derived type = user-defined type (struct) ● module = class (sorta, more in F2k than F95) ● scalar = basic entity (real, int, arrays, complex) ● functional language = procedural language ● extending = subclassing (makes more sense) ● use != uses/provides patterns

First the Good News ● Alternate returns obsolescent – calling procedure provides alternate places in itself to which a called procedure would return control ● Alternate entry points may be obsolescent in F2k – allowed execution start point in a called procedure to be specified by the caller ● Multiple ways of doing same thing eliminated ● Free (but reported unable to handle some dusty corners): s/f60l/noncom.htm

Fortran 90: General ● Format changes – Free-form (one-line multistatement delimiter ";") – Continuation is "&" at the end of the line to be continued – F77 required statements in columns 7-71 only – Comments start with "!", not "c" ● Array handling: multidimensional arrays are directly supported constructs. rank of an array is its dimensionality ● Complex numbers built-in (big performance win over current C++ libraries) ● Relational Operators (.eq. is ==,.le. is, etc.)

Fortran 90 ● Array syntax: you can replace array operations such as do k = 1, n y(k) = y(k) + a*x(k) end do with the one-liner y(1:n) = y(1:n) + a*x(1:n) or, if x and y are exactly of length n, y = y + a*x

Fortran 90 ● Numerical precision control: If at least 10 decimal digits will be needed and exponents from to , you can declare real(kind=selected_real_kind(10,50)) :: x, y Or you can match the precision with a given constant: integer, parameter:: dbk = kind(1.0d0) real(kind=dbk) :: x, y BEWARE: although a kind is an integer, the mapping between integers and the actual kinds is compiler implementor dependent So avoid real(kind=2) :: x, y

Fortran 90 ● Array declarations are clearer: real(kind=dbk), dimension(3,4) :: A, B real, dimension(-2:5,3:4) :: C ● Declares A and B to be 3x4 matrices of datatype real(dbk) with enough precision to match the constant 1.0d0 ● Declares C to be a 8x2 array, with row indices ranging from -2 to 5 and column indices from 3 to 4 ● Array bounds are inclusive of each end - unlike Perl which excludes the upper bound.

Fortran 90 ● Array sections: these work just like in Matlab, but with the stride last. So if real(kind=dbk), dimension(3,5) :: A then the section A(:3:2,1::2) = A(1:3:2,1:5:2) refers to the matrix [A(1,1) A(1,3) A(1,5)] [A(3,1) A(3,3) A(3,5)] Also like Matlab, you can use vector subscripts. So if integer, dimension(2) :: R = (/1,3/) integer, dimension(3) :: C = (/1,3,5/) then the array section A(R,C) is the same as the section A(:3:2,1::2) ● Can pass such a critter as A(R,C) as argument of a function!

Fortran 90 ● Array operations may use a mask operation, keyword where: where (x > 0) y = log(x) The general form of the where has an else clause: where (x > 0) y = log(x) elsewhere y = -inf end where ● What if you call a function with argument x in one of the clauses? – All statements within a where must be array assignments

Fortran 90 ● All the Fortran intrinsic functions can be used with arrays instead of just scalars: do k = 1, n x(k) = y(k) + a*sin(x(k)) end do becomes (if x,y are of conformant length n) x = y + a*sin(x)

Fortran 90: Array Classes ● Five classes of arrays are possible: 1)explicit shape (also available in F77) 2)assumed-shape 3)automatic 4)assumed size (also available in F77) 5)deferred-shape (allocatable arrays)

Explicit Shape Arrays ● Explicit shape have their dimensions passed as arguments to a subroutine (or dims passed in via modules, or host info access) subroutine explicit(A,B,m,n) integer, intent(in) :: m,n real, dimension(0:m+1, 0:n+1), intent(inout) :: A real, dimension(m,n,3), intent(out) :: B... end subroutine explicit ● The bounds of the actual and dummy arguments need not match Caller could have had real, dimension(m+2,n+2) :: A ● The extents should match (length of each dimension)

Assumed Shape Arrays ● Assumed shape have their extents associated with actual arguments when the call is made subroutine assumed(A,B) real, dimension(0:, 0:), intent(inout) :: A real, dimension(:,:,:), intent(out) :: B n = ubound(B, dim=2) -1 end subroutine assumed ● Actual arg must have same type and have same rank as the dummy arguments ● Can retrieve lbound, ubound, and size of each dimension.

Automatic Arrays ● Automatic arrays are explicit shape arrays which are not dummy args and have at least one nonconstant index bound subroutine automatic(B, n) integer :: n real, dimension(0:n+1, 0:n+1) :: A real, dimension(size(B,1)) :: C real, dimension(1000) :: D ! Not an auto array end subroutine automatic ● Space is allocated dynamically on entry, deallocated on exit ● Can be passed to other routines in between

Assumed Size Arrays ● Assumed size are the worst of all. All extents except last must be given explicitly; last is given as * or lower_bound:* ● Size of actual argument and dummy argument must match ● Shapes of actual and dummy arguments need not match. ● So can declare A(10,10,10) and pass to a routine which declares the dummy argument as B(5, *). ● Deprecated in F90; not sure about status in F2k but likely it will remain.

Fortran 90: Allocatable Arrays program arrayexample integer :: i, j, n, m ! newer style declaration of two integers real, dimension(:,:), allocatable :: A, B, C real, dimension(:), allocatable :: d, e, f write(*,"('Enter the size of A(n,m) :')", advance='no'); read(*,*) n,m allocate (A(n,m)); allocate (B(m,n)); allocate (C(n,n)) allocate ( e(size(C)) ) ! Creates rank-1 array of length n*n call define_AB( A, B, n, m ) ! user-defined setup of matrices A and B C = matmul( A, B ) ! intrinsic function matrix multiply print *, 'C = ' ! Output of entire array C with some default formatting deallocate(A) ! deallocation order not important... end program arrayexample

Modules and Derived Types module rational_arithmetic type ratnum integer :: num, den end type ratnum interface operator(*) module procedure rat_rat, int_rat, rat_int end interface private :: rat_rat, int_rat, rat_int contains type(ratnum) function rat_rat(l,r) type(ratnum), intent(in) :: l,r rat_rat%num = l%num * r%num rat_rat%den = l%den * r%den end function rat_rat type(ratnum) function int_rat(l,r) type(ratnum), intent(in) :: r integer, intent(in) :: l... end function int_rat end module rational_arithmetic program main use rational_arithmetic integer :: i = 32 type(ratnum) :: a,b,c a = ratnum(1,16); b = 2*a; c = b*3 b = a*i*b*c end program main

Modules and Derived Types ● A derived type can have – a further derived type – the type currently being declared (usual linked list data structure mechanism) ● sequence attribute can force storage locations ● User defined types cannot contain allocatable entities – restriction may disappear in F2k

Pointers ● ptr => y ! pointer assignment (aliasing) ● ptr = x ! value assignment; sets y = x ● nullify(ptr) ! disassociate ptr and y ● Thing pointed at must have the target attribute real, dimension(1:n,1:n), target :: A

Pointers ● real, dimension(:,:), pointer :: pa, pb – pa is a ptr for a 2D array of reals – can associate memory with a ptr allocate(prb(0:n,0:2*n*n), stat=ierr) ● pa => A(-k:k,-j:j) – pa is now an alias for (part of) A. ● Pointers are automatically dereferenced – pa(15:25,5:15) = pa(10:20,0:10) + 1.0

Optional Arguments subroutine optargs(scale, x) real, intent(in) :: x real, intent(in), optional :: scale real :: actual_scale actual_scale = 1.0 if (present(scale)) actual_scale = scale... end subroutine optargs

Interface Blocks interface subroutine assumed(A, B) real, dimension(-1:, -1:), intent(inout) :: A real, dimension(:,:,:), intent(out) :: B end subroutine assumed end interface ● Mandatory for external non-module procs with – optional and keyword arguments – pointer and target arguments – assumed shape or sliced array arguments – array or pointer valued procedures

Function Overloading ● User defined overload set interface zero module procedure zero_int module procedure zero_real end interface zero ● Generic name zero is associated with specific names zero_int and zero_real ● One called is based on argument signature

Fortran 90 Array Intrinsics all(mask, dim) : true if all values are true cshift(array, shift, dim) : circular shift dot_product(A, B) : dot product of two rank-one arrays matmul(A, B) : A*B, if conformant and at least one is rank-2 (no vector outer product) maxval(array, dim, mask) : maximum value of array merge(tsource, fsource, mask) : combining two arrays using a mask maxloc(array, mask) : location of element with maximum value spread(source, dim, ncopies) : replicate an array by adding a dimension sum(array, dim, mask) : sum array elements transpose(matrix) : transpose array of rank two

Fortran 95 Features ● Minor enhancements over F90: – forall statement and construct – pure and elemental procedures ● pure means no side effects; prefix for function ● elemental means pure function with only scalar dummy arguments. Can then be called with conformant array actual arguments, and it operates componentwise. – structure and pointer default initialization

Fortran 2004 ● Fortran standard is multi-part (ISO/IEC 1539) – Part 1 is Base Language – Part 2: Varying length strings – Part 3: Conditional compilation

Fortran 2000 ● Exception handling ● Standardized C interoperability (for OS access mainly) ● Allow derived data types to have allocatable components and parameterized types (kinds) ● Possibly facilities for interval arithmetic ● Asynchronous and derived type I/O ● Object oriented features (as long as they don't interfere with performance capabilities)

Fortran 2000 Additions ● Parameterized derived types: can use kinds and have allocatable entities type matrix(precision,m,n) integer, kind :: precision integer, nonkind :: m, n real(precision) :: A(m,n) end type type(matrix(kind(0.0d0), 20,20) :: G type(matrix(kind(0.0d0),:,:), allocatable :: G

Fortran 2000 Additions ● Procedure pointer with same interface as proc: procedure (proc), pointer : p => null( ) ● Abstract interfaces to allow ptr w/o proc abstract interface real function foo(x,y) real, intent(in) :: x, y end function end interface

Fortran 2000 Additions ● Type-bound procedures – analogous to methods of a class – can be referenced using % operator

Fortran 2000 Additions ● I/O – asynchronous transfer – stream access – user specified transfer operations for derived types – user control of rounding during format conversions – named constants for preconnected units – flush statement – regularization of keywords – access to error messages

C-Fortran Interoperability ● Interoperability is via ISO_C_BINDING module – C_INT = int – C_SIZE_T = size_t – C_CHAR = char (but not strings) – C_PTR = void * (interoperates w/ any C ptr) ● function C_LOC(x) for address finding – x = procedure, ptr to proc, variable w/ interoperable type, scalar with target attribute and already allocated

C-Fortran Interoperability ● Derived types must be explicitly bindable type, bind(C) :: stuff.... end type stuff ● All components of type must be interoperable, not a ptr, and not allocatable ● Equivalent to a corresponding C struct ● Not valid: C structs with union, bit fields, flexible array components

C-Fortran Interoperability ● Fortran arrays interoperable with C arrays, but with reversal of subscript order – real(kind(0.0d0)) :: A(7,4:9) – double A[6][7] ● In functions, can specify a dummy argument is passed by value integer, value :: x ● Lets compiler do a one-way copy

C-Fortran Interoperability ● Make Fortran functions available by name to a C process using bind(C): – function, bind(C) :: bar(x,y) – function, bind(C,name='foo') :: bar(x,y) ● Name available without underscore convention, as bar (first caes) or foo(second case)

Odds and Ends ● Recursive functions allowed. Specify via the recursive attribute when declared ● Many F90 compilers allow – %REF( ) arguments (passes in character arrays without the implicit appending of char array length) – %VAL( ) arguments (pass by value)

Conclusions ● Key issues of F90 are still going to be around – Array passing, especially with slices – Dynamic library loading mechanisms ● Fortran 2000 – Does not seem to introduce greatly more complexity – Does not seem to have helped much with its C- Fortran interoperability; existing conventions seem more powerful, useful ● Need to analyze real F9x codes used in labs and derive support set for CCA