The Computer Language
24.11 Benchmarks Game

spectral-norm Classic Fortran #2 program

source code

! The Computer Language Benchmarks Game
! https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
!
! contributed by Steve Decker based on the version by Simon Geard
! compilation:
!   g95 -O1 -fomit-frame-pointer -funroll-loops spectral_norm.f90
!   ifort -ipo -O3 -static spectral_norm.f90
module norm_subs
  implicit none

  integer, parameter :: dp = selected_real_kind(12)

contains

  ! Return element i,j of infinite matrix A
  pure real(dp) function A(i, j)
    integer, intent(in) :: i, j

    a = 1.d0 / ((i+j-2) * (i+j-1)/2 + i)
  end function A

  ! Multiply v by A
  pure function MultiplyAv(v) result (Av)
    real(dp), dimension(:), intent(in) :: v
    real(dp), dimension(size(v))       :: Av
    
    integer :: n, i, j

    n = size(v)
    Av = 0.d0
    do i = 1, n
       do j = 1, n
          Av(i) = Av(i) + A(i,j) * v(j)
       end do
    end do
  end function MultiplyAv
       
  ! Multiply v by A transpose
  pure function MultiplyAtv(v) result (Atv)
    real(dp), dimension(:), intent(in) :: v
    real(dp), dimension(size(v))       :: Atv

    integer :: n, i, j

    n = size(v)
    Atv = 0.d0
    do i = 1, n
       do j = 1, n
          Atv(i) = Atv(i) + A(j,i) * v(j)
       end do
    end do
  end function MultiplyAtv

  ! Multiply v by A and then by A transpose
  pure function MultiplyAtAv(v) result (AtAv)
    real(dp), dimension(:), intent(in) :: v
    real(dp), dimension(size(v))       :: AtAv
    
    AtAv = MultiplyAtv(MultiplyAv(v))
  end function MultiplyAtAv
end module norm_subs

program spectral_norm
  use norm_subs
  implicit none

  real(dp), dimension(:), allocatable :: u, v
  integer          :: i, n
  character(len=6) :: argv

  call get_command_argument(1, argv)
  read(argv, *) n

  allocate(u(n), v(n))
  u = 1.d0
  do i = 1, 10
     v = MultiplyAtAv(u)
     u = MultiplyAtAv(v)
  end do

  write(*, "(f0.9)") sqrt(dot_product(u,v) / dot_product(v,v))
  deallocate(u, v)
end program spectral_norm
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Fortran Compiler Classic
2021.12.0 20240222


 Mon, 03 Jun 2024 19:59:24 GMT

MAKE:
mv spectralnorm.ifc-2.ifc spectralnorm.ifc-2.f90
~/intel/oneapi/compiler/latest/bin/ifort -O3 -march=ivybridge -ipo -static -qopenmp spectralnorm.ifc-2.f90 -o spectralnorm.ifc-2.ifc_run
ifort: remark #10448: Intel(R) Fortran Compiler Classic (ifort) is now deprecated and will be discontinued late 2024. Intel recommends that customers transition now to using the LLVM-based Intel(R) Fortran Compiler (ifx) for continued Windows* and Linux* support, new language support, new language features, and optimizations. Use '-diag-disable=10448' to disable this message.
ipo: warning #11021: unresolved __ehdr_start
        Referenced in libc.a(dl-support.o)
ld: /home/dunham/intel/oneapi/compiler/2024.1/lib/libifcoremt.a(for_close_proc.o): in function `for__close_proc':
for_close_proc.c:(.text+0x1df): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
rm spectralnorm.ifc-2.f90

3.26s to complete and log all make actions

COMMAND LINE:
 ./spectralnorm.ifc-2.ifc_run 5500

PROGRAM OUTPUT:
1.274224153