The following is my example of my Function RDTSC_Rate, which shows how to utilise the tick rate of each routine. Only RDTSC and CPU_CLOCK@ give different values at each call, all others return values faster than their clock rate.
integer*8 rdtsc_rate, rate
external rdtsc_rate
!
rate = rdtsc_rate (10)
rate = rdtsc_rate (100)
rate = rdtsc_rate (1000)
rate = rdtsc_rate (2500)
rate = rdtsc_rate (10000)
rate = rdtsc_rate (25000)
rate = rdtsc_rate (100000)
rate = rdtsc_rate (1000000)
end
integer*8 function rdtsc_rate (num_cycle)
!
! initialises rdtsc pointer and estimates rdtsc tick rate
!
! calibrate using num_cycle of QueryPerform
!
integer*4 num_cycle
! integer*4, parameter :: num_cycle = 1000
integer*8 rd_list(0:2), last_rdtsc, rd_tick, ticks, call_rate
integer*8 qu_list(0:2), last_query, query_tick, query_rate
real*8 secs
integer*4 i, kk, nt, i_list(0:2), calls
!
integer*8 :: known_rate = -1 ! or 2666701126 ticks per second ~ processor clock rate
integer*8 rdtsc_tick, QueryPerformance_tick, QueryPerformance_rate
external rdtsc_tick, QueryPerformance_tick, QueryPerformance_rate
!
! if ( known_rate <= 0) then
write (*,11) 'rdtsc_rate Initialise ', known_rate
write (*,11) 'target number of cycles ', num_cycle
!
Query_rate = QueryPerformance_rate ()
!
! Run both clocks to get time
last_rdtsc = rdtsc_tick ()
last_Query = QueryPerformance_tick ()
kk = -1
nt = 0
do i = 0, huge(i)
rd_tick = rdtsc_tick ()
Query_tick = QueryPerformance_tick ()
if ( Query_tick == last_Query ) cycle
if ( kk < 2 ) then
kk = kk+1
else
nt = nt+1
end if
i_list(kk) = i
rd_list(kk) = rd_tick
last_rdtsc = rd_tick
qu_list(kk) = Query_tick
last_query = Query_tick
if ( nt > num_cycle ) exit
end do
!
! number of ticks of RDTSC
calls = i_list(2) - i_list(1)
ticks = rd_list(2) - rd_list(1)
query_tick = qu_list(2) - qu_list(1)
!
secs = dble (query_tick) / dble(query_rate)
rdtsc_rate = dble (ticks) / secs
call_rate = dble (calls) / secs
!
! rdtsc_rate = known_rate
!
write (*,11) 'rdtsc_tick cycles =', calls, ' calls'
write (*,11) 'Number of cycles =', nt, ' ticks'
write (*,11) 'query perform ticks =', query_tick, ' ticks'
write (*,12) 'initialise duration =', secs, ' seconds'
write (*,11) 'rdtsc_tick duration =', ticks, ' ticks'
write (*,11) 'rdtsc_tick rate =', call_rate, ' calls per second'
!
write (*,11) 'rdtsc rate =', rdtsc_rate, ' ticks per second'
write (*,11) 'change in rate =', rdtsc_rate-known_rate, ' ticks per second'
write (*,10) 'rdtsc_tick initialised'
write (*,10) ' '
known_rate = rdtsc_rate
! else
! rdtsc_rate = known_rate
! end if
10 format (3x,a)
11 format (3x,a,i15,a)
12 format (3x,a,f15.7,a)
!
end function rdtsc_rate