Odd need for printf()

General OpenMP discussion

Odd need for printf()

Postby octavio » Mon Mar 26, 2012 9:24 pm

Hi everybody

I'm trying to understand something weird that happened me today. Well here is the thing, I am parallelizing a loop which contains a function (myFunction) that works in parallel as well.
Now, in order to the program yields a good result the function printf() need to be called within the loop before calling myFunction.
Any idea why does this happen?
octavio
 
Posts: 4
Joined: Thu Feb 02, 2012 8:02 am

Re: Odd need for printf()

Postby MarkB » Tue Mar 27, 2012 2:58 am

You most likely have a synchronisation bug / race condition in your program. Adding the printf() may be delaying one or more of the threads sufficiently that such that the race does not occur.
MarkB
 
Posts: 428
Joined: Thu Jan 08, 2009 10:12 am

Re: Odd need for printf()

Postby octavio » Tue Mar 27, 2012 8:06 am

Thanks for the answer, but does this race condition depend on the number of threads? Because if I create 3, 4 or 5 threads (haven't tested with more threads) everything works correctly with or without the printf. So I only get this promblem if I set the number of threads equal to 1 or 2.
octavio
 
Posts: 4
Joined: Thu Feb 02, 2012 8:02 am

Re: Odd need for printf()

Postby MarkB » Tue Mar 27, 2012 8:27 am

octavio wrote:Thanks for the answer, but does this race condition depend on the number of threads?


The number of threads may affect the likelihood of the race actually occurring and resulting in the wrong answer.

However, if you are getting the wrong answer on 1 thread, it may be another type of bug, for example a private variable which is not being initialized before being used.
MarkB
 
Posts: 428
Joined: Thu Jan 08, 2009 10:12 am

Re: Odd need for printf()

Postby octavio » Tue Mar 27, 2012 11:46 am

MarkB wrote:However, if you are getting the wrong answer on 1 thread, it may be another type of bug, for example a private variable which is not being initialized before being used.


Yes, when using only 1 thread part of the problem was that I was not initializing a private variable, but still the printf function needs to be called.
Could you explain to me why does the variable need to be initialized before, even if inside the loop I assign it a new value?

Thank you
octavio
 
Posts: 4
Joined: Thu Feb 02, 2012 8:02 am

Re: Odd need for printf()

Postby james » Wed Apr 04, 2012 8:19 pm

A thread safe printf is going to be a serialization point. So adding the printf to your code is going to create a bottleneck in the code which might hide a race condition as Mark pointed out.

The reason for the difference between 2, 3 and 4 threads may have to do with the number of cores available to your program. How many times have you run your program before claiming the higher thread counts always get the same answer? This is a trick question. If you have a race condition the problem might magically appear in the higher thread counts when you least expect it.

Without seeing your code it is hard to help explain the need for the printf.

As far as the initialization problem, if the variable is scoped private than having a value defined before the region should have absolutely no effect on the region. One thing to try is looking at the code inside of a debugger to see what value the private variable has when the loop starts up.
james
 
Posts: 53
Joined: Fri May 16, 2008 9:27 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 11 guests