Goals for This Lecture:

Similar documents
Goals for This Lecture:

Goals for This Lecture:

Goals for This Lecture:

Programming Languages

Review More Arrays Modules Final Review

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Goals for This Lecture:

Introduction to Fortran Programming. -Internal subprograms (1)-

Subroutines, Functions and Modules

7. Procedures and Structured Programming

Lecture V: Introduction to parallel programming with Fortran coarrays

G Programming Languages - Fall 2012

Chapter 4. Fortran Arrays

Fortran 90 - A thumbnail sketch

Merge Sort. Run time typically depends on: Insertion sort can be faster than merge sort. Fast, able to handle any data

PROBLEM SOLVING WITH FORTRAN 90

PACKAGE SPECIFICATION HSL 2013

SUBPROGRAMS AND MODULES

Fortran. (FORmula TRANslator) History

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

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

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

ISO/IEC : TECHNICAL CORRIGENDUM 2

NAG Library Routine Document G05PZF.1

NAGWare f95 and reliable, portable programming.

Reusing this material

Chapter 3. Fortran Statements

FORTRAN 90: Functions, Modules, and Subroutines. Meteorology 227 Fall 2017

Subroutines and Functions

An Introduction to Fortran

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Exercises Lecture III: Random numbers with non uniform distributions; simulations of simple random processes

Information technology Programming languages Fortran Part 1: Base language

Review Functions Subroutines Flow Control Summary

41391 High performance computing: Miscellaneous parallel programmes in Fortran

AMath 483/583 Lecture 8

Introduction to Programming with Fortran 90

Co-arrays to be included in the Fortran 2008 Standard

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

Concepts Introduced in Chapter 7

Introduction to Modern Fortran

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

ENERGY 211 / CME 211. Functions

Fortran functions: some examples

PGI Accelerator Programming Model for Fortran & C

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Watcom FORTRAN 77. Language Reference. Edition 11.0c

NAGWare f95 Recent and Future Developments

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

NO CALCULATOR ALLOWED!!

190 Lecture 14 Arrays, allocation

Computers in Engineering. Subroutines Michael A. Hawker

G. Colin de Verdière CEA

TS Further Interoperability of Fortran with C WG5/N1917

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

G Programming Languages - Fall 2012

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Final thoughts on functions F E B 2 5 T H

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Parallel Programming in Fortran with Coarrays

UNIT V Sub u P b ro r g o r g a r m a s

1 The sorting problem

(Refer Slide Time 01:41 min)

Reflection Seismology (SCPY 482) An Introduction to Fortran 90 Programming

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

Computational Techniques I

Extrinsic Procedures. Section 6

Templates and Generic Programming

Level 3 Computing Year 2 Lecturer: Phil Smith

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

INTRODUCTION TO FORTRAN PART II

eccodes BUFR decoding

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

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

CS 3360 Design and Implementation of Programming Languages. Exam 1

Fortran Bill Long, Cray Inc. 21-May Cray Proprietary

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

Lecture 16: Static Semantics Overview 1

NAG Library Routine Document H02CFF.1

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Allocating Storage for 1-Dimensional Arrays

UNIT 3

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

Chapter 3:: Names, Scopes, and Bindings (cont.)

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

Chapter 3:: Names, Scopes, and Bindings (cont.)

Computers in Engineering COMP 208. Subprograms. Subroutines. Subroutines Michael A. Hawker

Memory management COSC346

NAG Library Routine Document G05REF.1

Storage and Sequence Association

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

NAG Library Routine Document G05RGF.1

Report from WG5 convener

Special Topics: Programming Languages

University of Kelaniya Sri Lanka

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Chapter 3:: Names, Scopes, and Bindings

A Brief Introduction to Fortran of 15

REGCM 4.3 Developer Guide

Transcription:

Goals for This Lecture: Understand what modules are Understand what module procedures are and how to use them Understand explicit and implicit interfaces Understand what automatic arrays are and how to use them

Sharing Data Using Modules Argument lists are one way of sharing data objects between calling programs and subprograms Another mechanism that FORTRAN provides to accomplish data sharing is the module Modules are external containers where variables defined outside of subprograms and main programs Program units can use the modules to gain access to data Modules are a way to share data between procedures without passing data via argument lists

