Skip to content

Commit b8b76f4

Browse files
authored
Merge pull request #2064 from igchor/memory_buffer
[L0 v2] add inital memory buffer support
2 parents 88c3287 + fc3f254 commit b8b76f4

24 files changed

+999
-1289
lines changed

.github/workflows/cmake.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
compiler: [{c: gcc, cxx: g++}]
2121
libbacktrace: ['-DVAL_USE_LIBBACKTRACE_BACKTRACE=OFF']
2222
pool_tracking: ['-DUMF_ENABLE_POOL_TRACKING=ON', '-DUMF_ENABLE_POOL_TRACKING=OFF']
23-
latency_tracking: ['-DUMF_ENABLE_LATENCY_TRACKING=OFF']
23+
latency_tracking: ['-DUR_ENABLE_LATENCY_HISTOGRAM=OFF']
2424
include:
2525
- os: 'ubuntu-22.04'
2626
build_type: Release
@@ -40,7 +40,7 @@ jobs:
4040
- os: 'ubuntu-22.04'
4141
build_type: Release
4242
compiler: {c: clang, cxx: clang++}
43-
latency_tracking: '-DUMF_ENABLE_LATENCY_TRACKING=ON'
43+
latency_tracking: '-DUR_ENABLE_LATENCY_HISTOGRAM=ON'
4444
runs-on: ${{ (matrix.os == 'ubuntu-22.04' && github.repository_owner == 'oneapi-src') && 'intel-ubuntu-22.04' || matrix.os }}
4545

4646
steps:

source/adapters/level_zero/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ if(UR_BUILD_ADAPTER_L0)
118118
${CMAKE_CURRENT_SOURCE_DIR}/queue.hpp
119119
${CMAKE_CURRENT_SOURCE_DIR}/sampler.hpp
120120
${CMAKE_CURRENT_SOURCE_DIR}/helpers/kernel_helpers.hpp
121+
${CMAKE_CURRENT_SOURCE_DIR}/helpers/memory_helpers.hpp
121122
${CMAKE_CURRENT_SOURCE_DIR}/ur_level_zero.cpp
122123
${CMAKE_CURRENT_SOURCE_DIR}/common.cpp
123124
${CMAKE_CURRENT_SOURCE_DIR}/context.cpp
@@ -136,6 +137,7 @@ if(UR_BUILD_ADAPTER_L0)
136137
${CMAKE_CURRENT_SOURCE_DIR}/sampler.cpp
137138
${CMAKE_CURRENT_SOURCE_DIR}/image.cpp
138139
${CMAKE_CURRENT_SOURCE_DIR}/helpers/kernel_helpers.cpp
140+
${CMAKE_CURRENT_SOURCE_DIR}/helpers/memory_helpers.cpp
139141
${CMAKE_CURRENT_SOURCE_DIR}/../../ur/ur.cpp
140142
)
141143

