cs1131/35 fortran 90/77 zinstructor: jim allert zemail: [email protected] zphone: 726-7194 zoffice:...
TRANSCRIPT
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
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
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)
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)
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.
History of FORTRAN
Important to knowFORTRAN was the first high-level
languageFor decades, it was synonymous with
computer programming
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)
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
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
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.)
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
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
Unix commands
umenu - goes to UMD menu systemwhoami - lists the current account
namepine - email program
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)
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
Fortran Processing
Source File - text fileObject File - generated after
compilingExecutable File - machine language
file
The unix compilation process
f77 lab1.f
Source code
f77 compiler
lab1.f
Compiler detects errors
f77 lab1.f
Source code
f77 compilerList oferror messages
lab1.f
Successful compilation
f77 lab1.f
Source code
f77 compiler
Objectcode
a.outlab1.f
Linking and loading
f77 lab1.f
Source code
f77 compiler
Objectcode
a.outlab1.f
To theloader.Programruns.
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
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.
Successful compilation
f77 -o lab2 lab2.f
Source code
f77 compiler
Objectcode
lab2lab2.f
DEBUGGING
TYPES OF ERRORS
SYNTAX (found during compiling)SpellingMissing charactersUsing the wrong
characters
DEBUGGING
RUN TIME ERRORS (found duringexecution)Example: divide by zero
LOGIC ERRORSExample: using the wrong
variable name
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
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
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
Structured Programming
Disciplined approach to programming
RESULTS: Programs are easy to read &
understand Less likely to contain errors Easier to maintain & modify
Flow Charts & Diagrams
DecisionProcessingTerminalInputOutputJunction
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
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.
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.
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
+
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
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.
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