Home | | Multi - Core Architectures and Programming | Accessing Private Data Outside the Parallel Region

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

Accessing Private Data Outside the Parallel Region

When a variable is declared as private, each thread gets a private copy of the variable.

Accessing Private Data Outside the Parallel Region

 

When a variable is declared as private, each thread gets a private copy of the variable. However, the variable does not get initialized at the start of the parallel region, and its value does not get propagated beyond the end of the region. Listing 7.28 shows an example of code where the value of the variable before the parallel region is important.

 


Listing 7.28  Parallel Region That Accesses the Value of a Private Variable

int main()

#include <stdio.h>

{

 

int data=1;

 

#pragma omp parallel for private(data) for ( int i=0; i<100; i++ )

 

{

 

printf( "data=%i\n", data );

 

}

 

return 0;

}

Although the variable data is initialized to the value one outside the parallel region, this value is not passed into the private copy that each thread obtains inside the parallel region. Hence, the value that is printed is undefined. To initialize the value of the vari-able in the parallel region to the value it held before the region, the variable needs to be declared using the clause firstprivate(variables). This tells the compiler to include code that copies the existing value into the private copy held by each thread in the par-allel region. Listing 7.29 shows the modified code.

 

Listing 7.29 Declaring a Variable as firstprivate to Pass Its Value into the Parallel Region

#include <stdio.h>

 

int main()

 

{

 

int data=1;

 

#pragma omp parallel for firstprivate(data) for ( int i=0; i<100; i++ )

 

{

 

printf( "data=%i\n", data );

 

}

 

return 0;

}

Another situation is where the value of a variable is used after a parallel region. In this case, it is important to retain the value that was written into this variable by the thread that executed the last iterations of the loop. This preserves the semantics of the serial program. The clause that enables this to happen is lastprivate(variables). This clause is also supported on parallel sections, which will be introduced later. Listing 7.30 shows an example of using lastprivate to retain the last value written into the variable.

 

Listing 7.30   Passing the Value of a Variable Out of a Parallel Region Using

 

Lastprivate

 

#include <stdio.h>

 

int main()

 

{

 

int data=1;

 

#pragma omp parallel for lastprivate(data) for ( int i=0; i<100; i++ )

 

{

 

data = i*i;

 

printf( "data=%i\n", data );

 

}

 

printf( "Final value=%i\n", data ); return 0;

 

}


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 : Accessing Private Data Outside the Parallel Region |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

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