Skip to content

Commit

Permalink
Merge pull request #1321 from pbalcer/adapter-compute-constructor
Browse files Browse the repository at this point in the history
[L0] move adapter init into its constructor from urAdapterGet
  • Loading branch information
aarongreig committed Feb 8, 2024
2 parents 3be8f20 + 90498ec commit 9babc4d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
69 changes: 35 additions & 34 deletions source/adapters/level_zero/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include "adapter.hpp"
#include "ur_level_zero.hpp"

ur_adapter_handle_t_ Adapter{};

ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
uint32_t ZeDriverCount = 0;
ZE2UR_CALL(zeDriverGet, (&ZeDriverCount, nullptr));
Expand All @@ -36,43 +34,45 @@ ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
return exceptionToResult(std::current_exception());
}

ur_result_t adapterStateInit() {
static std::once_flag ZeCallCountInitialized;
try {
std::call_once(ZeCallCountInitialized, []() {
if (UrL0LeaksDebug) {
ZeCallCount = new std::map<std::string, int>;
}
});
} catch (const std::bad_alloc &) {
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
} catch (...) {
return UR_RESULT_ERROR_UNKNOWN;
}
ur_result_t adapterStateInit() { return UR_RESULT_SUCCESS; }

// initialize level zero only once.
if (Adapter.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) {
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
setEnvVar("ZE_ENABLE_PARAMETER_VALIDATION", "1");
}
ur_adapter_handle_t_::ur_adapter_handle_t_() {

if (getenv("SYCL_ENABLE_PCI") != nullptr) {
urPrint("WARNING: SYCL_ENABLE_PCI is deprecated and no longer needed.\n");
Adapter.PlatformCache.Compute = [](Result<PlatformVec> &result) {
static std::once_flag ZeCallCountInitialized;
try {
std::call_once(ZeCallCountInitialized, []() {
if (UrL0LeaksDebug) {
ZeCallCount = new std::map<std::string, int>;
}
});
} catch (...) {
result = exceptionToResult(std::current_exception());
return;
}

// TODO: We can still safely recover if something goes wrong during the
// init. Implement handling segfault using sigaction.
// initialize level zero only once.
if (Adapter.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) {
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
setEnvVar("ZE_ENABLE_PARAMETER_VALIDATION", "1");
}

// 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));
}
if (getenv("SYCL_ENABLE_PCI") != nullptr) {
urPrint(
"WARNING: SYCL_ENABLE_PCI is deprecated and no longer needed.\n");
}

Adapter.PlatformCache.Compute = [](Result<PlatformVec> &result) {
// TODO: We can still safely recover if something goes wrong during the
// init. Implement handling segfault using sigaction.

// 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));
}
assert(Adapter.ZeResult !=
std::nullopt); // verify that level-zero is initialized
PlatformVec platforms;
Expand All @@ -95,9 +95,10 @@ ur_result_t adapterStateInit() {
result = err;
}
};
return UR_RESULT_SUCCESS;
}

ur_adapter_handle_t_ Adapter{};

ur_result_t adapterStateTeardown() {
bool LeakFound = false;

Expand Down
1 change: 1 addition & 0 deletions source/adapters/level_zero/adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using PlatformVec = std::vector<std::unique_ptr<ur_platform_handle_t_>>;

struct ur_adapter_handle_t_ {
ur_adapter_handle_t_();
std::atomic<uint32_t> RefCount = 0;
std::mutex Mutex;

Expand Down
3 changes: 2 additions & 1 deletion source/adapters/level_zero/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ 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 = Adapter.PlatformCache->get_value();
cached_platforms) {
uint32_t nplatforms = (uint32_t)cached_platforms->size();
if (NumPlatforms) {
*NumPlatforms = nplatforms;
Expand Down

0 comments on commit 9babc4d

Please sign in to comment.