Difference between revisions of "OpenMP in Small Bites/Tasking and Data Scoping"

From HPC Wiki
OpenMP in Small Bites/Tasking and Data Scoping
Jump to navigation Jump to search
m (Adapt aspect ration of video embedding)
Line 8: Line 8:
  
 
== Quiz ==
 
== Quiz ==
 +
{{hidden begin
 +
|title = 1. Consider the following code snippet
 +
 +
<code>int x = 42;</code><br/>
 +
<code>#pragma omp parallel private(x)</code><br/>
 +
<code>{</code><br/>
 +
<code>  #pragma omp task</code><br/>
 +
<code>  {</code><br/>
 +
<code>    x = 3;</code><br/>
 +
<code>  }</code><br/>
 +
<code>}</code><br/>
 +
<code>printf("x=%d\n", x);</code><br/>
 +
 +
What is the data scope of <code>x</code> and what is printed?
 +
}}
 +
<quiz display=simple>
 +
{
 +
|type="()"}
 +
+ <code>firstprivate</code>, x=42
 +
- <code>firstprivate</code>, x=3
 +
- <code>shared</code>, x=3
 +
</quiz>
 +
{{hidden end}}
 +
 +
{{hidden begin
 +
|title = 2. Consider the following code snippet
 +
 +
<code>int x = 42;</code><br/>
 +
<code>int y = 0;</code><br/>
 +
<code>#pragma omp parallel num_threads(4)</code><br/>
 +
<code>{</code><br/>
 +
<code>  #pragma omp task</code><br/>
 +
<code>  {</code><br/>
 +
<code>    y += x;</code><br/>
 +
<code>  }</code><br/>
 +
<code>}</code><br/>
 +
<code>printf("y=%d\n", y);</code><br/>
 +
 +
What is the data scope of <code>x</code> and what is printed?
 +
 +
}}
 +
<quiz display=simple>
 +
{
 +
|type="()"}
 +
- <code>shared</code>, y=42
 +
+ <code>shared</code>, y=168
 +
- <code>firstprivate</code>, y=168
 +
</quiz>
 +
{{hidden end}}
 +
 +
{{hidden begin
 +
|title = 3. Consider the following code snippet
 +
 +
<code>int x = 42;</code><br/>
 +
<code>int y = 0;</code><br/>
 +
<code>#pragma omp parallel num_threads(4)</code><br/>
 +
<code>{</code><br/>
 +
<code>  #pragma omp single</code><br/>
 +
<code>  {</code><br/>
 +
<code>    #pragma omp task</code><br/>
 +
<code>    {</code><br/>
 +
<code>      y += x;</code><br/>
 +
<code>    }</code><br/>
 +
<code>  }</code><br/>
 +
<code>}</code><br/>
 +
<code>printf("y=%d\n", y);</code><br/>
 +
 +
What is the data scope of <code>x</code> and what is printed?
 +
}}
 +
<quiz display=simple>
 +
{
 +
|type="()"}
 +
+ <code>shared</code>, y=42
 +
- <code>shared</code>, y=168
 +
- <code>firstprivate</code>, y=168
 +
</quiz>
 +
{{hidden end}}

Revision as of 17:59, 30 November 2020

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

Video

Quiz

1. Consider the following code snippet

int x = 42;
#pragma omp parallel private(x)
{
#pragma omp task
{
x = 3;
}
}
printf("x=%d\n", x);

What is the data scope of x and what is printed?

firstprivate, x=42
firstprivate, x=3
shared, x=3

2. Consider the following code snippet

int x = 42;
int y = 0;
#pragma omp parallel num_threads(4)
{
#pragma omp task
{
y += x;
}
}
printf("y=%d\n", y);

What is the data scope of x and what is printed?

shared, y=42
shared, y=168
firstprivate, y=168

3. Consider the following code snippet

int x = 42;
int y = 0;
#pragma omp parallel num_threads(4)
{
#pragma omp single
{
#pragma omp task
{
y += x;
}
}
}
printf("y=%d\n", y);

What is the data scope of x and what is printed?

shared, y=42
shared, y=168
firstprivate, y=168