Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[CPU][OMP] Fix for functional tests (#26801)

Nikolay Shchegolev285c352329f1
diff --git a/src/core/include/openvino/core/parallel.hpp b/src/core/include/openvino/core/parallel.hpp
index a4a810bf031..6d3a243c95e 100644
--- a/src/core/include/openvino/core/parallel.hpp
+++ b/src/core/include/openvino/core/parallel.hpp
@@ -111,6 +111,22 @@ inline int parallel_get_env_threads() {
}
return env_cores;
}
+inline int get_max_nested_levels() {
+# if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
+ return omp_get_nested();
+# else
+ return omp_get_max_active_levels();
+# endif // defined(_MSC_VER) && !defined(__INTEL_COMPILER)
+}
+// Controls the number of nested parallel blocks.
+// This flag has higher priority than pragma num_threads.
+inline void set_max_nested_levels(int levels) {
+# if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
+ return omp_set_nested(levels);
+# else
+ return omp_set_max_active_levels(levels);
+# endif // defined(_MSC_VER) && !defined(__INTEL_COMPILER)
+}
#elif OV_THREAD == OV_THREAD_SEQ
# include <algorithm>
@@ -151,9 +167,16 @@ void parallel_nt(int nthr, const F& func) {
func(0, 1);
return;
}
-
+ // We expect the number of threads here to be "nthr", so we need to disable dynamic behavior.
+ auto origin_dyn_val = omp_get_dynamic();
+ if (origin_dyn_val != 0) {
+ omp_set_dynamic(0);
+ }
# pragma omp parallel num_threads(nthr)
- func(parallel_get_thread_num(), parallel_get_num_threads());
+ { func(parallel_get_thread_num(), parallel_get_num_threads()); }
+ if (origin_dyn_val != 0) {
+ omp_set_dynamic(origin_dyn_val);
+ }
#elif OV_THREAD == OV_THREAD_SEQ
func(0, 1);
#endif
@@ -185,8 +208,16 @@ void parallel_nt_static(int nthr, const F& func) {
#elif OV_THREAD == OV_THREAD_OMP
+ // We expect the number of threads here to be "nthr", so we need to disable dynamic behavior.
+ auto origin_dyn_val = omp_get_dynamic();
+ if (origin_dyn_val != 0) {
+ omp_set_dynamic(0);
+ }
# pragma omp parallel num_threads(nthr)
{ func(parallel_get_thread_num(), parallel_get_num_threads()); }
+ if (origin_dyn_val != 0) {
+ omp_set_dynamic(origin_dyn_val);
+ }
#endif
}
diff --git a/src/plugins/intel_cpu/src/graph.cpp b/src/plugins/intel_cpu/src/graph.cpp
index 660f9e60694..a17b8d28e17 100644
--- a/src/plugins/intel_cpu/src/graph.cpp
+++ b/src/plugins/intel_cpu/src/graph.cpp
@@ -1278,6 +1278,14 @@ public:
m_completion.store(false);
auto startCounter = m_prepareCounter.load();
+ // Allow nested parallel execution.
+ // Some nodes use parallelism inside function updateDynParams, but OMP has one nested level here,
+ // so nested routines can only be executed in single thread.
+ auto origin_nested_levels = get_max_nested_levels();
+ if (origin_nested_levels < 2) {
+ set_max_nested_levels(2);
+ }
+
#pragma omp parallel
#pragma omp sections
{
@@ -1290,6 +1298,10 @@ public:
updateShapes(startCounter, stopIndx);
}
}
+
+ if (origin_nested_levels != 2) {
+ set_max_nested_levels(origin_nested_levels);
+ }
}
};
#endif
diff --git a/src/plugins/intel_cpu/src/nodes/interpolate.cpp b/src/plugins/intel_cpu/src/nodes/interpolate.cpp
index 19a19a36840..ee6afa33827 100644
--- a/src/plugins/intel_cpu/src/nodes/interpolate.cpp
+++ b/src/plugins/intel_cpu/src/nodes/interpolate.cpp
@@ -3777,7 +3777,7 @@ void Interpolate::InterpolateExecutorBase::create_pillow_working_buf(Interpolate
if (srcDimPad5d[3] == dstDim5d[3] || srcDimPad5d[4] == dstDim5d[4])
return;
size_t bufSize = srcDimPad5d[3] * dstDim5d[4] * srcDataSize; // IH * OW
- size_t threadsNum = parallel_get_num_threads();
+ size_t threadsNum = parallel_get_max_threads();
if (layout == InterpolateLayoutType::planar) {
// B and C execute in parallel, need separate buf
size_t parallelNum = srcDimPad5d[0] * srcDimPad5d[1];
diff --git a/src/plugins/intel_cpu/src/nodes/mvn.cpp b/src/plugins/intel_cpu/src/nodes/mvn.cpp
index 58d5252c071..61aa4738b8f 100644
--- a/src/plugins/intel_cpu/src/nodes/mvn.cpp
+++ b/src/plugins/intel_cpu/src/nodes/mvn.cpp
@@ -2417,7 +2417,7 @@ void MVN::MVNJitExecutor::mvn_nspc(const uint8_t* src_data, uint8_t* dst_data, c
const size_t H = shape5d[3];
const size_t W = shape5d[4];
- size_t threads_num = parallel_get_num_threads();
+ size_t threads_num = parallel_get_max_threads();
size_t aux_buffer_size = mvnAttrs.execAcrossChannels_ ? 1 : rnd_up(C, blk_size) + blk_size;
parallel_for(N, [&](size_t b) {
std::vector<float> mean_buffer(aux_buffer_size * threads_num, 0.f);
@@ -2537,7 +2537,7 @@ void MVN::MVNJitExecutor::mvn_blk(const uint8_t* src_data, uint8_t* dst_data, co
size_t C3 = C2 * CB;
size_t C5 = C * D * H * W;
- size_t threads_num = parallel_get_num_threads();
+ size_t threads_num = parallel_get_max_threads();
size_t aux_buffer_size = mvnAttrs.execAcrossChannels_ ? blk_size : rnd_up(C, blk_size);
aux_buffer_size += blk_size;
std::vector<float> mean_buffer(aux_buffer_size * threads_num);
diff --git a/src/plugins/intel_cpu/src/nodes/non_zero.cpp b/src/plugins/intel_cpu/src/nodes/non_zero.cpp
index 965b446f8dd..f897c6ef7bf 100644
--- a/src/plugins/intel_cpu/src/nodes/non_zero.cpp
+++ b/src/plugins/intel_cpu/src/nodes/non_zero.cpp
@@ -84,7 +84,7 @@ std::vector<size_t> NonZero::getNonZeroElementsCount(const T* src, const Shape&
break;
}
default: {
- threadsCount = parallel_get_num_threads();
+ threadsCount = parallel_get_max_threads();
if (inSize < static_cast<size_t>(blockSize * threadsCount))
threadsCount = 1;
diff --git a/src/plugins/intel_cpu/src/nodes/roi_align.cpp b/src/plugins/intel_cpu/src/nodes/roi_align.cpp
index 7857a8c569f..eb1797279e1 100644
--- a/src/plugins/intel_cpu/src/nodes/roi_align.cpp
+++ b/src/plugins/intel_cpu/src/nodes/roi_align.cpp
@@ -1074,7 +1074,7 @@ void ROIAlign::executeSpecified() {
if (!isPlainFmt) {
std::vector<float> workingBuf;
int bufSize = rnd_up(C, 16);
- size_t threadsNum = parallel_get_num_threads();
+ size_t threadsNum = parallel_get_max_threads();
workingBuf.resize(bufSize * threadsNum, 0.f);
parallel_for3d(realRois, pooledH, pooledW, [&](int n, int yBinInd, int xBinInd) {
int numSamplesROI = numSamples[n];