@@ -52,7 +52,32 @@ class ur_legacy_sink : public logger::Sink {
5252 };
5353};
5454
55- ur_result_t initPlatforms (PlatformVec &platforms) noexcept try {
55+ // Find the corresponding ZesDevice Handle for a given ZeDevice
56+ ur_result_t getZesDeviceHandle (zes_uuid_t coreDeviceUuid,
57+ zes_device_handle_t *ZesDevice,
58+ uint32_t *SubDeviceId, ze_bool_t *SubDevice) {
59+ uint32_t ZesDriverCount = 0 ;
60+ std::vector<zes_driver_handle_t > ZesDrivers;
61+ std::vector<zes_device_handle_t > ZesDevices;
62+ ze_result_t ZesResult = ZE_RESULT_ERROR_INVALID_ARGUMENT;
63+ ZE2UR_CALL (GlobalAdapter->getSysManDriversFunctionPtr ,
64+ (&ZesDriverCount, nullptr ));
65+ ZesDrivers.resize (ZesDriverCount);
66+ ZE2UR_CALL (GlobalAdapter->getSysManDriversFunctionPtr ,
67+ (&ZesDriverCount, ZesDrivers.data ()));
68+ for (uint32_t I = 0 ; I < ZesDriverCount; ++I) {
69+ ZesResult = ZE_CALL_NOCHECK (
70+ GlobalAdapter->getDeviceByUUIdFunctionPtr ,
71+ (ZesDrivers[I], coreDeviceUuid, ZesDevice, SubDevice, SubDeviceId));
72+ if (ZesResult == ZE_RESULT_SUCCESS) {
73+ return UR_RESULT_SUCCESS;
74+ }
75+ }
76+ return UR_RESULT_ERROR_INVALID_ARGUMENT;
77+ }
78+
79+ ur_result_t initPlatforms (PlatformVec &platforms,
80+ ze_result_t ZesResult) noexcept try {
5681 uint32_t ZeDriverCount = 0 ;
5782 ZE2UR_CALL (zeDriverGet, (&ZeDriverCount, nullptr ));
5883 if (ZeDriverCount == 0 ) {
@@ -65,24 +90,43 @@ ur_result_t initPlatforms(PlatformVec &platforms) noexcept try {
6590
6691 ZE2UR_CALL (zeDriverGet, (&ZeDriverCount, ZeDrivers.data ()));
6792 for (uint32_t I = 0 ; I < ZeDriverCount; ++I) {
93+ // Keep track of the first platform init for this Driver
94+ bool DriverPlatformInit = false ;
6895 ze_device_properties_t device_properties{};
6996 device_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES;
7097 uint32_t ZeDeviceCount = 0 ;
7198 ZE2UR_CALL (zeDeviceGet, (ZeDrivers[I], &ZeDeviceCount, nullptr ));
7299 ZeDevices.resize (ZeDeviceCount);
73100 ZE2UR_CALL (zeDeviceGet, (ZeDrivers[I], &ZeDeviceCount, ZeDevices.data ()));
101+ auto platform = std::make_unique<ur_platform_handle_t_>(ZeDrivers[I]);
74102 // Check if this driver has GPU Devices
75103 for (uint32_t D = 0 ; D < ZeDeviceCount; ++D) {
76104 ZE2UR_CALL (zeDeviceGetProperties, (ZeDevices[D], &device_properties));
77-
78105 if (ZE_DEVICE_TYPE_GPU == device_properties.type ) {
79- // If this Driver is a GPU, save it as a usable platform.
80- auto platform = std::make_unique<ur_platform_handle_t_>(ZeDrivers[I]);
81- UR_CALL (platform->initialize ());
82-
83- // Save a copy in the cache for future uses.
84- platforms.push_back (std::move (platform));
85- break ;
106+ // Check if this driver's platform has already been init.
107+ if (!DriverPlatformInit) {
108+ // If this Driver is a GPU, save it as a usable platform.
109+ UR_CALL (platform->initialize ());
110+
111+ // Save a copy in the cache for future uses.
112+ platforms.push_back (std::move (platform));
113+ // Mark this driver's platform as init to prevent additional platforms
114+ // from being created per driver.
115+ DriverPlatformInit = true ;
116+ }
117+ if (ZesResult == ZE_RESULT_SUCCESS) {
118+ // Populate the Zes/Ze device mapping for this Ze Device into the last
119+ // added platform which represents the current driver being queried.
120+ ur_zes_device_handle_data_t ZesDeviceData;
121+ zes_uuid_t ZesUUID;
122+ std::memcpy (&ZesUUID, &device_properties.uuid , sizeof (zes_uuid_t ));
123+ if (getZesDeviceHandle (
124+ ZesUUID, &ZesDeviceData.ZesDevice , &ZesDeviceData.SubDeviceId ,
125+ &ZesDeviceData.SubDevice ) == UR_RESULT_SUCCESS) {
126+ platforms.back ()->ZedeviceToZesDeviceMap .insert (
127+ std::make_pair (ZeDevices[D], std::move (ZesDeviceData)));
128+ }
129+ }
86130 }
87131 }
88132 }
@@ -171,8 +215,36 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
171215
172216 return ;
173217 }
218+ // Dynamically load the new L0 SysMan separate init and new EXP apis
219+ // separately. This must be done to avoid attempting to use symbols that do
220+ // not exist in older loader runtimes.
221+ #ifdef _WIN32
222+ HMODULE processHandle = GetModuleHandle (NULL );
223+ #else
224+ HMODULE processHandle = nullptr ;
225+ #endif
226+ GlobalAdapter->getDeviceByUUIdFunctionPtr =
227+ (zes_pfnDriverGetDeviceByUuidExp_t)ur_loader::LibLoader::getFunctionPtr (
228+ processHandle, " zesDriverGetDeviceByUuidExp" );
229+ GlobalAdapter->getSysManDriversFunctionPtr =
230+ (zes_pfnDriverGet_t)ur_loader::LibLoader::getFunctionPtr (
231+ processHandle, " zesDriverGet" );
232+ GlobalAdapter->sysManInitFunctionPtr =
233+ (zes_pfnInit_t)ur_loader::LibLoader::getFunctionPtr (processHandle,
234+ " zesInit" );
235+ if (GlobalAdapter->getDeviceByUUIdFunctionPtr &&
236+ GlobalAdapter->getSysManDriversFunctionPtr &&
237+ GlobalAdapter->sysManInitFunctionPtr ) {
238+ ze_init_flags_t L0ZesInitFlags = 0 ;
239+ logger::debug (" \n zesInit with flags value of {}\n " ,
240+ static_cast <int >(L0ZesInitFlags));
241+ GlobalAdapter->ZesResult = ZE_CALL_NOCHECK (
242+ GlobalAdapter->sysManInitFunctionPtr , (L0ZesInitFlags));
243+ } else {
244+ GlobalAdapter->ZesResult = ZE_RESULT_ERROR_UNINITIALIZED;
245+ }
174246
175- ur_result_t err = initPlatforms (platforms);
247+ ur_result_t err = initPlatforms (platforms, *GlobalAdapter-> ZesResult );
176248 if (err == UR_RESULT_SUCCESS) {
177249 result = std::move (platforms);
178250 } else {
0 commit comments