Data race with global but static defined variable

General OpenMP discussion

Data race with global but static defined variable

Postby Clemens09 » Mon May 26, 2014 1:07 am

Hi,
I like to parallelize a larger code with OpenMP. Since a part of this program is written with some globally defined variables, I'd like to make sure that my suspicion about a data race condition is true before I rewrite the whole thing.

The problem is like this:

In file1.c

Code: Select all
static double xyz;

void func_1(double *in)
{
....
xyz = in[0];
....
}


In file1.h

Code: Select all
void func_1(double *in);


In file2.c

Code: Select all
#include file1.h

void func_2(double *in)
{
#omp parallelize ...
for(...)
    func_1(in);
....
}


Now my suspicion is that no matter how I define xyz in file1.c, I will always get a data race when I call func_1 from another file. Is that correct?

What about include something like private(xyz) in file2.c? I guess this doesn't work because when I invoke the parallelization, the global variable hasn't been encountered yet.

Thanks for your help
Clemens09
 
Posts: 6
Joined: Sat Jun 19, 2010 3:25 am

Re: Data race with global but static defined variable

Postby ftinetti » Mon May 26, 2014 5:07 am

Hi,

Now my suspicion is that no matter how I define xyz in file1.c, I will always get a data race when I call func_1 from another file. Is that correct?


Yes.

What about include something like private(xyz) in file2.c? I guess this doesn't work because when I invoke the parallelization, the global variable hasn't been encountered yet.


Actually, It should not compile, since xyz is not known in file2.c, I would suggest starting with

static double xyz;
#pragma omp threadprivate(xyz)

void func_1(double *in)
{
....
xyz = in[0];
....
}


in file1.c, and remember to compile and link file1.c and file2.c with the corresponding omp flag/s.

HTH,

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

Re: Data race with global but static defined variable

Postby Clemens09 » Tue May 27, 2014 3:09 am

Hi Fernando,
thanks for your advice.

ftinetti wrote:Actually, It should not compile, since xyz is not known in file2.c, I would suggest starting with

static double xyz;
#pragma omp threadprivate(xyz)

void func_1(double *in)
{
....
xyz = in[0];
....
}



You think this prevents that a parallel call of func_1 from func_2 from overwriting "xyz"? That would be great, since I wouldn't need to rewrite func_1!

ftinetti wrote:in file1.c, and remember to compile and link file1.c and file2.c with the corresponding omp flag/s.


Just to be sure: I'm compiling and linking with the -fopenmp flag.
Clemens09
 
Posts: 6
Joined: Sat Jun 19, 2010 3:25 am

Re: Data race with global but static defined variable

Postby ftinetti » Tue May 27, 2014 3:45 am

Hi,

thanks for your advice.

You are welcome.

Code: Select all
static double xyz;
#pragma omp threadprivate(xyz)

void func_1(double *in)
{
  ....
  xyz = in[0];
  ....
}

You think this prevents that a parallel call of func_1 from func_2 from overwriting "xyz"? That would be great, since I wouldn't need to rewrite func_1!


No reply would be better than you analyzing, trying, and seeing, but... I think so, at least that is what the threadprivate directive is expected to do.

Just to be sure: I'm compiling and linking with the -fopenmp flag.


For gcc, yes.

HTH,

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

Re: Data race with global but static defined variable

Postby Clemens09 » Tue May 27, 2014 5:06 am

Hi Fernando,
I'll test and report it (if I remember ;) ).

Having not done the test before was just because I wanted to check if it's at all possible to realize what I had in mind.

Cheers
Clemens
Clemens09
 
Posts: 6
Joined: Sat Jun 19, 2010 3:25 am


Return to Using OpenMP

Who is online

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