Successfully reported this slideshow.
Upcoming SlideShare
×

Concurrency

307 views

Published on

Published in: Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Concurrency

1. 1. Concurrency Examples for .NET
2. 2. Responsive
3. 3. Performance Scalable algorithms
4. 4. Three pillars of Concurrency  Scalability (CPU)  Parallel.For  Responsiveness  Task  async/await  Consistency  lock  Interlocked.*  Mutex/Event/Semaphore  Monitor
5. 5. Scalability
6. 6. Which is fastest? var ints = new int[InnerLoop]; var random = new Random(); for (var inner = 0; inner < InnerLoop; ++inner) { ints[inner] = random.Next(); } // ------------------------------------------------ var ints = new int[InnerLoop]; var random = new Random(); Parallel.For( 0, InnerLoop, i => ints[i] = random.Next() );
7. 7. SHARED STATE  Race condition var ints = new int[InnerLoop]; var random = new Random(); for (var inner = 0; inner < InnerLoop; ++inner) { ints[inner] = random.Next(); } // ------------------------------------------------ var ints = new int[InnerLoop]; var random = new Random(); Parallel.For( 0, InnerLoop, i => ints[i] = random.Next() );
8. 8. SHARED STATE  Poor performance var ints = new int[InnerLoop]; var random = new Random(); for (var inner = 0; inner < InnerLoop; ++inner) { ints[inner] = random.Next(); } // ------------------------------------------------ var ints = new int[InnerLoop]; var random = new Random(); Parallel.For( 0, InnerLoop, i => ints[i] = random.Next() );
9. 9. Then and now Metric VAX-11/750 (’80) Today Improvement MHz 6 3300 550x Memory MB 2 16384 8192x Memory MB/s 13 R ~10000 W ~2500 770x 190x
10. 10. Then and now Metric VAX-11/750 (’80) Today Improvement MHz 6 3300 550x Memory MB 2 16384 8192x Memory MB/s 13 R ~10000 W ~2500 770x 190x Memory nsec 225 70 3x
11. 11. Then and now Metric VAX-11/750 (’80) Today Improvement MHz 6 3300 550x Memory MB 2 16384 8192x Memory MB/s 13 R ~10000 W ~2500 770x 190x Memory nsec 225 70 3x Memory cycles 1.4 210 -150x
12. 12. 299,792,458 m/s
13. 13. Speed of light is too slow
14. 14. 0.09 m/c
15. 15. 99% - latency mitigation 1% - computation
16. 16. 2 Core CPU RAM L3 L2 L1 CPU L2 L1 CPU
17. 17. 2 Core CPU – L1 Cache L1 CPU L1 CPU new Random () new int[InnerLoop]
18. 18. 4 Core CPU – L1 Cache L1 CPU L1 CPU L1 CPU L1 CPU new Random () new int[InnerLoop]
19. 19. 2x4 Core CPU RAM L3 L2 L1 CPU L2 L1 CPU L2 L1 CPU L2 L1 CPU L3 L2 L1 CPU L2 L1 CPU L2 L1 CPU L2 L1 CPU
20. 20. Solution 1 – Locks var ints = new int[InnerLoop]; var random = new Random(); Parallel.For( 0, InnerLoop, i => {lock (ints) {ints[i] = random.Next();}} );
21. 21. Solution 2 – No sharing var ints = new int[InnerLoop]; Parallel.For( 0, InnerLoop, () => new Random(), (i, pls, random) => {ints[i] = random.Next(); return random;}, random => {} );
22. 22. Parallel.For adds overhead Level0 Level1 Level2 ints[0] ints[1] Level2 ints[2] ints[3] Level1 Level2 ints[4] ints[5] Level2 ints[6] ints[7]
23. 23. Solution 3 – Less overhead var ints = new int[InnerLoop]; Parallel.For( 0, InnerLoop / Modulus, () => new Random(), (i, pls, random) => { var begin = i * Modulus ; var end = begin + Modulus ; for (var iter = begin; iter < end; ++iter) { ints[iter] = random.Next(); } return random; }, random => {} );
24. 24. var ints = new int[InnerLoop]; var random = new Random(); for (var inner = 0; inner < InnerLoop; ++inner) { ints[inner] = random.Next(); }
25. 25. Solution 4 – Independent runs var tasks = Enumerable.Range (0, 8).Select ( i => Task.Factory.StartNew ( () => { var ints = new int[InnerLoop]; var random = new Random (); while (counter.CountDown ()) { for (var inner = 0; inner < InnerLoop; ++inner) { ints[inner] = random.Next(); } } }, TaskCreationOptions.LongRunning)) .ToArray (); Task.WaitAll (tasks);
26. 26. Parallel.For Only for CPU bound problems