在Fortran中实现get_walltime

时间:2018-12-29 17:25:09

标签: time fortran

我正在根据this书中第一个练习(第34页)编写一个Fortran程序。本书旨在作为高性能计算的入门。第一个练习要求用户编写代码以估算pi。按照作者提供的内容,我已成功完成了此操作。

program compute_pi
   double precision :: x, delta_x, sum
   !double precision :: S,E,MFLOPS
    integer, parameter :: SLICES=10000000
    sum = 0.d0 ; delta_x = 1.d0/SLICES
    !call get_walltime(S)
    do i=0,SLICES-1
        x = (i+0.5)*delta_x
        sum = sum + 4.d0 / (1.d0 + x*x)
    enddo
    pi = sum*delta_x 
    print *, pi
    !call get_walltime(E)
    !MFLOPS = R*N*2.d0/((E-S)*1.d6)
end program compute_pi

接下来,我正在尝试以MFlops / sec计算性能。为了做到这一点,我遵循了第5页上作者的示例并写道

program compute_pi
   double precision :: x, delta_x, sum
   double precision :: S,E,MFLOPS
    integer, parameter :: SLICES=10000000
    sum = 0.d0 ; delta_x = 1.d0/SLICES
    call get_walltime(S)
    do i=0,SLICES-1
        x = (i+0.5)*delta_x
        sum = sum + 4.d0 / (1.d0 + x*x)
    enddo
    pi = sum*delta_x 
    print *, pi
    call get_walltime(E)
    MFLOPS = R*N*2.d0/((E-S)*1.d6)
end program compute_pi

当我在Netbeans(我用于Fortran的IDE)中运行以上代码时,它会返回

undefined reference to `get_walltime_'

我不确定为什么引用未定义。

2 个答案:

答案 0 :(得分:4)

这是get_walltime的纯Fortran实现(仅经过编译测试):

subroutine get_walltime(wctime)
  use iso_fortran_env, only: int64
  implicit none
  integer, parameter :: dp = kind(1.0d0)
  real(dp) :: wctime
  integer(int64) :: r, c
  call system_clock(c, r)
  wctime = real(c, dp) / r
end subroutine get_walltime

更好的是,将其放入模块中以免费进行接口检查。甚至比这更好,只需直接调用system_clock。

答案 1 :(得分:0)

The get_wallclock() in that exercise is a call to a C-code that calculated wall-clock time using gettimeofday() from <sys/time.h>. Flip to page 6 of the same book for an example on how to implement that function. Afterwards, compile your C-code with the -c flag to get the object file, e.g. get_wallclock.o. Then link your main Fortran program to the object file. This way the linker can resolve get_walltime().