feature: Add External Semaphore experimental [3/N]

Related-To: NEO-11488

Signed-off-by: Raiyan Latif <raiyan.latif@intel.com>
This commit is contained in:
Raiyan Latif
2024-12-05 17:30:07 +00:00
committed by Compute-Runtime-Automation
parent 263d84f0ed
commit 06c94d47f9
28 changed files with 763 additions and 15 deletions

View File

@@ -0,0 +1,14 @@
#
# Copyright (C) 2024 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
target_sources(${L0_STATIC_LIB_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/external_semaphore.h
${CMAKE_CURRENT_SOURCE_DIR}/external_semaphore_imp.h
${CMAKE_CURRENT_SOURCE_DIR}/external_semaphore_imp.cpp
)

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "shared/source/os_interface/product_helper.h"
#include "shared/source/utilities/tag_allocator.h"
#include "level_zero/core/source/helpers/api_handle_helper.h"
#include "level_zero/include/ze_intel_gpu.h"
#include <level_zero/ze_api.h>
#include <level_zero/zet_api.h>
#include <memory>
#include <mutex>
struct _ze_intel_external_semaphore_exp_handle_t {
const uint64_t objMagic = objMagicValue;
};
namespace L0 {
struct ExternalSemaphore : _ze_intel_external_semaphore_exp_handle_t {
virtual ~ExternalSemaphore() = default;
static ze_result_t importExternalSemaphore(ze_device_handle_t device, const ze_intel_external_semaphore_exp_desc_t *semaphoreDesc, ze_intel_external_semaphore_exp_handle_t *phSemaphore);
virtual ze_result_t releaseExternalSemaphore() = 0;
virtual ze_result_t appendWait(const NEO::CommandStreamReceiver *csr) = 0;
virtual ze_result_t appendSignal(const NEO::CommandStreamReceiver *csr) = 0;
static ExternalSemaphore *fromHandle(ze_intel_external_semaphore_exp_handle_t handle) { return static_cast<ExternalSemaphore *>(handle); }
inline ze_intel_external_semaphore_exp_handle_t toHandle() { return this; }
protected:
ze_intel_external_semaphore_exp_desc_t desc;
};
} // namespace L0

View File

@@ -0,0 +1,114 @@
/*
* Copyright (C) 2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "level_zero/core/source/semaphore/external_semaphore_imp.h"
#include "shared/source/device/device.h"
#include "level_zero/core/source/device/device.h"
#include "level_zero/core/source/driver/driver_handle_imp.h"
namespace L0 {
ze_result_t ExternalSemaphore::importExternalSemaphore(ze_device_handle_t device, const ze_intel_external_semaphore_exp_desc_t *semaphoreDesc, ze_intel_external_semaphore_exp_handle_t *phSemaphore) {
auto externalSemaphore = new ExternalSemaphoreImp();
if (externalSemaphore == nullptr) {
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
auto result = externalSemaphore->initialize(device, semaphoreDesc);
if (result != ZE_RESULT_SUCCESS) {
delete externalSemaphore;
return result;
}
*phSemaphore = externalSemaphore;
return result;
}
ze_result_t ExternalSemaphoreImp::initialize(ze_device_handle_t device, const ze_intel_external_semaphore_exp_desc_t *semaphoreDesc) {
auto deviceImp = Device::fromHandle(device);
this->neoDevice = Device::fromHandle(device)->getNEODevice();
this->desc = semaphoreDesc;
NEO::ExternalSemaphoreHelper::Type externalSemaphoreType;
void *handle = nullptr;
int fd = 0;
if (semaphoreDesc->pNext != nullptr) {
const ze_base_desc_t *extendedDesc =
reinterpret_cast<const ze_base_desc_t *>(semaphoreDesc->pNext);
if (extendedDesc->stype == ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_WIN32_EXP_DESC) { // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange)
const ze_intel_external_semaphore_win32_exp_desc_t *extendedSemaphoreDesc =
reinterpret_cast<const ze_intel_external_semaphore_win32_exp_desc_t *>(extendedDesc);
handle = extendedSemaphoreDesc->handle;
} else if (extendedDesc->stype == ZE_INTEL_STRUCTURE_TYPE_EXTERNAL_SEMAPHORE_FD_EXP_DESC) { // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange)
const ze_intel_external_semaphore_desc_fd_exp_desc_t *extendedSemaphoreDesc =
reinterpret_cast<const ze_intel_external_semaphore_desc_fd_exp_desc_t *>(extendedDesc);
fd = extendedSemaphoreDesc->fd;
} else {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
} else {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
switch (semaphoreDesc->flags) {
case ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_FD:
externalSemaphoreType = NEO::ExternalSemaphoreHelper::Type::OpaqueFd;
break;
case ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_WIN32:
externalSemaphoreType = NEO::ExternalSemaphoreHelper::Type::OpaqueWin32;
break;
case ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_OPAQUE_WIN32_KMT:
externalSemaphoreType = NEO::ExternalSemaphoreHelper::Type::OpaqueWin32Kmt;
break;
case ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_D3D12_FENCE:
externalSemaphoreType = NEO::ExternalSemaphoreHelper::Type::D3d12Fence;
break;
case ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_D3D11_FENCE:
externalSemaphoreType = NEO::ExternalSemaphoreHelper::Type::D3d11Fence;
break;
case ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_KEYED_MUTEX:
externalSemaphoreType = NEO::ExternalSemaphoreHelper::Type::KeyedMutex;
break;
case ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_KEYED_MUTEX_KMT:
externalSemaphoreType = NEO::ExternalSemaphoreHelper::Type::KeyedMutexKmt;
break;
case ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_TIMELINE_SEMAPHORE_FD:
externalSemaphoreType = NEO::ExternalSemaphoreHelper::Type::TimelineSemaphoreFd;
break;
case ZE_EXTERNAL_SEMAPHORE_EXP_FLAGS_TIMELINE_SEMAPHORE_WIN32:
externalSemaphoreType = NEO::ExternalSemaphoreHelper::Type::TimelineSemaphoreWin32;
break;
default:
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
this->neoExternalSemaphore = NEO::ExternalSemaphore::create(&deviceImp->getOsInterface(), externalSemaphoreType, handle, fd);
if (!this->neoExternalSemaphore) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
return ZE_RESULT_SUCCESS;
}
ze_result_t ExternalSemaphoreImp::releaseExternalSemaphore() {
delete this;
return ZE_RESULT_SUCCESS;
}
ze_result_t ExternalSemaphoreImp::appendWait(const NEO::CommandStreamReceiver *csr) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ze_result_t ExternalSemaphoreImp::appendSignal(const NEO::CommandStreamReceiver *csr) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
} // namespace L0

View File

@@ -0,0 +1,38 @@
/*
* Copyright (C) 2024 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "shared/source/semaphore/external_semaphore.h"
#include "level_zero/core/source/event/event.h"
#include "level_zero/core/source/semaphore/external_semaphore.h"
#include <level_zero/ze_api.h>
#include <level_zero/zet_api.h>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <thread>
namespace L0 {
class ExternalSemaphoreImp : public ExternalSemaphore {
public:
ze_result_t initialize(ze_device_handle_t device, const ze_intel_external_semaphore_exp_desc_t *semaphoreDesc);
ze_result_t releaseExternalSemaphore() override;
ze_result_t appendWait(const NEO::CommandStreamReceiver *csr) override;
ze_result_t appendSignal(const NEO::CommandStreamReceiver *csr) override;
protected:
NEO::Device *neoDevice = nullptr;
const ze_intel_external_semaphore_exp_desc_t *desc;
std::unique_ptr<NEO::ExternalSemaphore> neoExternalSemaphore;
};
} // namespace L0