# Tasking and Data Scoping (OpenMP)

OpenMP in Small Bites/Tasking and Data Scoping

Tutorial
Title: OpenMP in Small Bites
Provider: HPC.NRW

Contact: tutorials@hpc.nrw
Type: Multi-part video
Syllabus

1. Overview
2. Worksharing
3. Data Scoping
4. False Sharing
11. Non-Uniform Memory Access

## Contents

This video deepens the knowledge of OpenMP Tasking and Data Scoping by using an example which includes typical scenarios. Furthermore, aspects of the lifetime of a variable are discussed.

## 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` in the task region and what is printed at the end?

 `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`
` {`
` #pragma omp critical`
` {`
` y += x;`
` }`
` }`

`}`
`printf("y=%d\n", y);`

What is the data scope of `x` in the task region and what is printed out at the end?

 `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`
` {`
` #pragma omp critical`
` {`
` y += x;`
` }`
` }`
` }`

`}`
`printf("y=%d\n", y);`

What is the data scope of `x` in the task region and what is printed out at the end?

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