27
27
__SYCL_INLINE_NAMESPACE (cl) {
28
28
namespace sycl {
29
29
30
- // Utility function to check if device is of the preferred backend.
31
- // Currently preference is given to the level_zero backend.
32
- static bool isDeviceOfPreferredSyclBe (const device &Device) {
30
+ // SYCL_DEVICE_FILTER doesn't need to be considered in the device preferences
31
+ // as it filters the device list returned by device::get_devices itself, so
32
+ // only matching devices will be scored.
33
+ static int getDevicePreference (const device &Device) {
34
+ int Score = 0 ;
35
+
36
+ // No preferences for host devices.
33
37
if (Device.is_host ())
34
- return false ;
38
+ return Score;
39
+
40
+ // Strongly prefer devices with available images.
41
+ auto &program_manager = cl::sycl::detail::ProgramManager::getInstance ();
42
+ if (program_manager.hasCompatibleImage (Device))
43
+ Score += 1000 ;
44
+
45
+ // Prefer level_zero backend devices.
46
+ if (detail::getSyclObjImpl (Device)->getPlugin ().getBackend () ==
47
+ backend::ext_oneapi_level_zero)
48
+ Score += 50 ;
35
49
36
- return detail::getSyclObjImpl (Device)->getPlugin ().getBackend () ==
37
- backend::ext_oneapi_level_zero;
50
+ return Score;
38
51
}
39
52
40
53
device device_selector::select_device () const {
@@ -64,11 +77,12 @@ device device_selector::select_device() const {
64
77
65
78
// SYCL spec says: "If more than one device receives the high score then
66
79
// one of those tied devices will be returned, but which of the devices
67
- // from the tied set is to be returned is not defined". Here we give a
68
- // preference to the device of the preferred BE.
69
- //
80
+ // from the tied set is to be returned is not defined". So use the device
81
+ // preference score to resolve ties, this is necessary for custom_selectors
82
+ // that may not already include device preference in their scoring.
70
83
if ((score < dev_score) ||
71
- (score == dev_score && isDeviceOfPreferredSyclBe (dev))) {
84
+ ((score == dev_score) &&
85
+ (getDevicePreference (*res) < getDevicePreference (dev)))) {
72
86
res = &dev;
73
87
score = dev_score;
74
88
}
@@ -97,25 +111,13 @@ device device_selector::select_device() const {
97
111
// / 1. GPU
98
112
// / 2. CPU
99
113
// / 3. Host
114
+ // / 4. Accelerator
100
115
int default_selector::operator ()(const device &dev) const {
101
-
102
- int Score = REJECT_DEVICE_SCORE;
103
-
104
- // Give preference to device of SYCL BE.
105
- if (isDeviceOfPreferredSyclBe (dev))
106
- Score = 50 ;
107
-
108
- // If SYCL_DEVICE_FILTER is set, filter device gets a high point.
109
- // All unmatched devices should never be selected.
110
- detail::device_filter_list *FilterList =
111
- detail::SYCLConfig<detail::SYCL_DEVICE_FILTER>::get ();
112
- // device::get_devices returns filtered list of devices.
113
- // Keep 1000 for default score when filters were applied.
114
- if (FilterList)
115
- Score = 1000 ;
116
+ // The default selector doesn't reject any devices.
117
+ int Score = 0 ;
116
118
117
119
if (dev.get_info <info::device::device_type>() == detail::get_forced_type ())
118
- Score += 1000 ;
120
+ Score += 2000 ;
119
121
120
122
if (dev.is_gpu ())
121
123
Score += 500 ;
@@ -132,18 +134,18 @@ int default_selector::operator()(const device &dev) const {
132
134
if (dev.is_accelerator ())
133
135
Score += 75 ;
134
136
137
+ // Add preference score.
138
+ Score += getDevicePreference (dev);
139
+
135
140
return Score;
136
141
}
137
142
138
143
int gpu_selector::operator ()(const device &dev) const {
139
144
int Score = REJECT_DEVICE_SCORE;
140
145
141
146
if (dev.is_gpu ()) {
142
- // device::get_devices returns filtered list of devices.
143
147
Score = 1000 ;
144
- // Give preference to device of SYCL BE.
145
- if (isDeviceOfPreferredSyclBe (dev))
146
- Score += 50 ;
148
+ Score += getDevicePreference (dev);
147
149
}
148
150
return Score;
149
151
}
@@ -152,12 +154,8 @@ int cpu_selector::operator()(const device &dev) const {
152
154
int Score = REJECT_DEVICE_SCORE;
153
155
154
156
if (dev.is_cpu ()) {
155
- // device::get_devices returns filtered list of devices.
156
157
Score = 1000 ;
157
-
158
- // Give preference to device of SYCL BE.
159
- if (isDeviceOfPreferredSyclBe (dev))
160
- Score += 50 ;
158
+ Score += getDevicePreference (dev);
161
159
}
162
160
return Score;
163
161
}
@@ -166,12 +164,8 @@ int accelerator_selector::operator()(const device &dev) const {
166
164
int Score = REJECT_DEVICE_SCORE;
167
165
168
166
if (dev.is_accelerator ()) {
169
- // device::get_devices returns filtered list of devices.
170
167
Score = 1000 ;
171
-
172
- // Give preference to device of SYCL BE.
173
- if (isDeviceOfPreferredSyclBe (dev))
174
- Score += 50 ;
168
+ Score += getDevicePreference (dev);
175
169
}
176
170
return Score;
177
171
}
@@ -181,9 +175,7 @@ int host_selector::operator()(const device &dev) const {
181
175
182
176
if (dev.is_host ()) {
183
177
Score = 1000 ;
184
- // Give preference to device of SYCL BE.
185
- if (isDeviceOfPreferredSyclBe (dev))
186
- Score += 50 ;
178
+ Score += getDevicePreference (dev);
187
179
}
188
180
return Score;
189
181
}
0 commit comments