Skip to content

Commit 154e488

Browse files
committed
[SYCL][UR][L0 v2] fix urMemBufferCreateWithNativeHandle for host memory
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. Modify interop-level-zero-buffer.cpp test to verify that the buffer is properly initialized.
1 parent 51ce490 commit 154e488

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ int main() {
6262
// Check API
6363
void *HostBuffer1 = nullptr;
6464
zeMemAllocHost(ZeContext, &HostDesc, 10, 1, &HostBuffer1);
65+
std::fill(static_cast<char *>(HostBuffer1),
66+
static_cast<char *>(HostBuffer1) + 10, 'a');
6567

6668
backend_input_t<backend::ext_oneapi_level_zero, buffer<char, 1>>
6769
HostBufferInteropInput1 = {
@@ -74,6 +76,9 @@ int main() {
7476

7577
void *HostBuffer2 = nullptr;
7678
zeMemAllocHost(ZeContext, &HostDesc, 12 * sizeof(int), 1, &HostBuffer2);
79+
std::fill(static_cast<int *>(HostBuffer2),
80+
static_cast<int *>(HostBuffer2) + 12, 1);
81+
7782
backend_input_t<backend::ext_oneapi_level_zero, buffer<int, 1>>
7883
HostBufferInteropInput2 = {
7984
HostBuffer2, ext::oneapi::level_zero::ownership::transfer};
@@ -89,11 +94,11 @@ int main() {
8994

9095
CGH.single_task<class SimpleKernel1>([=]() {
9196
for (int i = 0; i < 10; i++) {
92-
Acc1[i] = 'a';
97+
Acc1[i] += 1;
9398
}
9499

95100
for (int i = 0; i < 12; i++) {
96-
Acc2[i] = 10;
101+
Acc2[i] += 10;
97102
}
98103
});
99104
});
@@ -102,12 +107,12 @@ int main() {
102107
{
103108
auto HostAcc1 = HostBufferInterop1.get_host_access();
104109
for (int i = 0; i < 10; i++) {
105-
assert(HostAcc1[i] == 'a');
110+
assert(HostAcc1[i] == 'b');
106111
}
107112

108113
auto HostAcc2 = HostBufferInterop2.get_host_access();
109114
for (int i = 0; i < 12; i++) {
110-
assert(HostAcc2[i] == 10);
115+
assert(HostAcc2[i] == 11);
111116
}
112117
}
113118

@@ -216,6 +221,8 @@ int main() {
216221
void *SharedBuffer = nullptr;
217222
zeMemAllocShared(ZeContext, &DeviceDesc, &HostDesc, 12 * sizeof(int), 1,
218223
nullptr, &SharedBuffer);
224+
std::fill(static_cast<int *>(SharedBuffer),
225+
static_cast<int *>(SharedBuffer) + 12, 1);
219226

220227
backend_input_t<backend::ext_oneapi_level_zero, buffer<int, 1>>
221228
SharedBufferInteropInput = {
@@ -242,7 +249,7 @@ int main() {
242249
DeviceBufferInterop.get_access<sycl::access::mode::read_write>(CGH);
243250
CGH.single_task<class SimpleKernel5>([=]() {
244251
for (int i = 0; i < 12; i++) {
245-
Acc1[i] = 77;
252+
Acc1[i] += 77;
246253
}
247254
for (int i = 0; i < 12; i++) {
248255
Acc2[i] = 99;
@@ -253,7 +260,7 @@ int main() {
253260
{
254261
auto HostAcc1 = SharedBufferInterop.get_host_access();
255262
for (int i = 0; i < 12; i++) {
256-
assert(HostAcc1[i] == 77);
263+
assert(HostAcc1[i] == 78);
257264
}
258265
auto HostAcc2 = DeviceBufferInterop.get_host_access();
259266
for (int i = 0; i < 12; i++) {

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,19 @@ ur_discrete_buffer_handle_t::ur_discrete_buffer_handle_t(
205205

206206
ur_discrete_buffer_handle_t::ur_discrete_buffer_handle_t(
207207
ur_context_handle_t hContext, ur_device_handle_t hDevice, void *devicePtr,
208-
size_t size, device_access_mode_t accessMode, void *writeBackMemory,
209-
bool ownZePtr)
208+
size_t size, device_access_mode_t accessMode, void *hostPtr, bool ownZePtr)
210209
: ur_mem_buffer_t(hContext, size, accessMode),
211210
deviceAllocations(hContext->getPlatform()->getNumDevices()),
212-
activeAllocationDevice(hDevice), writeBackPtr(writeBackMemory),
211+
activeAllocationDevice(hDevice), writeBackPtr(hostPtr),
213212
hostAllocations() {
214213

215214
if (!devicePtr) {
216215
hDevice = hDevice ? hDevice : hContext->getDevices()[0];
217216
devicePtr = allocateOnDevice(hDevice, size);
217+
218+
if (hostPtr) {
219+
UR_CALL_THROWS(migrateBufferTo(hDevice, hostPtr, size));
220+
}
218221
} else {
219222
assert(hDevice);
220223
deviceAllocations[hDevice->Id.value()] = usm_unique_ptr_t(

0 commit comments

Comments
 (0)