parallel? sleep well!

31
© Acando AS © Acando AB Parallel? Sleep well! Fredrik Bertilsson

Post on 17-Oct-2014

258 views

Category:

Technology


0 download

DESCRIPTION

Presentation on multicore programming held by Fredrik Bertilsson at Trondheim Developer Conference 2013.

TRANSCRIPT

Page 1: Parallel? Sleep well!

© Acando AS© Acando AB

Parallel? Sleep well!

Fredrik Bertilsson

Page 2: Parallel? Sleep well!

© Acando AS

Fredrik [email protected]

Page 3: Parallel? Sleep well!

© Acando AS 2023-04-074

Contents

●Introduction Why this talk? The free lunch

●Solution patterns Active object Lock ordering Execution pipeline

●Wrap-up

Page 4: Parallel? Sleep well!

© Acando AS 2023-04-075

Why This Talk?

●Something new? No.

●Multithreading available for decades

●What I will show: Modern APIs not enough to make safe and friendly classes

Simple, language independent techniques

Sleep well

Page 5: Parallel? Sleep well!

© Acando AS 2023-04-076

The Free Lunch

1970 – 2005

●Exponential CPU frequency

●Exponential transistor density

Page 6: Parallel? Sleep well!

© Acando AS 2023-04-077

The Free Lunch is …

●2013 – The free lunch is

OVER

Page 7: Parallel? Sleep well!

© Acando AS 2023-04-078

Now and Future

●Exponential cores If developers want it...

●Good! I’ll parallelize my whole program ●Amdahls law

The speedup of a program … is limited by the time needed for the sequential fraction …

Page 8: Parallel? Sleep well!

© Acando AS 2023-04-079

Amdahl’s Law

Page 9: Parallel? Sleep well!

© Acando AS 2023-04-0710

Sequential Program

●Enter threads

●Let’s speed up this sequential program:

private static long a;

for (long i = 0; i < LargeConstant; i++)

{

a++;

}

Page 10: Parallel? Sleep well!

© Acando AS 2023-04-0711

Sequential Program

●15 s

Page 11: Parallel? Sleep well!

© Acando AS 2023-04-0712

Remedy - Parallel

●Parallelize it

const long k = LargeConstant / 2;

var thread1 = new Thread(() =>

{ for (long i = 0; i < k; i++) { a++; } });

var thread2 = new Thread(() => { for (long i = k; i < LargeConstant; i++) { a++; } });

thread1.Start(); thread2.Start();

thread1.Join(); thread2.Join();

Page 12: Parallel? Sleep well!

© Acando AS 2023-04-0713

Remedy - Parallel

●40 s●Not 15 / 2

●Not 15 * 2

Page 13: Parallel? Sleep well!

© Acando AS 2023-04-0714

Memory Caching for One Core

Core

L1 Cache

L2 Cache

L3 Cache

DRAM

Page 14: Parallel? Sleep well!

© Acando AS 2023-04-0715

Memory Caching for Multiple Cores

Core

L1 Cache

L2 Cache

L3 Cache

DRAM

Core

L1 Cache

L2 Cache

L3 Cache

Core

L1 Cache

L2 Cache

L3 Cache

Page 15: Parallel? Sleep well!

© Acando AS 2023-04-0716

Memory Caching – Cache Misses are Expensive

Page 16: Parallel? Sleep well!

© Acando AS 2023-04-0717

What Have We Learned?

●Each thread should use “private” data, not shared among threads

●Better code:

var thread1 = new Thread(() =>

{

long temp = 0;

for (long i = 0; i < k; i++) { temp++; }

a += temp;

});

Corresponding for thread 2

Page 17: Parallel? Sleep well!

© Acando AS 2023-04-0718

What Have We Learned?

●Each thread should use “private” data, not shared

●Better code than our previous “remedy”:

var thread1 = new Thread(() =>

{

long temp = 0;

for (long i = 0; i < k; i++) { temp++; }

Interlocked.Add(ref a, temp);

});

Page 18: Parallel? Sleep well!

© Acando AS 2023-04-0719

Speed now

●9 s●Down to 9 from 15

Page 19: Parallel? Sleep well!

© Acando AS 2023-04-0720

Active Object Pattern – A Real Remedy

●Externally Safe and friendly interface

● Internally Private thread Private data Safe termination

Page 20: Parallel? Sleep well!

© Acando AS 2023-04-0721

Active Object Pattern – Externally

●Safe and friendly interface

●What if we could code something like this:

PrimeCalculator calculator = new PrimeCalculator();

var future = calculator.Calculate(7);

// do other work here WHILE CALCULATOR RUNS

var result = future.Result;

Page 21: Parallel? Sleep well!

© Acando AS 2023-04-0722

Active Object Pattern – Internally 1(2)

●First, the synchronous version:

public class PrimeActiveObject

{

public IEnumerable<int> Calculate(int limit)

{

}

Page 22: Parallel? Sleep well!

© Acando AS 2023-04-0723

Active Object Pattern – Internally 2(2)

●The asynchronous version:

public Task<IEnumerable<int>> CalculateAsync(int limit)

{

var future = new Task<IEnumerable<int>>(

() => Calculate(limit));

future.Start();

return future;

}

Page 23: Parallel? Sleep well!

© Acando AS 2023-04-0724

Active Object Demo

Page 24: Parallel? Sleep well!

© Acando AS 2023-04-0725

Where are we now?

●We have the Active Object pattern Works for NET 4.5 Works for Java 1.5? Works for C++11

●What about the rest of us?

Page 25: Parallel? Sleep well!

© Acando AS 2023-04-0726

Active Object with Thread

●A queue of commands

●Commands hold private data

●Thread executes commands in turn

●Properties Execution is parallel Execution is ordered

Page 26: Parallel? Sleep well!

© Acando AS 2023-04-0727

Lock Ordering

●Order your locking so that you Avoid deadlock Avoid starvation

●E.g. by file name File 1File 2

Thread A Thread B

Page 27: Parallel? Sleep well!

© Acando AS 2023-04-0728

Execution Pipeline

Page 28: Parallel? Sleep well!

© Acando AS 2023-04-0729

Execution Pipeline

●If you have a sequence of operations on data Like a pipeline

●Write your program that way, to avoid cache misses

●Techniques Manually Microsoft TPL (Task Parallel Library) Dataflow

Page 29: Parallel? Sleep well!

© Acando AS 2023-04-0730

Wrap-Up

●Free lunch is … Over

●Use new APIs and … Crash! They are nice, but not

enough

●Safe and friendly patterns Active Object Execution Pipeline Lock Ordering

●Now, you can: Deliver safe asynchronous objects Use the CPUs of today and

tomorrow

Page 30: Parallel? Sleep well!

© Acando AS 2023-04-0731

References

●Watch this presentation on slideshare (soon)http://www.slideshare.net/FredrikBertilsson

●Source code: https://github.com/fbertilsson/parallel-sleep-well.git

●Ordered Execution With ThreadPool, Stephen Toub, MSDNhttp://msdn.microsoft.com/en-us/magazine/dd419664.aspx

Page 31: Parallel? Sleep well!

© Acando AS 2023-04-0732

Thank you for listening. Sleep well!