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

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

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

AMath 483/583 Lecture 8

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

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

Subroutines, Functions and Modules

Lecture V: Introduction to parallel programming with Fortran coarrays

Review More Arrays Modules Final Review

Bits, Bytes, and Precision

Reusing this material

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

Goals for This Lecture:

Walt Brainerd s Fortran 90 programming tips

NAG Library Routine Document D01BAF.1

Computational Techniques I

Introduction to Fortran Programming. -External subprograms-

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

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

C for Engineers and Scientists

SPECIFICATION 2 STATEMENTS

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

7. Procedures and Structured Programming

AMath 483/583 Lecture 7

An Introduction to Fortran

Review Functions Subroutines Flow Control Summary

Chapter 4. Fortran Arrays

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

Introduction to Modern Fortran

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

Chapter 3. Fortran Statements

Programming Languages

Introduction to Modern Fortran

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

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

NAG Fortran Library Routine Document D01AJF.1

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

PACKAGE SPECIFICATION HSL 2013

AMath 483/583 Lecture 7. Notes: Notes: Changes in uwhpsc repository. AMath 483/583 Lecture 7. Notes:

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

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

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

SUBPROGRAMS AND MODULES

Computational Astrophysics AS 3013

Programming Languages: Lecture 11

Allocating Storage for 1-Dimensional Arrays

Fortran 90 - A thumbnail sketch

multiple variables having the same value multiple variables having the same identifier multiple uses of the same variable

Advanced Fortran Programming

Porting Guide. CUDA Fortran COMPILERS &TOOLS

CUDA Fortran COMPILERS &TOOLS. Porting Guide

NO CALCULATOR ALLOWED!!

AMath 483/583 Lecture 22. Notes: Another Send/Receive example. Notes: Notes: Another Send/Receive example. Outline:

PGI Accelerator Programming Model for Fortran & C

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

Paul F. Kunz. Stanford Linear Accelerator Center. Will try to do the dull stuff quickly, then move into OOP and OO design

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

10. Additional Intrinsic Data Types

PROBLEM SOLVING WITH FORTRAN 90

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

A Brief Introduction to Fortran of 15

In context with optimizing Fortran 90 code it would be very helpful to have a selection of

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Surprising behaviour of Fortran (90/95)

Language Fundamentals

These are reserved words of the C language. For example int, float, if, else, for, while etc.

AMath 483/583 Lecture 18 May 6, 2011

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

D01GCF NAG Fortran Library Routine Document

Problem #3 Daily Lessons and Assessments for AP* Calculus AB, A Complete Course Page Mark Sparks 2012

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

GPU Programming Paradigms

6.001 Notes: Section 17.5

D01ARF NAG Fortran Library Routine Document

Introduction to OpenMP. Martin Čuma Center for High Performance Computing University of Utah

ISO/IEC : TECHNICAL CORRIGENDUM 2

Run-Time Data Structures

G Programming Languages - Fall 2012

Lecture 8: Euler s Methods

MPI: A Message-Passing Interface Standard

An OpenACC construct is an OpenACC directive and, if applicable, the immediately following statement, loop or structured block.

Qualifying Exam in Programming Languages and Compilers

REGCM 4.3 Developer Guide

ME1107 Computing Y Yan.

Introduction to OpenMP

Computer System and programming in C

SC13 GPU Technology Theater. Accessing New CUDA Features from CUDA Fortran Brent Leback, Compiler Manager, PGI

Computers in Engineering. Subroutines Michael A. Hawker

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Size, Alignment, and Value Ranges of Data Types. Type Synonym Size Alignment Value Range. BYTE INTEGER*1 8 bits Byte

Homework #3 CS2255 Fall 2012

Introduction to OpenMP. Martin Čuma Center for High Performance Computing University of Utah

41391 High performance computing: Miscellaneous parallel programmes in Fortran

Introduction to OpenMP

Level 3 Computing Year 2 Lecturer: Phil Smith

Transcription:

Goals for This Lecture: Understand what function subprograms are Understand how to use function subprograms Understand the various kinds of REAL types Understand how to select precision in a processor independent manner

Function Subprograms Thus far we have used intrinsic functions which are built into the language FORTRAN provides a mechanism to define new functions in the form of function subprograms, a.k.a. user-defined functions Form: type function func_name(arg_list) declaration section execution section func_name = expr return end function func_name

