Skip to content

Commit 706d9f8

Browse files
Allow Device creating multiple CSRs [2/n]
Create new OsContext per CSR Change-Id: I8dad7fc1ab450e560f78eba3152b5913791e59a3 Signed-off-by: Dunajski, Bartosz <bartosz.dunajski@intel.com>
1 parent 3cb8683 commit 706d9f8

16 files changed

+104
-80
lines changed

runtime/device/device.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ Device::~Device() {
8686
performanceCounters->shutdown();
8787
}
8888

89-
for (auto &csr : commandStreamReceiver) {
90-
csr->flushBatchedSubmissions();
89+
for (auto &engine : engines) {
90+
engine.commandStreamReceiver->flushBatchedSubmissions();
9191
}
9292

9393
if (deviceInfo.sourceLevelDebuggerActive && executionEnvironment->sourceLevelDebugger) {
@@ -116,21 +116,20 @@ bool Device::createDeviceImpl(const HardwareInfo *pHwInfo, Device &outDevice) {
116116
executionEnvironment->initializeMemoryManager(outDevice.getEnabled64kbPages(), outDevice.getEnableLocalMemory(),
117117
outDevice.getDeviceIndex(), deviceCsrIndex);
118118

119-
outDevice.osContext = new OsContext(executionEnvironment->osInterface.get(), outDevice.getDeviceIndex());
120-
executionEnvironment->memoryManager->registerOsContext(outDevice.osContext);
121-
122-
outDevice.commandStreamReceiver.resize(1);
123-
outDevice.commandStreamReceiver[deviceCsrIndex] = executionEnvironment->commandStreamReceivers[outDevice.getDeviceIndex()][deviceCsrIndex].get();
124-
if (!outDevice.commandStreamReceiver[deviceCsrIndex]->initializeTagAllocation()) {
119+
auto osContext = executionEnvironment->memoryManager->createAndRegisterOsContext();
120+
auto commandStreamReceiver = executionEnvironment->commandStreamReceivers[outDevice.getDeviceIndex()][deviceCsrIndex].get();
121+
if (!commandStreamReceiver->initializeTagAllocation()) {
125122
return false;
126123
}
127124

125+
outDevice.engines.emplace_back(commandStreamReceiver, osContext);
126+
128127
auto pDevice = &outDevice;
129128
if (!pDevice->osTime) {
130-
pDevice->osTime = OSTime::create(outDevice.commandStreamReceiver[deviceCsrIndex]->getOSInterface());
129+
pDevice->osTime = OSTime::create(commandStreamReceiver->getOSInterface());
131130
}
132-
pDevice->driverInfo.reset(DriverInfo::create(outDevice.commandStreamReceiver[deviceCsrIndex]->getOSInterface()));
133-
pDevice->tagAddress = reinterpret_cast<uint32_t *>(outDevice.commandStreamReceiver[deviceCsrIndex]->getTagAllocation()->getUnderlyingBuffer());
131+
pDevice->driverInfo.reset(DriverInfo::create(commandStreamReceiver->getOSInterface()));
132+
pDevice->tagAddress = reinterpret_cast<uint32_t *>(commandStreamReceiver->getTagAllocation()->getUnderlyingBuffer());
134133

135134
pDevice->initializeCaps();
136135

@@ -142,8 +141,8 @@ bool Device::createDeviceImpl(const HardwareInfo *pHwInfo, Device &outDevice) {
142141
}
143142

144143
uint32_t deviceHandle = 0;
145-
if (outDevice.commandStreamReceiver[deviceCsrIndex]->getOSInterface()) {
146-
deviceHandle = outDevice.commandStreamReceiver[deviceCsrIndex]->getOSInterface()->getDeviceHandle();
144+
if (commandStreamReceiver->getOSInterface()) {
145+
deviceHandle = commandStreamReceiver->getOSInterface()->getDeviceHandle();
147146
}
148147

149148
if (pDevice->deviceInfo.sourceLevelDebuggerActive) {
@@ -160,14 +159,14 @@ bool Device::createDeviceImpl(const HardwareInfo *pHwInfo, Device &outDevice) {
160159
if (!pDevice->preemptionAllocation) {
161160
return false;
162161
}
163-
outDevice.commandStreamReceiver[deviceCsrIndex]->setPreemptionCsrAllocation(pDevice->preemptionAllocation);
162+
commandStreamReceiver->setPreemptionCsrAllocation(pDevice->preemptionAllocation);
164163
auto sipType = SipKernel::getSipKernelType(pHwInfo->pPlatform->eRenderCoreFamily, pDevice->isSourceLevelDebuggerActive());
165164
initSipKernel(sipType, *pDevice);
166165
}
167166

168167
if (DebugManager.flags.EnableExperimentalCommandBuffer.get() > 0) {
169-
outDevice.commandStreamReceiver[deviceCsrIndex]->setExperimentalCmdBuffer(
170-
std::unique_ptr<ExperimentalCommandBuffer>(new ExperimentalCommandBuffer(outDevice.commandStreamReceiver[deviceCsrIndex], pDevice->getDeviceInfo().profilingTimerResolution)));
168+
commandStreamReceiver->setExperimentalCmdBuffer(std::unique_ptr<ExperimentalCommandBuffer>(
169+
new ExperimentalCommandBuffer(commandStreamReceiver, pDevice->getDeviceInfo().profilingTimerResolution)));
171170
}
172171

173172
return true;
@@ -230,7 +229,7 @@ unique_ptr_if_unused<Device> Device::release() {
230229

231230
bool Device::isSimulation() const {
232231
bool simulation = hwInfo.capabilityTable.isSimulation(hwInfo.pPlatform->usDeviceID);
233-
if (commandStreamReceiver[0]->getType() != CommandStreamReceiverType::CSR_HW) {
232+
if (engines[0].commandStreamReceiver->getType() != CommandStreamReceiverType::CSR_HW) {
234233
simulation = true;
235234
}
236235
if (hwInfo.pSkuTable->ftrSimulationMode) {

runtime/device/device.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "runtime/device/device_info_map.h"
1212
#include "runtime/execution_environment/execution_environment.h"
1313
#include "runtime/helpers/base_object.h"
14+
#include "runtime/helpers/engine_control.h"
1415
#include "runtime/helpers/hw_info.h"
1516
#include "runtime/memory_manager/memory_constants.h"
1617
#include "runtime/os_interface/performance_counters.h"
@@ -118,7 +119,7 @@ class Device : public BaseObject<_cl_device_id> {
118119
SourceLevelDebugger *getSourceLevelDebugger() { return executionEnvironment->sourceLevelDebugger.get(); }
119120
ExecutionEnvironment *getExecutionEnvironment() const { return executionEnvironment; }
120121
const HardwareCapabilities &getHardwareCapabilities() const { return hardwareCapabilities; }
121-
OsContext *getOsContext() const { return osContext; }
122+
OsContext *getOsContext() const { return engines[0].osContext; }
122123
uint32_t getDeviceIndex() { return deviceIndex; }
123124
bool isFullRangeSvm() {
124125
return getHardwareInfo().capabilityTable.gpuAddressSpace == MemoryConstants::max48BitAddress;
@@ -155,7 +156,7 @@ class Device : public BaseObject<_cl_device_id> {
155156
std::unique_ptr<DriverInfo> driverInfo;
156157
std::unique_ptr<PerformanceCounters> performanceCounters;
157158

158-
OsContext *osContext = nullptr;
159+
std::vector<EngineControl> engines;
159160

160161
void *slmWindowStartAddress = nullptr;
161162

@@ -165,7 +166,6 @@ class Device : public BaseObject<_cl_device_id> {
165166
EngineType engineType;
166167
ExecutionEnvironment *executionEnvironment = nullptr;
167168
uint32_t deviceIndex = 0u;
168-
std::vector<CommandStreamReceiver *> commandStreamReceiver;
169169
};
170170

171171
template <cl_device_info Param>
@@ -177,7 +177,7 @@ inline void Device::getCap(const void *&src,
177177
}
178178

179179
inline CommandStreamReceiver &Device::getCommandStreamReceiver() {
180-
return *this->commandStreamReceiver[0];
180+
return *engines[0].commandStreamReceiver;
181181
}
182182

183183
inline volatile uint32_t *Device::getTagAddress() const {

runtime/helpers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ set(RUNTIME_SRCS_HELPERS_BASE
2828
${CMAKE_CURRENT_SOURCE_DIR}/dispatch_info.h
2929
${CMAKE_CURRENT_SOURCE_DIR}/dispatch_info_builder.h
3030
${CMAKE_CURRENT_SOURCE_DIR}/enable_product.inl
31+
${CMAKE_CURRENT_SOURCE_DIR}/engine_control.h
3132
${CMAKE_CURRENT_SOURCE_DIR}/error_mappers.h
3233
${CMAKE_CURRENT_SOURCE_DIR}/extendable_enum.h
3334
${CMAKE_CURRENT_SOURCE_DIR}/file_io.cpp

runtime/helpers/engine_control.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (C) 2018 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
10+
namespace OCLRT {
11+
class CommandStreamReceiver;
12+
class OsContext;
13+
14+
struct EngineControl {
15+
EngineControl() = default;
16+
EngineControl(CommandStreamReceiver *commandStreamReceiver, OsContext *osContext)
17+
: commandStreamReceiver(commandStreamReceiver), osContext(osContext){};
18+
19+
CommandStreamReceiver *commandStreamReceiver = nullptr;
20+
OsContext *osContext = nullptr;
21+
};
22+
} // namespace OCLRT

runtime/memory_manager/graphics_allocation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ GraphicsAllocation::GraphicsAllocation(void *cpuPtrIn, uint64_t gpuAddress, uint
2020
size(sizeIn),
2121
cpuPtr(cpuPtrIn),
2222
gpuAddress(gpuAddress),
23-
usageInfos(osContextCount),
2423
isShareable(isShareable) {
24+
usageInfos.resize(maxOsContextCount);
2525
}
2626

2727
GraphicsAllocation::GraphicsAllocation(void *cpuPtrIn, size_t sizeIn, osHandle sharedHandleIn, uint32_t osContextCount, bool isShareable) : size(sizeIn),
2828
cpuPtr(cpuPtrIn),
2929
gpuAddress(castToUint64(cpuPtrIn)),
3030
sharedHandle(sharedHandleIn),
31-
usageInfos(osContextCount),
3231
isShareable(isShareable) {
32+
usageInfos.resize(maxOsContextCount);
3333
}
3434
GraphicsAllocation::~GraphicsAllocation() = default;
3535

runtime/memory_manager/graphics_allocation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
152152
bool aubWritable = true;
153153
bool allocDumpable = false;
154154
bool memObjectsAllocationWithWritableFlags = false;
155-
StackVec<UsageInfo, maxOsContextCount> usageInfos;
155+
std::vector<UsageInfo> usageInfos;
156156
std::atomic<uint32_t> registeredContextsNum{0};
157157
bool isShareable = false;
158158
};

runtime/memory_manager/memory_manager.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "runtime/memory_manager/host_ptr_manager.h"
2020
#include "runtime/memory_manager/internal_allocation_storage.h"
2121
#include "runtime/os_interface/os_context.h"
22+
#include "runtime/os_interface/os_interface.h"
2223
#include "runtime/utilities/stackvec.h"
2324
#include "runtime/utilities/tag_allocator.h"
2425

@@ -200,13 +201,16 @@ bool MemoryManager::isMemoryBudgetExhausted() const {
200201
return false;
201202
}
202203

203-
void MemoryManager::registerOsContext(OsContext *contextToRegister) {
204-
auto contextId = contextToRegister->getContextId();
204+
OsContext *MemoryManager::createAndRegisterOsContext() {
205+
auto contextId = ++latestContextId;
205206
if (contextId + 1 > registeredOsContexts.size()) {
206207
registeredOsContexts.resize(contextId + 1);
207208
}
208-
contextToRegister->incRefInternal();
209-
registeredOsContexts[contextToRegister->getContextId()] = contextToRegister;
209+
auto osContext = new OsContext(executionEnvironment.osInterface.get(), contextId);
210+
osContext->incRefInternal();
211+
registeredOsContexts[contextId] = osContext;
212+
213+
return osContext;
210214
}
211215

212216
bool MemoryManager::getAllocationData(AllocationData &allocationData, const AllocationFlags &flags, const DevicesBitfield devicesBitfield,

runtime/memory_manager/memory_manager.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class GraphicsAllocation;
2424
class HostPtrManager;
2525
class CommandStreamReceiver;
2626
class OsContext;
27+
class OSInterface;
2728
class TimestampPacket;
2829

2930
struct HwPerfCounter;
@@ -246,7 +247,7 @@ class MemoryManager {
246247
::alignedFree(ptr);
247248
}
248249

249-
void registerOsContext(OsContext *contextToRegister);
250+
OsContext *createAndRegisterOsContext();
250251
uint32_t getOsContextCount() { return static_cast<uint32_t>(registeredOsContexts.size()); }
251252
CommandStreamReceiver *getCommandStreamReceiver(uint32_t contextId);
252253
HostPtrManager *getHostPtrManager() const { return hostPtrManager.get(); }
@@ -270,6 +271,7 @@ class MemoryManager {
270271
ExecutionEnvironment &executionEnvironment;
271272
std::vector<OsContext *> registeredOsContexts;
272273
std::unique_ptr<HostPtrManager> hostPtrManager;
274+
uint32_t latestContextId = std::numeric_limits<uint32_t>::max();
273275
};
274276

275277
std::unique_ptr<DeferredDeleter> createDeferredDeleter();

unit_tests/memory_manager/memory_manager_tests.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ TEST_F(MemoryAllocatorTest, allocateSystemAligned) {
197197
TEST_F(MemoryAllocatorTest, allocateGraphics) {
198198
unsigned int alignment = 4096;
199199

200+
memoryManager->createAndRegisterOsContext();
200201
auto allocation = memoryManager->allocateGraphicsMemory(sizeof(char));
202+
201203
ASSERT_NE(nullptr, allocation);
202204
// initial taskCount must be -1. if not, we may kill allocation before it will be used
203205
EXPECT_EQ((uint32_t)-1, allocation->getTaskCount(0));
@@ -1224,6 +1226,7 @@ TEST_F(MemoryManagerWithCsrTest, givenAllocationThatWasUsedAndIsCompletedWhenche
12241226
}
12251227

12261228
TEST_F(MemoryManagerWithCsrTest, givenAllocationThatWasUsedAndIsNotCompletedWhencheckGpuUsageAndDestroyGraphicsAllocationsIsCalledThenItIsAddedToTemporaryAllocationList) {
1229+
memoryManager->createAndRegisterOsContext();
12271230
auto usedAllocationAndNotGpuCompleted = memoryManager->allocateGraphicsMemory(4096);
12281231

12291232
auto tagAddress = csr->getTagAddress();
@@ -1393,24 +1396,21 @@ TEST(GraphicsAllocation, givenSharedHandleBasedConstructorWhenGraphicsAllocation
13931396
}
13941397

13951398
TEST(ResidencyDataTest, givenOsContextWhenItIsRegisteredToMemoryManagerThenRefCountIncreases) {
1396-
auto osContext = new OsContext(nullptr, 0u);
13971399
ExecutionEnvironment executionEnvironment;
1398-
OsAgnosticMemoryManager memoryManager(false, false, executionEnvironment);
1399-
memoryManager.registerOsContext(osContext);
1400+
MockMemoryManager memoryManager(false, false, executionEnvironment);
1401+
memoryManager.createAndRegisterOsContext();
14001402
EXPECT_EQ(1u, memoryManager.getOsContextCount());
1401-
EXPECT_EQ(1, osContext->getRefInternalCount());
1403+
EXPECT_EQ(1, memoryManager.registeredOsContexts[0]->getRefInternalCount());
14021404
}
14031405

14041406
TEST(ResidencyDataTest, givenTwoOsContextsWhenTheyAreRegistredFromHigherToLowerThenProperSizeIsReturned) {
1405-
auto osContext2 = new OsContext(nullptr, 1u);
1406-
auto osContext = new OsContext(nullptr, 0u);
14071407
ExecutionEnvironment executionEnvironment;
1408-
OsAgnosticMemoryManager memoryManager(false, false, executionEnvironment);
1409-
memoryManager.registerOsContext(osContext2);
1410-
memoryManager.registerOsContext(osContext);
1408+
MockMemoryManager memoryManager(false, false, executionEnvironment);
1409+
memoryManager.createAndRegisterOsContext();
1410+
memoryManager.createAndRegisterOsContext();
14111411
EXPECT_EQ(2u, memoryManager.getOsContextCount());
1412-
EXPECT_EQ(1, osContext->getRefInternalCount());
1413-
EXPECT_EQ(1, osContext2->getRefInternalCount());
1412+
EXPECT_EQ(1, memoryManager.registeredOsContexts[0]->getRefInternalCount());
1413+
EXPECT_EQ(1, memoryManager.registeredOsContexts[1]->getRefInternalCount());
14141414
}
14151415

14161416
TEST(ResidencyDataTest, givenResidencyDataWhenUpdateCompletionDataIsCalledThenItIsProperlyUpdated) {

unit_tests/mocks/mock_device.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ MockDevice::MockDevice(const HardwareInfo &hwInfo)
1919
executionEnvironment->commandStreamReceivers.resize(getDeviceIndex() + 1);
2020
executionEnvironment->commandStreamReceivers[getDeviceIndex()].push_back(std::unique_ptr<CommandStreamReceiver>(commandStreamReceiver));
2121
this->executionEnvironment->memoryManager = std::move(this->mockMemoryManager);
22-
this->commandStreamReceiver.push_back(commandStreamReceiver);
22+
this->engines.emplace_back(commandStreamReceiver, nullptr);
2323
}
2424
MockDevice::MockDevice(const HardwareInfo &hwInfo, ExecutionEnvironment *executionEnvironment, uint32_t deviceIndex)
2525
: Device(hwInfo, executionEnvironment, deviceIndex) {
@@ -49,7 +49,7 @@ void MockDevice::resetCommandStreamReceiver(CommandStreamReceiver *newCsr) {
4949
executionEnvironment->commandStreamReceivers[getDeviceIndex()][0].reset(newCsr);
5050
executionEnvironment->commandStreamReceivers[getDeviceIndex()][0]->initializeTagAllocation();
5151
executionEnvironment->commandStreamReceivers[getDeviceIndex()][0]->setPreemptionCsrAllocation(preemptionAllocation);
52-
this->commandStreamReceiver[0] = newCsr;
52+
this->engines[0].commandStreamReceiver = newCsr;
5353
UNRECOVERABLE_IF(getDeviceIndex() != 0u);
5454
this->tagAddress = executionEnvironment->commandStreamReceivers[getDeviceIndex()][0]->getTagAddress();
5555
}

0 commit comments

Comments
 (0)