Skip to content

Commit cd3bd6e

Browse files
committed
completion of copy-back path
Signed-off-by: Mateusz P. Nowak <mateusz.p.nowak@intel.com>
1 parent d002d08 commit cd3bd6e

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

sycl/test-e2e/lit.cfg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,7 @@ def get_sycl_ls_verbose(sycl_device, env):
11121112
features.update(sg_size_features)
11131113
features.update(architecture_feature)
11141114
features.update(device_family)
1115+
features.update(aspects)
11151116

11161117
be, dev = sycl_device.split(":")
11171118
features.add(dev.replace("fpga", "accelerator"))

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ ur_integrated_buffer_handle_t::ur_integrated_buffer_handle_t(
6666
if (ret == UR_RESULT_SUCCESS && memProps.type != ZE_MEMORY_TYPE_UNKNOWN) {
6767
// Already a USM allocation - just use it directly without import
6868
this->ptr = usm_unique_ptr_t(hostPtr, [](void *) {});
69-
// No copy-back needed for USM pointers
7069
return;
7170
}
7271

@@ -134,16 +133,52 @@ void *ur_integrated_buffer_handle_t::getDevicePtr(
134133
}
135134

136135
void *ur_integrated_buffer_handle_t::mapHostPtr(
137-
ur_map_flags_t /*flags*/, size_t offset, size_t /*size*/,
136+
ur_map_flags_t flags, size_t offset, size_t mapSize,
138137
ze_command_list_handle_t /*cmdList*/, wait_list_view & /*waitListView*/) {
139-
// For integrated devices, both device and host access the same memory
138+
if (writeBackPtr) {
139+
// Copy-back path: user gets back their original pointer
140+
void *mappedPtr = ur_cast<char *>(writeBackPtr) + offset;
141+
142+
if (flags & UR_MAP_FLAG_READ) {
143+
std::memcpy(mappedPtr, ur_cast<char *>(ptr.get()) + offset, mapSize);
144+
}
145+
146+
// Track this mapping for unmap
147+
mappedRegions.emplace_back(
148+
usm_unique_ptr_t(mappedPtr, [](void *) {}), mapSize, offset, flags);
149+
150+
return mappedPtr;
151+
}
152+
153+
// Zero-copy path: for successfully imported or USM pointers
140154
return ur_cast<char *>(ptr.get()) + offset;
141155
}
142156

143157
void ur_integrated_buffer_handle_t::unmapHostPtr(
144-
void * /*pMappedPtr*/, ze_command_list_handle_t /*cmdList*/,
158+
void *pMappedPtr, ze_command_list_handle_t /*cmdList*/,
145159
wait_list_view & /*waitListView*/) {
146-
// No-op: integrated buffers use zero-copy, no synchronization needed
160+
if (writeBackPtr) {
161+
// Copy-back path: find the mapped region and copy data back if needed
162+
auto mappedRegion =
163+
std::find_if(mappedRegions.begin(), mappedRegions.end(),
164+
[pMappedPtr](const host_allocation_desc_t &desc) {
165+
return desc.ptr.get() == pMappedPtr;
166+
});
167+
168+
if (mappedRegion == mappedRegions.end()) {
169+
UR_DFAILURE("could not find pMappedPtr:" << pMappedPtr);
170+
throw UR_RESULT_ERROR_INVALID_ARGUMENT;
171+
}
172+
173+
if (mappedRegion->flags & (UR_MAP_FLAG_WRITE | UR_MAP_FLAG_WRITE_INVALIDATE_REGION)) {
174+
std::memcpy(ur_cast<char *>(ptr.get()) + mappedRegion->offset,
175+
mappedRegion->ptr.get(), mappedRegion->size);
176+
}
177+
178+
mappedRegions.erase(mappedRegion);
179+
return;
180+
}
181+
// No op for zero-copy path, memory is synced
147182
}
148183

149184
static v2::raii::command_list_unique_handle

unified-runtime/source/adapters/level_zero/v2/memory.hpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ struct ur_usm_handle_t : ur_mem_buffer_t {
8787
void *ptr;
8888
};
8989

90+
struct host_allocation_desc_t {
91+
host_allocation_desc_t(usm_unique_ptr_t ptr, size_t size, size_t offset,
92+
ur_map_flags_t flags)
93+
: ptr(std::move(ptr)), size(size), offset(offset), flags(flags) {}
94+
95+
usm_unique_ptr_t ptr;
96+
size_t size;
97+
size_t offset;
98+
ur_map_flags_t flags;
99+
};
100+
90101
// Manages memory buffer for integrated GPU.
91102
// For integrated devices the buffer has been allocated in host memory
92103
// and can be accessed by the device without copying.
@@ -112,17 +123,7 @@ struct ur_integrated_buffer_handle_t : ur_mem_buffer_t {
112123
private:
113124
usm_unique_ptr_t ptr;
114125
void *writeBackPtr = nullptr;
115-
};
116-
117-
struct host_allocation_desc_t {
118-
host_allocation_desc_t(usm_unique_ptr_t ptr, size_t size, size_t offset,
119-
ur_map_flags_t flags)
120-
: ptr(std::move(ptr)), size(size), offset(offset), flags(flags) {}
121-
122-
usm_unique_ptr_t ptr;
123-
size_t size;
124-
size_t offset;
125-
ur_map_flags_t flags;
126+
std::vector<host_allocation_desc_t> mappedRegions;
126127
};
127128

128129
// Manages memory buffer for discrete GPU.

0 commit comments

Comments
 (0)