def code_to_function(text, name, args_size, return_size): from numpy import zeros from numpy import exp, log from numpy import sin, cos, tan from numpy import arcsin as asin from numpy import arccos as acos from numpy import arctan as atan from numpy import sinh, cosh, tanh from numpy import pi from numpy import inf d = locals() e = {} print(text) exec(text, d, e) fun = e[name] from numba.vectorize import GUVectorize from numba import float64 signature = str.join(',', ['(n{})'.format(i) for i in range(len(args_size))]) signature += '->(n)'.format(return_size) args_types = [float64[:]] * (len(args_size) + 1) print(signature) print(args_types) gufunc = GUVectorize(fun, signature) gufunc.add(argtypes=args_types) fun = gufunc.build_ufunc() return fun
def code_to_function(text, name, args_size, return_size): from numpy import zeros from numpy import exp, log from numpy import sin, cos, tan from numpy import arcsin as asin from numpy import arccos as acos from numpy import arctan as atan from numpy import sinh, cosh, tanh from numpy import pi from numpy import inf d = locals() e = {} print(text) exec(text, d, e) fun = e[name] from numba.vectorize import GUVectorize from numba import float64 signature = str.join(',',['(n{})'.format(i) for i in range(len(args_size))]) signature += '->(n)'.format(return_size) args_types = [float64[:]]*(len(args_size)+1) print(signature) print(args_types) gufunc = GUVectorize(fun, signature) gufunc.add(argtypes=args_types) fun = gufunc.build_ufunc() return fun
def test_exp_avg(): vect = GUVectorize(exp_avg, '(t),()->(t)') vect.add('void(f8[:], f8, f8[:])') ufunc = vect.build_ufunc() arr_t = numpy.arange(10, dtype=numpy.float64) decay_length = numpy.float64(123) out_t = numpy.empty(10, dtype=numpy.float64) ufunc(arr_t, decay_length, out_t) expect = numpy.zeros_like(out_t) exp_avg(arr_t, decay_length, expect) numpy.allclose(expect, out_t)
def test_gufunc_array_expressions(): gufunc = GUVectorize(array_expr_gufunc, '(m,n),(n,p)->(m,p)') gufunc.add(argtypes=[float_[:, :], float_[:, :], float_[:, :]]) gufunc = gufunc.build_ufunc() matrix_ct = 10 A = np.arange(matrix_ct * 2 * 4, dtype=np.float32).reshape(matrix_ct, 2, 4) B = np.arange(matrix_ct * 4 * 5, dtype=np.float32).reshape(matrix_ct, 4, 5) C = gufunc(A, B) Gold = ut.matrix_multiply(A, B) if (C != Gold).any(): print(C) print(Gold) raise ValueError
def test_scalar_output(): gufunc = GUVectorize(mycore, '(m,n)->()') gufunc.add('void(float32[:,:], float32[:])') gufunc.add('void(float64[:,:], float64[:])') myfunc = gufunc.build_ufunc() A = numpy.arange(2 * 2 * 10).reshape(10, 2, 2) out = numpy.zeros(10) myfunc(A, out=out) expect = numpy.zeros_like(out) for i in range(A.shape[0]): expect[i] = A[i].sum() numpy.allclose(expect, out)
def test_gufunc_array_expressions(): gufunc = GUVectorize(array_expr_gufunc, '(m,n),(n,p)->(m,p)') gufunc.add(argtypes=[float_[:,:], float_[:,:], float_[:,:]]) gufunc = gufunc.build_ufunc() matrix_ct = 10 A = np.arange(matrix_ct * 2 * 4, dtype=np.float32).reshape(matrix_ct, 2, 4) B = np.arange(matrix_ct * 4 * 5, dtype=np.float32).reshape(matrix_ct, 4, 5) C = gufunc(A, B) Gold = ut.matrix_multiply(A, B) if (C != Gold).any(): print(C) print(Gold) raise ValueError
def _test_gufunc(backend, target): gufunc = GUVectorize(matmulcore, '(m,n),(n,p)->(m,p)') gufunc.add(argtypes=[f4[:, :], f4[:, :], f4[:, :]]) gufunc = gufunc.build_ufunc() matrix_ct = 1001 # an odd number to test thread/block division in CUDA A = np.arange(matrix_ct * 2 * 4, dtype=np.float32).reshape(matrix_ct, 2, 4) B = np.arange(matrix_ct * 4 * 5, dtype=np.float32).reshape(matrix_ct, 4, 5) C = gufunc(A, B) Gold = ut.matrix_multiply(A, B) # print(A) # print(B) # print(C) # print(Gold) assert np.allclose(C, Gold)
def _test_gufunc(backend, target): gufunc = GUVectorize(matmulcore, '(m,n),(n,p)->(m,p)') gufunc.add(argtypes=[f4[:,:], f4[:,:], f4[:,:]]) gufunc = gufunc.build_ufunc() matrix_ct = 1001 # an odd number to test thread/block division in CUDA A = np.arange(matrix_ct * 2 * 4, dtype=np.float32).reshape(matrix_ct, 2, 4) B = np.arange(matrix_ct * 4 * 5, dtype=np.float32).reshape(matrix_ct, 4, 5) C = gufunc(A, B) Gold = ut.matrix_multiply(A, B) # print(A) # print(B) # print(C) # print(Gold) assert np.allclose(C, Gold)
def test_saxpy(): vect = GUVectorize(saxpy, '(),(t),(t)->(t)') vect.add('void(f4, f4[:], f4[:], f4[:])') gusaxpy = vect.build_ufunc() A = numpy.array(numpy.float32(2)) X = numpy.arange(10, dtype=numpy.float32).reshape(5, 2) Y = numpy.arange(10, dtype=numpy.float32).reshape(5, 2) out = gusaxpy(A, X, Y) for j in range(5): for i in range(2): exp = A * X[j, i] + Y[j, i] assert exp == out[j, i], (exp, out[j, i]) A = numpy.arange(5, dtype=numpy.float32) out = gusaxpy(A, X, Y) for j in range(5): for i in range(2): exp = A[j] * X[j, i] + Y[j, i] assert exp == out[j, i]
def test_saxpy(): vect = GUVectorize(saxpy, '(),(t),(t)->(t)') vect.add('void(f4, f4[:], f4[:], f4[:])') gusaxpy = vect.build_ufunc() A = numpy.array(numpy.float32(2)) X = numpy.arange(10, dtype=numpy.float32).reshape(5,2) Y = numpy.arange(10, dtype=numpy.float32).reshape(5,2) out = gusaxpy(A, X, Y) for j in range(5): for i in range(2): exp = A * X[j, i] + Y[j, i] assert exp == out[j, i], (exp, out[j, i]) A = numpy.arange(5, dtype=numpy.float32) out = gusaxpy(A, X, Y) for j in range(5): for i in range(2): exp = A[j] * X[j, i] + Y[j, i] assert exp == out[j, i]