|
| 1 | +import numba |
| 2 | +import numpy as np |
| 3 | +from numba import dppl, njit |
| 4 | +from numba.core import errors |
| 5 | +from numba.tests.support import captured_stdout |
| 6 | +from numba.dppl.testing import DPPLTestCase, unittest |
| 7 | +import dpctl |
| 8 | +import dpctl.ocldrv as ocldrv |
| 9 | + |
| 10 | + |
| 11 | +@unittest.skipIf(not dpctl.has_gpu_queues(), "No GPU platforms available") |
| 12 | +@unittest.skipIf(not dpctl.has_cpu_queues(), "No CPU platforms available") |
| 13 | +class TestWithDPPLContext(DPPLTestCase): |
| 14 | + def test_with_dppl_context_gpu(self): |
| 15 | + |
| 16 | + @njit |
| 17 | + def nested_func(a, b): |
| 18 | + np.sin(a, b) |
| 19 | + |
| 20 | + @njit |
| 21 | + def func(b): |
| 22 | + a = np.ones((64), dtype=np.float64) |
| 23 | + nested_func(a, b) |
| 24 | + |
| 25 | + numba.dppl.compiler.DEBUG = 1 |
| 26 | + expected = np.ones((64), dtype=np.float64) |
| 27 | + got_gpu = np.ones((64), dtype=np.float64) |
| 28 | + |
| 29 | + with captured_stdout() as got_gpu_message: |
| 30 | + with dpctl.device_context(dpctl.device_type.gpu): |
| 31 | + func(got_gpu) |
| 32 | + |
| 33 | + func(expected) |
| 34 | + |
| 35 | + np.testing.assert_array_equal(expected, got_gpu) |
| 36 | + self.assertTrue('Parfor lowered on DPPL-device' in got_gpu_message.getvalue()) |
| 37 | + |
| 38 | + |
| 39 | + def test_with_dppl_context_cpu(self): |
| 40 | + |
| 41 | + @njit |
| 42 | + def nested_func(a, b): |
| 43 | + np.sin(a, b) |
| 44 | + |
| 45 | + @njit |
| 46 | + def func(b): |
| 47 | + a = np.ones((64), dtype=np.float64) |
| 48 | + nested_func(a, b) |
| 49 | + |
| 50 | + numba.dppl.compiler.DEBUG = 1 |
| 51 | + expected = np.ones((64), dtype=np.float64) |
| 52 | + got_cpu = np.ones((64), dtype=np.float64) |
| 53 | + |
| 54 | + with captured_stdout() as got_cpu_message: |
| 55 | + with dpctl.device_context(dpctl.device_type.cpu): |
| 56 | + func(got_cpu) |
| 57 | + |
| 58 | + func(expected) |
| 59 | + |
| 60 | + np.testing.assert_array_equal(expected, got_cpu) |
| 61 | + self.assertTrue('Parfor lowered on DPPL-device' not in got_cpu_message.getvalue()) |
| 62 | + |
| 63 | + |
| 64 | + def test_with_dppl_context_target(self): |
| 65 | + |
| 66 | + @njit(target='cpu') |
| 67 | + def nested_func_target(a, b): |
| 68 | + np.sin(a, b) |
| 69 | + |
| 70 | + @njit(target='gpu') |
| 71 | + def func_target(b): |
| 72 | + a = np.ones((64), dtype=np.float64) |
| 73 | + nested_func_target(a, b) |
| 74 | + |
| 75 | + @njit |
| 76 | + def func_no_target(b): |
| 77 | + a = np.ones((64), dtype=np.float64) |
| 78 | + nested_func_target(a, b) |
| 79 | + |
| 80 | + a = np.ones((64), dtype=np.float64) |
| 81 | + b = np.ones((64), dtype=np.float64) |
| 82 | + |
| 83 | + with self.assertRaises(errors.UnsupportedError) as raises_1: |
| 84 | + with dpctl.device_context(dpctl.device_type.gpu): |
| 85 | + nested_func_target(a, b) |
| 86 | + |
| 87 | + with self.assertRaises(errors.UnsupportedError) as raises_2: |
| 88 | + with dpctl.device_context(dpctl.device_type.gpu): |
| 89 | + func_target(a) |
| 90 | + |
| 91 | + with self.assertRaises(errors.UnsupportedError) as raises_3: |
| 92 | + with dpctl.device_context(dpctl.device_type.gpu): |
| 93 | + func_no_target(a) |
| 94 | + |
| 95 | + msg = "Unsupported defined 'target' with using context device" |
| 96 | + self.assertTrue(msg in str(raises_1.exception)) |
| 97 | + self.assertTrue(msg in str(raises_2.exception)) |
| 98 | + self.assertTrue(msg in str(raises_3.exception)) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + unittest.main() |
0 commit comments