|
| 1 | +--- |
| 2 | +title: "A Trivial Program" |
| 3 | +teaching: -- |
| 4 | +exercises: -- |
| 5 | +--- |
| 6 | + |
| 7 | +::::::::::::::::::::::::::::::::::::: questions |
| 8 | + |
| 9 | +- How do you write programs in Fortran? |
| 10 | +- How do you compile the Fortran code to make an executable? |
| 11 | +- How do we run that executable and see any output? |
| 12 | + |
| 13 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 14 | + |
| 15 | +::::::::::::::::::::::::::::::::::::: objectives |
| 16 | + |
| 17 | +Be able to |
| 18 | + |
| 19 | +- write |
| 20 | +- compile |
| 21 | +- run |
| 22 | + |
| 23 | +a basic Fortran program. |
| 24 | + |
| 25 | +:::::::::::::::::::::::::::::::::::::::::::::::: |
| 26 | + |
| 27 | +During the setup you compiled a simple `hello world` program. |
| 28 | +Let's look at that program in more detail: |
| 29 | + |
| 30 | +```bash |
| 31 | +cd ~/Desktop/intro-to-modern-fortran/01-trivial-program |
| 32 | +cat hello_world.f90 |
| 33 | +``` |
| 34 | + |
| 35 | +```fortran |
| 36 | +program hello_world |
| 37 | +
|
| 38 | + implicit none |
| 39 | +
|
| 40 | + print *, 'Hello world!' |
| 41 | +
|
| 42 | +end program hello_world |
| 43 | +
|
| 44 | +``` |
| 45 | + |
| 46 | +The first statement `program hello_world` starts the program. |
| 47 | +It also defines the program name, `hello_world`. |
| 48 | +It is matched by the `end` statement (`end program hello_world`). |
| 49 | +The `end` statement is always the last statement in a program. |
| 50 | + |
| 51 | +::: spoiler |
| 52 | + |
| 53 | +### Style Guide - Labels |
| 54 | + |
| 55 | +`program` on its own marks the start of a program |
| 56 | +and `end` on its own will end the program. |
| 57 | +Words that follow `program` or `end` are labels which improve readability. |
| 58 | +Some legacy codes may not have labels. |
| 59 | + |
| 60 | +::: |
| 61 | + |
| 62 | +## Implicit None |
| 63 | + |
| 64 | +Some variables in Fortran have a default type. |
| 65 | +Variables with names beginning with letters `i-n` |
| 66 | +are implicitly of type `integer`. |
| 67 | +Anything else is of type `real`. |
| 68 | + |
| 69 | +**This is very bad practice and modern Fortran should not be used in this way.** |
| 70 | + |
| 71 | +To prevent implicit typing we add the: |
| 72 | + |
| 73 | +```fortran |
| 74 | +implicit none |
| 75 | +``` |
| 76 | + |
| 77 | +statement to all `programs` |
| 78 | +(and `modules`, `functions`, and `subroutines` that you will encounter in later episodes). |
| 79 | + |
| 80 | +Now all variable names must be declared explicitly before they are referenced. |
| 81 | + |
| 82 | +The only executable line in this program is the `print` statement. |
| 83 | +We will cover I/O (Input/Output) in a later episode. |
| 84 | +For now, know that `print *,` will print what follows |
| 85 | +to standard output when the program runs. |
| 86 | +In this case, it will print the string `Hello world!` to our terminal. |
| 87 | + |
| 88 | +## Comments |
| 89 | + |
| 90 | +Comments start with an exclamation mark `!`. |
| 91 | +Comments can appear on their own line, |
| 92 | +or after any other Fortran statement. |
| 93 | + |
| 94 | +::::::::::::::::::::::::::::::::::::::::: spoiler |
| 95 | + |
| 96 | +### Legacy Fortran - F77 style comments |
| 97 | + |
| 98 | +You may see code that has comments written like this: |
| 99 | + |
| 100 | +```fortran |
| 101 | +c this is a comment |
| 102 | +``` |
| 103 | + |
| 104 | +with a `c` in the first column. |
| 105 | +In new code use the modern `!` comments. |
| 106 | + |
| 107 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 108 | + |
| 109 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 110 | + |
| 111 | +## Add comments to hello_world.f90 |
| 112 | + |
| 113 | +Add two comments to your file: |
| 114 | + |
| 115 | +1. The first should explain what the program does |
| 116 | +2. The second should explain the purpose of `implicit none` |
| 117 | + |
| 118 | +Think about where the best place for these comments are. |
| 119 | + |
| 120 | +::::::::::::::: solution |
| 121 | + |
| 122 | +## Solution |
| 123 | + |
| 124 | +```fortran |
| 125 | +program hello_world |
| 126 | + !! A simple hello world program |
| 127 | +
|
| 128 | + implicit none ! prevent implicit typing (to integers) of variables |
| 129 | + ! whose name starts with the letters i-n |
| 130 | +
|
| 131 | + print *, 'Hello world!' |
| 132 | +
|
| 133 | +end program hello_world |
| 134 | +
|
| 135 | +``` |
| 136 | + |
| 137 | +1. We have placed a comment describing the program under the `program` statement. |
| 138 | + You might also see program descriptions before the `program` statement. |
| 139 | + Note the double `!` at the start of the comment. |
| 140 | + This allows the automatic documentation generator [FORD](https://forddocs.readthedocs.io/en/stable/index.html) to extract documentation from the comment. |
| 141 | +2. The second comment could have been placed before or after |
| 142 | + the `implicit none` statement. |
| 143 | + We have shown an _inline_ comment. |
| 144 | + Note the two spaces between the Fortran and the start of the comment, |
| 145 | + and the comment spans multiple lines with each `!` aligned vertically. |
| 146 | + |
| 147 | +You may have noticed comments at the top of the Fortran files |
| 148 | +you downloaded during the setup. |
| 149 | +Those comments provide licensing and authorship information. |
| 150 | + |
| 151 | +::::::::::::::::::::::::: |
| 152 | + |
| 153 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 154 | + |
| 155 | +## Compiling |
| 156 | + |
| 157 | +Fortran is a compiled language (like C++). |
| 158 | +A compiler turns human-readable source code into machine code. |
| 159 | +This machine code can then be executed by the computer. |
| 160 | +Languages like Python are interpreted. |
| 161 | +This means Python source code is executed directly |
| 162 | +without being compiled into machine code first. |
| 163 | +The program is parsed, interpreted, and executed each time it is run. |
| 164 | +Compiled programs are usually more efficient than interpreted programs. |
| 165 | +This is a major reason that compiled languages like Fortran remain popular. |
| 166 | +The draw back is that there is an extra step in building Fortran programs. |
| 167 | + |
| 168 | +There are several steps[^compiler-process] that occur during compilation. |
| 169 | +Your compiler takes care of each step for you. |
| 170 | +To test your compiler in the setup episode you ran: |
| 171 | + |
| 172 | +::: group-tab |
| 173 | + |
| 174 | +### GFortran |
| 175 | + |
| 176 | +```bash |
| 177 | +gfortran hello_world.f90 |
| 178 | +``` |
| 179 | + |
| 180 | +### Intel |
| 181 | + |
| 182 | +```bash |
| 183 | +ifx hello_world.f90 |
| 184 | +``` |
| 185 | + |
| 186 | +### Flang |
| 187 | + |
| 188 | +```bash |
| 189 | +flang hello_world.f90 |
| 190 | +``` |
| 191 | + |
| 192 | +### Cray |
| 193 | + |
| 194 | +```bash |
| 195 | +ftn hello_world.f90 |
| 196 | +``` |
| 197 | + |
| 198 | +::: |
| 199 | + |
| 200 | +This created a file named `a.out`. |
| 201 | +This is the default executable name if no name is specified. |
| 202 | +To tell the compiler the name of the executable use the `-o` flag: |
| 203 | + |
| 204 | +::: group-tab |
| 205 | + |
| 206 | +### GFortran |
| 207 | + |
| 208 | +```bash |
| 209 | +gfortran -o hello_world hello_world.f90 |
| 210 | +``` |
| 211 | + |
| 212 | +### Intel |
| 213 | + |
| 214 | +```bash |
| 215 | +ifx -o hello_world hello_world.f90 |
| 216 | +``` |
| 217 | + |
| 218 | +### Flang |
| 219 | + |
| 220 | +```bash |
| 221 | +flang -o hello_world hello_world.f90 |
| 222 | +``` |
| 223 | + |
| 224 | +### Cray |
| 225 | + |
| 226 | +```bash |
| 227 | +ftn -o hello_world hello_world.f90 |
| 228 | +``` |
| 229 | + |
| 230 | +::: |
| 231 | + |
| 232 | +Now run `ls` to see the new executable: |
| 233 | + |
| 234 | +```bash |
| 235 | +ls |
| 236 | +``` |
| 237 | + |
| 238 | +```output |
| 239 | +hello_world hello_world.f90 |
| 240 | +``` |
| 241 | + |
| 242 | +We now have an executable called `hello_world`. |
| 243 | +Sometimes code can compile but fail when executed[^runtime-error]. |
| 244 | +Let's run this executable to check it works: |
| 245 | + |
| 246 | +```bash |
| 247 | +./hello_world |
| 248 | +``` |
| 249 | + |
| 250 | +```output |
| 251 | + Hello world! |
| 252 | +``` |
| 253 | + |
| 254 | +::::::::::::::::::::::::::::::::::::::: challenge |
| 255 | + |
| 256 | +## Compiler Documentation and Flags |
| 257 | + |
| 258 | +Take a moment to find and bookmark the documentation for your compiler. |
| 259 | +Find the correct flags to: |
| 260 | + |
| 261 | +- Turn on debugging |
| 262 | +- Specify the default optimisation level |
| 263 | + |
| 264 | +::::::::::::::: solution |
| 265 | + |
| 266 | +## Solution |
| 267 | + |
| 268 | +- [GNU gfortran command options](https://gcc.gnu.org/onlinedocs/gcc-4.3.6/gfortran/Invoking-GNU-Fortran.html#Invoking-GNU-Fortran) |
| 269 | +- [Intel ifx docs](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2025-0/overview.html) |
| 270 | +- [LLVM Flang command line reference](https://flang.llvm.org/docs/FlangCommandLineReference.html) |
| 271 | +- [Cray ftn docs](https://cpe.ext.hpe.com/docs/latest/cce/man1/crayftn.1.html) |
| 272 | + |
| 273 | +::::::::::::::::::::::::: |
| 274 | + |
| 275 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 276 | + |
| 277 | +## Filenames |
| 278 | + |
| 279 | +Fortran files normally end in `.f90`, although they can have different |
| 280 | +[file extensions](https://fortranwiki.org/fortran/show/File+extensions). |
| 281 | +The `.f90` extension is the most widely recognised across compilers. |
| 282 | +You may see Fortran files with an upper-case extension `.F90`. |
| 283 | +This tells the compiler to [pre-process](https://fortranwiki.org/fortran/show/Preprocessors) |
| 284 | +the file before compiling. |
| 285 | + |
| 286 | +In the next episode we will introduce variable declaration, |
| 287 | +and you will write your first Fortran program from scratch. |
| 288 | + |
| 289 | +:::::::::::::::::::::::::::::::::::::::: keypoints |
| 290 | + |
| 291 | +- Fortran programs start with the `program <label>` statement. |
| 292 | +- Fortran programs end with the `end program <label>` statement. |
| 293 | +- Always use `implicit none` to prevent implicit typing for variables. |
| 294 | +- Fortran comments start with `!`. |
| 295 | +- The `-o` flag specifies the name of the compiled executable: |
| 296 | + `<compiler_command> -o <executable_name> <source_file.f90>`. |
| 297 | + |
| 298 | +:::::::::::::::::::::::::::::::::::::::::::::::::: |
| 299 | + |
| 300 | +[^compiler-process]: Compilation steps for a C program: <https://www.geeksforgeeks.org/compiling-a-c-program-behind-the-scenes/>, the steps are the same for Fortran! |
| 301 | +[^runtime-error]: |
| 302 | + If a program fails to compile that's a _compiler error_. |
| 303 | + If a program compiles but fails to run that's a _runtime error_. |
0 commit comments