Ask Your Question

How to enable vectorization in OpenCV?

asked 2012-07-24 02:46:32 -0600

Michael Burdinov gravatar image

updated 2020-11-30 00:58:49 -0600

I have 2 questions:

  1. I want the OpenCV to use vectorization whenever possible. How can i do that? (I don't have IPP).

  2. If I enable TBB option in OpenCV build, are resulting programs will try to create multiple threads by default? Is there a way to control it (sometimes the program must run in single thread, and sometimes it can use more)?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-07-24 02:53:13 -0600

sammy gravatar image

updated 2012-07-24 03:05:14 -0600

There are multiple ways to use vectorization/parallelization in OpenCV.

However, they only activate code that was written for TBB/SSE, etc. It will not parallelize generic code.

There are multiple flags to control vectorization, which you can set/unset in CMake:

  • WITH_TBB (Off by deafault) - activates multithread processing
  • CV_ENABLE_IPP (Off by default) - activates IPP library. Because many OpenCV functions are already optimized with SSE, the gains of IPP are low now. It used to be a big boost in the old times, when OpenCV code was only C-based.
  • CV_SSE2 (On by default) - similar in functionaliry and results with IPP, just that it's opensource. Many building blocks in OpenCV are optimized manually with SSE.
  • CV_ENABLE_UNROLLED (?? default) - Experimental feature, that activates some 4- or 8- unroll loops. It theoretically helps compiler optimize the code, and improves memory access. However, the coverage of this feature in code is still very limited.

If enabled, TBB will start by default a number of threads equal to the number of processor cores in your PC. You can view/set the number of threads with the following functions:

edit flag offensive delete link more


I see. Thank you

Michael Burdinov gravatar imageMichael Burdinov ( 2012-07-24 03:09:21 -0600 )edit

answered 2012-07-24 02:57:23 -0600

Kirill Kornyakov gravatar image
  1. SSE optimization is enabled by default in x86 builds. So, you shouldn't do anything, in fact it is not so easy to disable the SSE code in the OpenCV ;-)
  2. With WITH_TBB=ON OpenCV tries to use several threads for some functions. The problem is that just a handsome of function are threaded with TBB at the moment (may be a dozen). So, it is hard to see any speedup. OpenCV philosophy here is that application should be multi-threaded, not OpenCV functions. But some time ago we started to use TBB even for primitive functions, because we need faster processing on mobile architectures. So, let's wait some time I and hopefully you'll get visible speedup with TBB enabled.

setNumThreads is used to control the number of threads. But I have to check if it works for TBB...

edit flag offensive delete link more


Thank you for the answer. It raised another question. I thought that setNumThreads designed for use with TBB. Where else it can be used and why do you have any doubts about using it with TBB?

Michael Burdinov gravatar imageMichael Burdinov ( 2012-07-24 03:12:47 -0600 )edit

Simply because it was broken once, and I'm not sure that it was fixed. But 95% that it works now.

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-07-24 03:34:09 -0600 )edit

Question Tools


Asked: 2012-07-24 02:46:32 -0600

Seen: 6,550 times

Last updated: Jul 24 '12