time functions in pgfortran

I have been playing with pgfortran V10 for about a day now. My first project, naturally was to compile an existing code, previously compiled with g77 and gfortran.

There were a few wrinkles, one of which was a complaint about my use of the dtime() function as a subroutine, which is allowed in g77 but not evidently in pgfortran. I experimented with different ways of getting around this problem, trying to find a solution which would compile successfully with both compilers. Once I had managed that I found that g77 gives correct results, but pgfortran, running with the identical code, produces garbage. Here is a simple program which demonstrates the differences.

c ---------------------------------------------------------------
program test_time
c ---------------------------------------------------------------

real*4 dtime,

   timearray(2),
   telapse

integer*4 i, k

character*20 ctime,

        chartime(2)
  • Time the run
    C call dtime(timearray, telapse) ! This works in g77

telapse = dtime(timearray) ! Not recommended in g77, but OK?

  • telapse should be the total of the two elements of timearray

chartime(1) = ctime(time()) ! This is correct syntax, presumably.

do i = 1, 100000000 ! do some stuff…
k = iiiii
end do

C call dtime(timearray, telapse)
telapse = dtime(timearray) ! Total time elapsed since dtime last call

chartime(2) = ctime(time())

write (,) 'Total elapsed time is: ‘, telapse, ’ seconds’
write (,) 'Run started at: ', chartime(1)
write (,) 'Run finished at: ', chartime(2)

stop
end

*==============================================

This simple program produces results like this when compiled with g77:

verdi://scratch/verdi/pgitests/ibis/source%test_g77
Total elapsed time is: 0.990850031 seconds
Run started at: Thu Dec 31 17:03:17
Run finished at: Thu Dec 31 17:03:18


Compiled with pgfortran, running on the same machine it produces

verdi://scratch/verdi/pgitests/ibis/source%test_pgf
Total elapsed time is: 1.320000 seconds
Run started at: Thu Nov 13 03:54:56
Run finished at: Thu Nov 13 03:54:56
FORTRAN STOP

… which is clearly wrong (given that today’s date is 31 December!)

It is also interesting to note that the g77 executable is quite a bit faster (which is something I have confirmed with other tests).

Does anyone have any thoughts about this? All the best for HPC in 2010!

Hi David,

The complete list of Lib3F functions and how they should be declared (which includes dtime, ctime and time) can be found in Chapter 7 of the PGI Fortran Reference Guide. While not part of the Fortran standard, the 3F versions of these function are fairly common. However, compiler vendors such as GNU are free to change the syntax.

The problem with your code is that you forgot to declare “time” as integer*8. Hence it becomes implicitly typed as a real. With this change, the dates are correct.

% cat dtime.f
c       ---------------------------------------------------------------
        program test_time
c       ---------------------------------------------------------------
        implicit none
        real*4 timearray(2), telapse
        real*4 dtime
        integer*8 time
        character*20 ctime

        integer*4 i, k
        character*20 chartime(2)

*       Time the run
        telapse = dtime(timearray) ! Not recommended in g77, but OK?

        chartime(1) = ctime(time()) ! This is correct syntax, presumably.

        do i = 1, 100000000     ! do some stuff...
           k = i*i*i*i*i
        end do

        telapse = dtime(timearray) ! Total time elapsed since dtime last call

        chartime(2) = ctime(time())

        write (*,*) 'Total elapsed time is: ', telapse, ' seconds'
        write (*,*) 'Run started at: ', chartime(1)
        write (*,*) 'Run finished at: ', chartime(2)

        stop
        end

*==============================================
% gfortran dtime.f -O3 -o dtime_gnu.out
% pgfortran dtime.f -fastsse -o dtime_pgi.out
% dtime_gnu.out
 Total elapsed time is:   9.9900004E-04  seconds
 Run started at: Mon Jan  4 09:57:34
 Run finished at: Mon Jan  4 09:57:34
% dtime_pgi.out
 Total elapsed time is:     0.000000      seconds
 Run started at: Mon Jan  4 09:57:38
 Run finished at: Mon Jan  4 09:57:38
FORTRAN STOP

As for performance, PGI Fortran is generally faster then g77 or gfortran, especially with scientific and engineering applications that can utilize vectorization. Are you using optimization flags such as “-fastsse”?

Hope this helps,
Mat