• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Parallel? Sleep well!
 

Parallel? Sleep well!

on

  • 225 views

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

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

Statistics

Views

Total Views
225
Views on SlideShare
225
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • .NET but agnosticPlease ask questions afterwards.
  • Why this presentation???- Something new? No!- Multithreading many years!+ technology, APIsToo little: talk about good patternsWhat I will show today:- Modern APIs are not enough to make safe and friendly classes.- Simple, language independent techniques -> Sleep well
  • From 1970 ExponentialMy programs run faster every year!(single core)This is called the free lunch!Life is goodSmell roses and liliesAlmost hear spring music
  • The whole Amdahls law: The speedup of a program using multiple processors in parallel computing is limited by the time needed for the sequential fraction of the program.->Speedup of different algorithms
  • Speedup of different algorithms.If your algorithm … only small sequential parts … greenIf half … sequential … blueConclusion: Your type of algorithms or problems matter!-> Sequential program
  • Very simple piece of work
  • My old program: 15 s
  • -> Time now
  • It’s not half of 15, Not double of 15, but even worse!I have not got the faintest clue as to what is going on!We have limited time, so I’ll have to jump to a conclusion: It’s the memory model I don’t have a clue about when I write this code.What is going on?
  • So, what happened?Static long aImagine a librarian running between the cores managing the loans-> Tweet
  • Exactly what we experienced
  • Will this work… always???Who can spot the problem? Race condition: a += temp-> Interlocked/atomic
  • Release mode – the figures are not as bad-> Time now
  • -> Active object pattern
  • -> Let’s try to implement
  • future.Result returns now or blocks-> internally
  • ->Private thread, Private data, Safe termination
  • Private threadPrivate dataSafe termination-> Demo
  • PrimeActiveObjectTestOne timeMany in parallel – light up CPU?SorterActiveObjectTestImplTestNiceBreak it – shared data-> Where are we now?
  • Examples:Process new files arriving on disk, e.g. FileSystemWatcherProcess data arriving on network to even out bursts-> Lock ordering
  • 2 files2 threadseach thread need read/write access to both files for a little while-> Execution pipeline
  • Remember the pyramids of caches.Nice if the data is cachedIf we have sequence of operations, a pipeline, to perform on the same data, do it on the same thread.

Parallel? Sleep well! Parallel? Sleep well! Presentation Transcript

  • Parallel? Sleep well! Fredrik Bertilsson © Acando AB © Acando AS
  • Fredrik Bertilsson Fredrik.bertilsson@acando.com © Acando AS
  • Contents ● Introduction  Why this talk?  The free lunch ● Solution patterns  Active object  Lock ordering  Execution pipeline ● Wrap-up 3 © Acando AS 2013-10-29
  • 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 4 © Acando AS 2013-10-29
  • The Free Lunch 1970 – 2005 ● Exponential CPU frequency ● Exponential transistor density 5 © Acando AS 2013-10-29
  • The Free Lunch is … ● 2013 – The free lunch is OVER 6 © Acando AS 2013-10-29
  • 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 … 7 © Acando AS 2013-10-29
  • Amdahl’s Law 8 © Acando AS 2013-10-29
  • Sequential Program ● Enter threads ● Let’s speed up this sequential program: private static long a; for (long i = 0; i < LargeConstant; i++) { a++; } 9 © Acando AS 2013-10-29
  • Sequential Program ●15 s 10 © Acando AS 2013-10-29
  • 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(); 11 © Acando AS 2013-10-29
  • Remedy - Parallel ●40 s ● Not 15 / 2 ● Not 15 * 2 12 © Acando AS 2013-10-29
  • Memory Caching for One Core Core L1 Cache L2 Cache L3 Cache DRAM 13 © Acando AS 2013-10-29
  • Memory Caching for Multiple Cores Core Core Core L1 Cache L1 Cache L1 Cache L2 Cache L2 Cache L2 Cache L3 Cache L3 Cache L3 Cache DRAM 14 © Acando AS 2013-10-29
  • Memory Caching – Cache Misses are Expensive 15 © Acando AS 2013-10-29
  • 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 16 © Acando AS 2013-10-29
  • 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); }); 17 © Acando AS 2013-10-29
  • Speed now ●9 s ● Down to 9 from 15 18 © Acando AS 2013-10-29
  • Active Object Pattern – A Real Remedy ● Externally  Safe and friendly interface 19 © Acando AS 2013-10-29 ● Internally  Private thread  Private data  Safe termination
  • 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; 20 © Acando AS 2013-10-29
  • Active Object Pattern – Internally 1(2) ● First, the synchronous version: public class PrimeActiveObject { public IEnumerable<int> Calculate(int limit) { … } 21 © Acando AS 2013-10-29
  • 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; } 22 © Acando AS 2013-10-29
  • Active Object Demo 23 © Acando AS 2013-10-29
  • 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? 24 © Acando AS 2013-10-29
  • Active Object with Thread ● A queue of commands ● Properties ● Commands hold private data  Execution is parallel ● Thread executes commands in turn  Execution is ordered 25 © Acando AS 2013-10-29
  • Lock Ordering ● Order your locking so that you  Avoid deadlock  Avoid starvation ● E.g. by file name Thread A 26 © Acando AS 2013-10-29 File 1 File 2 Thread B
  • Execution Pipeline 27 © Acando AS 2013-10-29
  • 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 28 © Acando AS 2013-10-29
  • 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 29 © Acando AS 2013-10-29 ● Now, you can:  Deliver safe asynchronous objects  Use the CPUs of today and tomorrow
  • 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, MSDN http://msdn.microsoft.com/en-us/magazine/dd419664.aspx 30 © Acando AS 2013-10-29
  • Thank you for listening. Sleep well! 31 © Acando AS 2013-10-29