Asking for suggestions about books on modern Fortran

I have been using F77 and Fortran 90 for years. Now for the project I am currently working on, I would like to try the more recent Fortran (so-called modern Fortran?). Can you please suggest books that go through the modern features? I am particularly interested in the OOP paradigm included in these new versions. I know, derived types had been introduced since Fortran90 but from what I saw on some occasions, there seems to be more features associated with derived types that are added in these later Fortran versions, and I would like to learn the formal way. I am not entirely new to OOP, I have been using C++ and Python up to some degree, but

2 Likes

Welcome @diosium to the Fortran discourse. I am not an expert in OOP concepts in Fortran.
But here are a few suggestions including an online course at the University of Virginia.
1.Modern Fortran Explained
2. Online course at the University of Virginia, specifically see the classes part in there: RC Learning Portal

May be someone expert in OOP in Fortran can provide the best suggestions!!

best of luck on a new journey!!

2 Likes

Modern Fortran Explained (0xford 2018) by Metcalf, Reid and Cohen. A new edition is due out soon.

2 Likes

When I joined this discourse group I heard about Milan Curcic’s “Modern Fortran: Building Efficient Parallel Applications” from Manning Books. Milan is very active in this group.

5 Likes

New edition now listed at OUP: 07 December 2023 (Estimated)
Modern Fortran Explained
New color, new author.

3 Likes

Modern Fortran Explained is a good book for someone who already knows the basics of Fortran. For someone who has no prior experience with Fortran I prefer a true college level textbook. The only one of those I recommend is Stephen Chapman’s book,

Unlike MFE, the information is presented in a logical and consistent order suitable for classroom teaching.

Unfortunately is very pricey at least here in the U.S. I assume there is an international edition thats a lot cheaper.

I would consider Milan’s book a second choice. Once you are comfortable with the basics, I would also recommend Clerman and Spectors book, Modern Fortran - Style and Usage

Edit

I would add that Chapman’s book has (for me) the best explanation of Fortran OOP I’ve seen so far (at least in a book). Prior to reading his OOP chapter, I struggled with some of the basic OOP concepts since most of the examples of OOP were in other languages. Seeing these concepts expressed in Fortran suddenly made them understandable to my old Fortran centric brain.

I would also advise to not restrict yourself to just a book, there are several on-line courses, on-line books etc that might serve you just as well as a book will.

5 Likes

Welcome on the Discourse @diosium !

Yes, OOP was introduced in the Fortran 2003 standard. Many of the books published since will therefore have a chapter about OOP. At the bottom of the page Learn — Fortran Programming Language you will find many online ressources and a list of books.

If you want a book to quickly update your knowledge about latest Fortran standards, Milan Curcic’s Modern Fortran: Building Efficient Parallel Applications is a good choice. It can be read by beginners but also by confirmed Fortran developers not aware of the 21th century features.

And once your knowledge is updated, Modern Fortran Explained is an excellent reference book.

A more expert book on OOP is:
Rouson, D., Xia, J., and Xiaofeng, X. 2014 Scientific Software Design: The Object-Oriented Way 2nd ed Cambridge. Cambridge University Press

2 Likes

Introduction to Programming with Fortran by Ian Chivers and Jane Sleightholme seems like a tour de force intro to programming with modern Fortran. (If you’re an experienced programmer, much can be skimmed over.) But they cover a lot of Fortran in depth.

2 Likes

Thanks! I was a bit surprised I found this forum, when I started programming in Fortran intensively6 years ago, my only go-to-places for help was only StackOverflow and intel’s Fortran forum. I don’t remember seeing a Fortran-specific forum.

1 Like

The link does not work. It says “page not found”.

There have been great suggestions already. Though I appreciate people recommending my book (thank you!), @diosium, you should be aware that my book only scratches the surface regarding OOP. For example, it doesn’t cover extends and abstract features which are important elements of Fortran OOP.

4 Likes

