The Computer Language
24.11 Benchmarks Game

fannkuch-redux Classic Fortran program

source code

! The Computer Language Benchmarks Game
! https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
!
! converted to fortran by Gilbert Brietzke on 31. Januar 2011
! from C-code by Joseph Piché as a conversion
! from Java version by Oleg Mazurov and Isaac Gouy

program fannkuch

  implicit none

  integer :: n,checksum,maxFlipsCount
  character(len=2) :: arg
  character(len=10) :: out
  call get_command_argument(1,arg);read(arg,'(I2)')n
  call fannkuchredux(n,checksum,maxFlipsCount)
  write(out,'(I10)') checksum;
  write(*,'(A)') trim(adjustl(out))
  write(out,'(I10)') maxFlipsCount;
  write(*,'(A)') 'Pfannkuchen('//trim(adjustl(arg))//') = '//trim(adjustl(out))

contains

  subroutine fannkuchredux(n,checksum,maxFlipsCount)
    implicit none
    integer, intent(in) :: n
    integer, intent(out) :: maxFlipsCount,checksum
    integer :: perm(0:n-1),perm1(0:n-1),icount(0:n-1);
    integer :: i,r,temp,k,perm0,permCount = 0,flipsCount = 0;
    checksum = 0;  maxFlipsCount = 0;
    do i = 0,n-1
       perm1(i)= i;
    enddo
    r=n
    do while (.true.)
       do while (r /= 1)
          icount(r-1)=r; r=r-1;
       end do
       perm = perm1; flipsCount = 0;
       do while (perm(0)/=0) 
          k = perm(0)
          do  i=0,ishft(k+1,-1)-1
             temp = perm(i); perm(i) = perm(k-i); perm(k-i) = temp;
          enddo
          flipsCount = flipsCount + 1;
       end do
       maxFlipsCount = max(maxFlipsCount,flipsCount);
       if (modulo(permCount,2) == 0)then
          checksum = checksum + flipsCount
       else
          checksum = checksum - flipsCount
       end if
       do while (.true.)
          if (r==n) return
          perm0 = perm1(0); i = 0;
          do while (i < r) 
             perm1(i) = perm1(i + 1); i = i + 1;
          end do
          perm1(r) = perm0;
          icount(r) = icount(r) - 1;
          if (icount(r) > 0) exit;
          r = r + 1;
       end do
       permCount = permCount + 1;
    end do
  end subroutine fannkuchredux
  
end program fannkuch
    

notes, command-line, and program output

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


 Mon, 03 Jun 2024 19:36:06 GMT

MAKE:
mv fannkuchredux.ifc fannkuchredux.f90
~/intel/oneapi/compiler/latest/bin/ifort -O3 -march=ivybridge -ipo -static -qopenmp fannkuchredux.f90 -o fannkuchredux.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 fannkuchredux.f90

2.98s to complete and log all make actions

COMMAND LINE:
 ./fannkuchredux.ifc_run 12

PROGRAM OUTPUT:
3968050
Pfannkuchen(12) = 65