Skip to content

Commit fc61165

Browse files
committed
refactor MemPool options field
1 parent a591b0e commit fc61165

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

offload/plugins-nextgen/level_zero/include/L0Options.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,18 @@ struct L0OptionsTy {
7777
/// Staging buffer count
7878
size_t StagingBufferCount = L0StagingBufferCount;
7979

80-
// TODO: This should probably be an array indexed by AllocKind
8180
/// Memory pool parameters
8281
/// MemPoolInfo[MemType] = {AllocMax(MB), Capacity, PoolSize(MB)}
83-
std::map<int32_t, std::array<int32_t, 3>> MemPoolInfo = {
84-
{TARGET_ALLOC_DEVICE, {1, 4, 256}},
85-
{TARGET_ALLOC_HOST, {1, 4, 256}},
86-
{TARGET_ALLOC_SHARED, {8, 4, 256}}};
82+
struct MemPoolConfigTy {
83+
bool Use;
84+
int32_t AllocMax;
85+
int32_t Capacity;
86+
int32_t PoolSize;
87+
};
88+
std::array<MemPoolConfigTy, 3> MemPoolConfig{
89+
MemPoolConfigTy{true, 1, 4, 256}, // TARGET_ALLOC_DEVICE
90+
MemPoolConfigTy{true, 1, 4, 256}, // TARGET_ALLOC_HOST
91+
MemPoolConfigTy{true, 8, 4, 256}}; // TARGET_ALLOC_SHARED
8792

8893
/// Parameters for memory pools dedicated to reduction scratch space
8994
std::array<int32_t, 3> ReductionPoolInfo{256, 8, 8192};

offload/plugins-nextgen/level_zero/src/L0Memory.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ Error MemAllocatorTy::MemPoolTy::init(int32_t Kind, MemAllocatorTy *AllocatorIn,
6969
Allocator = AllocatorIn;
7070

7171
// Read user-defined options
72-
const auto &UserOptions = Option.MemPoolInfo.at(AllocKind);
73-
const size_t UserAllocMax = UserOptions[0];
74-
const size_t UserCapacity = UserOptions[1];
75-
const size_t UserPoolSize = UserOptions[2];
72+
const auto &UserOptions = Option.MemPoolConfig[AllocKind];
73+
const size_t UserAllocMax = UserOptions.AllocMax;
74+
const size_t UserCapacity = UserOptions.Capacity;
75+
const size_t UserPoolSize = UserOptions.PoolSize;
7676

7777
BlockCapacity = UserCapacity;
7878
PoolSizeMax = UserPoolSize << 20; // MB to B
@@ -373,7 +373,7 @@ Error MemAllocatorTy::initDevicePools(L0DeviceTy &L0Device,
373373
Device = &L0Device;
374374
L0Context = &L0Device.getL0Context();
375375
for (auto Kind : {TARGET_ALLOC_DEVICE, TARGET_ALLOC_SHARED}) {
376-
if (Options.MemPoolInfo.count(Kind) > 0) {
376+
if (Options.MemPoolConfig[Kind].Use) {
377377
std::lock_guard<std::mutex> Lock(Mtx);
378378
Pools[Kind] = std::make_unique<MemPoolTy>();
379379
if (auto Err = Pools[Kind]->init(Kind, this, Options))
@@ -395,7 +395,7 @@ Error MemAllocatorTy::initHostPool(L0ContextTy &Driver,
395395
SupportsLargeMem = Driver.supportsLargeMem();
396396
IsHostMem = true;
397397
this->L0Context = &Driver;
398-
if (Option.MemPoolInfo.count(TARGET_ALLOC_HOST) > 0) {
398+
if (Option.MemPoolConfig[TARGET_ALLOC_HOST].Use) {
399399
std::lock_guard<std::mutex> Lock(Mtx);
400400
Pools[TARGET_ALLOC_HOST] = std::make_unique<MemPoolTy>();
401401
if (auto Err =

offload/plugins-nextgen/level_zero/src/L0Options.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ void L0OptionsTy::processEnvironmentVars() {
4141
if (MemoryPoolVar.isPresent()) {
4242
if (MemoryPoolVar.get() == "0") {
4343
Flags.UseMemoryPool = 0;
44-
MemPoolInfo.clear();
44+
std::for_each(MemPoolConfig.begin(), MemPoolConfig.end(),
45+
[](auto &I) { I = {false, 0, 0, 0}; });
4546
} else {
4647
std::istringstream Str(MemoryPoolVar.get());
4748
int32_t MemType = -1;
@@ -50,19 +51,19 @@ void L0OptionsTy::processEnvironmentVars() {
5051
const std::array<int32_t, 3> DefaultValue{1, 4, 256};
5152
const int32_t AllMemType = INT32_MAX;
5253
std::array<int32_t, 3> AllInfo{1, 4, 256};
53-
std::map<int32_t, std::array<int32_t, 3>> PoolInfo;
54+
std::array<std::array<int32_t, 3>, 3> PoolInfo;
5455
for (std::string Token; std::getline(Str, Token, ',') && Valid > 0;) {
5556
if (Token == "device") {
5657
MemType = TARGET_ALLOC_DEVICE;
57-
PoolInfo.emplace(MemType, DefaultValue);
58+
PoolInfo[TARGET_ALLOC_DEVICE] = DefaultValue;
5859
Offset = 0;
5960
} else if (Token == "host") {
6061
MemType = TARGET_ALLOC_HOST;
61-
PoolInfo.emplace(MemType, DefaultValue);
62+
PoolInfo[TARGET_ALLOC_HOST] = DefaultValue;
6263
Offset = 0;
6364
} else if (Token == "shared") {
6465
MemType = TARGET_ALLOC_SHARED;
65-
PoolInfo.emplace(MemType, DefaultValue);
66+
PoolInfo[TARGET_ALLOC_SHARED] = DefaultValue;
6667
Offset = 0;
6768
} else if (Token == "all") {
6869
MemType = AllMemType;
@@ -87,19 +88,24 @@ void L0OptionsTy::processEnvironmentVars() {
8788
if (Valid == 2) {
8889
// "all" is specified -- ignore other inputs
8990
if (AllInfo[0] > 0) {
90-
MemPoolInfo[TARGET_ALLOC_DEVICE] = AllInfo;
91-
MemPoolInfo[TARGET_ALLOC_HOST] = AllInfo;
92-
MemPoolInfo[TARGET_ALLOC_SHARED] = std::move(AllInfo);
91+
MemPoolConfig[TARGET_ALLOC_DEVICE] = {true, AllInfo[0], AllInfo[1],
92+
AllInfo[2]};
93+
MemPoolConfig[TARGET_ALLOC_HOST] = {true, AllInfo[0], AllInfo[1],
94+
AllInfo[2]};
95+
MemPoolConfig[TARGET_ALLOC_SHARED] = {true, AllInfo[0], AllInfo[1],
96+
AllInfo[2]};
9397
} else {
94-
MemPoolInfo.clear();
98+
std::for_each(MemPoolConfig.begin(), MemPoolConfig.end(),
99+
[](auto &I) { I = {false, 0, 0, 0}; });
95100
}
96101
} else {
97-
// Use user-specified configuration
98-
for (auto &I : PoolInfo) {
99-
if (I.second[0] > 0)
100-
MemPoolInfo[I.first] = I.second;
101-
else
102-
MemPoolInfo.erase(I.first);
102+
for (size_t Pool = 0; Pool < PoolInfo.size(); ++Pool) {
103+
if (PoolInfo[Pool][0] == 0) {
104+
MemPoolConfig[Pool] = {false, 0, 0, 0};
105+
} else {
106+
MemPoolConfig[Pool] = {true, PoolInfo[Pool][0], PoolInfo[Pool][1],
107+
PoolInfo[Pool][2]};
108+
}
103109
}
104110
}
105111
} else {

0 commit comments

Comments
 (0)