Is there a way to replace parallel_for of tbb

Hello,
I tried running a project got from github, in which parallel_for is involved to accelerate the program. It seems the version of OpenCV for that project is about 4.0. I am using a 3.4 without tbb. In this case, I wanna find a way to solve the non-existing problem of tbb/tbb.h which includes parallel_for. How can I get this? Is downloading a tbb and use it as an independent library possible or there is other solution replaces the parallel_for. Thanks in advance.

  • download tbb
  • (you probably have to build that first, locally)
  • rerun cmake(-gui) for opencv, make sure it finds your tbb install
    (maybe add pathes manually)
  • rebuild opencv libs

btw, there *are* plans to add "dynamic hotswapping" parallelization engines, but it's not really ready yet, pity ;(
1 Like

@berak. Thanks for reply.

I am working on tbb compilation. Is it possible to compile it and use it as a 3rd party as OpenCV in the project. I really don’t want to build OpenCV again with tbb.

which os / compiler ?
how did you manage to build it without any parallel framework ?
(it should use pthreads on linux, internal on win by default)

please check cv::getBuildInformation()

if you want tbb support, you’ll have to, no way around it (for now)
on the other hand, if you already have some threading support built in, it’s probably not worth the change.

1 Like

get it. Thanks for reply.

On the other hand in order to use TBB in your code, you don’t need OpenCV with TBB support.

Just install TBB, and add the include folder and lib files to the project. (And copy the DLL files near the executable if working in windows)

gcc main.cpp -o main -I/usr/local/include/opencv4 -lopencv_core -lopencv_highgui -ltbb
2 Likes

OpenCV: How to use the OpenCV parallel_for_ to parallelize your code :

The first precondition is to have OpenCV built with a parallel framework. In OpenCV 3.2, the following parallel frameworks are available in that order:

  1. Intel Threading Building Blocks (3rdparty library, should be explicitly enabled)
  2. C= Parallel C/C++ Programming Language Extension (3rdparty library, should be explicitly enabled)
  3. OpenMP (integrated to compiler, should be explicitly enabled)
  4. APPLE GCD (system wide, used automatically (APPLE only))
  5. Windows RT concurrency (system wide, used automatically (Windows RT only))
  6. Windows concurrency (part of runtime, used automatically (Windows only - MSVC++ >= 10))
  7. Pthreads (if available)

most compilers support OpenMP. that includes GCC, LLVM, MSVC.

in other words, you don’t necessarily need TBB to get cv::parallel_for_

note, that “parallel for” is an OpenCV construct using any of those parallel frameworks. that’s different from TBB’s parallel for, but probably suitable for the same purposes.

so if you don’t like setting TBB up, you could rip all TBB stuff out the code you have.

2 Likes

@kbarni
Hahaha :grinning:. Thanks 4 sharing this good experience.

Thank you very much, @crackwitz. Every answer you gave really helps.