Skip to content

Commit 49cde62

Browse files
Adding Command container implementation
Change-Id: Ic2c4b9128fa0275fd10db6e37b89a1f36aee60bc Signed-off-by: Maciej Plewka <[email protected]>
1 parent caa16ea commit 49cde62

File tree

10 files changed

+409
-0
lines changed

10 files changed

+409
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# Copyright (C) 2019 Intel Corporation
3+
#
4+
# SPDX-License-Identifier: MIT
5+
#
6+
7+
set(NEO_CORE_COMMAND_CONTAINER
8+
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
9+
${CMAKE_CURRENT_SOURCE_DIR}/cmdcontainer.cpp
10+
${CMAKE_CURRENT_SOURCE_DIR}/cmdcontainer.h
11+
)
12+
13+
set_property(GLOBAL PROPERTY NEO_CORE_COMMAND_CONTAINER ${NEO_CORE_COMMAND_CONTAINER})
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "core/command_container/cmdcontainer.h"
9+
10+
#include "core/command_stream/linear_stream.h"
11+
#include "core/helpers/debug_helpers.h"
12+
#include "core/helpers/heap_helper.h"
13+
#include "runtime/command_stream/command_stream_receiver.h"
14+
#include "runtime/device/device.h"
15+
#include "runtime/indirect_heap/indirect_heap.h"
16+
#include "runtime/memory_manager/memory_manager.h"
17+
18+
#include <cassert>
19+
20+
namespace NEO {
21+
22+
CommandContainer::~CommandContainer() {
23+
if (!device) {
24+
DEBUG_BREAK_IF(device);
25+
return;
26+
}
27+
28+
auto memoryManager = device->getMemoryManager();
29+
30+
memoryManager->freeGraphicsMemory(cmdBufferAllocation);
31+
32+
for (auto allocationIndirectHeap : allocationIndirectHeaps) {
33+
heapHelper->storeHeapAllocation(allocationIndirectHeap);
34+
}
35+
36+
residencyContainer.clear();
37+
deallocationContainer.clear();
38+
}
39+
40+
bool CommandContainer::initialize(Device *device) {
41+
if (!device) {
42+
DEBUG_BREAK_IF(device);
43+
return false;
44+
}
45+
this->device = device;
46+
47+
heapHelper = std::unique_ptr<HeapHelper>(new HeapHelper(device->getMemoryManager(), device->getDefaultEngine().commandStreamReceiver->getInternalAllocationStorage(), device->getNumAvailableDevices() > 1u));
48+
49+
size_t alignedSize = alignUp<size_t>(totalCmdBufferSize, MemoryConstants::pageSize64k);
50+
NEO::AllocationProperties properties{device->getRootDeviceIndex(), true /* allocateMemory*/, alignedSize,
51+
GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY,
52+
(device->getNumAvailableDevices() > 1u) /* multiOsContextCapable */,
53+
false,
54+
NEO::AllocationProperties::noDeviceSpecified};
55+
56+
cmdBufferAllocation = device->getMemoryManager()->allocateGraphicsMemoryWithProperties(properties);
57+
58+
UNRECOVERABLE_IF(!cmdBufferAllocation);
59+
60+
commandStream = std::unique_ptr<LinearStream>(new LinearStream(cmdBufferAllocation->getUnderlyingBuffer(),
61+
defaultListCmdBufferSize));
62+
commandStream->replaceGraphicsAllocation(cmdBufferAllocation);
63+
64+
addToResidencyContainer(cmdBufferAllocation);
65+
size_t heapSize = 65536u;
66+
67+
for (auto &allocationIndirectHeap : allocationIndirectHeaps) {
68+
allocationIndirectHeap = heapHelper->getHeapAllocation(heapSize, alignedSize, device->getRootDeviceIndex());
69+
UNRECOVERABLE_IF(!allocationIndirectHeap);
70+
residencyContainer.push_back(allocationIndirectHeap);
71+
}
72+
73+
uint32_t index = 0;
74+
for (auto &indirectHeap : indirectHeaps) {
75+
auto alloc = allocationIndirectHeaps[index++];
76+
indirectHeap = std::make_unique<IndirectHeap>(alloc);
77+
}
78+
79+
instructionHeapBaseAddress = device->getMemoryManager()->getInternalHeapBaseAddress(0);
80+
81+
return true;
82+
}
83+
84+
void CommandContainer::addToResidencyContainer(NEO::GraphicsAllocation *alloc) {
85+
if (alloc == nullptr) {
86+
DEBUG_BREAK_IF(true);
87+
return;
88+
}
89+
auto end = this->residencyContainer.end();
90+
bool isUnique = (end == std::find(this->residencyContainer.begin(), end, alloc));
91+
if (isUnique == false) {
92+
return;
93+
}
94+
95+
this->residencyContainer.push_back(alloc);
96+
}
97+
98+
void CommandContainer::reset() {
99+
dirtyHeaps = std::numeric_limits<uint32_t>::max();
100+
slmSize = std::numeric_limits<uint32_t>::max();
101+
getResidencyContainer().clear();
102+
getDeallocationContainer().clear();
103+
104+
commandStream->replaceBuffer(this->getCommandStream()->getCpuBase(),
105+
defaultListCmdBufferSize);
106+
addToResidencyContainer(commandStream->getGraphicsAllocation());
107+
108+
for (auto &indirectHeap : indirectHeaps) {
109+
indirectHeap->replaceBuffer(indirectHeap->getCpuBase(),
110+
indirectHeap->getMaxAvailableSpace());
111+
addToResidencyContainer(indirectHeap->getGraphicsAllocation());
112+
}
113+
}
114+
115+
} // namespace NEO
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
#include "core/helpers/heap_helper.h"
10+
#include "core/helpers/non_copyable_or_moveable.h"
11+
#include "runtime/command_stream/csr_definitions.h"
12+
#include "runtime/indirect_heap/indirect_heap.h"
13+
14+
#include <cstdint>
15+
#include <limits>
16+
#include <memory>
17+
#include <vector>
18+
19+
namespace NEO {
20+
class Device;
21+
class GraphicsAllocation;
22+
class LinearStream;
23+
using ResidencyContainer = std::vector<GraphicsAllocation *>;
24+
using HeapType = IndirectHeap::Type;
25+
26+
class CommandContainer : public NonCopyableOrMovableClass {
27+
public:
28+
static constexpr size_t defaultListCmdBufferSize = MemoryConstants::kiloByte * 256;
29+
static constexpr size_t totalCmdBufferSize =
30+
defaultListCmdBufferSize +
31+
MemoryConstants::cacheLineSize +
32+
NEO::CSRequirements::csOverfetchSize;
33+
34+
CommandContainer() = default;
35+
36+
GraphicsAllocation *getCmdBufferAllocation() { return cmdBufferAllocation; }
37+
38+
ResidencyContainer &getResidencyContainer() { return residencyContainer; }
39+
40+
std::vector<GraphicsAllocation *> &getDeallocationContainer() { return deallocationContainer; }
41+
42+
void addToResidencyContainer(GraphicsAllocation *alloc);
43+
44+
LinearStream *getCommandStream() { return commandStream.get(); }
45+
46+
IndirectHeap *getIndirectHeap(HeapType heapType) { return indirectHeaps[heapType].get(); }
47+
48+
HeapHelper *getHeapHelper() { return heapHelper.get(); }
49+
50+
GraphicsAllocation *getIndirectHeapAllocation(HeapType heapType) { return allocationIndirectHeaps[heapType]; }
51+
52+
void setIndirectHeapAllocation(HeapType heapType, GraphicsAllocation *allocation) { allocationIndirectHeaps[heapType] = allocation; }
53+
54+
void setCmdBufferAllocation(GraphicsAllocation *allocation) { cmdBufferAllocation = allocation; }
55+
56+
uint64_t getInstructionHeapBaseAddress() const { return instructionHeapBaseAddress; }
57+
58+
bool initialize(Device *device);
59+
60+
virtual ~CommandContainer();
61+
62+
uint32_t dirtyHeaps = std::numeric_limits<uint32_t>::max();
63+
uint32_t slmSize = std::numeric_limits<uint32_t>::max();
64+
65+
Device *getDevice() const { return device; }
66+
67+
void reset();
68+
69+
protected:
70+
Device *device = nullptr;
71+
std::unique_ptr<HeapHelper> heapHelper;
72+
73+
GraphicsAllocation *cmdBufferAllocation = nullptr;
74+
GraphicsAllocation *allocationIndirectHeaps[HeapType::NUM_TYPES] = {};
75+
76+
uint64_t instructionHeapBaseAddress = 0u;
77+
78+
std::unique_ptr<LinearStream> commandStream;
79+
std::unique_ptr<IndirectHeap> indirectHeaps[HeapType::NUM_TYPES] = {};
80+
ResidencyContainer residencyContainer;
81+
std::vector<GraphicsAllocation *> deallocationContainer;
82+
};
83+
84+
} // namespace NEO