@@ -199,13 +201,15 @@ if(UR_BUILD_ADAPTER_L0_V2)
199201
${CMAKE_CURRENT_SOURCE_DIR}/platform.hpp
200202
${CMAKE_CURRENT_SOURCE_DIR}/program.hpp
201203
${CMAKE_CURRENT_SOURCE_DIR}/helpers/kernel_helpers.hpp
204+
${CMAKE_CURRENT_SOURCE_DIR}/helpers/memory_helpers.hpp
202205
${CMAKE_CURRENT_SOURCE_DIR}/adapter.cpp
203206
${CMAKE_CURRENT_SOURCE_DIR}/common.cpp
204207
${CMAKE_CURRENT_SOURCE_DIR}/device.cpp
205208
${CMAKE_CURRENT_SOURCE_DIR}/ur_interface_loader.cpp
206209
${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp
207210
${CMAKE_CURRENT_SOURCE_DIR}/program.cpp
208211
${CMAKE_CURRENT_SOURCE_DIR}/helpers/kernel_helpers.cpp
212+
${CMAKE_CURRENT_SOURCE_DIR}/helpers/memory_helpers.cpp
209213
${CMAKE_CURRENT_SOURCE_DIR}/../../ur/ur.cpp
210214
# v2-only sources
211215
${CMAKE_CURRENT_SOURCE_DIR}/v2/command_list_cache.hpp
@@ -217,6 +221,7 @@ if(UR_BUILD_ADAPTER_L0_V2)
217221
${CMAKE_CURRENT_SOURCE_DIR}/v2/event_provider.hpp
218222
${CMAKE_CURRENT_SOURCE_DIR}/v2/event.hpp
219223
${CMAKE_CURRENT_SOURCE_DIR}/v2/kernel.hpp
224+
${CMAKE_CURRENT_SOURCE_DIR}/v2/memory.hpp
220225
${CMAKE_CURRENT_SOURCE_DIR}/v2/queue_api.hpp
221226
${CMAKE_CURRENT_SOURCE_DIR}/v2/queue_immediate_in_order.hpp
222227
${CMAKE_CURRENT_SOURCE_DIR}/v2/usm.hpp
@@ -229,6 +234,7 @@ if(UR_BUILD_ADAPTER_L0_V2)
229234
${CMAKE_CURRENT_SOURCE_DIR}/v2/event_provider_normal.cpp
230235
${CMAKE_CURRENT_SOURCE_DIR}/v2/event.cpp
231236
${CMAKE_CURRENT_SOURCE_DIR}/v2/kernel.cpp
237+
${CMAKE_CURRENT_SOURCE_DIR}/v2/memory.cpp
232238
${CMAKE_CURRENT_SOURCE_DIR}/v2/queue_api.cpp
233239
${CMAKE_CURRENT_SOURCE_DIR}/v2/queue_create.cpp
234240
${CMAKE_CURRENT_SOURCE_DIR}/v2/queue_immediate_in_order.cpp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===--------- memory_helpers.cpp - Level Zero Adapter -------------------===//
2+
//
3+
// Copyright (C) 2024 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
#include "memory_helpers.hpp"
12+
#include "../common.hpp"
13+
14+
ze_memory_type_t getMemoryType(ze_context_handle_t hContext, void *ptr) {
15+
// TODO: use UMF once
16+
// https://github.com/oneapi-src/unified-memory-framework/issues/687 is
17+
// implemented
18+
ZeStruct<ze_memory_allocation_properties_t> zeMemoryAllocationProperties;
19+
ZE2UR_CALL_THROWS(zeMemGetAllocProperties,
20+
(hContext, ptr, &zeMemoryAllocationProperties, nullptr));
21+
return zeMemoryAllocationProperties.type;
22+
}
23+
24+
bool maybeImportUSM(ze_driver_handle_t hTranslatedDriver,
25+
ze_context_handle_t hContext, void *ptr, size_t size) {
26+
if (ZeUSMImport.Enabled && ptr != nullptr &&
27+
getMemoryType(hContext, ptr) == ZE_MEMORY_TYPE_UNKNOWN) {
28+
// Promote the host ptr to USM host memory
29+
ZeUSMImport.doZeUSMImport(hTranslatedDriver, ptr, size);
30+
return true;
31+
}
32+
return false;
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===--------- memory_helpers.hpp - Level Zero Adapter -------------------===//
2+
//
3+
// Copyright (C) 2024 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
#pragma once
11+
12+
#include <ur_api.h>
13+
#include <ze_api.h>
14+
15+
// If USM Import feature is enabled and hostptr is supplied,
16+
// import the hostptr if not already imported into USM.
17+
// Data transfer rate is maximized when both source and destination
18+
// are USM pointers. Promotion of the host pointer to USM thus
19+
// optimizes data transfer performance.
20+
bool maybeImportUSM(ze_driver_handle_t hTranslatedDriver,
21+
ze_context_handle_t hContext, void *ptr, size_t size);
22+
23+
ze_memory_type_t getMemoryType(ze_context_handle_t hContext, void *ptr);

source/adapters/level_zero/memory.cpp

+5-23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "context.hpp"
1717
#include "event.hpp"
18+
#include "helpers/memory_helpers.hpp"
1819
#include "image.hpp"
1920
#include "logger/ur_logger.hpp"
2021
#include "queue.hpp"
@@ -1599,30 +1600,11 @@ ur_result_t urMemBufferCreate(
15991600
Host = Properties->pHost;
16001601
}
16011602

1602-
// If USM Import feature is enabled and hostptr is supplied,
1603-
// import the hostptr if not already imported into USM.
1604-
// Data transfer rate is maximized when both source and destination
1605-
// are USM pointers. Promotion of the host pointer to USM thus
1606-
// optimizes data transfer performance.
16071603
bool HostPtrImported = false;
1608-
if (ZeUSMImport.Enabled && Host != nullptr &&
1609-
(Flags & UR_MEM_FLAG_USE_HOST_POINTER) != 0) {
1610-
// Query memory type of the host pointer
1611-
ze_device_handle_t ZeDeviceHandle;
1612-
ZeStruct<ze_memory_allocation_properties_t> ZeMemoryAllocationProperties;
1613-
ZE2UR_CALL(zeMemGetAllocProperties,
1614-
(Context->ZeContext, Host, &ZeMemoryAllocationProperties,
1615-
&ZeDeviceHandle));
1616-
1617-
// If not shared of any type, we can import the ptr
1618-
if (ZeMemoryAllocationProperties.type == ZE_MEMORY_TYPE_UNKNOWN) {
1619-
// Promote the host ptr to USM host memory
1620-
ze_driver_handle_t driverHandle =
1621-
Context->getPlatform()->ZeDriverHandleExpTranslated;
1622-
ZeUSMImport.doZeUSMImport(driverHandle, Host, Size);
1623-
HostPtrImported = true;
1624-
}
1625-
}
1604+
if (Flags & UR_MEM_FLAG_USE_HOST_POINTER)
1605+
HostPtrImported =
1606+
maybeImportUSM(Context->getPlatform()->ZeDriverHandleExpTranslated,
1607+
Context->ZeContext, Host, Size);
16261608

16271609
_ur_buffer *Buffer = nullptr;
16281610
auto HostPtrOrNull = (Flags & UR_MEM_FLAG_USE_HOST_POINTER)

source/adapters/level_zero/v2/api.cpp

-89
Original file line numberDiff line numberDiff line change
@@ -49,46 +49,13 @@ ur_result_t urMemImageCreate(ur_context_handle_t hContext, ur_mem_flags_t flags,
4949
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
5050
}
5151

52-
ur_result_t urMemBufferCreate(ur_context_handle_t hContext,
53-
ur_mem_flags_t flags, size_t size,
54-
const ur_buffer_properties_t *pProperties,
55-
ur_mem_handle_t *phBuffer) {
56-
logger::error("{} function not implemented!", __FUNCTION__);
57-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
58-
}
59-
60-
ur_result_t urMemRetain(ur_mem_handle_t hMem) {
61-
logger::error("{} function not implemented!", __FUNCTION__);
62-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
63-
}
64-
65-
ur_result_t urMemRelease(ur_mem_handle_t hMem) {
66-
logger::error("{} function not implemented!", __FUNCTION__);
67-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
68-
}
69-
70-
ur_result_t urMemBufferPartition(ur_mem_handle_t hBuffer, ur_mem_flags_t flags,
71-
ur_buffer_create_type_t bufferCreateType,
72-
const ur_buffer_region_t *pRegion,
73-
ur_mem_handle_t *phMem) {
74-
logger::error("{} function not implemented!", __FUNCTION__);
75-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
76-
}
77-
7852
ur_result_t urMemGetNativeHandle(ur_mem_handle_t hMem,
7953
ur_device_handle_t hDevice,
8054
ur_native_handle_t *phNativeMem) {
8155
logger::error("{} function not implemented!", __FUNCTION__);
8256
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
8357
}
8458

85-
ur_result_t urMemBufferCreateWithNativeHandle(
86-
ur_native_handle_t hNativeMem, ur_context_handle_t hContext,
87-
const ur_mem_native_properties_t *pProperties, ur_mem_handle_t *phMem) {
88-
logger::error("{} function not implemented!", __FUNCTION__);
89-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
90-
}
91-
9259
ur_result_t urMemImageCreateWithNativeHandle(
9360
ur_native_handle_t hNativeMem, ur_context_handle_t hContext,
9461
const ur_image_format_t *pImageFormat, const ur_image_desc_t *pImageDesc,
@@ -217,48 +184,13 @@ ur_result_t urPhysicalMemRelease(ur_physical_mem_handle_t hPhysicalMem) {
217184
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
218185
}
219186

220-
ur_result_t
221-
urKernelSetArgLocal(ur_kernel_handle_t hKernel, uint32_t argIndex,
222-
size_t argSize,
223-
const ur_kernel_arg_local_properties_t *pProperties) {
224-
logger::error("{} function not implemented!", __FUNCTION__);
225-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
226-
}
227-
228187
ur_result_t urKernelGetInfo(ur_kernel_handle_t hKernel,
229188
ur_kernel_info_t propName, size_t propSize,
230189
void *pPropValue, size_t *pPropSizeRet) {
231190
logger::error("{} function not implemented!", __FUNCTION__);
232191
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
233192
}
234193

235-
ur_result_t urKernelGetGroupInfo(ur_kernel_handle_t hKernel,
236-
ur_device_handle_t hDevice,
237-
ur_kernel_group_info_t propName,
238-
size_t propSize, void *pPropValue,
239-
size_t *pPropSizeRet) {
240-
logger::error("{} function not implemented!", __FUNCTION__);
241-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
242-
}
243-
244-
ur_result_t urKernelGetSubGroupInfo(ur_kernel_handle_t hKernel,
245-
ur_device_handle_t hDevice,
246-
ur_kernel_sub_group_info_t propName,
247-
size_t propSize, void *pPropValue,
248-
size_t *pPropSizeRet) {
249-
logger::error("{} function not implemented!", __FUNCTION__);
250-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
251-
}
252-
253-
ur_result_t
254-
urKernelSetExecInfo(ur_kernel_handle_t hKernel, ur_kernel_exec_info_t propName,
255-
size_t propSize,
256-
const ur_kernel_exec_info_properties_t *pProperties,
257-
const void *pPropValue) {
258-
logger::error("{} function not implemented!", __FUNCTION__);
259-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
260-
}
261-
262194
ur_result_t
263195
urKernelSetArgSampler(ur_kernel_handle_t hKernel, uint32_t argIndex,
264196
const ur_kernel_arg_sampler_properties_t *pProperties,
@@ -267,14 +199,6 @@ urKernelSetArgSampler(ur_kernel_handle_t hKernel, uint32_t argIndex,
267199
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
268200
}
269201

270-
ur_result_t
271-
urKernelSetArgMemObj(ur_kernel_handle_t hKernel, uint32_t argIndex,
272-
const ur_kernel_arg_mem_obj_properties_t *pProperties,
273-
ur_mem_handle_t hArgValue) {
274-
logger::error("{} function not implemented!", __FUNCTION__);
275-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
276-
}
277-
278202
ur_result_t urKernelSetSpecializationConstants(
279203
ur_kernel_handle_t hKernel, uint32_t count,
280204
const ur_specialization_constant_info_t *pSpecConstants) {
@@ -308,13 +232,6 @@ ur_result_t urKernelGetSuggestedLocalWorkSize(ur_kernel_handle_t hKernel,
308232
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
309233
}
310234

311-
ur_result_t urEventGetInfo(ur_event_handle_t hEvent, ur_event_info_t propName,
312-
size_t propSize, void *pPropValue,
313-
size_t *pPropSizeRet) {
314-
logger::error("{} function not implemented!", __FUNCTION__);
315-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
316-
}
317-
318235
ur_result_t urEventGetProfilingInfo(ur_event_handle_t hEvent,
319236
ur_profiling_info_t propName,
320237
size_t propSize, void *pPropValue,
@@ -323,12 +240,6 @@ ur_result_t urEventGetProfilingInfo(ur_event_handle_t hEvent,
323240
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
324241
}
325242

326-
ur_result_t urEventWait(uint32_t numEvents,
327-
const ur_event_handle_t *phEventWaitList) {
328-
logger::error("{} function not implemented!", __FUNCTION__);
329-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
330-
}
331-
332243
ur_result_t urEventGetNativeHandle(ur_event_handle_t hEvent,
333244
ur_native_handle_t *phNativeEvent) {
334245
logger::error("{} function not implemented!", __FUNCTION__);

source/adapters/level_zero/v2/context.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ ur_result_t urContextGetInfo(ur_context_handle_t hContext,
103103
return ReturnValue(uint32_t(hContext->getDevices().size()));
104104
case UR_CONTEXT_INFO_REFERENCE_COUNT:
105105
return ReturnValue(uint32_t{hContext->RefCount.load()});
106+
case UR_CONTEXT_INFO_USM_MEMCPY2D_SUPPORT:
107+
// TODO: this is currently not implemented
108+
return ReturnValue(uint8_t{false});
109+
case UR_CONTEXT_INFO_USM_FILL2D_SUPPORT:
110+
// 2D USM fill is not supported.
111+
return ReturnValue(uint8_t{false});
106112
default:
107113
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
108114
}

source/adapters/level_zero/v2/event.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,41 @@ ur_result_t urEventRetain(ur_event_handle_t hEvent) { return hEvent->retain(); }
5151
ur_result_t urEventRelease(ur_event_handle_t hEvent) {
5252
return hEvent->release();
5353
}
54+
55+
ur_result_t urEventWait(uint32_t numEvents,
56+
const ur_event_handle_t *phEventWaitList) {
57+
for (uint32_t i = 0; i < numEvents; ++i) {
58+
ZE2UR_CALL(zeEventHostSynchronize,
59+
(phEventWaitList[i]->getZeEvent(), UINT64_MAX));
60+
}
61+
return UR_RESULT_SUCCESS;
62+
}
63+
64+
ur_result_t urEventGetInfo(ur_event_handle_t hEvent, ur_event_info_t propName,
65+
size_t propValueSize, void *pPropValue,
66+
size_t *pPropValueSizeRet) {
67+
UrReturnHelper returnValue(propValueSize, pPropValue, pPropValueSizeRet);
68+
69+
switch (propName) {
70+
case UR_EVENT_INFO_COMMAND_EXECUTION_STATUS: {
71+
auto zeStatus = ZE_CALL_NOCHECK(zeEventQueryStatus, (hEvent->getZeEvent()));
72+
73+
if (zeStatus == ZE_RESULT_NOT_READY) {
74+
return returnValue(UR_EVENT_STATUS_SUBMITTED);
75+
} else {
76+
return returnValue(UR_EVENT_STATUS_COMPLETE);
77+
}
78+
}
79+
case UR_EVENT_INFO_REFERENCE_COUNT: {
80+
return returnValue(hEvent->RefCount.load());
81+
}
82+
default:
83+
logger::error(
84+
"Unsupported ParamName in urEventGetInfo: ParamName=ParamName={}(0x{})",
85+
propName, logger::toHex(propName));
86+
return UR_RESULT_ERROR_INVALID_VALUE;
87+
}
88+
89+
return UR_RESULT_SUCCESS;
90+
}
5491
} // namespace ur::level_zero

source/adapters/level_zero/v2/event_provider_counter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ event_allocation provider_counter::allocate() {
3939
if (freelist.empty()) {
4040
ZeStruct<ze_event_desc_t> desc;
4141
desc.index = 0;
42-
desc.signal = 0;
42+
desc.signal = ZE_EVENT_SCOPE_FLAG_HOST;
4343
desc.wait = 0;
4444
ze_event_handle_t handle;
4545

source/adapters/level_zero/v2/event_provider_normal.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ provider_pool::provider_pool(ur_context_handle_t context,
5151
for (int i = 0; i < EVENTS_BURST; ++i) {
5252
ZeStruct<ze_event_desc_t> desc;
5353
desc.index = i;
54-
desc.signal = 0;
54+
desc.signal = ZE_EVENT_SCOPE_FLAG_HOST;
5555
desc.wait = 0;
5656
ZE2UR_CALL_THROWS(zeEventCreate, (pool.get(), &desc, freelist[i].ptr()));
5757
}

0 commit comments

Comments
 (0)