2021-02-15 21:35:54 +03:00
|
|
|
// Copyright (C) 2018-2021 Intel Corporation
|
2020-02-11 22:48:49 +03:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include "cldnn_remote_context.h"
|
2021-01-14 09:56:18 +03:00
|
|
|
#include "cldnn_itt.h"
|
2020-02-11 22:48:49 +03:00
|
|
|
|
2021-06-16 09:27:16 +03:00
|
|
|
#include "cldnn/runtime/device_query.hpp"
|
|
|
|
|
|
2020-02-11 22:48:49 +03:00
|
|
|
using namespace InferenceEngine;
|
|
|
|
|
using namespace InferenceEngine::gpu;
|
|
|
|
|
using namespace InferenceEngine::details;
|
|
|
|
|
|
|
|
|
|
namespace CLDNNPlugin {
|
|
|
|
|
CLDNNRemoteAllocator CLDNNRemoteBlobImpl::m_allocator;
|
|
|
|
|
|
|
|
|
|
CLDNNRemoteBlobImpl::CLDNNRemoteBlobImpl(ClContext::Ptr context,
|
2021-06-16 09:27:16 +03:00
|
|
|
cldnn::stream& stream,
|
2020-02-11 22:48:49 +03:00
|
|
|
const cldnn::layout& layout,
|
|
|
|
|
cldnn::shared_handle mem,
|
|
|
|
|
cldnn::shared_surface surf,
|
|
|
|
|
uint32_t plane,
|
|
|
|
|
BlobType mem_type) :
|
2021-06-16 09:27:16 +03:00
|
|
|
m_context(context), m_stream(stream), m_layout(layout), m_mem_type(mem_type), m_mem(mem), m_surf(surf), m_plane(plane),
|
2021-03-16 08:28:00 +03:00
|
|
|
_handle(nullptr), _allocator(nullptr), m_memObject(nullptr), lockedHolder(nullptr) {
|
2020-02-11 22:48:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParamMap CLDNNRemoteBlobImpl::getParams() const {
|
|
|
|
|
assert(m_memObject != nullptr);
|
|
|
|
|
auto params = m_memObject->get_internal_params();
|
|
|
|
|
|
|
|
|
|
switch (m_mem_type) {
|
|
|
|
|
case BT_BUF_INTERNAL:
|
|
|
|
|
case BT_BUF_SHARED:
|
|
|
|
|
return{
|
|
|
|
|
{ GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(OCL_BUFFER) },
|
|
|
|
|
{ GPU_PARAM_KEY(OCL_CONTEXT), params.context },
|
|
|
|
|
{ GPU_PARAM_KEY(MEM_HANDLE), params.mem }
|
|
|
|
|
};
|
2021-02-02 20:11:40 +03:00
|
|
|
#ifdef _WIN32
|
2020-02-11 22:48:49 +03:00
|
|
|
case BT_DX_BUF_SHARED:
|
|
|
|
|
return{
|
|
|
|
|
{ GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(DX_BUFFER) },
|
|
|
|
|
{ GPU_PARAM_KEY(OCL_CONTEXT), params.context },
|
|
|
|
|
{ GPU_PARAM_KEY(VA_DEVICE), params.user_device },
|
|
|
|
|
{ GPU_PARAM_KEY(MEM_HANDLE), params.mem },
|
|
|
|
|
{ GPU_PARAM_KEY(DEV_OBJECT_HANDLE), params.surface }
|
|
|
|
|
};
|
|
|
|
|
#endif
|
|
|
|
|
case BT_IMG_SHARED:
|
|
|
|
|
return{
|
|
|
|
|
{ GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(OCL_IMAGE2D) },
|
|
|
|
|
{ GPU_PARAM_KEY(OCL_CONTEXT), params.context },
|
|
|
|
|
{ GPU_PARAM_KEY(MEM_HANDLE), params.mem }
|
|
|
|
|
};
|
|
|
|
|
case BT_SURF_SHARED:
|
|
|
|
|
return{
|
|
|
|
|
{ GPU_PARAM_KEY(SHARED_MEM_TYPE), GPU_PARAM_VALUE(VA_SURFACE) },
|
|
|
|
|
{ GPU_PARAM_KEY(OCL_CONTEXT), params.context },
|
|
|
|
|
{ GPU_PARAM_KEY(VA_DEVICE), params.user_device },
|
|
|
|
|
{ GPU_PARAM_KEY(MEM_HANDLE), params.mem },
|
|
|
|
|
{ GPU_PARAM_KEY(DEV_OBJECT_HANDLE), params.surface },
|
|
|
|
|
{ GPU_PARAM_KEY(VA_PLANE), params.plane }
|
|
|
|
|
};
|
|
|
|
|
default:
|
2021-03-23 18:57:12 +03:00
|
|
|
IE_THROW() << "Unsupported shared object type " << m_mem_type;
|
2020-02-11 22:48:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CLDNNRemoteBlobImpl::deallocate() noexcept {
|
2021-06-16 09:27:16 +03:00
|
|
|
m_memObject.reset();
|
2020-02-11 22:48:49 +03:00
|
|
|
return m_memObject == nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CLDNNRemoteBlobImpl::is_allocated() const noexcept {
|
|
|
|
|
return m_memObject != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CLDNNRemoteBlobImpl::is_locked() const noexcept {
|
|
|
|
|
return lockedHolder != nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CLDNNRemoteBlobImpl::allocate() noexcept {
|
2021-08-04 16:53:56 +03:00
|
|
|
OV_ITT_SCOPED_TASK(itt::domains::CLDNNPlugin, "CLDNNRemoteBlobImpl::Allocate");
|
2020-02-11 22:48:49 +03:00
|
|
|
assert(m_memObject == nullptr);
|
|
|
|
|
|
2021-08-04 16:53:56 +03:00
|
|
|
auto _impl = getContextImpl(m_context.lock());
|
|
|
|
|
_impl->acquire_lock();
|
|
|
|
|
std::shared_ptr<cldnn::engine> eng = _impl->GetEngine();
|
2020-02-11 22:48:49 +03:00
|
|
|
|
|
|
|
|
switch (m_mem_type) {
|
2021-06-16 09:27:16 +03:00
|
|
|
case BlobType::BT_BUF_INTERNAL: {
|
|
|
|
|
m_memObject = eng->allocate_memory(m_layout);
|
2020-02-11 22:48:49 +03:00
|
|
|
break;
|
2021-06-16 09:27:16 +03:00
|
|
|
}
|
|
|
|
|
case BlobType::BT_BUF_SHARED: {
|
|
|
|
|
m_memObject = eng->share_buffer(m_layout, m_mem);
|
2020-02-11 22:48:49 +03:00
|
|
|
break;
|
2021-06-16 09:27:16 +03:00
|
|
|
}
|
2021-02-02 20:11:40 +03:00
|
|
|
#ifdef _WIN32
|
2021-06-16 09:27:16 +03:00
|
|
|
case BlobType::BT_SURF_SHARED: {
|
|
|
|
|
m_memObject = eng->share_surface(m_layout, m_mem, m_plane);
|
2020-02-11 22:48:49 +03:00
|
|
|
break;
|
2021-06-16 09:27:16 +03:00
|
|
|
}
|
|
|
|
|
case BlobType::BT_DX_BUF_SHARED: {
|
|
|
|
|
m_memObject = eng->share_dx_buffer(m_layout, m_mem);
|
2020-02-11 22:48:49 +03:00
|
|
|
break;
|
2021-06-16 09:27:16 +03:00
|
|
|
}
|
2020-02-11 22:48:49 +03:00
|
|
|
#else
|
2021-06-16 09:27:16 +03:00
|
|
|
case BlobType::BT_SURF_SHARED: {
|
|
|
|
|
m_memObject = eng->share_surface(m_layout, m_surf, m_plane);
|
2020-02-11 22:48:49 +03:00
|
|
|
break;
|
2021-06-16 09:27:16 +03:00
|
|
|
}
|
2020-02-11 22:48:49 +03:00
|
|
|
#endif
|
2021-06-16 09:27:16 +03:00
|
|
|
case BlobType::BT_IMG_SHARED: {
|
|
|
|
|
m_memObject = eng->share_image(m_layout, m_mem);
|
2020-02-11 22:48:49 +03:00
|
|
|
break;
|
2021-06-16 09:27:16 +03:00
|
|
|
}
|
2020-02-11 22:48:49 +03:00
|
|
|
default:
|
2021-06-16 09:27:16 +03:00
|
|
|
m_memObject.reset();
|
2020-02-11 22:48:49 +03:00
|
|
|
}
|
2021-08-04 16:53:56 +03:00
|
|
|
_impl->release_lock();
|
2020-02-11 22:48:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::shared_ptr<IAllocator>& CLDNNRemoteBlobImpl::getAllocator() const noexcept {
|
|
|
|
|
if (!_allocator) {
|
2021-03-05 12:08:01 +03:00
|
|
|
_allocator = std::shared_ptr<IAllocator>(&m_allocator, [] (IAllocator*) {});
|
2020-02-11 22:48:49 +03:00
|
|
|
}
|
|
|
|
|
return _allocator;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::string CLDNNRemoteBlobImpl::getDeviceName() const noexcept {
|
2021-08-23 11:44:51 +03:00
|
|
|
return getContextImpl(m_context.lock())->getDeviceName();
|
2020-02-11 22:48:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<RemoteContext> CLDNNRemoteBlobImpl::getContext() const noexcept {
|
2020-04-13 21:17:23 +03:00
|
|
|
return std::dynamic_pointer_cast<RemoteContext>(m_context.lock());
|
2020-02-11 22:48:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CLDNNRemoteBlobImpl::lock() const {
|
2021-06-16 09:27:16 +03:00
|
|
|
lockedHolder = std::unique_ptr<cldnn::mem_lock<uint8_t>>(new cldnn::mem_lock<uint8_t>(m_memObject, m_stream));
|
2020-02-11 22:48:49 +03:00
|
|
|
auto ptr = lockedHolder->data();
|
|
|
|
|
_handle = reinterpret_cast<void*>(ptr);
|
|
|
|
|
m_allocator.regLockedBlob(_handle, this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CLDNNRemoteBlobImpl::unlock() const {
|
2021-08-04 16:53:56 +03:00
|
|
|
lockedHolder.reset();
|
2020-02-11 22:48:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LockedMemory<void> CLDNNRemoteBlobImpl::buffer() noexcept {
|
|
|
|
|
lock();
|
|
|
|
|
return LockedMemory<void>(reinterpret_cast<IAllocator*>(&m_allocator), _handle, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LockedMemory<const void> CLDNNRemoteBlobImpl::cbuffer() const noexcept {
|
|
|
|
|
lock();
|
|
|
|
|
return LockedMemory<const void>(reinterpret_cast<IAllocator*>(&m_allocator), _handle, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LockedMemory<void> CLDNNRemoteBlobImpl::rwmap()noexcept {
|
|
|
|
|
lock();
|
|
|
|
|
return LockedMemory<void>(reinterpret_cast<IAllocator *>(&m_allocator), _handle, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LockedMemory<const void> CLDNNRemoteBlobImpl::rmap() const noexcept {
|
|
|
|
|
lock();
|
|
|
|
|
return LockedMemory<const void>(reinterpret_cast<IAllocator *>(&m_allocator), _handle, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LockedMemory<void> CLDNNRemoteBlobImpl::wmap()noexcept {
|
|
|
|
|
lock();
|
|
|
|
|
return LockedMemory<void>(reinterpret_cast<IAllocator *>(&m_allocator), _handle, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CLDNNRemoteAllocator::regLockedBlob(void* handle, const CLDNNRemoteBlobImpl* blob) {
|
|
|
|
|
acquire_lock();
|
|
|
|
|
auto iter = m_lockedBlobs.find(handle);
|
|
|
|
|
if (iter == m_lockedBlobs.end()) {
|
|
|
|
|
m_lockedBlobs.emplace(handle, blob);
|
|
|
|
|
}
|
|
|
|
|
release_lock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CLDNNRemoteAllocator::unlock(void* handle) noexcept {
|
|
|
|
|
acquire_lock();
|
|
|
|
|
auto iter = m_lockedBlobs.find(handle);
|
|
|
|
|
if (iter != m_lockedBlobs.end()) {
|
|
|
|
|
iter->second->unlock();
|
|
|
|
|
m_lockedBlobs.erase(iter);
|
|
|
|
|
}
|
|
|
|
|
release_lock();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:11:54 +03:00
|
|
|
CLDNNExecutionContextImpl::CLDNNExecutionContextImpl(const std::shared_ptr<IInferencePlugin> plugin,
|
2020-02-11 22:48:49 +03:00
|
|
|
const ParamMap& params,
|
|
|
|
|
const Config& config) :
|
|
|
|
|
m_plugin(plugin),
|
|
|
|
|
m_type(ContextType::OCL),
|
|
|
|
|
m_config(config),
|
|
|
|
|
m_va_display(nullptr) {
|
|
|
|
|
lock.clear(std::memory_order_relaxed);
|
|
|
|
|
gpu_handle_param _context_id = nullptr;
|
|
|
|
|
gpu_handle_param _va_device = nullptr;
|
|
|
|
|
|
|
|
|
|
if (params.size()) {
|
|
|
|
|
// parameter map is non-empty
|
|
|
|
|
std::string contextTypeStr = _StrFromParams(params, GPU_PARAM_KEY(CONTEXT_TYPE));
|
|
|
|
|
|
|
|
|
|
if (GPU_PARAM_VALUE(OCL) == contextTypeStr) {
|
|
|
|
|
_context_id = _ObjFromParamSimple<gpu_handle_param>(params, GPU_PARAM_KEY(OCL_CONTEXT));
|
|
|
|
|
} else if (GPU_PARAM_VALUE(VA_SHARED) == contextTypeStr) {
|
|
|
|
|
m_va_display = _va_device = _ObjFromParamSimple<gpu_handle_param>(params, GPU_PARAM_KEY(VA_DEVICE));
|
|
|
|
|
m_type = ContextType::DEV_SHARED;
|
|
|
|
|
} else {
|
2021-03-23 18:57:12 +03:00
|
|
|
IE_THROW() << "Invalid execution context type" << contextTypeStr;
|
2020-02-11 22:48:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-16 09:27:16 +03:00
|
|
|
// TODO: Parameterize this based on plugin config and compilation options
|
|
|
|
|
auto engine_type = cldnn::engine_types::ocl;
|
|
|
|
|
auto runtime_type = cldnn::runtime_types::ocl;
|
|
|
|
|
// Use actual runtime and engine types
|
|
|
|
|
cldnn::device_query device_query(engine_type, runtime_type, _context_id, _va_device);
|
2020-02-11 22:48:49 +03:00
|
|
|
auto device_map = device_query.get_available_devices();
|
|
|
|
|
|
|
|
|
|
auto iter = device_map.find(m_config.device_id);
|
|
|
|
|
auto& dev = iter != device_map.end() ? iter->second : device_map.begin()->second;
|
|
|
|
|
|
2021-01-14 09:56:18 +03:00
|
|
|
{
|
|
|
|
|
OV_ITT_SCOPED_TASK(itt::domains::CLDNNPlugin, "CLDNNExecutionContextImpl::Create");
|
2021-06-16 09:27:16 +03:00
|
|
|
bool enable_profiling = (m_config.useProfiling ||
|
2021-01-14 09:56:18 +03:00
|
|
|
(m_config.tuningConfig.mode == cldnn::tuning_mode::tuning_tune_and_cache) ||
|
2021-06-16 09:27:16 +03:00
|
|
|
(m_config.tuningConfig.mode == cldnn::tuning_mode::tuning_retune_and_cache));
|
|
|
|
|
cldnn::queue_types queue_type = cldnn::queue_types::out_of_order;
|
|
|
|
|
bool use_unified_shared_memory = true;
|
|
|
|
|
m_engine = cldnn::engine::create(engine_type, runtime_type, dev, cldnn::engine_configuration(enable_profiling,
|
|
|
|
|
queue_type,
|
|
|
|
|
m_config.sources_dumps_dir,
|
|
|
|
|
m_config.queuePriority,
|
|
|
|
|
m_config.queueThrottle,
|
|
|
|
|
m_config.memory_pool_on,
|
|
|
|
|
use_unified_shared_memory,
|
|
|
|
|
m_config.kernels_cache_dir,
|
|
|
|
|
m_config.n_threads));
|
2021-01-14 09:56:18 +03:00
|
|
|
}
|
2020-02-11 22:48:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParamMap CLDNNExecutionContextImpl::getParams() const {
|
2021-06-16 09:27:16 +03:00
|
|
|
ParamMap ret = { { GPU_PARAM_KEY(OCL_CONTEXT), m_engine->get_user_context() } };
|
2020-02-11 22:48:49 +03:00
|
|
|
|
|
|
|
|
switch (m_type) {
|
|
|
|
|
case OCL:
|
|
|
|
|
ret[GPU_PARAM_KEY(CONTEXT_TYPE)] = GPU_PARAM_VALUE(OCL);
|
|
|
|
|
break;
|
|
|
|
|
case DEV_SHARED:
|
|
|
|
|
ret[GPU_PARAM_KEY(CONTEXT_TYPE)] = GPU_PARAM_VALUE(VA_SHARED);
|
|
|
|
|
ret[GPU_PARAM_KEY(VA_DEVICE)] = m_va_display;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2021-03-23 18:57:12 +03:00
|
|
|
IE_THROW() << "Unsupported shared context type " << m_type;
|
2020-02-11 22:48:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string CLDNNExecutionContextImpl::getDeviceName() const noexcept {
|
2021-08-23 11:44:51 +03:00
|
|
|
auto devName = m_plugin.lock()->GetName();
|
|
|
|
|
if (!m_config.device_id.empty())
|
|
|
|
|
devName += "." + m_config.device_id;
|
|
|
|
|
return devName;
|
2020-02-11 22:48:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}; // namespace CLDNNPlugin
|