Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Beliavsky/491b626b23687ebae4eb2df25436c663 to your computer and use it in GitHub Desktop.
Save Beliavsky/491b626b23687ebae4eb2df25436c663 to your computer and use it in GitHub Desktop.
xblock_sub.f90
program test
! 05/23/2018 11:49 AM times varies loop methods with global or block variables -- motivated by comp.lang.fortran thread "ASSOCIATE inside loops" https://groups.google.com/forum/#!topic/comp.lang.fortran/TBYnHxFIrS0
use, intrinsic :: iso_fortran_env
integer(int64), parameter :: n = 2**25
integer(int64) :: i, t0, t1, count_rate
integer, parameter :: nways = 6
real, dimension(n) :: x, y, ysum(nways), dt(nways)
character(*), parameter :: fmt1 = "(a14,1x,f8.4)"
logical, parameter :: print_sums = .false., print_each = .false.
character (len=10) :: methods(nways) = ["global ", &
"associate ", &
"block ", &
"no_temp ", &
"forall ", &
"do_concurrent"]
call system_clock(count_rate=count_rate)
call random_seed()
call random_number(x)
iway = 0
test_global: block
real :: xplus, xminus
call system_clock(t0)
do i = 1, n - 1
xplus = (x(i+1) + x(i)) / 2
xminus = x(i+1) - x(i)
y(i) = xplus**2 - 3 * xminus**2
end do
call compute_elapsed_time()
end block test_global
test_assoc: block
call system_clock(t0)
do i = 1, n - 1
associate (xplus => (x(i+1) + x(i)) / 2, xminus => x(i+1) - x(i))
y(i) = xplus**2 - 3 * xminus**2
end associate
end do
call compute_elapsed_time()
end block test_assoc
test_block: block
call system_clock(t0)
do i = 1, n - 1
block
real :: xplus, xminus
xplus = (x(i+1) + x(i)) / 2
xminus = x(i+1) - x(i)
y(i) = xplus**2 - 3 * xminus**2
end block
end do
call compute_elapsed_time()
end block test_block
no_temp: block
call system_clock(t0)
do i = 1, n - 1
y(i) = ((x(i+1) + x(i)) / 2)**2 - 3*(x(i+1) - x(i))**2
end do
call compute_elapsed_time()
end block no_temp
try_forall: block
call system_clock(t0)
forall (i=1:n-1) y(i) = ((x(i+1) + x(i)) / 2)**2 - 3*(x(i+1) - x(i))**2
call compute_elapsed_time()
end block try_forall
do_concurrent: block
call system_clock(t0)
do concurrent (i=1:n-1)
y(i) = ((x(i+1) + x(i)) / 2)**2 - 3*(x(i+1) - x(i))**2
end do
call compute_elapsed_time()
end block do_concurrent
write (*,"('times:',100(1x,f10.6))") dt
if (print_sums) write (*,"(/,'ysum = ',100(f0.4,1x))") ysum ! check that calcs are the same
! write (*,*)
contains
subroutine compute_elapsed_time()
call system_clock(t1)
iway = iway + 1
ysum(iway) = sum(y)
dt(iway) = (t1-t0)/dble(count_rate)
if (print_each) print fmt1, trim(methods(iway)),dt(iway)
end subroutine compute_elapsed_time
end program test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment