Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29487 Discussions

Loop optimization in the presence of a subroutine call

andyb123
Beginner
702 Views
I'm trying some code a bit like this:

module mm
interface
subroutine anything
end subroutine
end interface

contains
subroutine multiplygrid(a, b, c)
real, allocatable, dimension(:,:,:), intent(out) :: a
real, allocatable, dimension(:,:,:), intent(in) :: b, c

integer :: x, y, z

do z = 1, size(a, DIM=3)
do y = 1, size(a, DIM=2)
do x = 1, size(a, DIM=1)
a(x,y,z) = b(x,y,z)*c(x,y,z)
call anything
end do
end do
end do
end subroutine
end module

If I try to optimise this with"ifort -O -S prog.F90" (12.0.5.220) and then look at the generated code, I notice that the address calculations are liftedout of the inner loop by the optimizer for "b" and "c", but the ones for "a" stay in the inner loop suggesting the compiler thinks that the subroutine might reallocate the array. I know this kind of reflects the intents of the arguments, but I was wondering if anyone couldclarify if this is right as my understanding of the standard (I find it a hard to understand document so might be wrong) is that once something is passed to an argument you are not supposed to use the external name for it unless it isexplicitely a"pointer". If the behaviour is right, does anyone know of an option to let the compiler know it is safe to do the optimization as it would if there was no call?

Thanks.
0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
702 Views
If the subroutine is PURE then it may be possible to preserve some optimizations.
If it is host associated CONTAINS (and PURE)subroutine, optimization opportunities may be better.

Jim Dempsey
0 Kudos
mriedman
Novice
702 Views
In a case like this the directive !dec$ ivdep may do the job for you if you put it before the DO X loop. It tells the compiler that it shouldn't bother about data dependencies within the scope of this loop. I also recommend to use -vec-report to get more explanation from the compiler.
0 Kudos
jimdempseyatthecove
Honored Contributor III
702 Views
Also consider:

do z = 1, size(a, DIM=3)
do y = 1, size(a, DIM=2)
do x = 1, size(a, DIM=1)
a(x,y,z) = b(x,y,z)*c(x,y,z)
end do
end do
end do
do z = 1, size(a, DIM=3)
do y = 1, size(a, DIM=2)
do x = 1, size(a, DIM=1)
call anything
end do
end do
end do
.or.
do z = 1, size(a, DIM=3)
do y = 1, size(a, DIM=2)
do x = 1, size(a, DIM=1)
a(x,y,z) = b(x,y,z)*c(x,y,z)
end do
do x = 1, size(a, DIM=1)
call anything
end do end do
end do

Jim Dempsey
0 Kudos
Reply