Skip to content

Commit f4c1926

Browse files
author
Diptorup Deb
committed
Add SyclContext unit test cases.
- Added unit tests for __name__, __repr__, addressof_ref - Added an additional negative test for __eq__ - Removed test fixtures for device members. These members are already tested for SyclDevice.
1 parent 289ed36 commit f4c1926

File tree

1 file changed

+37
-292
lines changed

1 file changed

+37
-292
lines changed

dpctl/tests/test_sycl_context.py

Lines changed: 37 additions & 292 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@
2121

2222
import dpctl
2323

24-
list_of_standard_selectors = [
25-
dpctl.select_accelerator_device,
26-
dpctl.select_cpu_device,
27-
dpctl.select_default_device,
28-
dpctl.select_gpu_device,
29-
dpctl.select_host_device,
30-
]
31-
3224
list_of_valid_filter_selectors = [
3325
"opencl",
3426
"opencl:gpu",
@@ -45,308 +37,36 @@
4537
"1",
4638
]
4739

48-
list_of_invalid_filter_selectors = [
49-
"-1",
50-
"opencl:gpu:-1",
51-
"level_zero:cpu:0",
52-
"abc",
53-
]
54-
55-
56-
# Unit test cases that will be run for every device
57-
def check_get_max_compute_units(device):
58-
max_compute_units = device.max_compute_units
59-
assert max_compute_units > 0
60-
61-
62-
def check_get_max_work_item_dims(device):
63-
max_work_item_dims = device.max_work_item_dims
64-
assert max_work_item_dims > 0
65-
66-
67-
def check_get_max_work_item_sizes(device):
68-
max_work_item_sizes = device.max_work_item_sizes
69-
for size in max_work_item_sizes:
70-
assert size is not None
71-
72-
73-
def check_get_max_work_group_size(device):
74-
max_work_group_size = device.max_work_group_size
75-
# Special case for FPGA simulator
76-
if device.is_accelerator:
77-
assert max_work_group_size >= 0
78-
else:
79-
assert max_work_group_size > 0
80-
81-
82-
def check_get_max_num_sub_groups(device):
83-
max_num_sub_groups = device.max_num_sub_groups
84-
# Special case for FPGA simulator
85-
if device.is_accelerator or device.is_host:
86-
assert max_num_sub_groups >= 0
87-
else:
88-
assert max_num_sub_groups > 0
89-
90-
91-
def check_has_aspect_host(device):
92-
try:
93-
device.has_aspect_host
94-
except Exception:
95-
pytest.fail("has_aspect_host call failed")
96-
97-
98-
def check_has_aspect_cpu(device):
99-
try:
100-
device.has_aspect_cpu
101-
except Exception:
102-
pytest.fail("has_aspect_cpu call failed")
103-
104-
105-
def check_has_aspect_gpu(device):
106-
try:
107-
device.has_aspect_gpu
108-
except Exception:
109-
pytest.fail("has_aspect_gpu call failed")
110-
111-
112-
def check_has_aspect_accelerator(device):
113-
try:
114-
device.has_aspect_accelerator
115-
except Exception:
116-
pytest.fail("has_aspect_accelerator call failed")
117-
118-
119-
def check_has_aspect_custom(device):
120-
try:
121-
device.has_aspect_custom
122-
except Exception:
123-
pytest.fail("has_aspect_custom call failed")
124-
125-
126-
def check_has_aspect_fp16(device):
127-
try:
128-
device.has_aspect_fp16
129-
except Exception:
130-
pytest.fail("has_aspect_fp16 call failed")
131-
132-
133-
def check_has_aspect_fp64(device):
134-
try:
135-
device.has_aspect_fp64
136-
except Exception:
137-
pytest.fail("has_aspect_fp64 call failed")
138-
139-
140-
def check_has_aspect_int64_base_atomics(device):
141-
try:
142-
device.has_aspect_int64_base_atomics
143-
except Exception:
144-
pytest.fail("has_aspect_int64_base_atomics call failed")
145-
146-
147-
def check_has_aspect_int64_extended_atomics(device):
148-
try:
149-
device.has_aspect_int64_extended_atomics
150-
except Exception:
151-
pytest.fail("has_aspect_int64_extended_atomics call failed")
152-
153-
154-
def check_has_aspect_image(device):
155-
try:
156-
device.has_aspect_image
157-
except Exception:
158-
pytest.fail("has_aspect_image call failed")
159-
160-
161-
def check_has_aspect_online_compiler(device):
162-
try:
163-
device.has_aspect_online_compiler
164-
except Exception:
165-
pytest.fail("has_aspect_online_compiler call failed")
166-
167-
168-
def check_has_aspect_online_linker(device):
169-
try:
170-
device.has_aspect_online_linker
171-
except Exception:
172-
pytest.fail("has_aspect_online_linker call failed")
173-
174-
175-
def check_has_aspect_queue_profiling(device):
176-
try:
177-
device.has_aspect_queue_profiling
178-
except Exception:
179-
pytest.fail("has_aspect_queue_profiling call failed")
180-
181-
182-
def check_has_aspect_usm_device_allocations(device):
183-
try:
184-
device.has_aspect_usm_device_allocations
185-
except Exception:
186-
pytest.fail("has_aspect_usm_device_allocations call failed")
187-
188-
189-
def check_has_aspect_usm_host_allocations(device):
190-
try:
191-
device.has_aspect_usm_host_allocations
192-
except Exception:
193-
pytest.fail("has_aspect_usm_host_allocations call failed")
194-
195-
196-
def check_has_aspect_usm_shared_allocations(device):
197-
try:
198-
device.has_aspect_usm_shared_allocations
199-
except Exception:
200-
pytest.fail("has_aspect_usm_shared_allocations call failed")
201-
202-
203-
def check_has_aspect_usm_restricted_shared_allocations(device):
204-
try:
205-
device.has_aspect_usm_restricted_shared_allocations
206-
except Exception:
207-
pytest.fail("has_aspect_usm_restricted_shared_allocations call failed")
208-
209-
210-
def check_has_aspect_usm_system_allocator(device):
211-
try:
212-
device.has_aspect_usm_system_allocator
213-
except Exception:
214-
pytest.fail("has_aspect_usm_system_allocator call failed")
215-
216-
217-
def check_is_accelerator(device):
218-
try:
219-
device.is_accelerator
220-
except Exception:
221-
pytest.fail("is_accelerator call failed")
222-
223-
224-
def check_is_cpu(device):
225-
try:
226-
device.is_cpu
227-
except Exception:
228-
pytest.fail("is_cpu call failed")
229-
230-
231-
def check_is_gpu(device):
232-
try:
233-
device.is_gpu
234-
except Exception:
235-
pytest.fail("is_gpu call failed")
236-
237-
238-
def check_is_host(device):
239-
try:
240-
device.is_host
241-
except Exception:
242-
pytest.fail("is_hostcall failed")
243-
244-
245-
list_of_checks = [
246-
check_get_max_compute_units,
247-
check_get_max_work_item_dims,
248-
check_get_max_work_item_sizes,
249-
check_get_max_work_group_size,
250-
check_get_max_num_sub_groups,
251-
check_is_accelerator,
252-
check_is_cpu,
253-
check_is_gpu,
254-
check_is_host,
255-
check_has_aspect_host,
256-
check_has_aspect_cpu,
257-
check_has_aspect_gpu,
258-
check_has_aspect_accelerator,
259-
check_has_aspect_custom,
260-
check_has_aspect_fp16,
261-
check_has_aspect_fp64,
262-
check_has_aspect_int64_base_atomics,
263-
check_has_aspect_int64_extended_atomics,
264-
check_has_aspect_image,
265-
check_has_aspect_online_compiler,
266-
check_has_aspect_online_linker,
267-
check_has_aspect_queue_profiling,
268-
check_has_aspect_usm_device_allocations,
269-
check_has_aspect_usm_host_allocations,
270-
check_has_aspect_usm_shared_allocations,
271-
check_has_aspect_usm_restricted_shared_allocations,
272-
check_has_aspect_usm_system_allocator,
273-
]
274-
27540