A Module Example W/ Module W/O Module module example_1 save real :: x(10) end module example_1 program sub_ex3 use example_1 call arr_sub() stop end program sub_ex3 subroutine arr_sub() x = 2.0 return end subroutine arr_sub program sub_ex3 real :: x(10)=1.0 interface subroutine arr_sub(a) real :: a(:) end subroutine arr_sub end interface call arr_sub(x) stop end program sub_ex3 subroutine arr_sub(a) integer :: n real :: a(:) n = size(a,dim=1) a(1:n) = 2.0 return end subroutine arr_sub

Form: module module_name declaration section end module module_name Modules Module is accessed from a procedure via the use statement Form: use module_name Any use statements must precede any statements The use of the save statement in the module guarantees that data values are preserved between references to the module by different routines. Alternatively the variables in the module could have been declared with the save attribute. Modules need to be compiled before the routines that access them are compiled

Module Procedures Modules can contain subprograms in addition to data These are often referred to as module procedures Example: module array_sort contains subroutine bubble_sort(x) real :: tmpv, x(:) integer :: npnts, i, j npnts = size(x,dims=1) do i = 1,npts do j = 1,npnts-1,1 if(x(j) > x(j+1)) then tmpv = x(j) x(j) = x(j+1) x(j+1) = tmpv endif enddo enddo return end suboutine end module array_sort

Modules Form: module module_name declaration section contains subroutine sub1(arg_list1) end subroutine sub1 subroutine sub2(arg_list2) end subroutine sub2 end module module_name The contains statement signals that subsequent code defines procedures that are to be contained within the module If the module is accessed from the main program via a use module_name statement then the subroutines can be called as they would normally

! Purpose: Sort a data set! Author: F. Douglas Swesty! Date: 10/26/2005 program data_sort use array_sort interface routine) Module procedures avoid use of interface blocks! Use the array_sort module (no! block needed for bubble_sort do! Find number of points read(lun1,*,iostat=ierror) tmpv! Read in array if(ierror /= 0) exit npts = npts+1 enddo allocate(x(npts))! Allocate array to correct size! Turn off implicit typing integer, parameter :: lun1=11! Define file LUN integer, parameter :: npts=0! Number data pts real,allocatable :: x(:)! Array to hold data integer :: i! Loop index integer :: ierror! I/O error status real :: tmpv! Temporary var.! Open the file open(unit=lun1,file= exp1.dat,status= OLD,iostat= ierror) if(ierror /= 0) then write(*,*) Could not read in file! stop endif rewind(lun1)! Rewind the file to beginning do i= 1,npts,1! Read data into the array read(lun1,*) x(i) enddo close(unit=lun1)! Close the file call bubble_sort(x)! Sort the array write(*,*) sorted list: do i=1,npts! Loop over data set write(*,*) i,x(i) enddo stop end program data_sort

Module Procedures & Interfaces The process of accessing information contained within a module by means of a use statement is called use association Why employ modules to contain subroutines? When a subprogram is accessed via use association the details of the interface are made known to the compiler when the calling program is compiled. A procedure compiled within a module is said to have an explicit interface Detailed information about the interface is available to any program unit accessing the procedure through use association Procedures compiled outside a module are said to have an implicit interface Only limited information about the procedure interface is known to then compiler when compiling the calling program unit It just assumes that the number, type, intent, etc. of the arguments is OK Some intrinsic functions such as size, ubound, lbound, etc. may not work correctly in procedures with implicit interfaces

Automatic Arrays Occasionally it is desireable to quickly and easily create an array within a subprogram that is dynamic in size but which does not appear in the argument list. FORTRAN provides a way to do this through the mechanism of automatic arrays. The array is an explicit shape array whose dimensions are specified in terms of an integer variable, or integer expression based on a variable, that is in the argument list or which is accessed by use association. Example: subroutine sub_ex(n) real :: x(n) return end subroutine sub_ex

Automatic Arrays vs. Allocatable Arrays Automatic arrays & allocatable arrays can both be used to create temporary arrays Automatic arrays are allocated whenever the routine in which they are defined is entered and deallocated whenever the routine is exited. Allocatable arrays must be allocated & deallocated manually. Allocatable arrrays are more flexible They can be created in main programs Arrays can be created and destroyed in separate routines Allocatable arrays can be contained within modules Allocatable arrays can create memory leaks An array allocated in a routine and not deallocated ties up the memory associated with that array until the program terminates.

Reading Assignment Sections 7.4, 7.5, 11.1-11.2 of Chapman