Improve GNA MT sychronization (#2553)
authorKrzysztof Bruniecki <krzysztof.bruniecki@intel.com>
Mon, 19 Oct 2020 09:21:01 +0000 (11:21 +0200)
committerGitHub <noreply@github.com>
Mon, 19 Oct 2020 09:21:01 +0000 (12:21 +0300)
* Sync GNA lib calls to avoid multi threads and plugins crash

* Remove TODO

* Enable sync for GNA1

* Fix GNA1 sync

* Add core_threading_tests to GNA Plugin to address story 31709

* Disable and change test description

inference-engine/src/gna_plugin/gna_device.cpp
inference-engine/src/gna_plugin/gna_device.hpp
inference-engine/tests/functional/inference_engine/core_threading_tests.cpp
inference-engine/tests/functional/plugin/gna/shared_tests_instances/behavior/core_threading_tests.cpp [new file with mode: 0644]

index 97d4026..ce15f2b 100644 (file)
@@ -7,6 +7,7 @@
 #include <map>
 #include <string>
 #include <cstring>
+#include <mutex>
 #include <vector>
 
 #if GNA_LIB_VER == 2
@@ -24,6 +25,8 @@
 #include "details/ie_exception.hpp"
 #include "gna_plugin_log.hpp"
 
+std::mutex GNADeviceHelper::acrossPluginsSync{};
+
 uint8_t* GNADeviceHelper::alloc(uint32_t size_requested, uint32_t *size_granted) {
     void * memPtr = nullptr;
 #if GNA_LIB_VER == 1
@@ -62,6 +65,7 @@ uint32_t GNADeviceHelper::propagate(const intel_nnet_type_t *pNeuralNetwork,
     return reqId;
 }
 #else
+
 void GNADeviceHelper::setUpActiveList(const uint32_t requestConfigId, uint32_t layerIndex, uint32_t* ptr_active_indices, uint32_t num_active_indices) {
     const auto status = Gna2RequestConfigEnableActiveList(requestConfigId, layerIndex, num_active_indices, ptr_active_indices);
     checkGna2Status(status);
@@ -363,6 +367,7 @@ void GNADeviceHelper::checkStatus() const {
 #endif
 
 void GNADeviceHelper::open(uint8_t n_threads) {
+    std::unique_lock<std::mutex> lockGnaCalls{ acrossPluginsSync };
 #if GNA_LIB_VER == 1
     nGNAHandle = GNADeviceOpenSetThreads(&nGNAStatus, n_threads);
     checkStatus();
@@ -379,6 +384,7 @@ void GNADeviceHelper::open(uint8_t n_threads) {
 }
 
 void GNADeviceHelper::close() {
+    std::unique_lock<std::mutex> lockGnaCalls{ acrossPluginsSync };
 #if GNA_LIB_VER == 1
     GNADeviceClose(nGNAHandle);
     nGNAHandle = 0;
@@ -398,6 +404,7 @@ void GNADeviceHelper::close() {
 }
 
 void GNADeviceHelper::setOMPThreads(uint8_t const n_threads) {
+    std::unique_lock<std::mutex> lockGnaCalls{ acrossPluginsSync };
 #if GNA_LIB_VER == 1
     gmmSetThreads(n_threads);
 #else
index 7b35f3c..0f71772 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <cstdint>
 #include <memory>
+#include <mutex>
 #include <string>
 #include <map>
 #include <vector>
@@ -37,6 +38,7 @@ enum GnaWaitStatus : int {
  * holds gna - style handle in RAII way
  */
 class GNADeviceHelper {
+    static std::mutex acrossPluginsSync;
 #if GNA_LIB_VER == 1
     intel_gna_status_t nGNAStatus = GNA_NOERROR;
     intel_gna_handle_t nGNAHandle = 0;
@@ -168,6 +170,7 @@ public:
     void setOMPThreads(uint8_t const n_threads);
 
     void initGnaPerfCounters() {
+        std::unique_lock<std::mutex> lockGnaCalls{ acrossPluginsSync };
 #if GNA_LIB_VER == 1
         nGNAPerfResults = {{0, 0, 0, 0, 0, 0, 0}, {0, 0}, {0, 0, 0}, {0, 0}};
         nGNAPerfResultsTotal = {{0, 0, 0, 0, 0, 0, 0}, {0, 0}, {0, 0, 0}, {0, 0}};
index 576f112..2271ddd 100644 (file)
@@ -117,7 +117,7 @@ TEST_F(CoreThreadingTests, RegisterPlugins) {
 }
 
 // tested function: GetAvailableDevices, UnregisterPlugin
-// TODO: some plugins initialization (e.g. GNA) failed during such stress-test scenario
+// TODO: some initialization (e.g. thread/dlopen) sporadically fails during such stress-test scenario
 TEST_F(CoreThreadingTests, DISABLED_GetAvailableDevices) {
     InferenceEngine::Core ie;
     runParallel([&] () {
diff --git a/inference-engine/tests/functional/plugin/gna/shared_tests_instances/behavior/core_threading_tests.cpp b/inference-engine/tests/functional/plugin/gna/shared_tests_instances/behavior/core_threading_tests.cpp
new file mode 100644 (file)
index 0000000..139bd9f
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright (C) 2020 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <behavior/core_threading_tests.hpp>
+
+namespace {
+
+Params params[] = {
+    std::tuple<Device, Config>{ CommonTestUtils::DEVICE_GNA, {{ CONFIG_KEY(PERF_COUNT), CONFIG_VALUE(YES) }}},
+    std::tuple<Device, Config>{ CommonTestUtils::DEVICE_HETERO, {{ "TARGET_FALLBACK", CommonTestUtils::DEVICE_GNA }}},
+    std::tuple<Device, Config>{ CommonTestUtils::DEVICE_MULTI, {{ MULTI_CONFIG_KEY(DEVICE_PRIORITIES), CommonTestUtils::DEVICE_GNA }}},
+};
+
+}  // namespace
+
+INSTANTIATE_TEST_CASE_P(GNA, CoreThreadingTests, testing::ValuesIn(params), CoreThreadingTests::getTestCaseName);
+
+INSTANTIATE_TEST_CASE_P(DISABLED_GNA, CoreThreadingTestsWithIterations,
+    testing::Combine(testing::ValuesIn(params),
+                     testing::Values(2),
+                     testing::Values(2)),
+    CoreThreadingTestsWithIterations::getTestCaseName);