Using Flush in OpenMP >=3.0

General OpenMP discussion

Using Flush in OpenMP >=3.0

Postby Genie » Tue Jan 17, 2012 4:09 am

Hello,
I am a newbie in OpenMP and i have a question about using the Flush function inside the tasks in OpenMP 3.0.

If we have this code:

omp_set_num_threads (8);

#pragma omp parallel

{
#pragma omp single
{
int x=5;
#pragma omp task shared (x)
{
work1 (x);
}
#pragma omp task shared (x)
{
work2 (x);
}
#pragma omp task shared (x)
{
work3 (x);
}
#pragma omp taskwait
}
print (x);
}

is x considered as shared between the threads?
do we need to use the flush function in the "work1" "work2" and "work3" functions to keep the memory consistency when x is modified by one of the tasks?

Thank you in advance for your help
Genie
 
Posts: 4
Joined: Tue Jan 17, 2012 3:54 am

Re: Using Flush in OpenMP >=3.0

Postby ftinetti » Tue Jan 17, 2012 7:56 am

Hi,

I think I do not completely understand your question, since according to the standard calling conventions of C, the value of variable x is passed as the actual copied parameter to functions work1(), work2, and work3(), i.e. variable x is not affected/changed by any assignment made in those functions (aka "value parameter" or "parameter passed by value" as opposed to "reference parameter")...

HTH.
ftinetti
 
Posts: 571
Joined: Wed Feb 10, 2010 2:44 pm

Re: Using Flush in OpenMP >=3.0

Postby Genie » Tue Jan 17, 2012 8:15 am

Thank you a lot for your reply,

consider my code rather looks like this:

Code: Select all
omp_set_num_threads (8);

#pragma omp parallel

{
#pragma omp single
{
int x=5;
#pragma omp task shared (x)
{
do some work
x=x+1;
}
#pragma omp task shared (x)
{
do some work
if (x==5) {
printf("Task1 did not modify x");
x=x+2;}
else
{
x=x+5;
}
}
#pragma omp task shared (x)
{
do some work
if (x==7) printf (" Task2 modified x");
}
#pragma omp taskwait
}
print (x);
}


So if task1 executed the instruction x=x+1;
do task 2 detect this modification without a flush (considering that task2 is executing on another thread)?
same thing for task3

Thanks
Genie
 
Posts: 4
Joined: Tue Jan 17, 2012 3:54 am

Re: Using Flush in OpenMP >=3.0

Postby ftinetti » Tue Jan 17, 2012 8:37 am

I see... I think that if tasks are executed concurrently there will be race conditions and you will need some sort of synchronization, such as that provided by the critical directive, which also implies flushing variables.

I'm rather confused by the usage of tasks inside omp single... but I'm far from being an expert, I've not used tasks at all...

HTH.
ftinetti
 
Posts: 571
Joined: Wed Feb 10, 2010 2:44 pm

Re: Using Flush in OpenMP >=3.0

Postby Genie » Tue Jan 17, 2012 10:51 am

I think that using the critical directive is effective for this specific code, however, it's not always ideal since it flushes all variables while in some cases only specific variables need to be updated.

I am confused about the difference between defining the variable x before the parallel directive and putting it as shared like this
-------------------------------------
int x=5 ;
#pragma omp parallel shared(x)
...
------------------------------------

and creating it inside of one thread and then consider it as shared between the tasks that can run on the different threads (code presented earlier)
I believe they are similar since x is being shared anyways through tasks; can anyone please tell me if i'm right or wrong about it ?

Thanks
Genie
 
Posts: 4
Joined: Tue Jan 17, 2012 3:54 am


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 7 guests