Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdevice_selection.py
More file actions
Latest commit
134 lines (104 loc) · 4 KB
/
Copy pathdevice_selection.py
File metadata and controls
134 lines (104 loc) · 4 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
# 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.
"""Examples illustrating SYCL device selection features provided by dpctl."""
importdpctl
defcreate_default_device():
"""
Create default SyclDevice using `sycl::default_selector`.
Device created can be influenced by environment variable
ONEAPI_DEVICE_SELECTOR, which determines SYCL devices seen by the
oneAPI DPC++ runtime.
"""
d1=dpctl.SyclDevice()
d2=dpctl.select_default_device()
assertd1==d2
d1.print_device_info()
returnd1
defcreate_gpu_device():
"""
Create a GPU device.
Device created can be influenced by environment variable
ONEAPI_DEVICE_SELECTOR, which determines SYCL devices seen by the
oneAPI DPC++ runtime.
"""
try:
d1=dpctl.SyclDevice("gpu")
d2=dpctl.select_gpu_device()
assertd1==d2
d1.print_device_info()
exceptdpctl.SyclDeviceCreationError:
print("A GPU device is not available on the system")
defcreate_gpu_device_if_present():
"""
Select from union of two selections using default_selector.
If a GPU device is available, it will be selected, if not,
a CPU device will be selected, if available, otherwise an error
will be raised.
Device created can be influenced by environment variable
ONEAPI_DEVICE_SELECTOR, which determines SYCL devices seen by the
oneAPI DPC++ runtime.
"""
d=dpctl.SyclDevice("gpu,cpu")
print("Selected "+ ("GPU"ifd.is_gpuelse"CPU") +" device")
defcustom_select_device():
"""
Programmatically select among available devices.
Device created can be influenced by environment variable
ONEAPI_DEVICE_SELECTOR, which determines SYCL devices seen by the
oneAPI DPC++ runtime.
"""
# select devices that support half-precision computation
devs= [dfordindpctl.get_devices() ifd.has_aspect_fp16]
# choose the device with highest default_selector score
max_score=0
selected_dev=None
fordindevs:
ifd.default_selector_score>max_score:
max_score=d.default_selector_score
selected_dev=d
ifselected_dev:
selected_dev.print_device_info()
else:
print("No device with half-precision support is available.")
returnselected_dev
defcreate_device_with_aspects():
"""
Programmatically select a device based on specific set of aspects.
Demonstrate the usage of :func:`dpctl.select_device_with_aspects()`.
"""
dev=dpctl.select_device_with_aspects(
required_aspects=["fp64", "usm_shared_allocations"]
)
dev.print_device_info()
deflist_devices():
"""Programmatically get a list of the available devices.
The list can be filtered based on backend or device_type.
"""
print("Get a list of all devices:\n")
fordindpctl.get_devices():
d.print_device_info()
print("=======================================\n")
print("Get the list of only OpenCL devices:\n")
fordindpctl.get_devices(backend="opencl"):
d.print_device_info()
print("=======================================\n")
print("Get all OpenCL CPU devices:\n")
fordindpctl.get_devices(backend="opencl", device_type="cpu"):
d.print_device_info()
print("=======================================\n")
if__name__=="__main__":
import_runnerasrunner
runner.run_examples("Device selection examples for dpctl.", globals())