Thread subgroup shared variables

General OpenMP discussion

Thread subgroup shared variables

Postby siegela67 » Wed Apr 11, 2012 9:32 pm

Hi: I have a private variable (call it 'f') in a parallel region with say two threads (for simplicity). I enter a nested parallel region with now 4 total threads, would like f to be shared among the children threads, but not between them. In other words, I'd like two copies of f, one shared by the two children of thread 0, and the other shared by the children of thread 1. Of course I can program this in a roundabout way by creating four private copies and then synching, but I'd like to do it more elegantly and without the wasted memory and data copying. It seems like a fairly straightforward thing to want to do, but I can't find any examples of it anywhere. Is there a natural way to do this in OpenMP? I believe I tried all of the straightforward approaches but nothing really fit. Am I missing something obvious? thanks.
siegela67
 
Posts: 4
Joined: Wed Apr 11, 2012 4:35 pm

Re: Thread subgroup shared variables

Postby MarkB » Thu Apr 12, 2012 7:59 am

The natural way of doing this (i.e. declaring f as private on the outer parallel region and shared on the inner one) should achieve what you want. Does this appear not to work for you?
MarkB
 
Posts: 428
Joined: Thu Jan 08, 2009 10:12 am

Re: Thread subgroup shared variables

Postby siegela67 » Thu Apr 12, 2012 8:10 am

Thanks for the reply. What you say is exactly what I want to do and would have expected it to work, but if I use a shared clause with f upon entry to the nested parallel region, openmp complains that a threadprivate variable cannot appear in a shared clause. It isn't able to separate the two regions in that way and doesn't get what I'm trying to do.

Is there another way to do what you suggest. I'm dyed-in-the-wool MPI programmer who has been pounding on openMP for about month, far from an expert so I wouldn't be surprised or embarrassed to admit if I missed something obvious.
siegela67
 
Posts: 4
Joined: Wed Apr 11, 2012 4:35 pm

Re: Thread subgroup shared variables

Postby MarkB » Thu Apr 12, 2012 8:48 am

Aha! If you are trying to do this using threadprivate global variables then you will indeed have problems. If possible you should avoid these and do something like:

Code: Select all
int f;
#pragma omp parallel private(f)
{
   #pragma omp parallel shared(f)
   {
   ....
   }
}
MarkB
 
Posts: 428
Joined: Thu Jan 08, 2009 10:12 am

Re: Thread subgroup shared variables

Postby siegela67 » Thu Apr 12, 2012 9:25 am

It's at least very encouraging to know that what you have works (assuming it does). It also gives me a possible angle of attack. Problem is that I am not the original author of the (rather large) code that I'm now using and helping develop -- it is in f95 and takes a very fortranish approach to data sharing. the use of globally shared (across subroutines) variables is pretty deeply ingrained in the code architecture. some of these need to be threadprivate at nesting level 1. not sure how feasible it is to restructure at this level. I'll give it some thought, though. thanks for your help.
siegela67
 
Posts: 4
Joined: Wed Apr 11, 2012 4:35 pm

Re: Thread subgroup shared variables

Postby siegela67 » Thu Apr 12, 2012 10:18 am

With your reply I was able to do it with very little pain, just made a copy of the global threadprivate variable in the subroutine (automatic so local by default), specified shared on the nested region and it worked as you point out.
I do consider it a bit of an oversight to have to make the copy, but in this case the data structure is pretty small memory wise and the workaround is simpler than I realized. Many thanks for getting me going in the right direction. I had a bad attitude and assume the nesting case that you posted wouldn't work either!
siegela67
 
Posts: 4
Joined: Wed Apr 11, 2012 4:35 pm

Re: Thread subgroup shared variables

Postby MarkB » Fri Apr 13, 2012 6:43 am

Glad that helped: good luck with the rest of the code!
MarkB
 
Posts: 428
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests