4

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?

justHelloWorld
  • 6,478
  • 8
  • 58
  • 138
  • 1
    It may be using `Intel TBB` - check your config... `getBuildInformation()` – Mark Setchell Apr 05 '17 at 16:39
  • 1
    @MarkSetchell `cv::setNumThreads` should deal with that (as described [here](http://stackoverflow.com/questions/18478159/is-there-a-way-to-disable-tbb-in-opencv) ) – justHelloWorld Apr 05 '17 at 16:42
  • 1
    Do any SO folk know if any OpenCV folk/developers participate on SO and can answer in-depth questions such as this? It has always been a mystery to me as to how to know what optimisations (SIMD, OpenMP, TBB, CUDA) are actually being used in any OpenCV function.... or where anyone tells us typical performance numbers for all the OpenCV functions on any particular hardware? Anyone? – Mark Setchell Apr 05 '17 at 20:25
  • I think that the only way in this case is to rebuild the whole OpenCV with all parallel options disabled and ipp too. I'm sorry to say this, but if that's the case...well, what a pain in the ass. – justHelloWorld Apr 05 '17 at 20:28
  • @MarkSetchell I think it all depends on how you compiled opencv. There are different options in the CMake file for the build which control all that. – nnrales Feb 07 '18 at 22:51

4 Answers4

5

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
Devymex
  • 446
  • 3
  • 17
2

You can disable it with:

cv::setNumThreads(0);

OpenCV will try to set the number of threads for the next parallel region.

If threads == 0, OpenCV will disable threading optimizations and run all it's functions sequentially. Passing threads < 0 will reset threads number to system default. This function must be called outside of parallel region.

https://docs.opencv.org/4.5.1/db/de0/group__core__utils.html

y30
  • 722
  • 7
  • 17
0

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

Sergio
  • 1
0

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.

nnrales
  • 1,481
  • 1
  • 17
  • 26