问题
I've built OpenCV using Intel IPP, so I suppose that whenever possible it's used (e.g. matrix multiplication).
I want to test the scalability of my parallel application by comparing it with a serial version. In order to do so, when it I do:
omp_set_num_threads(1);
cv::setNumThreads(1);
However, by monitoring the CPU usage I see that multiple CPUs are still used. Why is that? And how can I force the program execution by using just one CPU?
回答1:
Re-building OpenCV from source with following CMake parameters should works:
cmake .. -DWITH_IPP=OFF -DWITH_TBB=OFF -DWITH_OPENMP=OFF -DWITH_PTHREADS_PF=OFF
and you will find the macro CV_PARALLEL_FRAMEWORK is not defined to something in modules/core/src/parallel.cpp any more:
#if defined HAVE_TBB
# define CV_PARALLEL_FRAMEWORK "tbb"
#elif defined HAVE_HPX
# define CV_PARALLEL_FRAMEWORK "hpx"
#elif defined HAVE_OPENMP
# define CV_PARALLEL_FRAMEWORK "openmp"
#elif defined HAVE_GCD
# define CV_PARALLEL_FRAMEWORK "gcd"
#elif defined WINRT
# define CV_PARALLEL_FRAMEWORK "winrt-concurrency"
#elif defined HAVE_CONCURRENCY
# define CV_PARALLEL_FRAMEWORK "ms-concurrency"
#elif defined HAVE_PTHREADS_PF
# define CV_PARALLEL_FRAMEWORK "pthreads"
#endif
回答2:
add ippSetNumThreads(1);
before the first IPP call in your code. This should set number of OpenMP threads in IPP to 1. More info can be found here in the "Controlling OpenMP Threading in the Intel IPP Primitives" section
回答3:
Are you using opencv from multi threads ? You have to disable opencv's multi threading from each thread, atleast in my experience with it.
Opencv' parallel_for functions creates multiple threads to distribute the work across.
来源:https://stackoverflow.com/questions/43235820/how-to-disable-parallelism-in-opencv