Upload
others
View
27
Download
0
Embed Size (px)
Citation preview
henlo
• Trevor L. McDonell
- BBG 5.68
- [email protected] mention [INFOB3CC] in the subject
• Office hours (may change)
- Mon 13:00 - 15:00
4ig: jasper.samoyed
Topics
• Program a multithreaded application
- Managing threads
- Synchronise with locks, etc.
- Software transactional memory
- Parallelism
• Work & span
• Concurrent algorithms / data structures
6
Goals
• By the end of the course you should be able to:
- Design and implement a multithreaded application
- Understand the difference between concurrency and parallelism
- Reason about the properties/complexity of parallel algorithms
7
Homepage
• http://www.cs.uu.nl/docs/vakken/b3cc/
- Feel free to let me know if there are broken links, missing slides, etc.
9
Resources
• Parallel and Concurrent Programming in Haskellhttps://simonmar.github.io/pages/pcph.html
• #infob3cc slack channel
• Many more on the website http://www.cs.uu.nl/docs/vakken/b3cc/resources.html
10
Sessions
• Lectures:
- Tue: 13:15 - 15:00 (KBG Cosmos)
- Thu: 9:00 - 10:45 (KBG Cosmos)
• Work collage:
- Tue: 15:15 - 17:00
- Thu: 11:00 - 12:45
• Participation in lectures is expected (please ask questions!)
11
Course components
• Exam (50%)
- Mid session exam: 19-12-2019 (40%) (EDUC Gamma 8:30 - 10:30)
- Final exam: 30-01-2020 (60%) (EDUC Gamma 8:30 - 10:30)
• Practicals (50%)
- Assignment 1: 30-11-2019 (individual) (20%)
- Assignment 2: 21-12-2019 (may change!) (in pairs) (40%)
- Assignment 3: 01-02-2020 (may change!) (in pairs) (40%)
12
If you wait until the last minute,
it only takes a minute to do.
— Key Skills for Professionals, 2013, pp65
Software installation
• A recent version of GHC
• I recommend using stack
- https://docs.haskellstack.org/en/stable/README/
13
What is concurrency?
• Consider multiple tasks being executed by the computer…
- Tasks are concurrent with respect to each other if:
• They may be executed out-of-order
• Implies they can be executed at the same time, but this is not required
- Concurrency: deal with lots of things at once
15
What is parallelism?
• Consider multiple tasks being executed by the computer…
- Tasks are parallel if they are executed simultaneously:
• Requires multiple processing elements
• The primary motivation for parallel programming is to reduce the overall running time (wall clock) of the program: parallel execution
- Parallelism: do lots of things at once
16
Question
• What does it mean for an application to be concurrent but not parallel?
- Give an example
• What does it mean for an application to be parallel but not concurrent?
- Give an example
17
Concurrency vs. Parallelism
• Concurrency: composition of independently executing processes
• Parallelism: simultaneous execution of (possibly related) computations
18
Concurrency
• Programming with multiple threads of control
- A tool for structuring programs with multiple interactions
- Examples: GUI, web server, different tasks in a game engine loop, …
• There is no single right answer. In this course we will discuss several approaches: it is up to you to pick which is right for your application
19
Concurrency control
• Concurrency appears in many contexts:
- Multithreading: concurrent threads share an address space
- Multiprogramming: concurrent processes execute on a uniprocessor
- Multiprocessing: concurrent processes on a multiprocessor
- Distributed processing: concurrent processes executing on multiple nodes connected by a network
• Concurrency is also used in different forms:
- Multiple applications (multiprogramming)
- Structured applications (applications is a set of threads/processes)
- Operating system structure (OS is a set of threads/processes)20
Concurrency control
• Concurrent processes (threads) need special support
- Communication among processes
- Allocation of processor time
- Sharing of resources
- Synchronisation of multiple processes
• Concurrency can be dangerous to the unwary programmer:
- Sharing global resources (order of read & write operations)
- Management of allocation of resources (danger of deadlock)
- Programming errors are difficult to locate (Heisenbugs)21
Example: access to a global queue
• Inserting:
- Create new object
- Set last!->next to &new
24
head last
Example: access to a global queue
• Inserting:
- Create new object
- Set last!->next to &new
- Set last to &new
25
head last
Example: concurrent access to a global queue
• Thread A:
- Create new object
• Thread B:
27
head last
Example: concurrent access to a global queue
• Thread A:
- Create new object- Set last!->next to &new
• Thread B:
28
head last
Example: concurrent access to a global queue
• Thread A:
- Create new object- Set last!->next to &new
• Thread B:
- Create new object
29
head last
Example: concurrent access to a global queue
• Thread A:
- Create new object- Set last!->next to &new
• Thread B:
- Create new object- Set last!->next to &new
30
head last
Example: concurrent access to a global queue
• Thread A:
- Create new object- Set last!->next to &new
• Thread B:
- Create new object- Set last!->next to &new- Set last to &new
31
head last
Example: concurrent access to a global queue
• Thread A:
- Create new object- Set last!->next to &new
- Set last to &new
• Thread B:
- Create new object- Set last!->next to &new- Set last to &new
32
head last
Example: concurrent access to a global queue
• Lessons learned
- We have to control access to shared resources (such as shared variables)
- We can do this by controlling access to the code utilising those shared resources: critical sections
33
head last
Example: concurrent access to a global queue
• Only one thread at a time should have access to the queue:
- Thread A creates a new object, sets last!->next pointer
- Thread A is suspended
- Thread B is scheduled: since Thread A is currently in insert, has to wait
- Thread A is resumed, the data structure is in the same state as it was when it was suspended
- Thread A completes operation
- Thread B is allowed to execute insert
34
Concurrency control
• Processes can
- Compete for resources
• Processes may not be aware of each other
• Execute must not be affected by each other
• OS is responsible for controlling access
- Cooperate by sharing a common resource
• Programmer responsible for controlling access
• Hardware / OS / programming language may provide support
• Threads of a process usually do not compete, but cooperate35
Concurrency control
• We face three control problems:
- Mutual exclusion: critical resources !=> critical sections
• Only one process at a time is allowed in a critical section
• e.g. only one process at a time is allowed to send commands to the GPU
- Deadlock: e.g. two processes and two resources
- Starvation: e.g. three processes compete for a single resource
36
Requirements for mutual exclusion
• Implementation:
- Only one thread at a time is allowed in the critical section for a resource
- No deadlock or starvation
- A thread must not be delayed access to a critical section when there is no other thread using it
- A thread that halts in its non-critical section must do so without interfering with other threads
- No assumptions made about relative thread speed or number of processes
• Usage
- A thread remains inside its critical section for a finite time only
- No potentially blocking operations should be executed inside the critical section
- No deadlock or starvation 37
Mutual exclusion
• Three ways to satisfy the implementation requirements:
- Software approach: put responsibility on the processes themselves
- Systems approach: provide support within the OS or programming language
- Hardware approach: special-purpose machine instructions
38
Therac-25
• Computer controlled medical radiation device (1982)
- Two operating modes: a low-current electron beam; or high-energy x-rays
• Involved in at least six incidents, resulting in serious injury or death
- A race condition could cause the high-power electron beam to be administered directly to the patient
- Resulted in radiation doses 100x higher than normal
- Additional problems related to poor software development practices
39https://en.wikipedia.org/wiki/Therac-25
Northeast blackout (2003)
• Widespread power outage throughout USA and Canada
- Second most widespread blackout in history (at the time)
- Affected an estimated 10M people in Ontario and 45M in 8 US states
- A race condition prevented an alarm from going off
- Operators were unaware of the need to redistribute power—a minor problem—which cascaded into complete collapse of the electrical grid
40https://en.wikipedia.org/wiki/Northeast_blackout_of_2003
Mars Pathfinder
• Launched by NASA in 1996
• Sojourner became the first rover to operate outside the Earth-Moon system
• Control computer contained a priority inversion bug
- Triggered under certain high loads causing a system reset
- Successfully patched remotely
41https://en.wikipedia.org/wiki/Mars_Pathfinder
Photo by Jf Brou
tot ziens