|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +import numba |
| 4 | +import numba_dppy |
| 5 | +from numba_dppy.testing import unittest |
| 6 | +from numba_dppy.testing import DPPYTestCase |
| 7 | +from numba.tests.support import captured_stderr |
| 8 | +import dpctl |
| 9 | + |
| 10 | + |
| 11 | +@unittest.skipUnless(dpctl.has_gpu_queues(), 'test only on GPU system') |
| 12 | +class TestDPPYFallback(DPPYTestCase): |
| 13 | + def test_dppy_fallback_true(self): |
| 14 | + @numba.jit |
| 15 | + def fill_value(i): |
| 16 | + return i |
| 17 | + |
| 18 | + def inner_call_fallback(): |
| 19 | + x = 10 |
| 20 | + a = np.empty(shape=x, dtype=np.float32) |
| 21 | + |
| 22 | + for i in numba.prange(x): |
| 23 | + a[i] = fill_value(i) |
| 24 | + |
| 25 | + return a |
| 26 | + |
| 27 | + numba_dppy.compiler.DEBUG = 1 |
| 28 | + with captured_stderr() as msg_fallback_true: |
| 29 | + with dpctl.device_context("opencl:gpu") as gpu_queue: |
| 30 | + dppy = numba.njit(parallel=True)(inner_call_fallback) |
| 31 | + dppy_fallback_true = dppy() |
| 32 | + |
| 33 | + ref_result = inner_call_fallback() |
| 34 | + numba_dppy.compiler.DEBUG = 0 |
| 35 | + |
| 36 | + np.testing.assert_array_equal(dppy_fallback_true, ref_result) |
| 37 | + self.assertTrue('Failed to lower parfor on DPPY-device' in msg_fallback_true.getvalue()) |
| 38 | + |
| 39 | + @unittest.expectedFailure |
| 40 | + def test_dppy_fallback_false(self): |
| 41 | + @numba.jit |
| 42 | + def fill_value(i): |
| 43 | + return i |
| 44 | + |
| 45 | + def inner_call_fallback(): |
| 46 | + x = 10 |
| 47 | + a = np.empty(shape=x, dtype=np.float32) |
| 48 | + |
| 49 | + for i in numba.prange(x): |
| 50 | + a[i] = fill_value(i) |
| 51 | + |
| 52 | + return a |
| 53 | + |
| 54 | + try: |
| 55 | + numba_dppy.compiler.DEBUG = 1 |
| 56 | + numba_dppy.config.FALLBACK_ON_CPU = 0 |
| 57 | + with captured_stderr() as msg_fallback_true: |
| 58 | + with dpctl.device_context("opencl:gpu") as gpu_queue: |
| 59 | + dppy = numba.njit(parallel=True)(inner_call_fallback) |
| 60 | + dppy_fallback_false = dppy() |
| 61 | + |
| 62 | + finally: |
| 63 | + ref_result = inner_call_fallback() |
| 64 | + numba_dppy.config.FALLBACK_ON_CPU = 1 |
| 65 | + numba_dppy.compiler.DEBUG = 0 |
| 66 | + |
| 67 | + not np.testing.assert_array_equal(dppy_fallback_false, ref_result) |
| 68 | + not self.assertTrue('Failed to lower parfor on DPPY-device' in msg_fallback_true.getvalue()) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == '__main__': |
| 72 | + unittest.main() |
0 commit comments