Skip to content

Commit

Permalink
Add a subroutine to prevent compiler optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
gha3mi committed Jan 24, 2024
1 parent a4c867d commit f5a9506
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions benchmarks/dot/dot.f90
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ program benchmark_dot

type(benchmark) :: bench
real(rk), allocatable :: u(:), v(:)
real(rk), volatile :: a
real(rk) :: a
integer(ik) :: p
integer :: nl, seed_size, i
integer, allocatable :: seed_array(:)
Expand All @@ -19,9 +19,9 @@ program benchmark_dot
allocate(seed_array(seed_size))
seed_array = 123456789

call bench%init(7,'Benchmark dot_product','benchmarks/dot/results/dot', 5000)
call bench%init(7,'Benchmark dot_product','benchmarks/dot/results/dot', 10000)

num_elements = [500_ik, 1000_ik, 10000_ik, 100000_ik, 1000000_ik]
num_elements = [1000_ik, 10000_ik, 100000_ik, 1000000_ik]

do i = 1, size(num_elements)
p = num_elements(i)
Expand All @@ -41,6 +41,7 @@ program benchmark_dot
call bench%start_benchmark(1,'dot_product','a = dot_product(u,v)',[p])
do nl = 1,bench%nloops
a = dot_product(u,v)
call prevent_optimization(a,nl) ! loop-invariant
end do
call bench%stop_benchmark(cmp_gflops)
!===============================================================================
Expand All @@ -50,6 +51,7 @@ program benchmark_dot
call bench%start_benchmark(2,'m1', "a = dot_product(u,v,'m1')",[p])
do nl = 1,bench%nloops
a = dot_product(u,v,'m1')
call prevent_optimization(a,nl) ! loop-invariant
end do
call bench%stop_benchmark(cmp_gflops)
!===============================================================================
Expand All @@ -59,6 +61,7 @@ program benchmark_dot
call bench%start_benchmark(3,'m2', "a = dot_product(u,v,'m2')",[p])
do nl = 1,bench%nloops
a = dot_product(u,v,'m2')
call prevent_optimization(a,nl) ! loop-invariant
end do
call bench%stop_benchmark(cmp_gflops)
!===============================================================================
Expand All @@ -68,6 +71,7 @@ program benchmark_dot
call bench%start_benchmark(4,'m3', "a = dot_product(u,v,'m3')",[p])
do nl = 1,bench%nloops
a = dot_product(u,v,'m3')
call prevent_optimization(a,nl) ! loop-invariant
end do
call bench%stop_benchmark(cmp_gflops)
!===============================================================================
Expand All @@ -77,6 +81,7 @@ program benchmark_dot
call bench%start_benchmark(5,'m4', "a = dot_product(u,v,'m4')",[p])
do nl = 1,bench%nloops
a = dot_product(u,v,'m4')
call prevent_optimization(a,nl) ! loop-invariant
end do
call bench%stop_benchmark(cmp_gflops)
!===============================================================================
Expand All @@ -86,6 +91,7 @@ program benchmark_dot
call bench%start_benchmark(6,'chunks', "a = fprod(u,v)",[p])
do nl = 1,bench%nloops
a = fprod(u,v)
call prevent_optimization(a,nl) ! loop-invariant
end do
call bench%stop_benchmark(cmp_gflops)
!===============================================================================
Expand All @@ -95,6 +101,7 @@ program benchmark_dot
call bench%start_benchmark(7,'kahan', "a = fprod_kahan(u,v)",[p])
do nl = 1,bench%nloops
a = fprod_kahan(u,v)
call prevent_optimization(a,nl) ! loop-invariant
end do
call bench%stop_benchmark(cmp_gflops)
!===============================================================================
Expand All @@ -115,4 +122,14 @@ function cmp_gflops(argi,argr) result(gflops)
end function cmp_gflops
!===============================================================================


!===============================================================================
! to prevent compiler from optimizing (loop-invariant)
subroutine prevent_optimization(a, nl)
real(rk), intent(in) :: a
integer, intent(in) :: nl
if (a == 0.0_rk) print*, nl, 'a = 0.0'
end subroutine prevent_optimization
!===============================================================================

end program benchmark_dot

0 comments on commit f5a9506

Please sign in to comment.