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

b3cc concurrency 01 introduction
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

B3CC: Concurrency 01: Introduction

Trevor L. McDonell Utrecht University, B2 2020-2021

slide-2
SLIDE 2

Today

  • 1. Introduction
  • 2. What is concurrency?

2

slide-3
SLIDE 3

Course structure

3

slide-4
SLIDE 4

henlo

  • Trevor L. McDonell
  • BBG 5.68 MS Teams
  • t.l.mcdonell@uu.nl


mention [INFOB3CC] in the subject

  • Ivo Gabe de Wolff
  • BBG 5.70 MS Teams
  • i.g.dewolff@uu.nl

4

ig: jasper.samoyed

slide-5
SLIDE 5

Teaching assistants

  • Bart Wijgers
  • Oscar Fickel
  • Erwin Glazenburg
  • Kevin Quach
  • Karel Kubat
  • Hugo Peters
  • Frans Haak

5

slide-6
SLIDE 6
  • Online teaching
  • Lectures (online, recorded)
  • Werkcollege (online & in person)
  • Online exam
  • Send us feedback!
  • Anonymous survey(s) on blackboard

Human malware

6

https://imgur.com/gallery/ooZcL1b

slide-7
SLIDE 7

Coffeebot

  • Add your name to the coffee bot for some social

interaction!

  • You’ll get an email once per week with the details
  • f somebody else to have a virtual coffee with

7

ig: sarahandersencomics

slide-8
SLIDE 8

Topics

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

8

slide-9
SLIDE 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

slide-10
SLIDE 10

Haskell

10

https://xkcd.com/1312

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 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

slide-14
SLIDE 14

Course components

  • 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

If you wait until the last minute, it only takes a minute to do. — Key Skills for Professionals, 2013, pp65

slide-15
SLIDE 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

slide-16
SLIDE 16

Concurrency control

16

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 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

slide-20
SLIDE 20

Concurrency vs. Parallelism

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

20

slide-21
SLIDE 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

slide-22
SLIDE 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 different forms:
  • Multiple applications (multiprogramming)
  • Structured applications (applications is a set of threads/processes)
  • Operating system structure (OS is a set of threads/processes)

22

slide-23
SLIDE 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

slide-24
SLIDE 24

Example: access to a global queue

  • Inserting:

24

head last

slide-25
SLIDE 25

Example: access to a global queue

  • Inserting:
  • Create new object

25

head last

slide-26
SLIDE 26

Example: access to a global queue

  • Inserting:
  • Create new object
  • Set last->.next to &new

26

head last

slide-27
SLIDE 27

Example: access to a global queue

  • Inserting:
  • Create new object
  • Set last->.next to &new
  • Set last to &new

27

head last

slide-28
SLIDE 28

Example: concurrent access to a global queue

  • Thread A:
  • Thread B:

28

head last

slide-29
SLIDE 29

Example: concurrent access to a global queue

  • Thread A:
  • Create new object
  • Thread B:

29

head last

slide-30
SLIDE 30

Example: concurrent access to a global queue

  • Thread A:
  • Create new object
  • Set last->.next to &new
  • Thread B:

30

head last

slide-31
SLIDE 31

Example: concurrent access to a global queue

  • Thread A:
  • Create new object
  • Set last->.next to &new
  • Thread B:
  • Create new object

31

head last

slide-32
SLIDE 32

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

32

head last

slide-33
SLIDE 33

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

33

head last

slide-34
SLIDE 34

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

34

head last

slide-35
SLIDE 35

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

35

head last

slide-36
SLIDE 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

slide-37
SLIDE 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

slide-38
SLIDE 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

slide-39
SLIDE 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
  • ther 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

slide-40
SLIDE 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

slide-41
SLIDE 41

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

41

https://en.wikipedia.org/wiki/Therac-25

slide-42
SLIDE 42

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

42

https://en.wikipedia.org/wiki/Northeast_blackout_of_2003

slide-43
SLIDE 43

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

43

https://en.wikipedia.org/wiki/Mars_Pathfinder

slide-44
SLIDE 44

Next time…

  • Haskell refresh
  • Make sure you have access to a modern Haskell installation

44

slide-45
SLIDE 45

Photo by Jf Brou

tot ziens

slide-46
SLIDE 46

Extra slides

  • Jonathan Blow (Braid, The Witness)


Techniques for dealing with lack of motivation, malaise, depression

46