I think a new version of Modern Fortran Explained that includes the new f2023 features is due to be available shortly.

I appreciate your transparency regarding what your book doesn’t cover @milancurcic. It does seem to be among the popular ones on Amazon US though, I will see which one I am going to get.

I would also add that people should obtain a copy of Milan’s book for his excellent coverage of co-arrays. Now if the compiler developers can just get co-arrays to work as well on a 8-16-32 core workstation as it does on a 100K plus core Cray then all the work thats been put into co-arrays in the recent standards will be worth it. Unfortunately, I’ve not been able to get it to perform any better than MPI if it runs at all.

2 Likes

I find this one

2 Likes

That is true. The new edition should be published on 7 December and is available for pre-ordering on the OUP and some Amazon websites already. The text has been completely revised and reordered. It contains two completely new chapters on the F2023 features. Further, the extended examples will be downloadable. This applies not only to the extisting OOP example, but also to three complete, new examples of coarray code. And, of course, we are joined by Reinhold Bader as a fourth author.

Mike Metcalf

8 Likes

One of the main features of modern Fortran is do concurrent. Somehow it is missing in its entirety in all books. Intel’s Fortran compiler now supports the reduce clause. This is significant!

The new version Modern Fortran Explained 2023 has two chapters (17 and 18) on the Coarrays but not on do concurrent. Perhaps a chapter on concurrent programming would be great as we have on Object-oriented Programming (chapter 15) and generic programming (chapter 22). Maybe its importance is hidden again in chapter 7 on the array features (as in the previous edition).

2 Likes

Modern Fortran Explained (2018) did not hide do concurrent: the index tells you where to look (pages 137, 435, 436). It also mentions the reduce clause (page 432). Presumably the authors thought those topics didn’t need a whole chapter. Next month will reveal what the 2023 edition says.

1 Like

Jane and I only have one do concurrent example. Here is the source

include ‘integer_kind_module.f90’
include ‘precision_module.f90’
include ‘timing_module.f90’

program ch3305

use timing_module
use precision_module
use omp_lib

use iso_fortran_env

implicit none

integer, parameter :: n = 100000000
integer, parameter :: loop_count = 10
integer, parameter :: n_types = 4
integer :: i
integer :: j
integer :: nthreads

