Skip to content

Commit

Permalink
Merge pull request #1419 from nrspruit/main_l0_adapter_release_lib
Browse files Browse the repository at this point in the history
[L0] Create/Destroy Adapter Handle during lib init
  • Loading branch information
kbenzie committed Mar 8, 2024
1 parent 8e4d0e1 commit 668fd78
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 27 deletions.
9 changes: 8 additions & 1 deletion source/adapters/level_zero/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2022 Intel Corporation
# Copyright (C) 2022-2024 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
Expand Down Expand Up @@ -119,6 +119,13 @@ add_ur_adapter(${TARGET_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/../../ur/ur.cpp
)

if(NOT WIN32)
target_sources(ur_adapter_level_zero
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/adapter_lib_init_linux.cpp
)
endif()

# TODO: fix level_zero adapter conversion warnings
target_compile_options(${TARGET_NAME} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/wd4805 /wd4244>
Expand Down
72 changes: 53 additions & 19 deletions source/adapters/level_zero/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
#include "adapter.hpp"
#include "ur_level_zero.hpp"

// Due to multiple DLLMain definitions with SYCL, Global Adapter is init at
// variable creation.
#if defined(_WIN32)
ur_adapter_handle_t_ *GlobalAdapter = new ur_adapter_handle_t_();
#else
ur_adapter_handle_t_ *GlobalAdapter;
#endif

UR_APIEXPORT ur_result_t UR_APICALL
urInit(ur_device_init_flags_t
DeviceFlags, ///< [in] device initialization flags.
Expand Down Expand Up @@ -48,8 +56,7 @@ ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
ur_result_t adapterStateInit() { return UR_RESULT_SUCCESS; }

ur_adapter_handle_t_::ur_adapter_handle_t_() {

Adapter.PlatformCache.Compute = [](Result<PlatformVec> &result) {
PlatformCache.Compute = [](Result<PlatformVec> &result) {
static std::once_flag ZeCallCountInitialized;
try {
std::call_once(ZeCallCountInitialized, []() {
Expand All @@ -63,7 +70,7 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
}

// initialize level zero only once.
if (Adapter.ZeResult == std::nullopt) {
if (GlobalAdapter->ZeResult == std::nullopt) {
// Setting these environment variables before running zeInit will enable
// the validation layer in the Level Zero loader.
if (UrL0Debug & UR_L0_DEBUG_VALIDATION) {
Expand All @@ -82,20 +89,21 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
// We must only initialize the driver once, even if urPlatformGet() is
// called multiple times. Declaring the return value as "static" ensures
// it's only called once.
Adapter.ZeResult = ZE_CALL_NOCHECK(zeInit, (ZE_INIT_FLAG_GPU_ONLY));
GlobalAdapter->ZeResult =
ZE_CALL_NOCHECK(zeInit, (ZE_INIT_FLAG_GPU_ONLY));
}
assert(Adapter.ZeResult !=
assert(GlobalAdapter->ZeResult !=
std::nullopt); // verify that level-zero is initialized
PlatformVec platforms;

// Absorb the ZE_RESULT_ERROR_UNINITIALIZED and just return 0 Platforms.
if (*Adapter.ZeResult == ZE_RESULT_ERROR_UNINITIALIZED) {
if (*GlobalAdapter->ZeResult == ZE_RESULT_ERROR_UNINITIALIZED) {
result = std::move(platforms);
return;
}
if (*Adapter.ZeResult != ZE_RESULT_SUCCESS) {
if (*GlobalAdapter->ZeResult != ZE_RESULT_SUCCESS) {
urPrint("zeInit: Level Zero initialization failure\n");
result = ze2urResult(*Adapter.ZeResult);
result = ze2urResult(*GlobalAdapter->ZeResult);
return;
}

Expand All @@ -108,7 +116,11 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
};
}

ur_adapter_handle_t_ Adapter{};
void globalAdapterOnDemandCleanup() {
if (GlobalAdapter) {
delete GlobalAdapter;
}
}

ur_result_t adapterStateTeardown() {
bool LeakFound = false;
Expand Down Expand Up @@ -195,6 +207,11 @@ ur_result_t adapterStateTeardown() {
}
if (LeakFound)
return UR_RESULT_ERROR_INVALID_MEM_OBJECT;
// Due to multiple DLLMain definitions with SYCL, register to cleanup the
// Global Adapter after refcnt is 0
#if defined(_WIN32)
std::atexit(globalAdapterOnDemandCleanup);
#endif

return UR_RESULT_SUCCESS;
}
Expand All @@ -221,11 +238,23 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(
///< adapters available.
) {
if (NumEntries > 0 && Adapters) {
std::lock_guard<std::mutex> Lock{Adapter.Mutex};
if (Adapter.RefCount++ == 0) {
adapterStateInit();
if (GlobalAdapter) {
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
if (GlobalAdapter->RefCount++ == 0) {
adapterStateInit();
}
} else {
// If the GetAdapter is called after the Library began or was torndown,
// then temporarily create a new Adapter handle and register a new
// cleanup.
GlobalAdapter = new ur_adapter_handle_t_();
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
if (GlobalAdapter->RefCount++ == 0) {
adapterStateInit();
}
std::atexit(globalAdapterOnDemandCleanup);
}
*Adapters = &Adapter;
*Adapters = GlobalAdapter;
}

if (NumAdapters) {
Expand All @@ -236,17 +265,22 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(
}

UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) {
std::lock_guard<std::mutex> Lock{Adapter.Mutex};
if (--Adapter.RefCount == 0) {
return adapterStateTeardown();
// Check first if the Adapter pointer is valid
if (GlobalAdapter) {
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
if (--GlobalAdapter->RefCount == 0) {
return adapterStateTeardown();
}
}

return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {
std::lock_guard<std::mutex> Lock{Adapter.Mutex};
Adapter.RefCount++;
if (GlobalAdapter) {
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
GlobalAdapter->RefCount++;
}

return UR_RESULT_SUCCESS;
}
Expand Down Expand Up @@ -275,7 +309,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t,
case UR_ADAPTER_INFO_BACKEND:
return ReturnValue(UR_ADAPTER_BACKEND_LEVEL_ZERO);
case UR_ADAPTER_INFO_REFERENCE_COUNT:
return ReturnValue(Adapter.RefCount.load());
return ReturnValue(GlobalAdapter->RefCount.load());
default:
return UR_RESULT_ERROR_INVALID_ENUMERATION;
}
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/level_zero/adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ struct ur_adapter_handle_t_ {
ZeCache<Result<PlatformVec>> PlatformCache;
};

extern ur_adapter_handle_t_ Adapter;
extern ur_adapter_handle_t_ *GlobalAdapter;
25 changes: 25 additions & 0 deletions source/adapters/level_zero/adapter_lib_init_linux.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===--------- adapter_lib_init_linux.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"

void __attribute__((constructor)) createAdapterHandle() {
if (!GlobalAdapter) {
GlobalAdapter = new ur_adapter_handle_t_();
}
}

void __attribute__((destructor)) deleteAdapterHandle() {
if (GlobalAdapter) {
delete GlobalAdapter;
GlobalAdapter = nullptr;
}
}
4 changes: 2 additions & 2 deletions source/adapters/level_zero/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
// a valid Level Zero device.

ur_device_handle_t Dev = nullptr;
if (const auto *platforms = Adapter.PlatformCache->get_value()) {
if (const auto *platforms = GlobalAdapter->PlatformCache->get_value()) {
for (const auto &p : *platforms) {
Dev = p->getDeviceFromNativeHandle(ZeDevice);
if (Dev) {
Expand All @@ -1332,7 +1332,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle(
}
}
} else {
return Adapter.PlatformCache->get_error();
return GlobalAdapter->PlatformCache->get_error();
}

if (Dev == nullptr)
Expand Down
6 changes: 3 additions & 3 deletions source/adapters/level_zero/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGet(
) {
// Platform handles are cached for reuse. This is to ensure consistent
// handle pointers across invocations and to improve retrieval performance.
if (const auto *cached_platforms = Adapter.PlatformCache->get_value();
if (const auto *cached_platforms = GlobalAdapter->PlatformCache->get_value();
cached_platforms) {
uint32_t nplatforms = (uint32_t)cached_platforms->size();
if (NumPlatforms) {
Expand All @@ -41,7 +41,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGet(
}
}
} else {
return Adapter.PlatformCache->get_error();
return GlobalAdapter->PlatformCache->get_error();
}

return UR_RESULT_SUCCESS;
Expand Down Expand Up @@ -133,7 +133,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle(
auto ZeDriver = ur_cast<ze_driver_handle_t>(NativePlatform);

uint32_t NumPlatforms = 0;
ur_adapter_handle_t AdapterHandle = &Adapter;
ur_adapter_handle_t AdapterHandle = GlobalAdapter;
UR_CALL(urPlatformGet(&AdapterHandle, 1, 0, nullptr, &NumPlatforms));

if (NumPlatforms) {
Expand Down
2 changes: 1 addition & 1 deletion source/adapters/level_zero/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle(
// Maybe this is not completely correct.
uint32_t NumEntries = 1;
ur_platform_handle_t Platform{};
ur_adapter_handle_t AdapterHandle = &Adapter;
ur_adapter_handle_t AdapterHandle = GlobalAdapter;
UR_CALL(urPlatformGet(&AdapterHandle, 1, NumEntries, &Platform, nullptr));

ur_device_handle_t UrDevice = Device;
Expand Down

0 comments on commit 668fd78

Please sign in to comment.