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

Similar documents
Introduction to Fortran95 Programming Part I. By Deniz Savas, CiCS, Shef. Univ., 2018

2 3. Syllabus Time Event 9:00{10:00 morning lecture 10:00{10:30 morning break 10:30{12:30 morning practical session 12:30{1:30 lunch break 1:30{2:00 a

Review More Arrays Modules Final Review

Chapter 4. Fortran Arrays

Subroutines, Functions and Modules

Introduction to Programming with Fortran 90


Fortran Coarrays John Reid, ISO Fortran Convener, JKR Associates and Rutherford Appleton Laboratory

Introduction to Modern Fortran

Fortran 90 - A thumbnail sketch

dbx90: Fortran debugger March 9, 2009

Bits, Bytes, and Precision

Lecture V: Introduction to parallel programming with Fortran coarrays

NO CALCULATOR ALLOWED!!

NAGWare f95 Recent and Future Developments

INTRODUCTION TO FORTRAN PART II

TS Further Interoperability of Fortran with C WG5/N1917

Goals for This Lecture:

The Fortran Basics. Handout Two February 10, 2006

2 Recommended books and a Web page

HPF commands specify which processor gets which part of the data. Concurrency is defined by HPF commands based on Fortran90

Technical Report on further interoperability with C

Basic Fortran Programming. National Computational Infrastructure

FORTRAN - ARRAYS. For example, to declare a one-dimensional array named number, of real numbers containing 5 elements, you write,

Technical Specification on further interoperability with C

NAGWare f95 and reliable, portable programming.

AN INTRODUCTION TO FORTRAN 90 LECTURE 2. Consider the following system of linear equations: a x + a x + a x = b

Welcome. Modern Fortran (F77 to F90 and beyond) Virtual tutorial starts at BST

Chapter 3. Fortran Statements

ARRAYS COMPUTER PROGRAMMING. By Zerihun Alemayehu

Numerical Modelling in Fortran: day 2. Paul Tackley, 2017

Parallel Programming in Fortran with Coarrays

7. Procedures and Structured Programming

A Brief Introduction to Fortran of 15

Practical Exercise 1 Question 1: The Hello World Program Write a Fortran 95 program to write out Hello World on the screen.

Chapter 6. A Brief Introduction to Fortran David A. Padua

Goals for This Lecture:

Fortran Coding Standards and Style

Allocating Storage for 1-Dimensional Arrays

More Coarray Features. SC10 Tutorial, November 15 th 2010 Parallel Programming with Coarray Fortran

Fortran. (FORmula TRANslator) History

Fortran 2003 Part 1. École normale supérieure L3 geosciences 2018/2019. Lionel GUEZ Laboratoire de météorologie dynamique Office E324

41391 High performance computing: Miscellaneous parallel programmes in Fortran

Visual basic tutorial problems, developed by Dr. Clement,

Appendix D. Fortran quick reference

Declaration and Initialization

Co-arrays to be included in the Fortran 2008 Standard

SHAPE Returns the number of elements in each direction in an integer vector.

Informatica 3 Syntax and Semantics

Table 2 1. F90/95 Data Types and Pointer Attributes. Data Option. (Default Precision) Selected-Int-Kind

Introduction to Fortran

ME1107 Computing Y Yan.

Page 1 of 7. Date: 1998/05/31 To: WG5 From: J3/interop Subject: Interoperability syntax (Part 1) References: J3/98-132r1, J3/98-139

Verification of Fortran Codes

CS 199 Computer Programming. Spring 2018 Lecture 2 Problem Solving

Computational Astrophysics AS 3013

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

AMath 483/583 Lecture 8

Fortran 2008: what s in it for high-performance computing

A quick guide to Fortran

Old Questions Name: a. if b. open c. output d. write e. do f. exit

C introduction: part 1

6.1 Expression Evaluation. 1 of 21

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

Intrinsic Numeric Operations

PROBLEM SOLVING WITH FORTRAN 90

CSE 262 Spring Scott B. Baden. Lecture 4 Data parallel programming

Parallel Programming Languages. HPC Fall 2010 Prof. Robert van Engelen

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

ISO/IEC : TECHNICAL CORRIGENDUM 2

PACKAGE SPECIFICATION HSL 2013

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

Scientific Programming in C VI. Common errors

International Standards Organisation. Parameterized Derived Types. Fortran

Implementation and Evaluation of Coarray Fortran Translator Based on OMNI XcalableMP. October 29, 2015 Hidetoshi Iwashita, RIKEN AICS

Our Strategy for Learning Fortran 90

Subroutines and Functions

Programming for High Performance Computing in Modern Fortran. Bill Long, Cray Inc. 17-May-2005

Introduction to Object-Oriented Concepts in Fortran95

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

ParaFEM Coding Standard for Fortran 90. Contents. 1.0 Introduction. 2.0 Documentation. 2.1 External Documentation

VBA Handout. References, tutorials, books. Code basics. Conditional statements. Dim myvar As <Type >

Computational Techniques I

Evolution of Fortran. Presented by: Tauqeer Ahmad. Seminar on Languages for Scientific Computing

C for Engineers and Scientists: An Interpretive Approach. Chapter 10: Arrays

Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill

Lab #10 Multi-dimensional Arrays

AMath 483/583 Lecture 7

An Introduction to Fortran

Arrays in C C Programming and Software Tools. N.C. State Department of Computer Science

Arrays in C. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur. Basic Concept

CSE 230 Intermediate Programming in C and C++ Arrays and Pointers

CS 161 Exam II Winter 2018 FORM 1

LF Fortran 95 Language Reference. Revision G.02

SUBPROGRAMS AND MODULES

Functions. Arash Rafiey. September 26, 2017

Advanced Fortran Programming

Module 5.5: nag sym bnd lin sys Symmetric Banded Systems of Linear Equations. Contents

Transcription:

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

Summary of topics covered Logical Expressions, IF and CASE statements Data Declarations and Specifications ARRAYS and Array Handling Where & Forall statements

Logical and Relational Expressions Logical variables can only take one of two values.true. or.false. Logical Operators are;.or..and..not..eqv..neqv. Example: LOGICAL :: A, B, C A =.TRUE. ; B =.NOT. A ; C= A.AND..NOT.B

Relational expressions Relational expressions are used for comparing the value of one variable with another variable of the same type. The result of a relational operation is a LOGICAL value of either.true. or.false. Relational expressions are classed as logical expressions and can be mixed with other logical expressions RELATIONAL OPERATORS:.LT..LE..EQ..GE..GT..NE. < <= == >= > /=

Logical and Relational expressions EXAMPLES: LOGICAL :: FIRED, LOCKED REAL :: A, B, C INTEGER :: I, J CHARACTER*10 :: NAME1, NAME2 : LOCKED = NAME1.EQ.NAME2.AND. I.LT.J FIRED = A.GE.B.AND. LOCKED

Taking Decisions If Statements IF (logical_expression) executable_statement or IF (logical_expression) THEN block ENDIF or IF ( logical_expression) THEN...block... ELSE block ENDIF [ name]

IF statement: The full syntax label: IF ( logical_expression) THEN...block... ELSE IF ( logic. expr. ) THEN... block... ELSE... block... ENDIF [ name]

Exercises 5 Modify the program you have written earlier about the area of a triangle to make sure that the calculations are performed for only realistic triangles. Hint: For a valid triangle the sum of its two sides must be greater then the length of the remaining side. The answers are given as ex3b, ex3c and ex3d (.f95) files in the answers directory.

CASE constructs Use this construct when options for action fall into a fixed set of choices Syntax SELECT CASE ( expression) CASE ( selector) block CASE ( selector) block : END SELECT

Example Case Statement CHARACTER (LEN=20) :: MONITOR SELECT CASE ( MONITOR) CASE ( E, EGA ) XRES =320 CASE ( V, VG, VGA ) XRES = 480 CASE ( SVGA, SUPERVGA ) XRES =600 CASE DEFAULT! Note that this means anything else XRES = 480 END SELECT

Few usefull compiler flags -c ( compile but not link) -o exefile ( give a name to executable otherwise it is a.out ) -Mfree -Mfixed declare source to be free or fixed form. -Mdclchk Require all variables to be declared. -Mdefaultunit Mnodefaultunit ( Default units behaviour) -Msave -Mnosave : save all local variables -Mbounds Check array bounds during execution -g Produce debugging information -fast Try to optimise the code. -fastsse is even faster on iceberg -r8 Promote reals to Double Precision -g77libs must use this option to link in.o files generated by the g77 compiler (GNU) -lnag -lacml use the NAG library

Exercises 6 Perform Exercises 5 & 6 from the exercise sheet

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 and above) 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 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: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 array_assignments END WHERE Note: ELSE WHERE clause is optional.

REAL :: A(300), B(300) WHERE ( A > 1.0 ) A = 1.0/A WHERE ( A > B) A = B END WHERE Examples 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 ) Value = X (1.5 ) WRONG!!! OK 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 )

Exercises Perform exercises 8.1a and 8.1b from the exercises sheet

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

END OF PART 2