forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_threading.cpp
161 lines (137 loc) · 5.21 KB
/
core_threading.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <ie_core.hpp>
#include <ie_plugin_config.hpp>
#include <ie_extension.h>
#include "openvino/util/file_util.hpp"
#include <ngraph_functions/subgraph_builders.hpp>
#include <functional_test_utils/test_model/test_model.hpp>
#include <common_test_utils/file_utils.hpp>
#include <common_test_utils/test_assertions.hpp>
#include <gtest/gtest.h>
#include <thread>
#include <atomic>
#include <mutex>
#include <chrono>
#include <fstream>
class CoreThreadingTests : public ::testing::Test {
protected:
std::string modelName = "CoreThreadingTests.xml", weightsName = "CoreThreadingTests.bin";
public:
void SetUp() override {
FuncTestUtils::TestModel::generateTestModel(modelName, weightsName);
}
void TearDown() override {
CommonTestUtils::removeIRFiles(modelName, weightsName);
}
void runParallel(std::function<void(void)> func,
const unsigned int iterations = 100,
const unsigned int threadsNum = 8) {
std::vector<std::thread> threads(threadsNum);
for (auto & thread : threads) {
thread = std::thread([&](){
for (unsigned int i = 0; i < iterations; ++i) {
func();
}
});
}
for (auto & thread : threads) {
if (thread.joinable())
thread.join();
}
}
void safeAddExtension(InferenceEngine::Core & ie) {
try {
auto extension = std::make_shared<InferenceEngine::Extension>(
ov::util::make_plugin_library_name(CommonTestUtils::getExecutableDirectory(),
std::string("template_extension") + IE_BUILD_POSTFIX));
ie.AddExtension(extension);
} catch (const InferenceEngine::Exception & ex) {
ASSERT_STR_CONTAINS(ex.what(), "name: custom_opset. Opset");
}
}
};
// tested function: SetConfig
TEST_F(CoreThreadingTests, SetConfigPluginDoesNotExist) {
InferenceEngine::Core ie;
std::map<std::string, std::string> localConfig = {
{ CONFIG_KEY(PERF_COUNT), InferenceEngine::PluginConfigParams::YES }
};
runParallel([&] () {
ie.SetConfig(localConfig);
}, 10000);
}
// TODO: CVS-68982
#ifndef OPENVINO_STATIC_LIBRARY
// tested function: RegisterPlugin
TEST_F(CoreThreadingTests, RegisterPlugin) {
InferenceEngine::Core ie;
std::atomic<int> index{0};
runParallel([&] () {
const std::string deviceName = std::to_string(index++);
ie.RegisterPlugin(ov::util::make_plugin_library_name(CommonTestUtils::getExecutableDirectory(),
std::string("mock_engine") + IE_BUILD_POSTFIX), deviceName);
ie.GetVersions(deviceName);
ie.UnregisterPlugin(deviceName);
}, 4000);
}
// tested function: RegisterPlugins
TEST_F(CoreThreadingTests, RegisterPlugins) {
InferenceEngine::Core ie;
std::atomic<unsigned int> index{0};
auto getPluginXml = [&] () -> std::tuple<std::string, std::string> {
std::string indexStr = std::to_string(index++);
std::string pluginsXML = "test_plugins" + indexStr + ".xml";
std::ofstream file(pluginsXML);
file << "<ie><plugins><plugin location=\"";
file << CommonTestUtils::getExecutableDirectory();
file << ov::util::FileTraits<char>::file_separator;
file << ov::util::FileTraits<char>::library_prefix();
file << "mock_engine";
file << IE_BUILD_POSTFIX;
file << ov::util::FileTraits<char>::dot_symbol;
file << ov::util::FileTraits<char>::library_ext();
file << "\" name=\"";
file << indexStr;
file << "\"></plugin></plugins></ie>";
file.flush();
file.close();
return std::tie(pluginsXML, indexStr);
};
runParallel([&] () {
std::string fileName, deviceName;
std::tie(fileName, deviceName) = getPluginXml();
ie.RegisterPlugins(fileName);
ie.GetVersions(deviceName);
ASSERT_EQ(0, std::remove(fileName.c_str()));
}, 1000);
}
#endif // !OPENVINO_STATIC_LIBRARY
// tested function: GetAvailableDevices, UnregisterPlugin
// TODO: some initialization (e.g. thread/dlopen) sporadically fails during such stress-test scenario
TEST_F(CoreThreadingTests, DISABLED_GetAvailableDevices) {
InferenceEngine::Core ie;
runParallel([&] () {
std::vector<std::string> devices = ie.GetAvailableDevices();
// unregister all the devices
for (auto && deviceName : devices) {
try {
ie.UnregisterPlugin(deviceName);
} catch (const InferenceEngine::Exception & ex) {
// if several threads unload plugin at once, the first thread does this
// while all others will throw an exception that plugin is not registered
ASSERT_STR_CONTAINS(ex.what(), "name is not registered in the");
}
}
}, 30);
}
// tested function: ReadNetwork, AddExtension
TEST_F(CoreThreadingTests, ReadNetwork) {
InferenceEngine::Core ie;
auto network = ie.ReadNetwork(modelName, weightsName);
runParallel([&] () {
safeAddExtension(ie);
(void)ie.ReadNetwork(modelName, weightsName);
}, 100, 12);
}