Skip to content

Commit 1e2a8f9

Browse files
committed
[UR][L0] Disable USM residency P2P restriction by default
Disable the "Restrict USM residency to peers with enabled P2P access" feature by default: P2P access is now treated as enabled for all connectable peer devices out of the box, so USM allocations are made resident on all of them without requiring an explicit ext_oneapi_enable_peer_access() call. When the restriction is disabled, urUsmP2P(Enable|Disable)PeerAccessExp become no-ops that always return success, matching the behavior of the Level Zero V1 adapter. Add the SYCL_UR_L0_RESTRICT_USM_RESIDENCY_TO_P2P environment variable to opt back into the restrictive behavior on demand, requiring P2P access to be explicitly enabled for a peer before USM allocations become resident on it, and enforcing the enable/disable state machine. Update the tests that exercise this feature (sycl/test-e2e/USM/P2P/p2p_usm_residency.cpp and unified-runtime/test/adapters/level_zero/v2/memory_residency.cpp) to set the new environment variable so they keep testing the restrictive behavior. Document the new variable in EnvironmentVariables.md. Signed-off-by: Lukasz Dorau <lukasz.dorau@intel.com>
1 parent 078d46c commit 1e2a8f9

6 files changed

Lines changed: 48 additions & 5 deletions

File tree

sycl/doc/EnvironmentVariables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ older hardware or when SYCL_UR_USE_LEVEL_ZERO_V2=0 is set.</span>
294294
| `SYCL_PI_LEVEL_ZERO_IMMEDIATE_COMMANDLISTS_EVENT_CLEANUP_THRESHOLD` | Integer | If non-negative then the threshold is set to this value. If negative, the threshold is set to INT_MAX. Whenever the number of events associated with an immediate command list exceeds this threshold, a check is made for signaled events and these events are recycled. Setting this threshold low causes events to be checked more often, which could result in unneeded events being recycled sooner. However, more frequent event status checks may cost time. The default is 1000. | Legacy |
295295
| `SYCL_PI_LEVEL_ZERO_USM_RESIDENT` | Integer | Bit-mask controls if/where to make USM allocations resident at the time of allocation. Input value is of the form 0xHSD, where 4-bits of D control device allocations, 4-bits of S control shared allocations, and 4-bits of H control host allocations. Each 4-bit component is holding one of the following values: "0" - then no special residency is forced, "1" - then allocation is made resident at the device of allocation, or "2" - then allocation is made resident on all devices in the context of allocation that have P2P access to the device of allocation. Default is 0x002, i.e. force full residency for device allocations only. | Legacy |
296296
| `SYCL_PI_LEVEL_ZERO_USE_NATIVE_USM_MEMCPY2D` | Integer | When set to a positive value enables the use of Level Zero USM 2D memory copy operations. Default is 0. | Legacy |
297+
| `SYCL_UR_L0_RESTRICT_USM_RESIDENCY_TO_P2P` | Any(\*) | By default, USM device allocations are made resident on all peer devices and `ext_oneapi_enable_peer_access`/`ext_oneapi_disable_peer_access` are no-ops. Setting this variable restricts USM allocation residency to only those peer devices that have had P2P access explicitly enabled via `ext_oneapi_enable_peer_access`, requiring it to be enabled before an allocation is made resident on a peer and allowing it to be disabled again. | V2 |
297298

298299
`(*) Note: Any means this environment variable is effective when set to any non-null value.`
299300

sycl/test-e2e/USM/P2P/p2p_usm_residency.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
// UNSUPPORTED-INTENDED: Test is specific to the Level Zero v2 adapter.
3030
//
3131
// RUN: %{build} -o %t.out
32-
// RUN: env UR_LOADER_USE_LEVEL_ZERO_V2=1 %{run} %t.out
32+
// RUN: %if level_zero %{env UR_LOADER_USE_LEVEL_ZERO_V2=1 SYCL_UR_L0_RESTRICT_USM_RESIDENCY_TO_P2P=1 %{run} %t.out %}
3333

3434
#include <iostream>
3535
#include <vector>

unified-runtime/source/adapters/level_zero/common/device.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "common/ur_ref_count.hpp"
2424
#include "helpers/shared_helpers.hpp"
2525
#include "interfaces.hpp"
26+
#include "ur_util.hpp"
2627
#include <unified-runtime/ur_ddi.h>
2728
#include <ur/ur.hpp>
2829
#include <ze_api.h>
@@ -35,6 +36,20 @@ typedef size_t DeviceId;
3536
struct ur_device_handle_t_;
3637
typedef struct ur_device_handle_t_ *ur_device_handle_t;
3738

