1

I got a question concerning OpenCV and OpenMP.

I used to parallelize my code using OpenMP, since I have to do some heavy computing on large images. Up to now (I did not use OpenCV so far) the parallelization worked fine for me.

Then I needed some functionality of the OpenCV library. Since I incorporated OCV in my code, the parallelization no longer works. Allthough I am NOT parallelizing any OCV functions. Moreover, even if I only link in the OCV libraries (core, imgproc, imgcodecs), parallelization does not work anymore.

So, how can that be? And how can I enable the parallelization (again)?

I use OpenCV 4.1 on a OpenSuSE 15.1 system. IDE is Code::Blocks. All libraries where installed from official repos (meaning: I have no clou how to work with cmake...).

Could anybody give me a hint, how to fix this?

Thank you

Phtagen

Phtagen
  • 121
  • 10
  • Maybe related: https://stackoverflow.com/questions/29494503/how-to-compile-opencv-with-openmp – Bob__ Jan 15 '21 at 20:51
  • Thank you. Already found that. But that's why I wrote, that I don't have a clue how to work with cmake. I guess there is maybe a way to set a compiler flag in the IDE? – Phtagen Jan 15 '21 at 21:05
  • I am not familiar with OpenSuSE but I assume you installed OpenCV with zypper (equivalent of apt-get). To enable OpenMP, you need to compile OpenCV from the source code yourself. What I want to ask is that do you need OpenMP with OpenCV? Because OpenCV is a highly optimized library and its functions are vectorized. – Burak Jan 15 '21 at 21:28
  • To enable OpenMP for your own code via Cmake, you might want to look at: https://stackoverflow.com/q/12399422/179910 (but it's not clear whether you want to mess with CMake stuff or not). When building OpenCV, there's already a flag `WITH_OPENMP` that you set to ON to enable OpenMP. – Jerry Coffin Jan 15 '21 at 21:30
  • I installed with Yast, which is a graphical package manager. Anyway, I am absolutely unexperienced concerning Cmake. As I wrote, I am not using OpenCV WITH OpenMP. I use OpenCV NEXT TO OpenMP, so my parallelizations are not related to any stuff I am doing with OpenCV. And yes of course, I would mess with Cmake, but I don't know how to do... – Phtagen Jan 15 '21 at 23:49
  • how do you see/recognize that OpenMP isnt working anymore in your own code? – Micka Jan 16 '21 at 10:15
  • 1
    So are you parallelizing pixel access? For example a line like `img.at(y, x) = 0`. If so, then OpenMP should work just fine. I've used this many times. It would be good if you shared code and how you timed the performance and the results of that timing. – Warpstar22 Jan 19 '21 at 01:15
  • 1
    @Micka:I see that OpenMP is not working, because I run KSsysGuard, which is SuSEs task manager. My old code (without OpenCV) ran on all kernels. The complete program took roughly 2,5 h hours running time. The version with OpenCV enabled only runs on one kernel, the complete program takes roughly 13 hours running time. – Phtagen Jan 20 '21 at 10:50
  • @Warpstar22: Yes I am accessing pixels this way but not in parallel. My parallelization runs on other functions I wrote, which are not using OpenCV at all. In these functions I am using Vigra, which is another computer vision library. – Phtagen Jan 20 '21 at 10:50
  • you could test #ifdef _OPENMP and omp_get_thread_num() states, maybe opencv changes conditions or settings – Micka Jan 20 '21 at 10:55
  • I added #ifdef _OPENMP, but nothing changed. omp_get_thread_num() always returns 0. – Phtagen Jan 20 '21 at 11:26
  • Does using omp_set_num_threads() before your parallel code not do anything? – Warpstar22 Jan 20 '21 at 23:15
  • @Warpstar22 No, and interestingly omp_get_max_threads() returns 1 (it should return 8), – Phtagen Jan 21 '21 at 08:58

1 Answers1

1

This installation guide assumes that the OS is Ubuntu. I post it because it will probably help solving your problem.

Install OpenCV dependencies.

sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev libv4l-dev
sudo apt-get install libcanberra-gtk-module libcanberra-gtk3-module

Download the source code of the OpenCV version you like with some extra modules.

CV_DISTRO=3.4.0
git clone --branch $CV_DISTRO https://github.com/opencv/opencv.git opencv_$CV_DISTRO --depth 1
git clone --branch $CV_DISTRO https://github.com/opencv/opencv_contrib.git opencv_contrib_$CV_DISTRO --depth 1

Then build OpenCV.

cd opencv_$CV_DISTRO
mkdir -p build
cd build/
cmake -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib_$CV_DISTRO/modules \
    -D WITH_TBB=ON \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D WITH_OPENMP=ON ..
make -j7
sudo make install
sudo ldconfig

Feel free to edit this post.

Burak
  • 2,251
  • 1
  • 16
  • 33
  • Sorry for my late reply. Thanks for your solution. I am actually struggling to "translate" this from Ubuntu to OpenSuSE, yet, with no success... – Phtagen Jan 20 '21 at 10:42
  • You can give some details [in chat](https://chat.stackoverflow.com/rooms/227587/does-opencv-suppress-openmp) so that I may help you. – Burak Jan 20 '21 at 11:52
  • Cool, I didn't even know that a chat exists ^^ So, yes maybe we could do this. When would be the right time for you? – Phtagen Jan 20 '21 at 15:14
  • @Phtagen you can write anytime. – Burak Jan 20 '21 at 16:50
  • This solution perfectly worked... Thank you very much!!! – Phtagen Jan 21 '21 at 12:19