Before we dive into the code, let’s go over the basics of editing, compiling, and running a Fortran program. I’ll recommend a few text editors that I like and guide you through setting up the complete Fortran development environment.
If you’re familiar with Docker and want to skip all the tedious setup, jump ahead to section A.4, where I describe how to get the Modern Fortran Dockerfile that will get you up and running in no time.
You’ll write Fortran programs and modules as plain text files. You can edit them in your favorite text editor. Here are some popular choices:
Vim (Vi IMproved, https://www.vim.org/) is lightweight and powerful, although with a steep learning curve for beginners. This is my editor of choice. I picked it up when I first started programming in 2006, and never looked back.
Emacs (https://www.gnu.org/software/emacs) is a powerful and extensible editor, as well as one of the oldest applications still in mainstream use.
Atom (https://atom.io) is a modern, feature-rich, integrated development environment developed by GitHub. Atom also features a built-in package manager for adding third-party functionality to the editor.
Visual Studio Code (https://code.visualstudio.com) is another modern, full-featured integrated development environment, like Atom.
An important feature to look for in your text editor is whether it can highlight Fortran syntax with different colors. All of the editors I’ve listed can do so, either out-
of-the-box or by extension. They’re also free and open source, so if you don’t have a preferred editor yet, I suggest you try each of them and see which one feels most comfortable.
There are several high-quality Fortran compilers available. Most are developed and maintained by commercial vendors like Intel, Cray, and others. If you have access to one of these, great! Feel free to use them as you work through this book. Of course, for any compiler-specific settings or usage instructions, you’ll need to refer to your compiler’s documentation.
Otherwise, a free, open source Fortran compiler is available as part of the GNU Compiler Collection (gcc). For examples and exercises in this book, we’ll use the GNU Fortran Compiler (gfortran, https://gcc.gnu.org/fortran). In comparison to other compilers, here are the pros of gfortran:
During the writing of this book, more open source compilers have emerged and have still been in active development. In particular, keep an eye out for LFortran (https://lfortran.org) and Flang (https://github.com/flang-compiler/flang).
On most Linux systems, gfortran is easily installed using the system package manager, without going to an external resource. On DEB-based systems like Debian or Ubuntu, installing gfortran is as easy as
apt install gfortran
This command resolves the download and install steps for you, and gfortran
will be available as soon as the command finishes.
On RPM-based systems like Fedora or Centos, you can install gfortran like this:
dnf install gcc-gfortran
Alternatively, if gfortran is not available through your system’s package manager, you can download binaries from https://gcc.gnu.org/wiki/GFortranBinaries.
For macOS, I recommend that you use the homebrew package manager (https://brew.sh). Once you have homebrew set up on your system, installing the Fortran compiler is as simple as
brew install gcc
This command will install the base GNU Compiler Collection along with the Fortran compiler.
The easiest way to set up the development environment in Windows is through the Windows Subsystem for Linux. This is an Ubuntu Linux instance that runs natively in your Windows 10 operating system. If your Windows 10 is up to date, you can get the Ubuntu Linux system from the Windows App Store. Once you have it up and running, installing the Fortran compiler is easy:
apt install gfortran
Otherwise, some people have had success developing Fortran on Windows using Cygwin (https://www.cygwin.com).
In chapter 1, I used an example of data copy between processors to demonstrate the use of MPI for parallel programming. Although in this book we’ll focus exclusively on Coarray Fortran (CAF) for parallel algorithms, we still need to install the MPI library, as it’s used as a dependency for coarrays when using the GNU compiler (see the “Setting up OpenCoarrays” section).
I recommend either OpenMPI (https://www.open-mpi.org) or MPICH (https://www.mpich.org) as popular, high-quality, and easy-to-use MPI implementations. They’re available to install from Linux package managers. For example, if you use Ubuntu or another Debian-based distro, you can install OpenMPI with this command:
apt install openmpi-bin libopenmpi-dev
On an RPM-based distro like Fedora or Centos, type
dnf install openmpi openmpi-devel
Once installed, the MPI library provides an executable wrapper around the compiler. If installed correctly, you can verify this by typing mpif90
at the command prompt:
mpif90 gfortran: fatal error: no input files compilation terminated.
Don’t worry about this error message. It simply means that mpif90
correctly invoked gfortran
under the hood, and that we didn’t pass any source files to it.
OpenCoarrays (http://www.opencoarrays.org) provides the interface between the GNU Fortran compiler and the underlying parallel implementation; in our case, MPI. You don’t need to know much more than this. Think of it as an extension to gfortran
that will allow you to build and run parallel programs using coarrays.
If you already have access to a compute platform with Intel or Cray compiler suites installed, you won’t need OpenCoarrays and can skip to the next section.
Get the OpenCoarrays release directly from its GitHub repository:
git clone --branch 2.9.0 https://github.com/sourceryinstitute/OpenCoarrays
The simplest way to get up and running with OpenCoarrays is to build it from source. Given that we already have gfortran and OpenMPI built, compiling OpenCoarrays is relatively straightforward:
cd OpenCoarrays mkdir build cd build FC=gfortran CC=gcc cmake .. make make install
You’ll also need CMake (https://cmake.org) to build OpenCoarrays, as well as root privileges to do a make install
on your system.
The latest release of OpenCoarrays is 2.9.0 as of this writing. However, keep an eye on their Releases page (http://mng.bz/eQBG) and download a later version if available.
Installing OpenCoarrays on macOS is straightforward using brew
:
brew install opencoarrays
OpenCoarrays provide two executables:
caf
--A wrapper script for compiling Coarray Fortran programs
cafrun
--A wrapper script for running Coarray Fortran programs
When compiling CAF programs, we’ll use caf
as the drop-in replacement for our compiler; for example
caf array_copy_caf.f90 -o array_copy_caf
To run the CAF program, you’ll invoke it using the cafrun
script:
cafrun -n 2 array_copy_caf
This command invokes the array_copy_caf
program on two parallel processes. If two physical processors are available in the computer, both will be used. Otherwise, cafrun
will spawn two parallel threads running on the same processor. These details won’t impact the semantics of the program.
If you’re familiar with Docker and want to skip all this tedious setup and jump right into action, download the Dockerfile from http://mng.bz/pBZR.
To build the modern Fortran image, type
docker build . -t modern-fortran:latest
This step will take a while as Docker pulls the base OS image and sets up the image with the compiler, dependencies, and Fortran code from this book.
Once done, if the build is successful, you’ll be able to see your new image; for example
docker images REPOSITORY TAG IMAGE ID CREATED SIZE modern-fortran latest 0e5c745c8928 6 minutes ago 546MB
docker run -it modern-fortran:latest /bin/bash
3.137.161.222