Skip to content

Commit ef5174f

Browse files
Jaime ArteagaCompute-Runtime-Automation
authored andcommitted
Eliminate wrappers in L0::Context class for driverHandle calls
Signed-off-by: Jaime Arteaga <[email protected]>
1 parent 01f51cc commit ef5174f

File tree

14 files changed

+386
-338
lines changed

14 files changed

+386
-338
lines changed

level_zero/core/source/context/context.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 Intel Corporation
2+
* Copyright (C) 2020-2021 Intel Corporation
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -19,6 +19,21 @@ namespace L0 {
1919
struct DriverHandle;
2020

2121
struct Context : _ze_context_handle_t {
22+
inline static ze_memory_type_t parseUSMType(InternalMemoryType memoryType) {
23+
switch (memoryType) {
24+
case InternalMemoryType::SHARED_UNIFIED_MEMORY:
25+
return ZE_MEMORY_TYPE_SHARED;
26+
case InternalMemoryType::DEVICE_UNIFIED_MEMORY:
27+
return ZE_MEMORY_TYPE_DEVICE;
28+
case InternalMemoryType::HOST_UNIFIED_MEMORY:
29+
return ZE_MEMORY_TYPE_HOST;
30+
default:
31+
return ZE_MEMORY_TYPE_UNKNOWN;
32+
}
33+
34+
return ZE_MEMORY_TYPE_UNKNOWN;
35+
}
36+
2237
virtual ~Context() = default;
2338
virtual ze_result_t destroy() = 0;
2439
virtual ze_result_t getStatus() = 0;

level_zero/core/source/context/context_imp.cpp

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,30 @@ ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice,
229229
}
230230

231231
ze_result_t ContextImp::freeMem(const void *ptr) {
232-
DEBUG_BREAK_IF(nullptr == this->driverHandle);
233-
return this->driverHandle->freeMem(ptr);
232+
auto allocation = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
233+
if (allocation == nullptr) {
234+
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
235+
}
236+
237+
for (auto pairDevice : this->devices) {
238+
DeviceImp *deviceImp = static_cast<DeviceImp *>(pairDevice.second);
239+
240+
std::unique_lock<NEO::SpinLock> lock(deviceImp->peerAllocationsMutex);
241+
242+
auto iter = deviceImp->peerAllocations.allocations.find(ptr);
243+
if (iter != deviceImp->peerAllocations.allocations.end()) {
244+
auto peerAllocData = &iter->second;
245+
auto peerAlloc = peerAllocData->gpuAllocations.getDefaultGraphicsAllocation();
246+
auto peerPtr = reinterpret_cast<void *>(peerAlloc->getGpuAddress());
247+
this->driverHandle->svmAllocsManager->freeSVMAlloc(peerPtr);
248+
}
249+
}
250+
251+
this->driverHandle->svmAllocsManager->freeSVMAlloc(const_cast<void *>(ptr));
252+
if (this->driverHandle->svmAllocsManager->getSvmMapOperation(ptr)) {
253+
this->driverHandle->svmAllocsManager->removeSvmMapOperation(ptr);
254+
}
255+
return ZE_RESULT_SUCCESS;
234256
}
235257

236258
ze_result_t ContextImp::makeMemoryResident(ze_device_handle_t hDevice, void *ptr, size_t size) {
@@ -304,42 +326,102 @@ ze_result_t ContextImp::evictImage(ze_device_handle_t hDevice, ze_image_handle_t
304326
ze_result_t ContextImp::getMemAddressRange(const void *ptr,
305327
void **pBase,
306328
size_t *pSize) {
307-
DEBUG_BREAK_IF(nullptr == this->driverHandle);
308-
return this->driverHandle->getMemAddressRange(ptr,
309-
pBase,
310-
pSize);
329+
NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
330+
if (allocData) {
331+
NEO::GraphicsAllocation *alloc;
332+
alloc = allocData->gpuAllocations.getDefaultGraphicsAllocation();
333+
if (pBase) {
334+
uint64_t *allocBase = reinterpret_cast<uint64_t *>(pBase);
335+
*allocBase = alloc->getGpuAddress();
336+
}
337+
338+
if (pSize) {
339+
*pSize = alloc->getUnderlyingBufferSize();
340+
}
341+
342+
return ZE_RESULT_SUCCESS;
343+
}
344+
DEBUG_BREAK_IF(true);
345+
return ZE_RESULT_ERROR_UNKNOWN;
311346
}
312347

313348
ze_result_t ContextImp::closeIpcMemHandle(const void *ptr) {
314-
DEBUG_BREAK_IF(nullptr == this->driverHandle);
315-
return this->driverHandle->closeIpcMemHandle(ptr);
349+
return this->freeMem(ptr);
316350
}
317351

318352
ze_result_t ContextImp::getIpcMemHandle(const void *ptr,
319353
ze_ipc_mem_handle_t *pIpcHandle) {
320-
DEBUG_BREAK_IF(nullptr == this->driverHandle);
321-
return this->driverHandle->getIpcMemHandle(ptr,
322-
pIpcHandle);
354+
NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
355+
if (allocData) {
356+
uint64_t handle = allocData->gpuAllocations.getDefaultGraphicsAllocation()->peekInternalHandle(this->driverHandle->getMemoryManager());
357+
memcpy_s(reinterpret_cast<void *>(pIpcHandle->data),
358+
sizeof(ze_ipc_mem_handle_t),
359+
&handle,
360+
sizeof(handle));
361+
362+
return ZE_RESULT_SUCCESS;
363+
}
364+
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
323365
}
324366

325367
ze_result_t ContextImp::openIpcMemHandle(ze_device_handle_t hDevice,
326-
ze_ipc_mem_handle_t handle,
368+
ze_ipc_mem_handle_t pIpcHandle,
327369
ze_ipc_memory_flags_t flags,
328370
void **ptr) {
329-
DEBUG_BREAK_IF(nullptr == this->driverHandle);
330-
return this->driverHandle->openIpcMemHandle(hDevice,
331-
handle,
332-
flags,
333-
ptr);
371+
uint64_t handle = 0u;
372+
memcpy_s(&handle,
373+
sizeof(handle),
374+
reinterpret_cast<void *>(pIpcHandle.data),
375+
sizeof(handle));
376+
377+
*ptr = this->driverHandle->importFdHandle(hDevice, flags, handle, nullptr);
378+
if (nullptr == *ptr) {
379+
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
380+
}
381+
382+
return ZE_RESULT_SUCCESS;
334383
}
335384

336385
ze_result_t ContextImp::getMemAllocProperties(const void *ptr,
337386
ze_memory_allocation_properties_t *pMemAllocProperties,
338387
ze_device_handle_t *phDevice) {
339-
DEBUG_BREAK_IF(nullptr == this->driverHandle);
340-
return this->driverHandle->getMemAllocProperties(ptr,
341-
pMemAllocProperties,
342-
phDevice);
388+
auto alloc = driverHandle->svmAllocsManager->getSVMAlloc(ptr);
389+
if (nullptr == alloc) {
390+
pMemAllocProperties->type = ZE_MEMORY_TYPE_UNKNOWN;
391+
return ZE_RESULT_SUCCESS;
392+
}
393+
394+
pMemAllocProperties->type = Context::parseUSMType(alloc->memoryType);
395+
pMemAllocProperties->id = alloc->gpuAllocations.getDefaultGraphicsAllocation()->getGpuAddress();
396+
397+
if (phDevice != nullptr) {
398+
if (alloc->device == nullptr) {
399+
*phDevice = nullptr;
400+
} else {
401+
auto device = static_cast<NEO::Device *>(alloc->device)->getSpecializedDevice<DeviceImp>();
402+
DEBUG_BREAK_IF(device == nullptr);
403+
*phDevice = device->toHandle();
404+
}
405+
}
406+
407+
if (pMemAllocProperties->pNext) {
408+
ze_base_properties_t *extendedProperties =
409+
reinterpret_cast<ze_base_properties_t *>(pMemAllocProperties->pNext);
410+
if (extendedProperties->stype == ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_FD) {
411+
ze_external_memory_export_fd_t *extendedMemoryExportProperties =
412+
reinterpret_cast<ze_external_memory_export_fd_t *>(extendedProperties);
413+
if (extendedMemoryExportProperties->flags & ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_FD) {
414+
return ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
415+
}
416+
if (pMemAllocProperties->type != ZE_MEMORY_TYPE_DEVICE) {
417+
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
418+
}
419+
uint64_t handle = alloc->gpuAllocations.getDefaultGraphicsAllocation()->peekInternalHandle(this->driverHandle->getMemoryManager());
420+
extendedMemoryExportProperties->fd = static_cast<int>(handle);
421+
}
422+
}
423+
424+
return ZE_RESULT_SUCCESS;
343425
}
344426

345427
ze_result_t ContextImp::createModule(ze_device_handle_t hDevice,

level_zero/core/source/driver/driver_handle.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,9 @@ struct DriverHandle : _ze_driver_handle_t {
3434
virtual ze_result_t getExtensionFunctionAddress(const char *pFuncName, void **pfunc) = 0;
3535
virtual ze_result_t getExtensionProperties(uint32_t *pCount,
3636
ze_driver_extension_properties_t *pExtensionProperties) = 0;
37-
virtual ze_result_t getMemAllocProperties(const void *ptr,
38-
ze_memory_allocation_properties_t *pMemAllocProperties,
39-
ze_device_handle_t *phDevice) = 0;
4037

41-
virtual ze_result_t freeMem(const void *ptr) = 0;
4238
virtual NEO::MemoryManager *getMemoryManager() = 0;
4339
virtual void setMemoryManager(NEO::MemoryManager *memoryManager) = 0;
44-
virtual ze_result_t getMemAddressRange(const void *ptr, void **pBase, size_t *pSize) = 0;
45-
virtual ze_result_t closeIpcMemHandle(const void *ptr) = 0;
46-
virtual ze_result_t getIpcMemHandle(const void *ptr, ze_ipc_mem_handle_t *pIpcHandle) = 0;
47-
virtual ze_result_t openIpcMemHandle(ze_device_handle_t hDevice, ze_ipc_mem_handle_t handle,
48-
ze_ipc_memory_flags_t flags, void **ptr) = 0;
4940
virtual ze_result_t openEventPoolIpcHandle(ze_ipc_event_pool_handle_t hIpc, ze_event_pool_handle_t *phEventPool) = 0;
5041
virtual ze_result_t checkMemoryAccessFromDevice(Device *device, const void *ptr) = 0;
5142
virtual bool findAllocationDataForRange(const void *buffer,

level_zero/core/source/driver/driver_handle_imp.cpp

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,6 @@ ze_result_t DriverHandleImp::getIPCProperties(ze_driver_ipc_properties_t *pIPCPr
101101
return ZE_RESULT_SUCCESS;
102102
}
103103

104-
inline ze_memory_type_t parseUSMType(InternalMemoryType memoryType) {
105-
switch (memoryType) {
106-
case InternalMemoryType::SHARED_UNIFIED_MEMORY:
107-
return ZE_MEMORY_TYPE_SHARED;
108-
case InternalMemoryType::DEVICE_UNIFIED_MEMORY:
109-
return ZE_MEMORY_TYPE_DEVICE;
110-
case InternalMemoryType::HOST_UNIFIED_MEMORY:
111-
return ZE_MEMORY_TYPE_HOST;
112-
default:
113-
return ZE_MEMORY_TYPE_UNKNOWN;
114-
}
115-
116-
return ZE_MEMORY_TYPE_UNKNOWN;
117-
}
118-
119104
ze_result_t DriverHandleImp::getExtensionFunctionAddress(const char *pFuncName, void **pfunc) {
120105
auto funcAddr = extensionFunctionsLookupMap.find(std::string(pFuncName));
121106
if (funcAddr != extensionFunctionsLookupMap.end()) {
@@ -144,48 +129,6 @@ ze_result_t DriverHandleImp::getExtensionProperties(uint32_t *pCount,
144129
return ZE_RESULT_SUCCESS;
145130
}
146131

147-
ze_result_t DriverHandleImp::getMemAllocProperties(const void *ptr,
148-
ze_memory_allocation_properties_t *pMemAllocProperties,
149-
ze_device_handle_t *phDevice) {
150-
auto alloc = svmAllocsManager->getSVMAlloc(ptr);
151-
if (nullptr == alloc) {
152-
pMemAllocProperties->type = ZE_MEMORY_TYPE_UNKNOWN;
153-
return ZE_RESULT_SUCCESS;
154-
}
155-
156-
pMemAllocProperties->type = parseUSMType(alloc->memoryType);
157-
pMemAllocProperties->id = alloc->gpuAllocations.getDefaultGraphicsAllocation()->getGpuAddress();
158-
159-
if (phDevice != nullptr) {
160-
if (alloc->device == nullptr) {
161-
*phDevice = nullptr;
162-
} else {
163-
auto device = static_cast<NEO::Device *>(alloc->device)->getSpecializedDevice<DeviceImp>();
164-
DEBUG_BREAK_IF(device == nullptr);
165-
*phDevice = device->toHandle();
166-
}
167-
}
168-
169-
if (pMemAllocProperties->pNext) {
170-
ze_base_properties_t *extendedProperties =
171-
reinterpret_cast<ze_base_properties_t *>(pMemAllocProperties->pNext);
172-
if (extendedProperties->stype == ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_FD) {
173-
ze_external_memory_export_fd_t *extendedMemoryExportProperties =
174-
reinterpret_cast<ze_external_memory_export_fd_t *>(extendedProperties);
175-
if (extendedMemoryExportProperties->flags & ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_FD) {
176-
return ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
177-
}
178-
if (pMemAllocProperties->type != ZE_MEMORY_TYPE_DEVICE) {
179-
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
180-
}
181-
uint64_t handle = alloc->gpuAllocations.getDefaultGraphicsAllocation()->peekInternalHandle(this->getMemoryManager());
182-
extendedMemoryExportProperties->fd = static_cast<int>(handle);
183-
}
184-
}
185-
186-
return ZE_RESULT_SUCCESS;
187-
}
188-
189132
DriverHandleImp::~DriverHandleImp() {
190133
for (auto &device : this->devices) {
191134
delete device;

level_zero/core/source/driver/driver_handle_imp.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,10 @@ struct DriverHandleImp : public DriverHandle {
3131
ze_result_t getExtensionFunctionAddress(const char *pFuncName, void **pfunc) override;
3232
ze_result_t getExtensionProperties(uint32_t *pCount,
3333
ze_driver_extension_properties_t *pExtensionProperties) override;
34-
ze_result_t getMemAllocProperties(const void *ptr,
35-
ze_memory_allocation_properties_t *pMemAllocProperties,
36-
ze_device_handle_t *phDevice) override;
3734

38-
ze_result_t getMemAddressRange(const void *ptr, void **pBase, size_t *pSize) override;
39-
ze_result_t freeMem(const void *ptr) override;
4035
NEO::MemoryManager *getMemoryManager() override;
4136
void setMemoryManager(NEO::MemoryManager *memoryManager) override;
4237
MOCKABLE_VIRTUAL void *importFdHandle(ze_device_handle_t hDevice, ze_ipc_memory_flags_t flags, uint64_t handle, NEO::GraphicsAllocation **pAlloc);
43-
ze_result_t closeIpcMemHandle(const void *ptr) override;
44-
ze_result_t getIpcMemHandle(const void *ptr, ze_ipc_mem_handle_t *pIpcHandle) override;
45-
ze_result_t openIpcMemHandle(ze_device_handle_t hDevice, ze_ipc_mem_handle_t handle,
46-
ze_ipc_memory_flags_t flags, void **ptr) override;
4738
ze_result_t openEventPoolIpcHandle(ze_ipc_event_pool_handle_t hIpc, ze_event_pool_handle_t *phEventPool) override;
4839
ze_result_t checkMemoryAccessFromDevice(Device *device, const void *ptr) override;
4940
NEO::SVMAllocsManager *getSvmAllocsManager() override;

level_zero/core/source/memory/memory.cpp

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,6 @@
1515

1616
namespace L0 {
1717

18-
ze_result_t DriverHandleImp::getIpcMemHandle(const void *ptr, ze_ipc_mem_handle_t *pIpcHandle) {
19-
NEO::SvmAllocationData *allocData = svmAllocsManager->getSVMAlloc(ptr);
20-
if (allocData) {
21-
uint64_t handle = allocData->gpuAllocations.getDefaultGraphicsAllocation()->peekInternalHandle(this->getMemoryManager());
22-
memcpy_s(reinterpret_cast<void *>(pIpcHandle->data),
23-
sizeof(ze_ipc_mem_handle_t),
24-
&handle,
25-
sizeof(handle));
26-
27-
return ZE_RESULT_SUCCESS;
28-
}
29-
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
30-
}
31-
3218
void *DriverHandleImp::importFdHandle(ze_device_handle_t hDevice, ze_ipc_memory_flags_t flags, uint64_t handle, NEO::GraphicsAllocation **pAlloc) {
3319
auto neoDevice = Device::fromHandle(hDevice)->getNEODevice();
3420
NEO::osHandle osHandle = static_cast<NEO::osHandle>(handle);
@@ -64,26 +50,6 @@ void *DriverHandleImp::importFdHandle(ze_device_handle_t hDevice, ze_ipc_memory_
6450
return reinterpret_cast<void *>(alloc->getGpuAddress());
6551
}
6652

67-
ze_result_t DriverHandleImp::openIpcMemHandle(ze_device_handle_t hDevice, ze_ipc_mem_handle_t pIpcHandle,
68-
ze_ipc_memory_flags_t flags, void **ptr) {
69-
uint64_t handle = 0u;
70-
memcpy_s(&handle,
71-
sizeof(handle),
72-
reinterpret_cast<void *>(pIpcHandle.data),
73-
sizeof(handle));
74-
75-
*ptr = this->importFdHandle(hDevice, flags, handle, nullptr);
76-
if (nullptr == *ptr) {
77-
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
78-
}
79-
80-
return ZE_RESULT_SUCCESS;
81-
}
82-
83-
ze_result_t DriverHandleImp::closeIpcMemHandle(const void *ptr) {
84-
return freeMem(ptr);
85-
}
86-
8753
ze_result_t DriverHandleImp::checkMemoryAccessFromDevice(Device *device, const void *ptr) {
8854
auto allocation = svmAllocsManager->getSVMAlloc(ptr);
8955
if (allocation == nullptr) {
@@ -101,51 +67,4 @@ ze_result_t DriverHandleImp::checkMemoryAccessFromDevice(Device *device, const v
10167
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
10268
}
10369

104-
ze_result_t DriverHandleImp::getMemAddressRange(const void *ptr, void **pBase, size_t *pSize) {
105-
NEO::SvmAllocationData *allocData = svmAllocsManager->getSVMAlloc(ptr);
106-
if (allocData) {
107-
NEO::GraphicsAllocation *alloc;
108-
alloc = allocData->gpuAllocations.getDefaultGraphicsAllocation();
109-
if (pBase) {
110-
uint64_t *allocBase = reinterpret_cast<uint64_t *>(pBase);
111-
*allocBase = alloc->getGpuAddress();
112-
}
113-
114-
if (pSize) {
115-
*pSize = alloc->getUnderlyingBufferSize();
116-
}
117-
118-
return ZE_RESULT_SUCCESS;
119-
}
120-
DEBUG_BREAK_IF(true);
121-
return ZE_RESULT_ERROR_UNKNOWN;
122-
}
123-
124-
ze_result_t DriverHandleImp::freeMem(const void *ptr) {
125-
auto allocation = svmAllocsManager->getSVMAlloc(ptr);
126-
if (allocation == nullptr) {
127-
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
128-
}
129-
130-
for (auto device : this->devices) {
131-
DeviceImp *deviceImp = static_cast<DeviceImp *>(device);
132-
133-
std::unique_lock<NEO::SpinLock> lock(deviceImp->peerAllocationsMutex);
134-
135-
auto iter = deviceImp->peerAllocations.allocations.find(ptr);
136-
if (iter != deviceImp->peerAllocations.allocations.end()) {
137-
auto peerAllocData = &iter->second;
138-
auto peerAlloc = peerAllocData->gpuAllocations.getDefaultGraphicsAllocation();
139-
auto peerPtr = reinterpret_cast<void *>(peerAlloc->getGpuAddress());
140-
svmAllocsManager->freeSVMAlloc(peerPtr);
141-
}
142-
}
143-
144-
svmAllocsManager->freeSVMAlloc(const_cast<void *>(ptr));
145-
if (svmAllocsManager->getSvmMapOperation(ptr)) {
146-
svmAllocsManager->removeSvmMapOperation(ptr);
147-
}
148-
return ZE_RESULT_SUCCESS;
149-
}
150-
15170
} // namespace L0

0 commit comments

Comments
 (0)