Modern Fortran for today and tomorrow

Up To Date

OpenCoarrays

I decided to build and test the latest GFortran and OpenCoarrays on CentOS 7.2, which comes with an older version of GFortran, so my first step was to install the latest and greatest version, GCC 6.2.0. Believe it or not, building and installing a new GCC is not as difficult as it would seem. If you install as many dependencies as possible using the packages of the distribution, it's much easier. In general, I follow the directions in the GCC wiki [29] and GNU GCC installation page [30].

I installed GCC 6.2.0 into my home directory, /home/laytonjb/bin/gcc-6.2.0, and then modified the environment variables $PATH and $LD_LIBRARY_PATH so the new compilers were used instead of the older ones. The command used to build and install GCC is:

$PWD/../gcc-6.2.0/configure --prefix=$HOME/gcc-6.2.0 --enable-languages=c,c++,fortran,go --disable-multilib

The next step was to build and install an MPI library using the GCC 6.2.0 compilers. The OpenCoarray website recommended MPICH first, so I built and installed MPICH-3.2 in /home/laytonjb/bin/mpich-3.2. Again, the environment variables $PATH and $LD_LIBRARY_PATH were modified in the .bashrc file to point to MPICH binaries and libraries.

After installing the MPI library, the next step was to build and install OpenCoarray (OCA). The latest stable version as of the writing of this article was 1.7.2:

./install.sh -i /home/laytonjb/bin/opencoarray-1.7.2

The OCA build didn't take too long and was much shorter than building GCC. At this point, I'm ready to start compiling and executing some CAF code!

The first example is very simple (Listing 10). This proverbial "hello world" program for Fortran coarrays was taken from the Wikipedia page for coarrays [24].

Listing 10

Hello World with Coarrays

01 program Hello_World
02   implicit none
03   character(len=20) :: name[*] ! scalar coarray, one "name" for each image.
04   ! Note: "name" is the local variable while "name[]" accesses the
05   ! variable in a specific image; "name[this_image()]" is the same as "name".
06
07   ! Interact with the user on Image 1; execution for all others pass by.
08   if (this_image() == 1) then
09      write(*,'(a)',advance='no') 'Enter your name: '
10      read(*,'(a)') name
11   end if
12   ! Distribute information to all images
13   call co_broadcast(name,source_image=1)
14
15   ! I/O from all images, executing in any order, but each record written is intact.
16   write(*,'(3a,i0)') 'Hello ',trim(name),' from image ', this_image()
17 end program Hello_world

Using GCC, particularly from GFortran, you have two ways to build and execute this code. The first way is to use the compile and run scripts provided by OCA. If the name of the source file is hello.f90, then you should compile and execute with:

$ caf hello.f90 -o hello
$ cafrun -np 4 ./hello
Enter your name: Jeff
Hello Jeff from image 1
Hello Jeff from image 4
Hello Jeff from image 3
Hello Jeff from image 2

The first line compiles the code and the second line executes it. The -np 4 in the cafrun command tells the application to use four images. The second way of building and executing coarray code is to build it with the mpif90 script and execute it using the mpirun script,

mpif90 -fcoarray=lib yourcoarray.f90 -L/path/to/libcaf_mpi.a-lcaf_mpi -o yourcoarray
$ mpirun -np <n> ./yourcoarray

where <n> is the number of images. The -fcoarray=lib compilation command adds in the all-important libraries.

Other, more complicated coarray Fortran code examples [31] are floating around the web, although not as many as for F90 or Fortran 2003. Because Fortran 2008 is so new, it looks like coarray examples haven't quite caught up yet.

A great place to find out what aspects of Fortran 2008 your compiler supports and does not support is the Fortran 2008 wiki [32]. In addition to CAF, Fortran 2008 has implemented submodules (additional structuring facilities for modules; supersedes ISO/IEC TR 19767:2005), Coarray Fortran, the DO CONCURRENT construct for loop iterations with no interdependencies, the CONTIGUOUS attribute to specify storage layout restrictions, the BLOCK construct (which can contain declarations of objects with construct scope), and recursive allocatable components as an alternative to recursive pointers in derived types, among other features.

