diff --git a/level_zero/core/source/context/context.h b/level_zero/core/source/context/context.h index ad10ed2496..de5eae731c 100644 --- a/level_zero/core/source/context/context.h +++ b/level_zero/core/source/context/context.h @@ -128,6 +128,10 @@ struct Context : _ze_context_handle_t { virtual ze_result_t createImage(ze_device_handle_t hDevice, const ze_image_desc_t *desc, ze_image_handle_t *phImage) = 0; + virtual NEO::MemoryManager *getMemoryManager() = 0; + virtual void setMemoryManager(NEO::MemoryManager *memoryManager) = 0; + virtual NEO::SVMAllocsManager *getSvmAllocsManager() = 0; + virtual void setSvmAllocsManager(NEO::SVMAllocsManager *) = 0; static Context *fromHandle(ze_context_handle_t handle) { return static_cast(handle); } inline ze_context_handle_t toHandle() { return this; } diff --git a/level_zero/core/source/context/context_imp.cpp b/level_zero/core/source/context/context_imp.cpp index c68d949bd6..ccbea6c6ff 100644 --- a/level_zero/core/source/context/context_imp.cpp +++ b/level_zero/core/source/context/context_imp.cpp @@ -18,6 +18,15 @@ namespace L0 { ze_result_t ContextImp::destroy() { + if (this->svmAllocsManager) { + delete this->svmAllocsManager; + this->svmAllocsManager = nullptr; + } + + if (this->driverHandle->mainContext == this) { + this->driverHandle->mainContext = nullptr; + } + delete this; return ZE_RESULT_SUCCESS; @@ -78,8 +87,8 @@ ze_result_t ContextImp::allocHostMem(const ze_host_mem_alloc_desc_t *hostDesc, this->rootDeviceIndices, this->deviceBitfields); - auto usmPtr = this->driverHandle->svmAllocsManager->createHostUnifiedMemoryAllocation(size, - unifiedMemoryProperties); + auto usmPtr = this->driverHandle->getSvmAllocsManager()->createHostUnifiedMemoryAllocation(size, + unifiedMemoryProperties); if (usmPtr == nullptr) { return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY; } @@ -157,7 +166,7 @@ ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice, } void *usmPtr = - this->driverHandle->svmAllocsManager->createUnifiedMemoryAllocation(size, unifiedMemoryProperties); + this->driverHandle->getSvmAllocsManager()->createUnifiedMemoryAllocation(size, unifiedMemoryProperties); if (usmPtr == nullptr) { return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY; } @@ -216,9 +225,9 @@ ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice, } auto usmPtr = - this->driverHandle->svmAllocsManager->createSharedUnifiedMemoryAllocation(size, - unifiedMemoryProperties, - static_cast(neoDevice->getSpecializedDevice())); + this->driverHandle->getSvmAllocsManager()->createSharedUnifiedMemoryAllocation(size, + unifiedMemoryProperties, + static_cast(neoDevice->getSpecializedDevice())); if (usmPtr == nullptr) { return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY; @@ -229,7 +238,7 @@ ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice, } ze_result_t ContextImp::freeMem(const void *ptr) { - auto allocation = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr); + auto allocation = this->driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr); if (allocation == nullptr) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } @@ -244,13 +253,13 @@ ze_result_t ContextImp::freeMem(const void *ptr) { auto peerAllocData = &iter->second; auto peerAlloc = peerAllocData->gpuAllocations.getDefaultGraphicsAllocation(); auto peerPtr = reinterpret_cast(peerAlloc->getGpuAddress()); - this->driverHandle->svmAllocsManager->freeSVMAlloc(peerPtr); + this->driverHandle->getSvmAllocsManager()->freeSVMAlloc(peerPtr); } } - this->driverHandle->svmAllocsManager->freeSVMAlloc(const_cast(ptr)); - if (this->driverHandle->svmAllocsManager->getSvmMapOperation(ptr)) { - this->driverHandle->svmAllocsManager->removeSvmMapOperation(ptr); + this->driverHandle->getSvmAllocsManager()->freeSVMAlloc(const_cast(ptr)); + if (this->driverHandle->getSvmAllocsManager()->getSvmMapOperation(ptr)) { + this->driverHandle->getSvmAllocsManager()->removeSvmMapOperation(ptr); } return ZE_RESULT_SUCCESS; } @@ -326,7 +335,7 @@ ze_result_t ContextImp::evictImage(ze_device_handle_t hDevice, ze_image_handle_t ze_result_t ContextImp::getMemAddressRange(const void *ptr, void **pBase, size_t *pSize) { - NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr); + NEO::SvmAllocationData *allocData = this->driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr); if (allocData) { NEO::GraphicsAllocation *alloc; alloc = allocData->gpuAllocations.getDefaultGraphicsAllocation(); @@ -351,7 +360,7 @@ ze_result_t ContextImp::closeIpcMemHandle(const void *ptr) { ze_result_t ContextImp::getIpcMemHandle(const void *ptr, ze_ipc_mem_handle_t *pIpcHandle) { - NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr); + NEO::SvmAllocationData *allocData = this->driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr); if (allocData) { uint64_t handle = allocData->gpuAllocations.getDefaultGraphicsAllocation()->peekInternalHandle(this->driverHandle->getMemoryManager()); memcpy_s(reinterpret_cast(pIpcHandle->data), @@ -385,7 +394,7 @@ ze_result_t ContextImp::openIpcMemHandle(ze_device_handle_t hDevice, ze_result_t ContextImp::getMemAllocProperties(const void *ptr, ze_memory_allocation_properties_t *pMemAllocProperties, ze_device_handle_t *phDevice) { - auto alloc = driverHandle->svmAllocsManager->getSVMAlloc(ptr); + auto alloc = driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr); if (nullptr == alloc) { pMemAllocProperties->type = ZE_MEMORY_TYPE_UNKNOWN; return ZE_RESULT_SUCCESS; diff --git a/level_zero/core/source/context/context_imp.h b/level_zero/core/source/context/context_imp.h index f77b24d3d2..1f2ad8751b 100644 --- a/level_zero/core/source/context/context_imp.h +++ b/level_zero/core/source/context/context_imp.h @@ -115,11 +115,28 @@ struct ContextImp : Context { return devices; } + NEO::MemoryManager *getMemoryManager() override { + return this->memoryManager; + } + void setMemoryManager(NEO::MemoryManager *memoryManager) override { + this->memoryManager = memoryManager; + } + NEO::SVMAllocsManager *getSvmAllocsManager() override { + return this->svmAllocsManager; + } + + void setSvmAllocsManager(NEO::SVMAllocsManager *svmManager) override { + this->svmAllocsManager = svmManager; + } + std::set rootDeviceIndices = {}; std::map deviceBitfields; bool isDeviceDefinedForThisContext(Device *inDevice); + NEO::MemoryManager *memoryManager = nullptr; + NEO::SVMAllocsManager *svmAllocsManager = nullptr; + protected: std::map devices; DriverHandleImp *driverHandle = nullptr; diff --git a/level_zero/core/source/driver/driver_handle.h b/level_zero/core/source/driver/driver_handle.h index 4ed9d17838..ab2b123bb3 100644 --- a/level_zero/core/source/driver/driver_handle.h +++ b/level_zero/core/source/driver/driver_handle.h @@ -47,6 +47,7 @@ struct DriverHandle : _ze_driver_handle_t { bool *allocationRangeCovered) = 0; virtual NEO::SVMAllocsManager *getSvmAllocsManager() = 0; + virtual void setSvmAllocsManager(NEO::SVMAllocsManager *) = 0; virtual ze_result_t sysmanEventsListen(uint32_t timeout, uint32_t count, zes_device_handle_t *phDevices, uint32_t *pNumDeviceEvents, zes_event_type_flags_t *pEvents) = 0; virtual ze_result_t sysmanEventsListenEx(uint64_t timeout, uint32_t count, zes_device_handle_t *phDevices, diff --git a/level_zero/core/source/driver/driver_handle_imp.cpp b/level_zero/core/source/driver/driver_handle_imp.cpp index 30038ddf81..a8402e5b10 100644 --- a/level_zero/core/source/driver/driver_handle_imp.cpp +++ b/level_zero/core/source/driver/driver_handle_imp.cpp @@ -53,13 +53,22 @@ ze_result_t DriverHandleImp::createContext(const ze_context_desc_t *desc, } } + bool multiOsContextDriver = false; for (auto devicePair : context->getDevices()) { auto neoDevice = devicePair.second->getNEODevice(); + multiOsContextDriver |= devicePair.second->isMultiDeviceCapable(); context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); } + if (this->mainContext == nullptr) { + this->mainContext = context; + + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), multiOsContextDriver)); + } + return ZE_RESULT_SUCCESS; } @@ -72,7 +81,11 @@ void DriverHandleImp::setMemoryManager(NEO::MemoryManager *memoryManager) { } NEO::SVMAllocsManager *DriverHandleImp::getSvmAllocsManager() { - return this->svmAllocsManager; + return this->mainContext->getSvmAllocsManager(); +} + +void DriverHandleImp::setSvmAllocsManager(NEO::SVMAllocsManager *svmManager) { + this->mainContext->setSvmAllocsManager(svmManager); } ze_result_t DriverHandleImp::getApiVersion(ze_api_version_t *version) { @@ -133,10 +146,6 @@ DriverHandleImp::~DriverHandleImp() { for (auto &device : this->devices) { delete device; } - if (this->svmAllocsManager) { - delete this->svmAllocsManager; - this->svmAllocsManager = nullptr; - } } ze_result_t DriverHandleImp::initialize(std::vector> neoDevices) { @@ -153,9 +162,6 @@ ze_result_t DriverHandleImp::initialize(std::vector if (this->memoryManager == nullptr) { this->memoryManager = neoDevice->getMemoryManager(); - if (this->memoryManager == nullptr) { - return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY; - } } const auto rootDeviceIndex = neoDevice->getRootDeviceIndex(); @@ -189,11 +195,6 @@ ze_result_t DriverHandleImp::initialize(std::vector return ZE_RESULT_ERROR_UNINITIALIZED; } - this->svmAllocsManager = new NEO::SVMAllocsManager(memoryManager, multiOsContextDriver); - if (this->svmAllocsManager == nullptr) { - return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY; - } - this->numDevices = static_cast(this->devices.size()); extensionFunctionsLookupMap = getExtensionFunctionsLookupMap(); @@ -250,8 +251,8 @@ bool DriverHandleImp::findAllocationDataForRange(const void *buffer, NEO::SvmAllocationData **allocData) { // Make sure the host buffer does not overlap any existing allocation const char *baseAddress = reinterpret_cast(buffer); - NEO::SvmAllocationData *beginAllocData = svmAllocsManager->getSVMAlloc(baseAddress); - NEO::SvmAllocationData *endAllocData = svmAllocsManager->getSVMAlloc(baseAddress + size - 1); + NEO::SvmAllocationData *beginAllocData = this->getSvmAllocsManager()->getSVMAlloc(baseAddress); + NEO::SvmAllocationData *endAllocData = this->getSvmAllocsManager()->getSVMAlloc(baseAddress + size - 1); if (allocData) { if (beginAllocData) { @@ -275,8 +276,8 @@ std::vector DriverHandleImp::findAllocationsWithinRang std::vector allocDataArray; const char *baseAddress = reinterpret_cast(buffer); // Check if the host buffer overlaps any existing allocation - NEO::SvmAllocationData *beginAllocData = svmAllocsManager->getSVMAlloc(baseAddress); - NEO::SvmAllocationData *endAllocData = svmAllocsManager->getSVMAlloc(baseAddress + size - 1); + NEO::SvmAllocationData *beginAllocData = this->getSvmAllocsManager()->getSVMAlloc(baseAddress); + NEO::SvmAllocationData *endAllocData = this->getSvmAllocsManager()->getSVMAlloc(baseAddress + size - 1); // Add the allocation that matches the beginning address if (beginAllocData) { diff --git a/level_zero/core/source/driver/driver_handle_imp.h b/level_zero/core/source/driver/driver_handle_imp.h index e73ed72f3d..32268ab336 100644 --- a/level_zero/core/source/driver/driver_handle_imp.h +++ b/level_zero/core/source/driver/driver_handle_imp.h @@ -10,11 +10,13 @@ #include "shared/source/os_interface/os_library.h" #include "level_zero/api/extensions/public/ze_exp_ext.h" +#include "level_zero/core/source/context/context.h" #include "level_zero/core/source/driver/driver_handle.h" #include "level_zero/core/source/get_extension_function_lookup_map.h" namespace L0 { class HostPointerManager; +struct Context; struct DriverHandleImp : public DriverHandle { ~DriverHandleImp() override; @@ -38,6 +40,7 @@ struct DriverHandleImp : public DriverHandle { ze_result_t openEventPoolIpcHandle(ze_ipc_event_pool_handle_t hIpc, ze_event_pool_handle_t *phEventPool) override; ze_result_t checkMemoryAccessFromDevice(Device *device, const void *ptr) override; NEO::SVMAllocsManager *getSvmAllocsManager() override; + void setSvmAllocsManager(NEO::SVMAllocsManager *) override; ze_result_t initialize(std::vector> neoDevices); bool findAllocationDataForRange(const void *buffer, size_t size, @@ -91,10 +94,11 @@ struct DriverHandleImp : public DriverHandle { uint64_t uuidTimestamp = 0u; NEO::MemoryManager *memoryManager = nullptr; - NEO::SVMAllocsManager *svmAllocsManager = nullptr; uint32_t numDevices = 0; + Context *mainContext = nullptr; + std::set rootDeviceIndices = {}; std::map deviceBitfields; diff --git a/level_zero/core/source/event/event.cpp b/level_zero/core/source/event/event.cpp index e9ce50e9b3..29552df251 100644 --- a/level_zero/core/source/event/event.cpp +++ b/level_zero/core/source/event/event.cpp @@ -96,7 +96,7 @@ ze_result_t EventPoolImp::initialize(DriverHandle *driver, Context *context, uin alignedSize, allocationType, false, - (deviceBitfield.count() > 1) && driverHandleImp->svmAllocsManager->getMultiOsContextSupport(), + (deviceBitfield.count() > 1) && driverHandleImp->getSvmAllocsManager()->getMultiOsContextSupport(), deviceBitfield}; unifiedMemoryProperties.flags.isUSMHostAllocation = true; unifiedMemoryProperties.flags.isUSMDeviceAllocation = false; diff --git a/level_zero/core/source/memory/memory.cpp b/level_zero/core/source/memory/memory.cpp index daaae4db61..1bc17fc58c 100644 --- a/level_zero/core/source/memory/memory.cpp +++ b/level_zero/core/source/memory/memory.cpp @@ -51,7 +51,7 @@ void *DriverHandleImp::importFdHandle(ze_device_handle_t hDevice, ze_ipc_memory_ } ze_result_t DriverHandleImp::checkMemoryAccessFromDevice(Device *device, const void *ptr) { - auto allocation = svmAllocsManager->getSVMAlloc(ptr); + auto allocation = this->getSvmAllocsManager()->getSVMAlloc(ptr); if (allocation == nullptr) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } diff --git a/level_zero/core/test/unit_tests/fixtures/cmdlist_fixture.h b/level_zero/core/test/unit_tests/fixtures/cmdlist_fixture.h index 48c19a88fc..06fde406c2 100644 --- a/level_zero/core/test/unit_tests/fixtures/cmdlist_fixture.h +++ b/level_zero/core/test/unit_tests/fixtures/cmdlist_fixture.h @@ -31,17 +31,23 @@ class CommandListFixture : public DeviceFixture { eventDesc.wait = 0; eventDesc.signal = 0; - eventPool = std::unique_ptr(EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc)); - event = std::unique_ptr(Event::create(eventPool.get(), &eventDesc, device)); + eventPool = EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc); + event = Event::create(eventPool, &eventDesc, device); } void TearDown() override { + if (event) { + event->destroy(); + } + if (eventPool) { + eventPool->destroy(); + } DeviceFixture::TearDown(); } std::unique_ptr commandList; - std::unique_ptr eventPool; - std::unique_ptr event; + EventPool *eventPool = nullptr; + Event *event = nullptr; }; } // namespace ult diff --git a/level_zero/core/test/unit_tests/fixtures/device_fixture.h b/level_zero/core/test/unit_tests/fixtures/device_fixture.h index 715a0a1359..43cf908720 100644 --- a/level_zero/core/test/unit_tests/fixtures/device_fixture.h +++ b/level_zero/core/test/unit_tests/fixtures/device_fixture.h @@ -15,7 +15,10 @@ #include "opencl/test/unit_test/mocks/mock_compilers.h" #include "level_zero/core/source/context/context_imp.h" -#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.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" namespace L0 { struct Context; diff --git a/level_zero/core/test/unit_tests/fixtures/host_pointer_manager_fixture.h b/level_zero/core/test/unit_tests/fixtures/host_pointer_manager_fixture.h index 2b212761e3..07875da62a 100644 --- a/level_zero/core/test/unit_tests/fixtures/host_pointer_manager_fixture.h +++ b/level_zero/core/test/unit_tests/fixtures/host_pointer_manager_fixture.h @@ -19,7 +19,6 @@ #include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" #include "level_zero/core/test/unit_tests/mocks/mock_built_ins.h" -#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.h" #include "level_zero/core/test/unit_tests/mocks/mock_host_pointer_manager.h" namespace L0 { @@ -39,29 +38,29 @@ struct HostPointerManagerFixure { devices.push_back(std::unique_ptr(neoDevice)); DebugManager.flags.EnableHostPointerImport.set(1); - hostDriverHandle = std::make_unique(); + hostDriverHandle = std::make_unique(); hostDriverHandle->initialize(std::move(devices)); + + ze_context_desc_t desc; + ze_result_t ret = hostDriverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, ret); + context = L0::Context::fromHandle(hContext); + device = hostDriverHandle->devices[0]; EXPECT_NE(nullptr, hostDriverHandle->hostPointerManager.get()); openHostPointerManager = static_cast(hostDriverHandle->hostPointerManager.get()); heapPointer = hostDriverHandle->getMemoryManager()->allocateSystemMemory(heapSize, MemoryConstants::pageSize); ASSERT_NE(nullptr, heapPointer); - - ze_context_desc_t desc; - ze_result_t ret = hostDriverHandle->createContext(&desc, 0u, nullptr, &hContext); - EXPECT_EQ(ZE_RESULT_SUCCESS, ret); - context = L0::Context::fromHandle(hContext); } void TearDown() { - context->destroy(); - hostDriverHandle->getMemoryManager()->freeSystemMemory(heapPointer); + context->destroy(); } DebugManagerStateRestore debugRestore; - std::unique_ptr hostDriverHandle; + std::unique_ptr hostDriverHandle; L0::ult::HostPointerManager *openHostPointerManager = nullptr; NEO::MockDevice *neoDevice = nullptr; diff --git a/level_zero/core/test/unit_tests/fixtures/module_fixture.h b/level_zero/core/test/unit_tests/fixtures/module_fixture.h index 1443eee547..b27912f1eb 100644 --- a/level_zero/core/test/unit_tests/fixtures/module_fixture.h +++ b/level_zero/core/test/unit_tests/fixtures/module_fixture.h @@ -147,11 +147,11 @@ struct ModuleImmutableDataFixture : public DeviceFixture { ModuleBuildLog *moduleBuildLog = nullptr; - module = std::make_unique(device, - moduleBuildLog, - ModuleType::User, - perHwThreadPrivateMemorySize, - mockKernelImmData); + module = new MockModule(device, + moduleBuildLog, + ModuleType::User, + perHwThreadPrivateMemorySize, + mockKernelImmData); module->type = isInternal ? ModuleType::Builtin : ModuleType::User; bool result = module->initialize(&moduleDesc, device->getNEODevice()); @@ -165,13 +165,16 @@ struct ModuleImmutableDataFixture : public DeviceFixture { } void TearDown() override { + if (module) { + module->destroy(); + } DeviceFixture::TearDown(); } const std::string binaryFilename = "test_kernel"; const std::string kernelName = "test"; const uint32_t numKernelArguments = 6; - std::unique_ptr module; + MockModule *module = nullptr; MockImmutableMemoryManager *memoryManager; }; @@ -201,27 +204,36 @@ struct ModuleFixture : public DeviceFixture { ModuleBuildLog *moduleBuildLog = nullptr; - module.reset(Module::create(device, &moduleDesc, moduleBuildLog, type)); + if (module) { + module->destroy(); + } + module = Module::create(device, &moduleDesc, moduleBuildLog, type); } void createKernel() { ze_kernel_desc_t desc = {}; desc.pKernelName = kernelName.c_str(); - kernel = std::make_unique>(); - kernel->module = module.get(); + kernel = new WhiteBox<::L0::Kernel>(); + kernel->module = module; kernel->initialize(&desc); } void TearDown() override { + if (kernel) { + kernel->destroy(); + } + if (module) { + module->destroy(); + } DeviceFixture::TearDown(); } const std::string binaryFilename = "test_kernel"; const std::string kernelName = "test"; const uint32_t numKernelArguments = 6; - std::unique_ptr module; - std::unique_ptr> kernel; + L0::Module *module = nullptr; + WhiteBox<::L0::Kernel> *kernel = nullptr; }; struct MultiDeviceModuleFixture : public MultiDeviceFixture { @@ -248,29 +260,35 @@ struct MultiDeviceModuleFixture : public MultiDeviceFixture { ModuleBuildLog *moduleBuildLog = nullptr; auto device = driverHandle->devices[rootDeviceIndex]; - modules[rootDeviceIndex].reset(Module::create(device, - &moduleDesc, - moduleBuildLog, ModuleType::User)); + modules[rootDeviceIndex] = Module::create(device, + &moduleDesc, + moduleBuildLog, ModuleType::User); } void createKernel(uint32_t rootDeviceIndex) { ze_kernel_desc_t desc = {}; desc.pKernelName = kernelName.c_str(); - kernel = std::make_unique>(); - kernel->module = modules[rootDeviceIndex].get(); + kernel = new WhiteBox<::L0::Kernel>(); + kernel->module = modules[rootDeviceIndex]; kernel->initialize(&desc); } void TearDown() override { + if (kernel) { + kernel->destroy(); + } + for (auto &module : modules) { + module->destroy(); + } MultiDeviceFixture::TearDown(); } const std::string binaryFilename = "test_kernel"; const std::string kernelName = "test"; const uint32_t numKernelArguments = 6; - std::vector> modules; - std::unique_ptr> kernel; + std::vector modules; + WhiteBox<::L0::Kernel> *kernel = nullptr; }; struct ImportHostPointerModuleFixture : public ModuleFixture { diff --git a/level_zero/core/test/unit_tests/gen11/test_cmdqueue_thread_arbitration_policy_gen11.cpp b/level_zero/core/test/unit_tests/gen11/test_cmdqueue_thread_arbitration_policy_gen11.cpp index 07c717ebd0..be38968d9b 100644 --- a/level_zero/core/test/unit_tests/gen11/test_cmdqueue_thread_arbitration_policy_gen11.cpp +++ b/level_zero/core/test/unit_tests/gen11/test_cmdqueue_thread_arbitration_policy_gen11.cpp @@ -13,6 +13,7 @@ #include "opencl/test/unit_test/mocks/mock_compilers.h" #include "test.h" +#include "level_zero/core/source/context/context_imp.h" #include "level_zero/core/source/driver/driver_imp.h" #include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" #include "level_zero/core/test/unit_tests/mocks/mock_built_ins.h" @@ -38,9 +39,14 @@ struct CommandQueueThreadArbitrationPolicyTests : public ::testing::Test { auto driverHandleUlt = whitebox_cast(DriverHandle::create(std::move(devices), L0EnvVariables{}, &returnValue)); driverHandle.reset(driverHandleUlt); - ASSERT_NE(nullptr, driverHandle); + ze_context_handle_t hContext; + ze_context_desc_t desc = {}; + returnValue = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, returnValue); + context = static_cast(Context::fromHandle(hContext)); + ze_device_handle_t hDevice; uint32_t count = 1; ze_result_t result = driverHandle->getDevice(&count, &hDevice); @@ -63,6 +69,7 @@ struct CommandQueueThreadArbitrationPolicyTests : public ::testing::Test { void TearDown() override { commandList->destroy(); commandQueue->destroy(); + context->destroy(); L0::GlobalDriver = nullptr; } @@ -72,6 +79,7 @@ struct CommandQueueThreadArbitrationPolicyTests : public ::testing::Test { std::unique_ptr> driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device; + L0::ContextImp *context = nullptr; }; HWTEST2_F(CommandQueueThreadArbitrationPolicyTests, diff --git a/level_zero/core/test/unit_tests/gen12lp/test_events_gen12lp.cpp b/level_zero/core/test/unit_tests/gen12lp/test_events_gen12lp.cpp index 4cc8bbea23..3a3ed9008b 100644 --- a/level_zero/core/test/unit_tests/gen12lp/test_events_gen12lp.cpp +++ b/level_zero/core/test/unit_tests/gen12lp/test_events_gen12lp.cpp @@ -32,18 +32,20 @@ struct TimestampEvent : public Test { eventDesc.signal = 0; eventDesc.wait = 0; - eventPool = std::unique_ptr(L0::EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc)); + eventPool = L0::EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc); ASSERT_NE(nullptr, eventPool); - event = std::unique_ptr(L0::Event::create(eventPool.get(), &eventDesc, device)); + event = L0::Event::create(eventPool, &eventDesc, device); ASSERT_NE(nullptr, event); } void TearDown() override { + event->destroy(); + eventPool->destroy(); DeviceFixture::TearDown(); } - std::unique_ptr eventPool; - std::unique_ptr event; + L0::EventPool *eventPool = nullptr; + L0::Event *event = nullptr; }; GEN12LPTEST_F(TimestampEvent, givenEventTimestampsWhenQueryKernelTimestampThenCorrectDataAreSet) { diff --git a/level_zero/core/test/unit_tests/gen9/test_cmdqueue_gen9.cpp b/level_zero/core/test/unit_tests/gen9/test_cmdqueue_gen9.cpp index bcd31f195f..7817807c8d 100644 --- a/level_zero/core/test/unit_tests/gen9/test_cmdqueue_gen9.cpp +++ b/level_zero/core/test/unit_tests/gen9/test_cmdqueue_gen9.cpp @@ -14,6 +14,7 @@ #include "test.h" #include "level_zero/core/source/cmdqueue/cmdqueue_imp.h" +#include "level_zero/core/source/context/context_imp.h" #include "level_zero/core/source/driver/driver_handle_imp.h" #include "level_zero/core/source/driver/driver_imp.h" #include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" @@ -40,9 +41,14 @@ struct CommandQueueThreadArbitrationPolicyTests : public ::testing::Test { auto driverHandleUlt = whitebox_cast(DriverHandle::create(std::move(devices), L0EnvVariables{}, &returnValue)); driverHandle.reset(driverHandleUlt); - ASSERT_NE(nullptr, driverHandle); + ze_context_handle_t hContext; + ze_context_desc_t desc = {}; + returnValue = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, returnValue); + context = static_cast(Context::fromHandle(hContext)); + ze_device_handle_t hDevice; uint32_t count = 1; ze_result_t result = driverHandle->getDevice(&count, &hDevice); @@ -65,6 +71,7 @@ struct CommandQueueThreadArbitrationPolicyTests : public ::testing::Test { void TearDown() override { commandList->destroy(); commandQueue->destroy(); + context->destroy(); L0::GlobalDriver = nullptr; } @@ -74,6 +81,7 @@ struct CommandQueueThreadArbitrationPolicyTests : public ::testing::Test { std::unique_ptr> driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device; + L0::ContextImp *context = nullptr; }; HWTEST2_F(CommandQueueThreadArbitrationPolicyTests, diff --git a/level_zero/core/test/unit_tests/mocks/CMakeLists.txt b/level_zero/core/test/unit_tests/mocks/CMakeLists.txt index 4866bf7b80..17ff27c814 100644 --- a/level_zero/core/test/unit_tests/mocks/CMakeLists.txt +++ b/level_zero/core/test/unit_tests/mocks/CMakeLists.txt @@ -25,8 +25,6 @@ set(L0_MOCKS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/mock_device_recompile_built_ins.h ${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_event.h ${CMAKE_CURRENT_SOURCE_DIR}/mock_event.cpp ${CMAKE_CURRENT_SOURCE_DIR}/mock_gmm_resource_info_l0.cpp 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 deleted file mode 100644 index dcec1ef5ed..0000000000 --- a/level_zero/core/test/unit_tests/mocks/mock_driver_handle.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (C) 2019-2021 Intel Corporation - * - * SPDX-License-Identifier: MIT - * - */ - -#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.h" - -#include "shared/test/common/mocks/mock_graphics_allocation.h" - -#include "level_zero/core/source/driver/host_pointer_manager.h" - -namespace L0 { -namespace ult { - -using MockDriverHandle = Mock; -using namespace testing; -using ::testing::Invoke; -using ::testing::Return; - -Mock::Mock() = default; - -NEO::MemoryManager *Mock::getMemoryManager() { - return memoryManager; -} - -NEO::SVMAllocsManager *Mock::getSvmAllocManager() { - return svmAllocsManager; -} - -ze_result_t Mock::getDevice(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::allocDeviceMem(ze_device_handle_t hDevice, const ze_device_mem_alloc_desc_t *deviceDesc, - size_t size, size_t alignment, void **ptr) { - NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::DEVICE_UNIFIED_MEMORY, rootDeviceIndices, deviceBitfields); - - auto allocation = svmAllocsManager->createUnifiedMemoryAllocation(size, unifiedMemoryProperties); - - if (allocation == nullptr) { - return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY; - } - - *ptr = allocation; - - return ZE_RESULT_SUCCESS; -} - -ze_result_t Mock::freeMem(const void *ptr) { - auto allocation = svmAllocsManager->getSVMAlloc(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) { - ze_result_t returnValue = ZE_RESULT_SUCCESS; - this->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); - this->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); - auto device = Device::create(this, neoDevice.release(), std::numeric_limits::max(), false, &returnValue); - 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 deleted file mode 100644 index baf3e496c7..0000000000 --- a/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (C) 2019-2021 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" - -namespace L0 { -namespace ult { - -template <> -struct WhiteBox<::L0::DriverHandle> : public ::L0::DriverHandleImp { - using ::L0::DriverHandleImp::enableProgramDebugging; -}; - -using DriverHandle = WhiteBox<::L0::DriverHandle>; -#define ADDMETHOD_NOBASE(funcName, retType, defaultReturn, funcParams) \ - retType funcName##Result = defaultReturn; \ - uint32_t funcName##Called = 0u; \ - retType funcName funcParams override { \ - funcName##Called++; \ - return funcName##Result; \ - } - -template <> -struct Mock : public DriverHandleImp { - Mock(); - ~Mock() override; - - ADDMETHOD_NOBASE(getProperties, ze_result_t, ZE_RESULT_SUCCESS, (ze_driver_properties_t * properties)) - ADDMETHOD_NOBASE(getApiVersion, ze_result_t, ZE_RESULT_SUCCESS, (ze_api_version_t * version)) - ADDMETHOD_NOBASE(getIPCProperties, ze_result_t, ZE_RESULT_SUCCESS, (ze_driver_ipc_properties_t * pIPCProperties)) - - uint32_t num_devices = 1; - Mock device; - - void setupDevices(std::vector> devices); - - ze_result_t freeMem(const void *ptr); - ze_result_t getDevice(uint32_t *pCount, - ze_device_handle_t *phDevices) override; - NEO::MemoryManager *getMemoryManager() override; - NEO::SVMAllocsManager *getSvmAllocManager(); - ze_result_t allocDeviceMem(ze_device_handle_t hDevice, - const ze_device_mem_alloc_desc_t *deviceDesc, - size_t size, - size_t alignment, - void **ptr); - - ze_result_t importExternalPointer(void *ptr, size_t size) override { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; - } - - ze_result_t releaseImportedPointer(void *ptr) override { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; - } - - ze_result_t getHostPointerBaseAddress(void *ptr, void **baseAddress) override { - return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE; - } - - NEO::GraphicsAllocation *findHostPointerAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) override { - return nullptr; - } - NEO::GraphicsAllocation *getDriverSystemMemoryAllocation(void *ptr, - size_t size, - uint32_t rootDeviceIndex, - uintptr_t *gpuAddress) override { - auto svmData = svmAllocsManager->getSVMAlloc(ptr); - if (svmData != nullptr) { - if (gpuAddress != nullptr) { - *gpuAddress = reinterpret_cast(ptr); - } - return svmData->gpuAllocations.getGraphicsAllocation(rootDeviceIndex); - } - return nullptr; - } -}; -#undef ADDMETHOD_NOBASE -} // namespace ult -} // namespace L0 diff --git a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_append_launch_kernel.cpp b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_append_launch_kernel.cpp index 0e1345168e..6ffb01a55b 100644 --- a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_append_launch_kernel.cpp +++ b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_append_launch_kernel.cpp @@ -200,7 +200,7 @@ HWTEST_F(CommandListAppendLaunchKernel, givenKernelWithPrintfUsedWhenAppendedToC EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_EQ(1u, commandList->getPrintfFunctionContainer().size()); - EXPECT_EQ(kernel.get(), commandList->getPrintfFunctionContainer()[0]); + EXPECT_EQ(kernel, commandList->getPrintfFunctionContainer()[0]); } HWTEST_F(CommandListAppendLaunchKernel, givenKernelWithPrintfUsedWhenAppendedToCommandListMultipleTimesThenKernelIsStoredOnce) { @@ -215,7 +215,7 @@ HWTEST_F(CommandListAppendLaunchKernel, givenKernelWithPrintfUsedWhenAppendedToC EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_EQ(1u, commandList->getPrintfFunctionContainer().size()); - EXPECT_EQ(kernel.get(), commandList->getPrintfFunctionContainer()[0]); + EXPECT_EQ(kernel, commandList->getPrintfFunctionContainer()[0]); result = commandList->appendLaunchKernel(kernel->toHandle(), &groupCount, nullptr, 0, nullptr); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -595,9 +595,9 @@ HWTEST_F(CommandListAppendLaunchKernel, givenIndirectDispatchWhenAppendingThenWo using MI_LOAD_REGISTER_REG = typename FamilyType::MI_LOAD_REGISTER_REG; using MI_LOAD_REGISTER_IMM = typename FamilyType::MI_LOAD_REGISTER_IMM; - Mock<::L0::Kernel> kernel; - kernel.descriptor.payloadMappings.dispatchTraits.numWorkGroups[0] = 2; - kernel.descriptor.payloadMappings.dispatchTraits.globalWorkSize[0] = 2; + Mock<::L0::Kernel> mockKernel; + mockKernel.descriptor.payloadMappings.dispatchTraits.numWorkGroups[0] = 2; + mockKernel.descriptor.payloadMappings.dispatchTraits.globalWorkSize[0] = 2; ze_result_t returnValue; std::unique_ptr commandList(L0::CommandList::create(productFamily, device, NEO::EngineGroupType::RenderCompute, returnValue)); @@ -606,7 +606,7 @@ HWTEST_F(CommandListAppendLaunchKernel, givenIndirectDispatchWhenAppendingThenWo auto result = context->allocDeviceMem(device->toHandle(), &deviceDesc, 16384u, 4096u, &alloc); ASSERT_EQ(ZE_RESULT_SUCCESS, result); - result = commandList->appendLaunchKernelIndirect(kernel.toHandle(), + result = commandList->appendLaunchKernelIndirect(mockKernel.toHandle(), static_cast(alloc), nullptr, 0, nullptr); EXPECT_EQ(ZE_RESULT_SUCCESS, result); diff --git a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_append_wait_on_events.cpp b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_append_wait_on_events.cpp index 3816d8e47f..ad7cd11317 100644 --- a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_append_wait_on_events.cpp +++ b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_append_wait_on_events.cpp @@ -112,7 +112,7 @@ HWTEST_F(CommandListAppendWaitOnEvent, givenEventWithWaitScopeFlagDeviceWhenAppe 0, ZE_EVENT_SCOPE_FLAG_DEVICE}; - auto event = std::unique_ptr(Event::create(eventPool.get(), &eventDesc, device)); + auto event = std::unique_ptr(Event::create(eventPool, &eventDesc, device)); ze_event_handle_t hEventHandle = event->toHandle(); auto result = commandList->appendWaitOnEvents(1, &hEventHandle); diff --git a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_blit.cpp b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_blit.cpp index fcdad32d98..1fd5d35af3 100644 --- a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_blit.cpp +++ b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_blit.cpp @@ -85,11 +85,19 @@ HWTEST2_F(AppendMemoryCopy, givenCopyOnlyCommandListWhenAppenBlitFillThenCopyBlt using GfxFamily = typename NEO::GfxFamilyMapper::GfxFamily; using XY_COLOR_BLT = typename GfxFamily::XY_COLOR_BLT; MockCommandListForMemFill commandList; + MockDriverHandle driverHandleMock; NEO::DeviceVector neoDevices; neoDevices.push_back(std::unique_ptr(neoDevice)); driverHandleMock.initialize(std::move(neoDevices)); device->setDriverHandle(&driverHandleMock); + + ze_context_handle_t hContext; + ze_context_desc_t desc; + ze_result_t res = driverHandleMock.createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + L0::ContextImp *contextDriverMock = static_cast(Context::fromHandle(hContext)); + commandList.initialize(device, NEO::EngineGroupType::Copy); uint16_t pattern = 1; void *ptr = reinterpret_cast(0x1234); @@ -99,6 +107,7 @@ HWTEST2_F(AppendMemoryCopy, givenCopyOnlyCommandListWhenAppenBlitFillThenCopyBlt cmdList, ptrOffset(commandList.commandContainer.getCommandStream()->getCpuBase(), 0), commandList.commandContainer.getCommandStream()->getUsed())); auto itor = find(cmdList.begin(), cmdList.end()); EXPECT_NE(cmdList.end(), itor); + contextDriverMock->destroy(); device->setDriverHandle(driverHandle.get()); } diff --git a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_fill.cpp b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_fill.cpp index 4cb01c6183..f93a7e036c 100644 --- a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_fill.cpp +++ b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_fill.cpp @@ -80,11 +80,18 @@ class AppendFillFixture : public DeviceFixture, public ::testing::Test { driverHandle = std::make_unique>(); driverHandle->initialize(std::move(devices)); device = driverHandle->devices[0]; + + ze_context_handle_t hContext; + ze_context_desc_t desc; + ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + context = static_cast(Context::fromHandle(hContext)); } void TearDown() override { delete[] immediateDstPtr; delete[] dstPtr; + context->destroy(); } std::unique_ptr> driverHandle; @@ -94,6 +101,7 @@ class AppendFillFixture : public DeviceFixture, public ::testing::Test { static constexpr size_t patternSize = 8; uint8_t *dstPtr = nullptr; uint8_t pattern[patternSize] = {1, 2, 3, 4}; + L0::ContextImp *context = nullptr; static constexpr size_t immediateAllocSize = 106; uint8_t immediatePattern = 4; diff --git a/level_zero/core/test/unit_tests/sources/context/test_context.cpp b/level_zero/core/test/unit_tests/sources/context/test_context.cpp index cb4b976ce9..330c939fc2 100644 --- a/level_zero/core/test/unit_tests/sources/context/test_context.cpp +++ b/level_zero/core/test/unit_tests/sources/context/test_context.cpp @@ -17,7 +17,6 @@ #include "level_zero/core/source/image/image.h" #include "level_zero/core/test/unit_tests/fixtures/device_fixture.h" #include "level_zero/core/test/unit_tests/fixtures/host_pointer_manager_fixture.h" -#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.h" #include "gtest/gtest.h" @@ -159,23 +158,9 @@ struct ContextHostAllocTests : public ::testing::Test { driverHandle = std::make_unique(); ze_result_t res = driverHandle->initialize(std::move(devices)); EXPECT_EQ(ZE_RESULT_SUCCESS, res); - - prevSvmAllocsManager = driverHandle->svmAllocsManager; - currSvmAllocsManager = new SVMAllocsManagerContextMock(driverHandle->memoryManager); - driverHandle->svmAllocsManager = currSvmAllocsManager; - - zeDevices.resize(numberOfDevicesInContext); - driverHandle->getDevice(&numberOfDevicesInContext, zeDevices.data()); - - for (uint32_t i = 0; i < numberOfDevicesInContext; i++) { - L0::DeviceImp *deviceImp = static_cast(L0::Device::fromHandle(zeDevices[i])); - currSvmAllocsManager->expectedRootDeviceIndexes.push_back(deviceImp->getRootDeviceIndex()); - } } void TearDown() override { - driverHandle->svmAllocsManager = prevSvmAllocsManager; - delete currSvmAllocsManager; } DebugManagerStateRestore restorer; @@ -189,6 +174,9 @@ struct ContextHostAllocTests : public ::testing::Test { TEST_F(ContextHostAllocTests, whenAllocatingHostMemoryOnlyIndexesOfDevicesWithinTheContextAreUsed) { + zeDevices.resize(numberOfDevicesInContext); + driverHandle->getDevice(&numberOfDevicesInContext, zeDevices.data()); + L0::ContextImp *context = nullptr; ze_context_handle_t hContext; ze_context_desc_t desc; @@ -199,6 +187,15 @@ TEST_F(ContextHostAllocTests, EXPECT_EQ(ZE_RESULT_SUCCESS, res); context = static_cast(Context::fromHandle(hContext)); + prevSvmAllocsManager = context->getSvmAllocsManager(); + currSvmAllocsManager = new SVMAllocsManagerContextMock(context->getMemoryManager()); + context->setSvmAllocsManager(currSvmAllocsManager); + + for (uint32_t i = 0; i < numberOfDevicesInContext; i++) { + L0::DeviceImp *deviceImp = static_cast(L0::Device::fromHandle(zeDevices[i])); + currSvmAllocsManager->expectedRootDeviceIndexes.push_back(deviceImp->getRootDeviceIndex()); + } + void *hostPtr = nullptr; ze_host_mem_alloc_desc_t hostDesc = {}; size_t size = 1024; @@ -209,6 +206,9 @@ TEST_F(ContextHostAllocTests, res = context->freeMem(hostPtr); EXPECT_EQ(ZE_RESULT_SUCCESS, res); + context->setSvmAllocsManager(prevSvmAllocsManager); + delete currSvmAllocsManager; + context->destroy(); } diff --git a/level_zero/core/test/unit_tests/sources/debugger/active_debugger_fixture.h b/level_zero/core/test/unit_tests/sources/debugger/active_debugger_fixture.h index f1ec675a50..6687c7f8ad 100644 --- a/level_zero/core/test/unit_tests/sources/debugger/active_debugger_fixture.h +++ b/level_zero/core/test/unit_tests/sources/debugger/active_debugger_fixture.h @@ -14,13 +14,13 @@ #include "opencl/test/unit_test/mocks/mock_source_level_debugger.h" #include "level_zero/core/source/cmdqueue/cmdqueue_hw.h" +#include "level_zero/core/source/context/context_imp.h" #include "level_zero/core/source/driver/driver_handle_imp.h" #include "level_zero/core/source/fence/fence.h" #include "level_zero/core/source/module/module.h" #include "level_zero/core/test/unit_tests/mocks/mock_built_ins.h" #include "level_zero/core/test/unit_tests/mocks/mock_device.h" #include "level_zero/core/test/unit_tests/mocks/mock_driver.h" -#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.h" #include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.h" namespace L0 { @@ -54,6 +54,12 @@ struct ActiveDebuggerFixture { ASSERT_NE(nullptr, driverHandle); + ze_context_handle_t hContext; + ze_context_desc_t desc; + ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + context = static_cast(Context::fromHandle(hContext)); + ze_device_handle_t hDevice; uint32_t count = 1; ze_result_t result = driverHandle->getDevice(&count, &hDevice); @@ -62,6 +68,7 @@ struct ActiveDebuggerFixture { ASSERT_NE(nullptr, deviceL0); } void TearDown() { // NOLINT(readability-identifier-naming) + context->destroy(); L0::GlobalDriver = nullptr; } @@ -70,6 +77,7 @@ struct ActiveDebuggerFixture { L0::Device *deviceL0; MockActiveSourceLevelDebugger *debugger = nullptr; HardwareInfo hwInfo; + L0::ContextImp *context = nullptr; }; } // namespace ult } // namespace L0 diff --git a/level_zero/core/test/unit_tests/sources/debugger/l0_debugger_fixture.h b/level_zero/core/test/unit_tests/sources/debugger/l0_debugger_fixture.h index df30b315ac..6457513aa1 100644 --- a/level_zero/core/test/unit_tests/sources/debugger/l0_debugger_fixture.h +++ b/level_zero/core/test/unit_tests/sources/debugger/l0_debugger_fixture.h @@ -38,15 +38,23 @@ struct L0DebuggerFixture { driverHandle->initialize(std::move(devices)); device = driverHandle->devices[0]; + + ze_context_handle_t hContext; + ze_context_desc_t desc; + ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + context = static_cast(Context::fromHandle(hContext)); } void TearDown() { + context->destroy(); } std::unique_ptr> driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device = nullptr; NEO::HardwareInfo hwInfo; + L0::ContextImp *context = nullptr; }; struct L0DebuggerHwFixture : public L0DebuggerFixture { 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 index 4e2ae799f4..3f37df1ccf 100644 --- a/level_zero/core/test/unit_tests/sources/device/test_device.cpp +++ b/level_zero/core/test/unit_tests/sources/device/test_device.cpp @@ -23,8 +23,11 @@ #include "level_zero/core/source/context/context_imp.h" #include "level_zero/core/source/driver/driver_handle_imp.h" #include "level_zero/core/source/driver/host_pointer_manager.h" +#include "level_zero/core/test/unit_tests/mock.h" #include "level_zero/core/test/unit_tests/mocks/mock_built_ins.h" -#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.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" #include "gtest/gtest.h" @@ -192,6 +195,16 @@ struct DeviceTest : public ::testing::Test { driverHandle = std::make_unique>(); driverHandle->initialize(std::move(devices)); device = driverHandle->devices[0]; + + ze_context_handle_t hContext; + ze_context_desc_t desc; + ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + context = static_cast(Context::fromHandle(hContext)); + } + + void TearDown() override { + context->destroy(); } DebugManagerStateRestore restorer; @@ -200,6 +213,7 @@ struct DeviceTest : public ::testing::Test { L0::Device *device = nullptr; const uint32_t rootDeviceIndex = 1u; const uint32_t numRootDevices = 2u; + L0::ContextImp *context = nullptr; }; TEST_F(DeviceTest, givenEmptySVmAllocStorageWhenAllocateManagedMemoryFromHostPtrThenBufferHostAllocationIsCreated) { @@ -248,12 +262,19 @@ struct DeviceHostPointerTest : public ::testing::Test { driverHandle = std::make_unique>(); driverHandle->initialize(std::move(devices)); + ze_context_handle_t hContext; + ze_context_desc_t desc; + ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + context = static_cast(Context::fromHandle(hContext)); + static_cast(driverHandle.get()->getMemoryManager())->isMockHostMemoryManager = true; static_cast(driverHandle.get()->getMemoryManager())->forceFailureInAllocationWithHostPointer = true; device = driverHandle->devices[0]; } void TearDown() override { + context->destroy(); } NEO::ExecutionEnvironment *executionEnvironment = nullptr; @@ -262,6 +283,7 @@ struct DeviceHostPointerTest : public ::testing::Test { L0::Device *device = nullptr; const uint32_t rootDeviceIndex = 1u; const uint32_t numRootDevices = 2u; + L0::ContextImp *context = nullptr; }; TEST_F(DeviceHostPointerTest, givenHostPointerNotAcceptedByKernelThenNewAllocationIsCreatedAndHostPointerCopied) { diff --git a/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp b/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp index 918c82e3fc..2ac10749aa 100644 --- a/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp +++ b/level_zero/core/test/unit_tests/sources/driver/test_driver.cpp @@ -165,10 +165,11 @@ TEST(DriverTest, givenNullEnvVariableWhenCreatingDriverThenEnableProgramDebuggin L0EnvVariables envVariables = {}; envVariables.programDebugging = false; - auto driverHandle = whitebox_cast(DriverHandle::create(std::move(devices), envVariables, &returnValue)); + auto driverHandle = DriverHandle::create(std::move(devices), envVariables, &returnValue); EXPECT_NE(nullptr, driverHandle); - EXPECT_FALSE(driverHandle->enableProgramDebugging); + DriverHandleImp *driverHandleImp = static_cast(driverHandle); + EXPECT_FALSE(driverHandleImp->enableProgramDebugging); delete driverHandle; L0::GlobalDriver = nullptr; @@ -262,10 +263,11 @@ TEST(DriverTest, givenProgramDebuggingEnvVarNonZeroWhenCreatingDriverThenEnableP L0EnvVariables envVariables = {}; envVariables.programDebugging = true; - auto driverHandle = whitebox_cast(DriverHandle::create(std::move(devices), envVariables, &returnValue)); + auto driverHandle = DriverHandle::create(std::move(devices), envVariables, &returnValue); EXPECT_NE(nullptr, driverHandle); - EXPECT_TRUE(driverHandle->enableProgramDebugging); + DriverHandleImp *driverHandleImp = static_cast(driverHandle); + EXPECT_TRUE(driverHandleImp->enableProgramDebugging); delete driverHandle; L0::GlobalDriver = nullptr; @@ -381,13 +383,21 @@ struct DriverHandleTest : public ::testing::Test { driverHandle = whitebox_cast(DriverHandle::create(std::move(devices), envVariables, &returnValue)); L0::GlobalDriverHandle = driverHandle; + + ze_context_handle_t hContext; + ze_context_desc_t desc; + ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + context = static_cast(Context::fromHandle(hContext)); } void TearDown() override { + context->destroy(); delete driverHandle; L0::GlobalDriver = nullptr; L0::GlobalDriverHandle = nullptr; } - L0::DriverHandle *driverHandle; + L0::DriverHandle *driverHandle = nullptr; + L0::ContextImp *context = nullptr; }; TEST_F(DriverHandleTest, givenInitializedDriverWhenZeDriverGetIsCalledThenDriverHandleCountIsObtained) { @@ -451,11 +461,9 @@ TEST_F(DriverHandleTest, givenInitializedDriverWhenZeDriverGetIsCalledThenGlobal } TEST_F(DriverHandleTest, givenInitializedDriverWhenGetDeviceIsCalledThenOneDeviceIsObtained) { - ze_result_t result; uint32_t count = 1; - ze_device_handle_t device; - result = driverHandle->getDevice(&count, &device); + ze_result_t result = driverHandle->getDevice(&count, &device); EXPECT_EQ(ZE_RESULT_SUCCESS, result); EXPECT_NE(nullptr, &device); } @@ -465,40 +473,37 @@ TEST_F(DriverHandleTest, givenValidDriverHandleWhenGetSvmAllocManagerIsCalledThe EXPECT_NE(nullptr, svmAllocsManager); } -TEST(zeDriverHandleGetProperties, whenZeDriverGetPropertiesIsCalledThenGetPropertiesIsCalled) { - ze_result_t result; - Mock driverHandle; - ze_driver_properties_t properties; - ze_result_t expectedResult = ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS; - - driverHandle.getPropertiesResult = expectedResult; - result = zeDriverGetProperties(driverHandle.toHandle(), &properties); - EXPECT_EQ(expectedResult, result); - EXPECT_EQ(1u, driverHandle.getPropertiesCalled); +TEST_F(DriverHandleTest, whenZeDriverGetPropertiesIsCalledThenSuccessIsReturned) { + ze_driver_properties_t properties = {}; + ze_result_t result = zeDriverGetProperties(driverHandle->toHandle(), &properties); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); } -TEST(zeDriverHandleGetApiVersion, whenZeDriverGetApiIsCalledThenGetApiVersionIsCalled) { - ze_result_t result; - Mock driverHandle; - ze_api_version_t version; - ze_result_t expectedResult = ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS; - - driverHandle.getApiVersionResult = expectedResult; - result = zeDriverGetApiVersion(driverHandle.toHandle(), &version); - EXPECT_EQ(expectedResult, result); - EXPECT_EQ(1u, driverHandle.getApiVersionCalled); +TEST_F(DriverHandleTest, whenZeDriverGetApiIsCalledThenSuccessIsReturned) { + ze_api_version_t version = {}; + ze_result_t result = zeDriverGetApiVersion(driverHandle->toHandle(), &version); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); } -TEST(zeDriverGetIpcProperties, whenZeDriverGetIpcPropertiesIsCalledThenGetIPCPropertiesIsCalled) { - ze_result_t result; - Mock driverHandle; - ze_driver_ipc_properties_t ipcProperties; - ze_result_t expectedResult = ZE_RESULT_ERROR_INSUFFICIENT_PERMISSIONS; - - driverHandle.getIPCPropertiesResult = expectedResult; - result = zeDriverGetIpcProperties(driverHandle.toHandle(), &ipcProperties); - EXPECT_EQ(expectedResult, result); - EXPECT_EQ(1u, driverHandle.getIPCPropertiesCalled); +TEST_F(DriverHandleTest, whenGettingApiVersionThenCorrectApiVersionIsReturned) { + ze_api_version_t version = {}; + ze_result_t result = driverHandle->getApiVersion(&version); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(ZE_API_VERSION_1_1, version); } + +TEST_F(DriverHandleTest, whenZeDriverGetIpcPropertiesIsCalledThenSuccessIsReturned) { + ze_driver_ipc_properties_t ipcProperties = {}; + ze_result_t result = zeDriverGetIpcProperties(driverHandle->toHandle(), &ipcProperties); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); +} + +TEST_F(DriverHandleTest, whenGettingIpcPropertiesThenCorrectPropertiesAreReturned) { + ze_driver_ipc_properties_t ipcProperties = {}; + ze_result_t result = driverHandle->getIPCProperties(&ipcProperties); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(ZE_IPC_PROPERTY_FLAG_MEMORY, ipcProperties.flags); +} + } // namespace ult } // namespace L0 diff --git a/level_zero/core/test/unit_tests/sources/event/test_event.cpp b/level_zero/core/test/unit_tests/sources/event/test_event.cpp index a3aac8dd7a..fe49608893 100644 --- a/level_zero/core/test/unit_tests/sources/event/test_event.cpp +++ b/level_zero/core/test/unit_tests/sources/event/test_event.cpp @@ -70,21 +70,32 @@ struct EventPoolFailTests : public ::testing::Test { devices.push_back(std::unique_ptr(neoDevice)); driverHandle = std::make_unique(); driverHandle->initialize(std::move(devices)); - prevMemoryManager = driverHandle->getMemoryManager(); - currMemoryManager = new MemoryManagerEventPoolFailMock(*neoDevice->executionEnvironment); - driverHandle->setMemoryManager(currMemoryManager); device = driverHandle->devices[0]; context = std::make_unique(driverHandle.get()); EXPECT_NE(context, nullptr); - context->getDevices().insert(std::make_pair(device->toHandle(), device)); - auto neoDevice = device->getNEODevice(); - context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); - context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + + { + context->getDevices().insert(std::make_pair(device->toHandle(), device)); + auto neoDevice = device->getNEODevice(); + context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); + context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + } + + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + + prevMemoryManager = context->getMemoryManager(); + currMemoryManager = new MemoryManagerEventPoolFailMock(*neoDevice->executionEnvironment); + context->setMemoryManager(currMemoryManager); + + driverHandle->mainContext = context.get(); + + driverHandle->setMemoryManager(context->getMemoryManager()); + driverHandle->setSvmAllocsManager(context->getSvmAllocsManager()); } void TearDown() override { - driverHandle->setMemoryManager(prevMemoryManager); + context->setMemoryManager(prevMemoryManager); delete currMemoryManager; } NEO::MemoryManager *prevMemoryManager = nullptr; @@ -315,18 +326,20 @@ class EventSynchronizeTest : public Test { eventDesc.signal = 0; eventDesc.wait = 0; - eventPool = std::unique_ptr(L0::EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc)); + eventPool = L0::EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc); ASSERT_NE(nullptr, eventPool); - event = std::unique_ptr(L0::Event::create(eventPool.get(), &eventDesc, device)); + event = L0::Event::create(eventPool, &eventDesc, device); ASSERT_NE(nullptr, event); } void TearDown() override { + event->destroy(); + eventPool->destroy(); DeviceFixture::TearDown(); } - std::unique_ptr eventPool = nullptr; - std::unique_ptr event; + L0::EventPool *eventPool = nullptr; + L0::Event *event = nullptr; }; TEST_F(EventSynchronizeTest, givenCallToEventHostSynchronizeWithTimeoutZeroAndStateInitialHostSynchronizeReturnsNotReady) { @@ -368,13 +381,17 @@ HWTEST_F(EventAubCsrTest, givenCallToEventHostSynchronizeWithAubModeCsrReturnsSu driverHandle = std::make_unique>(); driverHandle->initialize(std::move(devices)); device = driverHandle->devices[0]; + + ze_context_handle_t hContext; + ze_context_desc_t desc; + ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + L0::ContextImp *context = static_cast(Context::fromHandle(hContext)); + int32_t tag; auto aubCsr = new MockCsrAub(tag, *neoDevice->executionEnvironment, neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()); neoDevice->resetCommandStreamReceiver(aubCsr); - std::unique_ptr eventPool = nullptr; - std::unique_ptr event; - ze_event_pool_desc_t eventPoolDesc = {}; eventPoolDesc.count = 1; eventPoolDesc.flags = ZE_EVENT_POOL_FLAG_HOST_VISIBLE; @@ -384,13 +401,17 @@ HWTEST_F(EventAubCsrTest, givenCallToEventHostSynchronizeWithAubModeCsrReturnsSu eventDesc.signal = 0; eventDesc.wait = 0; - eventPool = std::unique_ptr(L0::EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc)); + L0::EventPool *eventPool = L0::EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc); ASSERT_NE(nullptr, eventPool); - event = std::unique_ptr(L0::Event::create(eventPool.get(), &eventDesc, device)); + L0::Event *event = L0::Event::create(eventPool, &eventDesc, device); ASSERT_NE(nullptr, event); ze_result_t result = event->hostSynchronize(10); EXPECT_EQ(ZE_RESULT_SUCCESS, result); + + event->destroy(); + eventPool->destroy(); + context->destroy(); } struct EventCreateAllocationResidencyTest : public ::testing::Test { @@ -428,18 +449,20 @@ class TimestampEventCreate : public Test { eventDesc.signal = 0; eventDesc.wait = 0; - eventPool = std::unique_ptr(L0::EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc)); + eventPool = L0::EventPool::create(driverHandle.get(), context, 0, nullptr, &eventPoolDesc); ASSERT_NE(nullptr, eventPool); - event = std::unique_ptr(L0::Event::create(eventPool.get(), &eventDesc, device)); + event = L0::Event::create(eventPool, &eventDesc, device); ASSERT_NE(nullptr, event); } void TearDown() override { + event->destroy(); + eventPool->destroy(); DeviceFixture::TearDown(); } - std::unique_ptr eventPool; - std::unique_ptr event; + L0::EventPool *eventPool = nullptr; + L0::Event *event = nullptr; }; TEST_F(TimestampEventCreate, givenEventCreatedWithTimestampThenIsTimestampEventFlagSet) { @@ -553,7 +576,7 @@ TEST_F(TimestampEventCreate, givenEventWhenQueryKernelTimestampThenNotReadyRetur } }; - auto mockEvent = std::make_unique(eventPool.get(), 1u, device); + auto mockEvent = std::make_unique(eventPool, 1u, device); ze_kernel_timestamp_result_t resultTimestamp = {}; @@ -583,11 +606,11 @@ TEST_F(EventPoolCreateMultiDevice, whenCreatingEventPoolWithMultipleDevicesThenE result = zeDeviceGet(driverHandle.get(), &deviceCount, devices); EXPECT_EQ(ZE_RESULT_SUCCESS, result); - std::unique_ptr eventPool(EventPool::create(driverHandle.get(), - context, - deviceCount, - devices, - &eventPoolDesc)); + L0::EventPool *eventPool = EventPool::create(driverHandle.get(), + context, + deviceCount, + devices, + &eventPoolDesc); EXPECT_NE(nullptr, eventPool); auto allocation = &eventPool->getAllocation(); @@ -596,6 +619,8 @@ TEST_F(EventPoolCreateMultiDevice, whenCreatingEventPoolWithMultipleDevicesThenE EXPECT_EQ(allocation->getGraphicsAllocations().size(), numRootDevices); delete[] devices; + + eventPool->destroy(); } TEST_F(EventPoolCreateMultiDevice, whenCreatingEventPoolWithNoDevicesThenEventPoolCreateSucceedsAndAllDeviceAreUsed) { @@ -655,15 +680,16 @@ struct EventPoolCreateNegativeTest : public ::testing::Test { driverHandle = std::make_unique>(); driverHandle->initialize(std::move(devices)); - static_cast(driverHandle.get()->getMemoryManager())->isMockEventPoolCreateMemoryManager = true; - - device = driverHandle->devices[0]; ze_context_handle_t hContext; ze_context_desc_t desc; ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); EXPECT_EQ(ZE_RESULT_SUCCESS, res); context = static_cast(Context::fromHandle(hContext)); + + static_cast(driverHandle.get()->getMemoryManager())->isMockEventPoolCreateMemoryManager = true; + + device = driverHandle->devices[0]; } void TearDown() override { context->destroy(); diff --git a/level_zero/core/test/unit_tests/sources/fence/test_fence.cpp b/level_zero/core/test/unit_tests/sources/fence/test_fence.cpp index 33aca2460e..eb20137698 100644 --- a/level_zero/core/test/unit_tests/sources/fence/test_fence.cpp +++ b/level_zero/core/test/unit_tests/sources/fence/test_fence.cpp @@ -136,6 +136,13 @@ HWTEST_F(FenceAubCsrTest, givenCallToFenceHostSynchronizeWithAubModeCsrReturnsSu devices.push_back(std::unique_ptr(neoDevice)); driverHandle = std::make_unique>(); driverHandle->initialize(std::move(devices)); + + ze_context_handle_t hContext; + ze_context_desc_t desc; + ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + L0::ContextImp *context = static_cast(Context::fromHandle(hContext)); + device = driverHandle->devices[0]; int32_t tag; auto aubCsr = new MockCsrAub(tag, *neoDevice->executionEnvironment, neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()); @@ -147,6 +154,8 @@ HWTEST_F(FenceAubCsrTest, givenCallToFenceHostSynchronizeWithAubModeCsrReturnsSu ze_result_t result = fence->hostSynchronize(10); EXPECT_EQ(ZE_RESULT_SUCCESS, result); fence->destroy(); + + context->destroy(); } } // namespace ult diff --git a/level_zero/core/test/unit_tests/sources/kernel/test_kernel.cpp b/level_zero/core/test/unit_tests/sources/kernel/test_kernel.cpp index f97ea83df9..05d62fb312 100644 --- a/level_zero/core/test/unit_tests/sources/kernel/test_kernel.cpp +++ b/level_zero/core/test/unit_tests/sources/kernel/test_kernel.cpp @@ -44,14 +44,15 @@ TEST_F(KernelInitTest, givenKernelToInitWhenItHasUnknownArgThenUnknowKernelArgHa std::make_unique(perHwThreadPrivateMemorySizeRequested); createModuleFromBinary(perHwThreadPrivateMemorySizeRequested, false, mockKernelImmData.get()); - std::unique_ptr kernel; - kernel = std::make_unique(module.get()); + ModuleImmutableDataFixture::MockKernel *kernel = new ModuleImmutableDataFixture::MockKernel(module); ze_kernel_desc_t desc = {}; desc.pKernelName = kernelName.c_str(); mockKernelImmData->resizeExplicitArgs(1); kernel->initialize(&desc); EXPECT_EQ(kernel->kernelArgHandlers[0], &KernelImp::setArgUnknown); EXPECT_EQ(mockKernelImmData->getDescriptor().payloadMappings.explicitArgs[0].type, NEO::ArgDescriptor::ArgTUnknown); + + delete kernel; } TEST(KernelArgTest, givenKernelWhenSetArgUnknownCalledThenSuccessRteurned) { @@ -258,7 +259,7 @@ HWTEST_F(KernelImmutableDataTests, givenKernelInitializedWithNoPrivateMemoryThen createModuleFromBinary(perHwThreadPrivateMemorySizeRequested, isInternal, mockKernelImmData.get()); std::unique_ptr kernel; - kernel = std::make_unique(module.get()); + kernel = std::make_unique(module); createKernel(kernel.get()); @@ -274,7 +275,7 @@ HWTEST_F(KernelImmutableDataTests, givenKernelInitializedWithPrivateMemoryThenPr createModuleFromBinary(perHwThreadPrivateMemorySizeRequested, isInternal, mockKernelImmData.get()); std::unique_ptr kernel; - kernel = std::make_unique(module.get()); + kernel = std::make_unique(module); createKernel(kernel.get()); @@ -309,7 +310,7 @@ HWTEST_F(KernelImmutableDataIsaCopyTests, whenUserKernelIsCreatedThenIsaIsaCopie mockMemoryManager->copyMemoryToAllocationCalledTimes); std::unique_ptr kernel; - kernel = std::make_unique(module.get()); + kernel = std::make_unique(module); createKernel(kernel.get()); @@ -339,7 +340,7 @@ HWTEST_F(KernelImmutableDataIsaCopyTests, whenInternalKernelIsCreatedThenIsaIsCo mockMemoryManager->copyMemoryToAllocationCalledTimes); std::unique_ptr kernel; - kernel = std::make_unique(module.get()); + kernel = std::make_unique(module); createKernel(kernel.get()); @@ -364,8 +365,8 @@ HWTEST_F(KernelImmutableDataIsaCopyTests, whenImmutableDataIsInitializedForUserK mockKernelImmData->initialize(mockKernelImmData->mockKernelInfo, device, device->getNEODevice()->getDeviceInfo().computeUnitsUsedForScratch, - module.get()->translationUnit->globalConstBuffer, - module.get()->translationUnit->globalVarBuffer, + module->translationUnit->globalConstBuffer, + module->translationUnit->globalVarBuffer, isInternal); EXPECT_EQ(previouscopyMemoryToAllocationCalledTimes + 1u, @@ -387,8 +388,8 @@ HWTEST_F(KernelImmutableDataIsaCopyTests, whenImmutableDataIsInitializedForInter mockKernelImmData->initialize(mockKernelImmData->mockKernelInfo, device, device->getNEODevice()->getDeviceInfo().computeUnitsUsedForScratch, - module.get()->translationUnit->globalConstBuffer, - module.get()->translationUnit->globalVarBuffer, + module->translationUnit->globalConstBuffer, + module->translationUnit->globalVarBuffer, isInternal); EXPECT_EQ(previouscopyMemoryToAllocationCalledTimes, @@ -415,8 +416,8 @@ HWTEST_F(KernelImmutableDataWithNullHeapTests, whenImmutableDataIsInitializedFor mockKernelImmData->initialize(mockKernelImmData->mockKernelInfo, device, device->getNEODevice()->getDeviceInfo().computeUnitsUsedForScratch, - module.get()->translationUnit->globalConstBuffer, - module.get()->translationUnit->globalVarBuffer, + module->translationUnit->globalConstBuffer, + module->translationUnit->globalVarBuffer, isInternal); EXPECT_EQ(previouscopyMemoryToAllocationCalledTimes, @@ -443,8 +444,8 @@ HWTEST_F(KernelImmutableDataWithNullHeapTests, whenImmutableDataIsInitializedFor mockKernelImmData->initialize(mockKernelImmData->mockKernelInfo, device, device->getNEODevice()->getDeviceInfo().computeUnitsUsedForScratch, - module.get()->translationUnit->globalConstBuffer, - module.get()->translationUnit->globalVarBuffer, + module->translationUnit->globalConstBuffer, + module->translationUnit->globalVarBuffer, isInternal); EXPECT_EQ(previouscopyMemoryToAllocationCalledTimes, @@ -475,7 +476,7 @@ HWTEST_F(KernelImmutableDataWithNullHeapTests, whenInternalKernelIsCreatedWithNu mockMemoryManager->copyMemoryToAllocationCalledTimes); std::unique_ptr kernel; - kernel = std::make_unique(module.get()); + kernel = std::make_unique(module); auto previousKernelHeap = mockKernelImmData->kernelInfo->heapInfo.pKernelHeap; mockKernelImmData->kernelInfo->heapInfo.pKernelHeap = nullptr; @@ -559,7 +560,7 @@ HWTEST_F(KernelIndirectPropertiesFromIGCTests, whenInitializingKernelWithNoKerne createModuleFromBinary(perHwThreadPrivateMemorySizeRequested, isInternal, mockKernelImmData.get()); std::unique_ptr kernel; - kernel = std::make_unique(module.get()); + kernel = std::make_unique(module); ze_kernel_desc_t desc = {}; desc.pKernelName = kernelName.c_str(); @@ -587,7 +588,7 @@ HWTEST_F(KernelIndirectPropertiesFromIGCTests, whenInitializingKernelWithKernelL { std::unique_ptr kernel; - kernel = std::make_unique(module.get()); + kernel = std::make_unique(module); ze_kernel_desc_t desc = {}; desc.pKernelName = kernelName.c_str(); @@ -603,7 +604,7 @@ HWTEST_F(KernelIndirectPropertiesFromIGCTests, whenInitializingKernelWithKernelL { std::unique_ptr kernel; - kernel = std::make_unique(module.get()); + kernel = std::make_unique(module); ze_kernel_desc_t desc = {}; desc.pKernelName = kernelName.c_str(); @@ -619,7 +620,7 @@ HWTEST_F(KernelIndirectPropertiesFromIGCTests, whenInitializingKernelWithKernelL { std::unique_ptr kernel; - kernel = std::make_unique(module.get()); + kernel = std::make_unique(module); ze_kernel_desc_t desc = {}; desc.pKernelName = kernelName.c_str(); @@ -932,7 +933,7 @@ HWTEST_F(KernelPropertiesTests, givenValidKernelAndNoMediavfestateThenSpillMemSi ze_result_t res = kernel->getProperties(&kernelProperties); EXPECT_EQ(ZE_RESULT_SUCCESS, res); - L0::ModuleImp *moduleImp = reinterpret_cast(module.get()); + L0::ModuleImp *moduleImp = reinterpret_cast(module); NEO::KernelInfo *ki = nullptr; for (uint32_t i = 0; i < moduleImp->getTranslationUnit()->programInfo.kernelInfos.size(); i++) { ki = moduleImp->getTranslationUnit()->programInfo.kernelInfos[i]; @@ -955,7 +956,7 @@ HWTEST_F(KernelPropertiesTests, givenValidKernelAndNollocateStatelessPrivateSurf ze_result_t res = kernel->getProperties(&kernelProperties); EXPECT_EQ(ZE_RESULT_SUCCESS, res); - L0::ModuleImp *moduleImp = reinterpret_cast(module.get()); + L0::ModuleImp *moduleImp = reinterpret_cast(module); NEO::KernelInfo *ki = nullptr; for (uint32_t i = 0; i < moduleImp->getTranslationUnit()->programInfo.kernelInfos.size(); i++) { ki = moduleImp->getTranslationUnit()->programInfo.kernelInfos[i]; @@ -1222,7 +1223,7 @@ HWTEST2_F(KernelImpPatchBindlessTest, GivenKernelImpWhenSetSurfaceStateBindlessT desc.pKernelName = kernelName.c_str(); WhiteBoxKernelHw mockKernel; - mockKernel.module = module.get(); + mockKernel.module = module; mockKernel.initialize(&desc); auto &arg = const_cast(mockKernel.kernelImmData->getDescriptor().payloadMappings.explicitArgs[0].template as()); arg.bindless = 0x40; @@ -1253,7 +1254,7 @@ HWTEST2_F(KernelImpPatchBindlessTest, GivenKernelImpWhenSetSurfaceStateBindfulTh desc.pKernelName = kernelName.c_str(); WhiteBoxKernelHw mockKernel; - mockKernel.module = module.get(); + mockKernel.module = module; mockKernel.initialize(&desc); auto &arg = const_cast(mockKernel.kernelImmData->getDescriptor().payloadMappings.explicitArgs[0].template as()); @@ -1293,7 +1294,7 @@ TEST_F(KernelImpPatchBindlessTest, GivenValidBindlessOffsetWhenSetArgBufferWithA desc.pKernelName = kernelName.c_str(); MyMockKernel mockKernel; - mockKernel.module = module.get(); + mockKernel.module = module; mockKernel.initialize(&desc); auto &arg = const_cast(mockKernel.kernelImmData->getDescriptor().payloadMappings.explicitArgs[0].as()); @@ -1312,7 +1313,7 @@ TEST_F(KernelImpPatchBindlessTest, GivenValidBindfulOffsetWhenSetArgBufferWithAl desc.pKernelName = kernelName.c_str(); MyMockKernel mockKernel; - mockKernel.module = module.get(); + mockKernel.module = module; mockKernel.initialize(&desc); auto &arg = const_cast(mockKernel.kernelImmData->getDescriptor().payloadMappings.explicitArgs[0].as()); @@ -1331,7 +1332,7 @@ TEST_F(KernelImpPatchBindlessTest, GivenUndefiedBidfulAndBindlesstOffsetWhenSetA desc.pKernelName = kernelName.c_str(); MyMockKernel mockKernel; - mockKernel.module = module.get(); + mockKernel.module = module; mockKernel.initialize(&desc); auto &arg = const_cast(mockKernel.kernelImmData->getDescriptor().payloadMappings.explicitArgs[0].as()); @@ -1352,7 +1353,7 @@ TEST_F(KernelBindlessUncachedMemoryTests, givenBindlessKernelAndAllocDataNoTfoun desc.pKernelName = kernelName.c_str(); MyMockKernel mockKernel; - mockKernel.module = module.get(); + mockKernel.module = module; mockKernel.initialize(&desc); auto &arg = const_cast(mockKernel.kernelImmData->getDescriptor().payloadMappings.explicitArgs[0].as()); @@ -1371,7 +1372,7 @@ TEST_F(KernelBindlessUncachedMemoryTests, desc.pKernelName = kernelName.c_str(); MyMockKernel mockKernel; - mockKernel.module = module.get(); + mockKernel.module = module; mockKernel.initialize(&desc); auto &arg = const_cast(mockKernel.kernelImmData->getDescriptor().payloadMappings.explicitArgs[0].as()); @@ -1421,7 +1422,7 @@ TEST_F(KernelBindlessUncachedMemoryTests, desc.pKernelName = kernelName.c_str(); MyMockKernel mockKernel; - mockKernel.module = module.get(); + mockKernel.module = module; mockKernel.initialize(&desc); auto &arg = const_cast(mockKernel.kernelImmData->getDescriptor().payloadMappings.explicitArgs[0].as()); @@ -1473,7 +1474,7 @@ TEST_F(KernelBindlessUncachedMemoryTests, desc.pKernelName = kernelName.c_str(); MyMockKernel mockKernel; - mockKernel.module = module.get(); + mockKernel.module = module; mockKernel.initialize(&desc); auto &arg = const_cast(mockKernel.kernelImmData->getDescriptor().payloadMappings.explicitArgs[0].as()); @@ -1657,8 +1658,8 @@ TEST_F(KernelPrintHandlerTest, whenPrintPrintfOutputIsCalledThenPrintfBufferIsUs ze_kernel_desc_t desc = {}; desc.pKernelName = kernelName.c_str(); - kernel = std::make_unique>(); - kernel->module = module.get(); + kernel = new WhiteBox<::L0::Kernel>(); + kernel->module = module; kernel->initialize(&desc); EXPECT_FALSE(kernel->printfBuffer == nullptr); diff --git a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp index 03f48ae294..5484445a73 100644 --- a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp +++ b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp @@ -122,21 +122,26 @@ struct OutOfMemoryTests : public ::testing::Test { devices.push_back(std::unique_ptr(neoDevice)); driverHandle = std::make_unique(); driverHandle->initialize(std::move(devices)); - prevSvmAllocsManager = driverHandle->svmAllocsManager; - currSvmAllocsManager = new SVMAllocsManagerOutOFMemoryMock(driverHandle->memoryManager); - driverHandle->svmAllocsManager = currSvmAllocsManager; device = driverHandle->devices[0]; - context = std::make_unique(driverHandle.get()); + context = new ContextImp(driverHandle.get()); EXPECT_NE(context, nullptr); context->getDevices().insert(std::make_pair(device->toHandle(), device)); auto neoDevice = device->getNEODevice(); context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), false)); + driverHandle->mainContext = context; + + prevSvmAllocsManager = context->getSvmAllocsManager(); + currSvmAllocsManager = new SVMAllocsManagerOutOFMemoryMock(context->getMemoryManager()); + context->setSvmAllocsManager(currSvmAllocsManager); } void TearDown() override { - driverHandle->svmAllocsManager = prevSvmAllocsManager; + context->setSvmAllocsManager(prevSvmAllocsManager); + context->destroy(); delete currSvmAllocsManager; } NEO::SVMAllocsManager *prevSvmAllocsManager; @@ -144,7 +149,7 @@ struct OutOfMemoryTests : public ::testing::Test { std::unique_ptr driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device = nullptr; - std::unique_ptr context; + L0::ContextImp *context = nullptr; }; TEST_F(OutOfMemoryTests, @@ -198,21 +203,26 @@ struct MemoryRelaxedSizeTests : public ::testing::Test { devices.push_back(std::unique_ptr(neoDevice)); driverHandle = std::make_unique(); driverHandle->initialize(std::move(devices)); - prevSvmAllocsManager = driverHandle->svmAllocsManager; - currSvmAllocsManager = new SVMAllocsManagerRelaxedSizeMock(driverHandle->memoryManager); - driverHandle->svmAllocsManager = currSvmAllocsManager; device = driverHandle->devices[0]; - context = std::make_unique(driverHandle.get()); + context = new ContextRelaxedSizeMock(driverHandle.get()); EXPECT_NE(context, nullptr); context->getDevices().insert(std::make_pair(device->toHandle(), device)); auto neoDevice = device->getNEODevice(); context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), false)); + driverHandle->mainContext = context; + + prevSvmAllocsManager = context->getSvmAllocsManager(); + currSvmAllocsManager = new SVMAllocsManagerRelaxedSizeMock(context->getMemoryManager()); + context->setSvmAllocsManager(currSvmAllocsManager); } void TearDown() override { - driverHandle->svmAllocsManager = prevSvmAllocsManager; + context->setSvmAllocsManager(prevSvmAllocsManager); + context->destroy(); delete currSvmAllocsManager; } NEO::SVMAllocsManager *prevSvmAllocsManager; @@ -220,7 +230,7 @@ struct MemoryRelaxedSizeTests : public ::testing::Test { std::unique_ptr driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device = nullptr; - std::unique_ptr context; + L0::ContextImp *context = nullptr; }; TEST_F(MemoryRelaxedSizeTests, @@ -549,21 +559,29 @@ struct MemoryExportImportFailTest : public ::testing::Test { driverHandle->initialize(std::move(devices)); device = driverHandle->devices[0]; - context = std::make_unique(driverHandle.get()); + context = new ContextFailFdMock(driverHandle.get()); EXPECT_NE(context, nullptr); context->getDevices().insert(std::make_pair(device->toHandle(), device)); auto neoDevice = device->getNEODevice(); context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), false)); + driverHandle->mainContext = context; + + driverHandle->setMemoryManager(context->getMemoryManager()); + driverHandle->setSvmAllocsManager(context->getSvmAllocsManager()); + driverHandle->mainContext = context; } void TearDown() override { + context->destroy(); } std::unique_ptr driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device = nullptr; ze_context_handle_t hContext; - std::unique_ptr context; + ContextFailFdMock *context = nullptr; }; TEST_F(MemoryExportImportFailTest, @@ -667,21 +685,27 @@ struct MemoryExportImportTest : public ::testing::Test { driverHandle->initialize(std::move(devices)); device = driverHandle->devices[0]; - context = std::make_unique(driverHandle.get()); + context = new ContextFdMock(driverHandle.get()); EXPECT_NE(context, nullptr); context->getDevices().insert(std::make_pair(device->toHandle(), device)); auto neoDevice = device->getNEODevice(); context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), false)); + driverHandle->mainContext = context; + driverHandle->setMemoryManager(context->getMemoryManager()); + driverHandle->setSvmAllocsManager(context->getSvmAllocsManager()); } void TearDown() override { + context->destroy(); } std::unique_ptr driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device = nullptr; ze_context_handle_t hContext; - std::unique_ptr context; + ContextFdMock *context = nullptr; }; TEST_F(MemoryExportImportTest, @@ -1087,21 +1111,27 @@ struct MemoryGetIpcHandleTest : public ::testing::Test { driverHandle->initialize(std::move(devices)); device = driverHandle->devices[0]; - context = std::make_unique(driverHandle.get()); + context = new ContextGetIpcHandleMock(driverHandle.get()); EXPECT_NE(context, nullptr); context->getDevices().insert(std::make_pair(device->toHandle(), device)); auto neoDevice = device->getNEODevice(); context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), false)); + driverHandle->mainContext = context; + driverHandle->setMemoryManager(context->getMemoryManager()); + driverHandle->setSvmAllocsManager(context->getSvmAllocsManager()); } void TearDown() override { + context->destroy(); } std::unique_ptr driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device = nullptr; - std::unique_ptr context; + ContextGetIpcHandleMock *context = nullptr; }; TEST_F(MemoryGetIpcHandleTest, @@ -1209,21 +1239,31 @@ struct MemoryOpenIpcHandleTest : public ::testing::Test { devices.push_back(std::unique_ptr(neoDevice)); driverHandle = std::make_unique(); driverHandle->initialize(std::move(devices)); - prevMemoryManager = driverHandle->getMemoryManager(); - currMemoryManager = new MemoryManagerOpenIpcMock(*neoDevice->executionEnvironment); - driverHandle->setMemoryManager(currMemoryManager); device = driverHandle->devices[0]; - context = std::make_unique(driverHandle.get()); + context = new ContextIpcMock(driverHandle.get()); EXPECT_NE(context, nullptr); context->getDevices().insert(std::make_pair(device->toHandle(), device)); - auto neoDevice = device->getNEODevice(); - context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); - context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + { + auto neoDevice = device->getNEODevice(); + context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); + context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + } + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), false)); + driverHandle->mainContext = context; + + prevMemoryManager = context->getMemoryManager(); + currMemoryManager = new MemoryManagerOpenIpcMock(*neoDevice->executionEnvironment); + context->setMemoryManager(currMemoryManager); + + driverHandle->setMemoryManager(context->getMemoryManager()); + driverHandle->setSvmAllocsManager(context->getSvmAllocsManager()); } void TearDown() override { - driverHandle->setMemoryManager(prevMemoryManager); + context->setMemoryManager(prevMemoryManager); + context->destroy(); delete currMemoryManager; } NEO::MemoryManager *prevMemoryManager = nullptr; @@ -1231,7 +1271,7 @@ struct MemoryOpenIpcHandleTest : public ::testing::Test { std::unique_ptr driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device = nullptr; - std::unique_ptr context; + ContextIpcMock *context = nullptr; }; struct MultipleDevicePeerAllocationTest : public ::testing::Test { @@ -1254,7 +1294,7 @@ struct MultipleDevicePeerAllocationTest : public ::testing::Test { ModuleBuildLog *moduleBuildLog = nullptr; - module.reset(Module::create(device, &moduleDesc, moduleBuildLog, type)); + module = Module::create(device, &moduleDesc, moduleBuildLog, type); } void SetUp() override { @@ -1276,11 +1316,9 @@ struct MultipleDevicePeerAllocationTest : public ::testing::Test { } driverHandle = std::make_unique(); driverHandle->initialize(std::move(devices)); - prevMemoryManager = driverHandle->getMemoryManager(); - currMemoryManager = new MemoryManagerOpenIpcMock(*executionEnvironment); - driverHandle->setMemoryManager(currMemoryManager); - context = std::make_unique(driverHandle.get()); + context = new ContextImp(driverHandle.get()); + bool multiOsContextDriver = false; EXPECT_NE(context, nullptr); for (auto i = 0u; i < numRootDevices; i++) { auto device = driverHandle->devices[i]; @@ -1288,20 +1326,39 @@ struct MultipleDevicePeerAllocationTest : public ::testing::Test { auto neoDevice = device->getNEODevice(); context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + multiOsContextDriver |= device->isMultiDeviceCapable(); } + + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), multiOsContextDriver)); + driverHandle->mainContext = context; + + prevMemoryManager = context->getMemoryManager(); + currMemoryManager = new MemoryManagerOpenIpcMock(*executionEnvironment); + context->setMemoryManager(currMemoryManager); + + driverHandle->setMemoryManager(context->getMemoryManager()); + driverHandle->setSvmAllocsManager(context->getSvmAllocsManager()); } void createKernel() { ze_kernel_desc_t desc = {}; desc.pKernelName = kernelName.c_str(); - kernel = std::make_unique>(); - kernel->module = module.get(); + kernel = new WhiteBox<::L0::Kernel>(); + kernel->module = module; kernel->initialize(&desc); } void TearDown() override { - driverHandle->setMemoryManager(prevMemoryManager); + if (kernel) { + kernel->destroy(); + } + if (module) { + module->destroy(); + } + context->setMemoryManager(prevMemoryManager); + context->destroy(); delete currMemoryManager; } @@ -1311,13 +1368,13 @@ struct MultipleDevicePeerAllocationTest : public ::testing::Test { std::unique_ptr driverHandle; std::unique_ptr deviceFactory; - std::unique_ptr context; + L0::ContextImp *context = nullptr; const std::string binaryFilename = "test_kernel"; const std::string kernelName = "test"; const uint32_t numKernelArguments = 6; - std::unique_ptr module; - std::unique_ptr> kernel; + L0::Module *module = nullptr; + WhiteBox<::L0::Kernel> *kernel = nullptr; const uint32_t numRootDevices = 2u; const uint32_t numSubDevices = 2u; @@ -1738,21 +1795,31 @@ struct MemoryFailedOpenIpcHandleTest : public ::testing::Test { devices.push_back(std::unique_ptr(neoDevice)); driverHandle = std::make_unique(); driverHandle->initialize(std::move(devices)); - prevMemoryManager = driverHandle->getMemoryManager(); - currMemoryManager = new MemoryManagerIpcMock(*neoDevice->executionEnvironment); - driverHandle->setMemoryManager(currMemoryManager); device = driverHandle->devices[0]; - context = std::make_unique(driverHandle.get()); + context = new ContextImp(driverHandle.get()); EXPECT_NE(context, nullptr); context->getDevices().insert(std::make_pair(device->toHandle(), device)); - auto neoDevice = device->getNEODevice(); - context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); - context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + { + auto neoDevice = device->getNEODevice(); + context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); + context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + } + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), false)); + driverHandle->mainContext = context; + + prevMemoryManager = context->getMemoryManager(); + currMemoryManager = new MemoryManagerIpcMock(*neoDevice->executionEnvironment); + context->setMemoryManager(currMemoryManager); + + driverHandle->setMemoryManager(context->getMemoryManager()); + driverHandle->setSvmAllocsManager(context->getSvmAllocsManager()); } void TearDown() override { - driverHandle->setMemoryManager(prevMemoryManager); + context->setMemoryManager(prevMemoryManager); + context->destroy(); delete currMemoryManager; } NEO::MemoryManager *prevMemoryManager = nullptr; @@ -1760,7 +1827,7 @@ struct MemoryFailedOpenIpcHandleTest : public ::testing::Test { std::unique_ptr driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device = nullptr; - std::unique_ptr context; + L0::ContextImp *context; }; TEST_F(MemoryFailedOpenIpcHandleTest, @@ -1900,7 +1967,7 @@ TEST_F(MemoryTest, givenNoDeviceWhenAllocatingSharedMemoryThenDeviceInAllocation &deviceDesc, &hostDesc, size, alignment, &ptr); - auto alloc = driverHandle->svmAllocsManager->getSVMAlloc(ptr); + auto alloc = driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr); EXPECT_EQ(alloc->device, nullptr); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -1993,17 +2060,22 @@ struct MemoryBitfieldTest : testing::Test { ASSERT_NE(nullptr, driverHandle->devices[0]->toHandle()); EXPECT_NE(neoDevice->getDeviceBitfield(), memoryManager->recentlyPassedDeviceBitfield); - context = std::make_unique(driverHandle.get()); + context = new ContextImp(driverHandle.get()); EXPECT_NE(context, nullptr); context->getDevices().insert(std::make_pair(device->toHandle(), device)); auto neoDevice = device->getNEODevice(); context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), false)); + driverHandle->mainContext = context; } void TearDown() override { auto result = context->freeMem(ptr); ASSERT_EQ(result, ZE_RESULT_SUCCESS); + context->destroy(); } std::unique_ptr> driverHandle; @@ -2013,7 +2085,7 @@ struct MemoryBitfieldTest : testing::Test { size_t size = 10; size_t alignment = 1u; void *ptr = nullptr; - std::unique_ptr context; + L0::ContextImp *context = nullptr; }; TEST_F(MemoryBitfieldTest, givenDeviceWithValidBitfieldWhenAllocatingDeviceMemoryThenPassProperBitfield) { @@ -2051,13 +2123,15 @@ TEST(MemoryBitfieldTests, givenDeviceWithValidBitfieldWhenAllocatingSharedMemory driverHandle->initialize(std::move(devices)); auto device = driverHandle->devices[0]; - std::unique_ptr context; - context = std::make_unique(driverHandle.get()); + ContextImp *context = new ContextImp(driverHandle.get()); EXPECT_NE(context, nullptr); context->getDevices().insert(std::make_pair(device->toHandle(), device)); auto neoDevice = device->getNEODevice(); context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), false)); + driverHandle->mainContext = context; memoryManager->recentlyPassedDeviceBitfield = {}; ASSERT_NE(nullptr, driverHandle->devices[1]->toHandle()); @@ -2088,6 +2162,7 @@ TEST(MemoryBitfieldTests, givenDeviceWithValidBitfieldWhenAllocatingSharedMemory EXPECT_NE(nullptr, ptr); result = context->freeMem(ptr); ASSERT_EQ(result, ZE_RESULT_SUCCESS); + context->destroy(); } struct AllocHostMemoryTest : public ::testing::Test { @@ -2358,7 +2433,7 @@ TEST_F(ImportFdUncachedTests, void *ptr = driverHandle->importFdHandle(device->toHandle(), flags, handle, nullptr); EXPECT_NE(nullptr, ptr); - auto allocData = driverHandle->svmAllocsManager->getSVMAlloc(ptr); + auto allocData = driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr); EXPECT_EQ(allocData->allocationFlagsProperty.flags.locallyUncachedResource, 1u); context->freeMem(ptr); @@ -2371,7 +2446,7 @@ TEST_F(ImportFdUncachedTests, void *ptr = driverHandle->importFdHandle(device->toHandle(), flags, handle, nullptr); EXPECT_NE(nullptr, ptr); - auto allocData = driverHandle->svmAllocsManager->getSVMAlloc(ptr); + auto allocData = driverHandle->getSvmAllocsManager()->getSVMAlloc(ptr); EXPECT_EQ(allocData->allocationFlagsProperty.flags.locallyUncachedResource, 0u); context->freeMem(ptr); @@ -2397,21 +2472,31 @@ struct SharedAllocFailTests : public ::testing::Test { devices.push_back(std::unique_ptr(neoDevice)); driverHandle = std::make_unique(); driverHandle->initialize(std::move(devices)); - prevSvmAllocsManager = driverHandle->svmAllocsManager; - currSvmAllocsManager = new SVMAllocsManagerSharedAllocFailMock(driverHandle->memoryManager); - driverHandle->svmAllocsManager = currSvmAllocsManager; device = driverHandle->devices[0]; - context = std::make_unique(driverHandle.get()); + context = new ContextImp(driverHandle.get()); EXPECT_NE(context, nullptr); context->getDevices().insert(std::make_pair(device->toHandle(), device)); - auto neoDevice = device->getNEODevice(); - context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); - context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + { + auto neoDevice = device->getNEODevice(); + context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); + context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + } + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), false)); + driverHandle->mainContext = context; + + prevSvmAllocsManager = context->getSvmAllocsManager(); + currSvmAllocsManager = new SVMAllocsManagerSharedAllocFailMock(context->getMemoryManager()); + context->setSvmAllocsManager(currSvmAllocsManager); + + driverHandle->setMemoryManager(context->getMemoryManager()); + driverHandle->setSvmAllocsManager(context->getSvmAllocsManager()); } void TearDown() override { - driverHandle->svmAllocsManager = prevSvmAllocsManager; + context->setSvmAllocsManager(prevSvmAllocsManager); + context->destroy(); delete currSvmAllocsManager; } NEO::SVMAllocsManager *prevSvmAllocsManager; @@ -2419,7 +2504,7 @@ struct SharedAllocFailTests : public ::testing::Test { std::unique_ptr driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device = nullptr; - std::unique_ptr context; + ContextImp *context; }; TEST_F(SharedAllocFailTests, whenAllocatinSharedMemoryAndAllocationFailsThenOutOfDeviceMemoryIsReturned) { @@ -2446,7 +2531,7 @@ struct ContextMultiDeviceMock : public L0::ContextImp { ContextMultiDeviceMock(L0::DriverHandleImp *driverHandle) : L0::ContextImp(driverHandle) {} ze_result_t freeMem(const void *ptr) override { SVMAllocsManagerSharedAllocMultiDeviceMock *currSvmAllocsManager = - static_cast(this->driverHandle->svmAllocsManager); + static_cast(this->driverHandle->getSvmAllocsManager()); if (currSvmAllocsManager->createHostUnifiedMemoryAllocationTimes == 0) { return ContextImp::freeMem(ptr); } @@ -2464,32 +2549,43 @@ struct SharedAllocMultiDeviceTests : public ::testing::Test { driverHandle = std::make_unique(); ze_result_t res = driverHandle->initialize(std::move(devices)); EXPECT_EQ(ZE_RESULT_SUCCESS, res); - prevSvmAllocsManager = driverHandle->svmAllocsManager; - currSvmAllocsManager = new SVMAllocsManagerSharedAllocMultiDeviceMock(driverHandle->memoryManager); - driverHandle->svmAllocsManager = currSvmAllocsManager; - context = std::make_unique(driverHandle.get()); + context = new ContextMultiDeviceMock(driverHandle.get()); EXPECT_NE(context, nullptr); + bool multiOsContextDriver = false; for (uint32_t i = 0; i < numRootDevices; i++) { auto device = driverHandle->devices[i]; context->getDevices().insert(std::make_pair(device->toHandle(), device)); auto neoDevice = device->getNEODevice(); context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex()); context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(), neoDevice->getDeviceBitfield()}); + multiOsContextDriver |= device->isMultiDeviceCapable(); } + + context->setMemoryManager(context->getDevices().begin()->second->getNEODevice()->getMemoryManager()); + context->setSvmAllocsManager(new NEO::SVMAllocsManager(context->getMemoryManager(), multiOsContextDriver)); + driverHandle->mainContext = context; + + prevSvmAllocsManager = context->getSvmAllocsManager(); + currSvmAllocsManager = new SVMAllocsManagerSharedAllocMultiDeviceMock(context->getMemoryManager()); + context->setSvmAllocsManager(currSvmAllocsManager); + + driverHandle->setMemoryManager(context->getMemoryManager()); + driverHandle->setSvmAllocsManager(context->getSvmAllocsManager()); } void TearDown() override { - driverHandle->svmAllocsManager = prevSvmAllocsManager; + context->setSvmAllocsManager(prevSvmAllocsManager); + context->destroy(); delete currSvmAllocsManager; } DebugManagerStateRestore restorer; - NEO::SVMAllocsManager *prevSvmAllocsManager; - SVMAllocsManagerSharedAllocMultiDeviceMock *currSvmAllocsManager; + NEO::SVMAllocsManager *prevSvmAllocsManager = nullptr; + SVMAllocsManagerSharedAllocMultiDeviceMock *currSvmAllocsManager = nullptr; std::unique_ptr driverHandle; - std::unique_ptr context; + ContextMultiDeviceMock *context = nullptr; const uint32_t numRootDevices = 4u; }; diff --git a/level_zero/core/test/unit_tests/sources/module/test_module.cpp b/level_zero/core/test/unit_tests/sources/module/test_module.cpp index 70d4d87496..898ac5ebdc 100644 --- a/level_zero/core/test/unit_tests/sources/module/test_module.cpp +++ b/level_zero/core/test/unit_tests/sources/module/test_module.cpp @@ -60,7 +60,7 @@ HWTEST_F(ModuleTest, givenZeroCountWhenGettingKernelNamesThenCountIsFilled) { uint32_t count = 0; auto result = module->getKernelNames(&count, nullptr); - auto whiteboxModule = whitebox_cast(module.get()); + auto whiteboxModule = whitebox_cast(module); EXPECT_EQ(whiteboxModule->kernelImmDatas.size(), count); EXPECT_EQ(ZE_RESULT_SUCCESS, result); @@ -576,7 +576,7 @@ TEST_F(ModulePropertyTest, givenCallToGetPropertiesWithUnresolvedSymbolsThenFlag NEO::Linker::RelocationInfo unresolvedRelocation; unresolvedRelocation.symbolName = "unresolved"; - whitebox_cast(module.get())->unresolvedExternalsInfo.push_back({unresolvedRelocation}); + whitebox_cast(module)->unresolvedExternalsInfo.push_back({unresolvedRelocation}); ze_module_property_flags_t expectedFlags = 0; expectedFlags |= ZE_MODULE_PROPERTY_FLAG_IMPORTS; @@ -690,7 +690,7 @@ class DeviceModuleSetArgBufferTest : public ModuleFixture, public ::testing::Tes void createKernelAndAllocMemory(uint32_t rootDeviceIndex, void **ptr, ze_kernel_handle_t *kernelHandle) { ze_kernel_desc_t kernelDesc = {}; kernelDesc.pKernelName = kernelName.c_str(); - ze_result_t res = module.get()->createKernel(&kernelDesc, kernelHandle); + ze_result_t res = module->createKernel(&kernelDesc, kernelHandle); EXPECT_EQ(ZE_RESULT_SUCCESS, res); ze_host_mem_alloc_desc_t hostDesc = {}; @@ -702,7 +702,6 @@ class DeviceModuleSetArgBufferTest : public ModuleFixture, public ::testing::Tes HWTEST_F(DeviceModuleSetArgBufferTest, givenValidMemoryUsedinFirstCallToSetArgBufferThenNullptrSetOnTheSecondCallThenArgBufferisUpdatedInEachCallAndSuccessIsReturned) { uint32_t rootDeviceIndex = 0; - createModuleFromBinary(); ze_kernel_handle_t kernelHandle; void *validBufferPtr = nullptr; @@ -752,7 +751,7 @@ class MultiDeviceModuleSetArgBufferTest : public MultiDeviceModuleFixture, publi void createKernelAndAllocMemory(uint32_t rootDeviceIndex, void **ptr, ze_kernel_handle_t *kernelHandle) { ze_kernel_desc_t kernelDesc = {}; kernelDesc.pKernelName = kernelName.c_str(); - ze_result_t res = modules[rootDeviceIndex].get()->createKernel(&kernelDesc, kernelHandle); + ze_result_t res = modules[rootDeviceIndex]->createKernel(&kernelDesc, kernelHandle); EXPECT_EQ(ZE_RESULT_SUCCESS, res); ze_host_mem_alloc_desc_t hostDesc = {}; @@ -771,10 +770,10 @@ HWTEST_F(MultiDeviceModuleSetArgBufferTest, void *ptr = nullptr; createKernelAndAllocMemory(rootDeviceIndex, &ptr, &kernelHandle); - L0::KernelImp *kernel = reinterpret_cast(Kernel::fromHandle(kernelHandle)); - kernel->setArgBuffer(0, sizeof(ptr), &ptr); + L0::KernelImp *pKernel = reinterpret_cast(Kernel::fromHandle(kernelHandle)); + pKernel->setArgBuffer(0, sizeof(ptr), &ptr); - for (auto alloc : kernel->getResidencyContainer()) { + for (auto alloc : pKernel->getResidencyContainer()) { if (alloc && alloc->getGpuAddress() == reinterpret_cast(ptr)) { EXPECT_EQ(rootDeviceIndex, alloc->getRootDeviceIndex()); } diff --git a/level_zero/tools/test/unit_tests/sources/metrics/test_metric_query_pool_1.cpp b/level_zero/tools/test/unit_tests/sources/metrics/test_metric_query_pool_1.cpp index c9c684da7b..1a93e82283 100644 --- a/level_zero/tools/test/unit_tests/sources/metrics/test_metric_query_pool_1.cpp +++ b/level_zero/tools/test/unit_tests/sources/metrics/test_metric_query_pool_1.cpp @@ -12,7 +12,6 @@ #include "level_zero/core/source/driver/driver_imp.h" #include "level_zero/core/test/unit_tests/mocks/mock_cmdlist.h" #include "level_zero/core/test/unit_tests/mocks/mock_device.h" -#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.h" #include "level_zero/tools/test/unit_tests/sources/metrics/mock_metric.h" #include "gmock/gmock.h" diff --git a/level_zero/tools/test/unit_tests/sources/metrics/test_metric_query_pool_2.cpp b/level_zero/tools/test/unit_tests/sources/metrics/test_metric_query_pool_2.cpp index 9a209c25bf..d78e12678b 100644 --- a/level_zero/tools/test/unit_tests/sources/metrics/test_metric_query_pool_2.cpp +++ b/level_zero/tools/test/unit_tests/sources/metrics/test_metric_query_pool_2.cpp @@ -13,7 +13,6 @@ #include "level_zero/core/source/driver/driver_imp.h" #include "level_zero/core/test/unit_tests/mocks/mock_cmdlist.h" #include "level_zero/core/test/unit_tests/mocks/mock_device.h" -#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.h" #include "level_zero/tools/test/unit_tests/sources/metrics/mock_metric.h" #include "gmock/gmock.h" diff --git a/level_zero/tools/test/unit_tests/sources/sysman/engine/linux/test_zes_engine.cpp b/level_zero/tools/test/unit_tests/sources/sysman/engine/linux/test_zes_engine.cpp index 01712eb4dd..5bc5a7afee 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/engine/linux/test_zes_engine.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/engine/linux/test_zes_engine.cpp @@ -30,10 +30,10 @@ class ZesEngineFixture : public SysmanDeviceFixture { void SetUp() override { SysmanDeviceFixture::SetUp(); - pMemoryManagerOriginal = device->getDriverHandle()->getMemoryManager(); + pMemoryManagerOriginal = driverHandle->getMemoryManager(); pMemoryManager = std::make_unique<::testing::NiceMock>(*neoDevice->getExecutionEnvironment()); pMemoryManager->localMemorySupported[0] = false; - device->getDriverHandle()->setMemoryManager(pMemoryManager.get()); + driverHandle->setMemoryManager(pMemoryManager.get()); pSysfsAccessOriginal = pLinuxSysmanImp->pSysfsAccess; pSysfsAccess = std::make_unique>>(); @@ -67,8 +67,8 @@ class ZesEngineFixture : public SysmanDeviceFixture { } void TearDown() override { + context->setMemoryManager(pMemoryManagerOriginal); SysmanDeviceFixture::TearDown(); - device->getDriverHandle()->setMemoryManager(pMemoryManagerOriginal); pLinuxSysmanImp->pDrm = pOriginalDrm; pLinuxSysmanImp->pPmuInterface = pOriginalPmuInterface; pLinuxSysmanImp->pSysfsAccess = pSysfsAccessOriginal; diff --git a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp index 58cfff9644..0c61b44910 100644 --- a/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp +++ b/level_zero/tools/test/unit_tests/sources/sysman/pci/linux/test_zes_pci.cpp @@ -50,6 +50,7 @@ class ZesPciFixture : public ::testing::Test { std::unique_ptr> driverHandle; NEO::MockDevice *neoDevice = nullptr; L0::Device *device = nullptr; + L0::Context *context = nullptr; void SetUp() override { neoDevice = NEO::MockDevice::createWithNewExecutionEnvironment(NEO::defaultHwInfo.get()); @@ -61,6 +62,12 @@ class ZesPciFixture : public ::testing::Test { driverHandle->initialize(std::move(devices)); device = driverHandle->devices[0]; + ze_context_handle_t hContext; + ze_context_desc_t desc; + ze_result_t res = driverHandle->createContext(&desc, 0u, nullptr, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + context = static_cast(Context::fromHandle(hContext)); + neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[device->getRootDeviceIndex()]->osInterface = std::make_unique(); auto osInterface = device->getOsInterface().get(); osInterface->setDrm(new SysmanMockDrm(const_cast(neoDevice->getRootDeviceEnvironment()))); @@ -110,6 +117,7 @@ class ZesPciFixture : public ::testing::Test { unsetenv("ZES_ENABLE_SYSMAN"); pLinuxSysmanImp->pSysfsAccess = pOriginalSysfsAccess; pLinuxSysmanImp->pFsAccess = pOriginalFsAccess; + context->destroy(); } SysmanDevice *pSysmanDevice = nullptr; SysmanDeviceImp *pSysmanDeviceImp = nullptr;