Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Intermediate Code Generation
Introduction to arrays
879 CISC Parallel Computation High Performance Fortran (HPF) Ibrahim Halil Saruhan Although the [Fortran] group broke new ground …
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.
Chapter 7: User-Defined Functions II
Kernighan/Ritchie: Kelley/Pohl:
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 ,
Need for Arrays Exercise Read the IDs and the grades for all ICS 101 students. Compute and print the average of the students. Print the grades and IDs.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
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.
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 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
FORTRAN Short Course Week 2 Kate Thayer-Calder February 23, 2009.
Introduction to Fortran Fortran Evolution Drawbacks of FORTRAN 77 Fortran 90 New features Advantages of Additions.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
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 ARRAYS. 2 Array subscript expressions  Each subscript in an array element designator is an expression of integer type. It need not be a constant.
Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
Structured Data Types. Date Types We have seen various data types –Integer, Real, Character, Logical All these types define data values of different kinds.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Fortran: Specification Statements Session Six ICoCSIS.
5.3 Machine-Independent Compiler Features
Introduction to FORTRAN
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Operator Overloading and Type Conversions
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Imperative Programming
Fortran: Program Units and Procedures Session Four ICoCSIS.
Multi-Dimensional Arrays
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Fortran: Expressions and Assignments Session Two ICoCSIS.
Scientific Computing Division A tutorial Introduction to Fortran Siddhartha Ghosh Consulting Services Group.
FORTRAN FORmula TRANslator -Anand Trivedi. HISTORY  Designed and written from scratch in by an IBM team lead by John W. Backus as the first.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Fortran 90, 95, and Fortran 90 ● Generalities: format changes, portable numerics ● Arrays: syntax, classes of arrays, dynamic storage ● Structures.
1 Serial Run-time Error Detection and the Fortran Standard Glenn Luecke Professor of Mathematics, and Director, High Performance Computing Group Iowa State.
CHAPTER 9 MUTIDIMENSIONAL ARRAYS. Introduction to multidimensional Arrays and Multiply subscripted variables.
An introduction to arrays WEEK 7 Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values,
Arrays Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values, such as vectors and matrices.
CHAPTER 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and.
FORTRAN 90+ Yetmen Wang Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT.
ISBN Chapter 10 Implementing Subprograms.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
HPF (High Performance Fortran). What is HPF? HPF is a standard for data-parallel programming. Extends Fortran-77 or Fortran-90. Similar extensions exist.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
CPE779: Shared Memory and OpenMP Based on slides by Laxmikant V. Kale and David Padua of the University of Illinois.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
CHAPTER 4 VARIABLES & BINDING SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Chapter 9: Value-Returning Functions
Test 2 Review Outline.
Programming For Nuclear Engineers Lecture 6 Arrays
Array processing and Matrix manipulation
Subroutine Comp 208 Yi Lin.
7 Arrays.
Midterm Review Programming in Fortran
By Deniz Savas, CiCS, Shef. Univ., 2015
Constructors and destructors
Languages and Compilers (SProg og Oversættere)
mdul-cntns-sub-cmmnts2.f90
Course Overview PART I: overview material PART II: inside a compiler
Array processing and Matrix manipulation
Presentation transcript:

Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007

Part II: Contents Data Declarations and Specifications ARRAYS and Array Handling Where & Forall statements

Data Specifications & Declarations Type declarations ( built in and user-defined) IMPLICIT statement PARAMETER statement DIMENSION statement DATA statements SAVE statement USE statement

Type Declarations Syntax: type [,attribute] :: list TYPE can be one of ; INTEGER ( KIND= n) - REAL ( KIND=n) COMPLEX( KIND= n ) - LOGICAL(KIND=n) CHARACTER ( LEN=m,KIND=n) where (KIND=n) is OPTIONAL TYPE ( type-name) ATTRIBUTE can be a combination of; PARAMETER - PUBLIC - PRIVATE POINTER - TARGET - ALLOCATABLE DIMENSION - INTENT(inout) - OPTIONAL SAVE - EXTERNAL - INTRINSIC

Example Type Declarations Old Style (FTN77) INTEGER N, M PARAMETER ( N = 20, M=30) REAL X, Y, Z, PI DIMENSION X(N), Y(N), Z(N) DATA PI / / New Style (FTN90 onwards) INTEGER, PARAMETER :: N =20, M = 30 REAL, DIMENSION(N) :: X, Y, Z REAL :: PI = Note: The above two code segments are identical in effect.

