“Parallel Programming with

“Parallel Programming with “Parallel Programming with - Start

Added : 2017-07-19 Views :64K

Download Presentation

“Parallel Programming with




Download Presentation - The PPT/PDF document "“Parallel Programming with" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.



Presentations text content in “Parallel Programming with

Slide1

“Parallel Programming with Async and Await”

Joe Hummel, PhDjoe@joehummel.netTechnical Staff: PluralsightAdjunct Professor: UIC, LUC

http://www.joehummel.net/downloads.html

Slide2

MotivationExecution modelParallel programming with TasksParallel programming with Async / AwaitDemos

2

Agenda

Slide3

3

Async vs. Parallel?

Async programming:Better responsiveness…GUIs (desktop, web, mobile)CloudWindows 8

Parallel programming:Better performance…EngineeringOil and GasPharmaScienceSocial media

C

C

C

C

C

C

C

C

Disk and network I/O

tasks

number crunching and big data processing

Slide4

4

Execution model

C

C

C

C

C

C

C

C

Main

thread

C

Main

<<start Work>>

if…

while…

Work

Stmt1;

Stmt2;

Stmt3;

Main

<<start Work1>>

<<start Work2>>

if…

while…

Work1

Stmt1;

Stmt2;

Stmt3;

Work2

Stmt4;

Stmt5;

Stmt6;

Worker

thread

Worker

thread

Worker

thread

Main

thread

Threads

share,

run asynchronously

Threads run in parallel

Single core:

Multicore:

Slide5

Threads (.NET 1.0)Async DelegatesQueueUserWorkItemBackgroundWorkerTask Parallel Library (.NET 4.0)Async / Await (.NET 4.5)

5

Numerous ways to program

Easier…

Slide6

Mandelbrot set…

6

Demo ― Performance

Slide7

Programming model based on concept of a Task

7

Task-based

Task

==

a unit of work

; an object denoting an

ongoing operation or computation

.

Slide8

8

Task-based execution model

C

C

C

C

C

C

C

C

Windows Process (.NET)

App

Domain

App

Domain

App

Domain

.NET Thread

Pool

worker

thread

worker

thread

worker

thread

worker

thread

Parallel.For

( ... );

task

task

task

task

Task Parallel Library

Resource Manager

Task Scheduler

Windows

Slide9

Asian options financial modeling…

9

Demo ― Responsiveness

Slide10

10

Async solution #1: Tasks

void button1_Click(…){ var uictx = // 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: );}

void button1_Click(…){ var result = DoLongLatencyOp(); lstBox.Items.Add(result);}

Slide11

11

Async solution #2: Async / Await

void button1_Click(…){ var result = DoLongLatencyOp(); lstBox.Items.Add(result);}

async void button1_Click(…) { var result = await Task.Run( () => DoLongRunningOp()); lstBox.Items.Add(result); }

T

ells compiler that method *may* perform an async, long-latency op

Tells compiler to execute operation but

don’t wait.

Instead, start op as a separate task, and setup a continuation to execute the remaining code when op finishes ― RUNNING ON THE SAME THREAD CONTEXT!

Slide12

For operations that may involve long latencyFile and network I/O are the classic use-caseFor chunks of work you want to run in parallelAnd you have multi-core hardware

12

When to use

Async

/ Await?

Slide13

Hides the complexities of async programmingNo visible callback, predictable exception handling, …Think chunky, not chattyi.e. designed for coarse-grain work / long-latency operationsDesigned to be used with APM patternAsynchronous Programming ModelAPIs that offer async calls via APM?File I/ONetwork I/OWindows 8 API

13

Observations…

Slide14

Async web calls are a classic use-case

14

Example

private byte[] GetURLContents(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = webReq.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { responseStream.CopyTo(content); } } return content.ToArray();}

Synchronous Version

Slide15

Asynchronous web requests…Work bottom-up changing sync calls to async callsAdd await, async, and Task or Task<T> as needed

15

Demo

private async Task<byte[]> GetURLContentsAsync(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = await webReq.GetResponseAsync()) { using (Stream responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(content); } } return content.ToArray();}

Asynchronous

Version

Slide16

16

That’s it!

Slide17

Thread-based execution model at very bottomTask-based execution model on topFor Performance:Prefer Task Parallel LibraryFor Responsiveness:Prefer Async / Await

17

Summary

Slide18

Presenter: Joe HummelEmail: joe@joehummel.netMaterials: http://www.joehummel.net/downloads.html For more info:MSDN Magazine, October 2011 (3 articles):“Easier Asynchronous Programming with the New Visual Studio Async CTP”“Pause and Play with Await”“Async Performance: Understanding the Costs of Async and Await”

18

Thank you for attending!


About DocSlides
DocSlides allows users to easily upload and share presentations, PDF documents, and images.Share your documents with the world , watch,share and upload any time you want. How can you benefit from using DocSlides? DocSlides consists documents from individuals and organizations on topics ranging from technology and business to travel, health, and education. Find and search for what interests you, and learn from people and more. You can also download DocSlides to read or reference later.
Youtube