Skip to content

Commit 15b525e

Browse files
authored
Merge pull request #2241 from igchor/native_2
[L0 v2] implement native APIs and urMemBufferPartition
2 parents d04dbc2 + 47daec1 commit 15b525e

22 files changed

+789
-348
lines changed

source/adapters/level_zero/helpers/memory_helpers.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,27 @@
1111
#include "memory_helpers.hpp"
1212
#include "../common.hpp"
1313

14-
ze_memory_type_t getMemoryType(ze_context_handle_t hContext, void *ptr) {
14+
ur_result_t
15+
getMemoryAttrs(ze_context_handle_t hContext, void *ptr,
16+
ze_device_handle_t *hDevice,
17+
ZeStruct<ze_memory_allocation_properties_t> *properties) {
1518
// TODO: use UMF once
1619
// https://github.com/oneapi-src/unified-memory-framework/issues/687 is
1720
// implemented
18-
ZeStruct<ze_memory_allocation_properties_t> zeMemoryAllocationProperties;
19-
ZE2UR_CALL_THROWS(zeMemGetAllocProperties,
20-
(hContext, ptr, &zeMemoryAllocationProperties, nullptr));
21-
return zeMemoryAllocationProperties.type;
21+
ZE2UR_CALL(zeMemGetAllocProperties, (hContext, ptr, properties, hDevice));
22+
return UR_RESULT_SUCCESS;
2223
}
2324

2425
bool maybeImportUSM(ze_driver_handle_t hTranslatedDriver,
2526
ze_context_handle_t hContext, void *ptr, size_t size) {
26-
if (ZeUSMImport.Enabled && ptr != nullptr &&
27-
getMemoryType(hContext, ptr) == ZE_MEMORY_TYPE_UNKNOWN) {
27+
if (!ZeUSMImport.Enabled || ptr == nullptr) {
28+
return false;
29+
}
30+
31+
ZeStruct<ze_memory_allocation_properties_t> properties;
32+
auto ret = getMemoryAttrs(hContext, ptr, nullptr, &properties);
33+
34+
if (ret == UR_RESULT_SUCCESS && properties.type == ZE_MEMORY_TYPE_UNKNOWN) {
2835
// Promote the host ptr to USM host memory
2936
ZeUSMImport.doZeUSMImport(hTranslatedDriver, ptr, size);
3037
return true;

source/adapters/level_zero/helpers/memory_helpers.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include <ur_api.h>
1313
#include <ze_api.h>
1414

15+
#include <utility>
16+
17+
#include "../common.hpp"
18+
1519
// If USM Import feature is enabled and hostptr is supplied,
1620
// import the hostptr if not already imported into USM.
1721
// Data transfer rate is maximized when both source and destination
@@ -20,7 +24,11 @@
2024
bool maybeImportUSM(ze_driver_handle_t hTranslatedDriver,
2125
ze_context_handle_t hContext, void *ptr, size_t size);
2226

23-
ze_memory_type_t getMemoryType(ze_context_handle_t hContext, void *ptr);
27+
// Get memory attributes for a given pointer
28+
ur_result_t
29+
getMemoryAttrs(ze_context_handle_t hContext, void *ptr,
30+
ze_device_handle_t *hDevice,
31+
ZeStruct<ze_memory_allocation_properties_t> *properties);
2432

2533
struct ze_region_params {
2634
const ze_copy_region_t dstRegion;

source/adapters/level_zero/memory.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,28 +1746,19 @@ ur_result_t urMemBufferCreateWithNativeHandle(
17461746
std::shared_lock<ur_shared_mutex> Lock(Context->Mutex);
17471747

17481748
// Get base of the allocation
1749-
void *Base = nullptr;
1750-
size_t Size = 0;
17511749
void *Ptr = ur_cast<void *>(NativeMem);
1750+
1751+
void *Base;
1752+
size_t Size;
17521753
ZE2UR_CALL(zeMemGetAddressRange, (Context->ZeContext, Ptr, &Base, &Size));
1753-
UR_ASSERT(Ptr == Base, UR_RESULT_ERROR_INVALID_VALUE);
17541754

1755-
ZeStruct<ze_memory_allocation_properties_t> ZeMemProps;
1756-
ze_device_handle_t ZeDevice = nullptr;
1757-
ZE2UR_CALL(zeMemGetAllocProperties,
1758-
(Context->ZeContext, Ptr, &ZeMemProps, &ZeDevice));
1755+
UR_ASSERT(Ptr == Base, UR_RESULT_ERROR_INVALID_VALUE);
17591756

1760-
// Check type of the allocation
1761-
switch (ZeMemProps.type) {
1762-
case ZE_MEMORY_TYPE_HOST:
1763-
case ZE_MEMORY_TYPE_SHARED:
1764-
case ZE_MEMORY_TYPE_DEVICE:
1765-
break;
1766-
case ZE_MEMORY_TYPE_UNKNOWN:
1767-
// Memory allocation is unrelated to the context
1768-
return UR_RESULT_ERROR_INVALID_CONTEXT;
1769-
default:
1770-
die("Unexpected memory type");
1757+
ze_device_handle_t ZeDevice;
1758+
ZeStruct<ze_memory_allocation_properties_t> MemoryAttrs;
1759+
UR_CALL(getMemoryAttrs(Context->ZeContext, Ptr, &ZeDevice, &MemoryAttrs));
1760+
if (MemoryAttrs.type == ZE_MEMORY_TYPE_UNKNOWN) {
1761+
return UR_RESULT_ERROR_INVALID_VALUE;
17711762
}
17721763

17731764
ur_device_handle_t Device{};

source/adapters/level_zero/v2/api.cpp

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@
1818
std::mutex ZeCall::GlobalLock;
1919

2020
namespace ur::level_zero {
21-
ur_result_t urContextGetNativeHandle(ur_context_handle_t hContext,
22-
ur_native_handle_t *phNativeContext) {
23-
logger::error("{} function not implemented!", __FUNCTION__);
24-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
25-
}
26-
27-
ur_result_t urContextCreateWithNativeHandle(
28-
ur_native_handle_t hNativeContext, ur_adapter_handle_t hAdapter,
29-
uint32_t numDevices, const ur_device_handle_t *phDevices,
30-
const ur_context_native_properties_t *pProperties,
31-
ur_context_handle_t *phContext) {
32-
logger::error("{} function not implemented!", __FUNCTION__);
33-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
34-
}
3521

3622
ur_result_t
3723
urContextSetExtendedDeleter(ur_context_handle_t hContext,
@@ -49,13 +35,6 @@ ur_result_t urMemImageCreate(ur_context_handle_t hContext, ur_mem_flags_t flags,
4935
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
5036
}
5137

52-
ur_result_t urMemGetNativeHandle(ur_mem_handle_t hMem,
53-
ur_device_handle_t hDevice,
54-
ur_native_handle_t *phNativeMem) {
55-
logger::error("{} function not implemented!", __FUNCTION__);
56-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
57-
}
58-
5938
ur_result_t urMemImageCreateWithNativeHandle(
6039
ur_native_handle_t hNativeMem, ur_context_handle_t hContext,
6140
const ur_image_format_t *pImageFormat, const ur_image_desc_t *pImageDesc,
@@ -192,22 +171,6 @@ ur_result_t urKernelSetSpecializationConstants(
192171
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
193172
}
194173

195-
ur_result_t urKernelGetNativeHandle(ur_kernel_handle_t hKernel,
196-
ur_native_handle_t *phNativeKernel) {
197-
logger::error("{} function not implemented!", __FUNCTION__);
198-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
199-
}
200-
201-
ur_result_t
202-
urKernelCreateWithNativeHandle(ur_native_handle_t hNativeKernel,
203-
ur_context_handle_t hContext,
204-
ur_program_handle_t hProgram,
205-
const ur_kernel_native_properties_t *pProperties,
206-
ur_kernel_handle_t *phKernel) {
207-
logger::error("{} function not implemented!", __FUNCTION__);
208-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
209-
}
210-
211174
ur_result_t urKernelGetSuggestedLocalWorkSize(ur_kernel_handle_t hKernel,
212175
ur_queue_handle_t hQueue,
213176
uint32_t numWorkDim,

source/adapters/level_zero/v2/command_list_cache.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ command_list_cache_t::createCommandList(const command_list_descriptor_t &desc) {
7676
}
7777
}
7878

79-
raii::cache_borrowed_command_list_t
80-
command_list_cache_t::getImmediateCommandList(
79+
raii::command_list_unique_handle command_list_cache_t::getImmediateCommandList(
8180
ze_device_handle_t ZeDevice, bool IsInOrder, uint32_t Ordinal,
8281
ze_command_queue_mode_t Mode, ze_command_queue_priority_t Priority,
8382
std::optional<uint32_t> Index) {
@@ -91,14 +90,14 @@ command_list_cache_t::getImmediateCommandList(
9190
Desc.Priority = Priority;
9291
Desc.Index = Index;
9392

94-
auto CommandList = getCommandList(Desc).release();
95-
return raii::cache_borrowed_command_list_t(
93+
auto [CommandList, _] = getCommandList(Desc).release();
94+
return raii::command_list_unique_handle(
9695
CommandList, [Cache = this, Desc](ze_command_list_handle_t CmdList) {
9796
Cache->addCommandList(Desc, raii::ze_command_list_handle_t(CmdList));
9897
});
9998
}
10099

101-
raii::cache_borrowed_command_list_t
100+
raii::command_list_unique_handle
102101
command_list_cache_t::getRegularCommandList(ze_device_handle_t ZeDevice,
103102
bool IsInOrder, uint32_t Ordinal) {
104103
TRACK_SCOPE_LATENCY("command_list_cache_t::getRegularCommandList");
@@ -108,9 +107,9 @@ command_list_cache_t::getRegularCommandList(ze_device_handle_t ZeDevice,
108107
Desc.IsInOrder = IsInOrder;
109108
Desc.Ordinal = Ordinal;
110109

111-
auto CommandList = getCommandList(Desc).release();
110+
auto [CommandList, _] = getCommandList(Desc).release();
112111

113-
return raii::cache_borrowed_command_list_t(
112+
return raii::command_list_unique_handle(
114113
CommandList, [Cache = this, Desc](ze_command_list_handle_t CmdList) {
115114
Cache->addCommandList(Desc, raii::ze_command_list_handle_t(CmdList));
116115
});

source/adapters/level_zero/v2/command_list_cache.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace v2 {
2323
namespace raii {
24-
using cache_borrowed_command_list_t =
24+
using command_list_unique_handle =
2525
std::unique_ptr<::_ze_command_list_handle_t,
2626
std::function<void(::ze_command_list_handle_t)>>;
2727
} // namespace raii
@@ -54,12 +54,12 @@ struct command_list_descriptor_hash_t {
5454
struct command_list_cache_t {
5555
command_list_cache_t(ze_context_handle_t ZeContext);
5656

57-
raii::cache_borrowed_command_list_t
57+
raii::command_list_unique_handle
5858
getImmediateCommandList(ze_device_handle_t ZeDevice, bool IsInOrder,
5959
uint32_t Ordinal, ze_command_queue_mode_t Mode,
6060
ze_command_queue_priority_t Priority,
6161
std::optional<uint32_t> Index = std::nullopt);
62-
raii::cache_borrowed_command_list_t
62+
raii::command_list_unique_handle
6363
getRegularCommandList(ze_device_handle_t ZeDevice, bool IsInOrder,
6464
uint32_t Ordinal);
6565

source/adapters/level_zero/v2/common.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,20 @@ struct ze_handle_wrapper {
6464
return;
6565
}
6666

67-
auto zeResult = ZE_CALL_NOCHECK(destroy, (handle));
68-
// Gracefully handle the case that L0 was already unloaded.
69-
if (zeResult && zeResult != ZE_RESULT_ERROR_UNINITIALIZED)
70-
throw ze2urResult(zeResult);
67+
if (ownZeHandle) {
68+
auto zeResult = ZE_CALL_NOCHECK(destroy, (handle));
69+
// Gracefully handle the case that L0 was already unloaded.
70+
if (zeResult && zeResult != ZE_RESULT_ERROR_UNINITIALIZED)
71+
throw ze2urResult(zeResult);
72+
}
7173

7274
handle = nullptr;
7375
}
7476

75-
ZeHandleT release() {
77+
std::pair<ZeHandleT, bool> release() {
7678
auto handle = this->handle;
7779
this->handle = nullptr;
78-
return handle;
80+
return {handle, ownZeHandle};
7981
}
8082

8183
ZeHandleT get() const { return handle; }

source/adapters/level_zero/v2/context.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,27 @@ ur_result_t urContextCreate(uint32_t deviceCount,
120120
return UR_RESULT_SUCCESS;
121121
}
122122

123+
ur_result_t urContextGetNativeHandle(ur_context_handle_t hContext,
124+
ur_native_handle_t *phNativeContext) {
125+
*phNativeContext =
126+
reinterpret_cast<ur_native_handle_t>(hContext->getZeHandle());
127+
return UR_RESULT_SUCCESS;
128+
}
129+
130+
ur_result_t urContextCreateWithNativeHandle(
131+
ur_native_handle_t hNativeContext, ur_adapter_handle_t, uint32_t numDevices,
132+
const ur_device_handle_t *phDevices,
133+
const ur_context_native_properties_t *pProperties,
134+
ur_context_handle_t *phContext) {
135+
auto zeContext = reinterpret_cast<ze_context_handle_t>(hNativeContext);
136+
137+
auto ownZeHandle = pProperties ? pProperties->isNativeHandleOwned : false;
138+
139+
*phContext =
140+
new ur_context_handle_t_(zeContext, numDevices, phDevices, ownZeHandle);
141+
return UR_RESULT_SUCCESS;
142+
}
143+
123144
ur_result_t urContextRetain(ur_context_handle_t hContext) {
124145
return hContext->retain();
125146
}
@@ -134,7 +155,8 @@ ur_result_t urContextGetInfo(ur_context_handle_t hContext,
134155
void *pContextInfo,
135156

136157
size_t *pPropSizeRet) {
137-
std::shared_lock<ur_shared_mutex> Lock(hContext->Mutex);
158+
// No locking needed here, we only read const members
159+
138160
UrReturnHelper ReturnValue(propSize, pContextInfo, pPropSizeRet);
139161
switch (
140162
(uint32_t)contextInfoType) { // cast to avoid warnings on EXT enum values

source/adapters/level_zero/v2/event.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ static uint64_t adjustEndEventTimestamp(uint64_t adjustedStartTimestamp,
9393
}
9494

9595
uint64_t ur_event_handle_t_::getEventEndTimestamp() {
96-
std::scoped_lock<ur_shared_mutex> lock(this->Mutex);
97-
9896
// If adjustedEventEndTimestamp on the event is non-zero it means it has
9997
// collected the result of the queue already. In that case it has been
10098
// adjusted and is ready for immediate return.
@@ -120,8 +118,6 @@ void ur_event_handle_t_::recordStartTimestamp() {
120118
UR_CALL_THROWS(ur::level_zero::urDeviceGetGlobalTimestamps(
121119
getDevice(), &deviceStartTimestamp, nullptr));
122120

123-
std::scoped_lock<ur_shared_mutex> lock(this->Mutex);
124-
125121
adjustedEventStartTimestamp = deviceStartTimestamp;
126122
}
127123

@@ -183,6 +179,8 @@ ur_result_t urEventGetProfilingInfo(
183179
size_t *pPropValueSizeRet ///< [out][optional] pointer to the actual size in
184180
///< bytes returned in propValue
185181
) {
182+
std::scoped_lock<ur_shared_mutex> lock(hEvent->Mutex);
183+
186184
// The event must either have profiling enabled or be recording timestamps.
187185
bool isTimestampedEvent = hEvent->isTimestamped();
188186
if (!hEvent->isProfilingEnabled() && !isTimestampedEvent) {

source/adapters/level_zero/v2/event_provider_counter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ raii::cache_borrowed_event provider_counter::allocate() {
5454
freelist.pop_back();
5555

5656
return raii::cache_borrowed_event(
57-
event.release(),
57+
event.release().first,
5858
[this](ze_event_handle_t handle) { freelist.push_back(handle); });
5959
}
6060

source/adapters/level_zero/v2/event_provider_normal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ raii::cache_borrowed_event provider_pool::allocate() {
6868
auto e = std::move(freelist.back());
6969
freelist.pop_back();
7070
return raii::cache_borrowed_event(
71-
e.release(),
71+
e.release().first,
7272
[this](ze_event_handle_t handle) { freelist.push_back(handle); });
7373
}
7474

0 commit comments

Comments
 (0)