What is default status of variables?

General OpenMP discussion

What is default status of variables?

Postby myusrnam » Wed Sep 17, 2008 5:11 pm

For a 'parallel for' directive, what is the default status of a variable within its construct-----shared, or private? I find it difficult to understand the spec on this issue. From (unoffiical) documents I've read on the web, I *think* this is how it works: variables in the construct of a 'parallel for' directive (which, basically, means variables within the for loop being parallelized) are shared except for the iterator of the loop and except for any variables created off the stack inside the loop, or created inside of any functions / methods called within the loop. Is this correct?

I have a follow on question. When a "shared" clause is used with a 'parallel for' directive, does OpenMP interpret this as the user specifying that *ONLY* the variables within that clause are shared, and all others private? Perhaps my question can be phrased more generally. What is the status of a variable *NOT* mentioned in the shared clause? What if the default status of that variable would have been shared had the clause not existed? Does that variable still remain shared even if it is not mentioned in the 'shared' clause?

We recently had an application core dump after inserting a 'parallel for' directive with an incomplete list of shared variables in a"shared" clause. By "incomplete" I mean that, just by inspection, one could identify variables in the for loop being parallelized that were obviously shared, but which had been left out of the shared clause's list. When I removed the entire shared clause, the core dump went away and the code seemed to function correctly, when using multiple threads. I am wondering if this is because, by using a shared clause, we made all variables not mentioned in that clause 'private', and, in this manner, accidentally corrupted the value of what should have been a shared variable. It also occured to me that *if* shared clauses work this way and if the default definition of shared variables encompasses almost all variables one would normally think of as shared, I am thinking that use of 'shared' clauses is (a) almost never needed (since the default captures all you're likely to want), and (b)is dangerous.

Please comment.

Thanks,

Richard
myusrnam
 
Posts: 17
Joined: Wed Sep 17, 2008 4:43 pm

Re: What is default status of variables?

Postby ejd » Thu Sep 18, 2008 9:01 am

myusrnam wrote:For a 'parallel for' directive, what is the default status of a variable within its construct-----shared, or private? I find it difficult to understand the spec on this issue.

In general, OpenMP uses a shared memory model, so by default most things are shared. That said, there are a lot of "ifs, ands, and buts". If you look at the OpenMP V2.5 spec, section 2.8 Data Environment tries to explain all the various cases for the different languages. So let's take the following simple example:
Code: Select all
int a = 0, b = 0, i;
#pragma omp parallel firstprivate(a)
{
  int d;
  static, int e;
  #pragma omp for
  for (i = 0; i < 5; i++)
    ...
}

myusrnam wrote:From (unoffiical) documents I've read on the web, I *think* this is how it works: variables in the construct of a 'parallel for' directive (which, basically, means variables within the for loop being parallelized) are shared except for the iterator of the loop and except for any variables created off the stack inside the loop, or created inside of any functions / methods called within the loop. Is this correct?

In my simple example, when the parallel directive is encountered (page and line numbers are from the OpenMP V2.5 spec):
  • "a" is firstprivate because of the clause (p64:35-36 - Variables referenced in the construct are said to have an explicitly determined sharing attribute if they are listed in a data-sharing attribute clause on the construct).
  • "b" and "i" are shared (p65:1-6 - Variables referenced in the construct whose sharing attribute is not predetermined or explicitly determined will have their sharing attribute implicitly determined. In a parallel construct, the sharing attributes of these variables is determined by the default clause, if present. If no default clause is present, variables with implicitly determined sharing attributes are shared).
Now looking at the variables declared within the parallel region:
  • "d" is private (p64: 1-2 - Variables with automatic storage duration which are declared in a scope inside the
    construct are private).
  • "e" is shared (p64:4 - Static data members are shared).
Now we see the for directive:
  • "i" is private (p64:5-6 - The loop iteration variable in the for-loop of a for or parallel for construct is private in that construct).

So you are correct on the first part.

