Skip to content

Commit 9babc4d

Browse files
authored
Merge pull request #1321 from pbalcer/adapter-compute-constructor
[L0] move adapter init into its constructor from urAdapterGet
2 parents 3be8f20 + 90498ec commit 9babc4d

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

source/adapters/level_zero/adapter.cpp

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include "adapter.hpp"
1212
#include "ur_level_zero.hpp"
1313

14-
ur_adapter_handle_t_ Adapter{};
15-
1614
ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
1715
uint32_t ZeDriverCount = 0;
1816
ZE2UR_CALL(zeDriverGet, (&ZeDriverCount, nullptr));
@@ -36,43 +34,45 @@ ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
3634
return exceptionToResult(std::current_exception());
3735
}
3836

39-
ur_result_t adapterStateInit() {
40-
static std::once_flag ZeCallCountInitialized;
41-
try {
42-
std::call_once(ZeCallCountInitialized, []() {
43-
if (UrL0LeaksDebug) {
44-
ZeCallCount = new std::map<std::string, int>;
45-
}
46-
});
47-
} catch (const std::bad_alloc &) {
48-
return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
49-
} catch (...) {
50-
return UR_RESULT_ERROR_UNKNOWN;
51-
}
37+
ur_result_t adapterStateInit() { return UR_RESULT_SUCCESS; }
5238

53-
// initialize level zero only once.
54-
if (Adapter.ZeResult == std::nullopt) {
55-
// Setting these environment variables before running zeInit will enable the
56-
// validation layer in the Level Zero loader.
57-
if (UrL0Debug & UR_L0_DEBUG_VALIDATION) {
58-
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
59-
setEnvVar("ZE_ENABLE_PARAMETER_VALIDATION", "1");
60-
}
39+
ur_adapter_handle_t_::ur_adapter_handle_t_() {
6140

62-
if (getenv("SYCL_ENABLE_PCI") != nullptr) {
63-
urPrint("WARNING: SYCL_ENABLE_PCI is deprecated and no longer needed.\n");
41+
Adapter.PlatformCache.Compute = [](Result<PlatformVec> &result) {
42+
static std::once_flag ZeCallCountInitialized;
43+
try {
44+
std::call_once(ZeCallCountInitialized, []() {
45+
if (UrL0LeaksDebug) {
46+
ZeCallCount = new std::map<std::string, int>;
47+
}
48+
});
49+
} catch (...) {
50+
result = exceptionToResult(std::current_exception());
51+
return;
6452
}
6553

66-
// TODO: We can still safely recover if something goes wrong during the
67-
// init. Implement handling segfault using sigaction.
54+
// initialize level zero only once.
55+
if (Adapter.ZeResult == std::nullopt) {
56+
// Setting these environment variables before running zeInit will enable
57+
// the validation layer in the Level Zero loader.
58+
if (UrL0Debug & UR_L0_DEBUG_VALIDATION) {
59+
setEnvVar("ZE_ENABLE_VALIDATION_LAYER", "1");
60+
setEnvVar("ZE_ENABLE_PARAMETER_VALIDATION", "1");
61+
}
6862

69-
// We must only initialize the driver once, even if urPlatformGet() is
70-
// called multiple times. Declaring the return value as "static" ensures
71-
// it's only called once.
72-
Adapter.ZeResult = ZE_CALL_NOCHECK(zeInit, (ZE_INIT_FLAG_GPU_ONLY));
73-
}
63+
if (getenv("SYCL_ENABLE_PCI") != nullptr) {
64+
urPrint(
65+
"WARNING: SYCL_ENABLE_PCI is deprecated and no longer needed.\n");
66+
}
7467

75-
Adapter.PlatformCache.Compute = [](Result<PlatformVec> &result) {
68+
// TODO: We can still safely recover if something goes wrong during the
69+
// init. Implement handling segfault using sigaction.
70+
71+
// We must only initialize the driver once, even if urPlatformGet() is
72+
// called multiple times. Declaring the return value as "static" ensures
73+
// it's only called once.
74+
Adapter.ZeResult = ZE_CALL_NOCHECK(zeInit, (ZE_INIT_FLAG_GPU_ONLY));
75+
}
7676
assert(Adapter.ZeResult !=
7777
std::nullopt); // verify that level-zero is initialized
7878
PlatformVec platforms;
@@ -95,9 +95,10 @@ ur_result_t adapterStateInit() {
9595
result = err;
9696
}
9797
};
98-
return UR_RESULT_SUCCESS;
9998
}
10099

100+
ur_adapter_handle_t_ Adapter{};
101+
101102
ur_result_t adapterStateTeardown() {
102103
bool LeakFound = false;
103104

source/adapters/level_zero/adapter.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using PlatformVec = std::vector<std::unique_ptr<ur_platform_handle_t_>>;
1818

1919
struct ur_adapter_handle_t_ {
20+
ur_adapter_handle_t_();
2021
std::atomic<uint32_t> RefCount = 0;
2122
std::mutex Mutex;
2223

source/adapters/level_zero/platform.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGet(
2929
) {
3030
// Platform handles are cached for reuse. This is to ensure consistent
3131
// handle pointers across invocations and to improve retrieval performance.
32-
if (const auto *cached_platforms = Adapter.PlatformCache->get_value()) {
32+
if (const auto *cached_platforms = Adapter.PlatformCache->get_value();
33+
cached_platforms) {
3334
uint32_t nplatforms = (uint32_t)cached_platforms->size();
3435
if (NumPlatforms) {
3536
*NumPlatforms = nplatforms;

0 commit comments

Comments
 (0)