Fortran 2015

The next evolution of Fortran, even though many compilers have yet to implement all of Fortran 2008, is Fortran 2015. The standard has been under discussion for several years and is still undergoing work, with the goal for a 2018 release [33].

In general, Fortran 2015 is intended to include minor revisions, some clarifications, and corrections to inconsistencies, as well as some new features. In general, Fortran 2015 has two "thrusts." The first is around improved C and Fortran integration, and the second is around enhancing coarrays.

Many of the Fortran/C interoperability standards for Fortran 2015 were aimed to help MPI 3.0. An article from "Dr. Fortran" [34] (see the "Dr. Fortran's Retirement" box) contains a discussion of the proposed Fortran/C changes and even has a couple of examples.

Dr. Fortran's Retirement

Dr. Fortran [35], better known as Steve Lionel, has been instrumental in Fortran development and usage for many years. Recently, he retired from Intel after 38 years in the Fortran business. He's not leaving Fortran completely, but he won't be quite as active. His blogs and tweets are a beacon to Fortran coders around the world; his blog posts are extremely well written, concise, and loaded with great information. All these years, he has not given up on what is arguably the best language for scientific and research coding. I wish him well and hope he will still pop up in the Fortran world from time to time.

A second feature is new additions to coarrays. The first addition is called "teams," which are collections of images in coarray applications that work together on a particular task. An application can have several teams working on separate tasks that then communicate their results to their parent.

From teams, you can also create subteams that can be dissolved and reformed dynamically. With subteams, if one image fails, you can capture the problem, recreate a new team, and proceed with your computations. Contrast this with MPI: If a rank fails, the entire application hangs or crashes.

The definition of a "failed image" reveals a problem of much discussion (or argument). For example, an image really might not be failed but just very slow for some reason. The complication is how to define "slow" (or stalled) and how to have Fortran detect and recover from these (slow or stalled) images. Initially, the Fortran team decided to add some synchronization constructs to detect that an image has failed. Although the final determination is not yet settled, the discussion is proceeding in the correct direction.

The second part of the coarray additions is called "events." This addition is very much as it sounds; that is, events allow an image to notify another image that it has completed a task and that it can proceed.

The third part of the coarray additions is called "atomics." Created in Fortran 2008, the concept of "atomic" objects allow for invisible operations. However, they have had limited support. In Fortran 2015, the following atomic operations were added: ADD, AND, CAS (compare and swap), OR, and XOR.

The fourth and last part of the coarray additions is called "collectives," which are intrinsic procedures for performing operations across all images of the current team. The new routines that have been defined (but are subject to change) are: CO_MAX, CO_MIN, CO_SUM, CO_BROADCAST, and CO_REDUCE.

As with previous versions of Fortran, some features are targeted for deprecation in Fortran 2015, including labeled DO loops, EQUIVALENCE, COMMON, and BLOCK DATA.

The features that are finally deleted from Fortran are the arithmetic IF and the non-block DO construct, which doesn't end in a CONTINUE or END DO.

Summary

Fortran 90 catapulted Fortran from a perceived "old" language to a modern language on equal footing with any other. It retained Fortran's history of simplicity and performance, but it added features that developers had been wanting, such as free-form input, dynamic arrays, pointers, derived types, modular programming (modules), and portable precision definitions (kind).

Furthermore, a number of older features were deprecated from the Fortran standard, a refreshing process that I believe allows the language to stay simple, with a focus on numerical performance. Although taking an older Fortran 77 program and updating it to Fortran 90 involved a bit of work, the result was definitely worth the effort, and you could even find tools, including some online tools, for autoconverting code from Fortran 77 to Fortran 90.

