By Deniz Savas, CiCS, Shef. Univ., 2015

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

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
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.
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.
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.
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 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: Specification Statements Session Six ICoCSIS.
Introduction to FORTRAN
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 ,
Scientific Computing Division A tutorial Introduction to Fortran Siddhartha Ghosh Consulting Services Group.
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.
An introduction to arrays WEEK 7 Introduction In scientific and engineering computing, it is very common to need to manipulate ordered sets of values,
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
FORTRAN 90+ Yetmen Wang Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
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.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007
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.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Secure Coding Rules for C++ Copyright © 2016 Curt Hill
Expressions and Assignment Statements
Chapter 9: Value-Returning Functions
Chapter 10 : Implementing Subprograms
Lecture: MATLAB Chapter 1 Introduction
Programming For Nuclear Engineers Lecture 6 Arrays
Functions and Structured Programming
Principles of programming languages 4: Parameter passing, Scope rules
Wrapping Fortran libraries
Organization of Programming Languages
Array processing and Matrix manipulation
Secure Coding Rules for C++ Copyright © Curt Hill
Programmazione I a.a. 2017/2018.
Arrays in C.
Lecture 6 C++ Programming
Pointers and References
Subroutine Comp 208 Yi Lin.
7 Arrays.
Midterm Review Programming in Fortran
Matlab tutorial course
College of Computer Science and Engineering
By Deniz Savas, CiCS, Shef. Univ., 2015
CISC181 Introduction to Computer Science Dr
Languages and Compilers (SProg og Oversættere)
7 Arrays.
CS150 Introduction to Computer Science 1
mdul-cntns-sub-cmmnts2.f90
CS150 Introduction to Computer Science 1
Course Overview PART I: overview material PART II: inside a compiler
CS150 Introduction to Computer Science 1
Array processing and Matrix manipulation
Presentation transcript:

By Deniz Savas, CiCS, Shef. Univ., 2015 Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, Shef. Univ., 2015

Summary of topics covered 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 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 , i.e. pre Fortran90 INTEGER N , M PARAMETER ( N = 20 , M=30) REAL X , Y , Z, PI DIMENSION X(N) , Y(N) , Z(N) DATA PI / 3.1416 / New (Fortran90) style INTEGER, PARAMETER :: N =20 , M = 30 REAL, DIMENSION(N) :: X , Y , Z REAL :: PI = 3.146 Note: The above two segments of code 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 to as Derived_Data_Types or Structures EXAMPLE: ! First define the type structure TYPE VERTEX REAL X , Y, Z END TYPE VERTEX ! Next declare variables of this type. TYPE ( VERTEX ) :: CORNER1 , CORNER2 ! Now we can initialise and use them 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 TYPE 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 )

ARRAYS: Declarations SIMPLE 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 ) , STAT=status) ALLOCATE ( Z(SIZE(X) ) DEALLOCATE ( X,Y,Z) END Note: status is an integer variable that will contain 0 if allocation has been successful or a non-zero value to indicate error.

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

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

Above only A and B are conformable with each other. 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:3) : 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 END WHERE Note: ELSE WHERE clause is optional.

Examples 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

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 0-340-60034-9 Fortran 90 Explained by Metcalf & Reid is available from Blackwells ‘ St Georges Lib.’ Oxford Science Publications, ISBN 0-19-853772-7