Skip to content

Commit

Permalink
Merge pull request #2242 from nrspruit/sysman_env_disable
Browse files Browse the repository at this point in the history
[L0] Enable Sysman Thru Env by default and have zesInit be optional
  • Loading branch information
pbalcer authored Oct 28, 2024
2 parents 694c1b9 + 27ad3f7 commit dbd168c
Showing 1 changed file with 73 additions and 9 deletions.
82 changes: 73 additions & 9 deletions source/adapters/level_zero/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,43 @@ ur_result_t adapterStateInit() {
return UR_RESULT_SUCCESS;
}

/*
This constructor initializes the `ur_adapter_handle_t_` object and
sets up the environment for Level Zero (L0) initialization.
The behavior of the initialization process is influenced by two
environment variables:
`UR_L0_ENABLE_SYSMAN_ENV_DEFAULT` and `UR_L0_ENABLE_ZESINIT_DEFAULT`.
| Environment Variable | Value | Behavior |
|--------------------------------|-------|----------------------------|
| UR_L0_ENABLE_SYSMAN_ENV_DEFAULT| 1 | Enables the default SysMan |
| | | environment initialization |
| | | by setting |
| | | `ZES_ENABLE_SYSMAN` to "1".|
| | 0 | Disables the default SysMan|
| | | environment initialization.|
| | unset | Defaults to 1, enabling the|
| | | SysMan environment |
| | | initialization. |
| UR_L0_ENABLE_ZESINIT_DEFAULT | 1 | Enables the default SysMan |
| | | initialization by loading |
| | | SysMan-related functions |
| | | and calling `zesInit`. |
| | 0 | Disables the default SysMan|
| | | initialization with zesInit|
| | unset | Defaults to 0, disabling |
| | | the SysMan initialization |
| | | thru zesInit. |
Behavior Summary:
- If `UR_L0_ENABLE_SYSMAN_ENV_DEFAULT` is set to 1 or is unset,
`ZES_ENABLE_SYSMAN` is set to "1".
- If `UR_L0_ENABLE_ZESINIT_DEFAULT` is set to 1 and
`UR_L0_ENABLE_SYSMAN_ENV_DEFAULT` is not set to 1,
SysMan-related functions are loaded and `zesInit` is called.
- If `UR_L0_ENABLE_ZESINIT_DEFAULT` is set to 0 or is unset,
SysMan initialization is skipped.
*/
ur_adapter_handle_t_::ur_adapter_handle_t_()
: logger(logger::get_logger("level_zero")) {

Expand All @@ -169,6 +206,14 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
return;
}

// Check if the user has disabled the default L0 Env initialization.
const int UrSysManEnvInitEnabled = [] {
const char *UrRet = std::getenv("UR_L0_ENABLE_SYSMAN_ENV_DEFAULT");
if (!UrRet)
return 1;
return std::atoi(UrRet);
}();

// initialize level zero only once.
if (GlobalAdapter->ZeResult == std::nullopt) {
// Setting these environment variables before running zeInit will enable
Expand Down Expand Up @@ -196,6 +241,11 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
if (UrL0InitAllDrivers) {
L0InitFlags |= ZE_INIT_FLAG_VPU_ONLY;
}

// Set ZES_ENABLE_SYSMAN by default if the user has not set it.
if (UrSysManEnvInitEnabled) {
setEnvVar("ZES_ENABLE_SYSMAN", "1");
}
logger::debug("\nzeInit with flags value of {}\n",
static_cast<int>(L0InitFlags));
GlobalAdapter->ZeResult = ZE_CALL_NOCHECK(zeInit, (L0InitFlags));
Expand Down Expand Up @@ -223,15 +273,29 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
#else
HMODULE processHandle = nullptr;
#endif
GlobalAdapter->getDeviceByUUIdFunctionPtr =
(zes_pfnDriverGetDeviceByUuidExp_t)ur_loader::LibLoader::getFunctionPtr(
processHandle, "zesDriverGetDeviceByUuidExp");
GlobalAdapter->getSysManDriversFunctionPtr =
(zes_pfnDriverGet_t)ur_loader::LibLoader::getFunctionPtr(
processHandle, "zesDriverGet");
GlobalAdapter->sysManInitFunctionPtr =
(zes_pfnInit_t)ur_loader::LibLoader::getFunctionPtr(processHandle,
"zesInit");

// Check if the user has enabled the default L0 SysMan initialization.
const int UrSysmanZesinitEnable = [] {
const char *UrRet = std::getenv("UR_L0_ENABLE_ZESINIT_DEFAULT");
if (!UrRet)
return 0;
return std::atoi(UrRet);
}();

// Enable zesInit by default only if ZES_ENABLE_SYSMAN has not been set by
// default and UrSysmanZesinitEnable is true.
if (UrSysmanZesinitEnable && !UrSysManEnvInitEnabled) {
GlobalAdapter->getDeviceByUUIdFunctionPtr =
(zes_pfnDriverGetDeviceByUuidExp_t)
ur_loader::LibLoader::getFunctionPtr(
processHandle, "zesDriverGetDeviceByUuidExp");
GlobalAdapter->getSysManDriversFunctionPtr =
(zes_pfnDriverGet_t)ur_loader::LibLoader::getFunctionPtr(
processHandle, "zesDriverGet");
GlobalAdapter->sysManInitFunctionPtr =
(zes_pfnInit_t)ur_loader::LibLoader::getFunctionPtr(processHandle,
"zesInit");
}
if (GlobalAdapter->getDeviceByUUIdFunctionPtr &&
GlobalAdapter->getSysManDriversFunctionPtr &&
GlobalAdapter->sysManInitFunctionPtr) {
Expand Down

0 comments on commit dbd168c

Please sign in to comment.