def test_trigonometric_fn(filter_str, trig_op, input_arrays): # FIXME: Why does archcosh fail on Gen12 discrete graphics card? if trig_op == "arccosh" and is_gen12(filter_str): pytest.skip() a, b = input_arrays trig_fn = getattr(np, trig_op) actual = np.empty(shape=a.shape, dtype=a.dtype) expected = np.empty(shape=a.shape, dtype=a.dtype) if trig_op == "arctan2": @njit def f(a, b): return trig_fn(a, b) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), assert_auto_offloading(): actual = f(a, b) expected = trig_fn(a, b) else: @njit def f(a): return trig_fn(a) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), assert_auto_offloading(): actual = f(a) expected = trig_fn(a) np.testing.assert_allclose(actual, expected, rtol=1e-5, atol=0)
def test_multiple_prange(self): @njit def f(a, b): # dimensions must be provided as scalar m, n = a.shape for i in prange(m): val = 10 for j in prange(n): b[i, j] = a[i, j] * val for i in prange(m): for j in prange(n): a[i, j] = a[i, j] * 10 m = 8 n = 8 a = np.ones((m, n)) b = np.ones((m, n)) device = dpctl.SyclDevice("opencl:gpu") with assert_auto_offloading( parfor_offloaded=2), dpctl.device_context(device): f(a, b) assert np.all(b == 10) assert np.all(a == 10)
def test_njit(filter_str): if _helper.platform_not_supported(filter_str): pytest.skip() if _helper.skip_test(filter_str): pytest.skip() @vectorize(nopython=True) def axy(a, x, y): return a * x + y def f(a0, a1): return np.cos(axy(a0, np.sin(a1) - 1.0, 1.0)) A = np.random.random(10) B = np.random.random(10) device = dpctl.SyclDevice(filter_str) with dppy.offload_to_sycl_device(device), assert_auto_offloading(): f_njit = njit(f) expected = f_njit(A, B) actual = f(A, B) max_abs_err = expected.sum() - actual.sum() assert max_abs_err < 1e-5
def test_dpnp_interacting_with_parfor(self): def f(a, b): c = np.sum(a) e = np.add(b, a) d = c + e return d device = dpctl.SyclDevice("opencl:gpu") with dpctl.device_context( device), assert_auto_offloading(), dpnp_debug(): njit_f = njit(f) got = njit_f(self.a, self.b) expected = f(self.a, self.b) max_abs_err = got.sum() - expected.sum() assert max_abs_err < 1e-4
def test_binary_ops(filter_str, binary_op, input_arrays): a, b = input_arrays binop = getattr(np, binary_op) actual = np.empty(shape=a.shape, dtype=a.dtype) expected = np.empty(shape=a.shape, dtype=a.dtype) @njit def f(a, b): return binop(a, b) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), assert_auto_offloading(): actual = f(a, b) expected = binop(a, b) np.testing.assert_allclose(actual, expected, rtol=1e-5, atol=0)
def test_one_prange(self): @njit def f(a, b): for i in prange(4): b[i, 0] = a[i, 0] * 10 m = 8 n = 8 a = np.ones((m, n)) b = np.ones((m, n)) device = dpctl.SyclDevice("opencl:gpu") with assert_auto_offloading(), dpctl.device_context(device): f(a, b) for i in range(4): assert b[i, 0] == a[i, 0] * 10
def test_one_prange(self): @njit def f(a, b): for i in prange(4): b[i, 0] = a[i, 0] * 10 m = 8 n = 8 a = np.ones((m, n)) b = np.ones((m, n)) device = dpctl.SyclDevice("opencl:gpu") with assert_auto_offloading(), dppy.offload_to_sycl_device(device): f(a, b) for i in range(4): self.assertTrue(b[i, 0] == a[i, 0] * 10)
def test_unary_ops(filter_str, unary_op, input_arrays): if skip_test(filter_str): pytest.skip() a = input_arrays[0] uop = getattr(np, unary_op) actual = np.empty(shape=a.shape, dtype=a.dtype) expected = np.empty(shape=a.shape, dtype=a.dtype) @njit def f(a): return uop(a) device = dpctl.SyclDevice(filter_str) with dppy.offload_to_sycl_device(device), assert_auto_offloading(): actual = f(a) expected = uop(a) np.testing.assert_allclose(actual, expected, rtol=1e-5, atol=0)
def test_njit(filter_str): @vectorize(nopython=True) def axy(a, x, y): return a * x + y def f(a0, a1): return np.cos(axy(a0, np.sin(a1) - 1.0, 1.0)) A = np.random.random(10) B = np.random.random(10) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), assert_auto_offloading(): f_njit = njit(f) expected = f_njit(A, B) actual = f(A, B) max_abs_err = expected.sum() - actual.sum() assert max_abs_err < 1e-5
def test_nested_prange(self): @njit def f(a, b): # dimensions must be provided as scalar m, n = a.shape for i in prange(m): for j in prange(n): b[i, j] = a[i, j] * 10 m = 8 n = 8 a = np.ones((m, n)) b = np.ones((m, n)) device = dpctl.SyclDevice("opencl:gpu") with assert_auto_offloading(), dppy.offload_to_sycl_device(device): f(a, b) self.assertTrue(np.all(b == 10))
def test_unary_ops(filter_str, unary_op, input_arrays): # FIXME: Why does sign fail on Gen12 discrete graphics card? skip_ops = ["sign", "log", "log2", "log10", "expm1"] if unary_op in skip_ops and is_gen12(filter_str): pytest.skip() a = input_arrays[0] uop = getattr(np, unary_op) actual = np.empty(shape=a.shape, dtype=a.dtype) expected = np.empty(shape=a.shape, dtype=a.dtype) @njit def f(a): return uop(a) device = dpctl.SyclDevice(filter_str) with dpctl.device_context(device), assert_auto_offloading(): actual = f(a) expected = uop(a) np.testing.assert_allclose(actual, expected, rtol=1e-5, atol=0)
def test_2d_arrays(self): def prange_example(): n = 10 a = np.ones((n, n), dtype=np.float64) b = np.ones((n, n), dtype=np.float64) c = np.ones((n, n), dtype=np.float64) for i in prange(n // 2): a[i] = b[i] + c[i] return a jitted = njit(prange_example) device = dpctl.SyclDevice("opencl:gpu") with assert_auto_offloading( parfor_offloaded=2), dpctl.device_context(device): jitted_res = jitted() res = prange_example() np.testing.assert_equal(res, jitted_res)
def test_three_prange(self): @njit def f(a, b): # dimensions must be provided as scalar m, n, o = a.shape for i in prange(m): val = 10 for j in prange(n): constant = 2 for k in prange(o): b[i, j, k] = a[i, j, k] * (val + constant) m = 8 n = 8 o = 8 a = np.ones((m, n, o)) b = np.ones((m, n, o)) device = dpctl.SyclDevice("opencl:gpu") with assert_auto_offloading( parfor_offloaded=1), dpctl.device_context(device): f(a, b) assert np.all(b == 12)