Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Parallel? Sleep well!

256 views

Published on

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

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Parallel? Sleep well!

  1. 1. Parallel? Sleep well! Fredrik Bertilsson © Acando AB © Acando AS
  2. 2. Fredrik Bertilsson Fredrik.bertilsson@acando.com © Acando AS
  3. 3. Contents ● Introduction  Why this talk?  The free lunch ● Solution patterns  Active object  Lock ordering  Execution pipeline ● Wrap-up 3 © Acando AS 2013-10-29
  4. 4. 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
  5. 5. The Free Lunch 1970 – 2005 ● Exponential CPU frequency ● Exponential transistor density 5 © Acando AS 2013-10-29
  6. 6. The Free Lunch is … ● 2013 – The free lunch is OVER 6 © Acando AS 2013-10-29
  7. 7. 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
  8. 8. Amdahl’s Law 8 © Acando AS 2013-10-29
  9. 9. 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
  10. 10. Sequential Program ●15 s 10 © Acando AS 2013-10-29
  11. 11. 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
  12. 12. Remedy - Parallel ●40 s ● Not 15 / 2 ● Not 15 * 2 12 © Acando AS 2013-10-29
  13. 13. Memory Caching for One Core Core L1 Cache L2 Cache L3 Cache DRAM 13 © Acando AS 2013-10-29
  14. 14. 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
  15. 15. Memory Caching – Cache Misses are Expensive 15 © Acando AS 2013-10-29
  16. 16. 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
  17. 17. 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
  18. 18. Speed now ●9 s ● Down to 9 from 15 18 © Acando AS 2013-10-29
  19. 19. Active Object Pattern – A Real Remedy ● Externally  Safe and friendly interface 19 © Acando AS 2013-10-29 ● Internally  Private thread  Private data  Safe termination
  20. 20. 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
  21. 21. 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
  22. 22. 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
  23. 23. Active Object Demo 23 © Acando AS 2013-10-29
  24. 24. 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
  25. 25. 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
  26. 26. 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
  27. 27. Execution Pipeline 27 © Acando AS 2013-10-29
  28. 28. 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
  29. 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
  30. 30. 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
  31. 31. Thank you for listening. Sleep well! 31 © Acando AS 2013-10-29

×