Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

optimization and parameters

tracyx
New Contributor I
553 Views
I often use parameters to enable or disable parts of my code. I want to make sure that I am doing it the most efficient way. e.g. if I have.

integer, parameter :: dosomething = 0

print *, y+dosomething*somefunction(x)

then would the compiler internally completely remove dosomething*somefunction(x) so treating it just the same as if I had just written print *, y? How, if at all, does this behaviour depend on the optimization settings? Would it be better to use conditional statements?

thank you for your time
0 Kudos
3 Replies
TimP
Honored Contributor III
553 Views
If somefunction were declared PURE, in principle, the compiler could do this. I doubt that it is sufficiently common practice for a compiler to be designed with this in mind.

logical, parameter :: dosomething = .false.

print *, merge(y+somefunction(x),y,dosomething)

for example, looks more direct to me, but would not solve the problem.
0 Kudos
tracyx
New Contributor I
553 Views
Thanks. I tried a simple piece of code:

program test

implicit none

integer, parameter :: dosomething = 0
real(8), external :: somefunction

integer :: i

do i = 0, 5
print *, 5.0_8 + dosomething*somefunction(i)
end do

end program

function somefunction(x)

implicit none

integer, intent(in) :: x
real(8) :: somefunction

somefunction = 1.0_8/dble(x)

end function

Now, if I compile this without any optimizations (O0), then the first thing that is printed is NAN since we have zero times infinity so the compiler is clearly not removing it. If I compile it with higher level optimizations, then it just prints 5 so it looks like the compiler is completely removing the code in that case. Moreover, if I compare the binary files with optimizations, then they are identical regardless of whether I have dosomething=0 or whether dosomething*somefunction(i) is commented out. Does anyone know if the compiler will always do this?
0 Kudos
Steven_L_Intel1
Employee
553 Views
"always" is perhaps a bit too strong. The compiler does some interprocedural optimization by default at level O2 or higher and in cases where it can see that a computation is not required, it may remove it or evaluate it at compile time.
0 Kudos
Reply