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

Slides:



Advertisements
Similar presentations
Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
Advertisements

Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Adjunct Professor: U. of Illinois, Chicago and Loyola University Chicago
{ async patterns } - or - using the asynchronous library in the.Net 4.5 Framework for more than keeping your UI responsive.
please wait for the next slide clicking won’t make it come any faster.
Async Programming WITH ASYNC TASK
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 TY, Sept 2011.
Tools for Investigating Graphics System Performance
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
1 Thread Pools. 2 What’s A Thread Pool? A programming technique which we will use. A collection of threads that are created once (e.g. when server starts).
App Windows UI object Windows object App code Windows object.
Joe Hummel, PhD Technical Staff: Pluralsight Adjunct Professor: UIC, LUC
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
Parallel Extensions A glimpse into the parallel universe By Eric De Carufel Microsoft.NET Solution Architect at Orckestra
please wait for the next slide clicking won’t make it come any faster.
About Me Microsoft MVP Intel Blogger TechEd Israel, TechEd Europe Expert C++ Book
SIMPLE PARALLEL PROGRAMMING WITH PATTERNS AND OMNITHREADLIBRARY PRIMOŽ GABRIJELČIČ SKYPE: GABR42
Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago stuff:
Joe Hummel, the compiler is at your service Chicago Code Camp 2014.
Future of VB and C# Lucian Wischik VB Language PM Microsoft.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
WHO WILL BENEFIT FROM THIS TALK TOPICS WHAT YOU’LL LEAVE WITH ASP.NET developers, including Web Forms & MVC History of async programming in.NET How async.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
Asynchronous Programming Writing Asynchronous Code in C# SoftUni Team Technical Trainers Software University
Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago stuff:
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Joe Hummel, the compiler is at your service SDC Meetup, Sept 2014.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
1 OS Review Processes and Threads Chi Zhang
Other news? async and await Anonymous types (var, dynamic) Tuples Object instantiation Extension methods UCN Teknologi/act2learn1FEN 2014.
please wait for the next slide clicking won’t make it come any faster.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Multithreading The objectives of this chapter are: To understand the purpose of multithreading To describe Java's multithreading mechanism.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
Patterns of Parallel Programming with.NET 4 Stephen Toub Principal Architect Parallel Computing Platform Microsoft Corporation
C# 5.0 Alex Davies 22 nd December What we will cover C# 5.0,.NET 4.5, Visual Studio 11 Caller Info Attributes Upgrade from synchronous to asynchronous.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Joe Hummel, the compiler is at your service Chicago Coder Conference, June 2016.
Async Made Simple in Windows 8, with C# and Visual Basic Alex Turner Program Manager VB/C# Compilers Microsoft Corporation DEV332.
Asynchronous Programming Writing Concurrent Code in C# SoftUni Team Technical Trainers Software University
THE FUTURE OF C#: GOOD THINGS COME TO THOSE WHO ‘AWAIT’ Joseph Albahari SESSION CODE: DEV411 (c) 2011 Microsoft. All rights reserved.
Concepts of Multithreading CS 378 – Mobile Computing for iOS Dr. William C. Bulko.
Chapter 4 – Thread Concepts
Introduction to Xamarin C# Everywhere
The Future of C# and Visual Basic
TechEd /6/2018 6:15 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Thread Fundamentals Header Advanced .NET Threading, Part 1
Day 12 Threads.
Chapter 4 – Thread Concepts
Async or Parallel? No they aren’t the same thing!
Chapter 4: Multithreaded Programming
Staying Afloat in the .NET Async Ocean
12 Asynchronous Programming
Creating Windows Store Apps Using Visual Basic
Chapter 15, Exploring the Digital Domain
TechEd /9/2018 4:17 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Tiberiu Covaci Senior Technical Trainer Many-core
Android Topics UI Thread and Limited processing resources
Lecture Topics: 11/1 General Operating System Concepts Processes
Build /2/2019 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Hold up, wait a minute, let me put some async in it
Chapter 4: Threads & Concurrency
Why Threads Are A Bad Idea (for most purposes)
Building Web Applications with Microsoft ASP
Introduction to OS (concept, evolution, some keywords)
Introduction to OS (concept, evolution, some keywords)
Why Threads Are A Bad Idea (for most purposes)
Why Threads Are A Bad Idea (for most purposes)
03 | Async Programming & Networking Intro
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

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 stuff:

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…

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

DEMO 4

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

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

DEMO 7

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)

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

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

DEMO 11

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

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

DEMO 14

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

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

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

DEMO 18