appendix A. Setting up the Fortran development environment

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.

A.1 Editing Fortran source files

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.

Fortran source file extensions

Although the Fortran Standard doesn’t impose a constraint on what the Fortran source file extension should be, compiler vendors have adopted an almost-general set of rules:

  • File names with the suffix .f, .for, or .ftn are interpreted as fixed-form (FORTRAN 77 and older) code.
  • File names with the suffix .f90, .f95, .f03, or .f08 are interpreted as free-form.
  • File names with an uppercase suffix (like .F, or .F90) indicate to the compiler that the files should be preprocessed.

All code that we’ll write in this book is free-form. Free-form simply refers to a more liberal syntax that was introduced with the Fortran 90 standard. Since all compilers that I know of support .f90 as the universal suffix for free-form code (for example, the Intel compiler by default doesn’t support .f95, .f03, or .f08), we’ll use this extension throughout the book.

A.2 Setting up the Fortran compiler

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:

  • Free to download, use, and modify

  • Easy to install on most operating systems

  • Actively developed

  • Implements most standard features, including some from the latest Fortran 2018 Standard

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).

Linux

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.

Permissions

You’ll need administrator (root) permissions to install the compiler using the package manager. If you know what you’re doing and your username is already on the sudoers list, just prepend sudo to the Linux install commands I’ve provided and you’re good to go.

macOS

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.

Windows

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).

A.3 Setting up the MPI library (Message Passing Interface)

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.

A.4 Setting up OpenCoarrays

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.

Linux

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.

macOS

Installing OpenCoarrays on macOS is straightforward using brew:

brew install opencoarrays

Using 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.

Why do we need OpenCoarrays?

Gfortran fully supports the Fortran Coarray syntax of the 2008 Standard. However, on its own, gfortran doesn’t yet have a built-in mechanism for parallel computing with coarrays. This means that with plain gfortran, you can compile coarray programs but run them using only a single image (serial) mode. Although this can be useful for early development and testing, we need to be able to run our programs on multiple images in parallel. This is where OpenCoarrays come in.

Note that you only need OpenCoarrays if you work with gfortran. If you’re on a system that has the Intel or Cray compiler suite set up, you’re good to go with building Coarray Fortran code.

A.5 Building a Docker image

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

To run it, type

docker run -it modern-fortran:latest /bin/bash

and you’re off to the races!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.161.222