Skip to content

Commit 5a6ad4b

Browse files
committed
[L0 v2] add inital memory buffer support
Currently there is no support for buffer migration. This will be done in next PRs.
1 parent bf4498d commit 5a6ad4b

16 files changed

+389
-584
lines changed

source/adapters/level_zero/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
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
Lines changed: 33 additions & 0 deletions
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+
}
Lines changed: 23 additions & 0 deletions
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

Lines changed: 5 additions & 23 deletions
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

Lines changed: 0 additions & 41 deletions
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,
@@ -258,14 +225,6 @@ urKernelSetArgSampler(ur_kernel_handle_t hKernel, uint32_t argIndex,
258225
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
259226
}
260227

261-
ur_result_t
262-
urKernelSetArgMemObj(ur_kernel_handle_t hKernel, uint32_t argIndex,
263-
const ur_kernel_arg_mem_obj_properties_t *pProperties,
264-
ur_mem_handle_t hArgValue) {
265-
logger::error("{} function not implemented!", __FUNCTION__);
266-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
267-
}
268-
269228
ur_result_t urKernelSetSpecializationConstants(
270229
ur_kernel_handle_t hKernel, uint32_t count,
271230
const ur_specialization_constant_info_t *pSpecConstants) {

source/adapters/level_zero/v2/kernel.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212

1313
#include "context.hpp"
1414
#include "kernel.hpp"
15+
#include "memory.hpp"
1516

1617
#include "../device.hpp"
1718
#include "../platform.hpp"
1819
#include "../program.hpp"
1920
#include "../ur_interface_loader.hpp"
2021

21-
ur_single_device_kernel_t::ur_single_device_kernel_t(ze_device_handle_t hDevice,
22+
ur_single_device_kernel_t::ur_single_device_kernel_t(ur_device_handle_t hDevice,
2223
ze_kernel_handle_t hKernel,
2324
bool ownZeHandle)
2425
: hDevice(hDevice), hKernel(hKernel, ownZeHandle) {
@@ -54,7 +55,7 @@ ur_kernel_handle_t_::ur_kernel_handle_t_(ur_program_handle_t hProgram,
5455
assert(urDevice != hProgram->Context->getDevices().end());
5556
auto deviceId = (*urDevice)->Id.value();
5657

57-
deviceKernels[deviceId].emplace(zeDevice, zeKernel, true);
58+
deviceKernels[deviceId].emplace(*urDevice, zeKernel, true);
5859
}
5960
completeInitialization();
6061
}
@@ -118,7 +119,7 @@ ur_kernel_handle_t_::getZeHandle(ur_device_handle_t hDevice) {
118119
auto &kernel = deviceKernels[0].value();
119120

120121
// hDevice is nullptr for native handle
121-
if ((kernel.hDevice != nullptr && kernel.hDevice != hDevice->ZeDevice)) {
122+
if ((kernel.hDevice != nullptr && kernel.hDevice != hDevice)) {
122123
throw UR_RESULT_ERROR_INVALID_DEVICE;
123124
}
124125

@@ -239,6 +240,16 @@ ur_result_t ur_kernel_handle_t_::setExecInfo(ur_kernel_exec_info_t propName,
239240
return UR_RESULT_SUCCESS;
240241
}
241242

243+
std::vector<ur_device_handle_t> ur_kernel_handle_t_::getDevices() const {
244+
std::vector<ur_device_handle_t> devices;
245+
for (size_t i = 0; i < deviceKernels.size(); ++i) {
246+
if (deviceKernels[i].has_value()) {
247+
devices.push_back(deviceKernels[i].value().hDevice);
248+
}
249+
}
250+
return devices;
251+
}
252+
242253
namespace ur::level_zero {
243254
ur_result_t urKernelCreate(ur_program_handle_t hProgram,
244255
const char *pKernelName,
@@ -291,6 +302,28 @@ ur_result_t urKernelSetArgPointer(
291302
return hKernel->setArgPointer(argIndex, pProperties, pArgValue);
292303
}
293304

305+
ur_result_t
306+
urKernelSetArgMemObj(ur_kernel_handle_t hKernel, uint32_t argIndex,
307+
const ur_kernel_arg_mem_obj_properties_t *pProperties,
308+
ur_mem_handle_t hArgValue) {
309+
TRACK_SCOPE_LATENCY("ur_kernel_handle_t_::setArgMemObj");
310+
311+
// TODO: support properties
312+
std::ignore = pProperties;
313+
314+
auto kernelDevices = hKernel->getDevices();
315+
if (kernelDevices.size() == 1) {
316+
auto zePtr = hArgValue->getPtr(kernelDevices.front());
317+
return hKernel->setArgPointer(argIndex, nullptr, zePtr);
318+
} else {
319+
// TODO: Implement this for multi-device kernels.
320+
// Do this the same way as in legacy (keep a pending Args vector and
321+
// do actual allocation on kernel submission) or allocate the memory
322+
// immediately (only for small allocations?)
323+
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
324+
}
325+
}
326+
294327
ur_result_t urKernelSetExecInfo(
295328
ur_kernel_handle_t hKernel, ///< [in] handle of the kernel object
296329
ur_kernel_exec_info_t propName, ///< [in] name of the execution attribute

source/adapters/level_zero/v2/kernel.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
#include "common.hpp"
1616

1717
struct ur_single_device_kernel_t {
18-
ur_single_device_kernel_t(ze_device_handle_t hDevice,
18+
ur_single_device_kernel_t(ur_device_handle_t hDevice,
1919
ze_kernel_handle_t hKernel, bool ownZeHandle);
2020
ur_result_t release();
2121

22-
ze_device_handle_t hDevice;
22+
ur_device_handle_t hDevice;
2323
v2::raii::ze_kernel_handle_t hKernel;
2424
mutable ZeCache<ZeStruct<ze_kernel_properties_t>> zeKernelProperties;
2525
};
@@ -40,6 +40,9 @@ struct ur_kernel_handle_t_ : _ur_object {
4040
// Get program handle of the kernel.
4141
ur_program_handle_t getProgramHandle() const;
4242

43+
// Get devices the kernel is built for.
44+
std::vector<ur_device_handle_t> getDevices() const;
45+
4346
// Get name of the kernel.
4447
const std::string &getName() const;
4548

0 commit comments

Comments
 (0)