Pass valid device bitfield while allocating memory

Related-To: NEO-4645

Change-Id: I96eaf3c4f5aba8b8b3de36182accdc16f28f7ee4
Signed-off-by: Andrzej Swierczynski <andrzej.swierczynski@intel.com>
This commit is contained in:
Andrzej Swierczynski
2020-06-16 16:36:50 +02:00
committed by sys_ocldev
parent dd407681ff
commit 2b7ce21709
2 changed files with 142 additions and 6 deletions

View File

@@ -31,9 +31,10 @@ ze_result_t DriverHandleImp::openIpcMemHandle(ze_device_handle_t hDevice, ze_ipc
ze_ipc_memory_flag_t flags, void **ptr) {
uint64_t handle = *(pIpcHandle.data);
NEO::osHandle osHandle = static_cast<NEO::osHandle>(handle);
NEO::AllocationProperties unifiedMemoryProperties{Device::fromHandle(hDevice)->getRootDeviceIndex(),
NEO::AllocationProperties unifiedMemoryProperties{Device::fromHandle(hDevice)->getNEODevice()->getRootDeviceIndex(),
MemoryConstants::pageSize,
NEO::GraphicsAllocation::AllocationType::BUFFER};
unifiedMemoryProperties.subDevicesBitfield = Device::fromHandle(hDevice)->getNEODevice()->getDeviceBitfield();
NEO::GraphicsAllocation *alloc =
this->getMemoryManager()->createGraphicsAllocationFromSharedHandle(osHandle,
unifiedMemoryProperties,
@@ -106,6 +107,7 @@ ze_result_t DriverHandleImp::allocHostMem(ze_host_mem_alloc_flag_t flags, size_t
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
}
NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::HOST_UNIFIED_MEMORY);
unifiedMemoryProperties.subdeviceBitfield = this->devices[0]->getNEODevice()->getDeviceBitfield();
auto usmPtr = svmAllocsManager->createUnifiedMemoryAllocation(0u, size, unifiedMemoryProperties);
@@ -127,6 +129,7 @@ ze_result_t DriverHandleImp::allocDeviceMem(ze_device_handle_t hDevice, ze_devic
NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::DEVICE_UNIFIED_MEMORY);
unifiedMemoryProperties.allocationFlags.flags.shareable = 1u;
unifiedMemoryProperties.device = Device::fromHandle(hDevice)->getNEODevice();
unifiedMemoryProperties.subdeviceBitfield = Device::fromHandle(hDevice)->getNEODevice()->getDeviceBitfield();
void *usmPtr =
svmAllocsManager->createUnifiedMemoryAllocation(Device::fromHandle(hDevice)->getRootDeviceIndex(),
size, unifiedMemoryProperties);
@@ -141,21 +144,26 @@ ze_result_t DriverHandleImp::allocDeviceMem(ze_device_handle_t hDevice, ze_devic
ze_result_t DriverHandleImp::allocSharedMem(ze_device_handle_t hDevice, ze_device_mem_alloc_flag_t deviceFlags,
ze_host_mem_alloc_flag_t hostFlags, size_t size, size_t alignment,
void **ptr) {
NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::SHARED_UNIFIED_MEMORY);
ze_device_handle_t device;
if (hDevice == nullptr) {
hDevice = this->devices[0];
device = this->devices[0];
unifiedMemoryProperties.device = nullptr;
} else {
device = hDevice;
unifiedMemoryProperties.device = Device::fromHandle(device)->getNEODevice();
}
if (size > this->devices[0]->getDeviceInfo().maxMemAllocSize) {
*ptr = nullptr;
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
}
NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::SHARED_UNIFIED_MEMORY);
unifiedMemoryProperties.device = Device::fromHandle(hDevice)->getNEODevice();
unifiedMemoryProperties.subdeviceBitfield = Device::fromHandle(device)->getNEODevice()->getDeviceBitfield();
auto usmPtr =
svmAllocsManager->createSharedUnifiedMemoryAllocation(Device::fromHandle(hDevice)->getRootDeviceIndex(),
svmAllocsManager->createSharedUnifiedMemoryAllocation(Device::fromHandle(device)->getRootDeviceIndex(),
size,
unifiedMemoryProperties,
static_cast<void *>(L0::Device::fromHandle(hDevice)));
static_cast<void *>(L0::Device::fromHandle(device)));
if (usmPtr == nullptr) {
return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY;

View File

@@ -5,6 +5,9 @@
*
*/
#include "shared/test/unit_test/helpers/debug_manager_state_restore.h"
#include "opencl/test/unit_test/mocks/mock_memory_manager.h"
#include "test.h"
#include "level_zero/core/test/unit_tests/fixtures/device_fixture.h"
@@ -134,5 +137,130 @@ TEST_F(MemoryTest, givenSharedPointerAndDeviceHandleAsNullThenDriverReturnsSucce
ASSERT_EQ(result, ZE_RESULT_SUCCESS);
}
TEST_F(MemoryTest, givenNoDeviceWhenAllocatingSharedMemoryThenDeviceInAllocationIsNullptr) {
size_t size = 10;
size_t alignment = 1u;
void *ptr = nullptr;
ASSERT_NE(nullptr, device->toHandle());
ze_result_t result = driverHandle->allocSharedMem(nullptr,
ZE_DEVICE_MEM_ALLOC_FLAG_DEFAULT,
ZE_HOST_MEM_ALLOC_FLAG_DEFAULT,
size, alignment, &ptr);
auto alloc = driverHandle->svmAllocsManager->getSVMAlloc(ptr);
EXPECT_EQ(alloc->device, nullptr);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
EXPECT_NE(nullptr, ptr);
result = driverHandle->freeMem(ptr);
ASSERT_EQ(result, ZE_RESULT_SUCCESS);
}
struct MemoryBitfieldTest : testing::Test {
void SetUp() override {
auto executionEnvironment = new NEO::ExecutionEnvironment();
executionEnvironment->prepareRootDeviceEnvironments(1);
executionEnvironment->rootDeviceEnvironments[0]->setHwInfo(defaultHwInfo.get());
memoryManager = new NEO::MockMemoryManager(*executionEnvironment);
executionEnvironment->memoryManager.reset(memoryManager);
neoDevice = new NEO::RootDevice(executionEnvironment, 0);
static_cast<RootDevice *>(neoDevice)->createDeviceImpl();
NEO::DeviceVector devices;
devices.push_back(std::unique_ptr<NEO::Device>(neoDevice));
driverHandle = std::make_unique<Mock<L0::DriverHandleImp>>();
driverHandle->initialize(std::move(devices));
device = driverHandle->devices[0];
memoryManager->recentlyPassedDeviceBitfield = {};
ASSERT_NE(nullptr, driverHandle->devices[0]->toHandle());
EXPECT_NE(neoDevice->getDeviceBitfield(), memoryManager->recentlyPassedDeviceBitfield);
}
void TearDown() override {
auto result = driverHandle->freeMem(ptr);
ASSERT_EQ(result, ZE_RESULT_SUCCESS);
}
std::unique_ptr<Mock<L0::DriverHandleImp>> driverHandle;
NEO::Device *neoDevice = nullptr;
L0::Device *device = nullptr;
NEO::MockMemoryManager *memoryManager;
size_t size = 10;
size_t alignment = 1u;
void *ptr = nullptr;
};
TEST_F(MemoryBitfieldTest, givenDeviceWithValidBitfieldWhenAllocatingDeviceMemoryThenPassProperBitfield) {
auto result = driverHandle->allocDeviceMem(device->toHandle(),
ZE_DEVICE_MEM_ALLOC_FLAG_DEFAULT,
size, alignment, &ptr);
EXPECT_EQ(neoDevice->getDeviceBitfield(), memoryManager->recentlyPassedDeviceBitfield);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
EXPECT_NE(nullptr, ptr);
}
TEST_F(MemoryBitfieldTest, givenDeviceWithValidBitfieldWhenAllocatingHostMemoryThenPassProperBitfield) {
auto result = driverHandle->allocHostMem(ZE_HOST_MEM_ALLOC_FLAG_DEFAULT,
size, alignment, &ptr);
EXPECT_EQ(neoDevice->getDeviceBitfield(), memoryManager->recentlyPassedDeviceBitfield);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
EXPECT_NE(nullptr, ptr);
}
TEST(MemoryBitfieldTests, givenDeviceWithValidBitfieldWhenAllocatingSharedMemoryThenPassProperBitfield) {
DebugManagerStateRestore restorer;
size_t size = 10;
size_t alignment = 1u;
void *ptr = nullptr;
auto executionEnvironment = new NEO::ExecutionEnvironment();
executionEnvironment->prepareRootDeviceEnvironments(2);
for (size_t i = 0; i < executionEnvironment->rootDeviceEnvironments.size(); i++) {
executionEnvironment->rootDeviceEnvironments[i]->setHwInfo(defaultHwInfo.get());
}
auto memoryManager = new NEO::MockMemoryManager(*executionEnvironment);
executionEnvironment->memoryManager.reset(memoryManager);
NEO::Device *neoDevice0 = new NEO::RootDevice(executionEnvironment, 0);
static_cast<NEO::RootDevice *>(neoDevice0)->createDeviceImpl();
DebugManager.flags.CreateMultipleSubDevices.set(4);
NEO::Device *neoDevice1 = new NEO::RootDevice(executionEnvironment, 1);
static_cast<NEO::RootDevice *>(neoDevice1)->createDeviceImpl();
NEO::DeviceVector devices;
devices.push_back(std::unique_ptr<NEO::Device>(neoDevice0));
devices.push_back(std::unique_ptr<NEO::Device>(neoDevice1));
auto driverHandle = std::make_unique<Mock<L0::DriverHandleImp>>();
driverHandle->initialize(std::move(devices));
memoryManager->recentlyPassedDeviceBitfield = {};
ASSERT_NE(nullptr, driverHandle->devices[1]->toHandle());
EXPECT_NE(neoDevice0->getDeviceBitfield(), neoDevice1->getDeviceBitfield());
EXPECT_NE(neoDevice0->getDeviceBitfield(), memoryManager->recentlyPassedDeviceBitfield);
auto result = driverHandle->allocSharedMem(nullptr,
ZE_DEVICE_MEM_ALLOC_FLAG_DEFAULT,
ZE_HOST_MEM_ALLOC_FLAG_DEFAULT,
size, alignment, &ptr);
EXPECT_EQ(neoDevice0->getDeviceBitfield(), memoryManager->recentlyPassedDeviceBitfield);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
EXPECT_NE(nullptr, ptr);
result = driverHandle->freeMem(ptr);
ASSERT_EQ(result, ZE_RESULT_SUCCESS);
memoryManager->recentlyPassedDeviceBitfield = {};
EXPECT_NE(neoDevice1->getDeviceBitfield(), memoryManager->recentlyPassedDeviceBitfield);
result = driverHandle->allocSharedMem(driverHandle->devices[1]->toHandle(),
ZE_DEVICE_MEM_ALLOC_FLAG_DEFAULT,
ZE_HOST_MEM_ALLOC_FLAG_DEFAULT,
size, alignment, &ptr);
EXPECT_EQ(neoDevice1->getDeviceBitfield(), memoryManager->recentlyPassedDeviceBitfield);
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
EXPECT_NE(nullptr, ptr);
result = driverHandle->freeMem(ptr);
ASSERT_EQ(result, ZE_RESULT_SUCCESS);
}
} // namespace ult
} // namespace L0