Openmp and static variables

General OpenMP discussion

Openmp and static variables

Postby erasmusjam » Tue Apr 27, 2010 10:37 am

Hi everyone, I'm learning openmp and beginning to convert my C codes in order to exploit parallelism. I have a big problem: i don't know how to handle static variables to avoid conflicts. For example i have the following parallel loop in my main.c program:
Code: Select all
#pragma omp parallel for default(shared) private(i3, thread_num)
        for (i3=0; i3<n3;i3++) {
         #ifdef _OPENMP
         thread_num=omp_get_thread_num();
         #endif
         patching_init(...);
         patching(input[thread_num], output[thread_num], parameters);
         patching_close(...);
        }


the problem is within the patching.c source code, where i have several static variables. For example:
Code: Select all
static int Ntot;
static int Nint;
static int nh;
static sf_complex** eye;

void patching_init(...){
Ntot=L*(nh-1)+1;
Nint=Ntot-nh;
eye=sf_complexalloc2( Ntot,Ntot) ;
}

void patching_apply(...)

void patching_close(void)
/*<free allocated space>*/{
free(*eye); free(eye);
...
}


The code compiles fine but when i run it in gdb gives each time different errors:
malloc(): memory corruption (fast)
double free or corruption (fasttop)
Bus error

I've tried to declare every static variable as extern in main.c, and then adding a
Code: Select all
#pragma omp threadprivate($STATIC_VARIABLES)

before the parallel loop, but it doesn't help. What should i do?

The program compiles and works fine disabling omp. Thank you for your help!
erasmusjam
 
Posts: 3
Joined: Tue Apr 27, 2010 10:11 am

Re: Openmp and static variables

Postby ftinetti » Tue Apr 27, 2010 11:21 am

Hi,

The problem is, of course, having static variables, since every thread will be using/assigning/etc.
The code compiles fine but when i run it in gdb gives each time different errors:

It's because of the race conditions, i.e. the error depends on the relative sequence of events (variable assignments, basically).
What should i do?

The first suggestion is avoiding static variables by having parameters, so that each iteration has its own set of values. I don't know if this is going to solve every problem...

You could do a quick try directly with
Code: Select all
#pragma omp threadprivate(Ntot, Nint, nh, eye)

in patching.c and see what happens (please report).

Hope this helps.
ftinetti
 
Posts: 582
Joined: Wed Feb 10, 2010 2:44 pm

Re: Openmp and static variables

Postby ejd » Tue Apr 27, 2010 5:09 pm

Since you indicate that you have already tried using threadprivate and it doesn't work, there is something else that you aren't seeing - like a possible ordering. For example, Ntot and Nint are static , but are calculated using nh. You don't show where nh is coming from or how Ntot and Nint are used. You are going to have to look at their use closer.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Openmp and static variables

Postby erasmusjam » Wed Apr 28, 2010 3:12 am

Thank you for you kind answers. Unfortunately it's not easy to convert every static variable to a parameter, because this code is an addition to madagascar project, which widely uses static variables to handle linear operators and solvers.
I'm going to try to find all static variables involved and add them to the threadprivate clause, but it's going to be a time expensive job.
I wonder why there isn't an easy omp clause to automatically initialize every static variable as private...
Thank you again ;)
erasmusjam
 
Posts: 3
Joined: Tue Apr 27, 2010 10:11 am

Re: Openmp and static variables

Postby erasmusjam » Wed Apr 28, 2010 8:34 am

I've solved the problem! I put a
Code: Select all
#pragma omp threadprivate($STATIC_VARIABLES)

just below static declarations in all sources involved, and everything's fine. It's good to see things like that:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
550 me 25 0 9981m 9.2g 1624 R 1457.1 29.3 126:11.32 sfspitzns

going on :D
Bye

Domenico
erasmusjam
 
Posts: 3
Joined: Tue Apr 27, 2010 10:11 am

Re: Openmp and static variables

Postby leonardosolis86 » Fri Aug 02, 2013 9:08 am

Hi guys, all were interesting posts.

I would like to ask you: why the private clause is not enough to privatize static/global variables?

Let's say, even in the case where we have just one parallel region in the whole program, what is the difference between private and threadprivate and when it is appropriate to use each of them?

Many thanks in advance for your reply.
Leonardo
leonardosolis86
 
Posts: 2
Joined: Fri Jun 28, 2013 3:16 am

Re: Openmp and static variables

Postby ftinetti » Sat Aug 03, 2013 5:03 am

Hi,

Basically, the difference private-threadprivate is about automatic (e.g. stack-assigned)-static data, just syntactically reflecting the semantic difference of automatic-static data. A quick search provides a lot of results, I copy just two "ramdomly" selected results here:
http://openmp.org/forum/viewtopic.php?f=3&t=1032
http://www.openmp.org/presentations/sc9 ... sld023.htm

HTH,

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

Re: Openmp and static variables

Postby leonardosolis86 » Sun Aug 04, 2013 8:04 am

Thanks for the quick reply and the links!

So, may I conclude the following?:

    In case I need to privatize automatic data, I should use the private clause, and
    In case I need to privatize file-scope, global scope data; I should use the threadprivate directive.
Leonardo
leonardosolis86
 
Posts: 2
Joined: Fri Jun 28, 2013 3:16 am

Re: Openmp and static variables

Postby MarkB » Mon Aug 12, 2013 4:48 am

leonardosolis86 wrote:
So, may I conclude the following?:

    In case I need to privatize automatic data, I should use the private clause, and
    In case I need to privatize file-scope, global scope data; I should use the threadprivate directive.


Essentially, that's correct, though you can use the private clause to privatize file-scope data etc. provided the only references to it
are in the lexical scope of the parallel region and not in any called routines.
MarkB
 
Posts: 487
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 6 guests