Skip to content

Commit 6aefd63

Browse files
authored
[SYCL] Improve ODS negative filter implementation (#7453)
The negative filter implementation for ONEAPI_DEVICE_SELECTOR uses a map to keep track of blacklisted devices. The keys used by this map were originally device addresses in a vector container which are not very robust because vectors can potentially move their data to other locations and the device addresses could change thus invalidating the blacklist map. Even though in the source code the resizing of the vector only happens after we are done with the blacklist, you never know what tricks the compiler might pull on us. We use device numbers instead which are unique for each device in a platform and do not change during the function execution.
1 parent 2b2beda commit 6aefd63

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

sycl/source/detail/platform_impl.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ static std::vector<int> filterDeviceFilter(std::vector<RT::PiDevice> &PiDevices,
185185
// used in the ONEAPI_DEVICE_SELECTOR implemenation. It cannot be placed
186186
// in the if statement above because it will then be out of scope in the rest
187187
// of the function
188-
std::map<RT::PiDevice *, bool> Blacklist;
188+
std::map<int, bool> Blacklist;
189+
// original indices keeps track of the device numbers of the chosen
190+
// devices and is whats returned by the function
189191
std::vector<int> original_indices;
190192

191193
std::vector<plugin> &Plugins = RT::initialize();
@@ -223,14 +225,14 @@ static std::vector<int> filterDeviceFilter(std::vector<RT::PiDevice> &PiDevices,
223225
// Last, match the device_num entry
224226
if (!Filter.DeviceNum || DeviceNum == Filter.DeviceNum.value()) {
225227
if constexpr (is_ods_target) { // dealing with ODS filters
226-
if (!Blacklist[&Device]) { // ensure it is not blacklisted
228+
if (!Blacklist[DeviceNum]) { // ensure it is not blacklisted
227229
if (!Filter.IsNegativeTarget) { // is filter positive?
228230
PiDevices[InsertIDx++] = Device;
229231
original_indices.push_back(DeviceNum);
230232
} else {
231233
// Filter is negative and the device matches the filter so
232234
// blacklist the device.
233-
Blacklist[&Device] = true;
235+
Blacklist[DeviceNum] = true;
234236
}
235237
}
236238
} else { // dealing with SYCL_DEVICE_FILTER
@@ -243,14 +245,14 @@ static std::vector<int> filterDeviceFilter(std::vector<RT::PiDevice> &PiDevices,
243245
} else if (FilterDevType == DeviceType) {
244246
if (!Filter.DeviceNum || DeviceNum == Filter.DeviceNum.value()) {
245247
if constexpr (is_ods_target) {
246-
if (!Blacklist[&Device]) {
248+
if (!Blacklist[DeviceNum]) {
247249
if (!Filter.IsNegativeTarget) {
248250
PiDevices[InsertIDx++] = Device;
249251
original_indices.push_back(DeviceNum);
250252
} else {
251253
// Filter is negative and the device matches the filter so
252254
// blacklist the device.
253-
Blacklist[&Device] = true;
255+
Blacklist[DeviceNum] = true;
254256
}
255257
}
256258
} else {

0 commit comments

Comments
 (0)