Skip to content

Commit 965fa3b

Browse files
author
Diptorup Deb
authored
Added an example showing filter string usage. (#725)
- Use try..except in examples to make sure we do not crash on a system where needed drivers are not installed.
1 parent 2a94235 commit 965fa3b

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

examples/python/device_selection.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ def create_gpu_device():
5454
SYCL_DEVICE_FILTER, which determines SYCL devices seen by the
5555
SYCL runtime.
5656
"""
57-
d1 = dpctl.SyclDevice("gpu")
58-
d2 = dpctl.select_gpu_device()
59-
assert d1 == d2
60-
print_device(d1)
61-
return d1
57+
try:
58+
d1 = dpctl.SyclDevice("gpu")
59+
d2 = dpctl.select_gpu_device()
60+
assert d1 == d2
61+
print_device(d1)
62+
except ValueError:
63+
print("A GPU device is not available on the system")
6264

6365

6466
def create_gpu_device_if_present():

examples/python/filter_selection.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2020-2021 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Examples illustrating SYCL device selection using filter strings.
18+
"""
19+
20+
import dpctl
21+
22+
23+
def print_device(d):
24+
"Display information about given device argument."
25+
if type(d) is not dpctl.SyclDevice:
26+
raise ValueError
27+
print("Name: ", d.name)
28+
print("Vendor: ", d.vendor)
29+
print("Driver version: ", d.driver_version)
30+
print("Backend: ", d.backend)
31+
print("Max EU: ", d.max_compute_units)
32+
33+
34+
def select_using_filter():
35+
"""
36+
Demonstrate the usage of a filter string to create a SyclDevice.
37+
38+
"""
39+
try:
40+
d1 = dpctl.SyclDevice("cpu")
41+
print_device(d1)
42+
except ValueError:
43+
print("A CPU type device is not available on the system")
44+
45+
try:
46+
d1 = dpctl.SyclDevice("opencl:cpu:0")
47+
print_device(d1)
48+
except ValueError:
49+
print("An OpenCL CPU driver needs to be installed on the system")
50+
51+
d1 = dpctl.SyclDevice("0")
52+
print_device(d1)
53+
54+
try:
55+
d1 = dpctl.SyclDevice("gpu")
56+
print_device(d1)
57+
except ValueError:
58+
print("A GPU type device is not available on the system")
59+
60+
61+
if __name__ == "__main__":
62+
import _runner as runner
63+
64+
runner.run_examples("Filter selection examples for dpctl.", globals())

0 commit comments

Comments
 (0)