diff --git a/modules/core/src/parallel.cpp b/modules/core/src/parallel.cpp index e1d591068b..fe2c94a9bb 100644 --- a/modules/core/src/parallel.cpp +++ b/modules/core/src/parallel.cpp @@ -454,7 +454,41 @@ namespace { static inline void pplParallelFor(int first, int last, const Body& body) { #if defined(_M_ARM64) || defined(_M_ARM64EC) - Concurrency::parallel_for(first, last, body, Concurrency::static_partitioner()); + // static_partitioner just spawns one task per worker; the atomic loop rebalances. + if (first >= last) + return; + + const int total = last - first; + // cv::getNumThreads() returns 0 for WINRT, which also reaches this function. + const unsigned vps = Concurrency::CurrentScheduler::Get()->GetNumberOfVirtualProcessors(); + const unsigned workers = std::min(std::max(1u, vps), (unsigned)total); + + // total<=workers means one unit each, so skip the contended claim loop below. + if (total <= (int)workers) + { + Concurrency::parallel_for(first, last, body, Concurrency::static_partitioner()); + return; + } + + const int remaining_multiplier = std::min(total, + (int)std::max(std::min(100u, workers * 4), workers * 2)); // experimental value + + std::atomic current_task(0); + const auto worker = [&](int) + { + for (;;) + { + int chunk_size = std::max(1, (total - current_task) / remaining_multiplier); + int id = current_task.fetch_add(chunk_size, std::memory_order_relaxed); + if (id >= total) + break; + int end_id = std::min(total, id + chunk_size); + for (int i = first + id; i < first + end_id; ++i) + body(i); + } + }; + Concurrency::parallel_for(0, (int)workers, worker, Concurrency::static_partitioner()); + CV_Assert(current_task >= total); #else Concurrency::parallel_for(first, last, body); #endif diff --git a/modules/core/test/test_misc.cpp b/modules/core/test/test_misc.cpp index e3f915dd44..fb94dddaac 100644 --- a/modules/core/test/test_misc.cpp +++ b/modules/core/test/test_misc.cpp @@ -441,6 +441,34 @@ TEST(Core_Parallel, propagate_fp_denormals_ignore_hint) } } +class CountingParallelLoopBody : public cv::ParallelLoopBody +{ +public: + explicit CountingParallelLoopBody(std::vector& hits) : hits_(hits) {} + void operator()(const cv::Range& r) const CV_OVERRIDE + { + for (int i = r.start; i < r.end; i++) + CV_XADD(&hits_[i], 1); + } + std::vector& hits_; +}; + +typedef testing::TestWithParam Core_Parallel_IndexCoverage; + +TEST_P(Core_Parallel_IndexCoverage, every_index_exactly_once) +{ + const int total = GetParam(); + std::vector hits(total, 0); + parallel_for_(cv::Range(0, total), CountingParallelLoopBody(hits)); + for (int i = 0; i < total; i++) + ASSERT_EQ(1, hits[i]) << "index " << i << " of " << total; +} + +// Brackets pplParallelFor's total<=workers fast path, and goes well past it to also +// exercise the shrinking-chunk claim loop. +INSTANTIATE_TEST_CASE_P(/**/, Core_Parallel_IndexCoverage, + testing::Values(1, 2, 4, 8, 16, 17, 32, 64, 100, 300, 100000)); + TEST(Core_Version, consistency) { // this test verifies that OpenCV version loaded in runtime