performance - Fortran strange behavior when timing subroutines -
the strangest behavior happens when i'm timing code shown below. it's long it's doing matrix multiplication different ways of passing arguments. matmul0
use explicit shape, matmul1
use assumed shape, , matmul2
use assumed shape plus use pointers inside subroutine point matrices (don't ask me why this, doesn't matter). thing is, when time 3 subroutines, like:
time explicit shape: 3.712099 time assumed shape: 12.55620 time assumed shape + pointer: 3.821299
now, if comment third subroutine (the 1 pointers), time like:
time explicit shape: 3.712099 time assumed shape: 3.824401 time assumed shape + pointer: 0.00000
why happening? , why doesn't happen first subroutine too? i'm running on intel core i3, intel compiler, no optimization flags, ifort test.f90 -fpp
(the fpp
preprocessing timer macro). full code below.
#define timer(func, store) call system_clock(start_t, rate); call func; call system_clock(end_t); store = store + real(end_t - start_t)/real(rate); program test interface subroutine matmul1(a, b, c) real :: a(:,:), b(:,:), c(:,:) end subroutine subroutine matmul2(a, b, c) real, target :: a(:,:), b(:,:), c(:,:) end subroutine end interface real, allocatable, dimension(:,:) :: a, b, c integer, parameter :: m = 500, n = 500, o = 500 integer, parameter :: loops = 100 integer :: start_t, end_t, rate real :: time allocate(a(m,n), b(n,o), c(m,o)) a(:,:) = 1; b(:,:) = 2; c(:,:) = 0 time = 0 = 1, loops timer(matmul0(a, b, c, m, n, o), time) end print*, 'time explicit shape:', time time = 0 = 1, loops timer(matmul1(a, b, c), time) end print*, 'time assumed shape:', time time = 0 = 1, loops ! timer(matmul2(a, b, c), time) end print*, 'time assumed shape + pointer:', time end program subroutine matmul0(a, b, c, m, n, o) integer :: m, n, o real :: a(m,n), b(n,o), c(m,o) = 1, m j = 1, o k = 1, n c(i,j) = c(i,j) + a(i,k)*b(k,j) end end end end subroutine subroutine matmul1(a, b, c) real :: a(:,:), b(:,:), c(:,:) = 1, size(c,1) j = 1, size(c,2) k = 1, size(a,2) c(i,j) = c(i,j) + a(i,k)*b(k,j) end end end end subroutine subroutine matmul2(a, b, c) real, target :: a(:,:), b(:,:), c(:,:) real, pointer :: a0(:,:), b0(:,:), c0(:,:) a0 => a; b0 => b; c0 => c = 1, size(c,1) j = 1, size(c,2) k = 1, size(a,2) c0(i,j) = c0(i,j) + a0(i,k)*b0(k,j) end end end end subroutine
this seems issue ifort
, size()
function calls inside loops...
on machine, code yields:
time explicit shape: 3.145800 time assumed shape: 14.98891 time assumed shape + pointer: 14.82460
i rewrote subroutines re-use array shapes:
subroutine matmul1(a, b, c) real,intent(in) :: a(:,:), b(:,:) real,intent(out) :: c(:,:) integer :: m, n, o m = size(c,1) n = size(c,2) o = size(a,2) = 1, m j = 1, n k = 1, o c(i,j) = c(i,j) + a(i,k)*b(k,j) end end end end subroutine
[matmul2
adjusted similarly...]
now, get:
time explicit shape: 3.159100 time assumed shape: 3.009802 time assumed shape + pointer: 3.567401
interestingly, gfortran
seems not have problem.
commenting out third subroutine call has no effect on either compilers on machine, can't there :(
Comments
Post a Comment