concurrency
TRANSCRIPT
![Page 1: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/1.jpg)
![Page 2: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/2.jpg)
ConcurrencyExamples for .NET
![Page 3: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/3.jpg)
![Page 4: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/4.jpg)
Responsive
![Page 5: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/5.jpg)
PerformanceScalable algorithms
![Page 6: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/6.jpg)
Three pillars of Concurrency
Scalability (CPU) Parallel.For
Responsiveness Task async/await
Consistency lock Interlocked.* Mutex/Event/Semaphore Monitor
![Page 7: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/7.jpg)
Scalability
![Page 8: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/8.jpg)
![Page 9: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/9.jpg)
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() );
![Page 10: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/10.jpg)
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() );
![Page 11: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/11.jpg)
SHARED STATE Poor performancevar 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() );
![Page 12: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/12.jpg)
![Page 13: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/13.jpg)
Then and now
Metric VAX-11/750 (’80)
Today Improvement
MHz 6 3300 550x
Memory MB 2 16384 8192x
Memory MB/s 13 R ~10000W ~2500
770x190x
![Page 14: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/14.jpg)
Then and now
Metric VAX-11/750 (’80)
Today Improvement
MHz 6 3300 550x
Memory MB 2 16384 8192x
Memory MB/s 13 R ~10000W ~2500
770x190x
Memory nsec 225 70 3x
![Page 15: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/15.jpg)
Then and now
Metric VAX-11/750 (’80)
Today Improvement
MHz 6 3300 550x
Memory MB 2 16384 8192x
Memory MB/s 13 R ~10000W ~2500
770x190x
Memory nsec 225 70 3x
Memory cycles
1.4 210 -150x
![Page 16: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/16.jpg)
299,792,458 m/s
![Page 17: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/17.jpg)
![Page 18: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/18.jpg)
Speed of light is too slow
![Page 19: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/19.jpg)
0.09 m/c
![Page 20: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/20.jpg)
![Page 21: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/21.jpg)
99% - latency mitigation
1% - computation
![Page 22: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/22.jpg)
2 Core CPU
RAM
L3L2
L1
CPU
L2
L1
CPU
![Page 23: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/23.jpg)
2 Core CPU – L1 Cache
L1
CPU
L1
CPU
new Random ()
new int[InnerLoop]
![Page 24: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/24.jpg)
4 Core CPU – L1 Cache
L1
CPU
L1
CPU
L1
CPU
L1
CPU
new Random ()
new int[InnerLoop]
![Page 25: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/25.jpg)
2x4 Core CPU
RAM
L3L2
L1
CPU
L2
L1
CPU
L2
L1
CPU
L2
L1
CPU
L3L2
L1
CPU
L2
L1
CPU
L2
L1
CPU
L2
L1
CPU
![Page 26: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/26.jpg)
Solution 1 – Locks
var ints = new int[InnerLoop];var random = new Random();Parallel.For( 0, InnerLoop, i => {lock (ints) {ints[i] = random.Next();}} );
![Page 27: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/27.jpg)
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 => {} );
![Page 28: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/28.jpg)
Parallel.For adds overheadLevel0
Level1
Level2
ints[0]
ints[1]
Level2
ints[2]
ints[3]
Level1
Level2
ints[4]
ints[5]
Level2
ints[6]
ints[7]
![Page 29: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/29.jpg)
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 => {} );
![Page 30: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/30.jpg)
var ints = new int[InnerLoop];var random = new Random();for (var inner = 0; inner < InnerLoop; ++inner){ ints[inner] = random.Next();}
![Page 31: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/31.jpg)
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);
![Page 32: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/32.jpg)
Parallel.For
Only for CPU bound problems
![Page 33: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/33.jpg)
Sharing is bad
Kills performanceRace conditions
Dead-locks
![Page 34: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/34.jpg)
Servers have natural concurrency
Avoid Parallel.For
![Page 35: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/35.jpg)
Act like an engineer
Measure before and after
![Page 36: Concurrency](https://reader036.vdocument.in/reader036/viewer/2022062707/5584f2b8d8b42a2f5c8b5127/html5/thumbnails/36.jpg)
One more thing…