ACCU 2012: Go, D, C++ and The Multicore Revolution

57
Copyright © 2011–2012 Russel Winder 1 Go, D, C++ and the Multicore Revolution Russel Winder email: [email protected] xmpp: [email protected] twitter: russel_winder

description

presenting actors, dataflow, CSP, data parallelism in Go, D and C++. Used the examples from the Bazaar branch at http://www.russel.org.uk/Bazaar/Pi_Quadrature

Transcript of ACCU 2012: Go, D, C++ and The Multicore Revolution

Page 1: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 1

Go, D, C++ and theMulticore Revolution

Russel Winder

email: [email protected]: [email protected]

twitter: russel_winder

Page 2: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 2

Aims, Goals and Objects

● Show that shared memory multi-threading should return to being an operating systems development technique and not continue to be pushed as an applications programming technique.

● Show, possibly, that the C++11 standard may, perhaps, have saved C++ from the dustbin of obscurity.

Page 3: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 3

Structure

A beginning.

A middle.

An end.

Page 4: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 4

Protocol

● Questions or short comments during the session are entirely in order.

● Let me know you have an interjection by raising your hand, and when I come to an appropriate pause, I'll pass you the token.

Questions, answers, comments, etc. appearingto get too long as interjections may get stackedto be unstacked at a break.

Page 5: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 5

Interstitial Advertisement

Page 6: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 6

A Beginning

Page 7: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 7

It is no longer contentious thatThe Multicore Revolution

is well underway.

Page 8: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 8

Quad core laptops and phones.

Eight and twelve core workstations.

Servers with “zillions” of cores.

Page 9: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 9

Software technologyis now lagging

hardware technologyby decades.

Page 10: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 10

Operating systems manage coreswith kernel threads.

Operating systems are fundamentally shared memory multi-threaded systems.

Operating systems rightly use all the lock, semaphore, monitor, etc. technologies.

Page 11: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 11

Operating systems really rather needto be native code systems.

Page 12: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 12

Constructive dichotomies…

Page 13: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 13

Native code languages.

vs.

Virtual machine based languages.

Page 14: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 14

Statically typed languages.

vs.

Dynamically typed languages.

Page 15: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 15

Compiled languages.

vs.

Scripting languages languages.

Page 16: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 16

Imperative languages.

vs.

Declarative languages.

Page 17: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 17

Sequential.

vs.

Parallel.

Page 18: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 18

Computationally intensive.

vs.

I/O intensive.

Page 19: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 19

Batch flow.

vs.

Event driven.

Page 20: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 20

Monolithic systems.

vs.

Cooperating subsystems.

Page 21: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 21

Computationally intensive systems orsubsystems definitely have to be parallel.

Other systems likely use concurrencybut not parallelism.

Page 22: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 22

Concurrency is a design tool.

Parallelism is a tool to improvecomputational performance.

Page 23: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 23

Static vs. Dynamic

● Fortran● C● C++● D● Go● Rust● Java● Scala● Ceylon● Kotlin

● Python, Jython

● Lisp, Clojure

● Ruby, JRuby

● Groovy

● JavaScript

● Dart

COBOL

Page 24: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 24

A Middle

Page 25: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 25

It's all about controllingconcurrency with tools that programmers find usable.

Page 26: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 26

Shared memory multi-threading is an operating system technique.

Page 27: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 27

Applications and tools programmersneed computational models with

integrated synchronization.

Page 28: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 28

ActorsIndependent processes communicating via asynchronous exchange of messages

CSPSequential processes connected by channels using synchronous message exchange (rendezvous).

DataflowOperators connected by channels with activity triggered by arrival of data on the channels.

Page 29: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 29

ActorsIndependent processes communicating via asynchronous exchange of messages

Page 30: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 30

DataflowOperators connected by channels with activity triggered by arrival of data on the channels.

Page 31: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 31

CSPSequential processes connected by channels using synchronous message exchange (rendezvous).

Page 32: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 32

Need examples.

Page 33: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 33

Page 34: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 34

What is the Value of ?

● Easy, it's known exactly, it's (obviously).

It's simples

Александр Орлов 2009

Page 35: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 35

Approximating

● What is it's value represented as a floating point number?● We can only obtain an approximation.● A plethora of possible algorithms to choose from, a

popular one is to employ the following integral equation.

4=∫0

1 1

1x2dx

Page 36: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 36

One Possible Algorithm

● Use quadrature to estimate the value of the integral – which is the area under the curve.

=4n∑i=1

n 1

1i−0.5n

2

With n = 3 not much to do, but potentially lots of error. Use n = 107 or n = 109?

Embarrassingly parallel.

Page 37: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 37

Scatter – Gather

map reduce

Page 38: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 38

Code!

Page 39: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 39

Need another example.

Page 40: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 40

The Sleeping Barber Problem

Page 41: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 41

The Sleeping Barber Problem

● The barber's shop has a single cutting chair and a row of waiting seats.

● The barber sleeps in the cutting chair unless trimming a customer.

● Customers arrive at the shop at intervals.

● If the barber is asleep, the customer wakes the barber takes the cutting chair and gets a trim.

● If the barber is cutting, a new customer checks to see if there is a free waiting seat.

● If there is join the queue to be trimmed.

● If there isn't leave disgruntled.

Problem originally dueto Edsgar Dijkstra.

Page 42: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 42

A new customer enters the shop,check to see if they can go straightto the cutting chair, if not can theytake a waiting chair, if not leave.

The waiting chairs

The cutting chair.

The barber's shop.

Page 43: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 43

Wikipedia article presents the classic operating systems approach using locks and semaphores.

http://en.wikipedia.org/wiki/Sleeping_barber_problem

Page 44: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 44

More code!

Page 45: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 45

Note to Self:

Did you, in fact, show all the examples in:

● C● D● Go● C++● Python

Page 46: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 46

An End

Page 47: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 47

Native code language are just asmuch scripting languages asvirtual machine languages

and interpreted ones.

Page 48: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 48

Virtual machine languages arejust as fast as native code

languages when using JITs.

Page 49: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 49

Multicore and multiprocessorare now the norm,not the exception.

Page 50: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 50

Parallelism only mattersif computational

performance matters.

Page 51: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 51

Unstructured synchronizationof concurrent systems

is not a feasible approach.

Page 52: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 52

Actors, CSP and Datafloware the future of

applications structure.

Page 53: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 53

Passing messages betweenprocesses is the way forward.

Page 54: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 54

Shared memory concurrencyis a dead end for applications.

Page 55: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 55

Surreptitious Advertisement

Page 56: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 56

The End

Page 57: ACCU 2012:  Go, D, C++ and The Multicore Revolution

Copyright © 2011–2012 Russel Winder 57

Go, D, C++ and theMulticore Revolution

Russel Winder

email: [email protected]: [email protected]

twitter: russel_winder