Repository navigation
Expand file tree
/
Copy pathtest_sycl_platform.py
More file actions
277 lines (216 loc) · 6.73 KB
/
Copy pathtest_sycl_platform.py
File metadata and controls
277 lines (216 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Data Parallel Control (dpctl)
#
# Copyright 2021 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Defines unit test cases for the SyclPlatform class."""
import sys
import pytest
import dpctl
from dpctl import device_type
from .helper import has_sycl_platforms
list_of_valid_filter_selectors = [
"opencl",
"opencl:gpu",
"opencl:cpu",
"opencl:gpu:0",
"gpu",
"cpu",
"level_zero",
"level_zero:gpu",
"opencl:cpu:0",
"level_zero:gpu:0",
"gpu:0",
"gpu:1",
"1",
]
list_of_invalid_filter_selectors = [
"-1",
"opencl:gpu:-1",
"cuda:cpu:0",
"abc",
]
def check_name(platform):
try:
platform.name
except Exception:
pytest.fail("Encountered an exception inside platform.name.")
def check_vendor(platform):
try:
platform.vendor
except Exception:
pytest.fail("Encountered an exception inside platform.vendor.")
def check_version(platform):
try:
platform.version
except Exception:
pytest.fail("Encountered an exception inside platform.version.")
def check_backend(platform):
try:
platform.backend
except Exception:
pytest.fail("Encountered an exception inside platform.backend.")
def check_print_info(platform):
try:
platform.print_platform_info()
except Exception:
pytest.fail("Encountered an exception inside print_info().")
def check_repr(platform):
r = repr(platform)
assert type(r) is str
assert r != ""
def check_default_context(platform):
if "linux" not in sys.platform:
return
r = platform.default_context
assert type(r) is dpctl.SyclContext
def check_equal_and_hash(platform):
assert platform == platform
if "linux" not in sys.platform:
return
default_ctx = platform.default_context
for d in default_ctx.get_devices():
assert platform == d.sycl_platform
assert hash(platform) == hash(d.sycl_platform)
def check_hash_in_dict(platform):
map = {platform: 0}
assert map[platform] == 0
list_of_checks = [
check_name,
check_vendor,
check_version,
check_backend,
check_print_info,
check_repr,
check_default_context,
check_equal_and_hash,
check_hash_in_dict,
]
@pytest.fixture(params=list_of_valid_filter_selectors)
def valid_filter(request):
return request.param
@pytest.fixture(params=list_of_invalid_filter_selectors)
def invalid_filter(request):
return request.param
@pytest.fixture(params=list_of_checks)
def check(request):
return request.param
def test_platform_creation(valid_filter, check):
"""Tests if we can create a SyclPlatform using a supported filter selector
string.
"""
platform = None
try:
platform = dpctl.SyclPlatform(valid_filter)
except ValueError:
pytest.skip("Failed to create platform with supported filter")
check(platform)
def test_default_platform_creation(check):
platform = None
try:
platform = dpctl.SyclPlatform()
except ValueError:
pytest.skip("Failed to create default platform")
check(platform)
def test_invalid_platform_creation(invalid_filter, check):
"""Tests if we can create a SyclPlatform using a supported filter selector
string.
"""
with pytest.raises(ValueError):
dpctl.SyclPlatform(invalid_filter)
def test_lsplatform():
try:
dpctl.lsplatform()
except Exception:
pytest.fail("Encountered an exception inside lsplatform().")
def test_lsplatform0():
try:
dpctl.lsplatform(0)
except Exception:
pytest.fail("Encountered an exception inside lsplatform().")
def test_lsplatform1():
try:
dpctl.lsplatform(1)
except Exception:
pytest.fail("Encountered an exception inside lsplatform().")
def test_lsplatform2():
try:
dpctl.lsplatform(2)
except Exception:
pytest.fail("Encountered an exception inside lsplatform().")
def test_lsplatform3():
try:
with pytest.warns(UserWarning):
dpctl.lsplatform(3)
except Exception:
pytest.fail("Encountered an exception inside lsplatform().")
def test_get_platforms():
try:
platforms = dpctl.get_platforms()
if platforms:
assert has_sycl_platforms()
except Exception:
pytest.fail("Encountered an exception inside get_platforms().")
def test_platform_get_devices():
platforms = dpctl.get_platforms()
if platforms:
for p in platforms:
assert len(p.get_devices())
else:
pytest.skip("No platforms available")
def _str_device_type_to_enum(dty):
if dty == "accelerator":
return device_type.accelerator
elif dty == "cpu":
return device_type.cpu
elif dty == "gpu":
return device_type.gpu
def test_platform_get_devices_str_device_type():
platforms = dpctl.get_platforms()
dtys = ["accelerator", "all", "cpu", "gpu"]
if platforms:
for p in platforms:
for dty in dtys:
devices = p.get_devices(device_type=dty)
if len(devices):
dty_enum = _str_device_type_to_enum(dty)
assert (d.device_type == dty_enum for d in devices)
def test_platform_get_devices_enum_device_type():
platforms = dpctl.get_platforms()
dtys = [
device_type.accelerator,
device_type.all,
device_type.cpu,
device_type.gpu,
]
if platforms:
for p in platforms:
for dty in dtys:
devices = p.get_devices(device_type=dty)
if len(devices):
assert (d.device_type == dty for d in devices)
def test_platform_get_composite_devices():
platforms = dpctl.get_platforms()
if platforms:
for p in platforms:
composite_devices = p.get_composite_devices()
if not composite_devices:
pass
devices = p.get_devices()
for Cd in composite_devices:
assert Cd.has_aspect_is_composite
component_devices = Cd.component_devices()
# all component devices are root devices
assert all(d in devices for d in component_devices)
else:
pytest.skip("No platforms available")