This is not your parents’ Fortran.
In the context of programming languages, Fortran is all of the following:
- Compiled –You’ll write whole programs and pass them to the compiler before executing them. This is in contrast to interpreted programming languages like Python or JavaScript, which are parsed and executed line by line. Although this makes writing programs a bit more tedious, it allows the compiler to generate efficient executable code. In typical use cases, it’s not uncommon for Fortran programs to be one or two orders of magnitude faster than equivalent Python programs.What is a compiler?A compiler is a computer program that reads source code written in one programming language and translates it to equivalent code in another programming language. In our case, a Fortran compiler will read Fortran source code and generate appropriate assembly code and machine (binary) instructions.
- Statically typed –In Fortran, you’ll declare all variables with a type, and they’ll remain of that type until the end of the program:real :: pi ❶ pi = 3.141592 ❷❶ pi must be declared before use.❷ pi remains a “real” number until the program halts.You’ll also need to explicitly declare the variables before their use, which is known as manifest typing. Finally, Fortran employs so-called strong typing, which means that the compiler will raise an error if a procedure is invoked with an argument of the wrong type. While static typing helps the compiler to generate efficient programs, manifest and strong typing enforce good programming hygiene and make Fortran a safe language. I find it’s easier to write correct Fortran programs than Python or Javascript, which come with many hidden caveats and “gotchas.”
- Multiparadigm –You can write Fortran programs in several different paradigms, or styles: imperative, procedural, object-oriented, and even functional. Some paradigms are more appropriate than others, depending on the problem you’re trying to solve. We’ll explore different paradigms as we develop code throughout the book.
- Parallel –Fortran is also a parallel language. Parallelism is the capability to split the computational problem between processes that communicate through a network. Parallel processes can be running on the same processing core (thread-based parallelism), on different cores that share RAM (shared-memory parallelism), or distributed across the network (distributed-memory parallelism). Computers working together on the same parallel program can be physically located in the same cabinet, across the room from each other, or across the world. Fortran’s main parallel structure is a coarray, which allows you to express parallel algorithms and remote data exchange without any external libraries. Coarrays allow you to access remote memory just like you’d access elements of an array, as shown in the following listing.Listing 1.1 Example data exchange between parallel imagesprogram hello_coarrays implicit none integer :: a[*] ❶ integer :: i a = this_image() ❷ if (this_image() == 1) then ❸ do i = 1, num_images() ❹ print *, ‘Value on image’, i, ‘is’, a[i] ❺ end do end if end program hello_coarrays❶ Each image declares a local copy of an integer “a.”❷ Each image assigns its number (1, 2, 3, etc.) to “a.”❸ Only image 1 will enter this if block.❹ Iterates from 1 to the total number of images❺ For each remote image, image 1 will get the value of “a” on that image and print it to the screen.The Fortran standard doesn’t dictate how the data exchange is implemented under the hood; it merely specifies the syntax and the expected behavior. This allows the compiler developers to use the best approach available on any specific hardware. Given a capable compiler and libraries, a Fortran programmer can write code that runs on conventional CPUs or general-purpose GPUs alike. Listing 1.1 is meant for illustration; however, if you’d like to compile and run it, do so after following the instructions in Appendix A to set up your Fortran development environment.
- Mature –In 2016, we celebrated 60 years since the birth of Fortran. The language has evolved through several revisions of the standard:
- FORTRAN 66, also known as FORTRAN IV (ANSI, 1966)FORTRAN 77 (ANSI, 1978)Fortran 90 (ISO/IEC, 1991; ANSI, 1992)Fortran 95 (ISO/IEC, 1997)Fortran 2003 (ISO/IEC, 2004)Fortran 2008 (ISO/IEC, 2010)Fortran 2018 (ISO/IEC, 2018)
- Easy to learn –Believe it or not, Fortran is quite easy to learn. This was my experience and that of many of my colleagues. It’s easy to learn partly due to its strict type system, which allows the compiler to keep the programmer in check and warn them at compile time when they make a mistake. Although verbose, the syntax is clean and easy to read. However, like every other programming language or skill in general, Fortran is difficult to master. This is one of the reasons why I chose to write this book.
Fortran features