朱金生 -...

36

Transcript of 朱金生 -...

Page 1: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models
Page 2: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

George Chu (朱金生)Principal Group ManagerMicrosoft CorporationDEV318

Page 3: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Agenda

Why Parallelism, Why Now?

Hard problems

What Microsoft is doing about it

Concurrency features in Visual Studio 2010Concurrency Runtime

Programming Models

Tools

Lots of demos along the way

Page 4: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Moore’s Law

“The number of transistors incorporated in a chip will approximately double every 24 months.”

Gordon Moore - Intel

Page 5: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

CPUs Are Hitting a WallP

ow

er

De

nsi

ty(W

/cm

2)

4004

8080

80858086

286386

486

Pentium®

processors

1

10

100

1,000

10,000

’70 ’80 ’90 ’00 ’10

Hot Plate

Nuclear Reactor

Rocket Nozzle

Page 6: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

The Manycore Shift

“[A]fter decades of single core processors, the high volume processor industry has gone from single to dual to quad-core in just the last two years. Moore’s Law scaling should easily let us hit 80-core mark in mainstream processors within the next ten years and quite possibly even less.”

Justin Rattner, CTO, Intel (February 2007)

“If you haven’t done so already, now is the time to take a hard look at the design of your application, determine what operations are CPU-sensitive now or are likely to become so soon, and identify how those places could benefit from concurrency.”

Herb Sutter, C++ Architect, Microsoft (March 2005)

Page 7: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Example: Matrix Multiplication

void MatrixMult(int size,, double** m1, double** m2, double** result)

{for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {result[i][j] = 0;for (int k = 0; k < size; k++) {

result[i][j] += m1[i][k] * m2[k][j];}

}}

}

Page 8: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Manual Parallel Solutionvoid MatrixMult(

int size, double** m1, double** m2, double** result) {int N = size;int P = NUMPROCS; int Chunk = N / P;HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);long counter = P;for (int c = 0; c < P; c++) {std::thread t ([&,c] {for (int i = c * Chunk;

i < (c + 1 == P ? N : (c + 1) * Chunk); i++) {for (int j = 0; j < size; j++) {

result[i][j] = 0;for (int k = 0; k < size; k++) {result[i][j] += m1[i][k] * m2[k][j];

}}

}if (InterlockedDecrement(counter) == 0)SetEvent(hEvent);

}); }WaitForSingleObject(hEvent,INFINITE);CloseHandle(hEvent);

}

Static Partitioning

Synchronization Knowledge

Error prone

Lots of boilerplate

Tricks

Lack of thread reuse

Heavy synchronization

Page 9: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Developer! Developer! Developer!

Make parallel programming easier for

developers of all skill levels

Express parallelism

and focus on the problem

Improve efficiency and

scalability

Simplify the design and

testing

Page 10: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Visual Studio 2010Tools/Programming Models/Runtimes

Parallel Pattern Library

Resource Manager

Task Scheduler

Task Parallel Library

PLINQ

Managed Library Native LibraryKey:

ThreadsOperating System

Concurrency Runtime

Programming Models

ThreadPool

Task Scheduler

Resource Manager

Data Stru

ctures D

ata

Stru

ctu

res

Tools

Tools

ParallelDebugger

Tool Windows

Profiler Concurrency

Analysis

AgentsLibrary

Win7: UMS Threads

Page 11: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

LINQ Ray Tracer (Image Processing)Game of Life (Simulation)

Page 12: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Visual Studio 2010Tools/Programming Models/Runtimes

Parallel Pattern Library

Resource Manager

Task Scheduler

Task Parallel Library

PLINQ

Managed Library Native LibraryKey:

ThreadsOperating System

Concurrency Runtime

Programming Models

ThreadPool

Task Scheduler

Resource Manager

Data Stru

ctures D

ata

Stru

ctu

res

Tools

Tools

ParallelDebugger

Tool Windows

Profiler Concurrency

Analysis

AgentsLibrary

Win7: UMS Threads

Page 13: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Concurrency RuntimeValue Propositions

Increase developer productivity

Increase scalability

Maximize CPU utilization

Reduce overhead

Avoid system oversubscription

Enable composition of parallel libraries

Provide extensibility

Visual Studio 2010

Programming Models

Concurrency Runtime

Operating System

Page 14: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Concurrency RuntimeKey Components

Task Scheduler

Policy-based instances

Work stealing

Cooperative blocking

Resource Manager

Concurrent Suballocator

Win7 support

Visual Studio 2010

Programming Models

Concurrency Runtime

Operating System

Page 15: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Dynamic Load Balancing

CPU0 CPU1 CPU2 CPU3CPU0 CPU1 CPU2 CPU3

