Skip to content

Commit

Permalink
Remove static result in InitDrivers given first init fails (#242)
Browse files Browse the repository at this point in the history
- Allow for the first zeInitDrivers to fail, but all following calls to
  succeed with different init flags.
- Updated to v1.19.2 with the init result fix

Signed-off-by: Neil R. Spruit <[email protected]>
  • Loading branch information
nrspruit authored Nov 20, 2024
1 parent 91e2866 commit 16fffbb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Level zero loader changelog

## v1.19.2
* Remove static result in InitDrivers given first init fails
## v1.19.1
* Fix to Use relative paths for events deadlock detection third party headers
## v1.19.0
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ message(FATAL_ERROR "Visual Studio Compiler Version >= 1900 Required to build.")
endif()

# This project follows semantic versioning (https://semver.org/)
project(level-zero VERSION 1.19.1)
project(level-zero VERSION 1.19.2)

include(GNUInstallDirs)

Expand Down
6 changes: 4 additions & 2 deletions scripts/templates/libapi.cpp.mako
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ ${th.make_func_name(n, tags, obj)}(
)
{
%if re.match("Init", obj['name']):
%if re.match("zes", n):
static ${x}_result_t result = ${X}_RESULT_SUCCESS;
%if re.match("zes", n):
std::call_once(${x}_lib::context->initOnceSysMan, [flags]() {
result = ${x}_lib::context->Init(flags, true, nullptr);

Expand All @@ -81,7 +81,8 @@ ${th.make_func_name(n, tags, obj)}(
}
%else:
%if re.match("InitDrivers", obj['name']):
std::call_once(${x}_lib::context->initOnceDrivers, [desc]() {
${x}_result_t result = ${X}_RESULT_SUCCESS;
std::call_once(${x}_lib::context->initOnceDrivers, [desc,&result]() {
result = ${x}_lib::context->Init(0, false, desc);
return result;
});
Expand Down Expand Up @@ -112,6 +113,7 @@ ${th.make_func_name(n, tags, obj)}(

return result;
%else:
static ${x}_result_t result = ${X}_RESULT_SUCCESS;
std::call_once(${x}_lib::context->initOnce, [flags]() {
result = ${x}_lib::context->Init(flags, false, nullptr);

Expand Down
4 changes: 2 additions & 2 deletions source/lib/ze_libapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ zeInitDrivers(
///< including ::ze_init_driver_type_flag_t combinations.
)
{
static ze_result_t result = ZE_RESULT_SUCCESS;
std::call_once(ze_lib::context->initOnceDrivers, [desc]() {
ze_result_t result = ZE_RESULT_SUCCESS;
std::call_once(ze_lib::context->initOnceDrivers, [desc,&result]() {
result = ze_lib::context->Init(0, false, desc);
return result;
});
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ add_test(NAME tests_both_npu COMMAND tests --gtest_filter=*GivenLevelZeroLoaderP
set_property(TEST tests_both_npu PROPERTY ENVIRONMENT "ZE_ENABLE_NULL_DRIVER=1")
add_test(NAME tests_missing_api COMMAND tests --gtest_filter=*GivenZeInitDriversUnsupportedOnTheDriverWhenCallingZeInitDriversThenUninitializedReturned*)
set_property(TEST tests_missing_api PROPERTY ENVIRONMENT "ZE_ENABLE_NULL_DRIVER=1")
add_test(NAME tests_multi_call_failure COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWhenCallingZeInitDriversWithTypesUnsupportedWithFailureThenSupportedTypesThenSuccessReturned*)
set_property(TEST tests_multi_call_failure PROPERTY ENVIRONMENT "ZE_ENABLE_NULL_DRIVER=1")

# These tests are currently not supported on Windows. The reason is that the std::cerr is not being redirected to a pipe in Windows to be then checked against the expected output.
if(NOT MSVC)
Expand Down
17 changes: 17 additions & 0 deletions test/loader_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ TEST(
}
}

TEST(
LoaderInit,
GivenLevelZeroLoaderPresentWhenCallingZeInitDriversWithTypesUnsupportedWithFailureThenSupportedTypesThenSuccessReturned) {

uint32_t pCount = 0;
ze_init_driver_type_desc_t desc = {ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC};
desc.flags = ZE_INIT_DRIVER_TYPE_FLAG_NPU;
desc.pNext = nullptr;
putenv_safe( const_cast<char *>( "ZEL_TEST_NULL_DRIVER_TYPE=GPU" ) );
EXPECT_EQ(ZE_RESULT_ERROR_UNINITIALIZED, zeInitDrivers(&pCount, nullptr, &desc));
EXPECT_EQ(pCount, 0);
pCount = 0;
desc.flags = UINT32_MAX;
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pCount, nullptr, &desc));
EXPECT_GT(pCount, 0);
}

TEST(
LoaderInit,
GivenLevelZeroLoaderPresentWhenCallingZeInitDriversWithGPUTypeThenExpectPassWithGPUorAllOnly) {
Expand Down

0 comments on commit 16fffbb

Please sign in to comment.