Title: FORTRAN Short Course Week 2
1FORTRAN Short CourseWeek 2
- Kate Thayer-Calder
- September 01, 2009
2Topics for this week
- Unix file manipulation
- Pipes and Redirection
- Logical execution control
- Relational and logical operators
- DO loops
- Processes and how to kill them
- Whole array manipulators
- Subroutines and Functions
- Scoping Variables
3The Machine Clock
- There is a little electronic heartbeat in the
processor called the clock, that switches on and
off - Each time the clock goes on, the processor gets
another chunk of 1s and 0s from memory and
sends them down the pipeline - Effectively, the computer can do one operation
per cycle. - If you are pulling from a slower part of memory,
the machine might sit idle for several cycles
waiting. - The speed of your processor is the speed of your
clock - 2 GHz On/Off 2x109 (2 billion) s-1
4FLOPS
- A FLOP is a FLoating point OPeration
- Because Floating point numbers have two parts and
extreme precision requirements, multiplication
and division with floats can take several cycles
to do one operation. - Supercomputers mostly do floating point math, so
their performance is measured in FLOPs instead of
cycles. - Recently, computers were created that produce
PETA-FLOP speed (1015 flops/s - 1 quadrillian, or
1,000 trillian operations each second)
5To Do Move and Rename A File, combine with
another
- cd Documents/FortranClass
- ls -l
- ls -l more
- cp MyDataFile.txt /code/FortranCourse/
- cd /code/FortranCourse
- ls
- pwd
- mv MyDataFile.txt KatesData.txt
- cat KatesData.txt FTCTempsLong.txt gt
LotsOData.txt - cat LotsOData.txt wc
- rm LotsOData.txt
6ls -l more
- We saw LS last week - it lists all of the files
in a directory - The -l flag (lower case L) gives the list in
long format, showing extra info about each file - more is a little program that displays output,
prompting before scrolling off the screen - The vertical bar is a PIPE
7Unix Pipes
- A simple way to connect two or more Unix commands
into one - Output from the left command is sent as input to
the one on the right
8cp file dest
- copies a file from one location to another
- The destination file may have the same (if
unspecified) name, or a different one (if
specified) - cp can be used to create a copy of a file with a
different name in the same place - cp -p preserve creation and ownership info
- cp -r copy directories (recursively traverse
the directory tree and copy all files and
directories)
9pwd
- Stands for Print Working Directory
- Tells you where you are right now!
10mv file dest
- moves a file from one place to another
- The name of the file can change (as in cp)
- A good way to rename a file locally
- To make sure you are moving a file into a
directory (and not renaming it the same as that
directory) finish the destination path with a / - Works just as well for directories
- Can move multiple files
- mv foo.txt bar.txt baz.o DestinationDir
11cat f1 f2 gt new
- CAT is a common unix command used to concatenate
files together - Used with only one file, it simply outputs the
contents of the file (a quick way to peak at file
contents) - The gt character redirects the output of the
command to a new file
12Input/Output Redirects
- A useful tool for storing the output of a command
(or program) into a file, or automating the input
to a program. - command1 gt file1 (redirect output)
- command1 lt file1 (redirect input)
- command1 lt infile gt outfile (redirect both
input and output)
13Whats the difference?
- MyProgram lt inputfile
- cat inputfile MyProgram
14wc
- Stands for Word Count
- This utility reads either standard input or a
given list of files and returns (1) the number of
lines in the files (2) the number of words in the
files and (3) the number of characters in the
files
15rm file
- Remove a file (delete)
- There is no recycle bin at a Unix prompt!
- rm -i (interactive) asks before deleting each
file - rm -v (verbose) lists all of the files being
removed as it happens - rm -r recursively removes directories - BE
CAREFUL!!
16man
- This command shows you the Unix manual entry for
a given command - basically help - Usually lists all of the flags and options for a
command, sometimes gives examples - This is VERY USEFUL. man can give out a whole
lot of information about anything unix-y and
useful. - man Why did you get a divorce?
man too many arguments.
17Types of Operations (So Far)
- Memory Allocation (aka Declaring Variables)
- Mathematic Operations (,,/,,-)
- Assignment Operations ()
- Output Operations (print )
18Execution Control
- Another type of operation that we can do is
decision making - The IF statement evaluates a logical statement
and determines whether or not the body statements
will be evaluated. - The Logical Type
- IF (logical expression) THEN
- .... (body)
- ENDIF
- See Example IfExample.f90
- Each If statement requires at least 2 cycles
19Relational and logical operators
- .TRUE. , .FALSE.
- 0 is false, all other values are true
- Equal (.eq.), / Not equal (.neq.)
- gt Greater than or equal (.ge.)
- lt Less than or equal (.le.)
- lt Less than (.lt.), gt Greater than (.gt.)
- .AND. , .OR. and .NOT.
- .eqv. , .neqv.
20Logical Operators
A B .not. A A .and. B A .or. B A .eqiv. B
T T F T T T
T F F F T F
F T T F T F
F F T F F T
21New Order of Operations
- (), , /, -
- .EQ., .NE., .LT., .LE., .GT., .GE.
- (aka , /, lt, lt, gt, gt)
- .NOT.
- .AND.
- .OR.
- .EQV. and .NEQV.
22Try some of these out...
- 2ab 2(ab)
- T .and. T .or. F
- a gt b .eqv. a gt b
- .not. T .or. 5 lt 3
- 34 .ne. 12-5 .and. 2 .gt. 1
23IF/THEN/ELSE
- An efficient way to branch control between two
sets of operations - Very similar to an IF statement, but with a
second option, only one body or the other will be
executed. - IF (logical expression) THEN
- .... (body1)
- ELSE
- .... (body2)
- ENDIF
24IF/Then/Else IF/Else
- A less efficient and more complex method to
branch between multiple bodies of operations - IF (logical expression) THEN
- .... (body1)
- ELSE IF (logical expression) THEN
- .... (body2)
- ELSE
- .... (body2)
- ENDIF
- Example HurricaneIF.f90
25CASE Statements
- Could be more efficient, because the initial
expression is only evaluated once. - Less likely to accidently evaluate code due to
poorly formed secondary tests. - Much easier to read, understand and debug.
- UNFORTUNATELY, the value of the case selector
must be a single integer, character (of any
length), or logical value, which limits use.
26CASE Statements
- SELECT CASE (expression)
- CASE (case selector)
- ....
- CASE (case selector)
- .... (body2)
- CASE DEFAULT
- .... (body2)
- END SELECT
- HurricaneCase.f90
- Rule of Robustness Robustness is the child of
transparency and simplicity.
27Computers DO Operations Repeatedly
- The DO loop lets us do the same series of
operations over and over again - DO Counter Start, End, Increment
- .... (Operations)
- ENDDO
- See Example HurricaneEnsemble.f90
28DO Loops, A Few Rules
- The statements in the body of the loop are
repeated until the counter no longer equals the
End value - Note that the loop IS executed at the start and
end values - unlike IF statements. - The Increment is optional, if omitted, the
compiler assumes it is 1 - You must be able to reach End from Start (so
an Increment of 0 is not allowed), if this is not
possible, the loop is not executed. - Need to be careful, plenty of infinite loops are
not caught by the compiler
29Nesting DO Loops
- This can be done as many times as necessary, but
be careful, because your statements will be
evaluated exponentially more times for each loop! - DO Counter Start, End, Increment
- DO Counter2 Start, End, Increment
- .... (Operations)
- ENDDO
- ENDDO
- To keep it understandable, Counter and Counter2
should be different variables - Two nested loops are order n2, three is n3, etc
30FTC Temp Analysis
- See Example FTCTempAnalysis1.f90
- This program reads in 41 years of monthly
temperature data and calculates the slope,
intercept, and correlation coefficient of a
linear least-squares fit trend-line. - read
- Redirect input
- Sum vs. Do Loops
31FTC Temp Analysis
- Kates Least Squares Fit Algorithm
- Slope is calculated by the ratio of the
differences between four sums - Correlation coefficient is a measure of the ratio
between the error and the variance
32From Math to Code
- Start with a specific goal
- Write it down in simple steps
- Draw pictures if you need to
- Write a rough draft, get it working
- Refactoring improve little bits at a time. Test
after each change. Document the hell out of it. - Rule of Optimization Prototype before polishing.
Get it working before you optimize it.
Read in Data
Make year index array
Calculate four sums
Whole array or Do loop?
Calculate the Slope
Calculate the Intercept
Calculate corr coef
Need the average temp
Do loops to calc error and var
Print out results
33FTC Temp Analysis
34Mistakes do happen...
- To break out of a Fortran program in the
terminal, use control-c - Also works to break out of an infinite loop
35ps
- Shows what processes (programs, utilities and
daemons) are currently running on your computer - ps Show processes run at the terminal (usually)
- ps -ef All processes, full format
- ps -u katetc All processes owned by user katetc
36kill
- A serious way to kill a process (but only one
that belongs to you) - kill psID The kernal tells the process its
time to stop. The process can do any cleaning up
necessary before quitting. - kill -9 psID The kernal kills the process.
Nothing nice about it.
37More on Arrays
- Rank, bounds, extent, size, conformable
- Subsections xarr(startend)
- Fortran Triplets xarr( startendstride )
- Whole Array arithmetic - the arrays must be
conformable - CAB ECD foobar0.5 results0
- adds or multiplies each element of each array to
the other - Rule of Clarity Clarity is better than
cleverness.
38Multi Dimensional Arrays
- type, dimension(dim1,dim2,...) name
- REAL, dimension(lon,lat,height,time) temp
- Array element ordering - indices vary slower as
the dimension gets larger (1,1)(2,1)(3,1)(1,2)(2,
2)(3,2)(1,3)(2,3)(3,3) - Higher dimensional arrays are usually stored
contiguously in memory, in ROW MAJOR order (lon,
lat)
39Multi-Dimensional
For more dimensions (1,1,1)(2,1,1)(3,1,1) (1,2,1)
(2,2,1)(3,2,1) (1,3,1)(2,3,1)(3,3,1) (1,1,2)(2,1,2
)(3,1,2) (1,2,2)(2,2,2)(3,2,2)
Dimension 1
1 2 3
4 5 6
7 8 9
Dimension 2
40Array Transformation
- Reshape function is pretty cool
- Matrix RESHAPE( Source, Shape )
- A RESHAPE( B, (/3,2/) )
- Another way to index your array elements uses
mod and integer division - lon array(MOD(i,num_lons))
- lat array(i/num_lats)
- Can store data as (lat, lon) as well, but it will
be counter-intuitive and less efficient to
transform
41WHERE statements
- An easy way to initialize or set sections of
arrays - WHERE (array expression)
- array assignment block
- ELSEWHERE
- array assignment block 2
- END WHERE
- This is called masking
42FORALL Construct
- This statement indicates to the compiler that the
operations can be performed in parallel (no
operations depend on the value of the operation
on other elements in the array) - FORALL (triplet)
- variable expression
43Subroutines
- In our constant effort to avoid re-inventing the
wheel, we can abstract our code by packaging
certain parts to be reusable. - A subroutine is just a sub-section of code that
can be reused in the main program. - Subroutines can be included in the same file as
the main program or in other files (but well
talk about that next week) - In Fortran, arguments are passed by reference, so
subroutines can change the value of variables
back in the main program.
44Subroutines
- SUBROUTINE name (arguments)
- Implicit None
- argument declaration with INTENT
- .... (body)
- END SUBROUTINE name
- The arguments are generally optional, but are the
safest way to pass information. - In your main program, call the subroutine as
- call name (arguments)
45FTC Temp Analysis
- See Example FTCTempAnalysis2.f90
- This program reads in 41 years of monthly
temperature data, pulls out the January and July
mean temperatures, and calculates the slope,
intercept, and correlation coefficient of a
linear least-squares fit trend-line for January
and July. - Two methods for sub-setting Januarys and Julys
- Separate Slope and CorrCoef calculations into two
different subroutines - Dummy variables and scoping
- Make them reusable for any data (abstraction)
- Separating functionality makes it easier to test
- Finding and fixing an error in one place is
easier than multiple places
46FTC Temp Analysis
- Sub-setting data is more difficult in Excel
- Redirect output (from a print statement) to save
off the subset of data and import into Excel
47Subroutines
- Intent lets the compiler know what you are doing
with the variables - It is not required, but will help your compiler
help you - INTENT(IN) - the parameter has a value when the
function is called, and that value is not changed - INTENT(OUT) - the parameter does not have a given
value, but is assigned to when the subroutine or
function is run - INTENT(INOUT) - the parameter is given a value
which could be changed by the subroutine.
48Scoping
- One of the benefits of separating functional
units is data encapsulation - Code works best on a need-to-know basis
- Easier to debug when you know that only certain
(small) bits of the program can change a variable
Subroutine 1
Passed In
Main Program
Local Vars
Results Out
Passed In
Subroutine 2
Results Out
Local Variables
Local Vars
Passed In
Subroutine 2
Results Out
Same Locals
Uh Oh
49Functions
- Exactly the same as subroutines, except Functions
can return a value. - Like all of the built-in functions weve been
using, a call to a function returns the value
which can be stored in a variable (but the types
must match). - result funct(arg1,arg2,...)
- Example FTCTempAnalysis3.f90
50Functions
- You can declare a function in the same file as
your main program (after it) - Must declare the function with your variables (as
a typed variable) if using Implicit None - TYPE FUNCTION name (arguments)
- Implicit None
- argument declaration with INTENT
- .... (body)
- END FUNCTION name
- Rule of Transparency Design for visibility to
make inspection and debugging easier.
51What did we cover today?
- Unix file manipulation
- Pipes and Redirection
- Logical execution control
- Relational and logical operators
- DO loops
- Processes and how to kill them
- Whole array manipulators
- Subroutines and Functions
- Scoping Variables