Skip to content

Commit

Permalink
autolabel versions
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Sep 26, 2018
1 parent 8e532bb commit 7e14f1a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 47 deletions.
46 changes: 34 additions & 12 deletions Pisum.py
Expand Up @@ -23,9 +23,9 @@ def main():
times = {}
for N in p.N:
print('\nN=', N)
print('-------------------')
print('----------------')
t = benchmark_pisum(N, p.Nrun)
t = {k:v for k,v in t.items() if math.isfinite(v)}
t = {k: v for k, v in t.items() if math.isfinite(v)}
times[N] = dict(sorted(t.items(), key=lambda x: x[1])) # Python >= 3.5

print(t)
Expand All @@ -51,27 +51,49 @@ def benchmark_pisum(N, Nrun) -> Dict[str, float]:

times = {}

times['c'] = pb.c(N, Nrun, bdir, './pisumc')
compinf = pb.compiler_info()

times['fortran'] = pb.fortran(N, Nrun, bdir, './pisumfort')
tv = pb.c(N, Nrun, bdir, './pisumc')
if tv is not None:
times['C\n'+compinf['cc']+'\n'+compinf['ccvers']] = tv

times['julia'] = pb.julia(N, Nrun, bdir)
tv = pb.fortran(N, Nrun, bdir, './pisumfort')
if tv is not None:
times['Fortran\n'+compinf['fc']+'\n'+compinf['fcvers']] = tv

times['gdl'] = pb.gdl(N, Nrun, bdir)
t = pb.julia(N, Nrun, bdir)
if t is not None:
times['julia \n'+t[1]] = t[0]

t = pb.gdl(N, Nrun, bdir)
if t is not None:
times['gdl \n'+t[1]] = t[0]

times['idl'] = pb.idl(N, Nrun, bdir)

times['octave'] = pb.octave(N, Nrun, bdir)
t = pb.octave(N, Nrun, bdir)
if t is not None:
times['octave \n'+t[1]] = t[0]

times['matlab'] = pb.matlab(N, Nrun, bdir)
t = pb.matlab(N, Nrun, bdir)
if t is not None:
times['matlab \n'+t[1]] = t[0]

times['python'] = pb.python(N, Nrun, bdir)
t = pb.python(N, Nrun, bdir)
if t is not None:
times['python \n'+t[1]] = t[0]

times['pypy'] = pb.pypy(N, Nrun, bdir)
t = pb.pypy(N, Nrun, bdir)
if t is not None:
times['pypy \n'+t[1]] = t[0]

times['cython'] = pb.cython(N, Nrun, bdir)
t = pb.cython(N, Nrun, bdir)
if t is not None:
times['cython \n'+t[1]] = t[0]

times['numba'] = pb.numba(N, Nrun, bdir)
t = pb.numba(N, Nrun, bdir)
if t is not None:
times['numba \n'+t[1]] = t[0]

return times

Expand Down
2 changes: 1 addition & 1 deletion pisum/pisum.f90
Expand Up @@ -17,7 +17,7 @@ Real(dp) function pisum(N,Nrun) result(t)
! real(wp) :: psum[*] = 0._wp
integer :: k,j, im, Nimg
real(wp), parameter :: pi = 4.0_wp*atan(1.0_wp)
real(wp) :: psum
real(wp), volatile :: psum

tmin = huge(0_i64)

Expand Down
2 changes: 1 addition & 1 deletion pisum/pisum.py
Expand Up @@ -23,7 +23,7 @@ def main():

assert math.isclose(math.pi, pisum_c(p.N), rel_tol=1e-4), 'CPython convergence error'

print('--> Python', platform.python_version, 'N=', p.N)
print('--> Python', platform.python_version(), 'N=', p.N)
t = timeit.repeat('pisum_c({})'.format(p.N),
'import gc; gc.enable(); from __main__ import pisum_c',
repeat=p.Nrun, number=1)
Expand Down
124 changes: 91 additions & 33 deletions pythonperformance/__init__.py
Expand Up @@ -3,15 +3,66 @@
import os
from pathlib import Path
import math


def c(N: int, Nrun: int, bdir: Path, exe: str) -> float:
from typing import Tuple, Optional, Dict


