Skip to content

Commit

Permalink
Disconnect zeInitDrivers and zeDriverGet (#234)
Browse files Browse the repository at this point in the history
- Ensure zeInitDrivers and zeDriverGet can get different sets of drivers
  and their inits do not conflict.
- Expanded unit tests to cover this case.
  • Loading branch information
nrspruit authored Nov 14, 2024
1 parent 1413421 commit ea296eb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
8 changes: 8 additions & 0 deletions scripts/templates/ldrddi.cpp.mako
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ namespace loader
continue;
%else:
if (!drv.dditable.${n}.${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)}) {
%if re.match(r"\w+InitDrivers$", th.make_func_name(n, tags, obj)):
drv.initDriversStatus = ${X}_RESULT_ERROR_UNINITIALIZED;
%else:
drv.initStatus = ${X}_RESULT_ERROR_UNINITIALIZED;
%endif
continue;
}
%endif
Expand All @@ -88,7 +92,11 @@ namespace loader
if( ${X}_RESULT_SUCCESS != result ) {
// If Get Drivers fails with Uninitialized, then update the driver init status to prevent reporting this driver in the next get call.
if (${X}_RESULT_ERROR_UNINITIALIZED == result) {
%if re.match(r"\w+InitDrivers$", th.make_func_name(n, tags, obj)):
drv.initDriversStatus = result;
%else:
drv.initStatus = result;
%endif
}
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions source/loader/ze_ldrddi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ namespace loader
for( auto& drv : loader::context->zeDrivers )
{
if (!drv.dditable.ze.Global.pfnInitDrivers) {
drv.initStatus = ZE_RESULT_ERROR_UNINITIALIZED;
drv.initDriversStatus = ZE_RESULT_ERROR_UNINITIALIZED;
continue;
}

Expand All @@ -147,7 +147,7 @@ namespace loader
if( ZE_RESULT_SUCCESS != result ) {
// If Get Drivers fails with Uninitialized, then update the driver init status to prevent reporting this driver in the next get call.
if (ZE_RESULT_ERROR_UNINITIALIZED == result) {
drv.initStatus = result;
drv.initDriversStatus = result;
}
continue;
}
Expand Down
62 changes: 42 additions & 20 deletions test/loader_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,97 +119,119 @@ TEST(
LoaderInit,
GivenLevelZeroLoaderPresentWhenCallingZeInitDriversThenzeInitThenBothCallsSucceedWithAllTypes) {

uint32_t pCount = 0;
uint32_t pInitDriversCount = 0;
uint32_t pDriverGetCount = 0;
ze_init_driver_type_desc_t desc = {ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC};
desc.flags = UINT32_MAX;
desc.pNext = nullptr;
putenv_safe( const_cast<char *>( "ZEL_TEST_NULL_DRIVER_TYPE=ALL" ) );
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pCount, nullptr, &desc));
EXPECT_GT(pCount, 0);
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pInitDriversCount, nullptr, &desc));
EXPECT_GT(pInitDriversCount, 0);
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(0));
EXPECT_EQ(ZE_RESULT_SUCCESS, zeDriverGet(&pDriverGetCount, nullptr));
EXPECT_GT(pDriverGetCount, 0);
}

TEST(
LoaderInit,
GivenLevelZeroLoaderPresentWhenCallingZeInitDriversThenzeInitThenBothCallsSucceedWithGPUTypes) {

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

TEST(
LoaderInit,
GivenZeInitDriversUnsupportedOnTheDriverWhenCallingZeInitDriversThenUninitializedReturned) {

uint32_t pCount = 0;
uint32_t pInitDriversCount = 0;
uint32_t pDriverGetCount = 0;
ze_init_driver_type_desc_t desc = {ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC};
desc.flags = UINT32_MAX;
desc.pNext = nullptr;
putenv_safe( const_cast<char *>( "ZEL_TEST_MISSING_API=zeInitDrivers" ) );
EXPECT_EQ(ZE_RESULT_ERROR_UNINITIALIZED, zeInitDrivers(&pCount, nullptr, &desc));
EXPECT_EQ(ZE_RESULT_ERROR_UNINITIALIZED, zeInitDrivers(&pInitDriversCount, nullptr, &desc));
EXPECT_EQ(pInitDriversCount, 0);
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(0));
EXPECT_EQ(ZE_RESULT_SUCCESS, zeDriverGet(&pDriverGetCount, nullptr));
EXPECT_GT(pDriverGetCount, 0);
}

TEST(
LoaderInit,
GivenLevelZeroLoaderPresentWhenCallingZeInitDriversThenzeInitThenBothCallsSucceedWithNPUTypes) {

uint32_t pCount = 0;
uint32_t pInitDriversCount = 0;
uint32_t pDriverGetCount = 0;
ze_init_driver_type_desc_t desc = {ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC};
desc.flags = UINT32_MAX;
desc.pNext = nullptr;
putenv_safe( const_cast<char *>( "ZEL_TEST_NULL_DRIVER_TYPE=NPU" ) );
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pCount, nullptr, &desc));
EXPECT_GT(pCount, 0);
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pInitDriversCount, nullptr, &desc));
EXPECT_GT(pInitDriversCount, 0);
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(ZE_INIT_FLAG_VPU_ONLY));
EXPECT_EQ(ZE_RESULT_SUCCESS, zeDriverGet(&pDriverGetCount, nullptr));
EXPECT_GT(pDriverGetCount, 0);
}

TEST(
LoaderInit,
GivenLevelZeroLoaderPresentWhenCallingzeInitThenZeInitDriversThenBothCallsSucceedWithAllTypes) {

uint32_t pCount = 0;
uint32_t pInitDriversCount = 0;
uint32_t pDriverGetCount = 0;
ze_init_driver_type_desc_t desc = {ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC};
desc.flags = UINT32_MAX;
desc.pNext = nullptr;
putenv_safe( const_cast<char *>( "ZEL_TEST_NULL_DRIVER_TYPE=ALL" ) );
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(0));
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pCount, nullptr, &desc));
EXPECT_GT(pCount, 0);
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pInitDriversCount, nullptr, &desc));
EXPECT_GT(pInitDriversCount, 0);
EXPECT_EQ(ZE_RESULT_SUCCESS, zeDriverGet(&pDriverGetCount, nullptr));
EXPECT_GT(pDriverGetCount, 0);
}

TEST(
LoaderInit,
GivenLevelZeroLoaderPresentWhenCallingzeInitThenZeInitDriversThenBothCallsSucceedWithGPUTypes) {

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

TEST(
LoaderInit,
GivenLevelZeroLoaderPresentWhenCallingzeInitThenZeInitDriversThenBothCallsSucceedWithNPUTypes) {

uint32_t pCount = 0;
uint32_t pInitDriversCount = 0;
uint32_t pDriverGetCount = 0;
ze_init_driver_type_desc_t desc = {ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC};
desc.flags = UINT32_MAX;
desc.pNext = nullptr;
putenv_safe( const_cast<char *>( "ZEL_TEST_NULL_DRIVER_TYPE=NPU" ) );
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInit(ZE_INIT_FLAG_VPU_ONLY));
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pCount, nullptr, &desc));
EXPECT_GT(pCount, 0);
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pInitDriversCount, nullptr, &desc));
EXPECT_GT(pInitDriversCount, 0);
EXPECT_EQ(ZE_RESULT_SUCCESS, zeDriverGet(&pDriverGetCount, nullptr));
EXPECT_GT(pDriverGetCount, 0);
}

} // namespace

0 comments on commit ea296eb

Please sign in to comment.