# introduction to fortran 90 - prace research to fortran 90 ... fortran 66 i the ﬁrst of all...

Post on 07-Feb-2018

217 views

Embed Size (px)

TRANSCRIPT

Introduction to Fortran 90

F. Salvadore I. Baccarelli G. AmatiCINECA Roma - SCAI Department

Roma, 20-23 January 2014

Part I

A Fortran Survey 1

Program main unit, source formats, comments, declarationsand instructions. Fundamental operators, expressions,conditional constructs, loops, functions: arguments passing,intent, interface, intrinsic and external functions. Modules:contains and use. Intrinsic types: integer, real, complex, logical,and parameter. I/O base.

Outline

Introduction

Fortran Basics

More Fortran Basics

Integer Types and Iterating

More on Compiling and Linking

Homeworks

Formula Translator HistoryI Developed in the 50s among the earliest high level languages

(HLL)I Widely and rapidly adopted in the area of numerical, scientific,

engineering and technical applicationsI First standard in 1966: Fortran 66

I The first of all programming language standardsI Second standard in 1978: Fortran 77I Third standard in 1991: Fortran 90

I Adds new, modern features such as structured constructs, arraysyntax and ADT

I Extended and revised in 1997: Fortran 95I Further extended with published Technical Reports

I Fourth standard in 2004: Fortran 2003I Major revision, incorporates TRs, adds many new features

(OO!), still not fully supportedI Fifth standard in 2010: Fortran 2008

Fortran General Philosophy

I Strongly oriented to number crunchingI Efficient language, highly optimized code

I Basic data types and operators mapping "naturally" to CPUsI Translated by a compiler to machine languageI Language rules allow for aggressive, automatic optimizationI Facilities to build new data types from the basic onesI Flexible flow control structures mapping the most common

numerical computing use casesI Scientific computing specialized syntax

I A wealth of math data types and functions available as intrinsicsof the language

I Compact, readable array syntax to operate on many values as awhole

Technical and Scientific Computing

I Why Fortran is badI Current standard embodies four different language versions,...I ... all of them still alive in legacy codesI Non-numeric computing in Fortran is a real painI There are more C than Fortran programmersI GUI and DB accesses are best programmed in CI C99 partly addressed numerical computing needs

I Why Fortran is goodI Fortran is highly tuned for numerical computationI Fortran is older and more rigid than C, compilers optimize

betterI Much better than C at managing user defined data typesI Object-oriented features are now part of the languageI Provides facilities for interoperability with C and other languages

Technical and Scientific Computing

I Why Fortran is badI Current standard embodies four different language versions,...I ... all of them still alive in legacy codesI Non-numeric computing in Fortran is a real painI There are more C than Fortran programmersI GUI and DB accesses are best programmed in CI C99 partly addressed numerical computing needs

I Why Fortran is goodI Fortran is highly tuned for numerical computationI Fortran is older and more rigid than C, compilers optimize

betterI Much better than C at managing user defined data typesI Object-oriented features are now part of the languageI Provides facilities for interoperability with C and other languages

Our Aims

I Teach you the fundamentals of modern FortranI For both reading (old and new) and writing (new) programsI Showing common idiomsI Illustrating and demonstrating many of the extensions

introduced in the more recent standardsI Illustrating best practicesI Blaming bad onesI Making you aware of the typical traps

I Youll happen to encounter things we didnt cover, but it will beeasy for you to learn more... or to attend a more advancedcourse!

I A course is not a substitute for a reference manual or a goodbook!

I Neither a substitute for personal practice

Our Aims

I Teach you the fundamentals of modern FortranI For both reading (old and new) and writing (new) programsI Showing common idiomsI Illustrating and demonstrating many of the extensions

introduced in the more recent standardsI Illustrating best practicesI Blaming bad onesI Making you aware of the typical trapsI Youll happen to encounter things we didnt cover, but it will be

easy for you to learn more... or to attend a more advancedcourse!

I A course is not a substitute for a reference manual or a goodbook!

I Neither a substitute for personal practice

Our Aims

I Teach you the fundamentals of modern FortranI For both reading (old and new) and writing (new) programsI Showing common idiomsI Illustrating and demonstrating many of the extensions

introduced in the more recent standardsI Illustrating best practicesI Blaming bad onesI Making you aware of the typical trapsI Youll happen to encounter things we didnt cover, but it will be

