1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-28 23:03:03 +04:00

core: fix pthreads performance

OpenCV pthreads-based implementation changes:
- rework worker threads pool, allow to execute job by the main thread too
- rework synchronization scheme (wait for job completion, threads 'pong' answer is not required)
- allow "active wait" (spin) by worker threads and by the main thread
- use _mm_pause() during active wait (support for Hyper-Threading technology)
- use sched_yield() to avoid preemption of still working other workers
- don't use getTickCount()
- optional builtin thread pool profiler (disabled by compilation flag)
This commit is contained in:
Alexander Alekhin
2018-01-19 17:26:29 +03:00
committed by Alexander Alekhin
parent 47998c03f8
commit c49d5d5252
5 changed files with 814 additions and 589 deletions
+30 -8
View File
@@ -42,6 +42,7 @@
#include "precomp.hpp"
#include <opencv2/core/utils/configuration.private.hpp>
#include <opencv2/core/utils/trace.private.hpp>
#if defined _WIN32 || defined WINCE
@@ -125,19 +126,15 @@
# define CV_PARALLEL_FRAMEWORK "pthreads"
#endif
#include "parallel_impl.hpp"
using namespace cv;
namespace cv
{
ParallelLoopBody::~ParallelLoopBody() {}
#ifdef HAVE_PTHREADS_PF
void parallel_for_pthreads(const cv::Range& range, const cv::ParallelLoopBody& body, double nstripes);
size_t parallel_pthreads_get_threads_num();
void parallel_pthreads_set_threads_num(int num);
#endif
}
namespace
{
#ifdef CV_PARALLEL_FRAMEWORK
@@ -536,10 +533,35 @@ int cv::getNumThreads(void)
#endif
}
void cv::setNumThreads( int threads )
namespace cv {
unsigned defaultNumberOfThreads()
{
(void)threads;
#ifdef __ANDROID__
// many modern phones/tables have 4-core CPUs. Let's use no more
// than 2 threads by default not to overheat the devices
const unsigned int default_number_of_threads = 2;
#else
const unsigned int default_number_of_threads = (unsigned int)std::max(1, cv::getNumberOfCPUs());
#endif
unsigned result = default_number_of_threads;
static int config_num_threads = (int)utils::getConfigurationParameterSizeT("OPENCV_FOR_THREADS_NUM", 0);
if (config_num_threads)
{
result = (unsigned)std::max(1, config_num_threads);
//do we need upper limit of threads number?
}
return result;
}
}
void cv::setNumThreads( int threads_ )
{
(void)threads_;
#ifdef CV_PARALLEL_FRAMEWORK
int threads = (threads_ < 0) ? defaultNumberOfThreads() : (unsigned)threads_;
numThreads = threads;
#endif