cs1131/35 fortran 90/77 zinstructor: jim allert zemail: [email protected] zphone: 726-7194 zoffice:...

37
CS1131/35 FORTRAN 90/77 Instructor: Jim Allert email: [email protected] phone: 726-7194 office: HH324A office hours: 1:00-4:00 Mon, Wed, Fri Undergraduate TA: R. Haque “Haque” email: rhaque

Upload: marjory-leonard

Post on 03-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

CS1131/35 FORTRAN 90/77

Instructor: Jim Allertemail: [email protected]: 726-7194office: HH324Aoffice hours: 1:00-4:00 Mon, Wed, Fri

Undergraduate TA: R. Haque “Haque”email: rhaque

Page 2: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Why 2 FORTRAN classes?CS-1131 Intro. to Programming in FORTRAN 90

3 credits, full semester covers both FORTRAN 77 and FORTRAN 90

CS-1135 Intro. to Programming in FORTRAN 77 2 credits, course ends after second midterm covers FORTRAN 77 with intro to FORTRAN

90

Page 3: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Lab

Labs are held once a week in SBE 45Programming assignments are due

at the start of lab.An UTA will be present to introduce

the next assignment and help you get started.

All programming will be done using unix compilers (f77 or f90)

Page 4: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Why study FORTRAN?

Traditional choice for science and engineering Over 40 years of legacy code Still language of choice for supercomputing Used to implement many popular

mathematical library routinesIMSL (International Mathematics and Statistics

Library)NAG (Numerical Algorithms Group)

Page 5: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

FORTRAN’s nicheFORTRAN has gone from being a general-

purpose language everyone should learn to a special-purpose language for scientists, mathematicians and engineers.

FORTRAN is most commonly used in mainframe environments (not PCs) the most popular mainframe operating system is

called unix At UMD we have several versions of FORTRAN on

our unix mainframe.

Page 6: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

History of FORTRAN

Important to knowFORTRAN was the first high-level

languageFor decades, it was synonymous with

computer programming

Page 7: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Software

Machine LanguageAssembly LanguageHigh Level Language

FORTRAN (FORmula TRANslation) (1956) COBOL (COmmon Business Oriented Language) (1958) BASIC (Beginners All-purpose Symbolic Instruction Code) (1964) PASCAL (1972) C (1972) C++ (1980) Java (1990)

Page 8: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

FORTRAN

First introduced by IBM in 1956FORTRAN IV - 1962ASA standard FORTRAN 66 - 1966ANSI standard FORTRAN 77 - 1978ANSI and ISO standard FORTRAN 90 -

1991ANSI and ISO standard FORTRAN 95 -

1997

Page 9: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

UNIX

The unix operating system is, by far, the most widespread one on mainframe computers.

Using it requires getting used to a command-line interface

You will have to quickly learn a handful of unix commands

Page 10: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Unix commands

ls - lists your filespico - used to edit your filescat - used to display the contents of a

file on the screen.f77 - the FORTRAN 77 compilerf90 - the FORTRAN 90 compilerscript - creates a typescript of your

session (Very important! More later.)

Page 11: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

More unix commands

cd - changes directoryenscript -2rg -Psbe45 filename -

prints in landscape with 2 pages per page in SBE45

lpr filename - prints a filemkdir - makes a directorybye - logs you off of your account

Page 12: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Still more unix commands

more - types file on screen one page at a time

mv - moves and renames a filepasswd - changes your passwordrm - deletes a filermdir - deletes a directory

Page 13: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Unix commands

umenu - goes to UMD menu systemwhoami - lists the current account

namepine - email program

Page 14: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Software

Machine LanguageAssembly LanguageHigh Level Language

FORTRAN (FORmula TRANslation) (1956) COBOL (COmmon Business Oriented Language) (1958) BASIC (Beginners All-purpose Symbolic Instruction Code) (1964) PASCAL (1972) C (1972) C++ (1980) Java (1990)

Page 15: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

FORTRAN

First introduced by IBM in 1956FORTRAN IV - 1962ASA standard FORTRAN 66 - 1966ANSI standard FORTRAN 77 - 1978ANSI and ISO standard FORTRAN 90 -

1991ANSI and ISO standard FORTRAN 95 -

1997

Page 16: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Fortran Processing

Source File - text fileObject File - generated after

compilingExecutable File - machine language

file

Page 17: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

The unix compilation process

f77 lab1.f

Source code

f77 compiler

lab1.f

Page 18: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Compiler detects errors

f77 lab1.f

Source code

f77 compilerList oferror messages

lab1.f

Page 19: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Successful compilation

f77 lab1.f

Source code

f77 compiler

Objectcode

