why is concurrent programming hard? and what can we do about it?

23
Why Is Concurrent Programming Hard? And What Can We Do about It? Stefan Marr Zurich, September 10, 2015

Upload: stefan-marr

Post on 11-Apr-2017

1.369 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Why Is Concurrent Programming Hard? And What Can We Do about It?

Why Is Concurrent Programming Hard?

And What Can We Do about It?

Stefan MarrZurich, September 10, 2015

Page 2: Why Is Concurrent Programming Hard? And What Can We Do about It?

2

70 Years of Parallel Execution

[U.S. Army Photo]

Page 3: Why Is Concurrent Programming Hard? And What Can We Do about It?

3

Perhaps >3,500 Books and >100,000 Papers?

Page 4: Why Is Concurrent Programming Hard? And What Can We Do about It?

4

The Problem

No Silver Bullet!

ActorsTransactional MemoryFork/JoinLocks, Monitors, …Data Flow

Page 5: Why Is Concurrent Programming Hard? And What Can We Do about It?

5

Actors

Actor A Actor B

Data: No Shared MemoryParallelism: Structural, Between Actors

Page 6: Why Is Concurrent Programming Hard? And What Can We Do about It?

6

Actor Use Cases

Hewitt/Agha Actors Communicating Event Loops

Finer GrainedParallel Entities

Coarser GrainedSubsystems

User Interface Data Backend

But you got to do parallel updates on graphs?

Page 7: Why Is Concurrent Programming Hard? And What Can We Do about It?

7

Transactional Memory

Thread A Thread B

IsolatedArea of A

IsolatedArea of B

Page 8: Why Is Concurrent Programming Hard? And What Can We Do about It?

8

Transactional Memory

Too much overhead?well structured data?Thread A Thread B

IsolatedArea of A

IsolatedArea of B

Page 9: Why Is Concurrent Programming Hard? And What Can We Do about It?

9

Fork/Join

Structured Data but not divide and conquer?

Page 10: Why Is Concurrent Programming Hard? And What Can We Do about It?

10

Pick Something Else!

Page 11: Why Is Concurrent Programming Hard? And What Can We Do about It?

11

And All Languages Got Everything

• Actors, Akktors, Actorz, …• Threads, Locks• Fork/Join• Parallel Collections• Futures, Promises• Java’s util.concurrent• Channels• STM, Data Flow, …

Ruby

• Actors, Agents• Looks, Semaphores, Barriers• Atoms• Futures, Promises• Channels• Thread Pools• Thread-safe Array, Hash, …• STM, Data Flow, …

Page 12: Why Is Concurrent Programming Hard? And What Can We Do about It?

12

But, Not designed for Interaction!

Clojure“designed to be a [.] language [.] with an efficient and robust infrastructure for multithreaded programming.”

(dosync (future fun-with-sideeffect) "...")

Haskell“an advanced [.] language [.] of cutting-edge research [.] with [.] built-in concurrency and parallelism”

Control.Concurrent– MVar– Chan

Control.Concurrent.STM– TMVar– TChan

re-execution

No Integration

Just Addition

Page 13: Why Is Concurrent Programming Hard? And What Can We Do about It?

13

Is it Just an Academic Issue?

• Uses Locks and Atomic*• Multiple async.

future/task abstractions• Multiple ‘transaction’

systems

> 4500 “deadlock” bugs> 530 “race condition” bugs

Page 14: Why Is Concurrent Programming Hard? And What Can We Do about It?

14

Is it Just an Academic Issue?

Jaroslav Tulach 2007-03-21 08:41:46 UTC

issue 75858 - e.g. calling 3rd party code from inside of runAtomicAction method

issue 85858 when the 3rd party code called other code while holding internal locks.

issue 95675 that broke web team & co. and five people were hunting for it for a week

From: https://netbeans.org/bugzilla/show_bug.cgi?id=97278

Page 15: Why Is Concurrent Programming Hard? And What Can We Do about It?

WHAT CAN WE DO ABOUT IT?Concurrency without accidental complexity

Page 16: Why Is Concurrent Programming Hard? And What Can We Do about It?

16

Harmonize Concurrency Abstractions

Desirable Properties– Free of low-level data races– Deadlock free– …

Design Dimensions– Activity granularity

• Lightweight vs. heavyweight– Blocking vs. non-blocking– Isolation

• Granularity• Permanent vs. temporary• Optimistic vs. pessimistic

– Deterministic vs. non-deterministic– …

Actors STM

many degrees of design freedom

Page 17: Why Is Concurrent Programming Hard? And What Can We Do about It?

17

Fundamental Elements as Building Blocks

• Identify and formalize

• Building blocks for languages and frameworks

• Compose harmonious elements to guarantee– Deadlock freedom– Race freedom– …

blocking

non-blocking

lightweight

heavyweight

Page 18: Why Is Concurrent Programming Hard? And What Can We Do about It?

18

Actor Domains:Controlling Mutable State

Actors

STM Locks

// new shared domainobjCls = actor.newShared(Obj);obj = objCls<-new;obj.whenExclusive(o => { o mutate});

// new observable domaincntCls = actor.newObs(Counter);cnt = cntCls<-new;

// synchronous, async. mutation print(cnt.read());cnt<-inc;

Actor model withsafe data parallelism

Work by Joeri De KosterDomains: Language Abstractions for Controlling Shared Mutable State in Actor Systems

Joeri De Koster, PhD Dissertation, 2015

Actors

Locks

STM

Page 19: Why Is Concurrent Programming Hard? And What Can We Do about It?

19

Deterministic Fork/Join

STM

Future Promise

Work by Janwillem SwalensTransactional Promises and Futures, Work in Progress

(Paper Draft available on request)

vec = Vector.make(10);prom = Promise.make();

atomic { f1 = future { process(vec,0,4); prom.resolve(42); // is safe }; f2 = future { process(vec,4,9); prom.get(); // read dependency };

} // throws exception on conflict

Page 20: Why Is Concurrent Programming Hard? And What Can We Do about It?

20

Approach: Design Combinations

…Actors

STM Locks

STM

Actors Locks

Actors

• One dominating abstraction• subordinates are assimilated

Page 21: Why Is Concurrent Programming Hard? And What Can We Do about It?

21

Better Insight into Building Blocks

To assemble useful combinations

Page 22: Why Is Concurrent Programming Hard? And What Can We Do about It?

22

Truffle-based Newspeak

• Class-based• No global/static state• Value objects• Actors

NS

1.65x slower than Javamin. -3%, max. 2.6x

Research Platform for Actor Domains and Other Models

Page 23: Why Is Concurrent Programming Hard? And What Can We Do about It?

23

Future Plans

Explore Safe Combinations• Complex concurrent systems

– Funding proposal submitted– Collaboration of SSW & SOFT

• Lightweight instrumentation• Independent of Concurrency

Models

Investigate Debugging

• Increase Applicability• Demonstrate Performance