Difference between revisions of "OpenMP in Small Bites/Scoping"
OpenMP in Small Bites/Scoping
Jump to navigation
Jump to search
(28 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | [[Category:Tutorials]] | + | [[Category:Tutorials|Data Scoping (OpenMP)]]<nowiki /> |
− | + | {{DISPLAYTITLE:Data Scoping (OpenMP)}}<nowiki /> | |
+ | {{Syllabus OpenMP in Small Bites}}<nowiki /> | ||
__TOC__ | __TOC__ | ||
− | + | This video provides an overview on managing one of the most challenging parts of OpenMP: Data Scoping. It discusses the differences between <code>private</code>, <code>firstprivate</code>, <code>lastprivate</code> and <code>shared</code> variables and shows how to implement a scalable reduction. | |
− | |||
=== Video === <!--T:5--> | === Video === <!--T:5--> | ||
− | <youtube width="600" height=" | + | <youtube width="600" height="340" right>3iU7rd7jLL0</youtube> |
+ | ([[Media:hpc.nrw_03_Introduction-Scoping.pdf | Slides as pdf]]) | ||
=== Quiz === <!--T:5--> | === Quiz === <!--T:5--> | ||
Line 25: | Line 26: | ||
{{hidden begin | {{hidden begin | ||
− | |title = 2. What is the data scoping of the variables <code>a</code>, <code>b</code>, <code>c</code> and <code>d</code> in the following code snippet in the parallel region? What is printed when executing the code? | + | |title = 2. What is the data scoping of the variables <code>a</code>, <code>b</code>, <code>c</code> and <code>d</code> in the following code snippet in the parallel region? What is printed when executing the code? <br> |
+ | <code>int a = 0;<br /> int b = 23;<br /> int c = -3;<br /> # pragma omp parallel num_threads(2) private(a) reduction(+:c)<br /> { </code> <div style="margin-left: 1em;"><code>int d = omp_get_thread_num();<br /> a = 42 + d;<br /> # pragma omp critical<br /> b = 1;<br /> c += a + b;</code></div> <code>} <br />c = c / 2;<br /> printf("a=%d, b=%d, c=%d\n", a, b, c) </code><br /> | ||
}} | }} | ||
<quiz display=simple> | <quiz display=simple> | ||
Line 31: | Line 33: | ||
|type="()"} | |type="()"} | ||
+ Click and submit to see the answer | + Click and submit to see the answer | ||
− | || | + | ||{{Note|'''a: private, b: shared, c: reduction, d: private '''<br />'''output: <code>a:0, b:1, c:42</code>'''}} |
</quiz> | </quiz> | ||
{{hidden end}} | {{hidden end}} | ||
− | {{hidden begin | + | {{hidden begin |
− | |title = 3. The following code snippet is wrong due to a missing data sharing attribute. How can you fix it? | + | |title = 3. The following code snippet is wrong due to a missing data sharing attribute. How can you fix it? <br> |
+ | <code> int i, int s = 0; <br /> # pragma omp parallel for <br /> for (i = 1; i < 100; i++){</code> <div style="margin-left: 1em;"><code> s = s + a[i];</code></div> <code>} </code> <br /> | ||
}} | }} | ||
<quiz display=simple> | <quiz display=simple> | ||
Line 42: | Line 45: | ||
|type="()"} | |type="()"} | ||
+ Click and submit to see the answer | + Click and submit to see the answer | ||
− | || | + | || {{Note|'''Add <code>reduction(+:s)</code> clause to the parallel for construct'''}} |
</quiz> | </quiz> | ||
{{hidden end}} | {{hidden end}} |
Latest revision as of 08:54, 18 January 2021
Tutorial | |
---|---|
Title: | OpenMP in Small Bites |
Provider: | HPC.NRW
|
Contact: | tutorials@hpc.nrw |
Type: | Multi-part video |
Topic Area: | Programming Paradigms |
License: | CC-BY-SA |
Syllabus
| |
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 | |
9. Tasking Example: Sudoku Solver | |
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.
Video
Quiz
1. What is the default data scoping of variable in a parallel region when this variable is declared before a parallel region?
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)
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];
}