0%

Book Description

Modern Fortran teaches you to develop fast, efficient parallel applications using twenty-first-century Fortran. In this guide, you'll dive into Fortran by creating fun apps, including a tsunami simulator and a stock price analyzer. Filled with real-world use cases, insightful illustrations, and hands-on exercises, Modern Fortran helps you see this classic language in a whole new light.

Table of Contents

  1. Modern Fortran
  2. Copyright
  3. contents
  4. front matter
    1. foreword
    2. preface
    3. acknowledgments
    4. about this book
    5. Who should read this book
    6. A bit of Fortran history
    7. How this book is organized: a roadmap
    8. About the code
    9. Requirements
    10. Get involved
    11. liveBook discussion forum
    12. about the author
    13. about the cover illustration
  5. Part 1. Getting started with Modern Fortran
  6. 1 Introducing Fortran
    1. 1.1 What is Fortran?
    2. 1.2 Fortran features
    3. 1.3 Why learn Fortran?
    4. 1.4 Advantages and disadvantages
      1. 1.4.1 Side-by-side comparison with Python
    5. 1.5 Parallel Fortran, illustrated
    6. 1.6 What will you learn in this book?
    7. 1.7 Think parallel!
      1. 1.7.1 Copying an array from one processor to another
    8. 1.8 Running example: A parallel tsunami simulator
      1. 1.8.1 Why tsunami simulator?
      2. 1.8.2 Shallow water equations
      3. 1.8.3 What we want our app to do
    9. 1.9 Further reading
    10. Summary
  7. 2 Getting started: Minimal working app
    1. 2.1 Compiling and running your first program
    2. 2.2 Simulating the motion of an object
      1. 2.2.1 What should our app do?
      2. 2.2.2 What is advection?
    3. 2.3 Implementing the minimal working app
      1. 2.3.1 Implementation strategy
      2. 2.3.2 Defining the main program
      3. 2.3.3 Declaring and initializing variables
      4. 2.3.4 Numeric data types
      5. 2.3.5 Declaring the data to use in our app
      6. 2.3.6 Branching with an if block
      7. 2.3.7 Using a do loop to iterate
      8. 2.3.8 Setting the initial water height values
      9. 2.3.9 Predicting the movement of the object
      10. 2.3.10 Printing results to the screen
      11. 2.3.11 Putting it all together
    4. 2.4 Going forward with the tsunami simulator
    5. 2.5 Answer key
      1. 2.5.1 Exercise: Cold front propagation
    6. 2.6 New Fortran elements, at a glance
    7. 2.7 Further reading
    8. Summary
  8. Part 2. Core elements of Fortran
  9. 3 Writing reusable code with functions and subroutines
    1. 3.1 Toward higher app complexity
      1. 3.1.1 Refactoring the tsunami simulator
      2. 3.1.2 Revisiting the cold front problem
      3. 3.1.3 An overview of Fortran program units
    2. 3.2 Don’t repeat yourself, use procedures
      1. 3.2.1 Your first function
      2. 3.2.2 Expressing finite difference as a function in the tsunami simulator
    3. 3.3 Modifying program state with subroutines
      1. 3.3.1 Defining and calling a subroutine
      2. 3.3.2 When do you use a subroutine over a function?
      3. 3.3.3 Initializing water height in the tsunami simulator
    4. 3.4 Writing pure procedures to avoid side effects
      1. 3.4.1 What is a pure procedure?
      2. 3.4.2 Some restrictions on pure procedures
      3. 3.4.3 Why are pure functions important?
    5. 3.5 Writing procedures that operate on both scalars and arrays
    6. 3.6 Procedures with optional arguments
    7. 3.7 Tsunami simulator: Putting it all together
    8. 3.8 Answer key
      1. 3.8.1 Exercise 1: Modifying state with a subroutine
      2. 3.8.2 Exercise 2: Writing an elemental function that operates on both scalars and arrays
    9. 3.9 New Fortran elements, at a glance
    10. 3.10 Further reading
    11. Summary
  10. 4 Organizing your Fortran code using modules
    1. 4.1 Accessing a module
      1. 4.1.1 Getting compiler version and options
      2. 4.1.2 Using portable data types
    2. 4.2 Creating your first module
      1. 4.2.1 The structure of a custom module
      2. 4.2.2 Defining a module
      3. 4.2.3 Compiling Fortran modules
      4. 4.2.4 Controlling access to variables and procedures
      5. 4.2.5 Putting it all together in the tsunami simulator
    3. 4.3 Toward realistic wave simulations
      1. 4.3.1 A brief look at the physics
      2. 4.3.2 Updating the finite difference calculation
      3. 4.3.3 Renaming imported entities to avoid name conflict
      4. 4.3.4 The complete code
    4. 4.4 Answer key
      1. 4.4.1 Exercise 1: Using portable type kinds in the tsunami simulator
      2. 4.4.2 Exercise 2: Defining the set_gaussian subroutine in a module
    5. 4.5 New Fortran elements, at a glance
    6. 4.6 Further reading
    7. Summary
  11. 5 Analyzing time series data with arrays
    1. 5.1 Analyzing stock prices with Fortran arrays
      1. 5.1.1 Objectives for this exercise
      2. 5.1.2 About the data
      3. 5.1.3 Getting the data and code
    2. 5.2 Finding the best and worst performing stocks
      1. 5.2.1 Declaring arrays
      2. 5.2.2 Array constructors
      3. 5.2.3 Reading stock data from files
      4. 5.2.4 Allocating arrays of a certain size or range
      5. 5.2.5 Allocating an array from another array
      6. 5.2.6 Automatic allocation on assignment
      7. 5.2.7 Cleaning up after use
      8. 5.2.8 Checking for allocation status
      9. 5.2.9 Catching allocation and deallocation errors
      10. 5.2.10 Implementing the CSV reader subroutine
      11. 5.2.11 Indexing and slicing arrays
    3. 5.3 Identifying risky stocks
    4. 5.4 Finding good times to buy and sell
    5. 5.5 Answer key
      1. 5.5.1 Exercise 1: Convenience (de)allocator subroutines
      2. 5.5.2 Exercise 2: Reversing an array
      3. 5.5.3 Exercise 3: Calculating moving average and standard deviation
    6. 5.6 New Fortran elements, at a glance
    7. 5.7 Further reading
    8. Summary
  12. 6 Reading, writing, and formatting your data
    1. 6.1 Your first I/O: Input from the keyboard and output to the screen
      1. 6.1.1 The simplest I/O
      2. 6.1.2 Reading and writing multiple variables at once
      3. 6.1.3 Standard input, output, and error
    2. 6.2 Formatting numbers and text
      1. 6.2.1 Designing the aircraft dashboard
      2. 6.2.2 Formatting strings, broken down
      3. 6.2.3 Format statements in legacy Fortran code
    3. 6.3 Writing to files on disk: A minimal note-taking app
      1. 6.3.1 Opening a file and writing to it
      2. 6.3.2 Opening a file
      3. 6.3.3 Writing to a file
      4. 6.3.4 Appending to a file
      5. 6.3.5 Opening files in read-only or write-only mode
      6. 6.3.6 Checking whether a file exists
      7. 6.3.7 Error handling and closing the file
    4. 6.4 Answer key
      1. 6.4.1 Exercise: Redirect stdout and stderr to files
    5. 6.5 New Fortran elements, at a glance
    6. Summary
  13. Part 3. Advanced Fortran use
  14. 7 Going parallel with Fortran coarrays
    1. 7.1 Why write parallel programs?
    2. 7.2 Processing real-world weather buoy data
      1. 7.2.1 About the data
      2. 7.2.2 Getting the data and code
      3. 7.2.3 Objectives
      4. 7.2.4 Serial implementation of the program
    3. 7.3 Parallel processing with images and coarrays
      1. 7.3.1 Fortran images
      2. 7.3.2 Getting information about the images
      3. 7.3.3 Telling images what to do
      4. 7.3.4 Gathering all data to a single image
    4. 7.4 Coarrays and synchronization, explained
      1. 7.4.1 Declaring coarrays
      2. 7.4.2 Allocating dynamic coarrays
      3. 7.4.3 Sending and receiving data
      4. 7.4.4 Controlling the order of image execution
    5. 7.5 Toward the parallel tsunami simulator
      1. 7.5.1 Implementation strategy
      2. 7.5.2 Finding the indices of neighbor images
      3. 7.5.3 Allocating the coarrays
      4. 7.5.4 The main time loop
    6. 7.6 Answer key
      1. 7.6.1 Exercise 1: Finding the array subranges on each image
      2. 7.6.2 Exercise 2: Writing a function that returns the indices of neighbor images
    7. 7.7 New Fortran elements, at a glance
    8. 7.8 Further reading
    9. Summary
  15. 8 Working with abstract data using derived types
    1. 8.1 Recasting the tsunami simulator with derived types
    2. 8.2 Defining, declaring, and initializing derived types
      1. 8.2.1 Defining a derived type
      2. 8.2.2 Instantiating a derived type
      3. 8.2.3 Accessing derived type components
      4. 8.2.4 Positional vs. keyword arguments in derived type constructors
      5. 8.2.5 Providing default values for derived type components
      6. 8.2.6 Writing a custom type constructor
      7. 8.2.7 Custom type constructor for the Field type
    3. 8.3 Binding procedures to a derived type
      1. 8.3.1 Your first type-bound method
      2. 8.3.2 Type-bound methods for the Field type
      3. 8.3.3 Controlling access to type components and methods
      4. 8.3.4 Bringing it all together
    4. 8.4 Extending tsunami to two dimensions
      1. 8.4.1 Going from 1-D to 2-D arrays
      2. 8.4.2 Updating the equation set
      3. 8.4.3 Finite differences in x and y
      4. 8.4.4 Passing a class instance to diffx and diffy functions
      5. 8.4.5 Derived type implementation of the tsunami solver
    5. 8.5 Answer key
      1. 8.5.1 Exercise 1: Working with private components
      2. 8.5.2 Exercise 2: Invoking a type-bound method from an array of instances
      3. 8.5.3 Exercise 3: Computing finite difference in y direction.
    6. 8.6 New Fortran elements, at a glance
    7. 8.7 Further reading
    8. Summary
  16. 9 Generic procedures and operators for any data type
    1. 9.1 Analyzing weather data of different types
      1. 9.1.1 About the data
      2. 9.1.2 Objectives
      3. 9.1.3 Strategy for this exercise
    2. 9.2 Type systems and generic procedures
      1. 9.2.1 Static versus strong typing
    3. 9.3 Writing your first generic procedure
      1. 9.3.1 The problem with strong typing
      2. 9.3.2 Writing the specific functions
      3. 9.3.3 Writing the generic interface
      4. 9.3.4 Results and complete program
    4. 9.4 Built-in and custom operators
      1. 9.4.1 What’s an operator?
      2. 9.4.2 Things to do with operators
      3. 9.4.3 Fortran’s built-in operators
      4. 9.4.4 Operator precedence
      5. 9.4.5 Writing custom operators
      6. 9.4.6 Redefining built-in operators
    5. 9.5 Generic procedures and operators in the tsunami simulator
      1. 9.5.1 Writing user-defined operators for the Field type
    6. 9.6 Answer key
      1. 9.6.1 Exercise 1: Specific average function for a derived type
      2. 9.6.2 Exercise 2: Defining a new string concatenation operator
    7. 9.7 New Fortran elements, at a glance
    8. Summary
  17. 10 User-defined operators for derived types
    1. 10.1 Happy Birthday! A countdown app
      1. 10.1.1 Some basic specification
      2. 10.1.2 Implementation strategy
    2. 10.2 Getting user input and current time
      1. 10.2.1 Your first datetime class
      2. 10.2.2 Reading user input
      3. 10.2.3 Getting current date and time
    3. 10.3 Calculating the difference between two times
      1. 10.3.1 Modeling a time interval
      2. 10.3.2 Implementing a custom subtraction operator
      3. 10.3.3 Time difference algorithm
      4. 10.3.4 The complete program
    4. 10.4 Overriding operators in the tsunami simulator
      1. 10.4.1 A refresher on the Field class
      2. 10.4.2 Implementing the arithmetic for the Field class
      3. 10.4.3 Synchronizing parallel images on assignment
    5. 10.5 Answer key
      1. 10.5.1 Exercise 1: Validating user input
      2. 10.5.2 Exercise 2: Leap year in the Gregorian calendar
      3. 10.5.3 Exercise 3: Implementing the addition for the Field type
    6. 10.6 New Fortran elements, at a glance
    7. Summary
  18. Part 4. The final stretch
  19. 11 Interoperability with C: Exposing your app to the web
    1. 11.1 Interfacing C: Writing a minimal TCP client and server
      1. 11.1.1 Introducing networking to Fortran
      2. 11.1.2 Installing libdill
    2. 11.2 TCP server program: Receiving network connections
      1. 11.2.1 IP address data structures
      2. 11.2.2 Initializing the IP address structure
      3. 11.2.3 Checking IP address values
      4. 11.2.4 Intermezzo: Matching compatible C and Fortran data types
      5. 11.2.5 Creating a socket and listening for connections
      6. 11.2.6 Accepting incoming connections to a socket
      7. 11.2.7 Sending a TCP message to the client
      8. 11.2.8 Closing a connection
    3. 11.3 TCP client program: Connecting to a remote server
      1. 11.3.1 Connecting to a remote socket
      2. 11.3.2 Receiving a message
      3. 11.3.3 The complete client program
    4. 11.4 Some interesting mixed Fortran-C projects
    5. 11.5 Answer key
      1. 11.5.1 Exercise 1: The Fortran interface to ipaddr_port
      2. 11.5.2 Exercise 2: Fortran interfaces to suffix_detach and tcp_close
    6. 11.6 New Fortran elements, at a glance
    7. 11.7 Further reading
    8. Summary
  20. 12 Advanced parallelism with teams, events, and collectives
    1. 12.1 From coarrays to teams, events, and collectives
    2. 12.2 Grouping images into teams with common tasks
      1. 12.2.1 Teams in the tsunami simulator
      2. 12.2.2 Forming new teams
      3. 12.2.3 Changing execution between teams
      4. 12.2.4 Synchronizing teams and exchanging data
    3. 12.3 Posting and waiting for events
      1. 12.3.1 A push notification example
      2. 12.3.2 Posting an event
      3. 12.3.3 Waiting for an event
      4. 12.3.4 Counting event posts
    4. 12.4 Distributed computing using collectives
      1. 12.4.1 Computing the minimum and maximum of distributed arrays
      2. 12.4.2 Collective subroutines syntax
      3. 12.4.3 Broadcasting values to other images
    5. 12.5 Answer key
      1. 12.5.1 Exercise 1: Hunters and gatherers
      2. 12.5.2 Exercise 2: Tsunami time step logging using events
      3. 12.5.3 Exercise 3: Calculating the global mean of water height
    6. 12.6 New Fortran elements, at a glance
    7. 12.7 Further reading
    8. Summary
  21. appendix A. Setting up the Fortran development environment
    1. A.1 Editing Fortran source files
    2. A.2 Setting up the Fortran compiler
      1. Linux
      2. macOS
      3. Windows
    3. A.3 Setting up the MPI library (Message Passing Interface)
    4. A.4 Setting up OpenCoarrays
      1. Linux
      2. macOS
      3. Using OpenCoarrays
    5. A.5 Building a Docker image
  22. appendix B. From calculus to code
    1. B.1 The advection equation explained
      1. B.1.1 Discretizing the derivatives
      2. B.1.2 Casting the derivatives into code
  23. appendix C. Concluding remarks
    1. C.1 Tsunami simulator: The complete code
      1. C.1.1 Main program: tsunami.f90
      2. C.1.2 The Field module: mod_field.f90
      3. C.1.3 The I/O module: mod_io.f90
      4. C.1.4 The parallel module: mod_parallel.f90
    2. C.2 Going forward with the tsunami simulator
    3. C.3 Neural networks and deep learning
    4. C.4 Online resources
    5. C.5 Compilers
    6. C.6 Books
  24. index
3.144.102.239