Skip to content

Commit c98fdbc

Browse files
authored
Merge pull request #1439 from nrspruit/fix_device_native_proxy_buffer
[L0] Fix Native Host memory usage on device with copy back sync
2 parents 5f4dd11 + 9b3cf9d commit c98fdbc

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

source/adapters/level_zero/memory.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreateWithNativeHandle(
18331833
UR_CALL(
18341834
Buffer->getZeHandle(ZeHandleDst, ur_mem_handle_t_::write_only, Device));
18351835

1836+
// Indicate that this buffer has the device buffer mapped to a native buffer
1837+
// and track the native pointer such that the memory is synced later at
1838+
// memory free.
1839+
Buffer->DeviceMappedHostNativePtr = Ptr;
18361840
// zeCommandListAppendMemoryCopy must not be called from simultaneous
18371841
// threads with the same command list handle, so we need exclusive lock.
18381842
std::scoped_lock<ur_mutex> Lock(Context->ImmediateCommandListMutex);
@@ -2169,6 +2173,17 @@ ur_result_t _ur_buffer::free() {
21692173
? Plt->ContextsMutex
21702174
: UrContext->Mutex);
21712175

2176+
// If this memory was allocated as a proxy device buffer, then we must
2177+
// copy the final memory contents back to the original native pointer
2178+
// before releasing the buffer memory.
2179+
if (DeviceMappedHostNativePtr != nullptr) {
2180+
// zeCommandListAppendMemoryCopy must not be called from simultaneous
2181+
// threads with the same command list handle, so we need exclusive lock.
2182+
std::scoped_lock<ur_mutex> Lock(UrContext->ImmediateCommandListMutex);
2183+
ZE2UR_CALL(zeCommandListAppendMemoryCopy,
2184+
(UrContext->ZeCommandListInit, DeviceMappedHostNativePtr,
2185+
ZeHandle, Size, nullptr, 0, nullptr));
2186+
}
21722187
UR_CALL(USMFreeHelper(reinterpret_cast<ur_context_handle_t>(UrContext),
21732188
ZeHandle));
21742189
break;

source/adapters/level_zero/memory.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ struct _ur_buffer final : ur_mem_handle_t_ {
158158
// Tells the host allocation to use for buffer map operations.
159159
char *MapHostPtr{nullptr};
160160

161+
// Pointer to the original native buffer handle given this memory is a proxy
162+
// device buffer.
163+
void *DeviceMappedHostNativePtr{nullptr};
164+
161165
// Supplementary data to keep track of the mappings of this buffer
162166
// created with piEnqueueMemBufferMap.
163167
struct Mapping {

0 commit comments

Comments
 (0)