easy for you to learn more... or to attend a more advancedcourse!

I A course is not a substitute for a reference manual or a goodbook!

I Neither a substitute for personal practice

Outline

Introduction

Fortran BasicsMy First Fortran ProgramCompiling and Linking Your First ProgramMaking ChoicesMore Types and ChoicesWrapping it Up 1

More Fortran Basics

Integer Types and Iterating

More on Compiling and Linking

Homeworks

Outline

Introduction

Fortran BasicsMy First Fortran ProgramCompiling and Linking Your First ProgramMaking ChoicesMore Types and ChoicesWrapping it Up 1

More Fortran Basics

Integer Types and Iterating

More on Compiling and Linking

Homeworks

My First Scientific Program in Fortran

! roots of a 2nd degree equation with real coefficients

program second_degree_eqimplicit nonereal :: deltareal :: x1, x2real :: a, b, c

print *,Solving ax^2+bx+c=0, enter a, b, c:read (*,*) a, b, c

delta = sqrt(b**2 - 4.0*a*c) ! square root of discriminantx1 = -b + deltax2 = -b - deltax1 = x1/(2.0*a)x2 = x2/(2.0*a)

write(*,*) Real roots:, x1, x2

end program second_degree_eq

My First Scientific Program in Fortran

! roots of a 2nd degree equation with real coefficients

program second_degree_eqimplicit nonereal :: deltareal :: x1, x2real :: a, b, c

print *,Solving ax^2+bx+c=0, enter a, b, c:read (*,*) a, b, c

delta = sqrt(b**2 - 4.0*a*c) ! square root of discriminantx1 = -b + deltax2 = -b - deltax1 = x1/(2.0*a)x2 = x2/(2.0*a)

write(*,*) Real roots:, x1, x2

end program second_degree_eq

Comments

I Text following ! is ignored up to the end of current line

I Best practice: do comment your code!

I Variable contentsI AlgorithmsI AssumptionsI Tricks

I Best practice: do not over-comment your code!

I Obvious comments obfuscate code and annoy readersI ! square root of discriminant is a bad example

Comments

I Text following ! is ignored up to the end of current line

I Best practice: do comment your code!I Variable contentsI AlgorithmsI AssumptionsI Tricks

I Best practice: do not over-comment your code!

I Obvious comments obfuscate code and annoy readersI ! square root of discriminant is a bad example

Comments

I Text following ! is ignored up to the end of current line

I Best practice: do comment your code!I Variable contentsI AlgorithmsI AssumptionsI Tricks

I Best practice: do not over-comment your code!I Obvious comments obfuscate code and annoy readersI ! square root of discriminant is a bad example

My First Scientific Program in Fortran

! roots of a 2nd degree equation with real coefficients

program second_degree_eqimplicit nonereal :: deltareal :: x1, x2real :: a, b, c

print *,Solving ax^2+bx+c=0, enter a, b, c:read (*,*) a, b, c

delta = sqrt(b**2 - 4.0*a*c)

! square root of discriminant

x1 = -b + deltax2 = -b - deltax1 = x1/(2.0*a)x2 = x2/(2.0*a)

write(*,*) Real roots:, x1, x2

end program second_degree_eq

Program Units: Main Program

I Fortran code is organized in program unitsI Main programI Procedures (subroutines and functions)I ModulesI More on this later...

I The main program (one, and only one!) cant be dispensedwith

I Its called automatically to execute the programI An optional program program-name can appear at the

beginningI An end statement must terminate it, optionally followed byprogram or program program-name

I Best practice: always mark unit beginning and ending with itstype and name

I Makes your readers (including you) happier

Program Units: Main Program

I Fortran code is organized in program unitsI Main programI Procedures (subroutines and functions)I ModulesI More on this later...

I The main program (one, and only one!) cant be dispensedwith

I Its called automatically to execute the programI An optional program program-name can appear at the

beginningI An end statement must terminate it, optionally followed byprogram or program program-name

I Best practice: always mark unit beginning and ending with itstype and name

I Makes your readers (including you) happier

Program Units: Main Program

I Fortran code is organized in program unitsI Main programI Procedures (subroutines and functions)I ModulesI More on this later...

I The main program (one, and only one!) cant be dispensedwith

I Its called automatically to execute the programI An optional program program-name can appear at the

beginningI An end statement must terminate it, optionally follo

Recommended