39+
// Controls whether USM device memory residency on peer devices is restricted
40+
// to peers with explicitly enabled P2P access (see
41+
// urUsmP2P(Enable|Disable)PeerAccessExp). This is disabled by default, which
42+
// means P2P access is treated as enabled for all connectable peers and USM
43+
// allocations are made resident on all of them. Set
44+
// SYCL_UR_L0_RESTRICT_USM_RESIDENCY_TO_P2P=1 to opt into the restrictive
45+
// behavior, requiring P2P access to be explicitly enabled for a peer before
46+
// USM allocations become resident on it.
47+
inline bool restrictUsmResidencyToP2P() {
48+
static const bool RestrictUsmResidencyToP2P =
49+
getenv_tobool("SYCL_UR_L0_RESTRICT_USM_RESIDENCY_TO_P2P", false);
50+
return RestrictUsmResidencyToP2P;
51+
}
52+
3853
enum EventsScope {
3954
// All events are created host-visible.
4055
AllHostVisible,

unified-runtime/source/adapters/level_zero/common/platform.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,9 +1115,22 @@ ur_result_t ur_platform_handle_t_::populateDeviceCacheIfNeeded() {
11151115
continue;
11161116
}
11171117

1118-
UR_LOG(INFO, "p2p access to memory of dev:{} from dev:{} can be enabled",
1119-
peerId, dev->Id.value());
1120-
dev->peers[peerId] = ur_device_handle_t_::PeerStatus::DISABLED;
1118+
// By default (i.e. unless SYCL_UR_L0_RESTRICT_USM_RESIDENCY_TO_P2P is
1119+
// set), P2P access is treated as enabled for all connectable peers, so
1120+
// that USM allocations are made resident on all of them without
1121+
// requiring the user to call urUsmP2PEnablePeerAccessExp explicitly.
1122+
if (restrictUsmResidencyToP2P()) {
1123+
UR_LOG(INFO,
1124+
"p2p access to memory of dev:{} from dev:{} can be enabled",
1125+
peerId, dev->Id.value());
1126+
dev->peers[peerId] = ur_device_handle_t_::PeerStatus::DISABLED;
1127+
} else {
1128+
UR_LOG(INFO,
1129+
"p2p access to memory of dev:{} from dev:{} is enabled by "
1130+
"default",
1131+
peerId, dev->Id.value());
1132+
dev->peers[peerId] = ur_device_handle_t_::PeerStatus::ENABLED;
1133+
}
11211134
}
11221135
}
11231136

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ static ur_result_t urUsmP2PChangePeerAccessExp(ur_device_handle_t commandDevice,
4848
UR_LOG(INFO, "user tries to {} peer access to memory of {} from {}",
4949
(isAdding ? "enable" : "disable"), *peerDevice, *commandDevice);
5050

51+
if (!restrictUsmResidencyToP2P()) {
52+
// The restrictive P2P/residency feature is disabled (the default), so
53+
// P2P access is always considered enabled for connectable peers and USM
54+
// allocations are always resident on all of them. Ignore the requested
55+
// state change and report success, matching the behavior of the Level
56+
// Zero V1 adapter.
57+
UR_LOG(INFO,
58+
"ignored {} peer access to memory of {} from {}, because P2P is "
59+
"always enabled unless SYCL_UR_L0_RESTRICT_USM_RESIDENCY_TO_P2P is "
60+
"set",
61+
(isAdding ? "enabling" : "disabling"), *peerDevice, *commandDevice);
62+
return UR_RESULT_SUCCESS;
63+
}
64+
5165
{
5266
const auto expectedPeerStatus =
5367
isAdding ? ur_device_handle_t_::PeerStatus::DISABLED

unified-runtime/test/adapters/level_zero/v2/memory_residency.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55

6-
// RUN: %with-v2 ZES_ENABLE_SYSMAN=1 ./memory_residency-test
6+
// RUN: %with-v2 ZES_ENABLE_SYSMAN=1 SYCL_UR_L0_RESTRICT_USM_RESIDENCY_TO_P2P=1 ./memory_residency-test
77
// REQUIRES: v2
88

99
#include "uur/fixtures.h"

0 commit comments

Comments
 (0)