Skip to content

Commit

Permalink
Added phase 6 assignment and lesson 8
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Greenburg committed Oct 16, 2023
1 parent add866d commit 69572ca
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lessons.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

### [7: Concurrency, Parallelism, and Threading](lessons/7.md)

<!---
### [8: Threading in C++](lessons/8.md)

<!---
### [9: Blocking and Non-Blocking Communication](lessons/8-communication.md)
### [10: Distributed Programming and MPI](lessons/10-mpi.md)
Expand Down
17 changes: 17 additions & 0 deletions lessons/8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Threading in C++

In this lesson you'll learn how to use OpenMP and C++ threads to create shared memory parallel programs in C++

## Study guide

- Know how to write parallel C++ programs using OpenMP and C++ threads

## Readings and Assignments

[Reading: OpenMP Threading](../readings/openmp.md)

[Reading: C++ Threading](../readings/jthread.md)

[Project: C++ Threading](../project/phase6.md)

[Quiz: Concurrency, Parallelism, and Threading](https://byu.instructure.com/courses/21221/quizzes)
2 changes: 1 addition & 1 deletion project/phase3.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ This phase is worth 20 points. The following deductions, up to 20 points total,
| `optimize` isn't relevant to the assignment, or just prints "5.39" or similar | 8 points |
| The informal essay doesn't answer the bolded questions above, or the answers are nonsensical | 1-8 points |

An extra credit point will be awarded to submissions that run in less than 0.1 seconds with 8 threads on `m9`.
An extra credit point will be awarded if `optimize` runs correctly in less than 0.1 seconds with 8 threads on `m9`.
26 changes: 26 additions & 0 deletions project/phase6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
---

# Phase 6: C++ Threads

In this assignment you'll parallelize your solver with [`std::jthread`s](../readings/jthread.md). You can use any threading paradigm you'd like (e.g. pushing work onto a [shared queue](https://github.com/cameron314/concurrentqueue) instead of following the [example code](https://github.com/BYUHPC/sci-comp-course-example-cxx/blob/main/src/MountainRangeThreaded.hpp)), but the number of threads spawned must be a low, constant number regardless of simulation time--you may *not* spawn threads on each iteration. Functionality and performance requirements are the same as they were for the [first optimization assignment](phase3.md)--given 8 threads on a whole `m9` node, `wavesolve_thread` must run in 20 seconds on `2d-medium-in.wo`, and checkpointing must still work.



## Submission

Update your `CMakeLists.txt` to create `wavesolve_thread` (making sure to [compile with C++ threads](../readings/jthread.md#compiling-with-c-threads)), tag the commit you'd like me to grade from `phase6`, and push it.



## Grading

This phase is worth 20 points. The following deductions, up to 20 points total, will apply for a program that doesn't work as laid out by the spec:

| Defect | Deduction |
| --- | --- |
| Failure to compile `wavesolve_thread` | 4 points |
| Failure of `wavesolve_thread` to work on each of 3 test files | 4 points each |
| Failure of `wavesolve_thread` to checkpoint correctly | 4 points |
| Failure of `wavesolve_thread` to run on `2d-medium-in.wo` on `m9` with 8 threads in 20 seconds | 4 points |
| ...in 60 seconds | 4 points |
6 changes: 3 additions & 3 deletions readings/jthread.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ target_link_libraries(blah PRIVATE Threads::Threads)

int main() {
// Latin numerals print in a spawned thread
auto latin_numeral_thread = std::jthread([]{ // using lambdas to spawn threads is the convention.
auto latin_numeral_thread = std::jthread([]{ // using lambdas to spawn threads is idiomatic
for (auto &numeral: {"I", "II", "III", "IV", "V"}) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
std::cout << numeral << std::endl;
Expand All @@ -53,7 +53,7 @@ int main() {
*/
```

This is simple enough when no coordination is required, or when clock-based coordination is sufficient as above, but getting threads to synchronize or communicate correctly is challenging. Even getting two threads to interleave the printing of "PING" and "PONG" requires 3 variables dedicated solely to coordination:
This is simple enough when no coordination is required, or when clock-based coordination is sufficient as above, but getting threads to synchronize or communicate correctly is challenging. Even getting two threads to interleave the printing of "PING" and "PONG" below requires 3 variables dedicated solely to coordination:

```c++
#include <iostream>
Expand Down Expand Up @@ -119,7 +119,7 @@ int main() {
}
```

### C++ Mutexes
### C++ Mutexes and Semaphores

[`std::mutex`](https://en.cppreference.com/w/cpp/thread/mutex) and [`std::counting_semaphore`](https://en.cppreference.com/w/cpp/thread/counting_semaphore) provide [mutexes and sempahores](threading.md#mutexes-and-semaphores) in C++. It's best to [use](#using-c-threads) [`std::unique_lock`](https://en.cppreference.com/w/cpp/thread/unique_lock) and [`std::lock_guard`](https://en.cppreference.com/w/cpp/thread/lock_guard) rather than locking and unlocking mutexes manually.

Expand Down

0 comments on commit 69572ca

Please sign in to comment.