The last part - variables are shared except when "created inside of any functions" is incorrect. If you look at section 2.8.1.2 Sharing Attribute Rules for Variables Referenced in a Region, but not in a Construct, you will see there are many cases where the variables are shared. For example (static variables declared in called routines in the region are shared):
Code: Select all
void func(void)
{
  static int a;   // a is shared
  ...
}

int main(void)
{
   ...
  #pragma omp parallel
  {
    func();
  }
  ...
}

myusrnam wrote:I have a follow on question. When a "shared" clause is used with a 'parallel for' directive, does OpenMP interpret this as the user specifying that *ONLY* the variables within that clause are shared, and all others private? Perhaps my question can be phrased more generally. What is the status of a variable *NOT* mentioned in the shared clause? What if the default status of that variable would have been shared had the clause not existed? Does that variable still remain shared even if it is not mentioned in the 'shared' clause?

From section 2.8.1.1 (p64:35-36), variables referenced in the construct are said to have an explicitly determined sharing attribute if they are listed in a data-sharing attribute clause on the construct. So by putting a variable in the shared clause, you have explicitly stated what you want it's sharing attributes to be. It doesn't say anything about the sharing attributes of any of the other variables. They are determined in accordance with the information in section 2.8.
myusrnam wrote:We recently had an application core dump after inserting a 'parallel for' directive with an incomplete list of shared variables in a "shared" clause. By "incomplete" I mean that, just by inspection, one could identify variables in the for loop being parallelized that were obviously shared, but which had been left out of the shared clause's list. When I removed the entire shared clause, the core dump went away and the code seemed to function correctly, when using multiple threads. I am wondering if this is because, by using a shared clause, we made all variables not mentioned in that clause 'private', and, in this manner, accidentally corrupted the value of what should have been a shared variable. It also occured to me that *if* shared clauses work this way and if the default definition of shared variables encompasses almost all variables one would normally think of as shared, I am thinking that use of 'shared' clauses is (a) almost never needed (since the default captures all you're likely to want), and (b)is dangerous.

Normally I would say that a shared clause is not necessary. However, when parallelizing code, personally I would use "default(none)" and explicitly state the sharing attributes for each variable in a clause. I know this can be a lot of work. However, there are tools that can help do this and in doing this you make sure you really know how each variable is being handled. In the long run, it usually saves you time. In any case, from what you have said, I have no idea why putting the variable in a shared clause would cause a core dump. If you can give me a small example, maybe it would make more sense to me.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: What is default status of variables?

Postby myusrnam » Thu Sep 18, 2008 12:48 pm

Thanks a lot for taking the time to explain this. One very important (to me) bit of information was the assurance that leaving a variable out of a 'shared' clause list does *not* make that variable private. That is good. I guess that our core dump must have been due to something else, and not this. Unfortunately, it is too difficult to send a meaningful example, the code in which this happened is proprietary and long. So, for the moment, we'll have to let it be a mystery. But, at least it's been clarified what *should* happen.

I would like to suggest that the explanation of how a variable's shared or private status is determined could benefit from a rewrite, in the spec. I find the presentation very confusing. I'm also not sure it's complete, meaning, I'm not sure it covers all cases. The spec emphasizes special cases, and does not inform the user very well about the general case. I think a better approach to the subject would be to start out saying "all variables are, by default, shared, except in the following cases...." and then list cases where variables would be private, by default. And, then, just as a clarification, mention some special cases where variables are shared, cases that might be confusing, otherwise And, then end with an explanation of what clauses do with respect to the shared or private status of a variable----and mention what they *don't* do as well, to dispel any notions like I had, which is that by leaving a variable out of a clause one might change its shared vs. private status.
myusrnam
 
Posts: 17
Joined: Wed Sep 17, 2008 4:43 pm

Re: What is default status of variables?

Postby ejd » Fri Sep 19, 2008 6:51 am

Thank you for your suggestion. I will make sure that your suggestion is passed on to the OpenMP language committee responsible for the specification.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am


Return to Using OpenMP

Who is online

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