27641
@pytest.fixture(params=list_of_valid_filter_selectors)
27742
def valid_filter(request):
27843
return request.param
27944

28045

281-
@pytest.fixture(params=list_of_invalid_filter_selectors)
282-
def invalid_filter(request):
283-
return request.param
284-
285-
286-
@pytest.fixture(params=list_of_standard_selectors)
287-
def device_selector(request):
288-
return request.param
289-
290-
291-
@pytest.fixture(params=list_of_checks)
292-
def check(request):
293-
return request.param
294-
295-
296-
def test_standard_selectors(device_selector, check):
297-
"""
298-
Tests if the standard SYCL device_selectors are able to select a device.
299-
"""
300-
try:
301-
device = device_selector()
302-
if device.default_selector_score < 0:
303-
pytest.skip()
304-
ctx = dpctl.SyclContext(device)
305-
devs = ctx.get_devices()
306-
assert len(devs) == 1
307-
check(devs[0])
308-
except ValueError:
309-
pytest.skip()
310-
311-
312-
def test_current_device(check):
313-
"""
314-
Test is the device for the current queue is valid.
315-
"""
316-
try:
317-
q = dpctl.get_current_queue()
318-
except Exception:
319-
pytest.fail("Encountered an exception inside get_current_queue().")
320-
ctx = q.get_sycl_context()
321-
devs = ctx.get_devices()
322-
# add check that device is among devs
323-
check(devs[0])
324-
325-
326-
def test_valid_filter_selectors(valid_filter, check):
46+
def test_ctxt_creation_from_filter(valid_filter):
32747
"""
328-
Tests if we can create a SyclDevice using a supported filter selector
329-
string.
48+
Test SyclContext creation using a filter selector string.
33049
"""
331-
device = None
33250
try:
333-
ctx = dpctl.SyclContext(valid_filter)
334-
device = ctx.get_devices()
51+
dpctl.SyclContext(valid_filter)
33552
except ValueError:
33653
pytest.skip("Failed to create context with supported filter")
337-
check(device[0])
33854