Fortran 95 and 2003 really pushed Fortran into the modern era by dropping old and unused features and adding newer, modern, and needed features. Fortran could now handle object-oriented programming and had much better and standardized integration with C. It also incorporated some features from HPF that improved code readability. Fortran was still oriented toward numerical computations, but now it was much easier to express programming concepts and write portable code.

The 2008 revision added concurrent computing via Coarray Fortran, and Fortran 2015, although still under development, hovers just within sight, with a projected release date of 2018.

Infos

  1. Modular programming: https://en.wikipedia.org/wiki/Modular_programming
  2. Linked list: https://en.wikipedia.org/wiki/Linked_list
  3. Generic linked lists: http://fortranwiki.org/fortran/show/Linked+list
  4. Linked list manager: http://degenerateconic.com/linked-lists/
  5. Fortran: https://en.wikipedia.org/wiki/Fortran
  6. Fortran 95: https://en.wikipedia.org/wiki/Fortran_95_language_features
  7. High Performance Fortran: https://en.wikipedia.org/wiki/High_Performance_Fortran
  8. High Performance Fortran Forum: http://hpff.rice.edu/
  9. 2D Poisson equation problem: http://www.math.chalmers.se/Math/Grundutb/CTH/tma881/1112/Assignments/MPI/poisson.pdf
  10. "How to write code in modern Fortran" by Lars Koesterke: https://portal.tacc.utexas.edu/documents/13601/162125/fortran_class.pdf
  11. HPF directives: http://hpff.rice.edu/versions/hpf1/hpf-v11/chapter2_4.html
  12. OpenACC: https://en.wikipedia.org/wiki/OpenACC
  13. OpenMP: https://en.wikipedia.org/wiki/OpenMP
  14. GFortran: https://en.wikipedia.org/wiki/GNU_Fortran
  15. Fortran 95 extensions: https://en.wikipedia.org/wiki/Fortran#Fortran_95
  16. OOP in Fortran 90: http://fortranwiki.org/fortran/show/Object-oriented+programming
  17. Object-based programming in Fortran 90: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.108.5397&rep=rep1&type=pdf
  18. OOP in Fortran 95: https://www.amazon.com/Object-Oriented-Programming-via-Fortran-90/dp/0521524083
  19. Comparison of Fortran 90 and C++: http://www1.gantep.edu.tr/~bingul/seminar/f90_c++/comparison.html
  20. Type-bound procedures: http://fortranwiki.org/fortran/show/Object-oriented+programming
  21. Fortran 2003 enhancements: https://en.wikipedia.org/wiki/Fortran#Fortran_2003
  22. Stream I/O in Fortran: http://fortranwiki.org/fortran/show/Stream+Input+Output
  23. Table 1 is from the Wikipedia article on concurrent computing: https://en.wikipedia.org/wiki/Concurrent_computing
  24. Coarray Fortran: https://en.wikipedia.org/wiki/Coarray_Fortran
  25. Partitioned Global Address Space: https://en.wikipedia.org/wiki/Partitioned_global_address_space
  26. CAF basics: http://caf.rice.edu/documentation/John-Reid-N1824-2010-04-21.pdf
  27. Coarray support in GFortran: https://gcc.gnu.org/wiki/Coarray
  28. OpenCoarrays: http://www.opencoarrays.org/
  29. GCC wiki: https://gcc.gnu.org/wiki/InstallingGCC
  30. GCC installation page: https://gcc.gnu.org/install/
  31. Coarray Fortran code examples: http://www.dursi.ca/coarray-fortran-goes-mainstream-gcc-5-1/
  32. Fortran 2008 wiki: http://fortranwiki.org/fortran/show/Fortran+2008+status
  33. Fortran 2015 release: https://en.wikipedia.org/wiki/Fortran
  34. "The Future of Fortran" by Steve Lionel: https://software.intel.com/en-us/blogs/2015/03/27/doctor-fortran-in-the-future-of-fortran
  35. Dr. Fortran: https://twitter.com/doctorfortran

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs



Support Our Work

ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

Learn More”>
	</a>

<hr>		    
			</div>
		    		</div>

		<div class=