Difference between revisions of "OpenMP in Small Bites/Scoping"

From HPC Wiki
OpenMP in Small Bites/Scoping
Jump to navigation Jump to search
(Created page with "{{Infobox OpenMP in Small Bites}}")
 
Line 1: Line 1:
 +
[[Category:Tutorials]]
 +
 +
__TOC__
 +
 
{{Infobox OpenMP in Small Bites}}
 
{{Infobox OpenMP in Small Bites}}
 +
 +
 +
=== Video === <!--T:5-->
 +
 +
<youtube width="600" height="400" right>IfD9IPixgpo</youtube>
 +
 +
 +
=== Quiz === <!--T:5--> 
 +
 +
{{hidden begin
 +
|title = 1. What is the default data scoping of variable in a parallel region when this variable is declared before a parallel region?
 +
}}
 +
<quiz display=simple>
 +
{
 +
|type="()"}
 +
+ Click and submit to see the answer
 +
|| shared
 +
</quiz>
 +
{{hidden end}}
 +
 +
{{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?
 +
}}
 +
<quiz display=simple>
 +
{
 +
|type="()"}
 +
+ Click and submit to see the answer
 +
|| <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 /> {{Note|'''a: shared, b: private, c: firstprivate (due to the reduction clause), d: private <br />output: a=0, b=1, c=42'''}}
 +
</quiz>
 +
{{hidden end}}
 +
 +
{{hidden begin
 +
|title = 3. The following code snippet is wrong due to a missing data sharing attribute. How can you fix it?
 +
}}
 +
<quiz display=simple>
 +
{
 +
|type="()"}
 +
+ Click and submit to see the answer
 +
|| <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 /> {{Note|'''Add <code>reduction(+:s)</code> clause to the parallel for construct'''}}
 +
</quiz>
 +
{{hidden end}}

Revision as of 18:39, 30 October 2020



HPC.NRW
HPC.NRW
Other HPC Courses
1. Gprof Tutorial
2. Introduction to Linux in HPC
OpenMP in Small Bites
1. Overview
2. Worksharing
3. Data Scoping
4. Non-Uniform Memory Access







Video


Quiz

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?

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?

Click and submit to see the answer