Human Multithreading Pascal Van Cauwenberghe Programmed by Thien - - PowerPoint PPT Presentation

human multithreading
SMART_READER_LITE
LIVE PREVIEW

Human Multithreading Pascal Van Cauwenberghe Programmed by Thien - - PowerPoint PPT Presentation

Human Multithreading Pascal Van Cauwenberghe Programmed by Thien Que Nguyen and Pascal Van Cauwenberghe Consults Manages projects Analyzes business Creates games Teaches yoga Organizes conferences Thien Que Nguyen http://atbru.be


slide-1
SLIDE 1

Human Multithreading

Pascal Van Cauwenberghe

Programmed by Thien Que Nguyen and Pascal Van Cauwenberghe

slide-2
SLIDE 2

Consults Manages projects Tests, programs, analyzes Creates games Tells tall stories Organizes conferences Pascal Van Cauwenberghe http://xpday.net http://atbru.be Thien Que Nguyen Consults Manages projects Analyzes business Creates games Teaches yoga Organizes conferences

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7
slide-8
SLIDE 8

We had fun We solved the puzzle You look at concurrent systems differently You’ve learned something you can apply in your work and life You want to know more

Acc Accept eptanc ance Test e Test

slide-9
SLIDE 9
slide-10
SLIDE 10
slide-11
SLIDE 11

“Computers”

slide-12
SLIDE 12

Demo: TDD Cell Exercise 1: TDD Grid Exercise 2: TDD Region Theory Human Computer Simulation

slide-13
SLIDE 13

TTDD Human Computer

2 + 2 = 4? 2 + 2 =…? Correct!

slide-14
SLIDE 14

1

Ce Cell ll Sp Speci ecifi ficat cation ion

[1 [1][2][3][4][5][6] ][2][3][4][5][6]

  • A Cell expresses which of the numbers

bers 1..6 are pos

  • ssibl

ble

  • By default every number

ber is pos

  • ssibl

ble

  • If there is more than one possible number, the value of the cell is unknown

nown

  • If exactly one number is possible, the value of the cell is know
  • wn == number
  • If there are no possible numbers there is a contradiction, this is impos
  • ssi

sibl ble

slide-15
SLIDE 15

Demo TDD: Cell (1)

1 2 3 4 5 6

  • GIVEN a new Cell

Question Answer 6 UNKNOWN Yes Yes How many numbers are possible? What is the value of the cell? Is 5 a possible value? Is 2 a possible value?

slide-16
SLIDE 16

Demo TDD: Cell (2)

1 2 3 4 5 6

  • GIVEN a new Cell
  • When 3 is not possible
  • When 4 is not possible

Question Answer 4 UNKNOWN Yes No How many numbers are possible? What is the value of the cell? Is 5 a possible value? Is 3 a possible value?

slide-17
SLIDE 17

Demo TDD: Cell (3)

1 2 3 4 5 6

  • GIVEN a new Cell
  • When the value is known to be 3

Question Answer 1 3 No No How many numbers are possible? What is the value of the cell? Is 5 a possible value? Is 2 a possible value?

slide-18
SLIDE 18

Demo TDD: Cell (4)

1 2 3 4 5 6

  • GIVEN a new Cell
  • When number 1, 6, 3, 2, 5, 4 are not possible

Question Answer IMPOSSIBLE How many numbers are possible? What is the value of the cell?

slide-19
SLIDE 19

