Skip to content

Commit a5e4edb

Browse files
Choose valid bank from memory bank selector
Related-To: NEO-4645 Change-Id: I8d1f63ba24ead2e77ba6381e4770068bf2eb1725 Signed-off-by: Andrzej Swierczynski <andrzej.swierczynski@intel.com>
1 parent ef4cc0e commit a5e4edb

File tree

9 files changed

+54
-19
lines changed

9 files changed

+54
-19
lines changed

opencl/source/api/api.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3521,6 +3521,7 @@ void *clHostMemAllocINTEL(
35213521
cl_mem_flags flags = 0;
35223522
cl_mem_flags_intel flagsIntel = 0;
35233523
cl_mem_alloc_flags_intel allocflags = 0;
3524+
unifiedMemoryProperties.subdeviceBitfield = neoContext->getDeviceBitfieldForAllocation();
35243525
if (!MemoryPropertiesHelper::parseMemoryProperties(properties, unifiedMemoryProperties.allocationFlags, flags, flagsIntel,
35253526
allocflags, MemoryPropertiesHelper::ObjType::UNKNOWN,
35263527
*neoContext)) {
@@ -3559,6 +3560,7 @@ void *clDeviceMemAllocINTEL(
35593560
cl_mem_flags flags = 0;
35603561
cl_mem_flags_intel flagsIntel = 0;
35613562
cl_mem_alloc_flags_intel allocflags = 0;
3563+
unifiedMemoryProperties.subdeviceBitfield = neoDevice->getDeviceBitfield();
35623564
if (!MemoryPropertiesHelper::parseMemoryProperties(properties, unifiedMemoryProperties.allocationFlags, flags, flagsIntel,
35633565
allocflags, MemoryPropertiesHelper::ObjType::UNKNOWN,
35643566
*neoContext)) {
@@ -3573,7 +3575,6 @@ void *clDeviceMemAllocINTEL(
35733575
}
35743576

35753577
unifiedMemoryProperties.device = device;
3576-
unifiedMemoryProperties.subdeviceBitfield = neoDevice->getDefaultEngine().osContext->getDeviceBitfield();
35773578

35783579
return neoContext->getSVMAllocsManager()->createUnifiedMemoryAllocation(neoDevice->getRootDeviceIndex(), size, unifiedMemoryProperties);
35793580
}
@@ -3626,6 +3627,7 @@ void *clSharedMemAllocINTEL(
36263627
if (!ptr) {
36273628
err.set(CL_OUT_OF_RESOURCES);
36283629
}
3630+
36293631
return ptr;
36303632
}
36313633

opencl/source/memory_manager/os_agnostic_memory_manager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ GraphicsAllocation *OsAgnosticMemoryManager::allocate32BitGraphicsMemoryImpl(con
145145
memoryAllocation->set32BitAllocation(true);
146146
memoryAllocation->setGpuBaseAddress(GmmHelper::canonize(gfxPartition->getHeapBase(heap)));
147147
memoryAllocation->sizeToFree = allocationSize;
148+
memoryAllocation->storageInfo = allocationData.storageInfo;
148149
}
149150
counter++;
150151
return memoryAllocation;

opencl/test/unit_test/memory_manager/local_memory_usage_tests.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include "shared/source/helpers/basic_math.h"
9+
#include "shared/source/helpers/constants.h"
910
#include "shared/source/memory_manager/local_memory_usage.h"
1011
#include "shared/test/unit_test/helpers/debug_manager_state_restore.h"
1112

@@ -19,7 +20,12 @@ struct MockLocalMemoryUsageBankSelector : public LocalMemoryUsageBankSelector {
1920
using LocalMemoryUsageBankSelector::LocalMemoryUsageBankSelector;
2021
using LocalMemoryUsageBankSelector::reserveOnBank;
2122
using LocalMemoryUsageBankSelector::updateUsageInfo;
23+
DeviceBitfield bitfield;
2224
std::atomic<uint64_t> *getMemorySizes() { return memorySizes.get(); }
25+
26+
MockLocalMemoryUsageBankSelector(uint32_t banksCount) : LocalMemoryUsageBankSelector(banksCount) {
27+
bitfield = maxNBitValue(banksCount);
28+
}
2329
};
2430

2531
TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenItsCreatedAllValuesAreZero) {
@@ -34,7 +40,7 @@ TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenMemoryIsReserved
3440
MockLocalMemoryUsageBankSelector selector(4u);
3541

3642
uint64_t allocationSize = 1024u;
37-
auto bankIndex = selector.getLeastOccupiedBank();
43+
auto bankIndex = selector.getLeastOccupiedBank(selector.bitfield);
3844
selector.reserveOnBank(bankIndex, allocationSize);
3945

4046
EXPECT_EQ(allocationSize, selector.getOccupiedMemorySizeForBank(bankIndex));
@@ -44,11 +50,11 @@ TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenMemoryIsReleased
4450
MockLocalMemoryUsageBankSelector selector(1u);
4551

4652
uint64_t allocationSize = 1024u;
47-
auto bankIndex = selector.getLeastOccupiedBank();
53+
auto bankIndex = selector.getLeastOccupiedBank(selector.bitfield);
4854
EXPECT_EQ(0u, bankIndex);
4955
selector.reserveOnBank(bankIndex, allocationSize);
5056

51-
bankIndex = selector.getLeastOccupiedBank();
57+
bankIndex = selector.getLeastOccupiedBank(selector.bitfield);
5258
EXPECT_EQ(0u, bankIndex);
5359
selector.reserveOnBank(bankIndex, allocationSize);
5460

@@ -60,12 +66,12 @@ TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenMemoryIsReleased
6066
TEST(localMemoryUsageTest, givenOverrideLeastOccupiedBankDebugFlagWhenGetLeastOccupiedBankIsCalledThenForcedBankIndexIsReturned) {
6167
DebugManagerStateRestore dbgRestore;
6268
MockLocalMemoryUsageBankSelector selector(1u);
63-
auto bankIndex = selector.getLeastOccupiedBank();
69+
auto bankIndex = selector.getLeastOccupiedBank(selector.bitfield);
6470
EXPECT_EQ(0u, bankIndex);
6571

6672
uint32_t forcedBankIndex = 64u;
6773
DebugManager.flags.OverrideLeastOccupiedBank.set(static_cast<int32_t>(forcedBankIndex));
68-
bankIndex = selector.getLeastOccupiedBank();
74+
bankIndex = selector.getLeastOccupiedBank(selector.bitfield);
6975
EXPECT_EQ(forcedBankIndex, bankIndex);
7076
}
7177

@@ -74,15 +80,15 @@ TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenMemoryAllocatedS
7480

7581
uint64_t allocationSize = 1024u;
7682

77-
auto bankIndex = selector.getLeastOccupiedBank();
83+
auto bankIndex = selector.getLeastOccupiedBank(selector.bitfield);
7884
selector.reserveOnBank(bankIndex, allocationSize);
79-
bankIndex = selector.getLeastOccupiedBank();
85+
bankIndex = selector.getLeastOccupiedBank(selector.bitfield);
8086
selector.reserveOnBank(bankIndex, allocationSize);
81-
bankIndex = selector.getLeastOccupiedBank();
87+
bankIndex = selector.getLeastOccupiedBank(selector.bitfield);
8288
selector.reserveOnBank(bankIndex, allocationSize);
83-
bankIndex = selector.getLeastOccupiedBank();
89+
bankIndex = selector.getLeastOccupiedBank(selector.bitfield);
8490
selector.reserveOnBank(bankIndex, allocationSize);
85-
bankIndex = selector.getLeastOccupiedBank();
91+
bankIndex = selector.getLeastOccupiedBank(selector.bitfield);
8692
selector.reserveOnBank(bankIndex, allocationSize);
8793

8894
for (uint32_t i = 0; i < selector.banksCount; i++) {
@@ -148,4 +154,12 @@ TEST(localMemoryUsageTest, givenLocalMemoryUsageBankSelectorWhenThereAreMoreThan
148154
EXPECT_EQ(0u, selector.getOccupiedMemorySizeForBank(32));
149155
}
150156

157+
TEST(localMemoryUsageTest, givenBitfieldWhenGettingLeastOccupiedBankThenReturnTheProperOne) {
158+
MockLocalMemoryUsageBankSelector selector(2u);
159+
DeviceBitfield bitfield(0b10);
160+
auto bank = selector.getLeastOccupiedBank(bitfield);
161+
162+
EXPECT_EQ(bank, 1u);
163+
}
164+
151165
} // namespace NEO

shared/source/command_container/cmdcontainer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bool CommandContainer::initialize(Device *device) {
5656
GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY,
5757
(device->getNumAvailableDevices() > 1u) /* multiOsContextCapable */,
5858
false,
59-
{}};
59+
device->getDeviceBitfield()};
6060

6161
auto cmdBufferAllocation = device->getMemoryManager()->allocateGraphicsMemoryWithProperties(properties);
6262
UNRECOVERABLE_IF(!cmdBufferAllocation);
@@ -188,7 +188,7 @@ void CommandContainer::allocateNextCommandBuffer() {
188188
GraphicsAllocation::AllocationType::INTERNAL_HOST_MEMORY,
189189
(device->getNumAvailableDevices() > 1u) /* multiOsContextCapable */,
190190
false,
191-
{}};
191+
device->getDeviceBitfield()};
192192

193193
auto cmdBufferAllocation = device->getMemoryManager()->allocateGraphicsMemoryWithProperties(properties);
194194
UNRECOVERABLE_IF(!cmdBufferAllocation);

shared/source/helpers/heap_helper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ GraphicsAllocation *HeapHelper::getHeapAllocation(uint32_t heapType, size_t heap
2424
if (allocation) {
2525
return allocation.release();
2626
}
27-
NEO::AllocationProperties properties{rootDeviceIndex, true, heapSize, allocationType, isMultiOsContextCapable, false, {}};
27+
NEO::AllocationProperties properties{rootDeviceIndex, true, heapSize, allocationType, isMultiOsContextCapable, false, storageForReuse->getDeviceBitfield()};
2828
properties.alignment = alignment;
2929

3030
return this->memManager->allocateGraphicsMemoryWithProperties(properties);

shared/source/memory_manager/internal_allocation_storage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,8 @@ GraphicsAllocation *AllocationsList::detachAllocationImpl(GraphicsAllocation *,
117117
return nullptr;
118118
}
119119

120+
DeviceBitfield InternalAllocationStorage::getDeviceBitfield() const {
121+
return commandStreamReceiver.getOsContext().getDeviceBitfield();
122+
}
123+
120124
} // namespace NEO

shared/source/memory_manager/internal_allocation_storage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
*/
77

88
#pragma once
9+
#include "shared/source/helpers/common_types.h"
910
#include "shared/source/memory_manager/allocations_list.h"
1011

1112
namespace NEO {
12-
class CommandStreamReceiver;
1313

1414
class InternalAllocationStorage {
1515
public:
@@ -22,6 +22,7 @@ class InternalAllocationStorage {
2222
std::unique_ptr<GraphicsAllocation> obtainTemporaryAllocationWithPtr(size_t requiredSize, const void *requiredPtr, GraphicsAllocation::AllocationType allocationType);
2323
AllocationsList &getTemporaryAllocations() { return temporaryAllocations; }
2424
AllocationsList &getAllocationsForReuse() { return allocationsForReuse; }
25+
DeviceBitfield getDeviceBitfield() const;
2526

2627
protected:
2728
void freeAllocationsList(uint32_t waitTaskCount, AllocationsList &allocationsList);

shared/source/memory_manager/local_memory_usage.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "shared/source/memory_manager/local_memory_usage.h"
99

1010
#include "shared/source/debug_settings/debug_settings_manager.h"
11+
#include "shared/source/helpers/common_types.h"
1112

1213
#include <algorithm>
1314
#include <bitset>
@@ -24,13 +25,24 @@ LocalMemoryUsageBankSelector::LocalMemoryUsageBankSelector(uint32_t banksCount)
2425
}
2526
}
2627

27-
uint32_t LocalMemoryUsageBankSelector::getLeastOccupiedBank() {
28+
uint32_t LocalMemoryUsageBankSelector::getLeastOccupiedBank(DeviceBitfield deviceBitfield) {
2829
if (DebugManager.flags.OverrideLeastOccupiedBank.get() != -1) {
2930
return static_cast<uint32_t>(DebugManager.flags.OverrideLeastOccupiedBank.get());
3031
}
32+
uint32_t leastOccupiedBank = 0u;
33+
uint64_t smallestViableMemorySize = std::numeric_limits<uint64_t>::max();
3134

32-
auto leastOccupiedBankIterator = std::min_element(memorySizes.get(), memorySizes.get() + banksCount);
33-
return static_cast<uint32_t>(std::distance(memorySizes.get(), leastOccupiedBankIterator));
35+
UNRECOVERABLE_IF(deviceBitfield.count() == 0);
36+
for (uint32_t i = 0u; i < banksCount; i++) {
37+
if (deviceBitfield.test(i)) {
38+
if (memorySizes[i] < smallestViableMemorySize) {
39+
leastOccupiedBank = i;
40+
smallestViableMemorySize = memorySizes[i];
41+
}
42+
}
43+
}
44+
45+
return leastOccupiedBank;
3446
}
3547

3648
void LocalMemoryUsageBankSelector::freeOnBank(uint32_t bankIndex, uint64_t allocationSize) {

shared/source/memory_manager/local_memory_usage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#pragma once
99

10+
#include "shared/source/helpers/common_types.h"
1011
#include "shared/source/helpers/debug_helpers.h"
1112
#include "shared/source/helpers/non_copyable_or_moveable.h"
1213

@@ -18,7 +19,7 @@ class LocalMemoryUsageBankSelector : public NonCopyableOrMovableClass {
1819
public:
1920
LocalMemoryUsageBankSelector() = delete;
2021
LocalMemoryUsageBankSelector(uint32_t banksCount);
21-
uint32_t getLeastOccupiedBank();
22+
uint32_t getLeastOccupiedBank(DeviceBitfield deviceBitfield);
2223
void reserveOnBanks(uint32_t memoryBanks, uint64_t allocationSize) {
2324
updateUsageInfo(memoryBanks, allocationSize, true);
2425
}

0 commit comments

Comments
 (0)