Goals for This Lecture:

Similar documents
Goals for This Lecture:

Goals for This Lecture:

Goals for This Lecture:

Goals for This Lecture:

Subroutines and Functions

Subroutines, Functions and Modules

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

ECE 3610 MICROPROCESSING SYSTEMS

1 The sorting problem

Reusing this material

41391 High performance computing: Miscellaneous parallel programmes in Fortran

Introduction to Fortran Programming. - Module -

7. Procedures and Structured Programming

SUBPROGRAMS AND MODULES

Review More Arrays Modules Final Review

LECTURE 17. Array Searching and Sorting

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Lecture V: Introduction to parallel programming with Fortran coarrays

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

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

Review Functions Subroutines Flow Control Summary

PACKAGE SPECIFICATION HSL 2013

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

AMath 483/583 Lecture 8

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

Programming Languages

Computers in Engineering. Subroutines Michael A. Hawker

NO CALCULATOR ALLOWED!!

Introduction to Fortran Programming. -External subprograms-

Applications of Integration. Copyright Cengage Learning. All rights reserved.

Notice that the height of each rectangle is and the width of each rectangle is.

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

Lecture 16: Static Semantics Overview 1

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

Exercise 1.1 Hello world

Computer Science & Engineering 150A Problem Solving Using Computers

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

Advanced Fortran Programming

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

Iterative Languages. Scoping

To become familiar with array manipulation, searching, and sorting.

SUBROUTINE subroutine-name (arg1, arg2,..., argn)

write (unit=*,fmt=*) i =, i! will print: i = 3

by Pearson Education, Inc. All Rights Reserved.

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

PGI Accelerator Programming Model for Fortran & C

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

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Interfacing With Other Programming Languages Using Cython

Elementary maths for GMT. Algorithm analysis Part II

Computational Physics - Fortran February 1997

FORTRAN Block data subprograms 2. Common blocks 3. Entry points 4. Function subprograms 5. Main program 6. Subroutines

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

INF 212 ANALYSIS OF PROG. LANGS PROCEDURES & FUNCTIONS. Instructors: Kaj Dreef Copyright Instructors.

Extrinsic Procedures. Section 6

Rectangle Sums

Programmation in Fortran

FROM SERIAL FORTRAN TO MPI PARALLEL FORTRAN AT THE IAS: SOME ILLUSTRATIVE EXAMPLES

Introduction to Modern Fortran

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

Chapter 3. Fortran Statements

A Brief Introduction to Fortran of 15

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Variable A variable is a value that can change during the execution of a program.

Elementary Parallel Programming with Examples. Reinhold Bader (LRZ) Georg Hager (RRZE)

Functions. Lecture 6 COP 3014 Spring February 11, 2018

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

A. Run-Time Error Messages

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

Lecture 7. OpenMP: Reduction, Synchronization, Scheduling & Applications

AMath 483/583 Lecture 18 May 6, 2011

Fortran. (FORmula TRANslator) History

6.1 Expression Evaluation. 1 of 21

ISO/IEC : TECHNICAL CORRIGENDUM 2

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Goals for This Lecture:

Programming in OOP/C++

Procedure Call Instructions

Searching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1

Basic Fortran Programming. National Computational Infrastructure

G Programming Languages - Fall 2012

NAG Fortran Library Routine Document D01AJF.1

D01GCF NAG Fortran Library Routine Document

OpenMP - II. Diego Fabregat-Traver and Prof. Paolo Bientinesi WS15/16. HPAC, RWTH Aachen

C/C++ Programming Lecture 18 Name:

Chapter 4. Fortran Arrays

AMath 483/583 Lecture 7

Data Structures and Algorithms Key to Homework 1

(Following Paper ID and Roll No. to be filled by the student in the Answer Book)

CS311 Lecture: The Architecture of a Simple Computer

OpenMP programming. Thomas Hauser Director Research Computing Research CU-Boulder

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

International Journal Of Engineering Research & Management Technology

NAG Library Routine Document D01BAF.1

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Fortran functions: some examples

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

DRAFT. ATMET Technical Note. Common Customizations for RAMS v6.0. Number 2. Prepared by: ATMET, LLC PO Box Boulder, Colorado

Transcription:

Goals for This Lecture: Learn a simple sorting algorithm Understand how compilation & linking of separate main program and subprogram files are accomplished. Understand how to use subprograms to create reusable programs Understand the save attribute and save statement and how it affects local variables Introduce the subject of modules

Example using assumed shape arrays: sorting a data set AST/PHY 277 Lecture 24! Purpose: Sort a data set! Author: F. Douglas Swesty! Date: 10/26/2005 program data_sort implicit none! Turn off implicit typing integer, parameter :: lun1=11! Define an LUN for file integer, parameter :: npts=0! Number of data points real,allocatable :: x(:)! Define array to hold data integer :: i! Loop index integer :: ierror! I/O error status variable interface! Define the explicit interface to the subroutine subroutine bubble_sort(x) implicit none real :: x(:) end subroutine bubble_sort end interface do! Find number of points read(lun1,*,iostat=ierror) tmpv! Read in data into array if(ierror /= 0) exit npts = npts+1 enddo allocate(x(npts))! Allocate array to correct size 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! 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 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