Example use of KIND values in variable declarations INTEGER, PARAMETER :: SMLINT=SELECTED_INT_KIND(2) INTEGER, PARAMETER :: BIGINT=SELECTED_INT_KIND(7) INTEGER,PARAMETER :: BIG=SELECTED_REAL_KIND(7,40) INTEGER(KIND=SMLINT) :: I, J, K INTEGER(KIND=BIGINT) :: ISUM, JSUM REAL ( KIND=BIG) :: A, B,C The Intrinsic function KIND( ) returns the kind value of its argument. EXAMPLE: IKIND = KIND(1.0E60)

User Defined Types Also referred as Derived-Data-Types or Structures EXAMPLE: TYPE VERTEX REAL X, Y, Z END TYPE VERTEX TYPE ( VERTEX ) :: CORNER1, CORNER2 CORNER2%Y = 2.5 ; CORNER1=( 1.0,1.0,0.2)

Derived Types ( Examples) TYPE VERTEX REAL :: X, Y, Z END TYPE VERTEX TYPE PATCH TYPE ( VERTEX ) :: CORNER(4) END PATCH TYPE (VERTEX) :: P1, P2, P3, P4 TYPE (PATCH) :: MYPATCH, YOURPATCH P1 = ( 0.0, 0.0, 0.0 ) ; P3 = ( 1.0, 1.0, 0.0 ) P2 = ( 1.0, 0.0, 0.0 ) ; P4 = ( 0.0, 1.0, 0.0 ) MYPATCH = ( P1, P2, P3,P4 ) YOURPATCH = MYPATCH YOURPATCH%CORNER(1) = MYPATCH%CORNER(4) - & (1.0,1.0,1.0)

Derived Types ( More Examples) TYPE USERNAME CHARACTER*2 :: DEPARTMENT INTEGER :: STATUS CHARACTER*4 :: INITIALS TYPE ( USERNAME ), POINTER :: NEIGHBOUR END TYPE USERNAME TYPE ( USERNAME ), DIMENSION(1000) :: USERS USERS(1) = ( ‘CS’, 1, ‘DS’ ) USERS(12) = (‘CS’, 1, ‘PF’ ) USERS(1)%NEIGHBOUR => USERS(12) NULLIFY( USERS(12)%NEIGHBOUR )

Array Declarations STATIC INTEGER, PARAMETER :: N = 36 REAL :: A( 20), B(-1:5), C(4,N,N), D(0:N,0:4) INTEGER, DIMENSION(10,10) :: MX,NX,OX Note that up to 7 dimensional arrays are allowed. ALLOCATABLE ( Dynamic global memory allocation) REAL, ALLOCATABLE :: A(:), B(:,:,:) AUTOMATIC ( Dynamic local memory allocation) REAL, DIMENSION SIZE(A) :: work ASSUMED SHAPE ( Used for declaring arrays passed to procedures ) REAL A(* ) ! Fortran77 syntax REAL :: A(:) ! or A(:,:) so on Fortran90 syntax

