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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Intermediate Code Generation
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.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
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 ,
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Application of Fortran 90 to ocean model codes Mark Hadfield National Institute of Water and Atmospheric Research New Zealand.
Arrays Array Terminology Declarations Visualisation of Arrays Array Conformance Array Element Ordering Array Syntax Whole Array Expressions Array Sections.
FORTRAN Short Course Week 2 Kate Thayer-Calder February 23, 2009.
Functions & Subroutines HTML version DFMain.chm. Types of subprograms Internal External Module Pure Elemental Recursive Optional arguments Generic Defined.
© Alan Burns and Andy Wellings, 2001 Programming in the Small n Aim: to remind you how to read/write programs in Ada 95, Java and ANSI C n Practicals will.
Introduction to Fortran Fortran Evolution Drawbacks of FORTRAN 77 Fortran 90 New features Advantages of Additions.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
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 10 Modules and programming with subroutines.
Chapter 9 Modules and Programming with Functions.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
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.
© Alan Burns and Andy Wellings, 2001 Programming in the Small Aim: to remind you how to read/write programs in Ada 95, Java and ANSI C.
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.
 Pearson Education, Inc. All rights reserved Arrays.
1/25 Pointer Logic Changki PSWLAB Pointer Logic Daniel Kroening and Ofer Strichman Decision Procedure.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Fortran: Specification Statements Session Six ICoCSIS.
5.3 Machine-Independent Compiler Features
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
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.
CS 403: Programming Languages Lecture 2 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Multi-Dimensional Arrays
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
A First Book of ANSI C Fourth Edition
Compiler Construction
Fortran: Expressions and Assignments Session Two ICoCSIS.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Module and Data Sharing. Programming in the Large Software, in general, is large having multiple units Multiple units designed and developed independently.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1 Serial Run-time Error Detection and the Fortran Standard Glenn Luecke Professor of Mathematics, and Director, High Performance Computing Group Iowa State.
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.
Compilation Technology SCINET compiler workshop | February 17-18, 2009 © 2009 IBM Corporation Software Group Fortran 2003 Jim Xia IBM Toronto Lab
Copyright Curt Hill Arrays in C/C++ What? Why? How?
FORTRAN 90+ Yetmen Wang Fortran 90/95/2000 INTRODUCTION FORTRAN VERSIONS PROGRAM STRUCTURE NEW SOURCE FORM OO PROGRAMMING ARRAY PROGRAMMING SIGNIFICANT.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Engineering Computing I Chapter 5 Pointers and Arrays.
HPF (High Performance Fortran). What is HPF? HPF is a standard for data-parallel programming. Extends Fortran-77 or Fortran-90. Similar extensions exist.
Fortran: Control Structures Session Three ICoCSIS.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
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.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Windows Programming Lecture 03. Pointers and Arrays.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Programming For Nuclear Engineers Lecture 6 Arrays
Array processing and Matrix manipulation
Midterm Review Programming in Fortran
By Deniz Savas, CiCS, Shef. Univ., 2015
Course Overview PART I: overview material PART II: inside a compiler
Array processing and Matrix manipulation
Presentation transcript:

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 and Assignments 6.Array-valued Functions 7.The where Statement and Construct 8.The forall Statement and Construct 9.Pure Procedures 10.Elemental Procedures 11.Array of Pointers

Introduction Linguistic elements – Enhancing programming – Serves for optimization of performance Libraries with intrinsic procedures – Integral part of programming environment Some definitions: An array consists of a rectangular set of elements, all of the same type and type parameters. The number of elements of an array is called its size real, dimension(-10:5) :: vector An array may extend in more than one dimension, and Fortran allows up to seven dimensions real, dimension(5,4) :: b The number of dimensions of an array is call rank The sequence of extents is call shape.

Zero-sized arrays Fortran allows arrays to have zero size in all contexts. Two zero-sized arrays of the same rank may have different shapes. One might have shape (0,2) and the other (0,3) or (2,0). Such arrays of differing shape are not conformable and therefore may not be used together as the operands of a binary operation. An array is always conformable with a scalar so the statement zero-sized-array = scalar is valid and the scalar is ‘broadcast to all the array elements’, making this a ‘do nothing’ statement. A zero-sized array is regarded as being defined always, because it has no values that can be undefined.

Assumed-shape arrays The shapes of actual and dummy arguments agree, but it is possible to require that the shape of the dummy array be taken automatically to be that of the corresponding actual array argument. When the shape is declared by the dimension clause, each dimension has the form [lower-bound]: If lower-bound is omitted, the default value is 1. Note that it is the shape that is passed, and not the upper and lower bounds. For example, if the actual array is a, declared as: real, dimension(0:10, 0:20) :: a and the dummy array is da, declared as: real, dimension(:, :) :: da then a(i,j) corresponds to da(i+1,j+1). To get the natural correspondence, the lower bound must be declared: real, dimension(0:, 0:) :: da Note: A dummy array with the pointer attribute is not regarded as an assumed-shape array because its shape is not necessarily assumed.

