fortran 1 overview
TRANSCRIPT
-
7/25/2019 Fortran 1 Overview
1/24
Fortran Programming
Grey GordonIndiana University
Fall 2014
-
7/25/2019 Fortran 1 Overview
2/24
Brief History of Fortran
Origins
Dates back to 1957.
Life before Fortran: assembly code very fast but tedious.
FORmula TRANslator: readable code translated to assembly. Focus from the start has been on speed.
Who uses Fortran today?
Scientists and engineers number crunchers NOT vast majority of programmers who need GUIs not speed
-
7/25/2019 Fortran 1 Overview
3/24
Brief History of Fortran
FORTRAN 77 (F77)
Many fast libraries coded in F77. Ex., BLAS and LAPACK. Matlab written in F77 originally (later translated to C). Built to run on punchcards (fixed-source format).
C FIND THE LARGEST NUMBER IN ABSOLUTE VALUE
IX = 1
DM AX = DA BS ( DX ( 1) )
IX = IX + INCX
DO 10 I = 2 , N
IF ( D A B S ( D X ( I X ) ) . LE . D M A X ) GO TO 5
IDAMAX = IDM AX = D ABS ( DX ( IX ) )
5 IX = IX + INCX
10 CONTINUE
RETURN
NOTE: C is very similar to this.
-
7/25/2019 Fortran 1 Overview
4/24
Brief History of Fortran
Fortran 90/95 (F90) (the standard)
Major advance adding new features.
Added dynamic memory allocation, pointers, basic
object-oriented programming. Built to run on modern computers (free-source format).
Fortran 95 was a slight revision that fixed a few problems.
Supports all F77 features but some are obsoleted like GOTO.
! S ame as p re ce di ng c od e
dmax = maxval ( abs ( x ) )
-
7/25/2019 Fortran 1 Overview
5/24
Brief History of Fortran
Fortran 2003 (F03)
Fairly well supported.
Latest Intel compiler supports all features now.
Large focus on interfacing with C.
Fortran 2008 (F08)
Intel compiler supports a fairly large number of features.
Some important features introduced like procedure pointers andcoarrays (a simple way to do distributed parallel computing).
-
7/25/2019 Fortran 1 Overview
6/24
Brief History of Fortran
Where to find help:
Very basic things: www.cs.mtu.edu/~shene/COURSES/
cs201/NOTES/fortran.html
Fortran manuals (Intel or gfortran)
Google
Other Fortran coders
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/fortran.htmlhttp://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/fortran.htmlhttp://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/fortran.htmlhttp://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/fortran.htmlhttp://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/fortran.htmlhttp://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/fortran.html -
7/25/2019 Fortran 1 Overview
7/24
Copyrights, wrongs, and plagiarism
You may use codes written by others if you cite their code.
If you do not cite their code, this is plagiarism. If you begin with their code as a base, modify it, and do not
cite their code, this is plagiarism.
Because it is for education, you should be fine under fair usean
exception to copyright laws (that is, they probably wont sue you).
In general, later in life you need to be concerned with licenses.Code is released under various licenses. Examples:
All rights reserved
BSD License
GNU GPL License
Public domain
Custom
Unspecified
-
7/25/2019 Fortran 1 Overview
8/24
Copyrights, wrongs, and plagiarism
Original code:function [ Z , Z p r ob ] = t a u ch e n ( N , mu , r h o , s i gm a , m )% F u n c t io n T A U CH E N
%% P ur po se : F in ds a M ar ko v ch ai n wh os e sa mp le p at hs% a pp r ox i m at e those of the AR (1) pr oce ss% z ( t +1) = (1 - rho )* mu + rho * z ( t ) + eps ( t +1)% where eps are norma l with std dev sigm a%% Fo rm at : { Z, Z pr ob } = T au ch en (N ,mu , rho , si gma , m )%% Input : N scalar , nu mbe r of nodes for Z
% mu scalar , u nc o n d it i o n al mean of p ro ces s% rho s cala r% sigma scalar , std . dev . of e ps il on s% m max + - std . devs .%% Out put : Z N *1 vector , nodes for Z% Zprob N * N matrix , t ra n si t io n p r o b ab i l i ti e s%% M arti n Flo den% Fall 1996%% T his p ro ce du re i s a n i mp le me nt at io n o f G eo rg e T au ch en 's a l g o ri t h m% d es cr ib ed in Ec . Le tt er s 20 ( 19 86 ) 177 - 18 1.
Z = zeros ( N , 1 ) ;Z p ro b = zeros ( N , N ) ;a = (1 - rho )* mu ;Z ( N ) = m * sqrt ( s ig ma ^ 2 / ( 1 - r ho ^ 2) );Z (1) = -Z (N );z st ep = ( Z( N) - Z (1 )) / ( N - 1 );[...]
-
7/25/2019 Fortran 1 Overview
9/24
Copyrights, wrongs, and plagiarism
New code with proper citation (fine):
function [ Z , Z p r ob ] = t a u ch e n ( N , mu , r h o , s i gm a , m )% F u n c t io n T A U CH E N%% Input : N scalar , nu mbe r of nodes for Z% mu scalar , u nc o n d it i o n al mean of p ro ces s% rho s cala r% sigma scalar , std . dev . of e ps il on s% m max + - std . devs .%
% Out put : Z N *1 vector , nodes for Z% Zprob N * N matrix , t ra n si t io n p r o b ab i l i ti e s%% Or igi nal ly by Mart in Fl oden% Mo di fi ed sl ig ht ly by Grey Gor don
Z = zeros ( N , 1 ) ;Z p ro b = zeros ( N , N ) ;a = (1 - rho )* mu ;Z ( N ) = m * sqrt ( s ig ma ^ 2 / ( 1 - r ho ^ 2) );Z (1) = -Z (N );z st ep = ( Z( N) - Z (1 )) / ( N - 1 );[...]
Since this acknowledges the original author, even though there arefew modifications, this is fine.
-
7/25/2019 Fortran 1 Overview
10/24
Copyrights, wrongs, and plagiarism
New code without proper citation (VERY BAD):
function [ Z , Z p r ob ] = t a u ch e n ( N , mu , r h o , s i gm a , m )% F u n c t io n T A U CH E N%% Input : N n umber of poi nts% mu mean% rho p e r s is t e nc e% sigma c on d it i on a l stdev% m co ve ra ge%% Out put : Z nodes% Zprob tr a ns i ti o n m atri x%Z = zeros ( N , 1 ) ;Z p ro b = zeros ( N , N ) ;a = (1 - rho )* mu ;Z = l i n s p a c e ( - m * sqrt ( si gm a ^2 / (1 - rh o ^2 )) , . ..
m * sqrt ( s ig ma ^ 2 / ( 1 - r ho ^ 2) ) , N) ;[...]
Since this has no mention of the original author, even though it ismodified, even though it improves on the original code, this isplagiarism.
-
7/25/2019 Fortran 1 Overview
11/24
Copyrights, wrongs, and plagiarism
When in doubt, cite!
-
7/25/2019 Fortran 1 Overview
12/24
Copyrights, wrongs, and plagiarism
Youll probably want to use the code you develop in this class laterin life. In that case, you need to be concerned about licenses.
-
7/25/2019 Fortran 1 Overview
13/24
Copyrights, wrongs, and plagiarism
Example of a nice license (LAPACK, emphasis added):
Copyright (c) 1992-2008 The University of Tennessee. All rights reserved.
COPYRIGHT
Additional copyrights may follow
HEADER
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the followingdisclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the followingdisclaimer listed in this license in the documentation and/or other materials provided with the distribution.
- Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promoteproducts derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANYEXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIESOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENTSHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ... [truncated]
You can use LAPACK as much as you want (if you meet theconditions).
-
7/25/2019 Fortran 1 Overview
14/24
Copyrights, wrongs, and plagiarism
The worst license I have ever seen (really a FAQ about theNumerical Recipes license, emphasis added) :
When Do I Need a License? The Numerical Recipes book, and its electronic subscription version Numerical RecipesElectronic, are sold as text and reference works, for reading and educational purposes. If you intend to useNumerical Recipes code on a computer, you need a separate license. What Kinds of Licenses Are There? There aretwo kinds of licenses. 1. With the purchase of a Numerical Recipes Code product, either a code download or aNumerical Recipes Code CD-ROM, you automatically get a non-expiring Numerical Recipes Personal, Single-UserLicense that allows one individual to use the code on any number of computers.
2. With the purchase of a Numerical Recipes institutional subscription, you get a Numerical Recipes InstitutionalSubscriber License that allows use of the code byany number of individuals on computers within a subscribersfixed IP address range, during the term of the subscription.
See complete license terms for both license types here.
Can I Distribute Numerical Recipes Routines with My Application Bound invisibly into your executable .exe file?Generally yes. As source code, linkable object libraries, or DLLs? Generally no. See more details here. Can aBusiness Use Personal, Single-User Licenses? Yes, if each such license purchased is permanently assigned to a
specific individual before it is first used. Note, however, that the Personal, Single-User License may be assignedonly once, to a single individual. Thereafter it stays with that individual, whether or not he/she continues as anemployee. The single-user license is not a floating licenses or a license by the seat. Businesses needing greaterflexibility should purchase institutional subscriptions. What Else Comes with an Institutional Subscriber License?For machines in the subscribers IP address range, institutional subscriptions include both unlimited access toNumerical Recipes Code (and the license for its use), and also unlimited access to Numerical Recipes Electronic,the electronic form of the Numerical Recipes book. The institutional subscription is generally the mostcost-effective way to make Numerical Recipes available to work groups, companies, and universities.
You should never use Numerical Recipes. Dont even look at it.
-
7/25/2019 Fortran 1 Overview
15/24
Programming
Now, back to the interesting stuff!
-
7/25/2019 Fortran 1 Overview
16/24
Comparison of Fortran and Matlab
Case sensitivity
1. Fortran is not case-sensitive: foo==FOO==fOo
2. Matlab is case-sensitive: foo/=FOO/=fOo
C i f F d M l b
-
7/25/2019 Fortran 1 Overview
17/24
Comparison of Fortran and Matlab
Compiled vs Interpreted Languages
Matlab is an interpreted language. What does B = sum(A) mean in Matlab? The interpreter has to
Read the expression. Check what A is currently (scalar? ND-array?). Determine what sum() does for this type of argument. Create space for B on the LHS.
This is in fact why loops are so slow.
C i f F d M l b
-
7/25/2019 Fortran 1 Overview
18/24
Comparison of Fortran and Matlab
Compiled vs Interpreted Languages
Fortran is a compiled language.
What does B = sum(A) mean in Fortran? The compiler determines before the program is run What A is (scalar, ND-array?). What sum() does. Whether B is the right shape to fit sum(A).
Unfortunately, this requires more work for the programmer.
C i f F d M l b
-
7/25/2019 Fortran 1 Overview
19/24
Comparison of Fortran and Matlab
Compiled vs Interpreted Languages
Advantages and Disadvantages Compiled is much faster.
Compiled is much better at error-checking before the programis run. Interpreted is much more flexible.
Can implement dramatically different operations with just oneline of code.
Compiled is much more verbose (this is true of C as well asFortran).
C i f F t d M tl b
-
7/25/2019 Fortran 1 Overview
20/24
Comparison of Fortran and Matlab
Typed vs Untyped Languages Matlab is an untyped language.
What does B = sum(A) mean in Matlab? Could mean A is a matrix and B is a vector. Could also mean A is a vector and B is a scalar. Moreover, A could be integers, or A could be decimals. Implication: Matlab must check each time the line is reached.
Fortran is a strongly-typed language.
What does B = sum(A) mean in Fortran? The programmer must declare A and B beforehand. The compiler then knows before runtime, e.g. that A is an
integer matrix and B is an integer vector.
C i f F t d M tl b
-
7/25/2019 Fortran 1 Overview
21/24
Comparison of Fortran and Matlab
Typed vs Untyped Languages
Matlab is now adding support for other classes (e.g. int64).
Advantages and Disadvantages Similar to compiled vs interpeted. Typed is much faster and safer better error-checking. Untyped is slower, but more flexible and less verbose.
Object-oriented programming is easy.
When to use Fortran v Matlab
-
7/25/2019 Fortran 1 Overview
22/24
When to use Fortran v Matlab
Basic principle: minimize totaltime
Time to run a program this will be faster in Fortran (maybe
by a factor of 10 or even 100) Time to make a program this will be faster in Matlab (less
verbose, better debugging)
Total time depends on these two components.
When to use Fortran v Matlab
-
7/25/2019 Fortran 1 Overview
23/24
When to use Fortran v Matlab
When to use Fortran.
If speed is critical. For example if it has many loops that cannot be vectorized.
If your code is conceptually fairly straightforward. O/w, it may be a good idea to prototype it in Matlab first.
When to use Matlab
If what you want to do is very complicated.
If what you want to do wont take long to run in Matlab.
When to use Fortran v Matlab
-
7/25/2019 Fortran 1 Overview
24/24
When to use Fortran v Matlab
Rough categorization of types of problems:
Linearization Matlab Estimation Fortran or Matlab
Probably use Mathematica or Matlab to do the symbolic mathand then generate Fortran code from it.
Higher-order perturbation Matlab or Fortran Grid search in one-dimension Matlab or Fortran
Grid search in multiple-dimensions Fortran
Grid search in one-dimension (Business Cycle) Fortran or
Matlab Grid search in multiple-dimensions (Business Cycle) Fortran
Projection methods with low-order polynomials Matlab
Projection methods with higher-order polynomials Fortran