Data Sharing Attributes¶
Private Variable¶
Each thread will have its own copy of the private variable.
The private variable is only accessible within the parallel region, not outside.
By default, the loop iteration counters are considered as private.
A change made by one thread is not visible to other threads.
Examples: Private variable
#include <iostream>
#include <omp.h>
using namespace std;
int main()
{
int N = 10;
float *a, b, c;
b = 1.0;
c = 2.0;
a = (float*)malloc(sizeof(float) * N);
#pragma omp parallel for private(b,c)
for (int i = 0; i < N; i++)
{
b = a[i] + i;
c = b + 10 * i;
cout << "value of c in the parallel region " << c << endl;
}
cout << "value of c after the parallel region " << c << endl;
return 0;
}
program main
use omp_lib
implicit none
real(8), dimension(:), allocatable :: a
real(8) :: b, c
integer :: n, i
n=10
b=1.0
c=2.0
allocate(a(n))
!$omp parallel private(b,c) shared(a)
!$omp do
do i = 1, n
b = a(i) + i
c = b + 10 * i
print*, 'value of c in the parallel region', c
end do
!$omp end do
!$omp end parallel
print*, 'value of c after the parallel region', c
deallocate(a)
end program main
Questions
What is the value of the variable
cin the parallel region and after the parallel region?After the parallel region, has variable
cbeen updated or not?
Lastprivate¶
lastprivateis similar to a private clause.But each thread will have an uninitialized copy of the variables passed as
lastprivate.At the end of the parallel loop or sections, the final variable value will be the last thread accessed value.
Examples: Lastprivate variable
#include<iostream>
#include<omp.h>
using namespace std;
int main()
{
int n = 10;
int var = 5;
omp_set_num_threads(10);
#pragma omp parallel for lastprivate(var)
for(int i = 0; i < n; i++)
{
var += omp_get_thread_num();
cout << " lastprivate in the parallel region " << var << endl;
}
cout << "lastprivate after the parallel region " << var <<endl;
return 0;
}
program main
use omp_lib
implicit none
real(8) :: var
integer :: n, i
n = 10
var = 5
call omp_set_num_threads(10)
!$omp parallel
!$omp do lastprivate(var)
do i = 1, n
var = var + omp_get_thread_num()
print*, 'lastprivate in the parallel region ', var
end do
!$omp end do
!$omp end parallel
print*, 'lastprivate after the parallel region ', var
end program main
Questions
What is the value of the variable
varin the parallel region and after the parallel region?Do you think the initial value of variable
varis considered within the parallel region?
Firstprivate¶
firstprivateis similar to a private clause.But each thread will have an initialized copy of the variables passed as
firstprivate.Available for parallel constructs, loops, sections and single constructs.
Examples: Firstprivate variable
#include<iostream>
#include<omp.h>
using namespace std;
int main()
{
int n = 10;
int var = 5;
omp_set_num_threads(10);
#pragma omp parallel for firstprivate(var)
for(int i = 0; i < n; i++)
{
var += omp_get_thread_num();
cout << " firstprivate in the parallel region " << var << endl;
}
cout << "firstprivate after the parallel region " << var <<endl;
return 0;
}
program main
use omp_lib
implicit none
real(8) :: var
integer :: n, i
n = 10
var = 5
call omp_set_num_threads(10)
!$omp parallel
!$omp do firstprivate(var)
do i = 1, n
var = var + omp_get_thread_num()
print*, 'firstprivate in the parallel region ', var
end do
!$omp end do
!$omp end parallel
print*, 'firstprivate after the parallel region ', var
end program main
Questions
What is the value of the variable
varin the parallel region and after the parallel region?Is variable
varupdated after the parallel region, if not why?