Allocatable Array Examples Used for declaring arrays whose size will be determined during run-time. REAL, ALLOCATABLE :: X(:), Y(:,:), Z(:) : READ(*,*) N ALLOCATE( X(N) ) ALLOCATE ( Y(-N:N,0:N ), STATUS=status) ALLOCATE ( Z(SIZE(X) ) : DEALLOCATE ( X,Y,Z) END

Assumed Shape Array Examples Used for declaring the dummy array arguments’ dimensions to procedures when these vary from call to call. PROGRAM TEST REAL :: AX1(20), AX2(30), AY(10,10), BX1(80), BX2(90),BY(20,30) : Call spline( AX1, AX2, AY) Call spline (BX1, BX2, BY) : END SUBROUTINE SPLINE( X1, X2, Y ) REAL, DIMENSION(:) :: X1, X2 REAL, DIMENSION(:,:) :: Y : RETURN END

Automatic Array Examples Use this method when you need short term storage for intermediate results. WORK arrays in NAG library are good examples ! SUBROUTINE INTERMIT( X1, X2, Y,M) REAL, DIMENSION(:) :: X1, X2, Y INTEGER :: M REAL, DIMENSION(SIZE(Y) ) :: WORK COMPLEX, DIMENSION(M) :: CWORK : RETURN END

Array Terminology RANK, EXTENT, SHAPE and SIZE Determines the conformance CONFORMANCE REAL A( 4,10), B( 0:3,10), C( 4,5,2), D(0:2, -1:4, 6 ) A & B are: rank=2, extents=4 and 10 shape=4,10, size=40 C is rank=3, extents=4,5and 2 - shape=4,5,2 - size=40 D is rank=3 - extents=3, 6 and 6 - shape=3,6,6 - size= 108 For two arrays to be conformable their rank, shape and size must match up. Above only A and B are conformable with each other.

Whole Array Operations Whole array operations can be performed on conformable arrays. This avoids using DO loops. REAL :: A(10,3,4), B( 0:9,3,2:5), C(0:9,0:2,0:4) : C = A + B ; C = A*B ; B= C/A ; C =sqrt(A) All the above array expressions are valid.

WHERE Statement This feature is a way of implementing vector operation masks. It can be seen as the vector equivalent of the IF statement WHERE (logical_array_expr ) array_var=array_expr WHERE (logical_array_expr ) array_assignments ELSE WHERE array_assignments END WHERE Note: ELSE WHERE clause is optional.

Examples of where REAL :: A(300), B(300) WHERE ( A > 1.0 ) A = 1.0/A WHERE ( A > B) A = B END WHERE WHERE ( A > 0.0.AND. A>B ) A = LOG10( A ) ELSEWHERE A = 0.0 END WHERE

FORALL statement This is a new Fortran95 feature. Extending the ability of Fortran Array Processing that was introduced by the WHERE statement. Syntax: FORALL (index=subscript_triplet, scalar_mask_expression) block of executable statements END FORALL

FORALL statement example FORALL ( II=1:100,A(II)>0 ) X(II)=X(II)+Y(II) Y(II)= Y(II) * X(II) END FORALL Mask is elements of A between 1 and 100 whose values are positive

Referencing Array Elements REAL A (10), X(10,20,30), value INTEGER I, K(10) value = A( 8) OK VALUE = A(12) WRONG !!!! I = 2; value = A( I ) OK J = 3 ; I = 4 ; VALUE = X ( J, 10, I ) OK Value = X (1.5 ) WRONG !!! X(:,1,1) = A(K) ! OK assuming K has values within the range 1 to 10

Referencing Array Sections REAL :: A(10), B(4,8), C(4,5,6) I = 2 ; J=3 ; K=4 Referencing the array elements: A(2) B(3,4) C( 3,1,1) A(K) B(I,4) C(I,J,K) Referencing the array section: A(2:5) B(1,1:6) C( 1:1,3:4, 1:6 ) A(I:K) B( I:J,K) C(1:I,1:J,5 )

Referencing Array Sections (cont.) It is also possible to use a stride index when referring to array sections. The rule is : array(start-index:stop-index:stride, ….) Stride can be a negative integer. If start-index is omitted it is assumed to be the lower_bound If stop-index is omitted it is assumed to be the upper_bound For example if array A is declared with DIMENSION(10,20) we can reference a subsections that contains only the even colums as; A(2:10:2,: ) The following are some other examples: A(1:9:3,1:20:2) this is the same as A(1:9:3,::2) A(10:1:-1,: ) this reverses the ordering of first dimension

Array Intrinsics Many of the elemental numeric functions such as those listed below can be called with array arguments to return array results ( of same shape and size). abs, sin, acos, exp, log, int, sqrt... There are also additional Vector/Matrix algebra functions designed to perform vector/matrix operations: dot_product, matmul, transpose, minval, sum, size, lbound, ubound, merge, maxloc, pack

Array Assignments REAL A(10,10), B(10), C(0:9), D(0:30) A = 1.0 ; B=0.55 B = (/ 3., 5.,6.,1., 22.,8.,16., 4.,9., 12.0 /) I = 3 C = A( I,1:10) D(11:20) = A(I+1,1:10) D(1:10) = D(11:20) D(3:10) = D(5:12) A(2,:) = SIN( B )

Acknowledgement &References: Thanks to Manchester and North High Performance Computing, Training & Education Centre for the Student Notes. See APPENDIX A of the above notes for a list of useful reference books Fortran 90 for Scientists and Engineers, Brian D Hahn, ISBN Fortran 90 Explained by Metcalf & Reid is available from Blackwells ‘ St Georges Lib.’ Oxford Science Publications, ISBN

THE END