Skip to content

Commit efc629e

Browse files
committed
[GPU] Make singleton OCL Context
1 parent d2d98f2 commit efc629e

File tree

8 files changed

+30
-22
lines changed

8 files changed

+30
-22
lines changed

docs/sphinx_setup/api/nodejs_api/openvino-node/interfaces/Core.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ Interface Core
22
==============
33

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

109
.. code-block:: ts
1110

src/bindings/js/node/lib/addon.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ export type OVAny = string | number | boolean;
2828
/**
2929
* Core represents an OpenVINO runtime Core entity.
3030
*
31-
* User applications can create several Core class instances,
32-
* but in this case, the underlying plugins
33-
* are created multiple times and not shared between several Core instances.
34-
* It is recommended to have a single Core instance per application.
31+
* User applications can create several Core class instances.
32+
* In that case the device plugins will still share underlying resources
33+
* (such as OCL context) in per-device singleton.
3534
*/
3635
export interface Core {
3736
/**

src/bindings/python/src/openvino/_ov_api.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,9 @@ class Core(openvino._pyopenvino.Core):
273273
"""
274274
Core class represents OpenVINO runtime Core entity.
275275
276-
User applications can create several Core class instances, but in this
277-
case, the underlying plugins are created multiple times and not shared
278-
between several Core instances. The recommended way is to have a single
279-
Core instance per application.
276+
User applications can create several Core class instances. In that case
277+
the device plugins will still share underlying resources (such as OCL context)
278+
in per-device singleton.
280279
281280
"""
282281
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:

src/bindings/python/src/pyopenvino/core/core.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ namespace py = pybind11;
2323

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

3130
cls.def(py::init([](const py::object& xml_config_file) {
3231
return std::make_shared<ov::Core>(Common::utils::to_fs_path(xml_config_file));

src/inference/include/openvino/runtime/core.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ namespace ov {
3131
/**
3232
* @brief This class represents an OpenVINO runtime Core entity.
3333
* @ingroup ov_runtime_cpp_api
34-
* User applications can create several Core class instances, but in this case the underlying plugins
35-
* are created multiple times and not shared between several Core instances. The recommended way is to have
36-
* a single Core instance per application.
34+
* User applications can create several Core class instances. In that case the device plugins
35+
* will still share underlying resources (such as OCL context) in per-device singleton.
3736
*/
3837
class OPENVINO_RUNTIME_API Core {
3938
class Impl;

src/plugins/intel_gpu/include/intel_gpu/plugin/plugin.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ class Plugin : public ov::IPlugin {
2020
std::map<std::string, ExecutionConfig> m_configs_map;
2121
ov::AnyMap m_compiled_model_runtime_properties;
2222

23-
mutable std::map<std::string, std::shared_ptr<RemoteContextImpl>> m_default_contexts;
24-
mutable std::once_flag m_default_contexts_once;
25-
2623
std::map<std::string, std::shared_ptr<RemoteContextImpl>> get_default_contexts() const;
2724

2825
std::shared_ptr<ov::Model> clone_and_transform_model(const std::shared_ptr<const ov::Model>& network,

src/plugins/intel_gpu/src/plugin/plugin.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ std::shared_ptr<ov::Model> Plugin::clone_and_transform_model(const std::shared_p
201201
}
202202

203203
std::map<std::string, RemoteContextImpl::Ptr> Plugin::get_default_contexts() const {
204+
static std::map<std::string, std::shared_ptr<RemoteContextImpl>> m_default_contexts;
205+
static std::once_flag m_default_contexts_once;
206+
204207
std::call_once(m_default_contexts_once, [this]() {
205208
// Create default context
206209
for (auto& device : m_device_map) {

src/plugins/intel_gpu/tests/functional/behavior/infer_request.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "common_test_utils/subgraph_builders/read_concat_split_assign.hpp"
1919
#include "openvino/op/add.hpp"
2020
#include "openvino/op/unsqueeze.hpp"
21+
#include "openvino/runtime/intel_gpu/ocl/ocl.hpp"
22+
#include "openvino/runtime/intel_gpu/ocl/ocl_wrapper.hpp"
2123

2224
namespace {
2325
typedef std::tuple<
@@ -393,4 +395,15 @@ TEST(TensorTest, smoke_canShareTensorIfModelsFromDifferentCores) {
393395
OV_ASSERT_NO_THROW(request1.infer());
394396
OV_ASSERT_NO_THROW(request2.infer());
395397
}
398+
399+
TEST(CoreTest, smoke_singletonOclContext) {
400+
auto core1 = ov::Core();
401+
auto ctx1 = core1.get_default_context("GPU");
402+
auto& oclContext1 = static_cast<ov::intel_gpu::ocl::ClContext&>(ctx1);
403+
auto core2 = ov::Core();
404+
auto ctx2 = core2.get_default_context("GPU");
405+
auto& oclContext2 = static_cast<ov::intel_gpu::ocl::ClContext&>(ctx2);
406+
ASSERT_EQ(oclContext1.get(), oclContext2.get());
407+
}
408+
396409
} // namespace

0 commit comments

Comments
 (0)