diff --git a/level_zero/api/core/ze_memory.cpp b/level_zero/api/core/ze_memory.cpp index ddad8aa744..54345b4d0a 100644 --- a/level_zero/api/core/ze_memory.cpp +++ b/level_zero/api/core/ze_memory.cpp @@ -22,6 +22,17 @@ zeDriverAllocSharedMem( return L0::DriverHandle::fromHandle(hDriver)->allocSharedMem(hDevice, deviceDesc->flags, hostDesc->flags, size, alignment, pptr); } +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemAllocShared(ze_context_handle_t hContext, + const ze_device_mem_alloc_desc_t *deviceDesc, + const ze_host_mem_alloc_desc_t *hostDesc, + size_t size, + size_t alignment, + ze_device_handle_t hDevice, + void **pptr) { + return L0::Context::fromHandle(hContext)->allocSharedMem(hDevice, deviceDesc->flags, hostDesc->flags, size, alignment, pptr); +} + __zedllexport ze_result_t __zecall zeDriverAllocDeviceMem( ze_driver_handle_t hDriver, @@ -33,6 +44,16 @@ zeDriverAllocDeviceMem( return L0::DriverHandle::fromHandle(hDriver)->allocDeviceMem(hDevice, deviceDesc->flags, size, alignment, pptr); } +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemAllocDevice(ze_context_handle_t hContext, + const ze_device_mem_alloc_desc_t *deviceDesc, + size_t size, + size_t alignment, + ze_device_handle_t hDevice, + void **pptr) { + return L0::Context::fromHandle(hContext)->allocDeviceMem(hDevice, deviceDesc->flags, size, alignment, pptr); +} + __zedllexport ze_result_t __zecall zeDriverAllocHostMem( ze_driver_handle_t hDriver, @@ -43,6 +64,15 @@ zeDriverAllocHostMem( return L0::DriverHandle::fromHandle(hDriver)->allocHostMem(hostDesc->flags, size, alignment, pptr); } +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemAllocHost(ze_context_handle_t hContext, + const ze_host_mem_alloc_desc_t *hostDesc, + size_t size, + size_t alignment, + void **pptr) { + return L0::Context::fromHandle(hContext)->allocHostMem(hostDesc->flags, size, alignment, pptr); +} + __zedllexport ze_result_t __zecall zeDriverFreeMem( ze_driver_handle_t hDriver, @@ -50,6 +80,12 @@ zeDriverFreeMem( return L0::DriverHandle::fromHandle(hDriver)->freeMem(ptr); } +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemFree(ze_context_handle_t hContext, + void *ptr) { + return L0::Context::fromHandle(hContext)->freeMem(ptr); +} + __zedllexport ze_result_t __zecall zeDriverGetMemAllocProperties( ze_driver_handle_t hDriver, @@ -59,6 +95,15 @@ zeDriverGetMemAllocProperties( return L0::DriverHandle::fromHandle(hDriver)->getMemAllocProperties(ptr, pMemAllocProperties, phDevice); } +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemGetAllocProperties( + ze_context_handle_t hContext, + const void *ptr, + ze_memory_allocation_properties_t *pMemAllocProperties, + ze_device_handle_t *phDevice) { + return L0::Context::fromHandle(hContext)->getMemAllocProperties(ptr, pMemAllocProperties, phDevice); +} + __zedllexport ze_result_t __zecall zeDriverGetMemAddressRange( ze_driver_handle_t hDriver, @@ -68,6 +113,15 @@ zeDriverGetMemAddressRange( return L0::DriverHandle::fromHandle(hDriver)->getMemAddressRange(ptr, pBase, pSize); } +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemGetAddressRange( + ze_context_handle_t hContext, + const void *ptr, + void **pBase, + size_t *pSize) { + return L0::Context::fromHandle(hContext)->getMemAddressRange(ptr, pBase, pSize); +} + __zedllexport ze_result_t __zecall zeDriverGetMemIpcHandle( ze_driver_handle_t hDriver, @@ -76,6 +130,14 @@ zeDriverGetMemIpcHandle( return L0::DriverHandle::fromHandle(hDriver)->getIpcMemHandle(ptr, pIpcHandle); } +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemGetIpcHandle( + ze_context_handle_t hContext, + const void *ptr, + ze_ipc_mem_handle_t *pIpcHandle) { + return L0::Context::fromHandle(hContext)->getIpcMemHandle(ptr, pIpcHandle); +} + __zedllexport ze_result_t __zecall zeDriverOpenMemIpcHandle( ze_driver_handle_t hDriver, @@ -86,6 +148,16 @@ zeDriverOpenMemIpcHandle( return L0::DriverHandle::fromHandle(hDriver)->openIpcMemHandle(hDevice, handle, flags, pptr); } +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemOpenIpcHandle( + ze_context_handle_t hContext, + ze_device_handle_t hDevice, + ze_ipc_mem_handle_t handle, + ze_ipc_memory_flags_t flags, + void **pptr) { + return L0::Context::fromHandle(hContext)->openIpcMemHandle(hDevice, handle, flags, pptr); +} + __zedllexport ze_result_t __zecall zeDriverCloseMemIpcHandle( ze_driver_handle_t hDriver, @@ -93,4 +165,11 @@ zeDriverCloseMemIpcHandle( return L0::DriverHandle::fromHandle(hDriver)->closeIpcMemHandle(ptr); } +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemCloseIpcHandle( + ze_context_handle_t hContext, + const void *ptr) { + return L0::Context::fromHandle(hContext)->closeIpcMemHandle(ptr); +} + } // extern "C" diff --git a/level_zero/core/source/context/context.h b/level_zero/core/source/context/context.h index cd31f6f1e9..cba7f4e506 100644 --- a/level_zero/core/source/context/context.h +++ b/level_zero/core/source/context/context.h @@ -24,6 +24,34 @@ struct Context : _ze_context_handle_t { virtual ze_result_t destroy() = 0; virtual ze_result_t getStatus() = 0; virtual DriverHandle *getDriverHandle() = 0; + virtual ze_result_t allocHostMem(ze_host_mem_alloc_flag_t flags, + size_t size, + size_t alignment, + void **ptr) = 0; + virtual ze_result_t allocDeviceMem(ze_device_handle_t hDevice, + ze_device_mem_alloc_flag_t flags, + size_t size, + size_t alignment, void **ptr) = 0; + virtual ze_result_t allocSharedMem(ze_device_handle_t hDevice, + ze_device_mem_alloc_flag_t deviceFlags, + ze_host_mem_alloc_flag_t hostFlags, + size_t size, + size_t alignment, + void **ptr) = 0; + virtual ze_result_t freeMem(const void *ptr) = 0; + virtual ze_result_t getMemAddressRange(const void *ptr, + void **pBase, + size_t *pSize) = 0; + virtual ze_result_t closeIpcMemHandle(const void *ptr) = 0; + virtual ze_result_t getIpcMemHandle(const void *ptr, + ze_ipc_mem_handle_t *pIpcHandle) = 0; + virtual ze_result_t openIpcMemHandle(ze_device_handle_t hDevice, + ze_ipc_mem_handle_t handle, + ze_ipc_memory_flags_t flags, + void **ptr) = 0; + virtual ze_result_t getMemAllocProperties(const void *ptr, + ze_memory_allocation_properties_t *pMemAllocProperties, + ze_device_handle_t *phDevice) = 0; static Context *fromHandle(ze_context_handle_t handle) { return static_cast(handle); } inline ze_context_handle_t toHandle() { return this; } diff --git a/level_zero/core/source/context/context_imp.cpp b/level_zero/core/source/context/context_imp.cpp index 36e97a9a88..a43da6898b 100644 --- a/level_zero/core/source/context/context_imp.cpp +++ b/level_zero/core/source/context/context_imp.cpp @@ -27,4 +27,88 @@ ContextImp::ContextImp(DriverHandle *driverHandle) { this->driverHandle = driverHandle; } +ze_result_t ContextImp::allocHostMem(ze_host_mem_alloc_flag_t flags, + size_t size, + size_t alignment, + void **ptr) { + DEBUG_BREAK_IF(nullptr == this->driverHandle); + return this->driverHandle->allocHostMem(flags, + size, + alignment, + ptr); +} + +ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice, + ze_device_mem_alloc_flag_t flags, + size_t size, + size_t alignment, void **ptr) { + DEBUG_BREAK_IF(nullptr == this->driverHandle); + return this->driverHandle->allocDeviceMem(hDevice, + flags, + size, + alignment, + ptr); +} + +ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice, + ze_device_mem_alloc_flag_t deviceFlags, + ze_host_mem_alloc_flag_t hostFlags, + size_t size, + size_t alignment, + void **ptr) { + DEBUG_BREAK_IF(nullptr == this->driverHandle); + return this->driverHandle->allocSharedMem(hDevice, + deviceFlags, + hostFlags, + size, + alignment, + ptr); +} + +ze_result_t ContextImp::freeMem(const void *ptr) { + DEBUG_BREAK_IF(nullptr == this->driverHandle); + return this->driverHandle->freeMem(ptr); +} + +ze_result_t ContextImp::getMemAddressRange(const void *ptr, + void **pBase, + size_t *pSize) { + DEBUG_BREAK_IF(nullptr == this->driverHandle); + return this->driverHandle->getMemAddressRange(ptr, + pBase, + pSize); +} + +ze_result_t ContextImp::closeIpcMemHandle(const void *ptr) { + DEBUG_BREAK_IF(nullptr == this->driverHandle); + return this->driverHandle->closeIpcMemHandle(ptr); +} + +ze_result_t ContextImp::getIpcMemHandle(const void *ptr, + ze_ipc_mem_handle_t *pIpcHandle) { + DEBUG_BREAK_IF(nullptr == this->driverHandle); + return this->driverHandle->getIpcMemHandle(ptr, + pIpcHandle); +} + +ze_result_t ContextImp::openIpcMemHandle(ze_device_handle_t hDevice, + ze_ipc_mem_handle_t handle, + ze_ipc_memory_flags_t flags, + void **ptr) { + DEBUG_BREAK_IF(nullptr == this->driverHandle); + return this->driverHandle->openIpcMemHandle(hDevice, + handle, + ZE_IPC_MEMORY_FLAG_NONE, + ptr); +} + +ze_result_t ContextImp::getMemAllocProperties(const void *ptr, + ze_memory_allocation_properties_t *pMemAllocProperties, + ze_device_handle_t *phDevice) { + DEBUG_BREAK_IF(nullptr == this->driverHandle); + return this->driverHandle->getMemAllocProperties(ptr, + pMemAllocProperties, + phDevice); +} + } // namespace L0 diff --git a/level_zero/core/source/context/context_imp.h b/level_zero/core/source/context/context_imp.h index 7ea2e7a879..29672f881a 100644 --- a/level_zero/core/source/context/context_imp.h +++ b/level_zero/core/source/context/context_imp.h @@ -18,6 +18,34 @@ struct ContextImp : Context { ze_result_t destroy() override; ze_result_t getStatus() override; DriverHandle *getDriverHandle() override; + ze_result_t allocHostMem(ze_host_mem_alloc_flag_t flags, + size_t size, + size_t alignment, + void **ptr) override; + ze_result_t allocDeviceMem(ze_device_handle_t hDevice, + ze_device_mem_alloc_flag_t flags, + size_t size, + size_t alignment, void **ptr) override; + ze_result_t allocSharedMem(ze_device_handle_t hDevice, + ze_device_mem_alloc_flag_t deviceFlags, + ze_host_mem_alloc_flag_t hostFlags, + size_t size, + size_t alignment, + void **ptr) override; + ze_result_t freeMem(const void *ptr) override; + ze_result_t getMemAddressRange(const void *ptr, + void **pBase, + size_t *pSize) override; + ze_result_t closeIpcMemHandle(const void *ptr) override; + ze_result_t getIpcMemHandle(const void *ptr, + ze_ipc_mem_handle_t *pIpcHandle) override; + ze_result_t openIpcMemHandle(ze_device_handle_t hDevice, + ze_ipc_mem_handle_t handle, + ze_ipc_memory_flags_t flags, + void **ptr) override; + ze_result_t getMemAllocProperties(const void *ptr, + ze_memory_allocation_properties_t *pMemAllocProperties, + ze_device_handle_t *phDevice) override; protected: DriverHandle *driverHandle = nullptr; diff --git a/level_zero/core/test/unit_tests/fixtures/device_fixture.h b/level_zero/core/test/unit_tests/fixtures/device_fixture.h index c0db363e48..6606ce34c1 100644 --- a/level_zero/core/test/unit_tests/fixtures/device_fixture.h +++ b/level_zero/core/test/unit_tests/fixtures/device_fixture.h @@ -12,6 +12,7 @@ #include "shared/test/unit_test/helpers/default_hw_info.h" #include "shared/test/unit_test/mocks/mock_device.h" +#include "level_zero/core/test/unit_tests/mocks/mock_context.h" #include "level_zero/core/test/unit_tests/mocks/mock_device.h" #include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.h" @@ -53,5 +54,24 @@ struct MultiDeviceFixture { const uint32_t numRootDevices = 4u; }; +struct ContextFixture : DeviceFixture { + void SetUp() override { + DeviceFixture::SetUp(); + + ze_context_handle_t hContext = {}; + ze_context_desc_t desc; + ze_result_t res = driverHandle->createContext(&desc, &hContext); + EXPECT_EQ(ZE_RESULT_SUCCESS, res); + EXPECT_NE(nullptr, hContext); + context = L0::Context::fromHandle(hContext); + } + + void TearDown() override { + context->destroy(); + DeviceFixture::TearDown(); + } + L0::Context *context = nullptr; +}; + } // namespace ult } // namespace L0 diff --git a/level_zero/core/test/unit_tests/mocks/mock_context.h b/level_zero/core/test/unit_tests/mocks/mock_context.h index 80394b59e6..569e1f64b2 100644 --- a/level_zero/core/test/unit_tests/mocks/mock_context.h +++ b/level_zero/core/test/unit_tests/mocks/mock_context.h @@ -37,6 +37,62 @@ struct Mock : public Context { getDriverHandle, (), (override)); + MOCK_METHOD(ze_result_t, + allocHostMem, + (ze_host_mem_alloc_flag_t flags, + size_t size, + size_t alignment, + void **ptr), + (override)); + MOCK_METHOD(ze_result_t, + allocDeviceMem, + (ze_device_handle_t hDevice, + ze_device_mem_alloc_flag_t flags, + size_t size, + size_t alignment, + void **ptr), + (override)); + MOCK_METHOD(ze_result_t, + allocSharedMem, + (ze_device_handle_t hDevice, + ze_device_mem_alloc_flag_t deviceFlags, + ze_host_mem_alloc_flag_t hostFlags, + size_t size, + size_t alignment, + void **ptr), + (override)); + MOCK_METHOD(ze_result_t, + freeMem, + (const void *ptr), + (override)); + MOCK_METHOD(ze_result_t, + getMemAllocProperties, + (const void *ptr, + ze_memory_allocation_properties_t *pMemAllocProperties, + ze_device_handle_t *phDevice), + (override)); + MOCK_METHOD(ze_result_t, + getMemAddressRange, + (const void *ptr, + void **pBase, + size_t *pSize), + (override)); + MOCK_METHOD(ze_result_t, + getIpcMemHandle, + (const void *ptr, + ze_ipc_mem_handle_t *pIpcHandle), + (override)); + MOCK_METHOD(ze_result_t, + closeIpcMemHandle, + (const void *ptr), + (override)); + MOCK_METHOD(ze_result_t, + openIpcMemHandle, + (ze_device_handle_t hDevice, + ze_ipc_mem_handle_t handle, + ze_ipc_memory_flags_t flags, + void **ptr), + (override)); }; } // namespace ult diff --git a/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h b/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h index b1d0cfebf4..b025c078de 100644 --- a/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h +++ b/level_zero/core/test/unit_tests/mocks/mock_driver_handle.h @@ -112,6 +112,15 @@ struct Mock : public DriverHandleImp { size_t alignment, void **ptr), (override)); + MOCK_METHOD(ze_result_t, + allocSharedMem, + (ze_device_handle_t hDevice, + ze_device_mem_alloc_flag_t deviceFlags, + ze_host_mem_alloc_flag_t hostFlags, + size_t size, + size_t alignment, + void **ptr), + (override)); MOCK_METHOD(ze_result_t, freeMem, (const void *ptr), diff --git a/level_zero/core/test/unit_tests/sources/context/test_context.cpp b/level_zero/core/test/unit_tests/sources/context/test_context.cpp index 0f404eb9da..4c9ef4cb10 100644 --- a/level_zero/core/test/unit_tests/sources/context/test_context.cpp +++ b/level_zero/core/test/unit_tests/sources/context/test_context.cpp @@ -24,7 +24,7 @@ TEST_F(ContextTest, givenCallToContextGetStatusThenUnsupportedIsReturned) { ze_result_t res = driverHandle->createContext(&desc, &hContext); EXPECT_EQ(ZE_RESULT_SUCCESS, res); - Context *context = L0::Context::fromHandle(hContext); + L0::Context *context = L0::Context::fromHandle(hContext); res = context->getStatus(); EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, res); diff --git a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp index 74370829cb..d6cd2e9a21 100644 --- a/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp +++ b/level_zero/core/test/unit_tests/sources/memory/test_memory.cpp @@ -347,5 +347,90 @@ TEST_F(AllocHostMemoryTest, EXPECT_EQ(nullptr, ptr); } +using ContextMemoryTest = Test; + +TEST_F(ContextMemoryTest, whenAllocatingSharedAllocationFromContextThenAllocationSucceeds) { + size_t size = 10u; + size_t alignment = 1u; + void *ptr = nullptr; + + ze_result_t result = context->allocSharedMem(device->toHandle(), + ZE_DEVICE_MEM_ALLOC_FLAG_DEFAULT, + ZE_HOST_MEM_ALLOC_FLAG_DEFAULT, + size, alignment, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, ptr); + + result = context->freeMem(ptr); + EXPECT_EQ(result, ZE_RESULT_SUCCESS); +} + +TEST_F(ContextMemoryTest, whenAllocatingHostAllocationFromContextThenAllocationSucceeds) { + size_t size = 10u; + size_t alignment = 1u; + void *ptr = nullptr; + + ze_result_t result = context->allocHostMem(ZE_HOST_MEM_ALLOC_FLAG_DEFAULT, + size, alignment, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, ptr); + + result = context->freeMem(ptr); + EXPECT_EQ(result, ZE_RESULT_SUCCESS); +} + +TEST_F(ContextMemoryTest, whenAllocatingDeviceAllocationFromContextThenAllocationSucceeds) { + size_t size = 10u; + size_t alignment = 1u; + void *ptr = nullptr; + + ze_result_t result = context->allocDeviceMem(device->toHandle(), + ZE_DEVICE_MEM_ALLOC_FLAG_DEFAULT, + size, alignment, &ptr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, ptr); + + result = context->freeMem(ptr); + EXPECT_EQ(result, ZE_RESULT_SUCCESS); +} + +TEST_F(ContextMemoryTest, whenRetrievingAddressRangeForDeviceAllocationThenRangeIsCorrect) { + size_t allocSize = 4096u; + size_t alignment = 1u; + void *allocPtr = nullptr; + + ze_result_t result = context->allocDeviceMem(device->toHandle(), + ZE_DEVICE_MEM_ALLOC_FLAG_DEFAULT, + allocSize, alignment, &allocPtr); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_NE(nullptr, allocPtr); + + void *base = nullptr; + size_t size = 0u; + void *pPtr = reinterpret_cast(reinterpret_cast(allocPtr) + 77); + result = context->getMemAddressRange(pPtr, &base, &size); + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(base, allocPtr); + EXPECT_GE(size, allocSize); + + result = context->freeMem(allocPtr); + EXPECT_EQ(result, ZE_RESULT_SUCCESS); +} + +TEST_F(ContextMemoryTest, givenSystemAllocatedPointerThenGetAllocPropertiesReturnsUnknownType) { + size_t size = 10; + int *ptr = new int[size]; + + ze_memory_allocation_properties_t memoryProperties = {}; + memoryProperties.version = ZE_MEMORY_ALLOCATION_PROPERTIES_VERSION_CURRENT; + ze_device_handle_t deviceHandle; + ze_result_t result = context->getMemAllocProperties(ptr, &memoryProperties, &deviceHandle); + + EXPECT_EQ(ZE_RESULT_SUCCESS, result); + EXPECT_EQ(memoryProperties.type, ZE_MEMORY_TYPE_UNKNOWN); + + delete[] ptr; +} + } // namespace ult } // namespace L0 diff --git a/third_party/level_zero/ze_api_ext.h b/third_party/level_zero/ze_api_ext.h index afd0521ea3..f540c1f067 100644 --- a/third_party/level_zero/ze_api_ext.h +++ b/third_party/level_zero/ze_api_ext.h @@ -427,6 +427,314 @@ zeDriverGetExtensionProperties( ///< extension properties ); +/////////////////////////////////////////////////////////////////////////////// +/// @brief Allocates shared memory on the context. +/// +/// @details +/// - Shared allocations share ownership between the host and one or more +/// devices. +/// - Shared allocations may optionally be associated with a device by +/// passing a handle to the device. +/// - Devices supporting only single-device shared access capabilities may +/// access shared memory associated with the device. +/// For these devices, ownership of the allocation is shared between the +/// host and the associated device only. +/// - Passing nullptr as the device handle does not associate the shared +/// allocation with any device. +/// For allocations with no associated device, ownership of the allocation +/// is shared between the host and all devices supporting cross-device +/// shared access capabilities. +/// - The application must only use the memory allocation for the context +/// and device, or its sub-devices, which was provided during allocation. +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function must be thread-safe. +/// +/// @returns +/// - ::ZE_RESULT_SUCCESS +/// - ::ZE_RESULT_ERROR_UNINITIALIZED +/// - ::ZE_RESULT_ERROR_DEVICE_LOST +/// - ::ZE_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `nullptr == hContext` +/// - ::ZE_RESULT_ERROR_INVALID_NULL_POINTER +/// + `nullptr == deviceDesc` +/// + `nullptr == hostDesc` +/// + `nullptr == pptr` +/// - ::ZE_RESULT_ERROR_INVALID_ENUMERATION +/// + `0x3 < deviceDesc->flags` +/// + `0x7 < hostDesc->flags` +/// - ::ZE_RESULT_ERROR_UNSUPPORTED_SIZE +/// + `0 == size` +/// - ::ZE_RESULT_ERROR_UNSUPPORTED_ALIGNMENT +/// + Must be zero or a power-of-two +/// + `0 != (alignment & (alignment - 1))` +/// - ::ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY +/// - ::ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemAllocShared( + ze_context_handle_t hContext, ///< [in] handle of the context object + const ze_device_mem_alloc_desc_t *deviceDesc, ///< [in] pointer to device memory allocation descriptor + const ze_host_mem_alloc_desc_t *hostDesc, ///< [in] pointer to host memory allocation descriptor + size_t size, ///< [in] size in bytes to allocate; must be less-than + ///< ::ze_device_properties_t.maxMemAllocSize. + size_t alignment, ///< [in] minimum alignment in bytes for the allocation; must be a power of + ///< two. + ze_device_handle_t hDevice, ///< [in][optional] device handle to associate with + void **pptr ///< [out] pointer to shared allocation +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Allocates device memory on the context. +/// +/// @details +/// - Device allocations are owned by a specific device. +/// - In general, a device allocation may only be accessed by the device +/// that owns it. +/// - The application must only use the memory allocation for the context +/// and device, or its sub-devices, which was provided during allocation. +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function must be thread-safe. +/// +/// @returns +/// - ::ZE_RESULT_SUCCESS +/// - ::ZE_RESULT_ERROR_UNINITIALIZED +/// - ::ZE_RESULT_ERROR_DEVICE_LOST +/// - ::ZE_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `nullptr == hContext` +/// + `nullptr == hDevice` +/// - ::ZE_RESULT_ERROR_INVALID_NULL_POINTER +/// + `nullptr == deviceDesc` +/// + `nullptr == pptr` +/// - ::ZE_RESULT_ERROR_INVALID_ENUMERATION +/// + `0x3 < deviceDesc->flags` +/// - ::ZE_RESULT_ERROR_UNSUPPORTED_SIZE +/// + `0 == size` +/// - ::ZE_RESULT_ERROR_UNSUPPORTED_ALIGNMENT +/// + Must be zero or a power-of-two +/// + `0 != (alignment & (alignment - 1))` +/// - ::ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY +/// - ::ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemAllocDevice( + ze_context_handle_t hContext, ///< [in] handle of the context object + const ze_device_mem_alloc_desc_t *deviceDesc, ///< [in] pointer to device memory allocation descriptor + size_t size, ///< [in] size in bytes to allocate; must be less-than + ///< ::ze_device_properties_t.maxMemAllocSize. + size_t alignment, ///< [in] minimum alignment in bytes for the allocation; must be a power of + ///< two. + ze_device_handle_t hDevice, ///< [in] handle of the device + void **pptr ///< [out] pointer to device allocation +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Allocates host memory on the context. +/// +/// @details +/// - Host allocations are owned by the host process. +/// - Host allocations are accessible by the host and all devices within the +/// driver's context. +/// - Host allocations are frequently used as staging areas to transfer data +/// to or from devices. +/// - The application must only use the memory allocation for the context +/// which was provided during allocation. +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function must be thread-safe. +/// +/// @returns +/// - ::ZE_RESULT_SUCCESS +/// - ::ZE_RESULT_ERROR_UNINITIALIZED +/// - ::ZE_RESULT_ERROR_DEVICE_LOST +/// - ::ZE_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `nullptr == hContext` +/// - ::ZE_RESULT_ERROR_INVALID_NULL_POINTER +/// + `nullptr == hostDesc` +/// + `nullptr == pptr` +/// - ::ZE_RESULT_ERROR_INVALID_ENUMERATION +/// + `0x7 < hostDesc->flags` +/// - ::ZE_RESULT_ERROR_UNSUPPORTED_SIZE +/// + `0 == size` +/// - ::ZE_RESULT_ERROR_UNSUPPORTED_ALIGNMENT +/// + Must be zero or a power-of-two +/// + `0 != (alignment & (alignment - 1))` +/// - ::ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY +/// - ::ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemAllocHost( + ze_context_handle_t hContext, ///< [in] handle of the context object + const ze_host_mem_alloc_desc_t *hostDesc, ///< [in] pointer to host memory allocation descriptor + size_t size, ///< [in] size in bytes to allocate; must be less-than + ///< ::ze_device_properties_t.maxMemAllocSize. + size_t alignment, ///< [in] minimum alignment in bytes for the allocation; must be a power of + ///< two. + void **pptr ///< [out] pointer to host allocation +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Frees allocated host memory, device memory, or shared memory on the +/// context. +/// +/// @details +/// - The application must ensure the device is not currently referencing +/// the memory before it is freed +/// - The implementation of this function may immediately free all Host and +/// Device allocations associated with this memory +/// - The application must **not** call this function from simultaneous +/// threads with the same pointer. +/// - The implementation of this function must be thread-safe. +/// +/// @returns +/// - ::ZE_RESULT_SUCCESS +/// - ::ZE_RESULT_ERROR_UNINITIALIZED +/// - ::ZE_RESULT_ERROR_DEVICE_LOST +/// - ::ZE_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `nullptr == hContext` +/// - ::ZE_RESULT_ERROR_INVALID_NULL_POINTER +/// + `nullptr == ptr` +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemFree( + ze_context_handle_t hContext, ///< [in] handle of the context object + void *ptr ///< [in][release] pointer to memory to free +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Retrieves attributes of a memory allocation +/// +/// @details +/// - The application may call this function from simultaneous threads. +/// - The application may query attributes of a memory allocation unrelated +/// to the context. +/// When this occurs, the returned allocation type will be +/// ::ZE_MEMORY_TYPE_UNKNOWN, and the returned identifier and associated +/// device is unspecified. +/// +/// @returns +/// - ::ZE_RESULT_SUCCESS +/// - ::ZE_RESULT_ERROR_UNINITIALIZED +/// - ::ZE_RESULT_ERROR_DEVICE_LOST +/// - ::ZE_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `nullptr == hContext` +/// - ::ZE_RESULT_ERROR_INVALID_NULL_POINTER +/// + `nullptr == ptr` +/// + `nullptr == pMemAllocProperties` +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemGetAllocProperties( + ze_context_handle_t hContext, ///< [in] handle of the context object + const void *ptr, ///< [in] memory pointer to query + ze_memory_allocation_properties_t *pMemAllocProperties, ///< [in,out] query result for memory allocation properties + ze_device_handle_t *phDevice ///< [out][optional] device associated with this allocation +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Retrieves the base address and/or size of an allocation +/// +/// @details +/// - The application may call this function from simultaneous threads. +/// +/// @returns +/// - ::ZE_RESULT_SUCCESS +/// - ::ZE_RESULT_ERROR_UNINITIALIZED +/// - ::ZE_RESULT_ERROR_DEVICE_LOST +/// - ::ZE_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `nullptr == hContext` +/// - ::ZE_RESULT_ERROR_INVALID_NULL_POINTER +/// + `nullptr == ptr` +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemGetAddressRange( + ze_context_handle_t hContext, ///< [in] handle of the context object + const void *ptr, ///< [in] memory pointer to query + void **pBase, ///< [in,out][optional] base address of the allocation + size_t *pSize ///< [in,out][optional] size of the allocation +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Supported IPC memory flags +typedef uint32_t ze_ipc_memory_flags_t; + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Opens an IPC memory handle to retrieve a device pointer on the +/// context. +/// +/// @details +/// - Takes an IPC memory handle from a remote process and associates it +/// with a device pointer usable in this process. +/// - The device pointer in this process should not be freed with +/// ::zeMemFree, but rather with ::zeMemCloseIpcHandle. +/// - Multiple calls to this function with the same IPC handle will return +/// unique pointers. +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function must be thread-safe. +/// +/// @returns +/// - ::ZE_RESULT_SUCCESS +/// - ::ZE_RESULT_ERROR_UNINITIALIZED +/// - ::ZE_RESULT_ERROR_DEVICE_LOST +/// - ::ZE_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `nullptr == hContext` +/// + `nullptr == hDevice` +/// - ::ZE_RESULT_ERROR_INVALID_ENUMERATION +/// + `0x1 < flags` +/// - ::ZE_RESULT_ERROR_INVALID_NULL_POINTER +/// + `nullptr == pptr` +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemOpenIpcHandle( + ze_context_handle_t hContext, ///< [in] handle of the context object + ze_device_handle_t hDevice, ///< [in] handle of the device to associate with the IPC memory handle + ze_ipc_mem_handle_t handle, ///< [in] IPC memory handle + ze_ipc_memory_flags_t flags, ///< [in] flags controlling the operation. + ///< must be 0 (default) or a valid combination of ::ze_ipc_memory_flag_t. + void **pptr ///< [out] pointer to device allocation in this process +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Closes an IPC memory handle +/// +/// @details +/// - Closes an IPC memory handle by unmapping memory that was opened in +/// this process using ::zeMemOpenIpcHandle. +/// - The application must **not** call this function from simultaneous +/// threads with the same pointer. +/// - The implementation of this function must be thread-safe. +/// +/// @returns +/// - ::ZE_RESULT_SUCCESS +/// - ::ZE_RESULT_ERROR_UNINITIALIZED +/// - ::ZE_RESULT_ERROR_DEVICE_LOST +/// - ::ZE_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `nullptr == hContext` +/// - ::ZE_RESULT_ERROR_INVALID_NULL_POINTER +/// + `nullptr == ptr` +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemCloseIpcHandle( + ze_context_handle_t hContext, ///< [in] handle of the context object + const void *ptr ///< [in][release] pointer to device allocation in this process +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Creates an IPC memory handle for the specified allocation +/// +/// @details +/// - Takes a pointer to a device memory allocation and creates an IPC +/// memory handle for exporting it for use in another process. +/// - The pointer must be base pointer of the device memory allocation; i.e. +/// the value returned from ::zeMemAllocDevice. +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function must be thread-safe. +/// +/// @returns +/// - ::ZE_RESULT_SUCCESS +/// - ::ZE_RESULT_ERROR_UNINITIALIZED +/// - ::ZE_RESULT_ERROR_DEVICE_LOST +/// - ::ZE_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `nullptr == hContext` +/// - ::ZE_RESULT_ERROR_INVALID_NULL_POINTER +/// + `nullptr == ptr` +/// + `nullptr == pIpcHandle` +ZE_APIEXPORT ze_result_t ZE_APICALL +zeMemGetIpcHandle( + ze_context_handle_t hContext, ///< [in] handle of the context object + const void *ptr, ///< [in] pointer to the device memory allocation + ze_ipc_mem_handle_t *pIpcHandle ///< [out] Returned IPC memory handle +); + } //extern C #endif // _ZE_API_EXT_H