core/helpers/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ set(NEO_CORE_HELPERS
1616
${CMAKE_CURRENT_SOURCE_DIR}/file_io.cpp
1717
${CMAKE_CURRENT_SOURCE_DIR}/file_io.h
1818
${CMAKE_CURRENT_SOURCE_DIR}/hash.h
19+
${CMAKE_CURRENT_SOURCE_DIR}/heap_helper.cpp
20+
${CMAKE_CURRENT_SOURCE_DIR}/heap_helper.h
1921
${CMAKE_CURRENT_SOURCE_DIR}/hw_helper.cpp
2022
${CMAKE_CURRENT_SOURCE_DIR}/hw_helper.h
2123
${CMAKE_CURRENT_SOURCE_DIR}/hw_helper_base.inl

core/helpers/heap_helper.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "core/helpers/heap_helper.h"
9+
10+
#include "core/memory_manager/graphics_allocation.h"
11+
#include "runtime/memory_manager/internal_allocation_storage.h"
12+
#include "runtime/memory_manager/memory_manager.h"
13+
14+
namespace NEO {
15+
GraphicsAllocation *HeapHelper::getHeapAllocation(size_t heapSize, size_t alignment, uint32_t rootDeviceIndex) {
16+
auto allocation = this->storageForReuse->obtainReusableAllocation(heapSize, GraphicsAllocation::AllocationType::INTERNAL_HEAP);
17+
if (allocation) {
18+
return allocation.release();
19+
}
20+
NEO::AllocationProperties properties{rootDeviceIndex, true /* allocateMemory*/, alignment,
21+
GraphicsAllocation::AllocationType::INTERNAL_HEAP,
22+
isMultiOsContextCapable /* multiOsContextCapable */,
23+
false,
24+
NEO::AllocationProperties::noDeviceSpecified};
25+
26+
return this->memManager->allocateGraphicsMemoryWithProperties(properties);
27+
}
28+
void HeapHelper::storeHeapAllocation(GraphicsAllocation *heapAllocation) {
29+
this->storageForReuse->storeAllocation(std::unique_ptr<NEO::GraphicsAllocation>(heapAllocation), NEO::AllocationUsage::REUSABLE_ALLOCATION);
30+
}
31+
} // namespace NEO

core/helpers/heap_helper.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (C) 2019 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#pragma once
9+
#include <stddef.h>
10+
#include <stdint.h>
11+
12+
namespace NEO {
13+
14+
class MemoryManager;
15+
class GraphicsAllocation;
16+
class InternalAllocationStorage;
17+
18+
class HeapHelper {
19+
public:
20+
HeapHelper(MemoryManager *memManager, InternalAllocationStorage *storageForReuse, bool isMultiOsContextCapable) : storageForReuse(storageForReuse),
21+
memManager(memManager),
22+
isMultiOsContextCapable(isMultiOsContextCapable) {}
23+
GraphicsAllocation *getHeapAllocation(size_t heapSize, size_t alignment, uint32_t rootDeviceIndex);
24+
void storeHeapAllocation(GraphicsAllocation *heapAllocation);
25+
26+
protected:
27+
InternalAllocationStorage *storageForReuse = nullptr;
28+
MemoryManager *memManager = nullptr;
29+
bool isMultiOsContextCapable = false;
30+
};
31+
} // namespace NEO
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright (C) 2019 Intel Corporation
3+
#
4+
# SPDX-License-Identifier: MIT
5+
#
6+
7+
set(NEO_COMMAND_CONTAINER_TESTS
8+
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
9+
${CMAKE_CURRENT_SOURCE_DIR}/command_container_tests.cpp
10+
)
11+
set_property(GLOBAL PROPERTY NEO_COMMAND_CONTAINER_TESTS ${NEO_COMMAND_CONTAINER_TESTS})
12+
add_subdirectories()

0 commit comments

Comments
 (0)