Skip to content

Commit

Permalink
ensure UR will clear context on unloading
Browse files Browse the repository at this point in the history
  • Loading branch information
omarahmed1111 committed Aug 29, 2024
1 parent 05d3ea7 commit 39ffb59
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
5 changes: 5 additions & 0 deletions source/adapters/level_zero/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ if(UR_BUILD_ADAPTER_L0)
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/adapter_lib_init_linux.cpp
)
else()
target_sources(ur_adapter_level_zero
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/adapter_lib_init_windows.cpp
)
endif()

# TODO: fix level_zero adapter conversion warnings
Expand Down
39 changes: 39 additions & 0 deletions source/adapters/level_zero/adapter_lib_init_windows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===--------- adapter_lib_init_windows.cpp - Level Zero Adapter
//------------===//
//
// Copyright (C) 2023 Intel Corporation
//
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
// Exceptions. See LICENSE.TXT
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "adapter.hpp"
#include "ur_level_zero.hpp"

#include <windows.h>

BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpReserved) // reserved
{
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH: {
const auto *platforms = GlobalAdapter->PlatformCache->get_value();
for (const auto &p : *platforms) {
for (auto ctx : p->Contexts) {
urContextRelease(ctx);
}
}
break;
}
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
5 changes: 5 additions & 0 deletions source/adapters/level_zero/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreate(

Context->initialize();
*RetContext = reinterpret_cast<ur_context_handle_t>(Context);
#ifdef _WIN32
std::scoped_lock<ur_shared_mutex> Lock(Platform->ContextsMutex);
Platform->Contexts.push_back(*RetContext);
#else
if (IndirectAccessTrackingEnabled) {
std::scoped_lock<ur_shared_mutex> Lock(Platform->ContextsMutex);
Platform->Contexts.push_back(*RetContext);
}
#endif
} catch (const std::bad_alloc &) {
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
} catch (...) {
Expand Down

0 comments on commit 39ffb59

Please sign in to comment.