Private vs threadprivate?

General OpenMP discussion

Private vs threadprivate?

Postby Woody » Wed Dec 01, 2010 1:31 pm

I am unclear on the distinction between declaring a variable private, and declaring it threadprivate. I am specifically interested in Fortran DO loops, with code such as

!$OMP PARALLEL DEFAULT(SHARED)
!$OMP DO PRIVATE(...)
DO i=1,n
...
END DO
!$OMP END DO
!$OMP END PARALLEL

I have discovered that stack variables (i e, declared in this subroutine) are handled correctly if they appear in the PRIVATE list above, but variables stored in a Fortran module are shared, even if they're included in the PRIVATE list. In order to have a separate copy for each thread, I must declare them threadprivate in the Fortran module, e g

MODULE My Module
INTEGER :: ijk
!$OMP threadprivate(ijk)
...

What is different about a private variable, and a threadprivate variable? Aren't they, in each case, created for each thread? Is it just the fact that one is created on the stack, and the other from the heap?
Woody
 
Posts: 3
Joined: Wed Dec 01, 2010 1:20 pm

Re: Private vs threadprivate?

Postby ejd » Thu Dec 02, 2010 1:29 pm

A private variable is local (to a region) and may well be placed on the stack. A private variable's scope is only for the region where it has been "defined" by the data scoping clause (private). Threadprivate on the other hand, may be heap storage or thread local storage. A threadprivate variable can persist across regions (depending on a number of restrictions).
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Private vs threadprivate?

Postby Woody » Thu Dec 02, 2010 2:01 pm

When I declare a variable private, I expect that code in each thread will reference a different memory location. This is all I want to accomplish by either private or threadprivate. Since the thread may cease to exist at the end of a region, I don't expect to be able to access the thread's value outside that region. This is true for both the private and threadprivate variables I'm using. If a variable is associated with a thread, I don't see how it could persist when the thread does not.
Woody
 
Posts: 3
Joined: Wed Dec 01, 2010 1:20 pm

Re: Private vs threadprivate?

Postby ejd » Fri Dec 03, 2010 7:56 am

There are actually a couple of other things that differentiate private and threadprivate that I had forgotten. For private variables the storage is created for each task, but the value is undefined. For threadprivate, the storage is associated with the thread and each copy is initialized once. Also, for private, each thread gets a private copy of the variable - including the master thread. For threadprivate, the master thread uses the original copy.

You can look in the OpenMP V3.0 specification, section 2.9.2 threadprivate Directive for more information:
Description
Each copy of a threadprivate variable is initialized once, in the manner specified by the program, but at an unspecified point in the program prior to the first reference to that copy. The storage of all copies of a threadprivate variable is freed according to how static variables are handled in the base language, but at an unspecified point in the program.

A program in which a thread references another thread’s copy of a threadprivate variable is non-conforming.

The content of a threadprivate variable can change across a task scheduling point if the executing thread switches to another schedulable task that modifies the variable. For more details on task scheduling, see Section 1.3 on page 11 and Section 2.7 on page 59.

In parallel regions, references by the master thread will be to the copy of the variable in the thread which encountered the parallel region.

During the sequential part references will be to the initial thread’s copy of the variable. The values of data in the initial thread’s copy of a threadprivate variable are guaranteed to persist between any two consecutive references to the variable in the program.

Also, you can see that threadprivate data can persist across active parallel regions under certain conditions:
The values of data in the threadprivate variables of non-initial threads are guaranteed to persist between two consecutive active parallel regions only if all the following conditions hold:
• Neither parallel region is nested inside another explicit parallel region.
• The number of threads used to execute both parallel regions is the same.
• The value of the dyn-var internal control variable in the enclosing task region is false at entry to both parallel regions.

If these conditions all hold, and if a threadprivate variable is referenced in both regions, then threads with the same thread number in their respective regions will reference the same copy of that variable.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Private vs threadprivate?

Postby Woody » Fri Dec 03, 2010 12:02 pm

Thank you for the explanation; it's certainly much more complicated than I thought.

In this particular case, I don't need to have the threadprivate variables initialized; they are temporaries that are used by different Fortran subroutines. Is there any way to avoid the overhead of copying the contents of that memory to each thread? I assume this is what is meant by "Each copy of a threadprivate variable is initialized once, in the manner specified by the program".
Woody
 
Posts: 3
Joined: Wed Dec 01, 2010 1:20 pm

Re: Private vs threadprivate?

Postby ejd » Fri Dec 03, 2010 2:10 pm

I know of no way to stop the initialization of the threadprivate areas. The initialization can take place at the start of the program or at the start of the region. That is why there is a COPYIN clause. The initialization is usually very efficient, doing a memcopy from one threadprivate copy to the others.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 3 guests

cron