Skip to content

Commit 59915b6

Browse files
igchorKornevNikita
authored andcommitted
[SYCL][UR][L0 v2] Fix urMemBufferCreateWithNativeHandle for host memory (#18066)
In case of creating a buffer from native host memory pointer, there was a missing initialization step. Host memory content was not being copied to the underlying buffer device memory.
1 parent e7f8cfe commit 59915b6

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

sycl/test-e2e/Adapters/interop-level-zero-buffer.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ int main() {
5454
// Check API
5555
void *HostBuffer1 = nullptr;
5656
zeMemAllocHost(ZeContext, &HostDesc, 10, 1, &HostBuffer1);
57+
std::fill(static_cast<char *>(HostBuffer1),
58+
static_cast<char *>(HostBuffer1) + 10, 'a');
5759

5860
backend_input_t<backend::ext_oneapi_level_zero, buffer<char, 1>>
5961
HostBufferInteropInput1 = {
@@ -66,6 +68,9 @@ int main() {
6668

6769
void *HostBuffer2 = nullptr;
6870
zeMemAllocHost(ZeContext, &HostDesc, 12 * sizeof(int), 1, &HostBuffer2);
71+
std::fill(static_cast<int *>(HostBuffer2),
72+
static_cast<int *>(HostBuffer2) + 12, 1);
73+
6974
backend_input_t<backend::ext_oneapi_level_zero, buffer<int, 1>>
7075
HostBufferInteropInput2 = {
7176
HostBuffer2, ext::oneapi::level_zero::ownership::transfer};
@@ -81,11 +86,11 @@ int main() {
8186

8287
CGH.single_task<class SimpleKernel1>([=]() {
8388
for (int i = 0; i < 10; i++) {
84-
Acc1[i] = 'a';
89+
Acc1[i] += 1;
8590
}
8691

8792
for (int i = 0; i < 12; i++) {
88-
Acc2[i] = 10;
93+
Acc2[i] += 10;
8994
}
9095
});
9196
});
@@ -94,12 +99,12 @@ int main() {
9499
{
95100
auto HostAcc1 = HostBufferInterop1.get_host_access();
96101
for (int i = 0; i < 10; i++) {
97-
assert(HostAcc1[i] == 'a');
102+
assert(HostAcc1[i] == 'b');
98103
}
99104

100105
auto HostAcc2 = HostBufferInterop2.get_host_access();
101106
for (int i = 0; i < 12; i++) {
102-
assert(HostAcc2[i] == 10);
107+
assert(HostAcc2[i] == 11);
103108
}
104109
}
105110

@@ -208,6 +213,8 @@ int main() {
208213
void *SharedBuffer = nullptr;
209214
zeMemAllocShared(ZeContext, &DeviceDesc, &HostDesc, 12 * sizeof(int), 1,
210215
nullptr, &SharedBuffer);
216+
std::fill(static_cast<int *>(SharedBuffer),
217+
static_cast<int *>(SharedBuffer) + 12, 1);
211218

212219
backend_input_t<backend::ext_oneapi_level_zero, buffer<int, 1>>
213220
SharedBufferInteropInput = {
@@ -234,7 +241,7 @@ int main() {
234241
DeviceBufferInterop.get_access<sycl::access::mode::read_write>(CGH);
235242
CGH.single_task<class SimpleKernel5>([=]() {
236243
for (int i = 0; i < 12; i++) {
237-
Acc1[i] = 77;
244+
Acc1[i] += 77;
238245
}
239246
for (int i = 0; i < 12; i++) {
240247
Acc2[i] = 99;
@@ -245,7 +252,7 @@ int main() {
245252
{
246253
auto HostAcc1 = SharedBufferInterop.get_host_access();
247254
for (int i = 0; i < 12; i++) {
248-
assert(HostAcc1[i] == 77);
255+
assert(HostAcc1[i] == 78);
249256
}
250257
auto HostAcc2 = DeviceBufferInterop.get_host_access();
251258
for (int i = 0; i < 12; i++) {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,19 @@ ur_discrete_buffer_handle_t::ur_discrete_buffer_handle_t(
221221

222222
ur_discrete_buffer_handle_t::ur_discrete_buffer_handle_t(
223223
ur_context_handle_t hContext, ur_device_handle_t hDevice, void *devicePtr,
224-
size_t size, device_access_mode_t accessMode, void *writeBackMemory,
225-
bool ownZePtr)
224+
size_t size, device_access_mode_t accessMode, void *hostPtr, bool ownZePtr)
226225
: ur_mem_buffer_t(hContext, size, accessMode),
227226
deviceAllocations(hContext->getPlatform()->getNumDevices()),
228-
activeAllocationDevice(hDevice), writeBackPtr(writeBackMemory),
227+
activeAllocationDevice(hDevice), writeBackPtr(hostPtr),
229228
hostAllocations() {
230229

231230
if (!devicePtr) {
232231
hDevice = hDevice ? hDevice : hContext->getDevices()[0];
233232
devicePtr = allocateOnDevice(hDevice, size);
233+
234+
if (hostPtr) {
235+
UR_CALL_THROWS(migrateBufferTo(hDevice, hostPtr, size));
236+
}
234237
} else {
235238
assert(hDevice);
236239
deviceAllocations[hDevice->Id.value()] = usm_unique_ptr_t(

0 commit comments

Comments
 (0)