Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stephen Toub Parallel Computing Platform Microsoft Corporation.

Similar presentations


Presentation on theme: "Stephen Toub Parallel Computing Platform Microsoft Corporation."— Presentation transcript:

1 Stephen Toub Parallel Computing Platform Microsoft Corporation

2 Why We (I) Care What We’ve Built for Developers What We’re Building for Developers

3

4 Background compilation Regex-based file search Graph layout Reference highlighting IntelliSense sorting Project build system Code analysis Unit testing …

5 Increasingly connected applications More latency e.g. everything as a service More UI responsiveness problems e.g. the toilet bowl of death More scalability issues Server, Cloud e.g. streaming data sources, data distribution Client e.g. modeling interacting biological entities Async-only APIs e.g. Silverlight

6 A different way of thinking Forcing the square peg (concurrency) into the round hole (sequential) is a common (inadequate) mistake Parallel patterns are not prevalent, well known, nor easy to implement CS curriculum is still largely “sequential”: Cormen, et. al; Knuth; etc. A different way of writing software Indeterminate program behavior No longer: “A happens, then B happens, then C happens” It’s just: “A, B, and C happen” With distinct costs, too Deadlocks, livelocks, latent race conditions, priority inversions, hardware- specific memory model reordering, … Businesses have little desire to go deep Best devs should focus on business value, not concurrency Need simple ways to allow all devs to write concurrent code

7 IEnumerable drivers =...; var results = new List (); foreach(var driver in drivers) { if (driver.Name == queryName && driver.Wins.Count >= queryWinCount) { results.Add(driver); } results.Sort((b1, b2) => b1.Age.CompareTo(b2.Age));

8 IEnumerable drivers = …; var results = new List (); int partitionsCount = Environment.ProcessorCount; int remainingCount = partitionsCount; var enumerator = drivers.GetEnumerator(); try { using (var done = new ManualResetEvent(false)) { for(int i = 0; i < partitionsCount; i++) { ThreadPool.QueueUserWorkItem(delegate { while(true) { RaceCarDriver driver; lock (enumerator) { if (!enumerator.MoveNext()) break; driver = enumerator.Current; } if (driver.Name == queryName && driver.Wins.Count >= queryWinCount) { lock(results) results.Add(driver); } if (Interlocked.Decrement(ref remainingCount) == 0) done.Set(); }); } done.WaitOne(); results.Sort((b1, b2) => b1.Age.CompareTo(b2.Age)); } finally { if (enumerator is IDisposable) ((IDisposable)enumerator).Dispose(); }

9 IEnumerable drivers =...; var results = from driver in drivers where driver.Name == queryName && driver.Wins.Count >= queryWinCount orderby driver.Age ascending select driver;

10 PLINQ

11 Parallel Pattern Library Resource Manager Task Scheduler Task Parallel Library Task Parallel Library Parallel LINQ Managed Native Key: Threads Operating System Concurrency Runtime Programming Models ThreadPool Task Scheduler Resource Manager Data Structures Tools Tooling Parallel Debugger Tool Windows Parallel Debugger Tool Windows Profiler Concurrency Visualizer Profiler Concurrency Visualizer Async Agents Library Async Agents Library UMS Threads.NET Framework 4 Visual C Visual Studio 2010 Visual Studio 2010 Windows

12

13 public void CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } }

14 public IAsyncResult BeginCopyStreamToStream( Stream source, Stream destination) { var tcs = new TaskCompletionSource (); byte[] buffer = new byte[0x1000]; Action readWriteLoop = null; readWriteLoop = iar => { try { for (bool isRead = iar == null; ; isRead = !isRead) { switch (isRead) { case true: iar = source.BeginRead(buffer, 0, buffer.Length, readResult => { if (readResult.CompletedSynchronously) return; readWriteLoop(readResult); }, null); if (!iar.CompletedSynchronously) return; break; case false: int numRead = source.EndRead(iar); if (numRead == 0) { tcs.TrySetResult(null); return; } iar = destination.BeginWrite(buffer, 0, numRead, writeResult => { if (writeResult.CompletedSynchronously) return; destination.EndWrite(writeResult); readWriteLoop(null); }, null); if (!iar.CompletedSynchronously) return; destination.EndWrite(iar); break; } } } catch (Exception e) { tcs.TrySetException(e); } }; readWriteLoop(null); return tcs.Task; } public void EndCopyStreamToStream(IAsyncResult asyncResult) { ((Task)asyncResult).Wait(); }

15 public void CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = source.Read(buffer, 0, buffer.Length)) != 0) { destination.Write(buffer, 0, numRead); } } public Task CopyStreamToStream(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while ((numRead = await source.ReadAsync(buffer, 0, buffer.Length)) != 0) { await destination.WriteAsync(buffer, 0, numRead); } }

16

17 © 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.


Download ppt "Stephen Toub Parallel Computing Platform Microsoft Corporation."

Similar presentations


Ads by Google