Function Example: Midpoint Rule Program! Purpose: Integrate a function via midpoint rule We can create a version of the midpoint rule program that uses functions instead of subroutines Create separate functions for upper & lower limits of integration as well as for integrand Function names must be declared with the appropriate type! 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 real :: upper_limit, lower_limit, integrand! Function subprogs xhi_= upper_limit() xlow_= lower_limit()! Obtain upper limit! Obtain lower limit 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 fi = integrand(xi)! 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

Functions upper_limit and lower_limit supply the limits of integration. Function integrand evaluates the function at a specified point Good programming tip: Use implicit none in function declaration section. Declare function type in both definition and calling program Functions can be contained in modules and can access data in modules via use association! Purpose: Evaluate f(x) = x+abs(sin(2x)) and! supply limits of integration! Author: F. Douglas Swesty! Date: 10/28/2005 real function upper_limit() implicit none! Turn off implicit typing upper_limit = 2.0*3.14159273! Return value return end function upper_limit!------------------------------------------------------------- real function lower_limit() implicit none! Turn off implicit typing lower_limit = 0.0! Return value return end function lower_limit!------------------------------------------------------------- real function integrand(x) implicit none! Turn off implicit typing real, intent(in) :: x! Value of x integrand = x+abs(sin(2.0*x))! Evaluate function return end function integrand

Side Effects of Function Subprograms Input values are passed to function subprograms by the same pass-byreference mechanism that is used for argument passing in subroutines Thus if the function parameters are modified then the corresponding arguments are also changed. This can lead to unintended side effects in programs It is illegal for a function to modify an argument which appears elsewhere in the same expression Good programming tip: Never modify the parameters in a subroutine argument list in a program Always declare arguments in a subroutine with the intent(in) attribute If you need to modify arguments use a subroutine instead of a subprogram

The RESULT clause The return value of the function can be specified through the use of the result clause Form: function func_name(arg_list) result(result_var) type :: result_var result_var = expr return end function func_name The type of the result variable listed in the result clause must be specified in a type declaration specification

Vector-valued functions Using the result clause vector valued functions can be constructed Example: function vec_init(x) result(vf) implicit none real :: x real :: vf(3) return end function vec_init In order to use this function in a calling routine an explicit interface must exist This can be done through an interface block or through the inclusion of the function in a module

Using vector-valued functions with interface block Example: program vfunc_ex1 implicit none real :: x=1.0 real :: xvector(3) interface function vec_init(x) result(vf) implicit none real :: x real :: vf(3) end function vec_init end interface vector = vec_init(x) write(*,*) xvector stop end function vec_init

Using vector-valued functions with modules Example: module vector_funcs contains function vec_init(x) result(vf) implicit none real :: x real :: vf(3) end function vec_init end module vector_funcs program vfunc_ex1 use vector_funcs implicit none real :: x=1.0 real :: xvector(3) vector = vec_init(x) write(*,*) xvector stop end function vec_init

Kinds of REAL variables The default REAL type on most machines is a 32-bit (4 byte) real This usually offers only seven digits of precision and can usually only represent numbers between 10 38 and 10-38 Such 4-byte real values are often referred to as single precision variables Most systems can support a 64-bit (8 byte), a.k.a. double precision, real type Always use double precision whenever possible! We will do so throughout the remainder of this course! The precision can be specified with a KIND= parameter to the real declaration statement real(kind=4) :: x_velocity real(kind=8) :: acceleration There is no guarantee that either single or double precision types will have the same representation of different compilers or machines On some machines double precision may be specified by kind=8 while on others it might be specified by kind=2

Parameterizing your REAL declarations The kind of REAL type can be parameterized through the use of integer parameters contained in modules Makes it trivial to change type declarations by changing one or two lines in a module Example: module real_kinds integer, parameter :: single_type=4 integer, parameter :: double_type=8 end module real_kinds program kind_example use real_kinds implicit none real(kind=single_type) :: x_velocity real(kind=double_type) :: acceleration stop end program kind_example

Parameterizing your REALs in a processor independent manner The fact the single and double precision real types can change from compiler to compiler is still a pain. Fortunately, FORTRAN provides an intrinsic function kind to determine the correct kind parameter based on the specification of a constant Example: module real_kinds integer, parameter :: single_type=kind(1.0) integer, parameter :: double_type=kind(1.0d0) end module real_kinds program kind_example use real_kinds implicit none real(kind=single_type) :: x_velocity real(kind=double_type) :: acceleration stop end program kind_example

Always use double precision whenever possible!

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