Skip to content

Commit 352450a

Browse files
Pass number of os contexts to Graphics Allocation constructor
Mark unshareable allocations Change-Id: Ie745dc639d8c6b01e2275d29ee1fb4c6343df2bc Signed-off-by: Mateusz Jablonski <mateusz.jablonski@intel.com>
1 parent 2f15ca0 commit 352450a

28 files changed

+169
-144
lines changed

runtime/command_stream/aub_command_stream_receiver_hw.inl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ bool AUBCommandStreamReceiverHw<GfxFamily>::writeMemory(GraphicsAllocation &gfxA
651651

652652
template <typename GfxFamily>
653653
bool AUBCommandStreamReceiverHw<GfxFamily>::writeMemory(AllocationView &allocationView) {
654-
GraphicsAllocation gfxAllocation(reinterpret_cast<void *>(allocationView.first), allocationView.first, 0llu, allocationView.second);
654+
GraphicsAllocation gfxAllocation(reinterpret_cast<void *>(allocationView.first), allocationView.first, 0llu, allocationView.second, this->getMemoryManager()->getOsContextCount(), false);
655655
return writeMemory(gfxAllocation);
656656
}
657657

runtime/memory_manager/graphics_allocation.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ bool GraphicsAllocation::isL3Capable() {
1616
}
1717
return false;
1818
}
19-
GraphicsAllocation::GraphicsAllocation(void *cpuPtrIn, uint64_t gpuAddress, uint64_t baseAddress, size_t sizeIn) : gpuBaseAddress(baseAddress),
20-
size(sizeIn),
21-
cpuPtr(cpuPtrIn),
22-
gpuAddress(gpuAddress) {
19+
GraphicsAllocation::GraphicsAllocation(void *cpuPtrIn, uint64_t gpuAddress, uint64_t baseAddress, size_t sizeIn, uint32_t osContextCount, bool isShareable) : gpuBaseAddress(baseAddress),
20+
size(sizeIn),
21+
cpuPtr(cpuPtrIn),
22+
gpuAddress(gpuAddress),
23+
usageInfos(osContextCount),
24+
isShareable(isShareable) {
2325
}
2426

25-
GraphicsAllocation::GraphicsAllocation(void *cpuPtrIn, size_t sizeIn, osHandle sharedHandleIn) : size(sizeIn),
26-
cpuPtr(cpuPtrIn),
27-
gpuAddress(castToUint64(cpuPtrIn)),
28-
sharedHandle(sharedHandleIn) {
27+
GraphicsAllocation::GraphicsAllocation(void *cpuPtrIn, size_t sizeIn, osHandle sharedHandleIn, uint32_t osContextCount, bool isShareable) : size(sizeIn),
28+
cpuPtr(cpuPtrIn),
29+
gpuAddress(castToUint64(cpuPtrIn)),
30+
sharedHandle(sharedHandleIn),
31+
usageInfos(osContextCount),
32+
isShareable(isShareable) {
2933
}
3034
GraphicsAllocation::~GraphicsAllocation() = default;
3135

runtime/memory_manager/graphics_allocation.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
7070
GraphicsAllocation &operator=(const GraphicsAllocation &) = delete;
7171
GraphicsAllocation(const GraphicsAllocation &) = delete;
7272

73-
GraphicsAllocation(void *cpuPtrIn, uint64_t gpuAddress, uint64_t baseAddress, size_t sizeIn);
73+
GraphicsAllocation(void *cpuPtrIn, uint64_t gpuAddress, uint64_t baseAddress, size_t sizeIn, uint32_t osContextCount, bool isShareable);
7474

75-
GraphicsAllocation(void *cpuPtrIn, size_t sizeIn, osHandle sharedHandleIn);
75+
GraphicsAllocation(void *cpuPtrIn, size_t sizeIn, osHandle sharedHandleIn, uint32_t osContextCount, bool isShareable);
7676

7777
void *getUnderlyingBuffer() const { return cpuPtr; }
7878
void setCpuPtrAndGpuAddress(void *cpuPtr, uint64_t gpuAddress) {
@@ -152,7 +152,8 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
152152
bool aubWritable = true;
153153
bool allocDumpable = false;
154154
bool memObjectsAllocationWithWritableFlags = false;
155-
StackVec<UsageInfo, maxOsContextCount> usageInfos{maxOsContextCount};
155+
StackVec<UsageInfo, maxOsContextCount> usageInfos;
156156
std::atomic<uint32_t> registeredContextsNum{0};
157+
bool isShareable = false;
157158
};
158159
} // namespace OCLRT

runtime/memory_manager/memory_manager.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ MemoryManager::MemoryManager(bool enable64kbpages, bool enableLocalMemory,
2929
ExecutionEnvironment &executionEnvironment) : allocator32Bit(nullptr), enable64kbpages(enable64kbpages),
3030
localMemorySupported(enableLocalMemory),
3131
executionEnvironment(executionEnvironment),
32-
hostPtrManager(std::make_unique<HostPtrManager>()){};
32+
hostPtrManager(std::make_unique<HostPtrManager>()) {
33+
registeredOsContexts.resize(1);
34+
};
3335

3436
MemoryManager::~MemoryManager() {
3537
for (auto osContext : registeredOsContexts) {
36-
osContext->decRefInternal();
38+
if (osContext) {
39+
osContext->decRefInternal();
40+
}
3741
}
3842
}
3943

runtime/memory_manager/memory_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class MemoryManager {
247247
}
248248

249249
void registerOsContext(OsContext *contextToRegister);
250-
size_t getOsContextCount() { return registeredOsContexts.size(); }
250+
uint32_t getOsContextCount() { return static_cast<uint32_t>(registeredOsContexts.size()); }
251251
CommandStreamReceiver *getCommandStreamReceiver(uint32_t contextId);
252252
HostPtrManager *getHostPtrManager() const { return hostPtrManager.get(); }
253253

runtime/memory_manager/os_agnostic_memory_manager.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
*/
77

8+
#include "runtime/execution_environment/execution_environment.h"
89
#include "runtime/memory_manager/os_agnostic_memory_manager.h"
910
#include "runtime/gmm_helper/gmm.h"
1011
#include "runtime/gmm_helper/gmm_helper.h"
@@ -35,14 +36,16 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemory(size_t size,
3536
MemoryAllocation *memoryAllocation = nullptr;
3637

3738
if (fakeBigAllocations && size > bigAllocation) {
38-
memoryAllocation = new MemoryAllocation(nullptr, (void *)dummyAddress, static_cast<uint64_t>(dummyAddress), size, counter, MemoryPool::System4KBPages);
39+
memoryAllocation = new MemoryAllocation(nullptr, (void *)dummyAddress, static_cast<uint64_t>(dummyAddress), size, counter,
40+
MemoryPool::System4KBPages, this->getOsContextCount(), false);
3941
counter++;
4042
memoryAllocation->uncacheable = uncacheable;
4143
return memoryAllocation;
4244
}
4345
auto ptr = allocateSystemMemory(sizeAligned, alignment ? alignUp(alignment, MemoryConstants::pageSize) : MemoryConstants::pageSize);
4446
if (ptr != nullptr) {
45-
memoryAllocation = new MemoryAllocation(ptr, ptr, reinterpret_cast<uint64_t>(ptr), size, counter, MemoryPool::System4KBPages);
47+
memoryAllocation = new MemoryAllocation(ptr, ptr, reinterpret_cast<uint64_t>(ptr), size, counter, MemoryPool::System4KBPages,
48+
this->getOsContextCount(), false);
4649
if (!memoryAllocation) {
4750
alignedFreeWrapper(ptr);
4851
return nullptr;
@@ -58,7 +61,8 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemoryForNonSvmHost
5861
auto alignedPtr = alignDown(reinterpret_cast<char *>(cpuPtr), MemoryConstants::pageSize);
5962
auto offsetInPage = reinterpret_cast<char *>(cpuPtr) - alignedPtr;
6063

61-
memoryAllocation = new MemoryAllocation(nullptr, cpuPtr, reinterpret_cast<uint64_t>(alignedPtr), size, counter, MemoryPool::System4KBPages);
64+
memoryAllocation = new MemoryAllocation(nullptr, cpuPtr, reinterpret_cast<uint64_t>(alignedPtr), size, counter, MemoryPool::System4KBPages,
65+
this->getOsContextCount(), false);
6266

6367
memoryAllocation->allocationOffset = offsetInPage;
6468
memoryAllocation->uncacheable = false;
@@ -83,7 +87,8 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemory(size_t
8387
return nullptr;
8488
}
8589
uint64_t offset = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr) & MemoryConstants::pageMask);
86-
MemoryAllocation *memAlloc = new MemoryAllocation(nullptr, const_cast<void *>(ptr), GmmHelper::canonize(gpuVirtualAddress + offset), size, counter, MemoryPool::System4KBPagesWith32BitGpuAddressing);
90+
MemoryAllocation *memAlloc = new MemoryAllocation(nullptr, const_cast<void *>(ptr), GmmHelper::canonize(gpuVirtualAddress + offset), size,
91+
counter, MemoryPool::System4KBPagesWith32BitGpuAddressing, this->getOsContextCount(), false);
8792
memAlloc->is32BitAllocation = true;
8893
memAlloc->gpuBaseAddress = GmmHelper::canonize(allocator32Bit->getBase());
8994
memAlloc->sizeToFree = allocationSize;
@@ -109,7 +114,8 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemory(size_t
109114

110115
MemoryAllocation *memoryAllocation = nullptr;
111116
if (ptrAlloc != nullptr) {
112-
memoryAllocation = new MemoryAllocation(freeCpuPointer ? ptrAlloc : nullptr, ptrAlloc, GmmHelper::canonize(gpuAddress), size, counter, MemoryPool::System4KBPagesWith32BitGpuAddressing);
117+
memoryAllocation = new MemoryAllocation(freeCpuPointer ? ptrAlloc : nullptr, ptrAlloc, GmmHelper::canonize(gpuAddress), size, counter,
118+
MemoryPool::System4KBPagesWith32BitGpuAddressing, this->getOsContextCount(), false);
113119
memoryAllocation->is32BitAllocation = true;
114120
memoryAllocation->gpuBaseAddress = GmmHelper::canonize(allocator32Bit->getBase());
115121
memoryAllocation->sizeToFree = allocationSize;
@@ -119,7 +125,8 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemory(size_t
119125
}
120126

121127
GraphicsAllocation *OsAgnosticMemoryManager::createGraphicsAllocationFromSharedHandle(osHandle handle, bool requireSpecificBitness) {
122-
auto graphicsAllocation = new MemoryAllocation(nullptr, reinterpret_cast<void *>(1), 1, 4096u, static_cast<uint64_t>(handle), MemoryPool::SystemCpuInaccessible);
128+
auto graphicsAllocation = new MemoryAllocation(nullptr, reinterpret_cast<void *>(1), 1, 4096u, static_cast<uint64_t>(handle),
129+
MemoryPool::SystemCpuInaccessible, this->getOsContextCount(), false);
123130
graphicsAllocation->setSharedHandle(handle);
124131
graphicsAllocation->is32BitAllocation = requireSpecificBitness;
125132
return graphicsAllocation;
@@ -187,7 +194,8 @@ uint64_t OsAgnosticMemoryManager::getInternalHeapBaseAddress() {
187194
}
188195

189196
GraphicsAllocation *OsAgnosticMemoryManager::createGraphicsAllocation(OsHandleStorage &handleStorage, size_t hostPtrSize, const void *hostPtr) {
190-
auto allocation = new MemoryAllocation(nullptr, const_cast<void *>(hostPtr), reinterpret_cast<uint64_t>(hostPtr), hostPtrSize, counter++, MemoryPool::System4KBPages);
197+
auto allocation = new MemoryAllocation(nullptr, const_cast<void *>(hostPtr), reinterpret_cast<uint64_t>(hostPtr),
198+
hostPtrSize, counter++, MemoryPool::System4KBPages, this->getOsContextCount(), false);
191199
allocation->fragmentsStorage = handleStorage;
192200
return allocation;
193201
}
@@ -227,7 +235,8 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocateGraphicsMemoryForImage(Imag
227235
} else {
228236
auto ptr = allocateSystemMemory(alignUp(imgInfo.size, MemoryConstants::pageSize), MemoryConstants::pageSize);
229237
if (ptr != nullptr) {
230-
alloc = new MemoryAllocation(ptr, ptr, reinterpret_cast<uint64_t>(ptr), imgInfo.size, counter, MemoryPool::SystemCpuInaccessible);
238+
alloc = new MemoryAllocation(ptr, ptr, reinterpret_cast<uint64_t>(ptr), imgInfo.size, counter,
239+
MemoryPool::SystemCpuInaccessible, this->getOsContextCount(), false);
231240
counter++;
232241
}
233242
}

runtime/memory_manager/os_agnostic_memory_manager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class MemoryAllocation : public GraphicsAllocation {
2121

2222
void setSharedHandle(osHandle handle) { this->sharedHandle = handle; }
2323

24-
MemoryAllocation(void *driverAllocatedCpuPointer, void *pMem, uint64_t gpuAddress, size_t memSize, uint64_t count, MemoryPool::Type pool) : GraphicsAllocation(pMem, gpuAddress, 0u, memSize),
25-
id(count) {
24+
MemoryAllocation(void *driverAllocatedCpuPointer, void *pMem, uint64_t gpuAddress, size_t memSize, uint64_t count, MemoryPool::Type pool, uint32_t osContextCount, bool isShareable) : GraphicsAllocation(pMem, gpuAddress, 0u, memSize, osContextCount, isShareable),
25+
id(count) {
2626
this->driverAllocatedCpuPointer = driverAllocatedCpuPointer;
2727
overrideMemoryPool(pool);
2828
}

runtime/os_interface/linux/drm_allocation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ struct OsHandle {
1717

1818
class DrmAllocation : public GraphicsAllocation {
1919
public:
20-
DrmAllocation(BufferObject *bo, void *ptrIn, size_t sizeIn, MemoryPool::Type pool) : GraphicsAllocation(ptrIn, castToUint64(ptrIn), 0llu, sizeIn), bo(bo) {
20+
DrmAllocation(BufferObject *bo, void *ptrIn, size_t sizeIn, MemoryPool::Type pool, uint32_t osContextsCount, bool isShareable) : GraphicsAllocation(ptrIn, castToUint64(ptrIn), 0llu, sizeIn, osContextsCount, isShareable), bo(bo) {
2121
this->memoryPool = pool;
2222
}
23-
DrmAllocation(BufferObject *bo, void *ptrIn, size_t sizeIn, osHandle sharedHandle, MemoryPool::Type pool) : GraphicsAllocation(ptrIn, sizeIn, sharedHandle), bo(bo) {
23+
DrmAllocation(BufferObject *bo, void *ptrIn, size_t sizeIn, osHandle sharedHandle, MemoryPool::Type pool, uint32_t osContextsCount, bool isShareable) : GraphicsAllocation(ptrIn, sizeIn, sharedHandle, osContextsCount, isShareable), bo(bo) {
2424
this->memoryPool = pool;
2525
}
26-
DrmAllocation(BufferObject *bo, void *ptrIn, uint64_t gpuAddress, size_t sizeIn, MemoryPool::Type pool) : GraphicsAllocation(ptrIn, gpuAddress, 0, sizeIn), bo(bo) {
26+
DrmAllocation(BufferObject *bo, void *ptrIn, uint64_t gpuAddress, size_t sizeIn, MemoryPool::Type pool, uint32_t osContextsCount, bool isShareable) : GraphicsAllocation(ptrIn, gpuAddress, 0, sizeIn, osContextsCount, isShareable), bo(bo) {
2727
this->memoryPool = pool;
2828
}
2929

runtime/os_interface/linux/drm_memory_manager.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ OCLRT::BufferObject *DrmMemoryManager::allocUserptr(uintptr_t address, size_t si
198198
}
199199

200200
DrmAllocation *DrmMemoryManager::createGraphicsAllocation(OsHandleStorage &handleStorage, size_t hostPtrSize, const void *hostPtr) {
201-
auto allocation = new DrmAllocation(nullptr, const_cast<void *>(hostPtr), hostPtrSize, MemoryPool::System4KBPages);
201+
auto allocation = new DrmAllocation(nullptr, const_cast<void *>(hostPtr), hostPtrSize, MemoryPool::System4KBPages, getOsContextCount(), false);
202202
allocation->fragmentsStorage = handleStorage;
203203
return allocation;
204204
}
@@ -226,7 +226,7 @@ DrmAllocation *DrmMemoryManager::allocateGraphicsMemory(size_t size, size_t alig
226226
if (forcePinEnabled && pinBB != nullptr && forcePin && size >= this->pinThreshold) {
227227
pinBB->pin(&bo, 1);
228228
}
229-
return new DrmAllocation(bo, res, cSize, MemoryPool::System4KBPages);
229+
return new DrmAllocation(bo, res, cSize, MemoryPool::System4KBPages, getOsContextCount(), false);
230230
}
231231

232232
DrmAllocation *DrmMemoryManager::allocateGraphicsMemory(size_t size, const void *ptr, bool forcePin) {
@@ -267,7 +267,7 @@ DrmAllocation *DrmMemoryManager::allocateGraphicsMemoryForNonSvmHostPtr(size_t s
267267
bo->softPin((uint64_t)offset);
268268
bo->setAllocationType(allocType);
269269

270-
auto allocation = new DrmAllocation(bo, cpuPtr, reinterpret_cast<uint64_t>(alignedPtr), size, MemoryPool::System4KBPages);
270+
auto allocation = new DrmAllocation(bo, cpuPtr, reinterpret_cast<uint64_t>(alignedPtr), size, MemoryPool::System4KBPages, getOsContextCount(), false);
271271
allocation->allocationOffset = offsetInPage;
272272
allocation->gpuBaseAddress = limitedGpuAddressRangeAllocator->getBase();
273273

@@ -312,7 +312,7 @@ GraphicsAllocation *DrmMemoryManager::allocateGraphicsMemoryForImage(ImageInfo &
312312

313313
bo->setUnmapSize(imgInfo.size);
314314

315-
auto allocation = new DrmAllocation(bo, nullptr, (uint64_t)gpuRange, imgInfo.size, MemoryPool::SystemCpuInaccessible);
315+
auto allocation = new DrmAllocation(bo, nullptr, (uint64_t)gpuRange, imgInfo.size, MemoryPool::SystemCpuInaccessible, getOsContextCount(), false);
316316
bo->setAllocationType(allocatorType);
317317
allocation->gmm = gmm;
318318
return allocation;
@@ -345,7 +345,8 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemory(size_t size, const
345345
uintptr_t offset = (uintptr_t)bo->address;
346346
bo->softPin((uint64_t)offset);
347347
bo->setAllocationType(allocatorType);
348-
auto drmAllocation = new DrmAllocation(bo, (void *)ptr, (uint64_t)ptrOffset(gpuVirtualAddress, inputPointerOffset), allocationSize, MemoryPool::System4KBPagesWith32BitGpuAddressing);
348+
auto drmAllocation = new DrmAllocation(bo, (void *)ptr, (uint64_t)ptrOffset(gpuVirtualAddress, inputPointerOffset),
349+
allocationSize, MemoryPool::System4KBPagesWith32BitGpuAddressing, getOsContextCount(), false);
349350
drmAllocation->is32BitAllocation = true;
350351
drmAllocation->gpuBaseAddress = allocatorToUse->getBase();
351352
return drmAllocation;
@@ -371,7 +372,8 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemory(size_t size, const
371372

372373
bo->setAllocationType(allocatorType);
373374

374-
auto drmAllocation = new DrmAllocation(bo, reinterpret_cast<void *>(res), alignedAllocationSize, MemoryPool::System4KBPagesWith32BitGpuAddressing);
375+
auto drmAllocation = new DrmAllocation(bo, reinterpret_cast<void *>(res), alignedAllocationSize,
376+
MemoryPool::System4KBPagesWith32BitGpuAddressing, getOsContextCount(), false);
375377
drmAllocation->is32BitAllocation = true;
376378
drmAllocation->gpuBaseAddress = allocatorToUse->getBase();
377379
return drmAllocation;
@@ -442,7 +444,7 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o
442444

443445
lock.unlock();
444446

445-
auto drmAllocation = new DrmAllocation(bo, bo->address, bo->size, handle, MemoryPool::SystemCpuInaccessible);
447+
auto drmAllocation = new DrmAllocation(bo, bo->address, bo->size, handle, MemoryPool::SystemCpuInaccessible, getOsContextCount(), false);
446448

447449
if (requireSpecificBitness && this->force32bitAllocations) {
448450
drmAllocation->is32BitAllocation = true;
@@ -473,7 +475,8 @@ GraphicsAllocation *DrmMemoryManager::createPaddedAllocation(GraphicsAllocation
473475
bo->softPin(gpuRange);
474476
bo->setUnmapSize(sizeWithPadding);
475477
bo->setAllocationType(storageType);
476-
return new DrmAllocation(bo, (void *)srcPtr, (uint64_t)ptrOffset(gpuRange, offset), sizeWithPadding, inputGraphicsAllocation->getMemoryPool());
478+
return new DrmAllocation(bo, (void *)srcPtr, (uint64_t)ptrOffset(gpuRange, offset), sizeWithPadding,
479+
inputGraphicsAllocation->getMemoryPool(), getOsContextCount(), false);
477480
}
478481

479482
void DrmMemoryManager::addAllocationToHostPtrManager(GraphicsAllocation *gfxAllocation) {

0 commit comments

Comments
 (0)