A simple sorting algorithm: The bubble sort AST/PHY 277 Lecture 24 1. for i=1,2,,n 2. for j=1,2,,n-1 3. compare elements x(j) & x(j+1) 4. if(x(j) > x(j+1)) then swap x(j) & x(j+1) 5. end of inner loop 6. end of outer loop Biggest value bubbles to the top of array The bubble sort is not the fastest sorting algorithm but it is easy to encode Therefore we ll use it for this example

A bubble sort subroutine! Purpose: Bubble sort an array! Author: F. Douglas Swesty! Date: 10/26/2005 subroutine bubble_sort(x) implicit none! Turn off implicit typing integer :: npts! Number of data points real :: x(:)! Define an assumed size array real :: tmpv! Temporary variable integer :: i, j! Loop indices npts = size(x,dim=1)! Get size of assumed size array! Execute a bubble sort of array do i=1,npts! Repeat bubbling task npts times do j=1,npts-1,1! Bubble bigger values to the top if(x(j) > x(j+1) ) then! Swap elements j & j+1 tmpv = x(j) x(j) = x(j+1) x(j+1) = tmpv endif enddo enddo return! Return to calling routine end subroutine bubble_sort

Separate Compilation of Subprograms Subprograms can be placed in a separate file and compiled separately from the main program. The compilation of each file produces a object file ending in.o The two object files can be linked together with the compiler to produce an executable Example: ifort c main.f90 ifort c subprogram.f90 ifort o main main.o subprogram.o This allows flexibility in combining subprograms with main programs

Subroutine Example 2: Midpoint Rule Program Subprograms can be used to evaluate a function needed for a numerical method. Having the function placed in a separate file and compiled separately from the main program allows the development of generic numerical method codes. Example: Midpoint Rule Subroutines to supply integration limits and evaluate function Place subroutines in separate, problemspecific file! Purpose: Integrate a function via midpoint rule! Author: F. Douglas Swesty! Date: 10/28/2005 program midpoint implicit none! Turn off implicit typing integer, parameter :: n=100! Number of subintervals integer :: i! Loop index real :: xlow, xhi! Bounds of integral real :: dx! Variable to hold width of subinterval real :: sum=0.0! Variable to hold sum real :: xi! Variable to hold location of ith subinterval real :: fi! Variable to value of function at ith subinterval call limits(xhi,xlow)! Obtain limits dx = (xhi-xlow)/(1.0*n)! Calculate with of subinterval xi = xlow+0.5*dx! Initialize value of xi do i = 1,n,1! Initiate loop call integrand(xi,fi)! Evaluate function at ith point sum = sum+fi*dx! Accumulate sum xi = xi+dx! Increment location of ith point enddo! Terminate loop write(*, (t2,a13,1x,1es12.5) ) integral =,integral stop! Stop execution of the program end program midpoint AST/PHY 277 Lecture 24

Subroutine limits supplies the limits of integration. Subroutine integrand evaluates the function at a specified point Placing these two routines in the same file allows all problem-specific information to be contained in a single file that is separate from the problem-independent midpoint rule code. Solving a different problem means creating a new set of limits & integrand subroutines! Purpose: Evaluate f(x) = x+abs(sin(2x)) and! supply limits of integration! Author: F. Douglas Swesty! Date: 10/28/2005 subroutine limits(xlow,xhi) implicit none! Turn off implicit typing real, intent(out) :: xlow, xhi! Bounds of integral xlow = 0.0! Lower limit of integration xhi = 2.0*3.14159273! Upper limit of integration return end subroutine limits!------------------------------------------------------------- subroutine integrand(x,fofx) implicit none! Turn off implicit typing real, intent(in) :: x! Value of x real, intent(out) :: fofx! Value of function fofx = x+abs(sin(2.0*x))! Evaluate function return end subroutine integrand

A Fact About Local Variables According to the FORTRAN 90, 95, and 2003 standards the values of local variables in subroutines become undefined when the procedure in which they are defined is exited. FORTRAN provides a way to preserve the values with the save attribute and save statement Example of save attribute use: real, save :: x_position Example of save statement use: save :: x_position, y_position Values of variables x_position & y_position are preserved between accesses of the procedure in which they are defined If no variables are listed in the save statement then all local variables have their values preserved between accesses of the procedure in which they are defined

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 module example_1 save real :: x(10) end module example_1 program sub_ex3 use example_1 implicit none call arr_sub() stop end program sub_ex3 subroutine arr_sub() use example_1 implicit none x = 2.0 return end subroutine arr_sub W/O Module program sub_ex3 implicit none real :: x(10)=1.0 interface subroutine arr_sub(a) implicit none real :: a(:) end subroutine arr_sub end interface call arr_sub(x) stop end program sub_ex3 subroutine arr_sub(a) implicit none 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 implicit none 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.

Reading Assignment Note: Mid-Term Examination will be closed book Sections 7.4-7.6, 9.2-9.3 of Chapman