10
0
mirror of https://gitlab.com/scemama/irpf90.git synced 2024-11-19 04:22:23 +01:00
irpf90/src/profile.py

161 lines
3.8 KiB
Python
Raw Normal View History

2011-09-30 19:10:18 +02:00
#!/usr/bin/python
rdtsc = """
2011-09-30 21:46:08 +02:00
#ifdef __i386
2011-09-30 23:18:17 +02:00
double irp_rdtsc_(void) {
unsigned long long x;
2011-09-30 19:10:18 +02:00
__asm__ volatile ("rdtsc" : "=A" (x));
2011-09-30 23:18:17 +02:00
return (double) x;
2011-09-30 19:10:18 +02:00
}
2011-09-30 21:46:08 +02:00
#elif __amd64
2011-09-30 23:18:17 +02:00
double irp_rdtsc_(void) {
unsigned long long a, d;
2011-09-30 19:10:18 +02:00
__asm__ volatile ("rdtsc" : "=a" (a), "=d" (d));
2011-09-30 23:18:17 +02:00
return (double)((d<<32) | a);
2011-09-30 19:10:18 +02:00
}
#endif
"""
import subprocess
import tempfile
import os
2011-09-30 23:18:17 +02:00
import threading
2011-09-30 19:10:18 +02:00
from variables import variables
def build_rdtsc():
file,filename = tempfile.mkstemp()
filename += ".c"
file = open(filename,'w')
file.write(rdtsc)
file.close()
2011-09-30 23:18:17 +02:00
def t():
2011-10-21 09:10:40 +02:00
p = subprocess.Popen(["gcc","-O2",filename,"-c","-o","irp_rdtsc.o"])
2011-09-30 23:18:17 +02:00
p.communicate()
os.remove(filename)
threading.Thread(target=t).start()
2011-09-30 19:10:18 +02:00
def build_module():
data = """
module irp_timer
2011-10-21 09:10:40 +02:00
double precision :: irp_profile(3,%(n)d)
integer :: irp_order(%(n)d)
2011-09-30 19:10:18 +02:00
character*(64) :: irp_profile_label(%(n)d)
2011-10-21 09:10:40 +02:00
double precision :: irp_rdtsc_shift
contains
subroutine profile_sort ()
implicit none
character*(64) :: xtmp
integer :: i, i0, j, jmax
do i=1,size(irp_profile_label)
irp_order(i)=i
enddo
do i=1,size(irp_profile_label)
xtmp = irp_profile_label(i)
i0 = irp_order(i)
j = i-1
do j=i-1,1,-1
if ( irp_profile_label(j) > xtmp ) then
irp_profile_label(j+1) = irp_profile_label(j)
irp_order(j+1) = irp_order(j)
else
exit
endif
enddo
irp_profile_label(j+1) = xtmp
irp_order(j+1) = i0
enddo
end subroutine profile_sort
2011-09-30 19:10:18 +02:00
end module
subroutine irp_init_timer
use irp_timer
implicit none
2011-10-21 09:10:40 +02:00
integer :: i
double precision :: irp_rdtsc, t0
irp_profile = 0.d0
irp_rdtsc_shift = 0.d0
do i=1,1000
t0 = irp_rdtsc()
irp_rdtsc_shift = irp_rdtsc_shift + (irp_rdtsc()-t0)
enddo
irp_rdtsc_shift = 1.d-3*irp_rdtsc_shift
2011-09-30 19:10:18 +02:00
%(text)s
end
subroutine irp_set_timer(i,value)
use irp_timer
implicit none
integer, intent(in) :: i
2011-10-21 09:10:40 +02:00
double precision, intent(inout) :: value
value = value - irp_rdtsc_shift
2011-09-30 23:18:17 +02:00
irp_profile(1,i) = irp_profile(1,i) + value
2011-10-21 09:10:40 +02:00
irp_profile(2,i) = irp_profile(2,i) + value*value
irp_profile(3,i) = irp_profile(3,i) + 1.d0
2011-09-30 19:10:18 +02:00
end
subroutine irp_print_timer()
use irp_timer
implicit none
2011-10-21 09:10:40 +02:00
integer :: i, ii
double precision :: error, sigma2, average, average2, frequency, t0
double precision :: irp_rdtsc
t0 = irp_rdtsc()
call sleep(1)
frequency = (irp_rdtsc()-t0-irp_rdtsc_shift)
call profile_sort()
print '(A24,A8,A17,A20,A13,A20)', '', 'N.Calls', 'Tot Cycles', 'Avg Cycles', &
'Tot Secs', 'Avg Secs'
2011-09-30 23:18:17 +02:00
print '(A)', '----------------------------------------------'// &
'----------------------------------------------'
2011-10-21 09:10:40 +02:00
do ii=1,%(n)d
i = irp_order(ii)
if (irp_profile(3,i) > 0.) then
error = 0.d0
average = irp_profile(1,i)/irp_profile(3,i)
if (irp_profile(3,i) > 1.d0) then
average2 = irp_profile(2,i)/irp_profile(3,i)
sigma2 = (average2 - average*average)
error = sqrt(sigma2/(irp_profile(3,i)+1.d0))
endif
print '(A24 , F8.0 , X,F12.0 , X,F12.0,A3,F8.0, X,F12.8, X,F8.5,A3,F8.5 )', &
irp_profile_label(ii), &
irp_profile(3,i), &
irp_profile(1,i), &
average, '+/-', error, &
irp_profile(1,i)/frequency, &
average/frequency, '+/-', error/frequency
2011-09-30 19:10:18 +02:00
endif
enddo
2011-10-21 09:10:40 +02:00
print *, 'Frequency :', frequency*1.d-9, ' GHz'
print *, 'rdtsc latency :', irp_rdtsc_shift, ' cycles'
2011-09-30 19:10:18 +02:00
end
"""
label = {}
for i in variables:
vi = variables[i]
2011-10-21 09:10:40 +02:00
label[vi.label] = vi.same_as
2011-09-30 19:10:18 +02:00
text = []
2011-09-30 23:18:17 +02:00
lmax = 0
2011-09-30 19:10:18 +02:00
for l in label:
text.append(" irp_profile_label(%d) = '%s'"%(l,label[l]))
2011-09-30 23:18:17 +02:00
lmax = max(lmax,l)
2011-09-30 19:10:18 +02:00
text.sort()
text = '\n'.join(text)
2011-09-30 23:18:17 +02:00
data = data%{'text': text, 'n':lmax}
2011-09-30 19:10:18 +02:00
file = open("IRPF90_temp/irp_profile.irp.F90",'w')
file.write(data)
file.close()
def run():
build_module()
build_rdtsc()
if __name__ == "__main__":
build_rdtsc()