Gr Grid id Sp Speci ecifi ficat cation ion (1 (1)

1

A Grid has a name: A, B, C, D, E, or F A Grid consists of 3x2 Cells Cells are addressed as (Row, Column) IF a Cell within a Grid has a known value THEN NO other Cell can have the same value

slide-20
SLIDE 20

1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6

Grid – Example (1)

Example: Cell(2,1) = 4

1 2 3 4 5 6

(1,1) (1,2) (2,1) (2,2) (3,1) (3,2)

ROW 1 ROW 2 ROW 3 COLUMN 1 COLUMN 2

slide-21
SLIDE 21

IF a cell in a grid has a known value THEN all cells on the same row

  • f all grids to the left and to the right

can not have the same value

Gr Grid id Sp Speci ecifi ficat cation ion (1 (1)

A B C D E F

slide-22
SLIDE 22

Grid – Specification (2)

  • Example: Grid E cell(2,1) = 6

1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6

(1,1) (1,2) (2,1) (2,2) (3,1) (3,2)

ROW 1 ROW 2 ROW 3 COLUMN 1 COLUMN 2

A B C D E F

Grid D

slide-23
SLIDE 23

IF a cell in a grid has a known value THEN all cells on the same column

  • f all grids above and below

can not have the same value

Gr Grid id Sp Speci ecifi ficat cation ion (1 (1)

A B C D E F

slide-24
SLIDE 24

Grid – Specification (3)

  • Example: Grid E, cell(2,1) = 6

1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6

(1,1) (1,2) (2,1) (2,2) (3,1) (3,2)

ROW 1 ROW 2 ROW 3 COLUMN 1 COLUMN 2

A B C D E F

Grid B

slide-25
SLIDE 25

Execute Grid Tests

Get Ready… Start! 10 mins execution time

slide-26
SLIDE 26
  • A Region contains a Grid
  • A Region has 4 inputs: North, East, South, West
  • A Region has 4 outputs: North, East, South, West
  • A Region has an output Display

Re Regi gion

  • n

Sp Speci ecifi ficat cation ion (1 (1)

slide-27
SLIDE 27

Region

Grid NORTH SOUTH EAST WEST DISPLAY

slide-28
SLIDE 28
  • When you discover the value of a cell
  • Send a message Gridname(Row,Column)=Value
  • To the Display
  • And to all output channels
  • You discover the starting values of a few cells.
  • You must send these values to the Display and all

the output channels

Re Regi gion

  • n S

Spe pecif cifica icatio tion n (2 (2)

slide-29
SLIDE 29

Region

Grid NORTH SOUTH EAST WEST DISPLAY

D(2,1)=3 D(2,1)=3 D(2,1)=3 D(2,1)=3 D(2,1)=3

slide-30
SLIDE 30
  • When you receive a GridName(Row,Column) = Value From the

North or South

  • The Grid can’t have Value in that column
  • When you receive a GridName(Row,Column) From the East
  • r West
  • The Grid can’t have Value in that row
  • And Send the message on to the other side

Re Regi gion

  • n

Spe Speci cific fication ation (2) (2)

slide-31
SLIDE 31

Region

Grid NORTH SOUTH EAST WEST DISPLAY

B(2,1)=3 F(1,2)=4

slide-32
SLIDE 32

Execute RegionTests

Get ready…Start! 15 mins execution time

slide-33
SLIDE 33

What happened?

And a smidgen of theory

slide-34
SLIDE 34

CSP = Communicating Sequential Processes

QuickSort

Sir Prof. Tony Hoare CSP Book (1985) Free download (2004)

slide-35
SLIDE 35
  • Processes
  • Independent, sequential, black box
  • Channels
  • Send and receive messages
  • ALT(ernating)
  • Wait for channels or timers

The 3 elements of CSP

A C B

slide-36
SLIDE 36

Back to 1985!

slide-37
SLIDE 37

Back to 1985!

  • Occam
  • Transputer
slide-38
SLIDE 38

Back to 1985!

slide-39
SLIDE 39

Process 1

Benefits of CSP

  • Separation between “domain” and “collaboration”
  • No shared mutable state => no locking (bugs)
  • Easy and fast unit tests
  • Easy to simulate edge cases
  • Composable. Composite Process can be tested

Domain 1 Process 2 Domain 2

Composite Process

slide-40
SLIDE 40

How do you test concurrent code?

  • 1. TDD single threaded domain objects
  • 2. Encapsulate domain objects in CSP Processes
  • 3. TDD each Process in isolation
  • WHEN send(input,value)
  • THEN expected_value == receive(output)
  • WHEN time.advance(amount)
  • THEN expected_value == receive(output)
  • 4. TDD composed Processes
  • TDD the inputs/outputs of ever larger ensembles
slide-41
SLIDE 41

Processor Processor Processor Processor Processor

Concurrent != Parallel

A B B C Concurrent Parallel Sequential B B

slide-42
SLIDE 42

To solve a real Sudoku

A

(3x3)

B C F E D G H I Display

slide-43
SLIDE 43

Another CSP

  • Constraint Satisfaction Problem
  • A set of variables (81 cells)
  • Each variable has a Domain of possible values (1..6)
  • There are constraints between variables (e.g. All cells

in a 3x3 grid must have distinct values)

slide-44
SLIDE 44

Constraint satisfaction

Eliminate possibilities Eliminate possibilities Cell(3,2)=5? Dead end Eliminate possibilities Cell(3,2)=1? Sequential: backtracking Eliminate possibilities Eliminate possibilities Cell(3,2)=5? Dead end Eliminate possibilities Cell(3,2)=1? Concurrent; spawning

slide-45
SLIDE 45

We had fun We solved the puzzle You look at concurrent systems differently You’ve learned something you can apply in your work and life You want to know more

Acc Accept eptanc ance Test e Test

slide-46
SLIDE 46

If you want to know more

  • Tony Hoare:http://www.usingcsp.com/
  • C++:http://www.cs.kent.ac.uk/projects/ofa/c++csp/
  • Java:http://www.cs.kent.ac.uk/projects/ofa/jcsp/
  • Go: http://golang.org/
  • Clojure: http://clojure.org/
  • Download from http://agilecoach.net
  • https://github.com/pascalvancauwenberghe/concurrent
slide-47
SLIDE 47
slide-48
SLIDE 48

Are you ready to execute the program?

We need (N)x8 players to run the simulation You can also observe

slide-49
SLIDE 49

Assemble the Computer

A B C F E D Display Test

slide-50
SLIDE 50

Run the simulation

Pause when you’ve sent all messages for known values Check Display of known values Process all incoming messages