Foreword |
|
xi | |
Preface |
|
xiii | |
Acknowledgments |
|
xiv | |
About this book |
|
xvi | |
About the author |
|
xxii | |
About the cover illustration |
|
xxiii | |
|
Part 1 Getting started with Modern Fortran |
|
|
1 | (54) |
|
|
3 | (23) |
|
|
4 | (2) |
|
|
6 | (2) |
|
|
8 | (2) |
|
1.4 Advantages and disadvantages |
|
|
10 | (2) |
|
Side-by-side comparison with Python |
|
|
10 | (2) |
|
1.5 Parallel Fortran, illustrated |
|
|
12 | (1) |
|
1.6 What will you learn in this book? |
|
|
13 | (1) |
|
|
14 | (8) |
|
Copying an array from one processor to another |
|
|
17 | (5) |
|
1.8 Running example: A parallel tsunami simulator |
|
|
22 | (3) |
|
|
22 | (1) |
|
|
23 | (1) |
|
What we want our app to do |
|
|
24 | (1) |
|
|
25 | (1) |
|
2 Getting started: Minimal working app |
|
|
26 | (29) |
|
2.1 Compiling and running your first program |
|
|
27 | (1) |
|
2.2 Simulating the motion of an object |
|
|
28 | (3) |
|
|
29 | (1) |
|
|
30 | (1) |
|
2.3 Implementing the minimal working app |
|
|
31 | (20) |
|
|
32 | (1) |
|
Defining the main program |
|
|
33 | (1) |
|
Declaring and initializing variables |
|
|
34 | (1) |
|
|
35 | (2) |
|
Declaring the data to use in our app |
|
|
37 | (3) |
|
Branching with an if block |
|
|
40 | (2) |
|
Using a do loop to iterate |
|
|
42 | (2) |
|
Setting the initial water height values |
|
|
44 | (1) |
|
Predicting the movement of the object |
|
|
45 | (2) |
|
Printing results to the screen |
|
|
47 | (1) |
|
|
47 | (4) |
|
2.4 Going forward with the tsunami simulator |
|
|
51 | (1) |
|
|
52 | (1) |
|
Exercise: Cold front propagation |
|
|
52 | (1) |
|
2.6 New Fortran elements, at a glance |
|
|
52 | (1) |
|
|
52 | (3) |
|
Part 2 Core elements of Fortran |
|
|
55 | (116) |
|
3 Writing reusable code with junctions and subroutines |
|
|
57 | (28) |
|
3.1 Toward higher app complexity |
|
|
58 | (7) |
|
Refactoring the tsunami simulator |
|
|
58 | (3) |
|
Revisiting the cold front problem |
|
|
61 | (2) |
|
An overview of Fortran program units |
|
|
63 | (2) |
|
3.2 Don't repeat yourself, use procedures |
|
|
65 | (7) |
|
|
65 | (5) |
|
Expressing finite difference as a function in the tsunami simulator |
|
|
70 | (2) |
|
3.3 Modifying program state with subroutines |
|
|
72 | (4) |
|
Defining and calling a subroutine |
|
|
72 | (2) |
|
When do you use a subroutine over a function? |
|
|
74 | (1) |
|
Initializing water height in the tsunami simulator |
|
|
75 | (1) |
|
3.4 Writing pure procedures to avoid side effects |
|
|
76 | (1) |
|
What is a pure procedure? |
|
|
76 | (1) |
|
Some restrictions on pure procedures |
|
|
77 | (1) |
|
Why are pure functions important'? |
|
|
77 | (1) |
|
3.5 Writing procedures that operate on both scalars and arrays |
|
|
77 | (2) |
|
3.6 Procedures with optional arguments |
|
|
79 | (2) |
|
3.7 Tsunami simulator: Putting it all together |
|
|
81 | (1) |
|
|
82 | (1) |
|
Exercise 1 Modifying state with a subroutine |
|
|
82 | (1) |
|
Exercise 2 Writing an elemental function that operates on both scalars and arrays |
|
|
83 | (1) |
|
3.9 New Fortran elements, at a glance |
|
|
83 | (1) |
|
|
84 | (1) |
|
4 Organizing your Fortran code using modules |
|
|
85 | (25) |
|
|
86 | (5) |
|
Getting compiler version and options |
|
|
86 | (3) |
|
Using portable data types |
|
|
89 | (2) |
|
4.2 Creating your first module |
|
|
91 | (8) |
|
The structure of a custom module |
|
|
92 | (1) |
|
|
93 | (2) |
|
Compiling Fortran modules |
|
|
95 | (2) |
|
Controlling access to variables and procedures |
|
|
97 | (1) |
|
Putting it all together in the tsunami simulator |
|
|
98 | (1) |
|
4.3 Toward realistic wave simulations |
|
|
99 | (8) |
|
A brief look at the physics |
|
|
101 | (1) |
|
Updating the finite difference calculation |
|
|
102 | (2) |
|
Renaming imported entities to avoid name conflict |
|
|
104 | (1) |
|
|
105 | (2) |
|
|
107 | (1) |
|
Exercise 1 Using portable type kinds in the tsunami simulator |
|
|
107 | (1) |
|
Exercise 2 Defining the set_gaussian subroutine in a module |
|
|
107 | (1) |
|
4.5 New Fortran elements, at a glance |
|
|
108 | (1) |
|
|
108 | (2) |
|
5 Analyzing time series data with arrays |
|
|
110 | (33) |
|
5.1 Analyzing stock prices with Fortran arrays |
|
|
111 | (3) |
|
Objectives for this exercise |
|
|
111 | (1) |
|
|
112 | (2) |
|
Getting the data and code |
|
|
114 | (1) |
|
5.2 Finding the best and worst performing stocks |
|
|
114 | (18) |
|
|
116 | (2) |
|
|
118 | (3) |
|
Reading stock data from files |
|
|
121 | (1) |
|
Allocating arrays of a certain size or range |
|
|
122 | (1) |
|
Allocating an array from another array |
|
|
123 | (1) |
|
Automatic allocation on assignment |
|
|
123 | (1) |
|
|
124 | (2) |
|
Checking for allocation status |
|
|
126 | (1) |
|
Catching allocation and deallocation errors |
|
|
126 | (1) |
|
Implementing the CSV reader subroutine |
|
|
127 | (2) |
|
Indexing and slicing arrays |
|
|
129 | (3) |
|
5.3 Identifying risky stocks |
|
|
132 | (3) |
|
5.4 Finding good times to buy and sell |
|
|
135 | (4) |
|
|
139 | (2) |
|
Exercise 1 Convenience (de)allocator subroutines |
|
|
139 | (1) |
|
Exercise 2 Reversing an array |
|
|
140 | (1) |
|
Exercise 3 Calculating moving average and standard deviation |
|
|
140 | (1) |
|
5.6 New Fortran elements, at a glance |
|
|
141 | (1) |
|
|
141 | (2) |
|
6 Reading, uniting, and formatting your data |
|
|
143 | (28) |
|
6.1 Your first I/O: Input from the keyboard and output to the screen |
|
|
144 | (7) |
|
|
144 | (3) |
|
Reading and uniting multiple variables at once |
|
|
147 | (1) |
|
Standard input, output, and error |
|
|
148 | (3) |
|
6.2 Formatting numbers and text |
|
|
151 | (6) |
|
Designing the aircraft dashboard |
|
|
151 | (1) |
|
Formatting strings, broken down |
|
|
152 | (5) |
|
Format statements in legacy Fortran code |
|
|
157 | (1) |
|
6.3 Writing to files on disk: A minimal note-taking app |
|
|
157 | (11) |
|
Opening a file and writing to it |
|
|
158 | (1) |
|
|
159 | (2) |
|
|
161 | (1) |
|
|
162 | (1) |
|
Opening files in read-only or write-only mode |
|
|
163 | (1) |
|
Checking whether a file exists |
|
|
164 | (3) |
|
Error handling and closing the file |
|
|
167 | (1) |
|
|
168 | (1) |
|
Exercise: Redirect stdout and stderr to files |
|
|
168 | (1) |
|
6.5 New Fortran elements, at a glance |
|
|
169 | (2) |
|
Part 3 Advanced Fortran use |
|
|
171 | (120) |
|
7 Going parallel with Fortran coarrays |
|
|
173 | (29) |
|
7.1 Why write parallel programs? |
|
|
174 | (1) |
|
7.2 Processing real-world weather buoy data |
|
|
175 | (6) |
|
|
176 | (2) |
|
Getting the data and code |
|
|
178 | (1) |
|
|
178 | (1) |
|
Serial implementation of the program |
|
|
179 | (2) |
|
7.3 Parallel processing with images and coarrays |
|
|
181 | (6) |
|
|
182 | (1) |
|
Getting information about the images |
|
|
183 | (1) |
|
Telling images what to do |
|
|
184 | (2) |
|
Gathering all data to a single image |
|
|
186 | (1) |
|
7.4 Coarrays and synchronization, explained |
|
|
187 | (5) |
|
|
188 | (1) |
|
Allocating dynamic coarrays |
|
|
188 | (1) |
|
Sending and receiving data |
|
|
189 | (2) |
|
Controlling the order of image execution |
|
|
191 | (1) |
|
7.5 Toward the parallel tsunami simulator |
|
|
192 | (7) |
|
|
192 | (2) |
|
Finding the indices of neighbor images |
|
|
194 | (1) |
|
|
195 | (1) |
|
|
196 | (3) |
|
|
199 | (2) |
|
Exercise 1 Finding the array subranges on each image |
|
|
199 | (1) |
|
Exercise 2 Writing a function that returns the indices of neighbor images |
|
|
200 | (1) |
|
7.7 New Fortran elements, at a glance |
|
|
201 | (1) |
|
|
201 | (1) |
|
8 Working with abstract data using derived types |
|
|
202 | (34) |
|
8.1 Recasting the tsunami simulator with derived types |
|
|
203 | (3) |
|
8.2 Defining, declaring, and initializing derived types |
|
|
206 | (14) |
|
|
209 | (1) |
|
Instantiating a derived type |
|
|
210 | (2) |
|
Accessing derived type components |
|
|
212 | (1) |
|
Positional vs. keyword arguments in derived type constructors |
|
|
212 | (2) |
|
Providing default values for derived type components |
|
|
214 | (1) |
|
Writing a custom type constructor |
|
|
215 | (3) |
|
Custom type constructor for the Field type |
|
|
218 | (2) |
|
8.3 Binding procedures to a derived type |
|
|
220 | (4) |
|
Your first type-bound method |
|
|
220 | (1) |
|
Type-bound methods for the Field type |
|
|
221 | (1) |
|
Controlling access to type components and methods |
|
|
222 | (2) |
|
|
224 | (1) |
|
8.4 Extending tsunami to two dimensions |
|
|
224 | (7) |
|
Going from 1-D to 2-D arrays |
|
|
225 | (1) |
|
Updating the equation set |
|
|
226 | (1) |
|
Finite differences in x and y |
|
|
226 | (2) |
|
Passing a class instance to diffx and diffy functions |
|
|
228 | (1) |
|
Derived type implementation of the tsunami solver |
|
|
229 | (2) |
|
|
231 | (3) |
|
Exercise 1 Working with private components |
|
|
231 | (2) |
|
Exercise 2 Invoking a type-bound method from an array of instances |
|
|
233 | (1) |
|
Exercise 3 Computing finite difference in y direction |
|
|
233 | (1) |
|
8.6 New Fortran elements, at a glance |
|
|
234 | (1) |
|
|
235 | (1) |
|
9 Generic procedures and operators for any data type |
|
|
236 | (28) |
|
9.1 Analyzing weather data of different types |
|
|
237 | (5) |
|
|
238 | (3) |
|
|
241 | (1) |
|
Strategy for this exercise |
|
|
242 | (1) |
|
9.2 Type systems and generic procedures |
|
|
242 | (1) |
|
Static versus strong typing |
|
|
242 | (1) |
|
9.3 Writing your first generic procedure |
|
|
243 | (10) |
|
The problem with strong typing |
|
|
243 | (1) |
|
Writing the specific functions |
|
|
244 | (3) |
|
Writing the generic interface |
|
|
247 | (4) |
|
Results and complete program |
|
|
251 | (2) |
|
9.4 Built-in and custom operators |
|
|
253 | (6) |
|
|
253 | (1) |
|
Things to do with operators |
|
|
253 | (2) |
|
Fortran's built-in operators |
|
|
255 | (2) |
|
|
257 | (1) |
|
|
257 | (1) |
|
Redefining built-in operators |
|
|
258 | (1) |
|
9.5 Generic procedures and operators in the tsunami simulator |
|
|
259 | (1) |
|
Writing user-defined operators for the Field type |
|
|
259 | (1) |
|
|
260 | (3) |
|
Exercise 1 Specific average function for a derived type |
|
|
260 | (2) |
|
Exercise 2 Defining a new string concatenation operator |
|
|
262 | (1) |
|
9.7 New Fortran elements, at a glance |
|
|
263 | (1) |
|
10 User-defined operators for derived types |
|
|
264 | (27) |
|
10.1 Happy Birthday! A countdown app |
|
|
265 | (1) |
|
|
265 | (1) |
|
|
266 | (1) |
|
10.2 Getting user input and current time |
|
|
266 | (6) |
|
Your first datetime class |
|
|
266 | (1) |
|
|
267 | (4) |
|
Getting current date and time |
|
|
271 | (1) |
|
10.3 Calculating the difference between two times |
|
|
272 | (10) |
|
|
273 | (1) |
|
Implementing a custom subtraction operator |
|
|
273 | (2) |
|
Time difference algorithm |
|
|
275 | (5) |
|
|
280 | (2) |
|
10.4 Overriding operators in the tsunami simulator |
|
|
282 | (6) |
|
A refresher on the Field class |
|
|
283 | (1) |
|
Implementing the arithmetic for the Field class |
|
|
284 | (2) |
|
Synchronizing parallel images on assignment |
|
|
286 | (2) |
|
|
288 | (2) |
|
Exercise 1 Validating user input |
|
|
288 | (1) |
|
Exercise 2 Leap year in the Gregorian calendar |
|
|
289 | (1) |
|
Exercise 3 Implementing the addition for the Field type |
|
|
289 | (1) |
|
10.6 New Fortran elements, at a glance |
|
|
290 | (1) |
|
|
291 | (64) |
|
11 Interoperability with C: Exposing your app to the web |
|
|
293 | (33) |
|
11.1 Interfacing C: Writing a minimal TCP client and server |
|
|
294 | (3) |
|
Introducing networking to Fortran |
|
|
295 | (2) |
|
|
297 | (1) |
|
11.2 TCP server program: Receiving network connections |
|
|
297 | (20) |
|
IP address data structures |
|
|
299 | (2) |
|
Initializing the IP address structure |
|
|
301 | (5) |
|
Checking IP address values |
|
|
306 | (2) |
|
Intermezzo: Matching compatible C and Fortran data types |
|
|
308 | (2) |
|
Creating a socket and listening for connections |
|
|
310 | (1) |
|
Accepting incoming connections to a socket |
|
|
311 | (1) |
|
Sending a TCP message to the client |
|
|
312 | (3) |
|
|
315 | (2) |
|
11.3 TCP client program: Connecting to a remote server |
|
|
317 | (5) |
|
Connecting to a remote socket |
|
|
317 | (2) |
|
|
319 | (2) |
|
The complete client program |
|
|
321 | (1) |
|
11.4 Some interesting mixed Fortran-C projects |
|
|
322 | (1) |
|
|
322 | (2) |
|
Exercise 1 The Fortran interface to ipaddr_port |
|
|
322 | (1) |
|
Exercise 2 Fortran interfaces to suffix_detach and tcp_close |
|
|
323 | (1) |
|
11.6 New Fortran elements, at a glance |
|
|
324 | (1) |
|
|
324 | (2) |
|
12 Advanced parallelism with teams, events, and collectives |
|
|
326 | (29) |
|
12.1 From coarrays to teams, events, and collectives |
|
|
327 | (1) |
|
12.2 Grouping images into teams with common tasks |
|
|
328 | (10) |
|
Teams in the tsunami simulator |
|
|
329 | (2) |
|
|
331 | (1) |
|
Changing execution between teams |
|
|
332 | (3) |
|
Synchronizing teams and exchanging data |
|
|
335 | (3) |
|
12.3 Posting and waiting for events |
|
|
338 | (5) |
|
A push notification example |
|
|
339 | (2) |
|
|
341 | (1) |
|
|
341 | (1) |
|
|
342 | (1) |
|
12.4 Distributed computing using collectives |
|
|
343 | (4) |
|
Computing the minimum and maximum of distributed arrays |
|
|
343 | (2) |
|
Collective subroutines syntax |
|
|
345 | (1) |
|
Broadcasting values to other images |
|
|
346 | (1) |
|
|
347 | (6) |
|
Exercise 1 Hunters and gatherers |
|
|
347 | (3) |
|
Exercise 2 Tsunami time step logging using events |
|
|
350 | (1) |
|
Exercise 3 Calculating the global mean of water height |
|
|
351 | (2) |
|
12.6 New Fortran elements, at a glance |
|
|
353 | (1) |
|
|
353 | (2) |
Appendix A Setting up the Fortran development environment |
|
355 | (6) |
Appendix B From calculus to code |
|
361 | (5) |
Appendix C Concluding remarks |
|
366 | (15) |
Index |
|
381 | |