5

I'm using OpenTLD (that uses OpenCV for image analysis) in a Raspberry Pi 2 project for object tracking. In order to combat lag issues (about 3 seconds of video lag) I enabled OpenMP support when I compiled. Now, I get only about 2 seconds of lag. Running top tells me that at most only ~170% CPU is being used by opentld, leading me to suspect that OpenMP is using only two of the Raspberry Pi 2's four cores.

From what I understand of this Wikipedia diagram, OpenMP should be able to utilize all four cores. Is this a matter of OpenMP not recognizing all the cores, or is it something else?

faeophyta
  • 323
  • 5
  • 16
  • Try to set some libgomp environment variables https://gcc.gnu.org/onlinedocs/libgomp/Environment-Variables.html - `export GOMP_DEBUG=1` for more debugging info (if it is implemented); and `export OMP_WAIT_POLICY=ACTIVE GOMP_CPU_AFFINITY=0-3` for more efficient locking and binding of OpenMP threads to cores. – osgx Mar 17 '15 at 15:10
  • Why don't you call omp_get_num_procs() to know for certain that this is indeed a matter of cores available to OpenMP? – CTZStef Mar 17 '15 at 15:14
  • @osgx, it worked! `top` now shows that `opentld` is using ~370% and the CPU monitor I have shows 100%. However, there is no noticeable change in performance (still 2 seconds of lag). Could it be that CPU processing power is not a bottleneck here? If not, what could it be? – faeophyta Mar 17 '15 at 15:21
  • @faeophyta, `OMP_WAIT_POLICY=ACTIVE` will always increase CPU load to high values but sometimes without useful work. Try only `export GOMP_CPU_AFFINITY=0-3` – osgx Mar 17 '15 at 18:14
  • faeophyta, What do you use from OpenTLD and what are parameters of video? – osgx Mar 17 '15 at 18:47
  • @osgx, I track an arbitrary object. In the end, the source video will be 1080p and the ouput will be streamed to a phone or tablet. I found [this link](http://www.answers.opencv.org/question/29957/highguivideocapture-buffer-introducing-lag/) and it appears to be relevant in solving my issues although I have virtually no experience in C++ so I'm not really in the position to modify OpenTLD. Looks like all I need to do is to decrease the frequency with which OpenTLD accesses the camera's output buffer. What do you think? – faeophyta Mar 17 '15 at 19:24
  • Careful, if you reduce the frequency, OpenTLD will be far less efficient, since its online learning algorithm relies on coherence from frame to frame. – CTZStef Mar 18 '15 at 17:49
  • @ctzsef duly noted. However, from what I could gather from [this link](http://www.answers.opencv.org/question/29957/highguivideocapture-buffer-introducing-lag/), OpenCV is requesting images faster than the camera can supply them thereby introducing lag. That's what I understood from it- maybe I'm wrong. What do you think? – faeophyta Mar 18 '15 at 20:28

1 Answers1

1

First of all, you should do export OMP_NUM_THREADS=4 on the console.

Top reporting usages of 170% for the CPU, doesn't necessarily mean you're only running with 2 threads. The code might be memory bound and that might also deliver low CPU usage levels.

a3mlord
  • 1,060
  • 6
  • 16
  • Thanks for the answer! From osgx's reply, I did `export GOMP_DEBUG=1 OMP_WAIT_POLICY=ACTIVE GOMP_CPU_AFFINITY=0-3`. Now `top` shows ~380%- which is exactly what I wanted. However, there hasn't been an increase in performance. Could this be because as you mentioned it might be "memory bound"? – faeophyta Mar 17 '15 at 15:30
  • Do you have any guesses as to why this might be? – faeophyta Mar 17 '15 at 15:46
  • I talked to gnebehay, author of OpenTLD and he said cause may be the buffer OpenCV has for camera images. Do you think [this link](http://www.answers.opencv.org/question/29957/highguivideocapture-buffer-introducing-lag/) is relevant? – faeophyta Mar 17 '15 at 23:55