parallel-processingopenmpprivate

Difference between OpenMP threadprivate and private


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.


Solution

  • The most important differences you have to memorize:


    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.