Skip to content

[SYCL] Keep platform_impl's device_impls alive until shutdown #18251

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
Apr 30, 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: 9 additions & 0 deletions sycl/source/detail/global_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ std::vector<std::shared_ptr<platform_impl>> &GlobalHandler::getPlatformCache() {
return PlatformCache;
}

void GlobalHandler::clearPlatforms() {
if (!MPlatformCache.Inst)
return;
for (auto &PltSmartPtr : *MPlatformCache.Inst)
PltSmartPtr->MDevices.clear();
MPlatformCache.Inst->clear();
}

std::mutex &GlobalHandler::getPlatformMapMutex() {
static std::mutex &PlatformMapMutex = getOrCreate(MPlatformMapMutex);
return PlatformMapMutex;
Expand Down Expand Up @@ -366,6 +374,7 @@ void shutdown_late() {
#endif

// First, release resources, that may access adapters.
Handler->clearPlatforms(); // includes dropping platforms' devices ownership.
Handler->MPlatformCache.Inst.reset(nullptr);
Handler->MScheduler.Inst.reset(nullptr);
Handler->MProgramManager.Inst.reset(nullptr);
Expand Down
2 changes: 2 additions & 0 deletions sycl/source/detail/global_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class GlobalHandler {
Sync &getSync();
std::vector<std::shared_ptr<platform_impl>> &getPlatformCache();

void clearPlatforms();

std::unordered_map<platform_impl *, ContextImplPtr> &
getPlatformToDefaultContextCache();

Expand Down
10 changes: 4 additions & 6 deletions sycl/source/detail/platform_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ platform_impl::getOrMakeDeviceImpl(ur_device_handle_t UrDevice) {
// Otherwise make the impl
Result = std::make_shared<device_impl>(UrDevice, *this,
device_impl::private_tag{});
MDeviceCache.emplace_back(Result);
MDevices.emplace_back(Result);

return Result;
}
Expand Down Expand Up @@ -637,11 +637,9 @@ bool platform_impl::has(aspect Aspect) const {

std::shared_ptr<device_impl>
platform_impl::getDeviceImplHelper(ur_device_handle_t UrDevice) {
for (const std::weak_ptr<device_impl> &DeviceWP : MDeviceCache) {
if (std::shared_ptr<device_impl> Device = DeviceWP.lock()) {
if (Device->getHandleRef() == UrDevice)
return Device;
}
for (const std::shared_ptr<device_impl> &Device : MDevices) {
if (Device->getHandleRef() == UrDevice)
return Device;
}
return nullptr;
}
Expand Down
3 changes: 2 additions & 1 deletion sycl/source/detail/platform_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ class platform_impl : public std::enable_shared_from_this<platform_impl> {

AdapterPtr MAdapter;

std::vector<std::weak_ptr<device_impl>> MDeviceCache;
std::vector<std::shared_ptr<device_impl>> MDevices;
friend class GlobalHandler;
std::mutex MDeviceMapMutex;
};

Expand Down
2 changes: 1 addition & 1 deletion sycl/unittests/context_device/DeviceRefCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ static ur_result_t redefinedDeviceReleaseAfter(void *) {
TEST(DevRefCounter, DevRefCounter) {
{
sycl::unittest::UrMock<> Mock;
sycl::platform Plt = sycl::platform();

mock::getCallbacks().set_after_callback("urDeviceGet",
&redefinedDevicesGetAfter);
mock::getCallbacks().set_after_callback("urDeviceRetain",
&redefinedDeviceRetainAfter);
mock::getCallbacks().set_after_callback("urDeviceRelease",
&redefinedDeviceReleaseAfter);
sycl::platform Plt = sycl::platform();

Plt.get_devices();
}
Expand Down
6 changes: 5 additions & 1 deletion sycl/unittests/helpers/UrMock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,11 @@ template <sycl::backend Backend = backend::opencl> class UrMock {
// clear platform cache in case subsequent tests want a different backend,
// this forces platforms to be reconstructed (and thus queries about UR
// backend info to be called again)
detail::GlobalHandler::instance().getPlatformCache().clear();
//
// This also erases each platform's devices (normally done in the library
// shutdown) so that platforms/devices' lifetimes could work in unittests
// scenario.
detail::GlobalHandler::instance().clearPlatforms();
mock::getCallbacks().resetCallbacks();
}

Expand Down