Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ Interface Core
==============

Core represents an OpenVINO runtime Core entity.
User applications can create several Core class instances,
but in this case, the underlying plugins
are created multiple times and not shared between several Core instances.
It is recommended to have a single Core instance per application.
User applications can create several Core class instances.
In that case the device plugins will still share
underlying resources (such as OCL context) in per-device singleton.

.. code-block:: ts

Expand Down
7 changes: 3 additions & 4 deletions src/bindings/js/node/lib/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ export type OVAny = string | number | boolean;
/**
* Core represents an OpenVINO runtime Core entity.
*
* User applications can create several Core class instances,
* but in this case, the underlying plugins
* are created multiple times and not shared between several Core instances.
* It is recommended to have a single Core instance per application.
* User applications can create several Core class instances.
* In that case the device plugins will still share underlying resources
* (such as OCL context) in per-device singleton.
*/
export interface Core {
/**
Expand Down
7 changes: 3 additions & 4 deletions src/bindings/python/src/openvino/_ov_api.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,9 @@ class Core(openvino._pyopenvino.Core):
"""
Core class represents OpenVINO runtime Core entity.

User applications can create several Core class instances, but in this
case, the underlying plugins are created multiple times and not shared
between several Core instances. The recommended way is to have a single
Core instance per application.
User applications can create several Core class instances. In that case
the device plugins will still share underlying resources (such as OCL context)
in per-device singleton.

"""
def compile_model(self, model: typing.Union[openvino._ov_api.Model, str, pathlib.Path], device_name: typing.Optional[str] = None, config: typing.Optional[dict[str, typing.Any]] = None, *, weights: typing.Optional[bytes] = None) -> CompiledModel:
Expand Down
7 changes: 3 additions & 4 deletions src/bindings/python/src/pyopenvino/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ namespace py = pybind11;

void regclass_Core(py::module m) {
py::class_<ov::Core, std::shared_ptr<ov::Core>> cls(m, "Core");
cls.doc() =
"openvino.Core class represents OpenVINO runtime Core entity. User applications can create several "
"Core class instances, but in this case, the underlying plugins are created multiple times and not shared "
"between several Core instances. The recommended way is to have a single Core instance per application.";
cls.doc() = "openvino.Core class represents OpenVINO runtime Core entity. User applications can create several "
"Core class instances. In that case the device plugins will still share underlying "
"resources (such as OCL context) in per-device singleton.";

cls.def(py::init([](const py::object& xml_config_file) {
return std::make_shared<ov::Core>(Common::utils::to_fs_path(xml_config_file));
Expand Down
5 changes: 2 additions & 3 deletions src/inference/include/openvino/runtime/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ namespace ov {
/**
* @brief This class represents an OpenVINO runtime Core entity.
* @ingroup ov_runtime_cpp_api
* User applications can create several Core class instances, but in this case the underlying plugins
* are created multiple times and not shared between several Core instances. The recommended way is to have
* a single Core instance per application.
* User applications can create several Core class instances. In that case the device plugins
* will still share underlying resources (such as OCL context) in per-device singleton.
*/
class OPENVINO_RUNTIME_API Core {
class Impl;
Expand Down
21 changes: 19 additions & 2 deletions src/plugins/intel_gpu/src/plugin/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,31 @@ std::shared_ptr<ov::Model> Plugin::clone_and_transform_model(const std::shared_p
return cloned_model;
}

// weak map to hold singleton default contexts
// Weak singleton map is used to share contexts between multiple plugin(or Core) instances.
// It is needed to ensure that context is released before plugin is unloaded.
// As the actual ownership is in plugin class, the ownership is released when plugin is destructed.
std::map<std::string, std::weak_ptr<RemoteContextImpl>> weak_singleton_default_contexts;
std::mutex singleton_default_contexts_mutex;

std::map<std::string, RemoteContextImpl::Ptr> Plugin::get_default_contexts() const {
std::call_once(m_default_contexts_once, [this]() {
// Create default context
std::lock_guard<std::mutex> lock(singleton_default_contexts_mutex);
for (auto& device : m_device_map) {
const auto device_name = get_device_name() + "." + device.first;

// If already initialized, use existing one
if (weak_singleton_default_contexts.find(device.first) != weak_singleton_default_contexts.end()) {
if (auto ctx = weak_singleton_default_contexts[device.first].lock()) {
m_default_contexts[device.first] = ctx;
continue;
}
}
// If context is not created yet or expired, create new one
const auto initialize = false;
auto ctx = std::make_shared<RemoteContextImpl>(device_name, std::vector<cldnn::device::ptr>{device.second}, initialize);
m_default_contexts.insert({device.first, ctx});
weak_singleton_default_contexts[device.first] = ctx;
m_default_contexts[device.first] = ctx;
}
});
return m_default_contexts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "common_test_utils/subgraph_builders/read_concat_split_assign.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/unsqueeze.hpp"
#include "openvino/runtime/intel_gpu/ocl/ocl.hpp"
#include "openvino/runtime/intel_gpu/ocl/ocl_wrapper.hpp"

namespace {
typedef std::tuple<
Expand Down Expand Up @@ -398,4 +400,15 @@ TEST(TensorTest, smoke_canShareTensorIfModelsFromDifferentCores) {
OV_ASSERT_NO_THROW(request1.infer());
OV_ASSERT_NO_THROW(request2.infer());
}

TEST(CoreTest, smoke_singletonOclContext) {
auto core1 = ov::Core();
auto ctx1 = core1.get_default_context("GPU");
auto& oclContext1 = static_cast<ov::intel_gpu::ocl::ClContext&>(ctx1);
auto core2 = ov::Core();
auto ctx2 = core2.get_default_context("GPU");
auto& oclContext2 = static_cast<ov::intel_gpu::ocl::ClContext&>(ctx2);
ASSERT_EQ(oclContext1.get(), oclContext2.get());
}

} // namespace
Loading