提交 74a105e8 authored 作者: AlOa's avatar AlOa

Adding scripts to test the speedup(slowdonw) using openmp in elemwise ops

上级 32fe7e5c
import os
import subprocess
import sys
def runScript():
script = 'elemwise_time_test.py'
dir = os.path.dirname(os.path.abspath(__file__))
proc = subprocess.Popen(['python', script, '--script'], stdout=subprocess.PIPE, stderr = subprocess.PIPE, cwd = dir)
(out, err) = proc.communicate()
if err:
print err
sys.exit()
return map(float, out.split(" "))
if __name__ == '__main__':
(cheapTime, costlyTime) = runScript()
os.environ['THEANO_FLAGS'] = 'openmp=true'
(cheapTimeOpenmp, costlyTimeOpenmp) = runScript()
if cheapTime > cheapTimeOpenmp:
cheapSpeed = (cheapTime - cheapTimeOpenmp) / cheapTime
cheapSpeedstring = "speedup"
else:
cheapSpeed = (cheapTimeOpenmp - cheapTime) / cheapTimeOpenmp
cheapSpeedstring = "slowdown"
if cheapTime > cheapTimeOpenmp:
costlySpeed = (costlyTime - costlyTimeOpenmp) / costlyTime
costlySpeedstring = "speedup"
else:
costlySpeed = (costlyTimeOpenmp - costlyTime) / costlyTimeOpenmp
costlySpeedstring = "slowdown"
print "Cheap op time without openmp %fs with openmp %fs %s %2.2f%%" % (cheapTime, cheapTimeOpenmp, cheapSpeedstring, cheapSpeed*100)
print "Costly op time without openmp %fs with openmp %fs %s %2.2f%%" % (costlyTime, costlyTimeOpenmp, costlySpeedstring, costlySpeed*100)
import theano
import theano.tensor as T
import numpy as np
import time
from optparse import OptionParser
import sys
parser = OptionParser(usage='%prog <options>\n Compute time for'
'cheap and costly elemwise operations')
parser.add_option('-N', '--N', action='store', dest='N',
default=theano.config.openmp_elemwise_minsize, type="int",
help="Number of vector element")
parser.add_option('--script', action='store_true', dest='script',
default=False,
help="Run program as script and print results on stdoutput")
def evalTime(f, v, script=False, loops=1000):
min = 1e10
for i in xrange(0, loops):
t0 = time.time()
f(v)
dt = time.time() - t0
min = dt if dt < min else min
if not script:
print ' run time in %d loops was %2.9f sec' % (loops, min)
return min
def ElemwiseOpTime(N, script=False, loops=1000):
x = T.vector('x')
np.random.seed(1235)
v = np.random.random(N).astype(theano.config.floatX)
f = theano.function([x], 2*x + x*x)
f1 = theano.function([x], T.tanh(x))
if not script:
if theano.config.openmp:
print "With openmp:"
print "Cheap op ",
ceapTime = evalTime(f, v, script=script, loops=loops)
if not script:
print "Costly op ",
costlyTime = evalTime(f1, v, script=script, loops=loops)
return (ceapTime, costlyTime)
if __name__ == '__main__':
options, arguments = parser.parse_args(sys.argv)
if hasattr(options, "help"):
print options.help
sys.exit(0)
(cheapTime, costlyTime) = ElemwiseOpTime(N=options.N,
script=options.script)
if options.script:
sys.stdout.write("%2.9f %2.9f\n" % (cheapTime, costlyTime))
sys.stdout.flush()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论