Skip to content

Commit

Permalink
levelzero: disable ZES_ENABLE_SYSMAN=1 when zesInit() is available
Browse files Browse the repository at this point in the history
When zesInit() is available (starting in specs 1.5), we don't need
all the stuff to set ZES_ENABLE_SYSMAN=1 in the environment anymore.

This assumes that zesInit() works for real, which was not the case
in first compute-runtime releases (it returned "unsupported").
Those releases won't be supported anymore because they'd need
a runtime-check at configure time to find out that the ZES_ENABLE_SYSMAN=1
stuff must be enabled.

Only use a compile-check to switch between zesInit() and
ZES_ENABLE_SYSMAN=1, and tell users to avoid those L0 runtime
versions where zesInit() fails.

Signed-off-by: Brice Goglin <Brice.Goglin@inria.fr>
  • Loading branch information
bgoglin committed Jun 20, 2023
1 parent a7549c0 commit 3dc6905
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
19 changes: 12 additions & 7 deletions hwloc/topology-levelzero.c
Expand Up @@ -604,8 +604,7 @@ hwloc_levelzero_discover(struct hwloc_backend *backend, struct hwloc_disc_status
uint32_t nbdrivers, i, k, zeidx;
struct hwloc_osdev_array oarray;
struct hwloc_levelzero_ports hports;
int sysman_maybe_missing = 0; /* 1 if ZES_ENABLE_SYSMAN=1 was NOT set early, 2 if ZES_ENABLE_SYSMAN=0 */
char *env;
int sysman_maybe_missing = 0; /* 1 if ZES_ENABLE_SYSMAN=1 was NOT set early and zesInit() isn't available, 2 if ZES_ENABLE_SYSMAN=0 */

assert(dstatus->phase == HWLOC_DISC_PHASE_IO);

Expand All @@ -620,18 +619,22 @@ hwloc_levelzero_discover(struct hwloc_backend *backend, struct hwloc_disc_status
#ifdef HWLOC_HAVE_ZESINIT
res = zesInit(0);
if (res != ZE_RESULT_SUCCESS) {
hwloc_debug("hwloc/levelzero: Failed to initialize LevelZero Sysman in zesInit(): 0x%x\n", (unsigned)res);
hwloc_debug("hwloc/levelzero: Continuing. Hopefully ZES_ENABLE_SYSMAN=1\n");
if (res == ZE_RESULT_ERROR_UNSUPPORTED_FEATURE) {
fprintf(stderr, "hwloc/levelzero: Unsupported zesInit(), please either an older or a more recent level-zero runtime.\n");
} else if (HWLOC_SHOW_ALL_ERRORS()) {
fprintf(stderr, "hwloc/levelzero: Failed to initialize LevelZero in zesInit(): 0x%x\n", (unsigned)res);
}
return 0;
}
#endif /* HWLOC_HAVE_ZESINIT */

#else /* !HWLOC_HAVE_ZESINIT */
/* Tell L0 to create sysman devices.
* If somebody already initialized L0 without Sysman,
* zesDeviceGetProperties() will fail and warn in hwloc__levelzero_properties_get().
* The lib constructor and Windows DllMain tried to set ZES_ENABLE_SYSMAN=1 early (see topology.c),
* we try again in case they didn't.
*/
env = getenv("ZES_ENABLE_SYSMAN");
{
char *env = getenv("ZES_ENABLE_SYSMAN");
if (!env) {
/* setenv() is safer than putenv() but not available on Windows */
#ifdef HWLOC_WIN_SYS
Expand All @@ -644,6 +647,8 @@ hwloc_levelzero_discover(struct hwloc_backend *backend, struct hwloc_disc_status
} else if (!atoi(env)) {
sysman_maybe_missing = 2;
}
}
#endif /* !HWLOC_HAVE_ZESINIT */

res = zeInit(0);
if (res != ZE_RESULT_SUCCESS) {
Expand Down
5 changes: 3 additions & 2 deletions hwloc/topology.c
Expand Up @@ -54,9 +54,10 @@
#endif


#ifdef HWLOC_HAVE_LEVELZERO
#if (defined HWLOC_HAVE_LEVELZERO) && !(defined HWLOC_HAVE_ZESINIT)
/*
* Define ZES_ENABLE_SYSMAN=1 early so that the LevelZero backend gets Sysman enabled.
* This is only for old releases (<1.5) without zesInit().
*
* Only if the levelzero was enabled in this build so that we don't enable sysman
* for external levelzero users when hwloc doesn't need it. If somebody ever loads
Expand Down Expand Up @@ -101,7 +102,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved)
return TRUE;
}
#endif
#endif /* HWLOC_HAVE_LEVELZERO */
#endif /* HWLOC_HAVE_LEVELZERO && !HWLOC_HAVE_ZESINIT */


unsigned hwloc_get_api_version(void)
Expand Down
10 changes: 5 additions & 5 deletions tests/hwloc/levelzero.c
Expand Up @@ -26,19 +26,19 @@ int main(void)
#ifdef HWLOC_HAVE_ZESINIT
res = zesInit(0);
if (res != ZE_RESULT_SUCCESS) {
fprintf(stderr, "Failed to initialize LevelZero Sysman in zesInit(): %d\n", (int)res);
/* continuing, assuming ZES_ENABLE_SYSMAN=1 will be enough */
fprintf(stderr, "Failed to initialize LevelZero in zesInit(): %d\n", (int)res);
return 0;
}
#endif

#else
putenv((char *) "ZES_ENABLE_SYSMAN=1");
#endif

res = zeInit(0);
if (res != ZE_RESULT_SUCCESS) {
fprintf(stderr, "Failed to initialize LevelZero in zeInit(): %d\n", (int)res);
return 0;
}

hwloc_topology_init(&topology);
hwloc_topology_set_io_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_IMPORTANT);
hwloc_topology_load(topology);
Expand Down

0 comments on commit 3dc6905

Please sign in to comment.