Well, maybe “joy” is a strong word. But over the past couple of days, I’ve been programming in Fortran a bit for the first time in about two years. Fortran, for the uninitiated, is the oldest programming language still in widespread use today. It gets a lot of grief (much of it deserved, some not) for being a kind of blockheaded dinosaur language, written, so its critics would have you believe, for the sole use of crew-cut IBM engineers wearing pocket protectors and skinny blue ties. I do a lot of programming for my research, but most of it is using Python/SciPy and R. Both are much easier to write in than Fortran, and they are both wonderful for 99% of what I need to do.
But I hit a snag this week: I need to compute empirical semivariograms for a large dataset. This is a statistic that requires comparing every point in the dataset to every other point. SciPy and R do vector-based math very efficiently, but pairwise comparisons do not lend themselves to vector-based math. The number of calculations increases as the number of datapoints squared, and trying to do this kind of brute-force calculation in pure Python or R is just too damn slow.
Fortran to the rescue. It may be a blockheaded dinosaur, it may have been invented to calculate the trajectories of missiles launched at the Russkies, but it isn’t slow. I wrote the relevant section of the program in Fortran, and then, using a tool called f2py, packaged it as a Python module. The result: an empirical semivariogram, in about 1/70 th the time. This process is kind of a hassle—not least because it required struggling with f2py for two days to get it working properly. I wouldn’t want to write large programs in Fortran. But I can see myself using it to clear certain computational bottlenecks now and then.
It’s kind of cool to still be coding in the direct descendent of the first real programming language. There are many scientists out there still using Fortran, especially in number-crunchy fields like weather and climate modeling, both because of its speed, and because it has been the lingua franca of these models for decades. And there is a kind of stark beauty in Fortran code. I had a little moment last winter at the Museum of Science in Boston, where I found, in a corner of the permanent “computers” exhibit, a copy of the program that guided the Apollo lander down to the Moon. It was a stack of Fortran punch-cards in a glass case.
You’ve got to know your roots…
One Response to The Joy of Fortran