from transonic import boost def func(x: int): return x**2 def func2(x: int): return x**2 func_boosted = boost(func)
return fx, fy def fxfy_loops(ft: A, fn: A, theta: A): n0 = theta.size fx = np.empty_like(ft) fy = np.empty_like(fn) for index in range(n0): sin_theta = np.sin(theta[index]) cos_theta = np.cos(theta[index]) fx[index] = cos_theta * ft[index] - sin_theta * fn[index] fy[index] = sin_theta * ft[index] + cos_theta * fn[index] return fx, fy fxfy_pythran = boost(backend="pythran")(fxfy) fxfy_numba = boost(backend="numba")(fxfy) fxfy_loops_pythran = boost(backend="pythran")(fxfy_loops) fxfy_loops_numba = boost(backend="numba")(fxfy_loops) if __name__ == "__main__": from transonic.util import print_versions, timeit_verbose print_versions() theta = np.linspace(0, 2 * np.pi, 10000) ft = 2.5 * theta fv = 1.5 * theta loc = locals()
def func(a, b): i: int i = 2 c: float = 0.1 + i return a + b + c def func_tmp(arg): return arg**2 def func0(a, b): return a + b func0_boosted = boost(func0) A = Union[int, Array[int, "1d", "C"]] @boost def func2(a: A, b: float): return a - func_tmp(b) A1 = Array[int, "1d", "C", "memview"] @boost def func3(c: const(A1)): return c[0] + 1
return np.arctan2(2 * np.exp(a)**2 + 4 * np.log(a * b)**3, 2 / a) def broadcast(a: A, b: A1, out: A): out[:] = expr(a, b) def broadcast_loops(a: A, b: A1, out: A): n0, n1, n2 = a.shape for i0 in range(n0): for i1 in range(n1): for i2 in range(n2): out[i0, i1, i2] = expr(a[i0, i1, i2], b[i2]) broadcast_pythran = boost(backend="pythran")(broadcast) broadcast_numba = boost(backend="numba")(broadcast) broadcast_loops_pythran = boost(backend="pythran")(broadcast_loops) broadcast_loops_numba = boost(backend="numba")(broadcast_loops) if __name__ == "__main__": from transonic.util import print_versions, timeit_verbose print_versions() shape = (4, 4, 64) a = np.linspace(1, 100, np.prod(shape)).reshape(shape) b = np.linspace(1, 100, shape[-1]) out = np.empty_like(a)
n0, n1, n2 = kx.shape[0], kx.shape[1], kx.shape[2] for i0 in range(n0): for i1 in range(n1): for i2 in range(n2): tmp = (kx[i0, i1, i2] * vx[i0, i1, i2] + ky[i0, i1, i2] * vy[i0, i1, i2] + kz[i0, i1, i2] * vz[i0, i1, i2]) * inv_k_square_nozero[i0, i1, i2] vx[i0, i1, i2] -= kx[i0, i1, i2] * tmp vy[i0, i1, i2] -= ky[i0, i1, i2] * tmp vz[i0, i1, i2] -= kz[i0, i1, i2] * tmp proj_pythran = boost(backend="pythran")(proj) proj_numba = boost(backend="numba")(proj) proj_cython = boost(backend="cython")(proj) proj_loop_pythran = boost(backend="pythran")(proj_loop) proj_loop_numba = boost(backend="numba")(proj_loop) proj_loop_cython = boost(backend="cython", boundscheck=False, wraparound=False)(proj_loop) if __name__ == "__main__": from textwrap import dedent from transonic.util import print_versions, timeit_verbose loc = locals()
from transonic import boost @boost def f(): pass @boost(inline=1) def f(): pass def f1(): pass def f2(): pass f1_ = boost(f1) f1_ = boost(inline=1)(f1) # decor = boost(inline=1) # f1_ = decor(f1) # f2_ = decor(f2)
def laplace_loops(image: Image): """Laplace operator for 2D images.""" h = image.shape[0] w = image.shape[1] laplacian = np.empty((h - 2, w - 2), np.uint8) for i in range(1, h - 1): for j in range(1, w - 1): laplacian[i - 1, j - 1] = ( np.abs(image[i - 1, j] + image[i + 1, j] + image[i, j - 1] + image[i, j + 1] - 4 * image[i, j]) > 0.05) return laplacian laplace_transonic_pythran = boost(backend="pythran")(laplace_numpy) laplace_transonic_cython = boost(backend="cython")(laplace_numpy) laplace_transonic_numba = boost(backend="numba")(laplace_numpy) laplace_transonic_python = boost(backend="python")(laplace_numpy) laplace_numba = numba.njit(laplace_numpy) laplace_loops_transonic_pythran = boost(backend="pythran")(laplace_loops) laplace_loops_transonic_python = boost(backend="python")(laplace_loops) laplace_loops_transonic_numba = boost(backend="numba")(laplace_loops) laplace_loops_numba = numba.njit(laplace_loops) # For Cython, we need to add more type annotations @boost(backend="cython", boundscheck=False, wraparound=False) def laplace_loops_transonic_cython(image: Image):