a.outlab1.f

Page 20: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Linking and loading

f77 lab1.f

Source code

f77 compiler

Objectcode

a.outlab1.f

To theloader.Programruns.

Page 21: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Problems

a.out is a meaningless name for an important file.

A.out is always the default name for the f77 object code file.

Subsequent compilation will destroy old a.out and replace it with a new one. f77 lab2.f

Page 22: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Solution: compiler parameters

f77 -o lab2 lab2.fCompiles lab2.f onto a new file called

‘lab2’ (not a.out)To run it you type

lab2This is much better

the name is more descriptive it doesn’t destroy other object code files you

might want to keep.

Page 23: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Successful compilation

f77 -o lab2 lab2.f

Source code

f77 compiler

Objectcode

lab2lab2.f

Page 24: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

DEBUGGING

TYPES OF ERRORS

SYNTAX (found during compiling)SpellingMissing charactersUsing the wrong

characters

Page 25: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

DEBUGGING

RUN TIME ERRORS (found duringexecution)Example: divide by zero

LOGIC ERRORSExample: using the wrong

variable name

Page 26: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

How to Debug

ISOLATE TO SECTIONADD DIAGNOSTIC PRINT STATEMENT

place near suspected error print current values of variables temporarily remove them with a C or *

in column 1SOME DEBUGGER PROGRAMS

EXECUTE 1 STATEMENT AT A TIME

Page 27: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

COMMON ERRORSForgetting END IF at end of IF

statementForgetting CONTINUE or END DO at

end of DO loopWrong format for DO statementForgetting commas as separatorsPutting characters in the wrong

columnTyping O for zero and vise versaNot terminating DO loops properly

Page 28: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Tracing an Algorithm or Program

Sometimes needed in order to find an error

This is a step by step simulation on paper

Shows the effect of each stepExecute each step exactly as it

would be done by the computer

Page 29: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Structured Programming

Disciplined approach to programming

RESULTS: Programs are easy to read &

understand Less likely to contain errors Easier to maintain & modify

Page 30: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Flow Charts & Diagrams

DecisionProcessingTerminalInputOutputJunction

Page 31: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Elementary FORTRAN 77

All FORTRAN programs consist of data that is manipulated by a set of control structures to produce a result.

Control structures are statements that implement the algorithm steps you have chosen when you designed the program.

Data + algorithms = programs

Page 32: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Command structure rules

FORTRAN 77 (and earlier versions) had a fixed-column method of structuring commands.

Later versions of FORTRAN allow free-format.

Since the fixed format is so common in FORTRAN we will start out writing our programs that way.

Page 33: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Column-based (fixed) structure (f77)

12345678901234567890123456789012345678901234567890123456789012345678901234567890

FORTRAN programs were originally punched oncards. Modern FORTRAN still supports the conventionsthat were used in that day. For example, every FORTRANcommand must obey the following set of rules.

Column 1: reserved for comment marks only. Valid comment marks are c, C or * in f77, f90 adds !Columns 2-5: reserved for statement labels. These are integers used to mark a line so that other statements can get back to it. They are labels, not line numbers.Column 6: reserved for a continuation mark (either a + or a single digit integer 1,2,3,4..etc. These indicate that the line is a continuation of the previous one.Columns 7-72: Your executable FORTRAN statements go hereColumns 73-80: For line sequence numbers. Not used any more.

Page 34: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Column-based structure (f77)

12345678901234567890123456789012345678901234567890123456789012345678901234567890

c this is a comment line, comments start in column 1c* Either a c, C or * may be used to indicate a commentc*********************************************************************** INTEGER i PRINT *, “This long line continues on the next one. To indicate this I place a + in column 6 (the continuation column)” DO 10 (i=1,10) PRINT*, “Hello world!” 10 CONTINUE END

+

Page 35: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Elementary FORTRAN 77

All FORTRAN programs consist of data that is manipulated by a set of control structures to produce a result.

Control structures are statements that implement the algorithm steps you have chosen when you designed the program.

Data + algorithms = programs

Page 36: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Program structure

First you should put in commentsThen specify to the compiler what data

items (variables) you program will need. Give each a name Tell what type of data it is Specify how many (if more than 1)

Then perform the executable statements that act on the data to produce results.

Page 37: CS1131/35 FORTRAN 90/77 zInstructor: Jim Allert zemail: jallert@d.umn.edu zphone: 726-7194 zoffice: HH324A zoffice hours: 1:00-4:00 Mon, Wed, Fri zUndergraduate

Program structure

Comments

Specification(variable declaration)

Execution

c This is a demo programc by mec

integer num

print*, “Enter a number” read*, num print*, “The number you entered is:” print*, num end