def compiler_info() -> Dict[str, str]:
"""
assumes CMake project has been generated
"""
fn = Path('bin') / 'CMakeCache.txt'
for ln in fn.open('r'):
if ln.startswith('CMAKE_C_COMPILER:'):
cc = ln.split('/')[-1].rstrip()
elif ln.startswith('CMAKE_Fortran_COMPILER:'):
fc = ln.split('/')[-1].rstrip()

if cc == 'cc':
cc = 'gcc'

if fc == 'f95':
fc = 'gfortran'
# %% versions
try:
if cc == 'clang':
cvers = S.check_output([cc, '-dumpversion'], universal_newlines=True).rstrip()
elif cc == 'gcc':
ret = S.check_output([cc, '--version'], universal_newlines=True).split('\n')
cvers = ret[0].split()[-1]
elif cc == 'icc':
ret = S.check_output([cc, '--version'], universal_newlines=True).split('\n')
cvers = ret[0].split()[-2][:4]
elif cc == 'pgcc':
ret = S.check_output([cc, '--version'], universal_newlines=True).split('\n')
cvers = ret[1].split()[1][:5]

if fc == 'flang':
fvers = S.check_output([fc, '-dumpversion'], universal_newlines=True).rstrip()
elif fc == 'gfortran':
ret = S.check_output([cc, '--version'], universal_newlines=True).split('\n')
fvers = ret[0].split()[-1]
elif fc == 'ifort':
ret = S.check_output([cc, '--version'], universal_newlines=True).split('\n')
fvers = ret[0].split()[-2][:4]
elif fc == 'pgf90':
ret = S.check_output([cc, '--version'], universal_newlines=True).split('\n')
fvers = ret[1].split()[1][:5]
except (FileNotFoundError, S.CalledProcessError):
cvers = fvers = ''

cinf = {'cc': cc, 'ccvers': cvers,
'fc': fc, 'fcvers': fvers}

return cinf


def c(N: int, Nrun: int, bdir: Path, exe: str) -> Optional[float]:
if os.name == 'nt':
exe = exe[2:]

try:
print('--> C')
ret = S.check_output([exe, str(N)], cwd='bin' / bdir, universal_newlines=True)
ret = S.check_output([exe, str(N)], cwd=Path('bin') / bdir, universal_newlines=True)
return float(ret.split('\n')[-2].split()[0])
except FileNotFoundError:
msg = 'please compile as per README'
Expand All @@ -20,16 +71,16 @@ def c(N: int, Nrun: int, bdir: Path, exe: str) -> float:

warnings.warn(msg)

return math.nan
return None


def fortran(N: int, Nrun: int, bdir: Path, exe: str) -> float:
def fortran(N: int, Nrun: int, bdir: Path, exe: str) -> Optional[float]:
if os.name == 'nt':
exe = exe[2:]

try:
print('--> Fortran')
ret = S.check_output([exe, str(N), str(Nrun)], cwd='bin' / bdir, universal_newlines=True)
ret = S.check_output([exe, str(N), str(Nrun)], cwd=Path('bin') / bdir, universal_newlines=True)
return float(ret.split('\n')[-2].split()[0])
except FileNotFoundError:
msg = 'please compile as per README'
Expand All @@ -38,39 +89,40 @@ def fortran(N: int, Nrun: int, bdir: Path, exe: str) -> float:

warnings.warn(msg)

return math.nan
return None


def julia(N: int, Nrun: int, bdir: Path) -> float:
def julia(N: int, Nrun: int, bdir: Path) -> Optional[Tuple[float, str]]:
try:
print('--> Julia')
ret = S.check_output(['julia', 'pisum.jl', str(N)], cwd=bdir, universal_newlines=True)
return float(ret.split('\n')[-2].split()[0])
ret = ret.split('\n')
return float(ret[-2].split()[0]), ret[0].split()[2]
except FileNotFoundError:
msg = 'executable not found'
except S.CalledProcessError:
msg = 'failed to converge'

warnings.warn(msg)

return math.nan
return None


def gdl(N: int, Nrun: int, bdir: Path) -> float:
def gdl(N: int, Nrun: int, bdir: Path) -> Optional[Tuple[float, str]]:
try:
print('--> GDL')
S.check_call(['gdl', '--version'])
vers = S.check_output(['gdl', '--version'], universal_newlines=True).split()[-2]

ret = S.check_output(['gdl', '-q', '-e', 'pisum', '-arg', str(N)], cwd=bdir, universal_newlines=True)
return float(ret.split('\n')[-2].split()[0])
return float(ret.split('\n')[-2].split()[0]), vers
except FileNotFoundError:
msg = 'executable not found'
except S.CalledProcessError:
msg = 'failed to converge'

