Slides Cours9 Multithreading
Slides Cours9 Multithreading
2023-11-30
Sequential programming
Other computers
(distributed computing)
GPUs
CPUs
Image credits:
https://www.geeksforgeeks.org/an-overview-of-cluster-computing/
https://insights.profitap.com/osi-7-layers-explained-the-easy-way
Distributed computing
Image credits:
https://www.geeksforgeeks.org/an-overview-of-cluster-computing/
https://insights.profitap.com/osi-7-layers-explained-the-easy-way
GPU programming
Graphics rendering
Matrix operations
Numerical simulations
Deep learning! Massive parallelization of simple computations
GPU programming
Graphics rendering
Matrix operations
Numerical simulations
Deep learning! Massive parallelization of simple computations
Speed up computations!
Why parallel programming on CPUs?
Network/cloud applications
Responsive user interfaces
Speed up computations!
Threads
Threads
Difference 1:
Processes live in isolation
Threads share memory
Process Process Process Process
Processes vs. threads
Threads
Difference 1:
Processes live in isolation
Threads share memory
Process Process Process Process
Difference 2:
Threads are more lightweight
Processes vs. threads
Threads
Difference 1:
Processes live in isolation
Threads share memory
Process Process Process Process
Difference 2:
Threads are more lightweight
Deadlocks
Synchronization
Threads in Java
Threads in Java
Sequential algorithm
Running it as a background task
Running it as a background task
User interface is
still responsive!
Speeding up a computation
Speeding up a computation
Additional information
to be given to the threads
Math.min()
What if 0 or 1 element in the array?
What if 0 or 1 element in the array?
Before:
Combining partial results
Combining partial results
Frequent pattern of multithreading on arrays
If not divisible by 4
Thread pools
Motivation
We could continue adding more threads in this way (for instance, 8, 16, 32…).
But if we use, say, 100 threads, does that mean that our program will run 100 faster?
Motivation
We could continue adding more threads in this way (for instance, 8, 16, 32…).
But if we use, say, 100 threads, does that mean that our program will run 100 faster?
Obviously, the level of parallelism is limited by
the number of CPU cores. If using a CPU with
4 cores, you cannot expect a speed up of more
than 4.
There is an overhead associated with the
creation and management of a thread. On a
modern computer, creating a simple thread
(without any extra object) takes around 0.05-0.1
ms. That is approximately the time to calculate
the sum from 1 to 100,000.
A thread pool is a group of threads that are ready to work
Creating a thread pool in Java
Creating a thread pool in Java
At the end of the program or when you don’t need the thread pool anymore, you have to
shut it down explicitly to stop all its threads, otherwise the software might not properly exit:
Thread pools handle tasks producing a result
Thread pools are waiting for tasks that produce results of a given type T!
This contrasts with Runnable that does not produce a return value.
Submitting tasks to a thread pool
Threads in thread pool are like chefs in the kitchen of a restaurant waiting for
orders. If you submit one task to the pool using the call above, one of the chefs will
take the task and it will immediately start working on it.
These new tasks might have to wait until one chef is ready!
Accessing the results of a thread pool
If the task is not finished yet, the method get() will wait.
This contrast with the executor.submit() method that always returns immediately.
Back to the minimum/maximum example
Back to the minimum/maximum example
Using the thread pool
Dividing the array into multiple blocks
Dividing the array into multiple blocks
Thread 1 Thread 2
With 2 threads, the following sequence can happen: Read counter
Read counter
Add 1
Add 1
Store
Store
This is a race condition!
Thread 1 Thread 2
With 2 threads, the following sequence can happen: Read counter
Read counter
Add 1
Add 1
Store
Store
Thread 1 overwrites change
made by thread 2
Mutual exclusion in Java
We must prevent that a thread changes a variable or an object while another thread
tries to use (or change) it.
Each Java object is associated with a monitor. Only the thread that holds the monitor
can enter the method.
If another thread wants the monitor, it must wait until the monitor is free. This is mutual
exclusion.
Methods are protected by the monitor if they have the synchronized keyword.
The synchronized keyword also ensure visibility of modifications between threads.
Fixing the result data structure
Final warning