Page 16: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

User Mode Scheduler For Tasks

Concurrency Runtime Scheduler

Worker Thread 1

Worker Thread p

Program Thread

GlobalQueue

LocalQueue

LocalQueue

Task 1Task 2

Task 3Task 5

Task 4

Task 6

Page 17: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Windows 7

UMS threads

> 64 proc

Page 18: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Visual Studio 2010Tools/ProgrammingModels/Runtimes

Parallel Pattern Library

Resource Manager

Task Scheduler

Task Parallel Library

PLINQ

Managed Library Native LibraryKey:

ThreadsOperating System

Concurrency Runtime

Programming Models

ThreadPool

Task Scheduler

Resource Manager

Data Stru

ctures D

ata

Stru

ctu

res

Tools

Tools

ParallelDebugger

Tool Windows

Profiler Concurrency

Analysis

AgentsLibrary

Win7: UMS Threads

Page 19: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Programming ModelsValue Propositions

Increase developer productivity

Make parallel programming safer

Improve scalability

Improve reusability

Visual Studio 2010

Concurrency Runtime

Programming Models

Operating System

Page 20: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Programming ModelsKey Concepts

Parallel Extensions to .NET 4.0PLINQ

Task Parallel Library

Parallel Pattern Library

Asynchronous Agents Library

Visual Studio 2010

Concurrency Runtime

Programming Models

Operating System

Page 21: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Programming ModelsKinds of Parallelism

Task ParallelParallel Invoke

Tasks

Data ParallelParallel Loop

PLINQ

Dataflow ParallelFutures

Continuations

Message Blocks & Asynchronous Agents

Visual Studio 2010

Concurrency Runtime

Programming Models

Operating System

Page 22: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Strassens (Scientific)Option Pricing (Financial Modeling)

Page 23: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Visual Studio 2010Tools/Programming Models/Runtimes

Parallel Pattern Library

Resource Manager

Task Scheduler

Task Parallel Library

PLINQ

Managed Library Native LibraryKey:

ThreadsOperating System

Concurrency Runtime

Programming Models

ThreadPool

Task Scheduler

Resource Manager

Data Stru

ctures D

ata

Stru

ctu

res

Tools

Tools

ParallelDebugger

Tool Windows

Profiler Concurrency

Analysis

AgentsLibrary

Win7: UMS Threads

Page 24: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Visual Studio 2010Value Propositions

Increase the correctness andperformance of your parallelapplications

Programming Models

Concurrency Runtime

Visual Studio 2010

Operating System

Page 25: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Visual Studio 2010Key Concepts

Parallel Debugger WindowsParallel Tasks

Parallel Stacks

Parallel Performance AnalyzerCore Utilization

Thread Blocking

Thread Migration

Programming Models

Concurrency Runtime

Visual Studio 2010

Operating System

Page 26: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Debugging Parallel Apps in VS2010

Page 27: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Parallel Performance Analyzer

Your Process

Idle time

Other processes

Core Utilization/ Concurrency

Thread Blocking

Blocked

Executing

Number of cores

Poor load balancing

Page 28: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Profile LINQ Ray Tracer

Page 29: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Summary of Visual Studio 2010 Concurrency Features

Parallel Extensions to .NET 4.0

Concurrency Runtime

Parallel Pattern Library

Asynchronous Agents Library

Parallel Debugger Tool Windows

Profiler

Page 30: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Parting Advice

Use declarative modes of parallelism where possible

Over-decompose

Win7 is great for concurrency!

Use parallel debugger and profiler tools to uncover and understand concurrency problems!

Page 31: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models
Page 32: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

http://microsoft.com/technet

Resources for IT Professionals

http://microsoft.com/msdn

Resources for Developers

www.microsoft.com/learning

Microsoft Certification & Training Resources

Resources

Page 33: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Related Content

DEV323: “A lap around building SharePoint 2010 applications in VS2010” by Rong LuDay 1 (November 2, 2009): 17:00 – 18:00

DEV377: “Building Business Application in Silverlight” by Xiaoying GuoDay 2 (November 3, 2009): 16:45 – 18:00

Page 34: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Track Resources

Parallel Computing Developer Center: http://msdn.microsoft.com/en-us/concurrency/default.aspx

Native Concurrency Blog: http://blogs.msdn.com/nativeconcurrency

Managed Concurrency Blog: http://blogs.msdn.com/pfxteam/

Page 35: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

Complete an

evaluation on

CommNet and

enter to win!

Page 36: 朱金生 - download.microsoft.comdownload.microsoft.com/documents/hk/technet/techdays2009/DEV318.pdfConcurrency features in Visual Studio 2010 Concurrency Runtime Programming Models

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS,

IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.