190 likes | 588 Views
“Parallel Programming with Async and Await ”. Joe Hummel, PhD joe@joehummel.net Technical Staff: Pluralsight Adjunct Professor: UIC, LUC. http://www.joehummel.net/downloads.html. Agenda. Motivation Execution model Parallel programming with Tasks
E N D
“Parallel Programming with Asyncand Await” Joe Hummel, PhD joe@joehummel.net Technical Staff: Pluralsight Adjunct Professor: UIC, LUC http://www.joehummel.net/downloads.html
Agenda • Motivation • Execution model • Parallel programming with Tasks • Parallel programming with Async / Await • Demos
Async vs. Parallel? • Asyncprogramming: • Better responsiveness… • GUIs (desktop, web, mobile) • Cloud • Windows 8 • Parallel programming: • Better performance… • Engineering • Oil and Gas • Pharma • Science • Social media C C C C C C C C Disk and network I/Otasks number crunching and big data processing
Execution model • Single core: • Multicore: C C Main <<start Work1>> <<start Work2>> if… while… Main <<start Work>> if… while… Work2 Stmt4; Stmt5; Stmt6; Work1 Stmt1; Stmt2; Stmt3; Work Stmt1; Stmt2; Stmt3; C C C C C C Workerthread Workerthread Workerthread Mainthread Mainthread Threads run in parallel Threadsshare, run asynchronously C
Numerous ways to program • Threads (.NET 1.0) • Async Delegates • QueueUserWorkItem • BackgroundWorker • Task Parallel Library (.NET 4.0) • Async / Await (.NET 4.5) Easier…
Demo ― Performance • Mandelbrot set…
Task-based • Programming model based on concept of a Task Task == a unit of work; an object denoting an ongoing operation or computation.
Task-based execution model Parallel.For( ... ); task task task task Windows Process (.NET) App Domain App Domain App Domain C C C C C C C C Task Parallel Library .NET Thread Pool Task Scheduler worker thread worker thread worker thread worker thread Resource Manager Windows
Demo ― Responsiveness • Asian options financial modeling…
Async solution #1: Tasks void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } void button1_Click(…) { varuictx = // grab UI thread context to run UI task: TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(()=> { return DoLongLatencyOp(); } ).ContinueWith((antecedent) => { lstBox.Items.Add(antecedent.Result); }, uictx// execute this task on UI thread: ); }
Async solution #2: Async / Await void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } async void button1_Click(…) { var result = awaitTask.Run( () => DoLongRunningOp()); lstBox.Items.Add(result); } Tells compiler that method *may* perform an async, long-latency op • Tells compiler to execute operation but don’t wait.Instead,start opas a separate task, and setup a continuation to execute the remaining code when op finishes ― RUNNING ON THE SAME THREAD CONTEXT!
When to use Async / Await? • For operations that may involve long latency • File and network I/O are the classic use-case • For chunks of work you want to run in parallel • And you have multi-core hardware
Observations… • Hides the complexities of async programming • No visible callback, predictable exception handling, … • Think chunky, not chatty • i.e. designed for coarse-grain work / long-latency operations • Designed to be used with APM pattern • Asynchronous Programming Model • APIs that offer async calls via APM? • File I/O • Network I/O • Windows 8 API
Example • Async web calls are a classic use-case Synchronous Version private byte[] GetURLContents(string url) { varcontent = new MemoryStream(); varwebReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = webReq.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { responseStream.CopyTo(content); } } return content.ToArray(); }
Demo • Asynchronous web requests… • Work bottom-up changing sync calls to async calls • Add await, async, and Task or Task<T> as needed Asynchronous Version private async Task<byte[]>GetURLContentsAsync(string url) { varcontent = new MemoryStream(); varwebReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = await webReq.GetResponseAsync()) { using (Stream responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(content); } } return content.ToArray(); }
Summary • Thread-based execution model at very bottom • Task-based execution model on top • For Performance: • Prefer Task Parallel Library • For Responsiveness: • Prefer Async / Await
Thank you for attending! • Presenter: Joe Hummel • Email: joe@joehummel.net • Materials: http://www.joehummel.net/downloads.html • For more info: • MSDN Magazine, October 2011 (3 articles): • “Easier Asynchronous Programming with the New Visual Studio AsyncCTP” • “Pause and Play with Await” • “Async Performance: Understanding the Costs of Async and Await”