Skip to content

Commit 88ca58b

Browse files
authored
[l0 v2] Add optional-returning USM pool config parser (#20350)
This PR contains 2 commits, first is just simplification of existing code, second is a rework to resolve TODO and provide the std::optional as a return value.
1 parent f120751 commit 88ca58b

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

unified-runtime/source/adapters/level_zero/usm.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ usm::DisjointPoolAllConfigs DisjointPoolConfigInstance =
4545
InitializeDisjointPoolConfig();
4646

4747
usm::DisjointPoolAllConfigs InitializeDisjointPoolConfig() {
48-
const char *PoolUrTraceVal = std::getenv("UR_L0_USM_ALLOCATOR_TRACE");
49-
const char *PoolPiTraceVal =
50-
std::getenv("SYCL_PI_LEVEL_ZERO_USM_ALLOCATOR_TRACE");
51-
const char *PoolTraceVal = PoolUrTraceVal
52-
? PoolUrTraceVal
53-
: (PoolPiTraceVal ? PoolPiTraceVal : nullptr);
48+
// Prefer the UR-specific env var, fall back to the PI-specific one.
49+
const char *PoolTraceVal = std::getenv("UR_L0_USM_ALLOCATOR_TRACE");
50+
if (!PoolTraceVal) {
51+
PoolTraceVal = std::getenv("SYCL_PI_LEVEL_ZERO_USM_ALLOCATOR_TRACE");
52+
}
5453

54+
// Parse integer value, defaulting to 0 on missing/invalid input.
5555
int PoolTrace = 0;
56-
if (PoolTraceVal != nullptr) {
56+
if (PoolTraceVal) {
5757
PoolTrace = std::atoi(PoolTraceVal);
5858
}
5959

60-
const char *PoolUrConfigVal = std::getenv("SYCL_PI_LEVEL_ZERO_USM_ALLOCATOR");
61-
const char *PoolPiConfigVal = std::getenv("UR_L0_USM_ALLOCATOR");
62-
const char *PoolConfigVal =
63-
PoolUrConfigVal ? PoolUrConfigVal : PoolPiConfigVal;
64-
if (PoolConfigVal == nullptr) {
60+
const char *PoolConfigVal = std::getenv("UR_L0_USM_ALLOCATOR");
61+
if (!PoolConfigVal) {
62+
PoolConfigVal = std::getenv("SYCL_PI_LEVEL_ZERO_USM_ALLOCATOR");
63+
}
64+
if (!PoolConfigVal) {
6565
return usm::DisjointPoolAllConfigs(PoolTrace);
6666
}
6767

unified-runtime/source/adapters/level_zero/usm.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
//===----------------------------------------------------------------------===//
1010
#pragma once
1111

12-
#include <set>
13-
1412
#include "common.hpp"
1513
#include "common/ur_ref_count.hpp"
1614
#include "enqueued_pool.hpp"

unified-runtime/source/adapters/level_zero/v2/usm.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,13 @@ ur_result_t getProviderNativeError(const char *providerName,
4343

4444
static std::optional<usm::DisjointPoolAllConfigs>
4545
initializeDisjointPoolConfig() {
46-
const char *UrRetDisable = std::getenv("UR_L0_DISABLE_USM_ALLOCATOR");
47-
const char *PiRetDisable =
48-
std::getenv("SYCL_PI_LEVEL_ZERO_DISABLE_USM_ALLOCATOR");
49-
const char *Disable =
50-
UrRetDisable ? UrRetDisable : (PiRetDisable ? PiRetDisable : nullptr);
51-
if (Disable != nullptr && Disable != std::string("")) {
46+
if (getenv_tobool("UR_L0_DISABLE_USM_ALLOCATOR") ||
47+
getenv_tobool("SYCL_PI_LEVEL_ZERO_DISABLE_USM_ALLOCATOR")) {
5248
return std::nullopt;
5349
}
5450

55-
const char *PoolUrTraceVal = std::getenv("UR_L0_USM_ALLOCATOR_TRACE");
56-
5751
int PoolTrace = 0;
58-
if (PoolUrTraceVal != nullptr) {
52+
if (auto PoolUrTraceVal = std::getenv("UR_L0_USM_ALLOCATOR_TRACE")) {
5953
PoolTrace = std::atoi(PoolUrTraceVal);
6054
}
6155

@@ -64,14 +58,7 @@ initializeDisjointPoolConfig() {
6458
return usm::DisjointPoolAllConfigs(PoolTrace);
6559
}
6660

67-
// TODO: rework parseDisjointPoolConfig to return optional,
68-
// once EnableBuffers is no longer used (by legacy L0)
69-
auto configs = usm::parseDisjointPoolConfig(PoolUrConfigVal, PoolTrace);
70-
if (configs.EnableBuffers) {
71-
return configs;
72-
}
73-
74-
return std::nullopt;
61+
return usm::parseDisjointPoolConfigOptional(PoolUrConfigVal, PoolTrace);
7562
}
7663

7764
inline umf_usm_memory_type_t urToUmfMemoryType(ur_usm_type_t type) {

unified-runtime/source/common/umf_pools/disjoint_pool_config_parser.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,13 @@ DisjointPoolAllConfigs parseDisjointPoolConfig(const std::string &config,
267267

268268
return AllConfigs;
269269
}
270+
271+
std::optional<DisjointPoolAllConfigs>
272+
parseDisjointPoolConfigOptional(const std::string &config, int trace) {
273+
auto configs = parseDisjointPoolConfig(config, trace);
274+
if (configs.EnableBuffers == 0) {
275+
return std::nullopt;
276+
}
277+
return configs;
278+
}
270279
} // namespace usm

unified-runtime/source/common/umf_pools/disjoint_pool_config_parser.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <umf/pools/pool_disjoint.h>
1313

1414
#include <memory>
15+
#include <optional>
1516
#include <string>
1617
#include <unordered_map>
1718

@@ -71,6 +72,13 @@ class DisjointPoolAllConfigs {
7172
DisjointPoolAllConfigs parseDisjointPoolConfig(const std::string &config,
7273
int trace = 1);
7374

75+
// Parse optional config parameters with optional return.
76+
// Returns std::nullopt when EnableBuffers is 0, indicating pooling is disabled.
77+
// This is for use cases where EnableBuffers flag controls pool creation,
78+
// particularly in the v2 Level Zero adapter.
79+
std::optional<DisjointPoolAllConfigs>
80+
parseDisjointPoolConfigOptional(const std::string &config, int trace = 1);
81+
7482
static inline void UMF_CALL_THROWS(umf_result_t res) {
7583
if (res != UMF_RESULT_SUCCESS) {
7684
throw res;

0 commit comments

Comments
 (0)