Skip to content
Snippets Groups Projects
Commit 40cc7307 authored by Georgiana Mania's avatar Georgiana Mania
Browse files

use Dominik's coding icon for C++

parent e30cf802
No related branches found
No related tags found
1 merge request!72Draft: Compute devices lecture
...@@ -63,7 +63,7 @@ FIXME ...@@ -63,7 +63,7 @@ FIXME
* OpenMP uses something called threads * OpenMP uses something called threads
* Wait until next week for a definition * Wait until next week for a definition
```c ```c++
#pragma omp parallel for #pragma omp parallel for
for (int i = 0; i < N; ++i) for (int i = 0; i < N; ++i)
a[i] = 2 * i; a[i] = 2 * i;
...@@ -149,13 +149,13 @@ Starting with batter for $N$ pancakes and 1 pan, we can scale by using $P$ pans ...@@ -149,13 +149,13 @@ Starting with batter for $N$ pancakes and 1 pan, we can scale by using $P$ pans
# Reductions FIXME title should be more generic # Reductions FIXME title should be more generic
## What is happening here? ## What is happening here?
```c ```c++
int a[] = {2, 4, 6}; int a[] = {2, 4, 6};
for (int i = 0; i < N; ++i) for (int i = 0; i < N; ++i)
sum = sum + a[i]; sum = sum + a[i];
``` ```
## What is happening here? ## What is happening here?
```c ```c++
int a[] = {2, 4, 6}; int a[] = {2, 4, 6};
#pragma omp parallel for #pragma omp parallel for
for (int i = 0; i < N; ++i) for (int i = 0; i < N; ++i)
...@@ -164,7 +164,7 @@ Starting with batter for $N$ pancakes and 1 pan, we can scale by using $P$ pans ...@@ -164,7 +164,7 @@ Starting with batter for $N$ pancakes and 1 pan, we can scale by using $P$ pans
[comment]: # (Can something go wrong?) [comment]: # (Can something go wrong?)
## Solution ## Solution
```c ```c++
int a[] = {2, 4, 6}; int a[] = {2, 4, 6};
#pragma omp parallel for reduction(+:sum) #pragma omp parallel for reduction(+:sum)
for (int i = 0; i < N; ++i) for (int i = 0; i < N; ++i)
...@@ -173,7 +173,7 @@ Starting with batter for $N$ pancakes and 1 pan, we can scale by using $P$ pans ...@@ -173,7 +173,7 @@ Starting with batter for $N$ pancakes and 1 pan, we can scale by using $P$ pans
# Doing stuff wrong # Doing stuff wrong
## What is going wrong here? ## What is going wrong here?
```c ```c++
temp = 0; temp = 0;
#pragma omp parallel for #pragma omp parallel for
for (int i = 0; i < N; ++i) { for (int i = 0; i < N; ++i) {
...@@ -182,7 +182,7 @@ Starting with batter for $N$ pancakes and 1 pan, we can scale by using $P$ pans ...@@ -182,7 +182,7 @@ Starting with batter for $N$ pancakes and 1 pan, we can scale by using $P$ pans
} }
``` ```
## Solution ## Solution
```c ```c++
temp = 0; temp = 0;
#pragma omp parallel for private(temp) #pragma omp parallel for private(temp)
for (int i = 0; i < N; ++i) { for (int i = 0; i < N; ++i) {
...@@ -212,8 +212,16 @@ FIXME: Citation correct?! ...@@ -212,8 +212,16 @@ FIXME: Citation correct?!
## Precondition for parallel execution ## Precondition for parallel execution
<br>
*"Two consecutive instructions or code segments can be executed in parallel if they are **independent**."* *"Two consecutive instructions or code segments can be executed in parallel if they are **independent**."*
<br>
:::{.fragment .info .smaller}
What does **dependence** mean?
:::
## Code and data dependence {.leftalign} ## Code and data dependence {.leftalign}
...@@ -222,7 +230,7 @@ FIXME: Citation correct?! ...@@ -222,7 +230,7 @@ FIXME: Citation correct?!
::: :::
:::{.fragment .fade-in-then-semi-out fragment-index=2} :::{.fragment .fade-in-then-semi-out fragment-index=2}
```c ```c++
a = b; a = b;
c = a + b; // flow dependence c = a + b; // flow dependence
``` ```
...@@ -233,7 +241,7 @@ c = a + b; // flow dependence ...@@ -233,7 +241,7 @@ c = a + b; // flow dependence
::: :::
:::{.fragment .fade-in-then-semi-out fragment-index=4} :::{.fragment .fade-in-then-semi-out fragment-index=4}
```c ```c++
for (int i = 1; i < n ; i++) { for (int i = 1; i < n ; i++) {
a[i] = (a[i-1] > a[i]) ? a[i] + 1 : 1; a[i] = (a[i-1] > a[i]) ? a[i] + 1 : 1;
} }
...@@ -245,7 +253,7 @@ for (int i = 1; i < n ; i++) { ...@@ -245,7 +253,7 @@ for (int i = 1; i < n ; i++) {
::: :::
:::{.fragment .fade-in-then-semi-out fragment-index=6} :::{.fragment .fade-in-then-semi-out fragment-index=6}
```c ```c++
a = b; a = b;
b = 42; // read after write: a has an old value b = 42; // read after write: a has an old value
``` ```
...@@ -257,7 +265,7 @@ b = 42; // read after write: a has an old value ...@@ -257,7 +265,7 @@ b = 42; // read after write: a has an old value
For data dependence, use the Bernstein's conditions: *"the intersection between read-write set, write-read set and write-write set of instructions is null"*. For data dependence, use the Bernstein's conditions: *"the intersection between read-write set, write-read set and write-write set of instructions is null"*.
:::{.fragment} :::{.fragment}
```c ```c++
c = a + b; // S1 c = a + b; // S1
d = a - b; // S2 d = a - b; // S2
``` ```
...@@ -302,7 +310,7 @@ S1 and S2 can be executed in parallel! ...@@ -302,7 +310,7 @@ S1 and S2 can be executed in parallel!
:::{.notes} :::{.notes}
How about these two? replace a in S2 with c How about these two? replace a in S2 with c
```c ```c++
c = a + b; // S1 c = a + b; // S1
d = c - b; // S2 d = c - b; // S2
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment