Data Scoping (OpenMP)

From HPC Wiki
OpenMP in Small Bites/Scoping
Jump to: navigation, search

Title: OpenMP in Small Bites
Provider: HPC.NRW

Type: Multi-part video
Topic Area: Programming Paradigms
License: CC-BY-SA

1. Overview
2. Worksharing
3. Data Scoping
4. False Sharing
5. Tasking
6. Tasking and Data Scoping
7. Tasking and Synchronization
8. Loops and Tasks
10. Task Scheduling
11. Non-Uniform Memory Access


This video provides an overview on managing one of the most challenging parts of OpenMP: Data Scoping. It discusses the differences between private, firstprivate, lastprivate and shared variables and shows how to implement a scalable reduction.


( Slides as pdf)


1. What is the default data scoping of variable in a parallel region when this variable is declared before a parallel region?

Click and submit to see the answer

2. What is the data scoping of the variables a, b, c and d in the following code snippet in the parallel region? What is printed when executing the code?
int a = 0;
int b = 23;
int c = -3;
# pragma omp parallel num_threads(2) private(a) reduction(+:c)
int d = omp_get_thread_num();
a = 42 + d;
# pragma omp critical
b = 1;
c += a + b;
c = c / 2;
printf("a=%d, b=%d, c=%d\n", a, b, c)

Click and submit to see the answer

3. The following code snippet is wrong due to a missing data sharing attribute. How can you fix it?
int i, int s = 0;
# pragma omp parallel for
for (i = 1; i < 100; i++){
s = s + a[i];

Click and submit to see the answer