diff --git a/lectures/parallelism/slides.qmd b/lectures/parallelism/slides.qmd index ce4f7891a134eac04c71f5dccf67fadf0efbf38a..ace2472e83228ee4c2f04552636f6cf04a16245d 100644 --- a/lectures/parallelism/slides.qmd +++ b/lectures/parallelism/slides.qmd @@ -63,7 +63,7 @@ FIXME * OpenMP uses something called threads * Wait until next week for a definition -```c +```c++ #pragma omp parallel for for (int i = 0; i < N; ++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 # Reductions FIXME title should be more generic ## What is happening here? -```c +```c++ int a[] = {2, 4, 6}; for (int i = 0; i < N; ++i) sum = sum + a[i]; ``` ## What is happening here? -```c +```c++ int a[] = {2, 4, 6}; #pragma omp parallel for 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 [comment]: # (Can something go wrong?) ## Solution -```c +```c++ int a[] = {2, 4, 6}; #pragma omp parallel for reduction(+:sum) 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 # Doing stuff wrong ## What is going wrong here? -```c +```c++ temp = 0; #pragma omp parallel for 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 } ``` ## Solution -```c +```c++ temp = 0; #pragma omp parallel for private(temp) for (int i = 0; i < N; ++i) { @@ -212,8 +212,16 @@ FIXME: Citation correct?! ## Precondition for parallel execution +<br> + *"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} @@ -222,7 +230,7 @@ FIXME: Citation correct?! ::: :::{.fragment .fade-in-then-semi-out fragment-index=2} -```c +```c++ a = b; c = a + b; // flow dependence ``` @@ -233,7 +241,7 @@ c = a + b; // flow dependence ::: :::{.fragment .fade-in-then-semi-out fragment-index=4} -```c +```c++ for (int i = 1; i < n ; i++) { a[i] = (a[i-1] > a[i]) ? a[i] + 1 : 1; } @@ -245,7 +253,7 @@ for (int i = 1; i < n ; i++) { ::: :::{.fragment .fade-in-then-semi-out fragment-index=6} -```c +```c++ a = b; 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"*. :::{.fragment} -```c +```c++ c = a + b; // S1 d = a - b; // S2 ``` @@ -302,7 +310,7 @@ S1 and S2 can be executed in parallel! :::{.notes} How about these two? replace a in S2 with c -```c +```c++ c = a + b; // S1 d = c - b; // S2 ```