How to disable parallelism in OpenCV?

China☆狼群 提交于 2020-01-24 10:56:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!