Skip to content

Commit cfad41f

Browse files
Introduce new interface for reinitializing level zero device
This change introduce - new method deviceReinit, which could be used, to reinitialize existing level zero device. Related-To: LOCI-2612 Signed-off-by: Jitendra Sharma <[email protected]>
1 parent afaef2b commit cfad41f

File tree

9 files changed

+103
-14
lines changed

9 files changed

+103
-14
lines changed

level_zero/core/source/device/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ struct Device : _ze_device_handle_t {
109109
inline ze_device_handle_t toHandle() { return this; }
110110

111111
static Device *create(DriverHandle *driverHandle, NEO::Device *neoDevice, uint32_t currentDeviceMask, bool isSubDevice, ze_result_t *returnValue);
112+
static Device *create(DriverHandle *driverHandle, NEO::Device *neoDevice, uint32_t currentDeviceMask, bool isSubDevice, ze_result_t *returnValue, L0::Device *deviceL0);
113+
static Device *deviceReinit(DriverHandle *driverHandle, L0::Device *device, std::unique_ptr<NEO::Device> &neoDevice, ze_result_t *returnValue);
112114

113115
virtual NEO::PreemptionMode getDevicePreemptionMode() const = 0;
114116
virtual const NEO::DeviceInfo &getDeviceInfo() const = 0;

level_zero/core/source/device/device_imp.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,28 @@ uint32_t DeviceImp::getMaxNumHwThreads() const { return maxNumHwThreads; }
692692

693693
const NEO::HardwareInfo &DeviceImp::getHwInfo() const { return neoDevice->getHardwareInfo(); }
694694

695+
// Use this method to reinitialize L0::Device *device, that was created during zeInit, with the help of Device::create
696+
Device *Device::deviceReinit(DriverHandle *driverHandle, L0::Device *device, std::unique_ptr<NEO::Device> &neoDevice, ze_result_t *returnValue) {
697+
const auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
698+
auto rootDeviceEnvironment = neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[rootDeviceIndex].get();
699+
auto pNeoDevice = neoDevice.release();
700+
701+
auto subDevicesMask = static_cast<uint32_t>(rootDeviceEnvironment->deviceAffinityMask.getGenericSubDevicesMask().to_ulong());
702+
return Device::create(driverHandle, pNeoDevice, subDevicesMask, false, returnValue, device);
703+
}
704+
695705
Device *Device::create(DriverHandle *driverHandle, NEO::Device *neoDevice, uint32_t currentDeviceMask, bool isSubDevice, ze_result_t *returnValue) {
696-
auto device = new DeviceImp;
706+
return Device::create(driverHandle, neoDevice, currentDeviceMask, isSubDevice, returnValue, nullptr);
707+
}
708+
709+
Device *Device::create(DriverHandle *driverHandle, NEO::Device *neoDevice, uint32_t currentDeviceMask, bool isSubDevice, ze_result_t *returnValue, L0::Device *deviceL0) {
710+
L0::DeviceImp *device = nullptr;
711+
if (deviceL0 == nullptr) {
712+
device = new DeviceImp;
713+
} else {
714+
device = static_cast<L0::DeviceImp *>(deviceL0);
715+
}
716+
697717
UNRECOVERABLE_IF(device == nullptr);
698718

699719
device->setDriverHandle(driverHandle);
@@ -731,7 +751,7 @@ Device *Device::create(DriverHandle *driverHandle, NEO::Device *neoDevice, uint3
731751
ze_device_handle_t subDevice = Device::create(driverHandle,
732752
device->neoDevice->getSubDevice(i),
733753
0,
734-
true, returnValue);
754+
true, returnValue, nullptr);
735755
if (subDevice == nullptr) {
736756
return nullptr;
737757
}
@@ -783,10 +803,8 @@ Device *Device::create(DriverHandle *driverHandle, NEO::Device *neoDevice, uint3
783803
->notifyNewDevice(osInterface ? osInterface->getDriverModel()->getDeviceHandle() : 0);
784804
}
785805

786-
if (static_cast<DriverHandleImp *>(driverHandle)->enableSysman && !isSubDevice) {
787-
device->setSysmanHandle(L0::SysmanDeviceHandleContext::init(device->toHandle()));
788-
}
789-
806+
device->createSysmanHandle(isSubDevice);
807+
device->resourcesReleased = false;
790808
return device;
791809
}
792810

@@ -801,6 +819,7 @@ void DeviceImp::releaseResources() {
801819
for (uint32_t i = 0; i < this->numSubDevices; i++) {
802820
delete this->subDevices[i];
803821
}
822+
this->subDevices.clear();
804823
this->numSubDevices = 0;
805824

806825
if (this->pageFaultCommandList) {

level_zero/core/source/device/device_imp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ struct DeviceImp : public Device {
137137
NEO::SpinLock peerAllocationsMutex;
138138
std::map<NEO::SvmAllocationData *, MemAdviseFlags> memAdviseSharedAllocations;
139139
NEO::AllocationsList allocationsForReuse;
140+
void createSysmanHandle(bool isSubDevice);
140141

141142
protected:
142143
NEO::GraphicsAllocation *debugSurface = nullptr;

level_zero/core/source/driver/driver_handle_imp.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ DriverHandleImp::~DriverHandleImp() {
139139
}
140140
}
141141

142+
void DriverHandleImp::updateRootDeviceBitFields(std::unique_ptr<NEO::Device> &neoDevice) {
143+
const auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
144+
auto entry = this->deviceBitfields.find(rootDeviceIndex);
145+
entry->second = neoDevice->getDeviceBitfield();
146+
}
147+
148+
void DriverHandleImp::enableRootDeviceDebugger(std::unique_ptr<NEO::Device> &neoDevice) {
149+
const auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
150+
auto rootDeviceEnvironment = neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[rootDeviceIndex].get();
151+
152+
if (enableProgramDebugging) {
153+
if (neoDevice->getDebugger() != nullptr) {
154+
NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr,
155+
"%s", "Source Level Debugger cannot be used with Environment Variable enabling program debugging.\n");
156+
UNRECOVERABLE_IF(neoDevice->getDebugger() != nullptr && enableProgramDebugging);
157+
}
158+
rootDeviceEnvironment->debugger = DebuggerL0::create(neoDevice.get());
159+
}
160+
}
161+
142162
ze_result_t DriverHandleImp::initialize(std::vector<std::unique_ptr<NEO::Device>> neoDevices) {
143163
bool multiOsContextDriver = false;
144164
for (auto &neoDevice : neoDevices) {
@@ -157,14 +177,7 @@ ze_result_t DriverHandleImp::initialize(std::vector<std::unique_ptr<NEO::Device>
157177
const auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
158178
auto rootDeviceEnvironment = neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[rootDeviceIndex].get();
159179

160-
if (enableProgramDebugging) {
161-
if (neoDevice->getDebugger() != nullptr) {
162-
NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr,
163-
"%s", "Source Level Debugger cannot be used with Environment Variable enabling program debugging.\n");
164-
UNRECOVERABLE_IF(neoDevice->getDebugger() != nullptr && enableProgramDebugging);
165-
}
166-
rootDeviceEnvironment->debugger = DebuggerL0::create(neoDevice.get());
167-
}
180+
enableRootDeviceDebugger(neoDevice);
168181

169182
this->rootDeviceIndices.insert(rootDeviceIndex);
170183
this->deviceBitfields.insert({rootDeviceIndex, neoDevice->getDeviceBitfield()});

level_zero/core/source/driver/driver_handle_imp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ struct DriverHandleImp : public DriverHandle {
9898

9999
std::set<uint32_t> rootDeviceIndices = {};
100100
std::map<uint32_t, NEO::DeviceBitfield> deviceBitfields;
101+
void updateRootDeviceBitFields(std::unique_ptr<NEO::Device> &neoDevice);
102+
void enableRootDeviceDebugger(std::unique_ptr<NEO::Device> &neoDevice);
101103

102104
// Environment Variables
103105
bool enableProgramDebugging = false;

level_zero/core/test/unit_tests/sources/device/test_device.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ extern HwHelper *hwHelperFactory[IGFX_MAX_CORE];
4141
namespace L0 {
4242
namespace ult {
4343

44+
TEST(L0DeviceTest, GivenCreatedDeviceHandleWhenCallingdeviceReinitThenNewDeviceHandleIsNotCreated) {
45+
ze_result_t returnValue = ZE_RESULT_SUCCESS;
46+
std::unique_ptr<DriverHandleImp> driverHandle(new DriverHandleImp);
47+
auto hwInfo = *NEO::defaultHwInfo;
48+
auto neoDevice = std::unique_ptr<NEO::Device>(NEO::MockDevice::createWithNewExecutionEnvironment<NEO::MockDevice>(&hwInfo, 0));
49+
auto device = Device::create(driverHandle.get(), neoDevice.release(), 1, false, &returnValue);
50+
ASSERT_NE(nullptr, device);
51+
static_cast<DeviceImp *>(device)->releaseResources();
52+
53+
auto newNeoDevice = std::unique_ptr<NEO::Device>(NEO::MockDevice::createWithNewExecutionEnvironment<NEO::MockDevice>(&hwInfo, 0));
54+
EXPECT_EQ(device, Device::deviceReinit(device->getDriverHandle(), device, newNeoDevice, &returnValue));
55+
delete device;
56+
}
57+
4458
TEST(L0DeviceTest, GivenDualStorageSharedMemorySupportedWhenCreatingDeviceThenPageFaultCmdListImmediateWithInitializedCmdQIsCreated) {
4559
ze_result_t returnValue = ZE_RESULT_SUCCESS;
4660
DebugManagerStateRestore restorer;

level_zero/core/test/unit_tests/sources/driver/test_driver.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ TEST(zeInit, whenCallingZeInitWithoutGpuOnlyFlagThenInitializeOnDriverIsNotCalle
5353
EXPECT_EQ(0u, driver.initCalledCount);
5454
}
5555

56+
using DriverHandleImpTest = Test<DeviceFixture>;
57+
TEST_F(DriverHandleImpTest, givenDriverImpWhenCallingupdateRootDeviceBitFieldsThendeviceBitfieldsAreUpdatedInAccordanceWithNeoDevice) {
58+
auto hwInfo = *NEO::defaultHwInfo;
59+
auto newNeoDevice = std::unique_ptr<NEO::Device>(NEO::MockDevice::createWithNewExecutionEnvironment<NEO::MockDevice>(&hwInfo, 0));
60+
driverHandle->updateRootDeviceBitFields(newNeoDevice);
61+
const auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
62+
auto entry = driverHandle->deviceBitfields.find(rootDeviceIndex);
63+
EXPECT_EQ(newNeoDevice->getDeviceBitfield(), entry->second);
64+
}
65+
5666
using DriverVersionTest = Test<DeviceFixture>;
5767

5868
TEST_F(DriverVersionTest, givenCallToGetExtensionPropertiesThenSupportedExtensionsAreReturned) {

level_zero/tools/source/sysman/sysman.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
namespace L0 {
1818

19+
void DeviceImp::createSysmanHandle(bool isSubDevice) {
20+
if (static_cast<DriverHandleImp *>(driverHandle)->enableSysman && !isSubDevice) {
21+
if (this->getSysmanHandle() == nullptr) {
22+
// Sysman handles are created only during zeInit time device creation. And destroyed during L0::device destroy.
23+
this->setSysmanHandle(L0::SysmanDeviceHandleContext::init(this->toHandle()));
24+
}
25+
}
26+
}
27+
1928
SysmanDevice *SysmanDeviceHandleContext::init(ze_device_handle_t coreDevice) {
2029
SysmanDeviceImp *sysmanDevice = new SysmanDeviceImp(coreDevice);
2130
DEBUG_BREAK_IF(!sysmanDevice);

level_zero/tools/test/unit_tests/sources/sysman/linux/test_sysman.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,25 @@ TEST_F(SysmanMultiDeviceFixture, GivenValidEffectiveUserIdCheckWhetherPermission
338338
}
339339
}
340340

341+
TEST_F(SysmanMultiDeviceFixture, GivenSysmanEnvironmentVariableSetWhenCreateL0DeviceThenSysmanHandleCreateIsAttempted) {
342+
driverHandle->enableSysman = true;
343+
// In SetUp of SysmanMultiDeviceFixture, sysman handle for device is already created, so new sysman handle should not be created
344+
static_cast<DeviceImp *>(device)->createSysmanHandle(true);
345+
EXPECT_EQ(device->getSysmanHandle(), pSysmanDevice);
346+
347+
static_cast<DeviceImp *>(device)->createSysmanHandle(false);
348+
EXPECT_EQ(device->getSysmanHandle(), pSysmanDevice);
349+
350+
// delete previously allocated sysman handle and then attempt to create sysman handle again
351+
delete pSysmanDevice;
352+
device->setSysmanHandle(nullptr);
353+
static_cast<DeviceImp *>(device)->createSysmanHandle(true);
354+
EXPECT_EQ(device->getSysmanHandle(), nullptr);
355+
356+
static_cast<DeviceImp *>(device)->createSysmanHandle(false);
357+
EXPECT_EQ(device->getSysmanHandle(), nullptr);
358+
}
359+
341360
class UnknownDriverModel : public DriverModel {
342361
public:
343362
UnknownDriverModel() : DriverModel(DriverModelType::UNKNOWN) {}

0 commit comments

Comments
 (0)