warnings.warn(msg)

return math.nan
return None


def idl(N: int, Nrun: int, bdir: Path) -> float:
Expand All @@ -89,92 +141,98 @@ def idl(N: int, Nrun: int, bdir: Path) -> float:
return math.nan


def octave(N: int, Nrun: int, bdir: Path) -> float:
def octave(N: int, Nrun: int, bdir: Path) -> Optional[Tuple[float, str]]:
try:
print('--> Octave')
ret = S.check_output(['octave-cli', '-q', '--eval', 'pisum({})'.format(N)], cwd=bdir, universal_newlines=True)
return float(ret.split('\n')[-2].split()[0])
ret = ret.split('\n')
return float(ret[-2].split()[0]), ret[0].split()[2]
except FileNotFoundError:
msg = 'executable not found'
except S.CalledProcessError:
msg = 'failed to converge'

warnings.warn(msg)

return math.nan
return None


def matlab(N: int, Nrun: int, bdir: Path) -> float:
def matlab(N: int, Nrun: int, bdir: Path) -> Optional[Tuple[float, str]]:
try:
print('--> Matlab')
ret = S.check_output(['matlab', '-nodesktop', '-nojvm', '-nosplash', '-r',
'pisum({}); exit'.format(N)], cwd=bdir, universal_newlines=True)
return float(ret.split('\n')[-2].split()[0])
ret = ret.split('\n')
return float(ret[-2].split()[0]), ret[3].split()[0]
except FileNotFoundError:
msg = 'executable not found'
except S.CalledProcessError:
msg = 'failed to converge'

warnings.warn(msg)

return math.nan
return None


def python(N: int, Nrun: int, bdir: Path) -> float:
def python(N: int, Nrun: int, bdir: Path) -> Optional[Tuple[float, str]]:
try:
print('--> Python')
ret = S.check_output(['python', 'pisum.py', str(N)], cwd=bdir, universal_newlines=True)
return float(ret.split('\n')[1].split()[0])
ret = ret.split('\n')
return float(ret[-2].split()[0]), ret[0].split()[2]
except FileNotFoundError:
msg = 'executable not found'
except S.CalledProcessError:
msg = 'failed to converge'

warnings.warn(msg)

return math.nan
return None


def pypy(N: int, Nrun: int, bdir: Path) -> float:
def pypy(N: int, Nrun: int, bdir: Path) -> Optional[Tuple[float, str]]:
try:
print('--> PyPy')
ret = S.check_output(['pypy3', 'pisum.py', str(N)], cwd=bdir, universal_newlines=True)
return float(ret.split('\n')[1].split()[0])
ret = ret.split('\n')
return float(ret[-2].split()[0]), ret[0].split()[2]
except FileNotFoundError:
msg = 'executable not found'
except S.CalledProcessError:
msg = 'failed to converge'

warnings.warn(msg)

return math.nan
return None


def cython(N: int, Nrun: int, bdir: Path) -> float:
def cython(N: int, Nrun: int, bdir: Path) -> Optional[Tuple[float, str]]:
try:
print('--> Cython')
ret = S.check_output(['python', 'pisum_cython.py', str(N)], cwd=bdir, universal_newlines=True)
return float(ret.split('\n')[1].split()[0])
ret = ret.split('\n')
return float(ret[-2].split()[0]), ret[0].split()[2]
except FileNotFoundError:
msg = 'executable not found'
except S.CalledProcessError:
msg = 'failed to converge'

warnings.warn(msg)

return math.nan
return None


def numba(N: int, Nrun: int, bdir: Path) -> float:
def numba(N: int, Nrun: int, bdir: Path) -> Optional[Tuple[float, str]]:
try:
print('--> Numba')
ret = S.check_output(['python', 'pisum_numba.py', str(N)], cwd=bdir, universal_newlines=True)
return float(ret.split('\n')[1].split()[0])
ret = ret.split('\n')
return float(ret[-2].split()[0]), ret[0].split()[2]
except FileNotFoundError:
msg = 'executable not found'
except S.CalledProcessError:
msg = 'failed to converge'

warnings.warn(msg)

return math.nan
return None

0 comments on commit 7e14f1a

Please sign in to comment.