Home | | Multi - Core Architectures and Programming | Keeping Data Private to Threads

Chapter: Multicore Application Programming For Windows, Linux, and Oracle Solaris : Using Automatic Parallelization and OpenMP

Keeping Data Private to Threads

It is possible to set up thread-local data using the OpenMP directive threadprivate.

Keeping Data Private to Threads

 

It is possible to set up thread-local data using the OpenMP directive threadprivate. This directive works in a similar way to the __thread declaration, described in Chapter 5, in making each thread hold a private copy of some variable. Listing 7.42 shows an example of declaring a threadprivate variable.

 

Listing 7.42   Declaring a threadprivate Variable

int i;

 

#pragma omp threadprivate( i )

 

int main()

 

{

 

...

 

}

The value of the threadprivate variable can persist between parallel regions. The rules governing when this will happen are slightly complex but can be summarized as the requirement that the active thread count is the same for the two parallel regions.

 

There are other constraints on threadprivate variables. If the code is parallelized using tasks and the value of the variable depends on the order that the tasks are com-pleted, then its value will be unpredictable.

 

During serial portions of the application’s execution, the variable will return the value held by the master thread. This can be demonstrated using the code shown in Listing 7.43. The variable i is thread private. In the master thread, it is set to hold the value -1, but in the parallel region, it is set to hold the thread ID. Each thread will set the value of i to its thread ID. The ID of the master thread is zero. Outside the parallel region, the refer-ence to the variable i resolves to the value held by the master thread. So, the final version of the variable i will be zero.

 

Listing 7.43   Printing the Value of a threadprivate Variable

#include<stdio.h>

 

#include<omp.h>

 

int i;

 

#pragma omp threadprivate( i )

 

int main()

 

{

 

i = -1;

 

#pragma omp parallel

 

{

i = omp_get_thread_num();

printf( "Parallel value %i\n", i );

 

}

 

printf( "Serial value %i\n", i );

 

}

 

The main reason for using threadprivate variables is to hold a value within a par-allel region, not necessarily across parallel regions. Consequently, there may be require-ments to copy a value into a region. The copyin clause copies the value from the master thread into the threadprivate values held by the worker threads. This clause can be placed on parallel regions. Listing 7.44 shows an example of using the copyin clause. The value of the variable i within the parallel region will be -1 for all threads.

 

Listing 7.44   Using copyin to Copy Data from the Master Thread

#include<stdio.h>

 

#include<omp.h>

 

int i;

 

#pragma omp threadprivate( i )

 

void main()

 

{

 

i = -1;

 

#pragma omp parallel copyin( i )

 

{

 

printf( "Parallel value %i\n", i );

 

}

 

}

The copyprivate directive can be used to propagate the value of a threadprivate variable calculated in a single region to all threads. Although this applies to the single directive, the impact of the clause is at the end of the single region where the value is copied from the single thread to all the other threads. This can be used for the initializa-tion of the threadprivate variables or dissemination of a new value to all threads. Listing 7.45 shows an example of using copyprivate. In this example, all threads will receive the value 2 for their private copy of the variable i.

 

Listing 7.45   Using copyprivate to Copy Data a Single Thread to All Other Threads

#include<stdio.h>

 

#include<omp.h>

 

int i;

 

#pragma omp threadprivate( i )

int main()

 

{

 

i = -1;

 

#pragma omp parallel

 

{

 

#pragma omp single copyprivate( i )

 

{

 

i = 2;

 

}

 

printf( "Parallel value %i\n", i );

 

}

 

}


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Multicore Application Programming For Windows, Linux, and Oracle Solaris : Using Automatic Parallelization and OpenMP : Keeping Data Private to Threads |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.