Grok all the things

grok (v): to understand (something) intuitively.

Fortran

👷‍♀ïļ  Professionals

Hello there, fellow engineers! Today, we're going to dive into the fascinating world of Fortran. You might have heard about this legendary programming language, which has played a remarkable role in the evolution of computing and continues to be a go-to language in many scientific and numerical domains. This long-lived language has stood the test of time and is still employed by professionals who appreciate its performance and robustness. So buckle up and join me on this journey as we explore the foundations, strengths, and peculiarities of Fortran!

A Quick Trip Down Memory Lane 🕰ïļ

Before we get into the nitty-gritty of Fortran, let's take a moment to appreciate how this language came into existence. In 1957, a team led by John Backus at IBM developed the first version of Fortran - then called FORTRAN (short for FORmula TRANslation). The primary goal was to simplify and automate the tedious process of coding mathematical algorithms for the IBM 704 mainframe. Little did they know that their creation would become one of the most enduring programming languages in history!

C AREA OF A CIRCLE
PI = 3.14159
READ INPUT TAPE 1, RADIUS
AREA = PI * RADIUS^2
PRINT OUTPUT TAPE 2, AREA
STOP
END

This is an example of early Fortran code to calculate the area of a circle.

Throughout its history, Fortran has paved the way for high-level languages and set the standard for scientific and engineering applications. As new computing paradigms emerged, the language evolved through several major revisions:

  1. FORTRAN II (1958)
  2. FORTRAN IV (1962)
  3. Fortran 66 (1966)
  4. Fortran 77 (1978)
  5. Fortran 90 (1991)
  6. Fortran 95 (1997)
  7. Fortran 2003 (2004)
  8. Fortran 2008 (2010)
  9. Fortran 2018 (2018)

Throughout this evolution, key features like array manipulation, parallel processing, and object-oriented programming have been added to the language, making Fortran a powerful choice for modern scientific computing.

The Beauty of Fortran ðŸŽĻ

If you've never encountered Fortran before, you might be wondering what all the fuss is about. Why has this language remained relevant for more than six decades? The answer lies in its simplicity, performance, and suitability for number-crunching tasks. Let's dig into some of the most notable features that have secured Fortran's place in the pantheon of programming languages.

Performance 🏃‍♂ïļ

In the realm of scientific computing, performance is often the top priority. Fortran compilers are known for their excellent optimization capabilities, which enable them to generate efficient and fast code for a wide range of hardware architectures. This quality has made Fortran a preferred choice for scientists and engineers working on computationally intensive tasks, like fluid dynamics simulations or quantum chemistry calculations.

Array Manipulation âœĻ

One of the key strengths of Fortran is its built-in support for array manipulation. Arrays are a fundamental aspect of numerical computing, and Fortran makes it easy to work with them using powerful, concise syntax. For instance, let's say you want to add two arrays A and B element-wise and store the result in array C. In Fortran, you can write:

C = A + B

This straightforward, intuitive syntax allows you to focus on the algorithm rather than getting bogged down with tedious loop constructs.

Built-in Parallelism 🚀

Modern Fortran has built-in support for parallelism, which allows you to make the most of today's multi-core processors and supercomputers. With features like DO CONCURRENT loops and Coarray Fortran, you can easily write parallel code that scales across thousands of cores without resorting to cumbersome libraries or APIs.

integer :: i
real :: array(10)[*] ! Declare coarray

! Parallel loop with DO CONCURRENT
do concurrent (i = 1:10)
  array(i)[this_image()] = this_image() + i * 2.0
end do

Interoperability ðŸĪ

As mentioned earlier, Fortran has been around for a long time. This means that a wealth of scientific code has been written in various versions of the language. One might think that this could lead to compatibility issues, but fear not! Modern Fortran offers excellent interoperability with older Fortran code as well as other languages like C and C++. This makes Fortran an appealing choice for those who need to interface with legacy codebases or integrate with other high-performance libraries.

The Quirky Side of Fortran ðŸĪŠ

Now that we've covered the pragmatic features that make Fortran a powerful language for scientific computing, let's explore some of the more peculiar aspects of its syntax and behavior.

Column-based Formatting 🗂ïļ

In early versions of Fortran, source code formatting was strictly column-based, meaning that certain columns had specific meanings.

  • Columns 1-5: Statement labels
  • Column 6: Continuation character
  • Columns 7-72: Source code

This quirky formatting made for some interesting challenges when writing and maintaining code. Although modern free-form Fortran has done away with these limitations, you may still encounter old-school column-based code in the wild.

Implicit Typing ðŸ’Ŧ

Fortran has an unconventional approach to variable typing known as "implicit typing". By default, variables with names starting with the letters I through N are assumed to be integers, while others are assumed to be floating-point numbers. While this can lead to more compact code, it can also cause confusion and bugs if not managed carefully.

implicit none ! Disable implicit typing

integer :: i, j, k
real :: a, b, c

Thankfully, modern Fortran includes the IMPLICIT NONE statement to disable implicit typing and require explicit declaration of all variables.

Fortran in the 21st Century 🌐

Despite its age, Fortran is far from being a relic of the past. It continues to be actively developed and supported by a vibrant community of programmers. There are numerous compilers available, such as GNU Fortran (gfortran), Intel Fortran (ifort), and NAG Fortran (nagfor), which deliver excellent performance and support the latest language features.

Moreover, Fortran has embraced modern programming paradigms, like object-oriented programming and functional programming, making it a versatile choice for scientific computing tasks. If you're working in a field that requires heavy numerical computation and highest performance, don't overlook the timeless powerhouse that is Fortran!

In conclusion, Fortran has a rich history and a bright future. Its unique combination of performance, robustness, and ease of use make it an enduring choice for engineers and scientists tackling complex computational problems. So next time you're seeking to "grok" a new language for your numerical adventures, I encourage you to give Fortran a try – you might just fall in love with its timeless charm!

Grok.foo is a collection of articles on a variety of technology and programming articles assembled by James Padolsey. Enjoy! And please share! And if you feel like you can donate here so I can create more free content for you.