Skip to content

Commit 3ee4679

Browse files
committed
Update to include implicit save statement description
1 parent 1ebb7e4 commit 3ee4679

6 files changed

Lines changed: 131 additions & 10 deletions

File tree

chapters/Getting_your_toes_wet.tex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
\begin{itemize}
5252
\item The program starts with a declaration of variables.\index{Fortran!types}
5353
\texttt{\keyword{real}(8)} denotes a floating-point variable with double (8 byte) precision.
54-
Similarly, \keyword{integer} denotes an integer number.\footnote{Fortran intrinsic types include \keyword{logical}, \keyword{integer}, \keyword{real}, \keyword{complex}, and \keyword{character} data. Modern Fortran also gives you the ability to define your own datatypes, though a structures-of-arrays design (vs.\ an arrays-of-structures design) tends to work best for high-performance computing.}
54+
Similarly, \keyword{integer} denotes an integer number.\footnote{Fortran intrinsic types include \keyword{logical}, \keyword{integer}, \keyword{real}, \keyword{complex}, and \keyword{character} data. Modern Fortran also gives you the ability to define your own datatypes.}
5555
Not specifying a size generally defaults to 4-byte precision.
5656
\keyword{implicit none}\index{Fortran!implicit none@\keyword{implicit none}} prevents Fortran from trying to infer the type from the variable name, which is a major source of bugs---\emph{always include this!}
5757
\item The attribute \keyword{parameter}\index{Fortran!parameter@\keyword{parameter}} specifies that we are declaring a constant.
@@ -62,14 +62,14 @@
6262
It is also possible to specify the precision as a suffix: \texttt{1.0\_4} for single and \texttt{1.0\_8} for double precision.
6363
This also works for integers.
6464
\item \texttt{\keyword{integer} :: fibonacci(N)} allocates an array of 20
65-
integers, with the array\index{Fortran!arrays} index\footnote{Array indices can start at any integer by replacing \texttt{N} with a lower and upper bound separated with a colon. That is, declaring \texttt{myArray(-312:74)} means \texttt{myArray} starts at index -312 and runs through index 74.} running from 1 to 20.
65+
integers, with the array\index{Fortran!arrays} index\footnote{Array indices can start at any integer by replacing \texttt{N} with a lower and upper bound separated with a colon. Thus, \lstinline$real :: myArray(-312:74)$ means \texttt{myArray} starts at index -312 and runs through index 74.} running from 1 to 20.
6666
\item `\texttt{*}' is multiplication, `\texttt{**}' is exponentiation.
6767
\item The \keyword{print} statement on line 24 contains the format code\index{Fortran!format codes} \str{"(4I6)"}.
6868
This tells Fortran to print 4 records of integer type, each having a width of 6 characters (including spaces), per line.
6969
Format strings for other datatypes include \texttt{Ew.d} (real -- decimal form), \texttt{ESw.d} (real -- scientific form), \texttt{ENw.d} (real -- engineering form), \texttt{Lw} (logical), and \texttt{A} (characters). Here, \texttt{w} gives the width of a record and \texttt{d} gives the number of places right of the decimal.
7070
\item Comments in Fortran start with `\texttt{!}' and last until the end of the line.
7171
\item Dividing integers \textcolor{red}{\textbf{results in an integer}}\index{Fortran!integer division}, so \texttt{3 / 2 == 1} instead of 1.5 as you might expect.
72-
Multiplying by \texttt{1d0} on line 27 forces Fortran to do a double-precision floating-point calculation.
72+
Multiplying by \texttt{1d0} on line 28 forces Fortran to do a double-precision floating-point calculation.
7373
\end{itemize}
7474
Now we compile and run the program.
7575
Compiling means translating the Fortran source code into machine code (processor instructions).
@@ -102,7 +102,7 @@
102102
The downside is that it can result in executables that will not run on a different machine.
103103
\item \texttt{-O3}\index{compiler flags!-O3} turns on all optimizations.
104104
This increases the compilation time significantly, although it should still be fast enough for the programs you'll write in ICCP.
105-
The runtime of your program, on the other hand, will dramatically decrease.
105+
The run time of your program, on the other hand, will dramatically decrease.
106106
The only reason not to use this flag is that it might interfere with the debugger (see below).
107107
\item A possible additional optimization flag is \texttt{-ffast-math}\index{compiler flags!-ffast-math}.
108108
This flag enables floating-point optimizations which might reduce accuracy or result in incorrect behavior, especially in situations such as divide-by-zero.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
\Chapter{Getting your toes wet with Fortran and Linux}{}
2+
\label{chap:Getting your toes wet}
3+
\begin{quote}\small
4+
\emph{``1957: John Backus and IBM create FORTRAN. There's nothing funny about IBM or FORTRAN. It is a syntax error to write FORTRAN while not wearing a blue tie.''} \\ \hspace*{\fill}---James Iry
5+
\end{quote}
6+
7+
For this course we recommend using the GNU Fortran compiler, as it is free and of reasonably high quality.\footnote{You're welcome to use the Intel Fortran compiler, which is free on Linux, but remember to change the compiler flags, since they differ from \texttt{gfortran}.}
8+
Linux is the platform of choice, since it's most suited to programming and high-performance computing.\footnote{As of this writing, 476 of the \href{http://www.top500.org/list/2013/11/}{TOP500} supercomputers in the world run some form of Linux.}
9+
OS~X, being a Unix variant, is also an option, but installing the required packages is generally a bit more work.
10+
In these notes we will assume that you're using Ubuntu.\footnote{You can use Ubuntu as a native installation, run it from a flash drive, under a virtual machine, or under Windows using Wubi.
11+
Ask your instructor.}
12+
13+
After booting Ubuntu, you need to install a Fortran compiler.
14+
Open a terminal and at the prompt (the \$) type
15+
\begin{lstlisting}[style=prompt, nolol]
16+
$ sudo apt-get install gfortran gfortran-doc
17+
\end{lstlisting}
18+
\emph{Note that anything you type in the console is case-sensitive!}
19+
This command grants \texttt{aptitude} privileges to search for and then install the \texttt{gfortran} package and its documentation.
20+
You may also find it useful to install the \LaTeX\ typesetting system for writing reports.
21+
You can install it with another call to \texttt{aptitude}:
22+
\begin{lstlisting}[style=prompt, nolol]
23+
$ sudo apt-get install texlive # or texlive-full for a complete installation
24+
\end{lstlisting}
25+
26+
Once you have \texttt{gfortran} installed, you can start writing programs.
27+
You will generally compile and run your programs from a terminal window (also called a \emph{console}).
28+
A few useful commands include:
29+
\begin{lstlisting}[style=prompt, nolol]
30+
$ ls # display the contents of the current directory
31+
$ ls -l # display contents with extra details
32+
$ cp file path # copy 'file' to 'path'
33+
$ mv file path # move 'file' to 'path'
34+
$ rm file # remove 'file'
35+
$ mkdir dir # create a new directory called 'dir'
36+
$ cd dir # change current directory to 'dir'
37+
$ rmdir dir # remove directory 'dir' (only if it's empty)
38+
$ rm -r dir # remove directory 'dir' (even if it's not empty)
39+
$ man cmd # provide documentation on 'cmd'
40+
\end{lstlisting}
41+
42+
For your first program, open a terminal, create a directory for ICCP files and open your first Fortran file by typing
43+
\marginnote[-0.5cm]{Modern terminal emulators all have a feature called \emph{tab completion}; enter part of a command \& hit the \texttt{TAB} key and the terminal will attempt to fill in the rest of the command for you.}
44+
\begin{lstlisting}[style=prompt, nolol]
45+
$ mkdir iccp # create a new directory called 'iccp'
46+
$ cd iccp # move to the new directory
47+
$ gedit myprog.f90 # open a text editor with the file 'myprog.f90'
48+
\end{lstlisting}
49+
The gedit text editor pops open in which you can type the program in \autoref{lst:myProg}.
50+
You can probably guess what this program does, however, a few remarks are in order:
51+
\begin{itemize}
52+
\item The program starts with a declaration of variables.\index{Fortran!types}
53+
\texttt{\keyword{real}(8)} denotes a floating-point variable with double (8 byte) precision.
54+
Similarly, \keyword{integer} denotes an integer number.\footnote{Fortran intrinsic types include \keyword{logical}, \keyword{integer}, \keyword{real}, \keyword{complex}, and \keyword{character} data. Modern Fortran also gives you the ability to define your own datatypes, though a structures-of-arrays design (vs.\ an arrays-of-structures design) tends to work best for high-performance computing.}
55+
Not specifying a size generally defaults to 4-byte precision.
56+
\keyword{implicit none}\index{Fortran!implicit none@\keyword{implicit none}} prevents Fortran from trying to infer the type from the variable name, which is a major source of bugs---\emph{always include this!}
57+
\item The attribute \keyword{parameter}\index{Fortran!parameter@\keyword{parameter}} specifies that we are declaring a constant.
58+
Although Fortran is case-insensitive, it is considered good practice to always use uppercase names for constant variables.
59+
\item Note the calculation of $\pi$ as \texttt{4*\keyword{atan}(1d0)}---convenient and accurate.
60+
\item Single-precision (4 byte) floating-point numbers can be written as \texttt{0.1} or \texttt{1e-1} (scientific notation).
61+
For double-precision numbers, use \texttt{d} instead of \texttt{e} in scientific notation, \eg, \texttt{1d-1}.
62+
It is also possible to specify the precision as a suffix: \texttt{1.0\_4} for single and \texttt{1.0\_8} for double precision.
63+
This also works for integers.
64+
\item \texttt{\keyword{integer} :: fibonacci(N)} allocates an array of 20
65+
integers, with the array\index{Fortran!arrays} index\footnote{Array indices can start at any integer by replacing \texttt{N} with a lower and upper bound separated with a colon. That is, declaring \texttt{myArray(-312:74)} means \texttt{myArray} starts at index -312 and runs through index 74.} running from 1 to 20.
66+
\item `\texttt{*}' is multiplication, `\texttt{**}' is exponentiation.
67+
\item The \keyword{print} statement on line 24 contains the format code\index{Fortran!format codes} \str{"(4I6)"}.
68+
This tells Fortran to print 4 records of integer type, each having a width of 6 characters (including spaces), per line.
69+
Format strings for other datatypes include \texttt{Ew.d} (real -- decimal form), \texttt{ESw.d} (real -- scientific form), \texttt{ENw.d} (real -- engineering form), \texttt{Lw} (logical), and \texttt{A} (characters). Here, \texttt{w} gives the width of a record and \texttt{d} gives the number of places right of the decimal.
70+
\item Comments in Fortran start with `\texttt{!}' and last until the end of the line.
71+
\item Dividing integers \textcolor{red}{\textbf{results in an integer}}\index{Fortran!integer division}, so \texttt{3 / 2 == 1} instead of 1.5 as you might expect.
72+
Multiplying by \texttt{1d0} on line 28 forces Fortran to do a double-precision floating-point calculation.
73+
\end{itemize}
74+
Now we compile and run the program.
75+
Compiling means translating the Fortran source code into machine code (processor instructions).
76+
This can be done by simply typing
77+
\begin{verbatim}
78+
$ gfortran myprog.f90
79+
\end{verbatim}
80+
which will result in the executable file \texttt{a.out}.
81+
This is the default name for the output.
82+
You can specify a different name by compiling with
83+
\begin{verbatim}
84+
$ gfortran myprog.f90 -o myprog
85+
\end{verbatim}
86+
which results in a program with the name \texttt{myprog}.
87+
During compilation, the compiler may generate error messages and warnings.
88+
The errors must be fixed before an actual running program is produced, and it is good practice to also make sure no warnings are generated.
89+
90+
The compilation process can be tuned by passing certain command-line arguments to the compiler.
91+
We recommend using always at least the following:
92+
\begin{verbatim}
93+
$ gfortran -Wall -Wextra -march=native -O3 myprog.f90 -o myprog
94+
\end{verbatim}
95+
\begin{itemize}
96+
\item \texttt{-Wall}\index{compiler flags!-Wall} and \texttt{-Wextra}\index{compiler flags!-Wextra} turn on all warnings.
97+
This may generate a lot of messages, but fixing them all leads to much cleaner code.
98+
This can be a huge time saver; not only for you, but also for your instructors!
99+
If your program doesn't behave as expected, first try to fix all warnings before asking your instructors for help.
100+
\item \texttt{-march=native}\index{compiler flags!-march=native} tells the compiler to generate machine code using all available processor instructions on your machine.
101+
On modern CPUs this leads to much faster code, since \texttt{gfortran} can use vector instructions.
102+
The downside is that it can result in executables that will not run on a different machine.
103+
\item \texttt{-O3}\index{compiler flags!-O3} turns on all optimizations.
104+
This increases the compilation time significantly, although it should still be fast enough for the programs you'll write in ICCP.
105+
The runtime of your program, on the other hand, will dramatically decrease.
106+
The only reason not to use this flag is that it might interfere with the debugger (see below).
107+
\item A possible additional optimization flag is \texttt{-ffast-math}\index{compiler flags!-ffast-math}.
108+
This flag enables floating-point optimizations which might reduce accuracy or result in incorrect behavior, especially in situations such as divide-by-zero.
109+
We therefore recommend not using this flag until you have verified that your program produces the correct results.
110+
After that you can use it to make your program faster, but do check that it still behaves correctly.
111+
\item Finally, if your code starts throwing segmentation faults (segfaults), consider using the \texttt{-fbounds-check}\index{compiler flags!-fbounds-check} flag.
112+
Segfaults usually occur because you've run an index off the end of an array, so compiling with this flag turns on runtime bounds checking (at the expense of significantly reduced performance).
113+
\end{itemize}
114+
If the program compiled correctly, you can run it by typing \texttt{./a.out} or \texttt{./myprog}.
115+
Note that \texttt{./} specifies the \emph{path}, \ie, the location where to find the program.
116+
The dot means the current directory (similarly, \texttt{..} refers to the parent directory), and the slash separates directories and file names (like the backslash in DOS and Windows).
117+
118+
\lstinputlisting[float=h!,label=lst:myProg]{examples/myProg.f90}

chapters/Introduction.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
Even if you already have some experience with another language, take it from us that both you and your code will be faster if you use Fortran for ICCP.
1919
If you want to continue in computational physics you'll encounter it sooner or later anyway (think \idx{LAPACK}), and ICCP is the perfect time to learn.
2020

21-
These notes are by no means a Fortran reference.
21+
These notes are by no means a complete Fortran reference.
2222
High-quality documentation can be easily found online.
2323
Good starting points are \url{http://fortran90.org} and \url{http://en.wikipedia.org/wiki/Fortran_language_features}.
2424
The \emph{Intel Fortran Compiler Language Reference} is freely available (Google it).

chapters/Structuring_your_code.tex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ \section{Functions and subroutines}
3131
Fortran subroutines are invoked with the keyword \keyword{call} followed by the name of the routine and an optional list of parameters in parentheses.
3232
The code for the subroutine itself appears after the keyword \keyword{contains}.
3333
We don't have to type \keyword{implicit none} again, since that's inherited from the program.
34-
When declaring the parameters, we need to specify not only the type, but also how we're going to use them, \ie, the \emph{intent}.\index{Fortran!intent(in/out)@\keyword{intent (in}/\keyword{out)}}
34+
\marginnote{A \textbf{big} fortran gotcha involves initializing local variables while declaring them (\ie\ \lstinline$real :: x = 10.0$). Variables like this have an implicit \keyword{save}\index{Fortran!save@\keyword{save}} statement, meaning they retain their value through subsequent calls to the subroutine. It's best to either mark these variables with the \keyword{save} attribute explicitly \emph{or} separate initialization from declaration within subroutines and functions.}
35+
When declaring the parameters, we need to specify not only the type, but also how we're going to use them, \ie, the \emph{intent}.\index{Fortran!intent(in/out)@\keyword{intent} (\keyword{in}/\keyword{out})}
3536
For input and output parameters, we use \texttt{\keyword{intent}(\keyword{in})} and \texttt{\keyword{intent}(\keyword{out})}, respectively.
3637
For parameters functioning as both, we use \texttt{\keyword{intent}(\keyword{inout})}.
3738

examples/myProg.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ program MyProg
1212
read *, radius
1313
surface = PI * radius**2
1414
circumference = 2 * PI * radius
15-
print *, "The surface of the circle is ", surface
16-
print *, "and the circumference is ", circumference
15+
print *, "The surface of the circle is", surface
16+
print *, "and the circumference is", circumference
1717

1818
fibonacci(1) = 0
1919
fibonacci(2) = 1

notes.tex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@
101101
\begin{center}
102102
\includegraphics[width=0.15\textwidth]{figures/by-nc-sa.eps}
103103
\end{center}
104-
This work, including its figures and \LaTeX\ source code, is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
105-
To view a copy of this license, visit \url{http://creativecommons.org/licenses/by-nc-sa/4.0/deed.en_US}.
104+
This work, including its figures and source code (where applicable), is
105+
licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0
106+
International License. To view a copy of this license, visit
107+
\url{http://creativecommons.org/licenses/by-nc-sa/4.0/deed.en_US}.
106108

107109
\pagestyle{fancy}
108110

0 commit comments

Comments
 (0)