Tuesday, July 26, 2011

Parallelizing Loops with OpenMP

According to OpenCV Release Notes, use of OpenMP is no longer in active support since OpenCV 2.1. They have been replaced by Thread Building Blocks (TBB).

OpenMP relies on #pragma directives. Telling compiler to parallelize loops / code blocks. Change of existing code is small compare to other methods.

_OPENMP will be defined by compiler that supports OpenMP.

Searching _OPENMP from source code discover current 'leftover' implementations here:
  • selfsimilarity.cpp (disabled with #if 0 block)
  • spinimages.cpp ( parallel for in computeSpinImages() )
  • stardetector.cpp ( parallel for in icvStarDetectorComputeResponses() )
  • system.cpp (getNumThreads(),...)
    • cvboost.cpp ( parallel private in cvCreateMTStumpClassifier() ) 
    • cvhaartraining ( uses CV_OPENMP instead of _OPENMP. Moreover, it's only enabled with MSVC and ICC compilers, not GCC )
    • blobtrackanalysisior.cpp: parallel CvBlobTrackAnalysisIOR:Process()
    • blobtrackingmsfg.cpp: parallel UpdateWeightsMS(), UpdateWeightsCC()

    Experiment with haartraining
    1. Used Linux Build too take advantage of GCC 4 OpenMP support. There are OpenMP options in MSVC 2010 Express. But Microsoft webpage states there is no support. (http://msdn.microsoft.com/en-us/library/tt15eb9t.aspx)
    2. Recovered ENABLE_OPENMP in OpenCV 2.2 CMakeFiles by un-commenting such occurrences.
    3. Used CMake-GUI to configure build with ENABLE_OPENMP turned on.
    4. Called CvGetNumThreads() in haartraining.cpp to see if both CPU cores are available for use.
    • Took half the time to perform the same training. The results are basically the same. Noticeable differences are the node-split threshold values differs from the 5th decimal points on. 
    • cvhaartraining.cpp uses a variant of the OPENMP define - CV_OPENMP. Training terminated with SegFault with that turned on.


    1. so, in linux command instead of using opencv-haartraining -data data/cascade -vec data/muestra.vec
      -bg negativas.txt -nstages 30 -nsplit 2 -minhitrate 0.999
      -maxfalsealarm 0.5 -npos 1500 -nneg 5000 -w 20 -h 20 -mem 1300
      -mode ALL
      command i should use the "opencv cvhaartraining" command if exist?

    2. Hi Christian, the executable for haartraining is opencv_haartraining on Linux build. Use the same command for OpenMP build. The cvhaartraining.cpp is located in the haartraining module. And there is no need to change it.