|
18 | 18 | """
|
19 | 19 |
|
20 | 20 | import os
|
21 |
| -import unittest |
| 21 | + |
| 22 | +import pytest |
22 | 23 |
|
23 | 24 | import dpctl
|
24 | 25 | import dpctl.program as dpctl_prog
|
25 | 26 |
|
26 |
| -from ._helper import has_gpu |
27 |
| - |
28 |
| - |
29 |
| -@unittest.skipUnless(has_gpu(), "No OpenCL GPU queues available") |
30 |
| -class TestProgramFromOCLSource(unittest.TestCase): |
31 |
| - def test_create_program_from_source(self): |
32 |
| - oclSrc = " \ |
33 |
| - kernel void add(global int* a, global int* b, global int* c) { \ |
34 |
| - size_t index = get_global_id(0); \ |
35 |
| - c[index] = a[index] + b[index]; \ |
36 |
| - } \ |
37 |
| - kernel void axpy(global int* a, global int* b, global int* c, int d) { \ |
38 |
| - size_t index = get_global_id(0); \ |
39 |
| - c[index] = a[index] + d*b[index]; \ |
40 |
| - }" |
41 |
| - q = dpctl.SyclQueue("opencl:gpu") |
42 |
| - prog = dpctl_prog.create_program_from_source(q, oclSrc) |
43 |
| - self.assertIsNotNone(prog) |
44 |
| - |
45 |
| - self.assertTrue(prog.has_sycl_kernel("add")) |
46 |
| - self.assertTrue(prog.has_sycl_kernel("axpy")) |
47 |
| - |
48 |
| - addKernel = prog.get_sycl_kernel("add") |
49 |
| - axpyKernel = prog.get_sycl_kernel("axpy") |
50 |
| - |
51 |
| - self.assertEqual(addKernel.get_function_name(), "add") |
52 |
| - self.assertEqual(axpyKernel.get_function_name(), "axpy") |
53 |
| - self.assertEqual(addKernel.get_num_args(), 3) |
54 |
| - self.assertEqual(axpyKernel.get_num_args(), 4) |
55 |
| - |
56 |
| - |
57 |
| -@unittest.skipUnless(has_gpu(), "No OpenCL GPU queues available") |
58 |
| -class TestProgramFromSPRIV(unittest.TestCase): |
59 |
| - def test_create_program_from_spirv(self): |
60 |
| - |
61 |
| - CURR_DIR = os.path.dirname(os.path.abspath(__file__)) |
62 |
| - spirv_file = os.path.join(CURR_DIR, "input_files/multi_kernel.spv") |
63 |
| - with open(spirv_file, "rb") as fin: |
64 |
| - spirv = fin.read() |
65 |
| - q = dpctl.SyclQueue("opencl:gpu") |
66 |
| - prog = dpctl_prog.create_program_from_spirv(q, spirv) |
67 |
| - self.assertIsNotNone(prog) |
68 |
| - self.assertTrue(prog.has_sycl_kernel("add")) |
69 |
| - self.assertTrue(prog.has_sycl_kernel("axpy")) |
70 |
| - |
71 |
| - addKernel = prog.get_sycl_kernel("add") |
72 |
| - axpyKernel = prog.get_sycl_kernel("axpy") |
73 |
| - |
74 |
| - self.assertEqual(addKernel.get_function_name(), "add") |
75 |
| - self.assertEqual(axpyKernel.get_function_name(), "axpy") |
76 |
| - self.assertEqual(addKernel.get_num_args(), 3) |
77 |
| - self.assertEqual(axpyKernel.get_num_args(), 4) |
78 |
| - |
79 |
| - |
80 |
| -@unittest.skipUnless( |
81 |
| - has_gpu(backend=dpctl.backend_type.level_zero), |
82 |
| - "No Level0 GPU queues available", |
| 27 | + |
| 28 | +def get_spirv_abspath(fn): |
| 29 | + curr_dir = os.path.dirname(os.path.abspath(__file__)) |
| 30 | + spirv_file = os.path.join(curr_dir, "input_files", fn) |
| 31 | + return spirv_file |
| 32 | + |
| 33 | + |
| 34 | +def test_create_program_from_source_ocl(): |
| 35 | + oclSrc = " \ |
| 36 | + kernel void add(global int* a, global int* b, global int* c) { \ |
| 37 | + size_t index = get_global_id(0); \ |
| 38 | + c[index] = a[index] + b[index]; \ |
| 39 | + } \ |
| 40 | + kernel void axpy(global int* a, global int* b, global int* c, int d) { \ |
| 41 | + size_t index = get_global_id(0); \ |
| 42 | + c[index] = a[index] + d*b[index]; \ |
| 43 | + }" |
| 44 | + try: |
| 45 | + q = dpctl.SyclQueue("opencl") |
| 46 | + except dpctl.SyclQueueCreationError: |
| 47 | + pytest.skip("No OpenCL queue is available") |
| 48 | + prog = dpctl_prog.create_program_from_source(q, oclSrc) |
| 49 | + assert prog is not None |
| 50 | + |
| 51 | + assert prog.has_sycl_kernel("add") |
| 52 | + assert prog.has_sycl_kernel("axpy") |
| 53 | + |
| 54 | + addKernel = prog.get_sycl_kernel("add") |
| 55 | + axpyKernel = prog.get_sycl_kernel("axpy") |
| 56 | + |
| 57 | + assert "add" == addKernel.get_function_name() |
| 58 | + assert "axpy" == axpyKernel.get_function_name() |
| 59 | + assert 3 == addKernel.get_num_args() |
| 60 | + assert 4 == axpyKernel.get_num_args() |
| 61 | + |
| 62 | + |
| 63 | +def test_create_program_from_spirv_ocl(): |
| 64 | + try: |
| 65 | + q = dpctl.SyclQueue("opencl") |
| 66 | + except dpctl.SyclQueueCreationError: |
| 67 | + pytest.skip("No OpenCL queue is available") |
| 68 | + spirv_file = get_spirv_abspath("multi_kernel.spv") |
| 69 | + with open(spirv_file, "rb") as fin: |
| 70 | + spirv = fin.read() |
| 71 | + prog = dpctl_prog.create_program_from_spirv(q, spirv) |
| 72 | + assert prog is not None |
| 73 | + assert prog.has_sycl_kernel("add") |
| 74 | + assert prog.has_sycl_kernel("axpy") |
| 75 | + |
| 76 | + addKernel = prog.get_sycl_kernel("add") |
| 77 | + axpyKernel = prog.get_sycl_kernel("axpy") |
| 78 | + |
| 79 | + assert "add" == addKernel.get_function_name() |
| 80 | + assert "axpy" == axpyKernel.get_function_name() |
| 81 | + assert 3 == addKernel.get_num_args() |
| 82 | + assert 4 == axpyKernel.get_num_args() |
| 83 | + |
| 84 | + |
| 85 | +def test_create_program_from_spirv_l0(): |
| 86 | + try: |
| 87 | + q = dpctl.SyclQueue("level_zero") |
| 88 | + except dpctl.SyclQueueCreationError: |
| 89 | + pytest.skip("No Level-zero queue is available") |
| 90 | + spirv_file = get_spirv_abspath("multi_kernel.spv") |
| 91 | + with open(spirv_file, "rb") as fin: |
| 92 | + spirv = fin.read() |
| 93 | + dpctl_prog.create_program_from_spirv(q, spirv) |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.xfail( |
| 97 | + reason="Level-zero backend does not support compilation from source" |
83 | 98 | )
|
84 |
| -class TestProgramForLevel0GPU(unittest.TestCase): |
85 |
| - |
86 |
| - import sys |
87 |
| - |
88 |
| - def test_create_program_from_spirv(self): |
89 |
| - CURR_DIR = os.path.dirname(os.path.abspath(__file__)) |
90 |
| - spirv_file = os.path.join(CURR_DIR, "input_files/multi_kernel.spv") |
91 |
| - with open(spirv_file, "rb") as fin: |
92 |
| - spirv = fin.read() |
93 |
| - q = dpctl.SyclQueue("level_zero:gpu") |
94 |
| - dpctl_prog.create_program_from_spirv(q, spirv) |
95 |
| - |
96 |
| - @unittest.expectedFailure |
97 |
| - def test_create_program_from_source(self): |
98 |
| - oclSrc = " \ |
99 |
| - kernel void add(global int* a, global int* b, global int* c) { \ |
100 |
| - size_t index = get_global_id(0); \ |
101 |
| - c[index] = a[index] + b[index]; \ |
102 |
| - } \ |
103 |
| - kernel void axpy(global int* a, global int* b, global int* c, int d) { \ |
104 |
| - size_t index = get_global_id(0); \ |
105 |
| - c[index] = a[index] + d*b[index]; \ |
106 |
| - }" |
107 |
| - q = dpctl.SyclQueue("level_zero:gpu") |
108 |
| - dpctl_prog.create_program_from_source(q, oclSrc) |
109 |
| - |
110 |
| - |
111 |
| -if __name__ == "__main__": |
112 |
| - unittest.main() |
| 99 | +def test_create_program_from_source_l0(): |
| 100 | + try: |
| 101 | + q = dpctl.SyclQueue("level_zero") |
| 102 | + except dpctl.SyclQueueCreationError: |
| 103 | + pytest.skip("No Level-zero queue is available") |
| 104 | + oclSrc = " \ |
| 105 | + kernel void add(global int* a, global int* b, global int* c) { \ |
| 106 | + size_t index = get_global_id(0); \ |
| 107 | + c[index] = a[index] + b[index]; \ |
| 108 | + } \ |
| 109 | + kernel void axpy(global int* a, global int* b, global int* c, int d) { \ |
| 110 | + size_t index = get_global_id(0); \ |
| 111 | + c[index] = a[index] + d*b[index]; \ |
| 112 | + }" |
| 113 | + dpctl_prog.create_program_from_source(q, oclSrc) |
0 commit comments