Update sub device internal ref counts

Change-Id: I82eea99bbb3d1edc32d09c0b703dee30b62f6b76
Signed-off-by: Filip Hazubski <filip.hazubski@intel.com>
This commit is contained in:
Filip Hazubski
2020-04-02 16:38:42 +02:00
committed by sys_ocldev
parent 6d4832fe24
commit f2c11eb870
8 changed files with 107 additions and 11 deletions

View File

@ -69,6 +69,23 @@ ClDevice::~ClDevice() {
device.decRefInternal();
}
void ClDevice::incRefInternal() {
if (deviceInfo.parentDevice == nullptr) {
BaseObject<_cl_device_id>::incRefInternal();
return;
}
auto pParentDevice = static_cast<ClDevice *>(deviceInfo.parentDevice);
pParentDevice->incRefInternal();
}
unique_ptr_if_unused<ClDevice> ClDevice::decRefInternal() {
if (deviceInfo.parentDevice == nullptr) {
return BaseObject<_cl_device_id>::decRefInternal();
}
auto pParentDevice = static_cast<ClDevice *>(deviceInfo.parentDevice);
return pParentDevice->decRefInternal();
}
void ClDevice::allocateSyncBufferHandler() {
TakeOwnershipWrapper<ClDevice> lock(*this);
if (syncBufferHandler.get() == nullptr) {

View File

@ -51,6 +51,9 @@ class ClDevice : public BaseObject<_cl_device_id> {
explicit ClDevice(Device &device, Platform *platformId);
~ClDevice() override;
void incRefInternal();
unique_ptr_if_unused<ClDevice> decRefInternal();
unsigned int getEnabledClVersion() const { return enabledClVersion; };
unsigned int getSupportedClVersion() const;

View File

@ -6,6 +6,7 @@
*/
#include "shared/source/program/sync_buffer_handler.h"
#include "shared/test/unit_test/helpers/debug_manager_state_restore.h"
#include "opencl/source/api/api.h"
#include "opencl/test/unit_test/fixtures/enqueue_handler_fixture.h"
@ -171,17 +172,20 @@ TEST(SyncBufferHandlerDeviceTest, GivenRootDeviceWhenAllocateSyncBufferIsCalledT
}
TEST(SyncBufferHandlerDeviceTest, GivenSubDeviceWhenAllocateSyncBufferIsCalledTwiceThenTheObjectIsCreatedOnlyOnce) {
const size_t testUsedBufferSize = 100;
MockClDevice rootDevice{new MockDevice};
ClDevice subDevice{*rootDevice.createSubDevice(0), platform()};
subDevice.allocateSyncBufferHandler();
auto syncBufferHandler = reinterpret_cast<MockSyncBufferHandler *>(subDevice.syncBufferHandler.get());
DebugManagerStateRestore restorer;
DebugManager.flags.CreateMultipleSubDevices.set(2);
VariableBackup<bool> mockDeviceFlagBackup(&MockDevice::createSingleDevice, false);
auto rootDevice = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(defaultHwInfo.get()));
auto &subDevice = rootDevice->subDevices[0];
subDevice->allocateSyncBufferHandler();
auto syncBufferHandler = reinterpret_cast<MockSyncBufferHandler *>(subDevice->syncBufferHandler.get());
const size_t testUsedBufferSize = 100;
ASSERT_NE(syncBufferHandler->usedBufferSize, testUsedBufferSize);
syncBufferHandler->usedBufferSize = testUsedBufferSize;
subDevice.allocateSyncBufferHandler();
syncBufferHandler = reinterpret_cast<MockSyncBufferHandler *>(subDevice.syncBufferHandler.get());
subDevice->allocateSyncBufferHandler();
syncBufferHandler = reinterpret_cast<MockSyncBufferHandler *>(subDevice->syncBufferHandler.get());
EXPECT_EQ(testUsedBufferSize, syncBufferHandler->usedBufferSize);
}

View File

@ -10,9 +10,9 @@
#include "shared/test/unit_test/helpers/debug_manager_state_restore.h"
#include "shared/test/unit_test/helpers/ult_hw_config.h"
#include "shared/test/unit_test/helpers/variable_backup.h"
#include "shared/test/unit_test/mocks/mock_device.h"
#include "opencl/source/cl_device/cl_device.h"
#include "opencl/test/unit_test/mocks/mock_cl_device.h"
#include "opencl/test/unit_test/mocks/mock_memory_manager.h"
#include "opencl/test/unit_test/mocks/mock_platform.h"
@ -54,7 +54,7 @@ TEST(SubDevicesTest, givenCreateMultipleSubDevicesFlagSetWhenCreateRootDeviceThe
EXPECT_EQ(1u, device->subdevices.at(1)->getNumAvailableDevices());
}
TEST(SubDevicesTest, givenDeviceWithSubDevicesWhenSubDeviceRefcountsAreChangedThenChangeIsPropagatedToRootDevice) {
TEST(SubDevicesTest, givenDeviceWithSubDevicesWhenSubDeviceApiRefCountsAreChangedThenChangeIsPropagatedToRootDevice) {
DebugManagerStateRestore restorer;
DebugManager.flags.CreateMultipleSubDevices.set(2);
VariableBackup<bool> mockDeviceFlagBackup(&MockDevice::createSingleDevice, false);
@ -89,6 +89,62 @@ TEST(SubDevicesTest, givenDeviceWithSubDevicesWhenSubDeviceRefcountsAreChangedTh
EXPECT_EQ(baseDefaultDeviceInternalRefCount, defaultDevice->getRefInternalCount());
}
TEST(SubDevicesTest, givenDeviceWithSubDevicesWhenSubDeviceInternalRefCountsAreChangedThenChangeIsPropagatedToRootDevice) {
DebugManagerStateRestore restorer;
DebugManager.flags.CreateMultipleSubDevices.set(2);
VariableBackup<bool> mockDeviceFlagBackup(&MockDevice::createSingleDevice, false);
auto device = std::unique_ptr<MockDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(defaultHwInfo.get()));
device->incRefInternal();
auto subDevice = device->getDeviceById(0);
auto baseDeviceInternalRefCount = device->getRefInternalCount();
auto baseSubDeviceInternalRefCount = subDevice->getRefInternalCount();
subDevice->incRefInternal();
EXPECT_EQ(baseDeviceInternalRefCount + 1, device->getRefInternalCount());
EXPECT_EQ(baseSubDeviceInternalRefCount, subDevice->getRefInternalCount());
device->incRefInternal();
EXPECT_EQ(baseDeviceInternalRefCount + 2, device->getRefInternalCount());
EXPECT_EQ(baseSubDeviceInternalRefCount, subDevice->getRefInternalCount());
subDevice->decRefInternal();
EXPECT_EQ(baseDeviceInternalRefCount + 1, device->getRefInternalCount());
EXPECT_EQ(baseSubDeviceInternalRefCount, subDevice->getRefInternalCount());
device->decRefInternal();
EXPECT_EQ(baseDeviceInternalRefCount, device->getRefInternalCount());
EXPECT_EQ(baseSubDeviceInternalRefCount, subDevice->getRefInternalCount());
}
TEST(SubDevicesTest, givenClDeviceWithSubDevicesWhenSubDeviceInternalRefCountsAreChangedThenChangeIsPropagatedToRootDevice) {
DebugManagerStateRestore restorer;
DebugManager.flags.CreateMultipleSubDevices.set(2);
VariableBackup<bool> mockDeviceFlagBackup(&MockDevice::createSingleDevice, false);
auto device = std::make_unique<MockClDevice>(MockDevice::createWithNewExecutionEnvironment<MockDevice>(defaultHwInfo.get()));
device->incRefInternal();
auto &subDevice = device->subDevices[0];
auto baseDeviceInternalRefCount = device->getRefInternalCount();
auto baseSubDeviceInternalRefCount = subDevice->getRefInternalCount();
subDevice->incRefInternal();
EXPECT_EQ(baseDeviceInternalRefCount + 1, device->getRefInternalCount());
EXPECT_EQ(baseSubDeviceInternalRefCount, subDevice->getRefInternalCount());
device->incRefInternal();
EXPECT_EQ(baseDeviceInternalRefCount + 2, device->getRefInternalCount());
EXPECT_EQ(baseSubDeviceInternalRefCount, subDevice->getRefInternalCount());
subDevice->decRefInternal();
EXPECT_EQ(baseDeviceInternalRefCount + 1, device->getRefInternalCount());
EXPECT_EQ(baseSubDeviceInternalRefCount, subDevice->getRefInternalCount());
device->decRefInternal();
EXPECT_EQ(baseDeviceInternalRefCount, device->getRefInternalCount());
EXPECT_EQ(baseSubDeviceInternalRefCount, subDevice->getRefInternalCount());
}
TEST(SubDevicesTest, givenDeviceWithSubDevicesWhenSubDeviceCreationFailThenWholeDeviceIsDestroyed) {
DebugManagerStateRestore restorer;
DebugManager.flags.CreateMultipleSubDevices.set(10);

View File

@ -30,6 +30,13 @@ class Device : public ReferenceTrackedObject<Device> {
return createDeviceInternals(device);
}
virtual void incRefInternal() {
ReferenceTrackedObject<Device>::incRefInternal();
}
virtual unique_ptr_if_unused<Device> decRefInternal() {
return ReferenceTrackedObject<Device>::decRefInternal();
}
bool getDeviceAndHostTimer(uint64_t *deviceTimestamp, uint64_t *hostTimestamp) const;
bool getHostTimer(uint64_t *hostTimestamp) const;
const HardwareInfo &getHardwareInfo() const;

View File

@ -22,7 +22,7 @@ RootDevice::RootDevice(ExecutionEnvironment *executionEnvironment, uint32_t root
RootDevice::~RootDevice() {
for (auto subdevice : subdevices) {
if (subdevice) {
subdevice->decRefInternal();
delete subdevice;
}
}
}
@ -67,7 +67,6 @@ bool RootDevice::createDeviceImpl() {
if (!subDevice) {
return false;
}
subDevice->incRefInternal();
subdevices[i] = subDevice;
}
auto status = Device::createDeviceImpl();

View File

@ -15,6 +15,13 @@ SubDevice::SubDevice(ExecutionEnvironment *executionEnvironment, uint32_t subDev
: Device(executionEnvironment), subDeviceIndex(subDeviceIndex), rootDevice(rootDevice) {
}
void SubDevice::incRefInternal() {
rootDevice.incRefInternal();
}
unique_ptr_if_unused<Device> SubDevice::decRefInternal() {
return rootDevice.decRefInternal();
}
DeviceBitfield SubDevice::getDeviceBitfield() const {
DeviceBitfield deviceBitfield;
deviceBitfield.set(subDeviceIndex);

View File

@ -13,6 +13,9 @@ class RootDevice;
class SubDevice : public Device {
public:
SubDevice(ExecutionEnvironment *executionEnvironment, uint32_t subDeviceIndex, RootDevice &rootDevice);
void incRefInternal() override;
unique_ptr_if_unused<Device> decRefInternal() override;
uint32_t getNumAvailableDevices() const override;
uint32_t getRootDeviceIndex() const override;
Device *getDeviceById(uint32_t deviceId) const override;