Difference between OpenMP threadprivate and private

L30nardo SV. picture L30nardo SV. · Aug 2, 2013 · Viewed 15.8k times · Source

I am trying to parallelize a C program using OpenMP.

I would like to know more about:

  1. The differences between the threadprivate directive and the private clause and
  2. In which cases we must use any of them.

As far as I know, the difference is the global scope with threadprivate and the preserved value across parallel regions. I found in several examples that when a piece of code contains some global/static variables that must be privatized, these variables are included in a threadprivate list and their initial values are copied into the private copies using copyin.

However, is there any rule that prevents us to use the private clause to deal with global/static variables? perhaps any implementation detail?

I couldn't find any explanation in the OpenMP3.0 specification.

Answer

Kyle_the_hacker picture Kyle_the_hacker · Aug 4, 2013

The most important differences you have to memorize:

  • A private variable is local to a region and will most of the time be placed on the stack. The lifetime of the variable's privacy is the duration defined of the data scoping clause. Every thread (including the master thread) makes a private copy of the original variable (the new variable is no longer storage-associated with the original variable).

  • A threadprivate variable on the other hand will be most likely placed in the heap or in the thread local storage (that can be seen as a global memory local to a thread). A threadprivate variable persist across regions (depending on some restrictions). The master thread uses the original variable, all other threads make a private copy of the original variable (the master variable is still storage-associated with the original variable).


  • There are also more tricky differences:

    • Variables defined as private are undefined for each thread upon entering the construct and the corresponding shared variable is undefined when the parallel construct is exited; the initial status of a private pointer is undefine.

    • But data in the threadprivate common blocks should be assumed to be undefined on entry to the first parallel region unless a copyin clause is specified. When a common block appears in a threadprivate directive, each thread copy is initialized once prior to its first use.

  • The OpenMP Specifications (section 2.14.2) actually give a very good description (and also more detailled) of the threadprivate directive:

    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 task that modifies the variable. For more details on task scheduling, see Section 1.3 on page 14 and Section 2.11 on page 113.

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

    During a 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.

    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 thread affinity policies used to execute both parallel regions are 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.