2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2020-01-07 19:11:38 +08:00
|
|
|
* Copyright (C) 2017-2020 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-02-23 22:20:22 +08:00
|
|
|
#include "opencl/test/unit_test/mocks/mock_context.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2020-02-24 20:10:44 +08:00
|
|
|
#include "shared/source/built_ins/built_ins.h"
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/compiler_interface/compiler_interface.h"
|
|
|
|
#include "shared/source/memory_manager/deferred_deleter.h"
|
2020-11-02 22:54:01 +08:00
|
|
|
#include "shared/source/memory_manager/os_agnostic_memory_manager.h"
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/memory_manager/unified_memory_manager.h"
|
2020-02-24 17:22:30 +08:00
|
|
|
|
2020-02-23 05:50:57 +08:00
|
|
|
#include "opencl/source/command_queue/command_queue.h"
|
|
|
|
#include "opencl/source/sharings/sharing.h"
|
2020-05-28 20:05:12 +08:00
|
|
|
#include "opencl/test/unit_test/fixtures/cl_device_fixture.h"
|
2020-03-17 19:37:38 +08:00
|
|
|
#include "opencl/test/unit_test/mocks/mock_cl_device.h"
|
2020-12-10 21:22:10 +08:00
|
|
|
#include "opencl/test/unit_test/mocks/mock_kernel.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "d3d_sharing_functions.h"
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2017-12-21 07:45:38 +08:00
|
|
|
|
2020-04-02 16:52:00 +08:00
|
|
|
MockContext::MockContext(ClDevice *pDevice, bool noSpecialQueue) {
|
|
|
|
cl_device_id deviceId = pDevice;
|
|
|
|
initializeWithDevices(ClDeviceVector{&deviceId, 1}, noSpecialQueue);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 23:50:54 +08:00
|
|
|
MockContext::MockContext(const ClDeviceVector &clDeviceVector, bool noSpecialQueue) {
|
|
|
|
initializeWithDevices(clDeviceVector, noSpecialQueue);
|
2020-06-05 06:16:55 +08:00
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
MockContext::MockContext(
|
|
|
|
void(CL_CALLBACK *funcNotify)(const char *, const void *, size_t, void *),
|
|
|
|
void *data) {
|
2020-04-02 16:52:00 +08:00
|
|
|
pDevice = nullptr;
|
2017-12-21 07:45:38 +08:00
|
|
|
properties = nullptr;
|
|
|
|
numProperties = 0;
|
|
|
|
contextCallback = funcNotify;
|
|
|
|
userData = data;
|
|
|
|
memoryManager = nullptr;
|
|
|
|
defaultDeviceQueue = nullptr;
|
|
|
|
driverDiagnostics = nullptr;
|
2020-09-28 19:11:58 +08:00
|
|
|
rootDeviceIndices = {};
|
|
|
|
maxRootDeviceIndex = std::numeric_limits<uint32_t>::max();
|
|
|
|
deviceBitfields = {};
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MockContext::~MockContext() {
|
2020-10-29 17:21:29 +08:00
|
|
|
for (auto &rootDeviceIndex : rootDeviceIndices) {
|
|
|
|
if (specialQueues[rootDeviceIndex]) {
|
|
|
|
specialQueues[rootDeviceIndex]->release();
|
|
|
|
specialQueues[rootDeviceIndex] = nullptr;
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
2020-04-02 16:52:00 +08:00
|
|
|
if (memoryManager && memoryManager->isAsyncDeleterEnabled()) {
|
2017-12-21 07:45:38 +08:00
|
|
|
memoryManager->getDeferredDeleter()->removeClient();
|
|
|
|
}
|
|
|
|
memoryManager = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MockContext::MockContext() {
|
2020-04-02 16:52:00 +08:00
|
|
|
pDevice = new MockClDevice{MockClDevice::createWithNewExecutionEnvironment<MockDevice>(nullptr)};
|
|
|
|
cl_device_id deviceId = pDevice;
|
|
|
|
initializeWithDevices(ClDeviceVector{&deviceId, 1}, false);
|
|
|
|
pDevice->decRefInternal();
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MockContext::setSharingFunctions(SharingFunctions *sharingFunctions) {
|
|
|
|
this->sharingFunctions[sharingFunctions->getId()].reset(sharingFunctions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MockContext::releaseSharingFunctions(SharingType sharing) {
|
|
|
|
this->sharingFunctions[sharing].release();
|
|
|
|
}
|
|
|
|
|
2019-01-28 15:27:26 +08:00
|
|
|
void MockContext::resetSharingFunctions(SharingType sharing) {
|
|
|
|
this->sharingFunctions[sharing].reset();
|
|
|
|
}
|
|
|
|
|
2018-08-06 15:22:04 +08:00
|
|
|
void MockContext::registerSharingWithId(SharingFunctions *sharing, SharingType sharingId) {
|
|
|
|
this->sharingFunctions[sharingId].reset(sharing);
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
void MockContext::clearSharingFunctions() {
|
|
|
|
std::vector<decltype(this->sharingFunctions)::value_type> v;
|
|
|
|
this->sharingFunctions.swap(v);
|
|
|
|
}
|
2020-03-17 19:37:38 +08:00
|
|
|
|
|
|
|
std::unique_ptr<AsyncEventsHandler> &MockContext::getAsyncEventsHandlerUniquePtr() {
|
|
|
|
return static_cast<MockClExecutionEnvironment *>(devices[0]->getExecutionEnvironment())->asyncEventsHandler;
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:52:00 +08:00
|
|
|
void MockContext::initializeWithDevices(const ClDeviceVector &devices, bool noSpecialQueue) {
|
|
|
|
for (auto &pClDevice : devices) {
|
|
|
|
pClDevice->incRefInternal();
|
2020-09-28 19:11:58 +08:00
|
|
|
rootDeviceIndices.insert(pClDevice->getRootDeviceIndex());
|
2020-04-02 16:52:00 +08:00
|
|
|
}
|
2020-09-28 19:11:58 +08:00
|
|
|
maxRootDeviceIndex = *std::max_element(rootDeviceIndices.begin(), rootDeviceIndices.end(), std::less<uint32_t const>());
|
2020-10-29 17:21:29 +08:00
|
|
|
specialQueues.resize(maxRootDeviceIndex + 1u);
|
2020-09-28 19:11:58 +08:00
|
|
|
|
2020-04-02 16:52:00 +08:00
|
|
|
this->devices = devices;
|
|
|
|
memoryManager = devices[0]->getMemoryManager();
|
|
|
|
svmAllocsManager = new SVMAllocsManager(memoryManager);
|
2020-09-28 19:11:58 +08:00
|
|
|
|
|
|
|
for (auto &rootDeviceIndex : rootDeviceIndices) {
|
|
|
|
DeviceBitfield deviceBitfield{};
|
|
|
|
for (const auto &pDevice : devices) {
|
|
|
|
if (pDevice->getRootDeviceIndex() == rootDeviceIndex) {
|
|
|
|
deviceBitfield |= pDevice->getDeviceBitfield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deviceBitfields.insert({rootDeviceIndex, deviceBitfield});
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:52:00 +08:00
|
|
|
cl_int retVal;
|
|
|
|
if (!noSpecialQueue) {
|
2020-10-29 17:21:29 +08:00
|
|
|
for (auto &device : devices) {
|
|
|
|
if (!specialQueues[device->getRootDeviceIndex()]) {
|
|
|
|
auto commandQueue = CommandQueue::create(this, device, nullptr, false, retVal);
|
|
|
|
assert(retVal == CL_SUCCESS);
|
|
|
|
overrideSpecialQueueAndDecrementRefCount(commandQueue, device->getRootDeviceIndex());
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 16:52:00 +08:00
|
|
|
}
|
2020-11-06 19:49:03 +08:00
|
|
|
|
|
|
|
setupContextType();
|
2020-04-02 16:52:00 +08:00
|
|
|
}
|
|
|
|
|
2020-12-10 21:22:10 +08:00
|
|
|
SchedulerKernel &MockContext::getSchedulerKernel() {
|
|
|
|
if (schedulerBuiltIn->pKernel) {
|
|
|
|
return *static_cast<SchedulerKernel *>(schedulerBuiltIn->pKernel);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto initializeSchedulerProgramAndKernel = [&] {
|
|
|
|
cl_int retVal = CL_SUCCESS;
|
|
|
|
auto clDevice = getDevice(0);
|
|
|
|
auto src = SchedulerKernel::loadSchedulerKernel(&clDevice->getDevice());
|
|
|
|
|
|
|
|
auto program = Program::createBuiltInFromGenBinary(this,
|
|
|
|
devices,
|
|
|
|
src.resource.data(),
|
|
|
|
src.resource.size(),
|
|
|
|
&retVal);
|
|
|
|
DEBUG_BREAK_IF(retVal != CL_SUCCESS);
|
|
|
|
DEBUG_BREAK_IF(!program);
|
|
|
|
|
|
|
|
retVal = program->processGenBinary(*clDevice);
|
|
|
|
DEBUG_BREAK_IF(retVal != CL_SUCCESS);
|
|
|
|
|
|
|
|
schedulerBuiltIn->pProgram = program;
|
|
|
|
|
|
|
|
KernelInfoContainer kernelInfos;
|
|
|
|
kernelInfos.resize(getMaxRootDeviceIndex() + 1);
|
|
|
|
for (auto rootDeviceIndex : rootDeviceIndices) {
|
|
|
|
auto kernelInfo = schedulerBuiltIn->pProgram->getKernelInfo(SchedulerKernel::schedulerName, rootDeviceIndex);
|
|
|
|
DEBUG_BREAK_IF(!kernelInfo);
|
|
|
|
kernelInfos[rootDeviceIndex] = kernelInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
schedulerBuiltIn->pKernel = Kernel::create<MockSchedulerKernel>(
|
|
|
|
schedulerBuiltIn->pProgram,
|
|
|
|
kernelInfos,
|
|
|
|
&retVal);
|
|
|
|
|
|
|
|
UNRECOVERABLE_IF(schedulerBuiltIn->pKernel->getScratchSize(clDevice->getRootDeviceIndex()) != 0);
|
|
|
|
|
|
|
|
DEBUG_BREAK_IF(retVal != CL_SUCCESS);
|
|
|
|
};
|
|
|
|
std::call_once(schedulerBuiltIn->programIsInitialized, initializeSchedulerProgramAndKernel);
|
|
|
|
|
|
|
|
UNRECOVERABLE_IF(schedulerBuiltIn->pKernel == nullptr);
|
|
|
|
return *static_cast<SchedulerKernel *>(schedulerBuiltIn->pKernel);
|
|
|
|
}
|
|
|
|
|
2020-04-02 16:52:00 +08:00
|
|
|
MockDefaultContext::MockDefaultContext() : MockContext(nullptr, nullptr) {
|
|
|
|
pRootDevice0 = ultClDeviceFactory.rootDevices[0];
|
|
|
|
pRootDevice1 = ultClDeviceFactory.rootDevices[1];
|
|
|
|
cl_device_id deviceIds[] = {pRootDevice0, pRootDevice1};
|
|
|
|
initializeWithDevices(ClDeviceVector{deviceIds, 2}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
MockSpecializedContext::MockSpecializedContext() : MockContext(nullptr, nullptr) {
|
|
|
|
pRootDevice = ultClDeviceFactory.rootDevices[0];
|
|
|
|
pSubDevice0 = ultClDeviceFactory.subDevices[0];
|
|
|
|
pSubDevice1 = ultClDeviceFactory.subDevices[1];
|
|
|
|
cl_device_id deviceIds[] = {pSubDevice0, pSubDevice1};
|
|
|
|
initializeWithDevices(ClDeviceVector{deviceIds, 2}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
MockUnrestrictiveContext::MockUnrestrictiveContext() : MockContext(nullptr, nullptr) {
|
2020-06-05 06:16:55 +08:00
|
|
|
pRootDevice = ultClDeviceFactory.rootDevices[0];
|
|
|
|
pSubDevice0 = ultClDeviceFactory.subDevices[0];
|
|
|
|
pSubDevice1 = ultClDeviceFactory.subDevices[1];
|
2020-04-02 16:52:00 +08:00
|
|
|
cl_device_id deviceIds[] = {pRootDevice, pSubDevice0, pSubDevice1};
|
|
|
|
initializeWithDevices(ClDeviceVector{deviceIds, 3}, true);
|
|
|
|
}
|
|
|
|
|
2020-06-15 04:18:42 +08:00
|
|
|
MockUnrestrictiveContextMultiGPU::MockUnrestrictiveContextMultiGPU() : MockContext(nullptr, nullptr) {
|
|
|
|
pRootDevice0 = ultClDeviceFactory.rootDevices[0];
|
|
|
|
pSubDevice00 = ultClDeviceFactory.subDevices[0];
|
|
|
|
pSubDevice01 = ultClDeviceFactory.subDevices[1];
|
|
|
|
pRootDevice1 = ultClDeviceFactory.rootDevices[1];
|
|
|
|
pSubDevice10 = ultClDeviceFactory.subDevices[2];
|
|
|
|
pSubDevice11 = ultClDeviceFactory.subDevices[3];
|
|
|
|
cl_device_id deviceIds[] = {pRootDevice0, pSubDevice00, pSubDevice01,
|
|
|
|
pRootDevice1, pSubDevice10, pSubDevice11};
|
|
|
|
initializeWithDevices(ClDeviceVector{deviceIds, 6}, true);
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|