1 / 18

Parallel Programming: Responsiveness vs. Performance

Parallel Programming: Responsiveness vs. Performance. Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight , LLC Professor: U. of Illinois, Chicago. email: jhummel2@uic.edu stuff: http://www.joehummel.net/downloads.html. Parallel programming… Why is this important?.

tamiw
Download Presentation

Parallel Programming: Responsiveness vs. Performance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parallel Programming: Responsiveness vs. Performance Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email: jhummel2@uic.edu stuff: http://www.joehummel.net/downloads.html

  2. Parallel programming… • Why is this important? Operations may take unpredictable amounts of time — you cannot wait and do nothing… 1. 2. Some operations are data or time intensive — the only way to solve faster is to process in parallel…

  3. Responsiveness vs. Performance • Better responsiveness? • Asynchronous programming • Long-running operations • I/O operations • OS calls • Better performance? • Parallel programming • Numerical processing • Data analysis • Big data

  4. DEMO

  5. SWsolution for better responsiveness? • Multithreading Main GUI <<start Work>> interactwith user Work Stmt1; Stmt2; Stmt3; Workerthread Mainthread Operating System Rapidly switches CPU from one thread to the other, effectively running both at the "same time" C

  6. Multithreaded programming in the UI void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } using System.Threading; void button1_Click(…) { t = new thread(code); t.Start(); } • void code() • { • var result = DoLongLatencyOp(); • lstBox.Items.Add(result); • } click, start, return

  7. DEMO

  8. Threads are… • expensiveto create (involves OS) • easy to over-subscribe (i.e. create too many) • tediousto program (lots of little details not shown in PPT) • an obfuscation(intent of code is harder to recognize)

  9. A Better Solution: Tasks • Separate notion of work (“task”) from the workers (“threads”) Task == a unit of work; an object denoting an ongoing operation or computation.

  10. 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

  11. DEMO

  12. Task-based programming in the UI void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } using System.Threading.Tasks; void button1_Click(…) { var context = // grab UI thread context to run UI task: TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(()=> { return DoLongLatencyOp(); } ).ContinueWith((antecedent) => { lstBox.Items.Add(antecedent.Result); }, context // execute this task on UI thread: ); }

  13. An Even Better solution? • This is where language design can help… • Why not have the compiler generate pattern for us? • Solution: async / await keywords in C# void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } using System.Threading.Tasks; async void button1_Click(…) { var result = awaitTask.Run(() => DoLongRunningOp()); lstBox.Items.Add(result); }

  14. DEMO

  15. async / await async void button1_Click(…) { var result = awaitTask.Run(() => DoLongRunningOp()); lstBox.Items.Add(result); } Method *may* perform async, long-latency op void button1_Click(…) { Task.Run( () => { return DoLongLatencyOp(); } .ContinueWith( (prev) => { var result = prev.Result; lstBox.Items.Add(result); } } • Tells compiler to run this task on a separate thread AND setup a callback to execute remaining code when thread finishes… click, start task, return runs on a separate worker thread runs on main GUI thread when above finishes

  16. Responsiveness vs. Performance • Better responsiveness? • Asynchronous programming • Long-running operations • I/O operations • OS calls • Better performance? • Parallel programming • Numerical processing • Data analysis • Big data

  17. SWsolution for better performance? • Threading across different cores… C C Main <<start Work1>> <<start Work2>> My work… Work1 Stmt1; Stmt2; Stmt3; Work2 Stmt4; Stmt5; Stmt6; C C C C C C Workerthread Workerthread Mainthread

  18. DEMO

More Related