Skip to content

Commit 041299f

Browse files
[SYCL][Level Zero] Use MemAdvise for read-only shared allocs
Those don't affect stability: > Memory advice is a performance hint only and is not required for > functional correctness.
1 parent 10d4ae9 commit 041299f

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

sycl/plugins/level_zero/pi_level_zero.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8077,7 +8077,6 @@ static pi_result USMSharedAllocImpl(void **ResultPtr, pi_context Context,
80778077
pi_device Device,
80788078
pi_usm_mem_properties *Properties,
80798079
size_t Size, pi_uint32 Alignment) {
8080-
(void)Properties;
80818080
PI_ASSERT(Context, PI_ERROR_INVALID_CONTEXT);
80828081
PI_ASSERT(Device, PI_ERROR_INVALID_DEVICE);
80838082

@@ -8102,6 +8101,31 @@ static pi_result USMSharedAllocImpl(void **ResultPtr, pi_context Context,
81028101
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0,
81038102
PI_ERROR_INVALID_VALUE);
81048103

8104+
// See if the memory is going to be read-only on the device.
8105+
bool DeviceReadOnly = false;
8106+
// Check that incorrect bits are not set in the properties.
8107+
if (Properties && *Properties != 0) {
8108+
PI_ASSERT(*(Properties) == PI_MEM_ALLOC_FLAGS && *(Properties + 2) == 0,
8109+
PI_ERROR_INVALID_VALUE);
8110+
DeviceReadOnly = *(Properties + 1) & PI_MEM_ALLOC_DEVICE_READ_ONLY;
8111+
}
8112+
if (!DeviceReadOnly)
8113+
return PI_SUCCESS;
8114+
8115+
// For read-only memory, let L0 know about that by using advises.
8116+
8117+
// zeCommandListAppendMemAdvise must not be called from simultaneous threads
8118+
// with the same command list handle.
8119+
std::scoped_lock<pi_mutex> Lock(Context->ImmediateCommandListMutex);
8120+
8121+
ZE_CALL(zeCommandListAppendMemAdvise,
8122+
(Context->ZeCommandListInit, Device->ZeDevice, *ResultPtr, Size,
8123+
ZE_MEMORY_ADVICE_SET_READ_MOSTLY));
8124+
8125+
ZE_CALL(zeCommandListAppendMemAdvise,
8126+
(Context->ZeCommandListInit, Device->ZeDevice, *ResultPtr, Size,
8127+
ZE_MEMORY_ADVICE_SET_PREFERRED_LOCATION));
8128+
81058129
return PI_SUCCESS;
81068130
}
81078131

@@ -8153,7 +8177,9 @@ pi_result USMSharedMemoryAlloc::allocateImpl(void **ResultPtr, size_t Size,
81538177
pi_result USMSharedReadOnlyMemoryAlloc::allocateImpl(void **ResultPtr,
81548178
size_t Size,
81558179
pi_uint32 Alignment) {
8156-
return USMSharedAllocImpl(ResultPtr, Context, Device, nullptr, Size,
8180+
pi_usm_mem_properties Props[] = {PI_MEM_ALLOC_FLAGS,
8181+
PI_MEM_ALLOC_DEVICE_READ_ONLY, 0};
8182+
return USMSharedAllocImpl(ResultPtr, Context, Device, Props, Size,
81578183
Alignment);
81588184
}
81598185

0 commit comments

Comments
 (0)