Threads not sleeping outside parallel regions

General OpenMP discussion

Threads not sleeping outside parallel regions

Postby DiscoViking » Mon Aug 13, 2012 1:58 pm

So I've written a 3D Software Renderer over the last couple of weeks, and a while ago I added OpenMP support. Just a single "#pragma omp parallel for schedule(dynamic, 100)" before the loop that draws all the triangles. It speeds up the rendering nearly 4-fold (on a quad-core CPU), which is great, but now I don't seem to be able to reduce the CPU usage at all.

I have a sleep() call at the end of the main loop to limit the frame rate. When running on just one core, it works fine, bringing the CPU usage down to about 4% when it's not drawing anything (but is still calculating coordinates of ~9000 vertices).

But when using all 4 cores, even with nothing on-screen, the CPU usage only drops down to 88% or so (down from 99). The frame-rate limiting is still working, it runs at 100fps (would go up to 350 or so otherwise). It just seems like the threads aren't giving up their CPU time outside the parallel region.
Is there a better alternative to get a parallel program to sleep? I've heard sleep() isn't a great way of doing it anyway.

To avoid you having to go through all my code, I've written a very simple program that shows off the problem I'm having. http://pastebin.com/KCmpriZS If I run that with openMP off, I get about 10% CPU usage without the Sleep(50), and 0% CPU usage with it.
With openMP ON, I get about 75% CPU usage with AND without the Sleep(50).
If I increase it to Sleep(500) then the usages does drop to about 10%, but that's still absurdly high for something that's running only twice a second.
NOTE: that program runs significantly slower with openMP enabled, but that's besides the point, sleeping for 50ms per iteration should reduce the CPU usage to 0, but it doesn't.
DiscoViking
 
Posts: 3
Joined: Fri Aug 10, 2012 4:06 pm

Re: Threads not sleeping outside parallel regions

Postby MarkB » Tue Aug 14, 2012 2:18 am

Hi there,

You could try setting the OMP_WAIT_POLICY environment variable to PASSIVE and see if that helps. However, this is just a hint: in the end it is up to the OpenMP implementation to decide what to do with threads between parallel regions: you might find that different compilers show different behaviour in this respect, and there may be additional vendor-specific ways of controlling the behaviour.

Hope that helps,
Mark.
MarkB
 
Posts: 434
Joined: Thu Jan 08, 2009 10:12 am

Re: Threads not sleeping outside parallel regions

Postby ftinetti » Tue Aug 14, 2012 4:58 am

Hi,

You could try setting the OMP_WAIT_POLICY environment variable to PASSIVE and see if that helps.

Hmm... I thought OMP_WAIT_POLICY was related to OpenMP threads waiting because of "OpenMP synchronizations" such as barrier construct, critical construct, end of loop construct without nowait, etc. I did not find anything in the spec. excluding the sleep() call in particular, but I think that being sleep() not part of C it is beyond the scope of the OpenMP spec. and runtime. BTW, when reading on this I found a minor error/typo in the spec. 3.1, section 4.7 OMP_WAIT_POLICY, p. 159:
Cross References
• wait-policy-var ICV, see Section 2.3 on page 24.

and section 2.3 is on page 28.

Fernando.
ftinetti
 
Posts: 581
Joined: Wed Feb 10, 2010 2:44 pm

Re: Threads not sleeping outside parallel regions

Postby MarkB » Tue Aug 14, 2012 5:19 am

Hi Fernando,

ftinetti wrote:Hmm... I thought OMP_WAIT_POLICY was related to OpenMP threads waiting because of "OpenMP synchronizations" such as barrier construct, critical construct, end of loop construct without nowait, etc. I did not find anything in the spec. excluding the sleep() call in particular, but I think that being sleep() not part of C it is beyond the scope of the OpenMP spec. and runtime.


The spec is (deliberately) a bit vague: it says that OMP_WAIT_POLICY affects the behaviour of waiting threads, but does not enumerate the situations in which "waiting" occurs. In this case, threads other than the master are waiting for the next parallel region to start, and I would expect implementations to take note of OMP_WAIT_POLICY here (the fact that the master thread has called sleep() is probably not relevant: the threads could also be waiting for the master to do some computation).


Thanks for spotting the typo!

Mark.
MarkB
 
Posts: 434
Joined: Thu Jan 08, 2009 10:12 am

Re: Threads not sleeping outside parallel regions

Postby ftinetti » Tue Aug 14, 2012 6:00 am

I see, thanks for the explanation, I did not realize that the sleep() is outside the parallel region and threads could be still "alive somewhere" waiting for some work to do.

Fernando.
ftinetti
 
Posts: 581
Joined: Wed Feb 10, 2010 2:44 pm

Re: Threads not sleeping outside parallel regions

Postby DiscoViking » Tue Aug 14, 2012 6:11 am

I've heard of OMP_WAIT_POLICY, however I'm using Visual Studio, which doesn't seem to support that environment variable. (see http://msdn.microsoft.com/en-us/library/6sfk977f(v=vs.110))
I was hoping there might be another way of dealing with this issue, or that I'd just done the code wrong :P.

Which compiler would you recommend? gcc?
DiscoViking
 
Posts: 3
Joined: Fri Aug 10, 2012 4:06 pm

Re: Threads not sleeping outside parallel regions

Postby MarkB » Tue Aug 14, 2012 7:03 am

Yes, Visual Studio is only OpenMP 2.0 compliant, so does not support OMP_WAIT_POLICY

gcc is worth a try (you will need 4.6 or later, I think), though it does not have the best OpenMP runtime implementation around.
Commercial implementations such as Intel or PGI might be a better bet.
MarkB
 
Posts: 434
Joined: Thu Jan 08, 2009 10:12 am

Re: Threads not sleeping outside parallel regions

Postby DiscoViking » Tue Aug 14, 2012 8:21 am

Since this is just a hobby project, I don't want to pay for a commercial compiler. I got Visual Studio for free since I'm a student.
Intel seem to offer their stuff for free non-commercially, but only for Linux. Really don't want to have to dual-boot Linux just to see if this works.
I guess I'll get the Windows trial and see if it works before doing anything else.
Argh, why aren't the Windows versions available also.
DiscoViking
 
Posts: 3
Joined: Fri Aug 10, 2012 4:06 pm

Re: Threads not sleeping outside parallel regions

Postby ftinetti » Tue Aug 14, 2012 10:15 am

Hi,

Since this is just a hobby project, I don't want to pay for a commercial compiler. I got Visual Studio for free since I'm a student.
Intel seem to offer their stuff for free non-commercially, but only for Linux. Really don't want to have to dual-boot Linux just to see if this works.
I guess I'll get the Windows trial and see if it works before doing anything else.
Argh, why aren't the Windows versions available also.


Some (extra) information that could be useful:
1) You can use gcc, g++ (I think, I haven't tested this, specifically), and gfortran on Linux
2) You can use a virtual machine for testing, and performance is not too penalized in performance

I've played around a little bit with your code on linux with g++ 4.4.2 (20091027) and it works fine on this issue (setting OMP_WAIT_POLICY do what I expect to do).

Fernando.
ftinetti
 
Posts: 581
Joined: Wed Feb 10, 2010 2:44 pm


Return to Using OpenMP

Who is online

Users browsing this forum: ftinetti and 7 guests