33955

340-
def test_invalid_filter_selectors(invalid_filter):
56+
def test_address_of():
34157
"""
342-
An invalid filter string should always be caught and a
343-
SyclQueueCreationError raised.
58+
Test if the address_of method returns an int value.
34459
"""
345-
with pytest.raises(ValueError):
346-
dpctl.SyclContext(invalid_filter)
60+
ctx = dpctl.SyclContext()
61+
assert ctx.addressof_ref() is not None
62+
assert isinstance(ctx.addressof_ref(), int)
34763

34864

34965
def test_context_not_equals():
66+
"""
67+
Test if context equality fails when devices in different contexts are
68+
compared.
69+
"""
35070
try:
35171
ctx_gpu = dpctl.SyclContext("gpu")
35272
except ValueError:
@@ -358,6 +78,15 @@ def test_context_not_equals():
35878
assert ctx_cpu != ctx_gpu
35979

36080

81+
def test_context_not_equals2():
82+
"""
83+
Test if comparing a SyclContext object to some random Python object is
84+
correctly handled and returns False.
85+
"""
86+
ctx = dpctl.SyclContext()
87+
assert ctx != "some context"
88+
89+
36190
def test_context_equals():
36291
try:
36392
ctx1 = dpctl.SyclContext("gpu")
@@ -367,6 +96,22 @@ def test_context_equals():
36796
assert ctx0 == ctx1
36897

36998

99+
def test_name():
100+
"""
101+
Test if a __name__ method is defined for SyclContext.
102+
"""
103+
ctx = dpctl.SyclContext()
104+
assert ctx.__name__ == "SyclContext"
105+
106+
107+
def test_repr():
108+
"""
109+
Test if a __repr__ method is defined for SyclContext.
110+
"""
111+
ctx = dpctl.SyclContext()
112+
assert ctx.__repr__ is not None
113+
114+
370115
def test_context_can_be_used_in_queue(valid_filter):
371116
try:
372117
ctx = dpctl.SyclContext(valid_filter)

0 commit comments

Comments
 (0)