def compare_perf(fn, args, numba= True, cpython = True): parakeet_fn = jit(fn) name = fn.__name__ parakeet_result = None numba_result = None cpython_result = None with timer('Parakeet #1 -- %s' % name): parakeet_result = parakeet_fn(*args) with timer('Parakeet #2 -- %s' % name): parakeet_result = parakeet_fn(*args) if numba: numba_fn = autojit(fn) with timer('Numba #1 -- %s' % name): numba_result = numba_fn(*args) with timer('Numba #2 -- %s' % name): numba_result = numba_fn(*args) if parakeet_result is not None and numba_result is not None: assert np.allclose(parakeet_result, numba_result) if cpython: with timer('Python -- %s' % name): python_result = fn(*args) if cpython_result is not None and parakeet_result is not None: assert np.allclose(parakeet_result, python_result)
def run(fn, prop): fn = parakeet.jit(fn) for v in values: expected = getattr(v, prop) result = fn(v) assert np.allclose( expected, result), "Expected %s but got %s" % (expected, result)
def run(fn, method_name): fn = parakeet.jit(fn) for v in values: method = getattr(v, method_name) expected = method() result = fn(v) assert np.allclose(expected, result), "Expected %s but got %s" % (expected, result)
def run(fn, method_name): fn = jit(fn) for v in values: method = getattr(v, method_name) expected = method() result = fn(v) assert np.allclose(expected, result), "For test input %s, expected %s but got %s" % (v, expected, result)
def compare_perf(fn, args, numba= True, cpython = True): parakeet_fn = jit(fn) with timer('Parakeet #1'): parakeet_result = parakeet_fn(*args) with timer('Parakeet #2'): parakeet_result = parakeet_fn(*args) if numba: numba_fn = autojit(fn) with timer('Numba #1'): numba_result = numba_fn(*args) with timer('Numba #2'): numba_result = numba_fn(*args) assert np.allclose(parakeet_result, numba_result) if cpython: with timer('Python'): python_result = fn(*args) assert np.allclose(parakeet_result, python_result)
def test_copy(): def call_copy(x): return x.copy() run(call_copy, 'copy') x = np.array([1,2,3]) y = parakeet.jit(call_copy)(x) x[0] = 10 assert y[0] == 1
def test_copy(): def call_copy(x): return x.copy() run(call_copy, 'copy') x = np.array([1,2,3]) y = jit(call_copy)(x) old_val = x[0] x[0] = 10 assert y[0] == old_val assert y[1] == x[1]
def test_copy(): def call_copy(x): return x.copy() run(call_copy, 'copy') x = np.array([1, 2, 3]) y = jit(call_copy)(x) old_val = x[0] x[0] = 10 assert y[0] == old_val assert y[1] == x[1]
def run(fn, method_name): fn = jit(fn) for v in values: method = getattr(v, method_name) expected = method() result = fn(v) try: if result == expected: return except: pass if hasattr(expected, 'dtype') and hasattr(result, 'dtype'): assert expected.dtype == result.dtype, \ "Mismatching dtype, expected %s but got %s" % (expected.dtype, result.dtype) else: assert type(expected) == type(result), \ "Mismatching type, expected %s but got %s" % (type(expected), type(result)) assert np.allclose(expected, result), \ "For test input %s, expected %s but got %s" % (v, expected, result)
parakeet.config.print_x86 = True parakeet.config.print_untyped_function = True parakeet.config.print_lowered_function = True parakeet.config.print_optimized_llvm = True parakeet.config.print_specialized_function = True parakeet.config.opt_loop_unrolling = False parakeet.config.stride_specialization = True def count_thresh_orig(values, thresh): n = 0 for elt in values: n += elt < thresh return n count_thresh = parakeet.jit(count_thresh_orig) """ def gini_orig(values, thresh): n_left = 0 n_right = 0 for elt in values: n_left = n_left + (elt < thresh) n_right = n_right + (not (elt<thresh)) total = 1.0 * n_left + n_right return 1.0 - (n_left/total) ** 2 - (n_right/total) ** 2 gini = parakeet.jit(gini_orig) """ def test_count_thresh(): import ast
from growcut import growcut_python from parakeet import jit benchmarks = ( ("growcut_parakeet", jit(growcut_python.growcut_python)), )
from julia import julia_python from parakeet import jit benchmarks = ( ("julia_parakeet_for_loops", jit(julia_python.julia_python_for_loops)), ("julia_parakeet_numpy", jit(julia_python.julia_python_numpy)), )
# Authors: Serge Guelton # License: MIT from rosen_der import rosen_der_python from parakeet import jit benchmarks = ( ("rosen_der_loops_parakeet", jit(rosen_der_python.rosen_der_python)), ("rosen_der_numpy_parakeet", jit(rosen_der_python.rosen_der_numpy)) )
def rosen_der_np(x): der = np.empty_like(x) der[1:-1] = (+200 * (x[1:-1] - x[:-2]**2) - 400 * (x[2:] - x[1:-1]**2) * x[1:-1] - 2 * (1 - x[1:-1])) der[0] = -400 * x[0] * (x[1] - x[0]**2) - 2 * (1 - x[0]) der[-1] = 200 * (x[-1] - x[-2]**2) return der def rosen_der_loops(x): n = x.shape[0] der = np.empty_like(x) for i in range(1, n - 1): der[i] = (+200 * (x[i] - x[i - 1]**2) - 400 * (x[i + 1] - x[i]**2) * x[i] - 2 * (1 - x[i])) der[0] = -400 * x[0] * (x[1] - x[0]**2) - 2 * (1 - x[0]) der[-1] = 200 * (x[-1] - x[-2]**2) return der if __name__ == '__main__': N = 10**7 x = np.arange(N) / float(N) jit(rosen_der_np)(x) from compare_perf import compare_perf # numba still crashes on negative indexing compare_perf(rosen_der_np, [x.copy()], numba=False) compare_perf(rosen_der_loops, [x.copy()], numba=False)
from timer import timer print print "Computing distances between %d and %d %s vectors of length %d" % \ (nsamples, nclusters, X.dtype, ndims) # # Parakeet # import parakeet parakeet_dists = parakeet.jit(sqr_dists) parakeet_dists(X,Y) parakeet_dists(X,Y) with timer('Parakeet (comprehensions) #1'): parakeet_dists(X,Y) with timer('Parakeet (comprehensions) #2'): parakeet_dists(X,Y) parakeet_dists_loops = parakeet.jit(sqr_dists_loops) with timer('Parakeet (loops) #1'): parakeet_dists_loops(X,Y) with timer('Parakeet (loops) #2'):
def grotation(alpha,theta,phi): return np.dot(zrotation(phi), np.dot(yrotation(theta), np.dot(zrotation(alpha), np.dot(yrotation(-theta),zrotation(-phi))))) xyzdata = np.zeros((3,len(relativeomega))) phi = pulseShapePhase # Loop through the entire frequency range calculating the rotation matrix (r) at each frequency for ind in range(len(relativeomega)): theta = np.arctan2(pulseShapeInten, relativeomega[ind]/gammaB1max) alpha = nu1maxdt * np.sqrt(pulseShapeInten**2+(relativeomega[ind]/gammaB1max)**2) prop = np.eye(3) # The rotation matrix is a recursive loop through each step of the shaped pulse for pulseindex in range(n_pulse): r = grotation(alpha[pulseindex],theta[pulseindex],phi[pulseindex]) prop = np.dot(r,prop) res = np.dot(prop, inputVector) xyzdata[:,ind] = np.dot(prop,inputVector)[:, 0] return xyzdata from compare_perf import compare_perf from parakeet import jit f = jit(pulseprop) f(relativeomega, pulseShapeInten, pulseShapePhase, gammaB1max, nu1maxdt, inputVector, n_pulse, n_freq)
return y_strip def dilate_decompose_interior(x, k): m,n = x.shape y = np.array([dilate_1d_interior(x[row_idx, :],k) for row_idx in xrange(m)]) return np.array([dilate_1d_interior(y[:, col_idx],k) for col_idx in xrange(n)]).T if __name__ == '__main__': if not running_pypy: scipy_result = None import scipy.ndimage with timer('scipy'): scipy_result = scipy.ndimage.grey_dilation(image, k, mode='nearest') from numba import autojit from parakeet import jit jit(dilate_naive)(image, k) run(dilate_naive, 'naive', imshow=False) run(dilate_naive_inline, 'naive-inline') run(dilate_decompose_loops, 'decompose-loops') run(dilate_decompose_loops_inline, 'decompose-loops-inline') run(dilate_decompose, 'decompose-slices' ) run(dilate_decompose_interior, 'decompose-interior') with timer('cpython-naive'): dilate_naive(image, k,) with timer('cpython-naive-inline'): dilate_naive_inline(image, k) with timer('cpython-decompose-loops'): dilate_decompose_loops(image, k) with timer('cpython-decompose-loops-inline'):
def create_mlp(n_inputs, n_hidden, n_outputs): W1 = init_layer(n_inputs, n_hidden) W2 = init_layer(n_hidden, n_outputs) return (W1, W2) def dot(x,y): return sum(x*y) def fprop_elt(x,w): return np.tanh(dot(x,w)) def fprop_layer(layer, x): return [fprop_elt(x,w) for w in layer] fprop_layer_parakeet = jit(fprop_layer) def fprop(network, x): for layer in network: x = fprop_layer_parakeet(layer, x) return x def fprop_python(network, x): for layer in network: x = np.array(fprop_layer(layer,x)) return x n = 1000 d = 500 Xpos = np.random.randn(n,d)+1.0 Xneg = np.random.randn(n,d)-1.0
def compare_perf(fn, args, numba=True, cpython=True, extra={}, backends=('c', 'openmp', 'cuda'), suppress_output=False, propagate_exceptions=False): parakeet_fn = jit(fn) name = fn.__name__ parakeet_result = None numba_result = None cpython_result = None kwargs = { 'suppress_stdout': suppress_output, 'suppress_stderr': suppress_output, 'propagate_exceptions': propagate_exceptions } backend = None for backend in backends: with timer('Parakeet (backend = %s) #1 -- %s' % (backend, name), **kwargs): parakeet_result = parakeet_fn(*args, _backend=backend) with timer('Parakeet (backend = %s) #2 -- %s' % (backend, name), **kwargs): parakeet_result = parakeet_fn(*args, _backend=backend) if numba: from numba import autojit, config numba_fn = autojit(fn) with timer('Numba #1 -- %s' % name, **kwargs): numba_result = numba_fn(*args) with timer('Numba #2 -- %s' % name, **kwargs): numba_result = numba_fn(*args) if cpython: with timer('Python -- %s' % name, **kwargs): cpython_result = fn(*args) for name, impl in extra.iteritems(): with timer("%s #1" % name, **kwargs): impl(*args) with timer("%s #2" % name, **kwargs): extra_result = impl(*args) if python_result is not None: diffs = np.abs(parakeet_result - extra_result) assert np.allclose(parakeet_result, extra_result, atol = atol, rtol = rtol), \ "Max elt difference between Parakeet and %s = %s (median = %s, min = %s)" % \ (name, np.max(diffs), np.median(diffs), np.min(diffs)) rtol = 0.0001 if backend in ('cuda', 'gpu'): atol = 0.001 else: atol = 0.00001 if parakeet_result is not None and cpython_result is not None: diffs = np.abs(cpython_result - parakeet_result) assert np.allclose(cpython_result, parakeet_result, atol = atol, rtol = rtol), \ "Max elt difference between Parakeet and CPython = %s (median = %s, min = %s)" % \ (np.max(diffs), np.median(diffs), np.min(diffs)) if numba_result is not None and cpython_result is not None: diffs = np.abs(cpython_result - numba_result) assert np.allclose(cpython_result, numba_result, atol = atol, rtol = rtol), \ "Max elt difference between Numba and CPython = %s (median = %s, min = %s)" % \ (np.max(diffs), np.median(diffs), np.min(diffs))
# Authors: Serge Guelton # License: MIT from rosen_der import rosen_der_python from parakeet import jit benchmarks = (("rosen_der_loops_parakeet", jit(rosen_der_python.rosen_der_python)), ("rosen_der_numpy_parakeet", jit(rosen_der_python.rosen_der_numpy)))
[dilate_1d_interior(x[row_idx, :], k) for row_idx in xrange(m)]) return np.array( [dilate_1d_interior(y[:, col_idx], k) for col_idx in xrange(n)]).T if __name__ == '__main__': if not running_pypy: scipy_result = None import scipy.ndimage with timer('scipy'): scipy_result = scipy.ndimage.grey_dilation(image, k, mode='nearest') from numba import autojit from parakeet import jit jit(dilate_naive)(image, k) run(dilate_naive, 'naive', imshow=False) run(dilate_naive_inline, 'naive-inline') run(dilate_decompose_loops, 'decompose-loops') run(dilate_decompose_loops_inline, 'decompose-loops-inline') run(dilate_decompose, 'decompose-slices') run(dilate_decompose_interior, 'decompose-interior') with timer('cpython-naive'): dilate_naive( image, k, ) with timer('cpython-naive-inline'): dilate_naive_inline(image, k)
return (W1, W2) def dot(x, y): return sum(x * y) def fprop_elt(x, w): return np.tanh(dot(x, w)) def fprop_layer(layer, x): return [fprop_elt(x, w) for w in layer] fprop_layer_parakeet = jit(fprop_layer) def fprop(network, x): for layer in network: x = fprop_layer_parakeet(layer, x) return x def fprop_python(network, x): for layer in network: x = np.array(fprop_layer(layer, x)) return x n = 1000
def test_lower_right_corner(): expect_each(jit(lower_right_corner), lower_right_corner, matrices)
X = np.random.randn(nsamples, ndims) Y = np.random.randn(nclusters, ndims) from timer import timer print print "Computing distances between %d and %d %s vectors of length %d" % \ (nsamples, nclusters, X.dtype, ndims) # # Parakeet # import parakeet parakeet_dists = parakeet.jit(sqr_dists) with timer('Parakeet (comprehensions) #1'): parakeet_dists(X, Y) with timer('Parakeet (comprehensions) #2'): parakeet_dists(X, Y) parakeet_dists_loops = parakeet.jit(sqr_dists_loops) with timer('Parakeet (loops) #1'): parakeet_dists_loops(X, Y) with timer('Parakeet (loops) #2'): parakeet_dists_loops(X, Y)
from julia import julia_python from parakeet import jit benchmarks = ( ("julia_parakeet_for_loops", jit(julia_python.julia_python_for_loops)), # Can't run the NumPy version under Parakeet since the following # features are not supported: # - np.seterr # - np.ogrid # - complex numbers #("julia_parakeet_numpy", # jit(julia_python.julia_python_numpy)), )
from parakeet import jit from parakeet.testing_helpers import run_local_tests, expect import time def f(x,y,z=1,q=3): return x + y + z + q jitf = jit(f) def test_call_overhead_identity(): n = 2000 x = 3 start_t = time.time() for i in xrange(n): f(x,x,x,x) python_time = time.time() - start_t print "Python time for %d calls: %f" % (n, python_time) # warm up! jitf(x,x,x,x) start_t = time.time() for i in xrange(n): jitf(x,x,x,x) parakeet_time = time.time() - start_t print "Parakeet time for %d calls: %f" % (n, parakeet_time) print "Slowdown = %f" % (parakeet_time / python_time ) assert parakeet_time < 1000 * python_time, "Excessive call overhead: %f Python vs. %f Parakeet" % (python_time, parakeet_time) if __name__ == "__main__": run_local_tests()
from parakeet import jit import timer def covariance(x,y): return ((x-x.mean()) * (y-y.mean())).mean() def fit_simple_regression(x,y): slope = covariance(x,y) / covariance(x,x) offset = y.mean() - slope * x.mean() return slope, offset import numpy as np N = 10**7 x = np.random.randn(N).astype('float64') slope = 903.29 offset = 102.1 y = slope * x + offset jit(fit_simple_regression)(x,y) timer.compare_perf(fit_simple_regression, (x,y))
import d2q9_nsnxny_vec as d2q9 from parakeet import jit m2f = jit(d2q9.m2f) f2m = jit(d2q9.f2m) transport = jit(d2q9.transport) relaxation = jit(d2q9.relaxation) periodic_bc = jit(d2q9.periodic_bc) @jit def one_time_step(f, m): periodic_bc(f) transport(f) f2m(f, m) relaxation(m) m2f(m, f)
-1 * L * L / 2.) * (a1 * K + a2 * K * K + a3 * K * K * K + a4 * K * K * K * K + a5 * K * K * K * K * K) if x < 0: w = 1.0 - w return w def black_scholes(CallFlag, S, X, T, r, v): d1 = ((r + v * v / 2.) * T + log(S / X)) / (v * sqrt(T)) d2 = d1 - v * sqrt(T) z = exp(-1.0 * r * T) * X if CallFlag: return S * CND(d1) - z * CND(d2) else: return z * CND(-1.0 * d2) - S * CND(-1.0 * d1) black_scholes_parakeet = jit(black_scholes) def test_black_scholes(): x1 = (False, 10.0, 10.0, 2.0, 2.0, 2.0) x2 = (True, 10.0, 10.0, 2.0, 2.0, 2.0) xs = [x1, x2] for x in xs: expect(black_scholes, x, black_scholes(*x)) if __name__ == '__main__': run_local_tests()
# Authors: Olivier Grisel # License: MIT from pairwise import pairwise_python from parakeet import jit benchmarks = ( ("pairwise_parakeet_nested_for_loops", jit(pairwise_python.pairwise_python_nested_for_loops)), ("pairwise_parakeet_inner_numpy", jit(pairwise_python.pairwise_python_inner_numpy)), )
L = abs(x) K = 1.0 / (1.0 + 0.2316419 * L) w = 1.0 - 1.0/sqrt(2*3.141592653589793)* exp(-1*L*L/2.) * (a1*K + a2*K*K + a3*K*K*K + a4*K*K*K*K + a5*K*K*K*K*K) if x<0: w = 1.0-w return w def black_scholes(CallFlag,S,X,T,r,v): d1 = ((r+v*v/2.)*T+log(S/X))/(v*sqrt(T)) d2 = d1-v*sqrt(T) z = exp(-1.0*r*T) * X if CallFlag: return S*CND(d1) - z*CND(d2) else: return z*CND(-1.0*d2) - S*CND(-1.0*d1) black_scholes_parakeet = jit(black_scholes) def test_black_scholes(): x1 = (False, 10.0, 10.0, 2.0, 2.0, 2.0) x2 = (True, 10.0, 10.0, 2.0, 2.0, 2.0) xs = [x1, x2] for x in xs: par_rslt = black_scholes_parakeet(*x) py_rslt = black_scholes(*x) assert eq(par_rslt, py_rslt) if __name__ == '__main__': run_local_tests()
import d2q9_nxnyns as d2q9 import numpy as np from parakeet import jit import parakeet #parakeet.config.backend = "c" m2f_loc = jit(d2q9.m2f_loc) f2m_loc = jit(d2q9.f2m_loc) getf = jit(d2q9.getf) setf = jit(d2q9.setf) relaxation_loc = jit(d2q9.relaxation_loc) periodic_bc = jit(d2q9.periodic_bc) @jit def one_time_step(f1, f2): nx, ny, ns = f1.shape floc = np.zeros(ns) mloc = np.zeros(ns) periodic_bc(f1) for i in range(1, nx-1): for j in range(1, ny-1): getf(f1, floc, i, j) f2m_loc(floc, mloc) relaxation_loc(mloc) m2f_loc(mloc, floc) setf(f2, floc, i, j)
def compare_perf(fn, args, numba= True, cpython = True, extra = {}, backends = ('c', 'openmp', 'cuda'), suppress_output = False, propagate_exceptions = False): parakeet_fn = jit(fn) name = fn.__name__ parakeet_result = None numba_result = None cpython_result = None kwargs = {'suppress_stdout': suppress_output, 'suppress_stderr':suppress_output, 'propagate_exceptions' : propagate_exceptions } backend = None for backend in backends: with timer('Parakeet (backend = %s) #1 -- %s' % (backend, name), **kwargs): parakeet_result = parakeet_fn(*args, _backend = backend) with timer('Parakeet (backend = %s) #2 -- %s' % (backend, name), **kwargs): parakeet_result = parakeet_fn(*args, _backend = backend) if numba: from numba import autojit, config numba_fn = autojit(fn) with timer('Numba #1 -- %s' % name, **kwargs): numba_result = numba_fn(*args) with timer('Numba #2 -- %s' % name, **kwargs): numba_result = numba_fn(*args) if cpython: with timer('Python -- %s' % name, **kwargs): cpython_result = fn(*args) for name, impl in extra.iteritems(): with timer("%s #1" % name, **kwargs): impl(*args) with timer("%s #2" % name, **kwargs): extra_result = impl(*args) if python_result is not None: diffs = np.abs(parakeet_result - extra_result) assert np.allclose(parakeet_result, extra_result, atol = atol, rtol = rtol), \ "Max elt difference between Parakeet and %s = %s (median = %s, min = %s)" % \ (name, np.max(diffs), np.median(diffs), np.min(diffs)) rtol = 0.0001 if backend in ('cuda', 'gpu'): atol = 0.001 else: atol = 0.00001 if parakeet_result is not None and cpython_result is not None: diffs = np.abs(cpython_result - parakeet_result) assert np.allclose(cpython_result, parakeet_result, atol = atol, rtol = rtol), \ "Max elt difference between Parakeet and CPython = %s (median = %s, min = %s)" % \ (np.max(diffs), np.median(diffs), np.min(diffs)) if numba_result is not None and cpython_result is not None: diffs = np.abs(cpython_result - numba_result) assert np.allclose(cpython_result, numba_result, atol = atol, rtol = rtol), \ "Max elt difference between Numba and CPython = %s (median = %s, min = %s)" % \ (np.max(diffs), np.median(diffs), np.min(diffs))
def rosen_der_np(x): der = np.empty_like(x) der[1:-1] = (+ 200 * (x[1:-1] - x[:-2] ** 2) - 400 * (x[2:] - x[1:-1] ** 2) * x[1:-1] - 2 * (1 - x[1:-1])) der[0] = -400 * x[0] * (x[1] - x[0] ** 2) - 2 * (1 - x[0]) der[-1] = 200 * (x[-1] - x[-2] ** 2) return der def rosen_der_loops(x): n = x.shape[0] der = np.empty_like(x) for i in range(1, n - 1): der[i] = (+ 200 * (x[i] - x[i - 1] ** 2) - 400 * (x[i + 1] - x[i] ** 2) * x[i] - 2 * (1 - x[i])) der[0] = -400 * x[0] * (x[1] - x[0] ** 2) - 2 * (1 - x[0]) der[-1] = 200 * (x[-1] - x[-2] ** 2) return der if __name__ == '__main__': N = 10**5 x = np.arange(N) / float(N) jit(rosen_der_np)(x) from timer import compare_perf # numba still crashes on negative indexing compare_perf(rosen_der_np, [x.copy()], numba=False) compare_perf(rosen_der_loops, [x.copy()], numba=False)
from pairwise import pairwise_python from parakeet import jit benchmarks = ( ("pairwise_parakeet_nested_for_loops", jit(pairwise_python.pairwise_python_nested_for_loops)), ("pairwise_parakeet_inner_broadcasting", jit(pairwise_python.pairwise_python_inner_broadcasting)), )
def test_multiple_slices(): x = np.random.randn(10,10) expect_each(jit(multiple_slices), multiple_slices, matrices)
# License: MIT from arc_distance import arc_distance_python as adp from parakeet import jit import numpy as np @jit def arc_distance_parakeet_comprehensions(a, b): """ Calculates the pairwise arc distance between all points in vector a and b. Uses nested list comprehensions, which are efficiently parallelized by Parakeet. """ def arc_dist(ai, bj): theta1 = ai[0] phi1 = ai[1] theta2 = bj[0] phi2 = bj[1] d_theta = theta2 - theta1 d_phi = phi2 - phi1 temp = (np.sin(d_theta / 2) ** 2) + \ (np.cos(theta1) * np.cos(theta2) * np.sin(d_phi / 2) ** 2) return 2 * np.arctan2(np.sqrt(temp), np.sqrt(1 - temp)) return np.array([[arc_dist(ai, bj) for bj in b] for ai in a]) benchmarks = (("arc_distance_parakeet_for_loops", jit(adp.arc_distance_python_nested_for_loops)), ("arc_distance_parakeet_comprehensions", arc_distance_parakeet_comprehensions) )
def run(parakeet_fn, python_fn): testing_helpers.expect_allpairs(jit(parakeet_fn), python_fn, values)
from growcut import growcut_python from parakeet import jit benchmarks = (("growcut_parakeet", jit(growcut_python.growcut_python)), )
def run(fn, prop ): fn = parakeet.jit(fn) for v in values: expected = getattr(v, prop) result = fn(v) assert np.allclose(expected, result), "Expected %s but got %s" % (expected, result)
# Authors: Olivier Grisel # License: MIT from pairwise import pairwise_python from parakeet import jit import numpy as np def pairwise_parakeet_comprehensions(data): return np.array([[np.sqrt(np.sum((a - b)**2)) for b in data] for a in data]) benchmarks = ( ("pairwise_parakeet_nested_for_loops", jit(pairwise_python.pairwise_python_nested_for_loops)), ("pairwise_parakeet_inner_numpy", jit(pairwise_python.pairwise_python_inner_numpy)), ("pairwise_parakeet_comprehensions", jit(pairwise_parakeet_comprehensions)), )
from arc_distance import arc_distance_python as adp from parakeet import jit import numpy as np @jit def arc_distance_parakeet_comprehensions(a, b): """ Calculates the pairwise arc distance between all points in vector a and b. Uses nested list comprehensions, which are efficiently parallelized by Parakeet. """ def arc_dist(ai, bj): theta1 = ai[0] phi1 = ai[1] theta2 = bj[0] phi2 = bj[1] d_theta = theta2 - theta1 d_phi = phi2 - phi1 temp = (np.sin(d_theta / 2) ** 2) + \ (np.cos(theta1) * np.cos(theta2) * np.sin(d_phi / 2) ** 2) return 2 * np.arctan2(np.sqrt(temp), np.sqrt(1 - temp)) return np.array([[arc_dist(ai, bj) for bj in b] for ai in a]) benchmarks = (("arc_distance_parakeet_for_loops", jit(adp.arc_distance_python_nested_for_loops)), ("arc_distance_parakeet_comprehensions", arc_distance_parakeet_comprehensions))
# Authors: Olivier Grisel # License: MIT from pairwise import pairwise_python from parakeet import jit import numpy as np def pairwise_parakeet_comprehensions(data): return np.array([[np.sqrt(np.sum((a-b)**2)) for b in data] for a in data]) benchmarks = ( ("pairwise_parakeet_nested_for_loops", jit(pairwise_python.pairwise_python_nested_for_loops)), ("pairwise_parakeet_inner_numpy", jit(pairwise_python.pairwise_python_inner_numpy)), ("pairwise_parakeet_comprehensions", jit(pairwise_parakeet_comprehensions)), )
import numpy as np import time import parakeet from parakeet import testing_helpers def count_thresh_orig(values, thresh): n = 0 for elt in values: n += elt < thresh return n count_thresh = parakeet.jit(count_thresh_orig) def np_thresh(values, thresh): return np.sum(values < thresh) def par_thresh(values, thresh): return parakeet.sum(values < thresh) def test_count_thresh(): v = np.array([1.2, 1.4, 5.0, 2, 3]) parakeet_result = count_thresh(v, 2.0) python_result = count_thresh_orig(v, 2.0) testing_helpers.expect(count_thresh, [v, 2.0], count_thresh_orig(v, 2.0))
from parakeet import jit from parakeet.testing_helpers import run_local_tests, expect import time def f(x, y, z=1, q=3): return x + y + z + q jitf = jit(f) def test_call_overhead_identity(): n = 2000 x = 3 start_t = time.time() for i in xrange(n): f(x, x, x, x) python_time = time.time() - start_t print "Python time for %d calls: %f" % (n, python_time) # warm up! jitf(x, x, x, x) start_t = time.time() for i in xrange(n): jitf(x, x, x, x) parakeet_time = time.time() - start_t print "Parakeet time for %d calls: %f" % (n, parakeet_time) print "Slowdown = %f" % (parakeet_time / python_time) assert parakeet_time < 1000 * python_time, "Excessive call overhead: %f Python vs. %f Parakeet" % ( python_time, parakeet_time)
def rep(f, n = 1000, d = 10000): rands = np.random.randn(d, 1) urands = np.random.rand(d, 1) for i in xrange(n): f(1,2,rands,urands,1,0) n = 1000 t = time.time() rep(wald, n) py_t = time.time() - t waldjit = jit(wald) #warmup rep(waldjit, 1) t = time.time() rep(waldjit, n) par_t = time.time() - t config.value_specialization = False rep(waldjit, 1) t = time.time() rep(waldjit, n) par_t_no_specialization = time.time() - t print "Python time per call:", 1000 * (py_t / n), "ms" print "Parakeet time w/ value specialization:", 1000 * (par_t / n), "ms" print "Parakeet time w/out value specialization", 1000 * (par_t_no_specialization / n), "ms"