b3cc concurrency 01 introduction
play

B3CC: Concurrency 01: Introduction Trevor L. McDonell Utrecht - PowerPoint PPT Presentation

B3CC: Concurrency 01: Introduction Trevor L. McDonell Utrecht University, B2 2020-2021 Today 1. Introduction 2. What is concurrency? 2 Course structure 3 henlo Trevor L. McDonell - BBG 5.68 MS Teams - t.l.mcdonell@uu.nl mention


  1. B3CC: Concurrency 01: Introduction Trevor L. McDonell Utrecht University, B2 2020-2021

  2. Today 1. Introduction 2. What is concurrency? 2

  3. Course structure 3

  4. henlo • Trevor L. McDonell - BBG 5.68 MS Teams - t.l.mcdonell@uu.nl 
 mention [INFOB3CC] in the subject • Ivo Gabe de Wol ff - BBG 5.70 MS Teams - i.g.dewolff@uu.nl ig: jasper.samoyed 4

  5. Teaching assistants • Bart Wijgers • Oscar Fickel • Erwin Glazenburg • Kevin Quach • Karel Kubat • Hugo Peters • Frans Haak 5

  6. Human malware • Online teaching - Lectures (online, recorded) - Werkcollege (online & in person) - Online exam • Send us feedback! - Anonymous survey(s) on blackboard https://imgur.com/gallery/ooZcL1b 6

  7. Coffeebot • Add your name to the co ff ee bot for some social interaction! - You’ll get an email once per week with the details of somebody else to have a virtual coffee with ig: sarahandersencomics 7

  8. Topics • Program a multithreaded application - Managing threads - Synchronise with locks, etc. - Software transactional memory - Parallelism • Work & span • Concurrent algorithms / data structures 8

  9. 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 9

  10. Haskell https://xkcd.com/1312 10

  11. Homepage • http://www.cs.uu.nl/docs/vakken/b3cc/ - Feel free to let me know if there are broken links, missing slides, etc. 11

  12. Resources • Parallel and Concurrent Programming in Haskell 
 https://simonmar.github.io/pages/pcph.html • Many more on the website 
 http://www.cs.uu.nl/docs/vakken/b3cc/ resources.html 12

  13. Sessions • Lectures: - Mon: 15:15 - 17:00 (online) - Thu: 11:00 - 12:45 (online) • Werkcollege: - Tue: 15:15 - 17:00 (in person & online) - Thu 09:00 - 10:45 (online) • Participation in lectures is expected (please ask questions!) 13

  14. Course components If you wait until the last minute, it only takes a minute to do. — Key Skills for Professionals , 2013, pp65 • Exam (50%) - Mid session exam: 17-12-2020 (50%) (Online) - Final exam: 04-02-2021 (50%) (Online) • Practicals (50%) (exact dates may change!) - Assignment 1: 28-11-2020 (individual) (20%) - Assignment 2: 19-12-2020 (in pairs) (40%) - Assignment 3: 06-02-2021 (in pairs) (40%) 14

  15. Software installation • A recent version of GHC • I recommend using stack - https://docs.haskellstack.org/en/stable/README/ - The starting frameworks will use stack 15

  16. Concurrency control 16

  17. 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 17

  18. 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 18

  19. 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 19

  20. Concurrency vs. Parallelism • Concurrency: composition of independently executing processes • Parallelism: simultaneous execution of (possibly related) computations 20

  21. 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 21

  22. 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 di ff erent forms: - Multiple applications (multiprogramming) - Structured applications (applications is a set of threads/processes) - Operating system structure (OS is a set of threads/processes) 22

  23. 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) 23

  24. Example: access to a global queue head last • Inserting: 24

  25. Example: access to a global queue head last • Inserting: - Create new object 25

  26. Example: access to a global queue head last • Inserting: - Create new object - Set last ->. next to &new 26

  27. Example: access to a global queue head last • Inserting: - Create new object - Set last ->. next to &new - Set last to &new 27

  28. Example: concurrent access to a global queue head last • Thread A: • Thread B: 28

  29. Example: concurrent access to a global queue head last • Thread A: • Thread B: - Create new object 29

  30. Example: concurrent access to a global queue head last • Thread A: • Thread B: - Create new object - Set last ->. next to &new 30

  31. Example: concurrent access to a global queue head last • Thread A: • Thread B: - Create new object - Set last ->. next to &new - Create new object 31

  32. Example: concurrent access to a global queue head last • Thread A: • Thread B: - Create new object - Set last ->. next to &new - Create new object - Set last ->. next to &new 32

  33. Example: concurrent access to a global queue head last • Thread A: • Thread B: - Create new object - Set last ->. next to &new - Create new object - Set last ->. next to &new - Set last to &new 33

  34. Example: concurrent access to a global queue head last • Thread A: • Thread B: - Create new object - Set last ->. next to &new - Create new object - Set last ->. next to &new - Set last to &new - Set last to &new 34

  35. Example: concurrent access to a global queue head last • 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 35

  36. 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 36

  37. 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 cooperate 37

  38. 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 38

  39. 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 39

  40. 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 40

Recommend


More recommend