diff --git a/level_zero/core/source/cmdlist/cmdlist_hw.inl b/level_zero/core/source/cmdlist/cmdlist_hw.inl index 9b30ba1c6b..e70269a002 100644 --- a/level_zero/core/source/cmdlist/cmdlist_hw.inl +++ b/level_zero/core/source/cmdlist/cmdlist_hw.inl @@ -720,7 +720,7 @@ ze_result_t CommandListCoreFamily::appendMemoryCopyRegion(void *d bool hostPointerNeedsFlush = false; bool dstAllocFound = device->getDriverHandle()->findAllocationDataForRange(alignedDstPtr, dstSize, &allocData); if (dstAllocFound == false) { - auto dstAlloc = device->getDriverHandle()->allocateManagedMemoryFromHostPtr(device, alignedDstPtr, dstSize, this); + auto dstAlloc = device->allocateManagedMemoryFromHostPtr(alignedDstPtr, dstSize, this); commandContainer.getDeallocationContainer().push_back(dstAlloc); hostPointerNeedsFlush = true; } else { @@ -733,7 +733,7 @@ ze_result_t CommandListCoreFamily::appendMemoryCopyRegion(void *d bool srcAllocFound = device->getDriverHandle()->findAllocationDataForRange(alignedSrcPtr, srcSize, nullptr); if (srcAllocFound == false) { - auto srcAlloc = device->getDriverHandle()->allocateManagedMemoryFromHostPtr(device, alignedSrcPtr, dstSize, this); + auto srcAlloc = device->allocateManagedMemoryFromHostPtr(alignedSrcPtr, dstSize, this); commandContainer.getDeallocationContainer().push_back(srcAlloc); } @@ -937,9 +937,8 @@ ze_result_t CommandListCoreFamily::appendMemoryFill(void *ptr, } else { builtinFunction = device->getBuiltinFunctionsLib()->getFunction(Builtin::FillBufferSSHOffset); - auto patternAlloc = device->getDriverHandle()->allocateManagedMemoryFromHostPtr(device, - reinterpret_cast(srcPtr), - srcOffset + patternSize, this); + auto patternAlloc = device->allocateManagedMemoryFromHostPtr(reinterpret_cast(srcPtr), + srcOffset + patternSize, this); if (patternAlloc == nullptr) { DEBUG_BREAK_IF(true); return ZE_RESULT_ERROR_UNKNOWN; @@ -1047,7 +1046,7 @@ inline AlignedAllocationData CommandListCoreFamily::getAlignedAll bool hostPointerNeedsFlush = false; if (srcAllocFound == false) { - alloc = device->getDriverHandle()->allocateMemoryFromHostPtr(device, buffer, bufferSize); + alloc = device->allocateMemoryFromHostPtr(buffer, bufferSize); hostPtrMap.insert(std::make_pair(buffer, alloc)); alignedPtr = static_cast(alloc->getGpuAddress() - offset); diff --git a/level_zero/core/source/device/device.h b/level_zero/core/source/device/device.h index 76401ef460..bfa5db8540 100644 --- a/level_zero/core/source/device/device.h +++ b/level_zero/core/source/device/device.h @@ -116,6 +116,11 @@ struct Device : _ze_device_handle_t { return getNEODevice() ? reinterpret_cast(getNEODevice()->getDebugger()) : nullptr; } virtual NEO::GraphicsAllocation *getDebugSurface() const = 0; + + virtual NEO::GraphicsAllocation *allocateManagedMemoryFromHostPtr(void *buffer, + size_t size, struct CommandList *commandList) = 0; + + virtual NEO::GraphicsAllocation *allocateMemoryFromHostPtr(const void *buffer, size_t size) = 0; }; } // namespace L0 diff --git a/level_zero/core/source/device/device_imp.cpp b/level_zero/core/source/device/device_imp.cpp index a29deb5d03..f51b38fe24 100644 --- a/level_zero/core/source/device/device_imp.cpp +++ b/level_zero/core/source/device/device_imp.cpp @@ -462,8 +462,8 @@ ze_result_t DeviceImp::registerCLMemory(cl_context context, cl_mem mem, void **p NEO::GraphicsAllocation *graphicsAllocation = memObj->getGraphicsAllocation(); DEBUG_BREAK_IF(graphicsAllocation == nullptr); - auto allocation = getDriverHandle()->allocateManagedMemoryFromHostPtr( - this, graphicsAllocation->getUnderlyingBuffer(), + auto allocation = allocateManagedMemoryFromHostPtr( + graphicsAllocation->getUnderlyingBuffer(), graphicsAllocation->getUnderlyingBufferSize(), nullptr); *ptr = allocation->getUnderlyingBuffer(); @@ -622,4 +622,74 @@ const NEO::DeviceInfo &DeviceImp::getDeviceInfo() const { NEO::Device *DeviceImp::getNEODevice() { return neoDevice; } + +NEO::GraphicsAllocation *DeviceImp::allocateManagedMemoryFromHostPtr(void *buffer, size_t size, struct CommandList *commandList) { + char *baseAddress = reinterpret_cast(buffer); + NEO::GraphicsAllocation *allocation = nullptr; + bool allocFound = false; + std::vector allocDataArray = driverHandle->findAllocationsWithinRange(buffer, size, &allocFound); + if (allocFound) { + return allocDataArray[0]->gpuAllocation; + } + + if (!allocDataArray.empty()) { + UNRECOVERABLE_IF(commandList == nullptr); + for (auto allocData : allocDataArray) { + allocation = allocData->gpuAllocation; + char *allocAddress = reinterpret_cast(allocation->getGpuAddress()); + size_t allocSize = allocData->size; + + driverHandle->getSvmAllocsManager()->getSVMAllocs()->remove(*allocData); + neoDevice->getMemoryManager()->freeGraphicsMemory(allocation); + commandList->eraseDeallocationContainerEntry(allocation); + commandList->eraseResidencyContainerEntry(allocation); + + if (allocAddress < baseAddress) { + buffer = reinterpret_cast(allocAddress); + baseAddress += size; + size = ptrDiff(baseAddress, allocAddress); + baseAddress = reinterpret_cast(buffer); + } else { + allocAddress += allocSize; + baseAddress += size; + if (allocAddress > baseAddress) { + baseAddress = reinterpret_cast(buffer); + size = ptrDiff(allocAddress, baseAddress); + } else { + baseAddress = reinterpret_cast(buffer); + } + } + } + } + + allocation = neoDevice->getMemoryManager()->allocateGraphicsMemoryWithProperties( + {getRootDeviceIndex(), false, size, NEO::GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, false}, + buffer); + + if (allocation == nullptr) { + return allocation; + } + + NEO::SvmAllocationData allocData; + allocData.gpuAllocation = allocation; + allocData.cpuAllocation = nullptr; + allocData.size = size; + allocData.memoryType = InternalMemoryType::NOT_SPECIFIED; + allocData.device = nullptr; + driverHandle->getSvmAllocsManager()->getSVMAllocs()->insert(allocData); + + return allocation; +} + +NEO::GraphicsAllocation *DeviceImp::allocateMemoryFromHostPtr(const void *buffer, size_t size) { + NEO::AllocationProperties properties = {getRootDeviceIndex(), false, size, NEO::GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, false}; + properties.flags.flushL3RequiredForRead = properties.flags.flushL3RequiredForWrite = true; + auto allocation = neoDevice->getMemoryManager()->allocateGraphicsMemoryWithProperties(properties, + buffer); + + UNRECOVERABLE_IF(allocation == nullptr); + + return allocation; +} + } // namespace L0 diff --git a/level_zero/core/source/device/device_imp.h b/level_zero/core/source/device/device_imp.h index 814ae909ff..1b50d262c1 100644 --- a/level_zero/core/source/device/device_imp.h +++ b/level_zero/core/source/device/device_imp.h @@ -80,6 +80,8 @@ struct DeviceImp : public Device { NEO::GraphicsAllocation *getDebugSurface() const override { return debugSurface; } void setDebugSurface(NEO::GraphicsAllocation *debugSurface) { this->debugSurface = debugSurface; }; ~DeviceImp() override; + NEO::GraphicsAllocation *allocateManagedMemoryFromHostPtr(void *buffer, size_t size, struct CommandList *commandList) override; + NEO::GraphicsAllocation *allocateMemoryFromHostPtr(const void *buffer, size_t size) override; NEO::Device *neoDevice = nullptr; bool isSubdevice = false; diff --git a/level_zero/core/source/driver/driver_handle.h b/level_zero/core/source/driver/driver_handle.h index ef63d9d93a..61b349be14 100644 --- a/level_zero/core/source/driver/driver_handle.h +++ b/level_zero/core/source/driver/driver_handle.h @@ -51,9 +51,6 @@ struct DriverHandle : _ze_driver_handle_t { ze_event_pool_handle_t *phEventPool) = 0; virtual ze_result_t openEventPoolIpcHandle(ze_ipc_event_pool_handle_t hIpc, ze_event_pool_handle_t *phEventPool) = 0; virtual ze_result_t checkMemoryAccessFromDevice(Device *device, const void *ptr) = 0; - virtual NEO::GraphicsAllocation *allocateManagedMemoryFromHostPtr(Device *device, void *buffer, - size_t size, struct CommandList *commandList) = 0; - virtual NEO::GraphicsAllocation *allocateMemoryFromHostPtr(Device *device, const void *buffer, size_t size) = 0; virtual bool findAllocationDataForRange(const void *buffer, size_t size, NEO::SvmAllocationData **allocData) = 0; diff --git a/level_zero/core/source/driver/driver_handle_imp.cpp b/level_zero/core/source/driver/driver_handle_imp.cpp index 8269adb54b..5c28a98608 100644 --- a/level_zero/core/source/driver/driver_handle_imp.cpp +++ b/level_zero/core/source/driver/driver_handle_imp.cpp @@ -213,76 +213,6 @@ std::vector DriverHandleImp::findAllocationsWithinRang return allocDataArray; } -NEO::GraphicsAllocation *DriverHandleImp::allocateManagedMemoryFromHostPtr(Device *device, void *buffer, - size_t size, struct CommandList *commandList) { - char *baseAddress = reinterpret_cast(buffer); - NEO::GraphicsAllocation *allocation = nullptr; - bool allocFound = false; - std::vector allocDataArray = findAllocationsWithinRange(buffer, size, &allocFound); - if (allocFound) { - return allocDataArray[0]->gpuAllocation; - } - - if (!allocDataArray.empty()) { - UNRECOVERABLE_IF(commandList == nullptr); - for (auto allocData : allocDataArray) { - allocation = allocData->gpuAllocation; - char *allocAddress = reinterpret_cast(allocation->getGpuAddress()); - size_t allocSize = allocData->size; - - device->getDriverHandle()->getSvmAllocsManager()->getSVMAllocs()->remove(*allocData); - memoryManager->freeGraphicsMemory(allocation); - commandList->eraseDeallocationContainerEntry(allocation); - commandList->eraseResidencyContainerEntry(allocation); - - if (allocAddress < baseAddress) { - buffer = reinterpret_cast(allocAddress); - baseAddress += size; - size = ptrDiff(baseAddress, allocAddress); - baseAddress = reinterpret_cast(buffer); - } else { - allocAddress += allocSize; - baseAddress += size; - if (allocAddress > baseAddress) { - baseAddress = reinterpret_cast(buffer); - size = ptrDiff(allocAddress, baseAddress); - } else { - baseAddress = reinterpret_cast(buffer); - } - } - } - } - - allocation = memoryManager->allocateGraphicsMemoryWithProperties( - {0u, false, size, NEO::GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, false}, - buffer); - - if (allocation == nullptr) { - return allocation; - } - - NEO::SvmAllocationData allocData; - allocData.gpuAllocation = allocation; - allocData.cpuAllocation = nullptr; - allocData.size = size; - allocData.memoryType = InternalMemoryType::NOT_SPECIFIED; - allocData.device = nullptr; - svmAllocsManager->getSVMAllocs()->insert(allocData); - - return allocation; -} - -NEO::GraphicsAllocation *DriverHandleImp::allocateMemoryFromHostPtr(Device *device, const void *buffer, size_t size) { - NEO::AllocationProperties properties = {0u, false, size, NEO::GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, false}; - properties.flags.flushL3RequiredForRead = properties.flags.flushL3RequiredForWrite = true; - auto allocation = memoryManager->allocateGraphicsMemoryWithProperties(properties, - buffer); - - UNRECOVERABLE_IF(allocation == nullptr); - - return allocation; -} - ze_result_t DriverHandleImp::createEventPool(const ze_event_pool_desc_t *desc, uint32_t numDevices, ze_device_handle_t *phDevices, diff --git a/level_zero/core/source/driver/driver_handle_imp.h b/level_zero/core/source/driver/driver_handle_imp.h index 68c069c492..256dc8d848 100644 --- a/level_zero/core/source/driver/driver_handle_imp.h +++ b/level_zero/core/source/driver/driver_handle_imp.h @@ -50,9 +50,6 @@ struct DriverHandleImp : public DriverHandle { ze_result_t checkMemoryAccessFromDevice(Device *device, const void *ptr) override; NEO::SVMAllocsManager *getSvmAllocsManager() override; ze_result_t initialize(std::vector> devices); - NEO::GraphicsAllocation *allocateManagedMemoryFromHostPtr(Device *device, void *buffer, - size_t size, struct CommandList *commandList) override; - NEO::GraphicsAllocation *allocateMemoryFromHostPtr(Device *device, const void *buffer, size_t size) override; bool findAllocationDataForRange(const void *buffer, size_t size, NEO::SvmAllocationData **allocData) override; diff --git a/level_zero/core/test/unit_tests/mocks/CMakeLists.txt b/level_zero/core/test/unit_tests/mocks/CMakeLists.txt index 1144446912..6d6b2db807 100644 --- a/level_zero/core/test/unit_tests/mocks/CMakeLists.txt +++ b/level_zero/core/test/unit_tests/mocks/CMakeLists.txt @@ -16,6 +16,8 @@ set(L0_MOCKS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/mock_device.cpp ${CMAKE_CURRENT_SOURCE_DIR}/mock_driver.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_driver.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/mock_driver_handle.h + ${CMAKE_CURRENT_SOURCE_DIR}/mock_driver_handle.cpp ${CMAKE_CURRENT_SOURCE_DIR}/mock_memory_manager.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_memory_manager.cpp ) diff --git a/level_zero/core/test/unit_tests/mocks/mock_device.h b/level_zero/core/test/unit_tests/mocks/mock_device.h index 25771e0cd4..fb23021458 100644 --- a/level_zero/core/test/unit_tests/mocks/mock_device.h +++ b/level_zero/core/test/unit_tests/mocks/mock_device.h @@ -109,6 +109,8 @@ struct Mock : public Device { MOCK_METHOD0(getNEODevice, NEO::Device *()); MOCK_METHOD0(activateMetricGroups, void()); MOCK_CONST_METHOD0(getDebugSurface, NEO::GraphicsAllocation *()); + MOCK_METHOD3(allocateManagedMemoryFromHostPtr, NEO::GraphicsAllocation *(void *buffer, size_t size, struct L0::CommandList *commandList)); + MOCK_METHOD2(allocateMemoryFromHostPtr, NEO::GraphicsAllocation *(const void *buffer, size_t size)); }; template <> diff --git a/level_zero/core/test/unit_tests/mocks/mock_driver_handle.cpp b/level_zero/core/test/unit_tests/mocks/mock_driver_handle.cpp new file mode 100644 index 0000000000..2c5211c470 --- /dev/null +++ b/level_zero/core/test/unit_tests/mocks/mock_driver_handle.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2019-2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "mock_driver_handle.h" + +namespace L0 { +namespace ult { + +using MockDriverHandle = Mock; +using namespace testing; +using ::testing::Invoke; +using ::testing::Return; + +Mock::Mock() { + EXPECT_CALL(*this, getDevice) + .WillRepeatedly(testing::Invoke(this, &MockDriverHandle::doGetDevice)); + EXPECT_CALL(*this, getMemoryManager) + .WillRepeatedly(Invoke(this, &MockDriverHandle::doGetMemoryManager)); + EXPECT_CALL(*this, getSvmAllocsManager) + .WillRepeatedly(Invoke(this, &MockDriverHandle::doGetSvmAllocManager)); + EXPECT_CALL(*this, allocHostMem) + .WillRepeatedly(Invoke(this, &MockDriverHandle::doAllocHostMem)); + EXPECT_CALL(*this, allocDeviceMem) + .WillRepeatedly(Invoke(this, &MockDriverHandle::doAllocDeviceMem)); + EXPECT_CALL(*this, freeMem) + .WillRepeatedly(Invoke(this, &MockDriverHandle::doFreeMem)); +}; + +NEO::MemoryManager *Mock::doGetMemoryManager() { + return memoryManager; +} + +NEO::SVMAllocsManager *Mock::doGetSvmAllocManager() { + return svmAllocsManager; +} + +ze_result_t Mock::doGetDevice(uint32_t *pCount, ze_device_handle_t *phDevices) { + if (*pCount == 0) { // User wants to know number of devices + *pCount = this->num_devices; + return ZE_RESULT_SUCCESS; + } + + if (phDevices == nullptr) // User is expected to allocate space + return ZE_RESULT_ERROR_INVALID_ARGUMENT; + + phDevices[0] = &this->device; + + return ZE_RESULT_SUCCESS; +} + +ze_result_t Mock::doAllocHostMem(ze_host_mem_alloc_flag_t flags, size_t size, size_t alignment, + void **ptr) { + NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::HOST_UNIFIED_MEMORY); + + auto allocation = svmAllocsManager->createUnifiedMemoryAllocation(0u, size, unifiedMemoryProperties); + + if (allocation == nullptr) { + return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY; + } + + *ptr = allocation; + + return ZE_RESULT_SUCCESS; +} + +ze_result_t Mock::doAllocDeviceMem(ze_device_handle_t hDevice, ze_device_mem_alloc_flag_t flags, size_t size, size_t alignment, void **ptr) { + NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::DEVICE_UNIFIED_MEMORY); + + auto allocation = svmAllocsManager->createUnifiedMemoryAllocation(0u, size, unifiedMemoryProperties); + + if (allocation == nullptr) { + return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY; + } + + *ptr = allocation; + + return ZE_RESULT_SUCCESS; +} + +ze_result_t Mock::doFreeMem(const void *ptr) { + auto allocation = svmAllocsManager->getSVMAllocs()->get(ptr); + if (allocation == nullptr) { + return ZE_RESULT_ERROR_INVALID_ARGUMENT; + } + svmAllocsManager->freeSVMAlloc(const_cast(ptr)); + if (svmAllocsManager->getSvmMapOperation(ptr)) { + svmAllocsManager->removeSvmMapOperation(ptr); + } + return ZE_RESULT_SUCCESS; +} + +void Mock::setupDevices(std::vector> neoDevices) { + this->numDevices = static_cast(neoDevices.size()); + for (auto &neoDevice : neoDevices) { + auto device = Device::create(this, neoDevice.release()); + this->devices.push_back(device); + } +} + +Mock::~Mock(){}; + +} // namespace ult +} // namespace L0 diff --git a/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h b/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h new file mode 100644 index 0000000000..08051975c9 --- /dev/null +++ b/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2019-2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "level_zero/core/source/driver/driver_handle_imp.h" +#include "level_zero/core/test/unit_tests/mock.h" +#include "level_zero/core/test/unit_tests/mocks/mock_device.h" +#include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.h" +#include "level_zero/core/test/unit_tests/white_box.h" + +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Winconsistent-missing-override" +#endif + +namespace L0 { +namespace ult { + +template <> +struct WhiteBox<::L0::DriverHandleImp> : public ::L0::DriverHandleImp { +}; + +using DriverHandle = WhiteBox<::L0::DriverHandleImp>; + +template <> +struct Mock : public DriverHandleImp { + Mock(); + ~Mock() override; + + MOCK_METHOD2(getDevice, ze_result_t(uint32_t *pCount, ze_device_handle_t *phDevices)); + MOCK_METHOD1(getProperties, ze_result_t(ze_driver_properties_t *properties)); + MOCK_METHOD1(getApiVersion, ze_result_t(ze_api_version_t *version)); + MOCK_METHOD1(getIPCProperties, ze_result_t(ze_driver_ipc_properties_t *pIPCProperties)); + MOCK_METHOD3(getMemAllocProperties, ze_result_t(const void *ptr, + ze_memory_allocation_properties_t *pMemAllocProperties, + ze_device_handle_t *phDevice)); + + MOCK_METHOD0(getMemoryManager, NEO::MemoryManager *()); + MOCK_METHOD1(setMemoryManager, void(NEO::MemoryManager *)); + MOCK_METHOD3(getMemAddressRange, ze_result_t(const void *ptr, void **pBase, size_t *pSize)); + MOCK_METHOD2(getIpcMemHandle, ze_result_t(const void *ptr, ze_ipc_mem_handle_t *pIpcHandle)); + MOCK_METHOD1(closeIpcMemHandle, ze_result_t(const void *ptr)); + MOCK_METHOD4(openIpcMemHandle, ze_result_t(ze_device_handle_t hDevice, ze_ipc_mem_handle_t handle, + ze_ipc_memory_flag_t flags, void **ptr)); + MOCK_METHOD4(createEventPool, ze_result_t(const ze_event_pool_desc_t *desc, + uint32_t numDevices, + ze_device_handle_t *phDevices, + ze_event_pool_handle_t *phEventPool)); + MOCK_METHOD4(allocHostMem, ze_result_t(ze_host_mem_alloc_flag_t flags, size_t size, size_t alignment, void **ptr)); + MOCK_METHOD5(allocDeviceMem, ze_result_t(ze_device_handle_t hDevice, ze_device_mem_alloc_flag_t flags, size_t size, size_t alignment, void **ptr)); + MOCK_METHOD1(freeMem, ze_result_t(const void *ptr)); + + MOCK_METHOD0(getSvmAllocsManager, NEO::SVMAllocsManager *()); + uint32_t num_devices = 1; + Mock device; + + void setupDevices(std::vector> devices); + + ze_result_t doFreeMem(const void *ptr); + ze_result_t doGetDevice(uint32_t *pCount, ze_device_handle_t *phDevices); + NEO::MemoryManager *doGetMemoryManager(); + NEO::SVMAllocsManager *doGetSvmAllocManager(); + ze_result_t doAllocHostMem(ze_host_mem_alloc_flag_t flags, size_t size, size_t alignment, void **ptr); + ze_result_t doAllocDeviceMem(ze_device_handle_t hDevice, ze_device_mem_alloc_flag_t flags, size_t size, size_t alignment, void **ptr); +}; + +} // namespace ult +} // namespace L0 + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif diff --git a/level_zero/core/test/unit_tests/sources/device/CMakeLists.txt b/level_zero/core/test/unit_tests/sources/device/CMakeLists.txt new file mode 100644 index 0000000000..e317829302 --- /dev/null +++ b/level_zero/core/test/unit_tests/sources/device/CMakeLists.txt @@ -0,0 +1,10 @@ +# +# Copyright (C) 2020 Intel Corporation +# +# SPDX-License-Identifier: MIT +# + +target_sources(${TARGET_NAME} PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/test_device.cpp +) \ No newline at end of file diff --git a/level_zero/core/test/unit_tests/sources/device/test_device.cpp b/level_zero/core/test/unit_tests/sources/device/test_device.cpp new file mode 100644 index 0000000000..7cd9a4b4bf --- /dev/null +++ b/level_zero/core/test/unit_tests/sources/device/test_device.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "shared/test/unit_test/helpers/debug_manager_state_restore.h" + +#include "opencl/test/unit_test/mocks/mock_device.h" + +#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.h" + +#include "gtest/gtest.h" + +#include + +namespace L0 { +namespace ult { + +struct DeviceTest : public ::testing::Test { + void SetUp() override { + DebugManager.flags.CreateMultipleRootDevices.set(numRootDevices); + neoDevice = NEO::MockDevice::createWithNewExecutionEnvironment(NEO::defaultHwInfo.get(), rootDeviceIndex); + NEO::DeviceVector devices; + devices.push_back(std::unique_ptr(neoDevice)); + driverHandle = std::make_unique>(); + driverHandle->initialize(std::move(devices)); + device = driverHandle->devices[0]; + } + + DebugManagerStateRestore restorer; + std::unique_ptr> driverHandle; + NEO::Device *neoDevice = nullptr; + L0::Device *device = nullptr; + const uint32_t rootDeviceIndex = 1u; + const uint32_t numRootDevices = 2u; +}; + +TEST_F(DeviceTest, givenEmptySVmAllocStorageWhenAllocateManagedMemoryFromHostPtrThenBufferHostAllocationIsCreated) { + int data; + auto allocation = device->allocateManagedMemoryFromHostPtr(&data, sizeof(data), nullptr); + EXPECT_NE(nullptr, allocation); + EXPECT_EQ(NEO::GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY, allocation->getAllocationType()); + EXPECT_EQ(rootDeviceIndex, allocation->getRootDeviceIndex()); + neoDevice->getMemoryManager()->freeGraphicsMemory(allocation); +} + +TEST_F(DeviceTest, givenEmptySVmAllocStorageWhenAllocateMemoryFromHostPtrThenValidExternalHostPtrAllocationIsCreated) { + DebugManager.flags.EnableHostPtrTracking.set(0); + constexpr auto dataSize = 1024u; + auto data = std::make_unique(dataSize); + + constexpr auto allocationSize = sizeof(int) * dataSize; + + auto allocation = device->allocateMemoryFromHostPtr(data.get(), allocationSize); + EXPECT_NE(nullptr, allocation); + EXPECT_EQ(NEO::GraphicsAllocation::AllocationType::EXTERNAL_HOST_PTR, allocation->getAllocationType()); + EXPECT_EQ(rootDeviceIndex, allocation->getRootDeviceIndex()); + + auto alignedPtr = alignDown(data.get(), MemoryConstants::pageSize); + auto offsetInPage = ptrDiff(data.get(), alignedPtr); + + EXPECT_EQ(allocation->getAllocationOffset(), offsetInPage); + EXPECT_EQ(allocation->getUnderlyingBufferSize(), allocationSize); + EXPECT_EQ(allocation->isFlushL3Required(), true); + + neoDevice->getMemoryManager()->freeGraphicsMemory(allocation); +} + +} // namespace ult +} // namespace L0 \ No newline at end of file