Automatic objects A procedure with dummy arguments that are arrays whose size varies from call to call may also need local arrays whose size varies. A simple example is the array work in the subroutine to interchange two arrays. subroutine swap(a, b) real, dimension(:), intent(inout) :: a, b real, dimension(size(a)) :: work ! automatic array ! size provides the size of an array work = a; a = b; b = work end subroutine swap The other way that automatic objects arise is through varying character length. subroutine example(word1) character(len = *), intent(inout) :: word1 character(len = len(word1)) :: word2

Allocation of data (1) The allocatable attribute – if an array is required to be of a size that is known only after some data have been read or some calculations performed: real, dimension(:, :), allocatable :: a deallocate (a) The allocate statement – to give fresh storage for a pointer target directly. allocate ( allocation-list [, stat=stat ] ) where allocation-list is a list of allocations of the form allocate-object [ ( array-bounds-list ) ] each array-bound has the form [ lower-bound : ] upper-bound and stat is a scalar integer variable that must not be part of an object being allocated. The deallocate statement deallocate ( allocate-object-list [,stat=stat] ) Allocatable dummy arguments – a dummy arrray is permitted to have the allocatable attribute. In this case, the corresponding actual argument must be an allocatable array of the same type. Allocatable functions – an array function result is permitted to have the allocatable attribute. Allocatable components – array components of derived type are permitted to have the allocatable attribute.

Allocation of data (2) Allocatable arrays vs. pointers Code for a pointer array is likely to be less efficient If a defined operation involves a temporary variable of a derived type with a pointer component, the compiler will probably be unable to deallocate its target when storage for the variable is freed. Intrinsic assignment is often unsuitable for a derived type with a pointer. Similar considerations apply to a function invocation within an expression. When a variable of derived type is deallocated, any ultimate allocatable component that is currently allocated is also deallocated. To avoid memory leakage with pointer components, the programmer would need to deallocate each one explicitly and be careful to order the deallocations correctly.

Elemental operations and assignments An intrinsic operator can be applied to conformable operands, to produce an array result whose element values are the values of the operation applied to the corresponding elements of the operands. Such an operation is called elemental. For a function, the shape of the result is the shape of the array arguments. For example, we may find the square roots of all the elements of a real array thus: a = sqrt(a)

Array-valued functions A function may have an array-valued result. An array-valued function is not necessarily elemental.

The where statement and construct It is often desired to perform an array operation only for certain elements, say those whose values are positive. The where statement provides this facility. A simple example is where ( a > 1.0 ) a = 1.0/a ! a is a real array which reciprocates those elements of a that are greater than 1.0 and leaves the rest unaltered. The general form is where (logical-array-expr) array-variable = expr

The forall statement and construct When elements of an array are assigned values by a do construct such as do i = 1, n a(i, i) = 2.0 * x(i) ! a is rank-2 and x rank-1 end do the processor is required to perform each successive iteration in order and one after the other. This represents a potentially severe impediment to optimization on a parallel processor so, for this purpose, Fortran has the forall statement. The above loop can be written as forall(i = 1:n) a(i, i) = 2.0 * x(i)

Pure procedures To assert that a procedure has no side-effects – add the pure keyword to the subroutine or function statement. In practical terms, this is an assertion that the procedure if a function, does not alter any dummy argument; does not alter any part of a variable accessed by host or use association; contains no local variable with the save attribute; performs no operation on an external file; and Contains no stop statement. pure function distance(p, q)

Elemental procedures Elemental intrinsic procedures are those with scalar dummy arguments that may be called with array actual arguments provided that the array arguments have the same shape (that is, provided all the arguments are conformable). For a function, the shape of the result is the shape of the array arguments. This feature exists too for non-intrinsic procedures. This requires the elemental prefix on the function or subroutine statement. This is an aid to optimization on parallel processors. elemental function add_intervals(a,b) type(interval) :: add_intervals type(interval), intent(in) :: a, b add_intervals%lower = a%lower + b%lower ! Production code add_intervals%upper = a%upper + b%upper ! would allow for end function add_intervals ! roundoff. An elemental procedure must satisfy all the requirements of a pure procedure

Array elements An array element is a scalar of the form part-ref [%part-ref]... where part-ref is part-name[(subscript-list)] To illustrate this, take the type type triplet real :: u real, dimension(3) :: du real, dimension(3,3) :: d2u end type triplet An array may be declared of this type: type(triplet), dimension(10,20,30) :: tar and tar(n,2,n*n) ! n of type integer is an array element. It is a scalar of type triplet and tar(n, 2, n*n)%du is a real array with tar(n, 2, n*n)%du(2)as one of its elements

Arrays of pointers Although arrays of pointers as such are not allowed in Fortran, the equivalent effect can be achieved by creating a type containing a pointer component. type ptr type(entry), pointer :: point end type ptr type entry real :: value integer :: index type(ptr), pointer :: children(:) end type entry