real (dp), allocatable, dimension (:slight_smile: :: x
real (dp), allocatable, dimension (:slight_smile: :: y
real (dp), allocatable, dimension (:slight_smile: :: z

real, dimension (n_types, loop_count) :: timing_details = 0.0
real, dimension (n_types) :: t_sum = 0.0
real, dimension (n_types) :: t_average = 0.0
real :: reset = 0.0

character (15), dimension (n_types) :: heading_1 = &
[ ’ Whole array ', &
’ Do loop ', &
’ Do concurrent ', &
’ openmp ’ ]

print * , ’ ’
print *,compiler_version()
print *,compiler_options()
print *, ’ ’
call start_timing()
print * , ’ ’

nthreads = omp_get_max_threads()
open (unit=20, file=‘ch3305.dat’)
print 100, nthreads
100 format (’ Nthreads = ', i3)

!
! Dynamic allocation

allocate (x(n))
allocate (y(n))
allocate (z(n))

!
! Initialisation
!

call random_number(x)
call random_number(y)
z = 0.0_dp

print 110, time_difference()
110 format (’ Initialise time = ', f9.6)
write (20, 120) x(1), y(1), z(1)
120 format (3(2x,f6.3))
print *, ’ ’

do j = 1, loop_count

print 130, j
130 format (' Iteration = ', i3)

!
! Whole array syntax
!

z = x + y
timing_details(1, j) = time_difference()
write (20, 120) x(1), y(1), z(1)
z = 0.0_dp
reset = time_difference()

!
! Simple traditional do loop
!

do i = 1, n
  z(i) = x(i) + y(i)
end do
timing_details(2, j) = time_difference()
z = 0.0_dp
reset = time_difference()

!
! do concurrent loop
!

do concurrent (i=1:n)
  z(i) = x(i) + y(i)
end do
timing_details(3, j) = time_difference()
write (20, 120) x(1), y(1), z(1)
z = 0.0_dp
reset = time_difference()

!
! OpenMP parallel loop
!

!$omp parallel do
do i = 1, n
z(i) = x(i) + y(i)
end do
!$omp end parallel do
timing_details(4, j) = time_difference()
write (20, 120) x(1), y(1), z(1)
z = 0.0_dp
reset = time_difference()

end do

close (20)
print 140
140 format (15x, ’ Sum Average’)

do i = 1, n_types
t_sum(i) = sum(timing_details(i,1:loop_count))
t_average(i) = t_sum(i)/loop_count
print 150, heading_1(i), t_sum(i), t_average(i)
150 format (a, 2(3x,f9.6))
end do

print *, ’ ’
call end_timing()

end program

The program compares the performance of whole array syntax, traditional
do loops, openmp and do concurrent.

Here are results from a Dell 5820 workstation
for Intel, Nag and gfortran.

C:\document\fortran\4th_edition_update\examples>ch3305_gfortran.exe

GCC version 13.2.0
-mtune=generic -march=x86-64 -mthreads -O2 -fopenmp

2023/11/12 15:17:40 731

Nthreads = 36
Initialise time = 0.998391

Iteration = 1
Iteration = 2
Iteration = 3
Iteration = 4
Iteration = 5
Iteration = 6
Iteration = 7
Iteration = 8
Iteration = 9
Iteration = 10
Sum Average
Whole array 1.871372 0.187137
Do loop 1.852208 0.185221
Do concurrent 1.745150 0.174515
openmp 0.468791 0.046879

2023/11/12 15:17:49 836
Total time = 9.104781

NAG Fortran Compiler Release 7.1(Hanzomon) Build 7110
-O4 -openmp

2023/11/12 15:25:44 840

Nthreads = 36
Initialise time = 0.671898

Iteration = 1
Iteration = 2
Iteration = 3
Iteration = 4
Iteration = 5
Iteration = 6
Iteration = 7
Iteration = 8
Iteration = 9
Iteration = 10
Sum Average
Whole array 3.873122 0.387312
Do loop 1.951388 0.195139
Do concurrent 1.794080 0.179408
openmp 0.477791 0.047779

2023/11/12 15:25:55 808
Total time = 10.966643

Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel
(R) 64, Version 2021.10.0 Build 20230609_000000
/O2 /openmp /o:ch3305_ifort.exe

2023/11/12 15:26:38 621

Nthreads = 36
Initialise time = 14.672000

Iteration = 1
Iteration = 2
Iteration = 3
Iteration = 4
Iteration = 5
Iteration = 6
Iteration = 7
Iteration = 8
Iteration = 9
Iteration = 10
Sum Average
Whole array 1.889000 0.188900
Do loop 1.718000 0.171800
Do concurrent 0.386000 0.038600
openmp 0.389000 0.038900

2023/11/12 15:26:59 402
Total time = 20.781000

Ian

3 Likes

Very interesting thread!
I have found Chapman book on Fortran for engeneers (already mentioned above) to be the best reference as a manual covering modern Fortran, from the very beginning up to Coarrays.

I have learnt quite a lot from “Modern Fortran in practice” by Arjen Markus, link: Modern Fortran in Practice
Compared to Chapman, it is much shorter and tailored for programmers who know Fortran already but want to improve and learn new features of the modern versions. In my opinion, a little gem.

Recently I came across “CUDA Fortran for Scientists and Engineers: Best Practices for Efficient CUDA Fortran Programming 1st Edition”, available on amazon. I would like to learn how to use gpu parallelization with Fortran, is this a good source? Does anyone recommend it?
Thanks!!

1 Like