Skip to content

Commit bacad63

Browse files
[UR] Fix usm pools creation failure at context initialization (#20054)
This is a cherry-pick of #19921 Context creation was failing when sub-sub-devices were exposed, because the Level Zero adapter attempted to add multiple USM pools with identical descriptors. This occurred since, for the L0 backend, sub-sub-devices and their parent sub-devices share the same Level Zero device handle and should use the same USM pool. This PR resolves the issue by ensuring only devices with unique Level Zero handles are collected for USM pool creation, preventing duplicate pools. Additionally, this PR fixes an issue with an uninitialized `ur_device_partition_property_t` variable passed to UR. Previously, this could result in an unexpected value for `value.affinity_domain` when the property type was `UR_DEVICE_PARTITION_BY_CSLICE` resulting on error from adapter. Patch-by: Artur Gainullin <[email protected]>
1 parent af71862 commit bacad63

File tree

5 files changed

+56
-10
lines changed

5 files changed

+56
-10
lines changed

sycl/source/detail/device_impl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ std::vector<device> device_impl::create_sub_devices(
258258
affinityDomainToString(AffinityDomain) + ".");
259259
}
260260

261-
ur_device_partition_property_t Prop;
261+
ur_device_partition_property_t Prop{};
262262
Prop.type = UR_DEVICE_PARTITION_BY_AFFINITY_DOMAIN;
263263
Prop.value.affinity_domain =
264264
static_cast<ur_device_affinity_domain_flags_t>(AffinityDomain);
@@ -285,9 +285,8 @@ std::vector<device> device_impl::create_sub_devices() const {
285285
"sycl::info::partition_property::ext_intel_partition_by_cslice.");
286286
}
287287

288-
ur_device_partition_property_t Prop;
288+
ur_device_partition_property_t Prop{};
289289
Prop.type = UR_DEVICE_PARTITION_BY_CSLICE;
290-
291290
ur_device_partition_properties_t Properties{};
292291
Properties.stype = UR_STRUCTURE_TYPE_DEVICE_PARTITION_PROPERTIES;
293292
Properties.pProperties = &Prop;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// REQUIRES: arch-intel_gpu_pvc, level_zero
2+
// UNSUPPORTED: gpu-intel-pvc-1T
3+
// UNSUPPORTED-TRACKER: GSD-9121
4+
5+
// DEFINE: %{setup_env} = env ZE_FLAT_DEVICE_HIERARCHY=COMPOSITE ZE_AFFINITY_MASK=0 ZEX_NUMBER_OF_CCS=0:4
6+
// RUN: %{build} -o %t.out
7+
// RUN: %{setup_env} %{run} %t.out
8+
9+
// Check that context can be created successfully when sub-sub-devices are
10+
// exposed.
11+
#include <iostream>
12+
#include <sycl/detail/core.hpp>
13+
#include <vector>
14+
15+
using namespace sycl;
16+
17+
int main() {
18+
std::cout << "[info] start context_create_sub_sub_device test" << std::endl;
19+
device d;
20+
std::vector<device> subsubdevices;
21+
22+
auto subdevices = d.create_sub_devices<
23+
info::partition_property::partition_by_affinity_domain>(
24+
info::partition_affinity_domain::next_partitionable);
25+
std::cout << "[info] sub device size = " << subdevices.size() << std::endl;
26+
27+
for (auto &subdev : subdevices) {
28+
subsubdevices = subdev.create_sub_devices<
29+
info::partition_property::ext_intel_partition_by_cslice>();
30+
31+
std::cout << "[info] sub-sub device size = " << subsubdevices.size()
32+
<< std::endl;
33+
}
34+
35+
// Create contexts
36+
context ctx1(d);
37+
context ctx2(subdevices);
38+
context ctx3(subsubdevices);
39+
40+
std::cout << "[info] contexts created successfully" << std::endl;
41+
return 0;
42+
}

unified-runtime/source/adapters/level_zero/device.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,20 @@ struct ur_device_handle_t_ : ur_object {
248248
ur::RefCount RefCount;
249249
};
250250

251-
inline std::vector<ur_device_handle_t>
252-
CollectDevicesAndSubDevices(const std::vector<ur_device_handle_t> &Devices) {
251+
// Collects a flat vector of unique devices for USM memory pool creation.
252+
// Traverses the input devices and their sub-devices, ensuring each Level Zero
253+
// device handle appears only once in the result.
254+
inline std::vector<ur_device_handle_t> CollectDevicesForUsmPoolCreation(
255+
const std::vector<ur_device_handle_t> &Devices) {
253256
std::vector<ur_device_handle_t> DevicesAndSubDevices;
254-
std::unordered_set<ur_device_handle_t> Seen;
257+
std::unordered_set<ze_device_handle_t> Seen;
258+
255259
std::function<void(const std::vector<ur_device_handle_t> &)>
256260
CollectDevicesAndSubDevicesRec =
257261
[&](const std::vector<ur_device_handle_t> &Devices) {
258262
for (auto &Device : Devices) {
259-
// Only add device if has not been seen before.
260-
if (Seen.insert(Device).second) {
263+
// Only add device if ZeDevice has not been seen before.
264+
if (Seen.insert(Device->ZeDevice).second) {
261265
DevicesAndSubDevices.push_back(Device);
262266
CollectDevicesAndSubDevicesRec(Device->SubDevices);
263267
}

unified-runtime/source/adapters/level_zero/usm.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,8 @@ ur_usm_pool_handle_t_::ur_usm_pool_handle_t_(ur_context_handle_t Context,
10611061
}
10621062
}
10631063

1064-
auto DevicesAndSubDevices = CollectDevicesAndSubDevices(Context->Devices);
1064+
auto DevicesAndSubDevices =
1065+
CollectDevicesForUsmPoolCreation(Context->Devices);
10651066
auto Descriptors = usm::pool_descriptor::createFromDevices(
10661067
this, Context, DevicesAndSubDevices);
10671068
for (auto &Desc : Descriptors) {

unified-runtime/source/adapters/level_zero/v2/usm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ ur_usm_pool_handle_t_::ur_usm_pool_handle_t_(ur_context_handle_t hContext,
168168
}
169169

170170
auto devicesAndSubDevices =
171-
CollectDevicesAndSubDevices(hContext->getDevices());
171+
CollectDevicesForUsmPoolCreation(hContext->getDevices());
172172
auto descriptors = usm::pool_descriptor::createFromDevices(
173173
this, hContext, devicesAndSubDevices);
174174
for (auto &desc : descriptors) {

0 commit comments

Comments
 (0)