Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parallel Programming: Responsiveness vs. Performance Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois,

Similar presentations


Presentation on theme: "Parallel Programming: Responsiveness vs. Performance Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois,"— Presentation transcript:

1 Parallel Programming: Responsiveness vs. Performance Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago 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… 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 3

4 DEMO 4

5 Multithreading SW solution for better responsiveness? Operating System Rapidly switches CPU from one thread to the other, effectively running both at the "same time" Operating System Rapidly switches CPU from one thread to the other, effectively running both at the "same time" Main thread C Main GUI > interact with user Main GUI > interact with user Work Stmt1; Stmt2; Stmt3; Work Stmt1; Stmt2; Stmt3; Worker thread 5

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

7 DEMO 7

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

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

10 Windows Process (.NET) App Domain App Domain App Domain App Domain App Domain App Domain.NET Thread Pool worker thread worker thread worker thread worker thread Parallel.For(... ); task global work queue Task Parallel Library Resource Manager Task Scheduler Windows Task-based Execution Model 10

11 DEMO 11

12 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: ); } 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: ); } void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } Task-based programming in the UI using System.Threading.Tasks; 12

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); } void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } async void button1_Click(…) { var result = await Task.Run(() => DoLongRunningOp()); lstBox.Items.Add(result); } async void button1_Click(…) { var result = await Task.Run(() => DoLongRunningOp()); lstBox.Items.Add(result); } using System.Threading.Tasks; 13

14 DEMO 14

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

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 16

17 Threading across different cores… SW solution for better performance? C C C C C C C C Main > My work… Main > My work… Work1 Stmt1; Stmt2; Stmt3; Work1 Stmt1; Stmt2; Stmt3; Work2 Stmt4; Stmt5; Stmt6; Work2 Stmt4; Stmt5; Stmt6; Worker thread Main thread 17

18 DEMO 18


Download ppt "Parallel Programming: Responsiveness vs. Performance Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois,"

Similar presentations


Ads by Google