nadavhalahmi
nadavhalahmi

Reputation: 101

exp function run time comparison ifort vs gfortran

I wrote this code:

program exponent
    implicit none

    real(8) :: sum
    integer(8) :: i
    integer :: limit
    real :: start, end

    sum = 0d0
    limit = 10000000
    call CPU_TIME(start)
    do i=1, limit
        sum = sum + exp(i*1.d0/limit)
    end do
    call CPU_TIME(end)
    print *, sum
    print '("Time = ",f6.3," seconds.")',end-start
end program exponent

And I compiled it with gfortran 10.1.0 and ifort 19.1.3.304 on CentOS Linux 7 using:

ifort *.f90 -O3 -o intel.out

gfortran *.f90 -O3 -o gnu.out

and the outputs are:

gnu:

17182819.143730670
Time = 0.248 seconds.

intel:

17182819.1437313
Time = 0.051 seconds.

When I run a few times, the run time of each is pretty much the same.

Why is ifort faster than gfortran and how can I make gfortran run as fast as ifort?

Upvotes: 3

Views: 561

Answers (1)

Jérôme Richard
Jérôme Richard

Reputation: 50806

ifort is mainly faster because it uses its own optimized math library called SVML (provided with the Intel compiler). This library is often faster since it provides optimized vectorized primitives, even without -ffastmath. Moreover, the Intel compiler tends to better vectorize loops (especially with reduction like this).

You can see the difference on GodBolt: the ifort version vectorizes the loop by working on 2 numbers at a time while the gfortran version uses a slower scalar exponential.

Note that using -mavx2 helps ifort to generate a faster code thanks to the AVX instruction set. Using AVX-512 instructions (if available on the target machine) could be even faster. gfortran can vectorize the loop with -march=native on GodBolt (but strangely not with -march=skylake and -ffast-math).

Upvotes: 6

Related Questions