How is Fortran's performance so extreme?

This toy code is compiled with -O3 and -fopenmp on GFortran 12.1.1 on Fedora Linux 36. When executed, it consumes 131.9 GB (97.8%) of the RAM of my computer at peak and takes about 48 seconds to complete. This kind of performance is amazing, especially given the little effort it takes to write the code. More surprisingly, after the execution, my computer does not feel laggy at all, even though Chrome browser, Lyx, and Skype are still running. They tend to consume a lot of CPU power based on my personal experience. To be honest, I didn’t expect that I could push Fortran to such a limit and it still reliably runs without crashing my computer. I tried C++'s vector and valarray, but they both crashed my computer when n reaches 1d10.

program main
  integer(8) :: n, i
  real(8), allocatable :: x(:)
  n = 1.6d10
  allocate(x(n))
  !$omp parallel do
  do i = 1, n
    call random_number(x(i))
  end do
  print "(f10.5)", maxval(x), minval(x)
end program
1 Like

From jblevins: Parallel Computing in Fortran with OpenMP

1 Like

My bad. I thought Fortran’s RNG is also thread-unsafe. I was thinking about the seed entropy issue of C++ RNG I encountered. It seems Fortran’s random_number works fine with OpenMP, according to the recent GFortran manual. The C++ RNG doesn’t mention thread-safety at all:
Pseudo-random number generation - cppreference.com.