Skip to content

[UR] Fix creation of context with parent device and its sub-devices #19223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions unified-runtime/source/adapters/level_zero/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <stdarg.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include "adapters/level_zero/platform.hpp"
Expand Down Expand Up @@ -247,12 +248,16 @@ struct ur_device_handle_t_ : ur_object {
inline std::vector<ur_device_handle_t>
CollectDevicesAndSubDevices(const std::vector<ur_device_handle_t> &Devices) {
std::vector<ur_device_handle_t> DevicesAndSubDevices;
std::unordered_set<ur_device_handle_t> Seen;
std::function<void(const std::vector<ur_device_handle_t> &)>
CollectDevicesAndSubDevicesRec =
[&](const std::vector<ur_device_handle_t> &Devices) {
for (auto &Device : Devices) {
DevicesAndSubDevices.push_back(Device);
CollectDevicesAndSubDevicesRec(Device->SubDevices);
// Only add device if has not been seen before.
if (Seen.insert(Device).second) {
DevicesAndSubDevices.push_back(Device);
CollectDevicesAndSubDevicesRec(Device->SubDevices);
}
}
};
CollectDevicesAndSubDevicesRec(Devices);
Expand Down
51 changes: 51 additions & 0 deletions unified-runtime/test/conformance/context/urContextCreate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,57 @@ TEST_P(urContextCreateTest, InvalidEnumeration) {
urContextCreate(1, &device, &properties, context.ptr()));
}

TEST_P(urContextCreateTest, SuccessParentAndSubDevices) {
if (!uur::hasDevicePartitionSupport(device,
UR_DEVICE_PARTITION_BY_AFFINITY_DOMAIN)) {
GTEST_SKIP() << "Device \'" << device
<< "\' does not support partitioning by affinity domain.\n";
}

ur_device_affinity_domain_flags_t flag = UR_DEVICE_AFFINITY_DOMAIN_FLAG_NUMA;
ur_device_affinity_domain_flags_t supported_flags{0};
ASSERT_SUCCESS(
uur::GetDevicePartitionAffinityDomainFlags(device, supported_flags));
if (!(flag & supported_flags)) {
GTEST_SKIP() << static_cast<ur_device_affinity_domain_flag_t>(flag)
<< " is not supported by the device: \'" << device << "\'.\n";
}

ur_device_partition_property_t prop =
uur::makePartitionByAffinityDomain(flag);

ur_device_partition_properties_t properties{
UR_STRUCTURE_TYPE_DEVICE_PARTITION_PROPERTIES,
nullptr,
&prop,
1,
};

// Get the number of devices that will be created
uint32_t n_devices = 0;
ASSERT_SUCCESS(
urDevicePartition(device, &properties, 0, nullptr, &n_devices));
ASSERT_NE(n_devices, 0);

std::vector<ur_device_handle_t> sub_devices(n_devices);
ASSERT_SUCCESS(urDevicePartition(device, &properties,
static_cast<uint32_t>(sub_devices.size()),
sub_devices.data(), nullptr));

std::vector<ur_device_handle_t> all_devices;
all_devices.push_back(device);
all_devices.insert(all_devices.end(), sub_devices.begin(), sub_devices.end());
uur::raii::Context context = nullptr;
ASSERT_SUCCESS(urContextCreate(static_cast<uint32_t>(all_devices.size()),
all_devices.data(), nullptr, context.ptr()));
ASSERT_NE(nullptr, context);

for (auto sub_device : sub_devices) {
ASSERT_NE(sub_device, nullptr);
ASSERT_SUCCESS(urDeviceRelease(sub_device));
}
}

using urContextCreateMultiDeviceTest = uur::urAllDevicesTest;
UUR_INSTANTIATE_PLATFORM_TEST_SUITE(urContextCreateMultiDeviceTest);

Expand Down
Loading