27
Igor Ostrovsky Software Engineer Microsoft Corporation Parallel Programming with the .NET Framework 4 and Visual Studio 2010 Introducing

Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Embed Size (px)

Citation preview

Page 1: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Igor Ostrovsky Software Engineer Microsoft Corporation

Parallel Programming with the .NET Framework 4 and Visual Studio 2010

Introducing

Page 2: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Agenda

Why

What Managed APIs/runtime (.NET 4)

Tools (in the VS2010 IDE)

Page 3: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Why The Manycore Shift

Page 4: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Why Problem and Goals

Free lunch is over

How much will you pay?

Multithreaded programming is hard today

Goals Developer productivity

Performance

Page 5: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Agenda Checkpoint

Why

What Managed APIs/runtime (.NET 4)

a.k.a. Parallel Extensions to .NET

Tools (in the VS2010 IDE)

Page 6: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Parallel Extensions to .NET Overview

Pure .NET libraries

Feature areas Task Parallel Library (TPL)

Parallel LINQ (PLINQ)

Thread-safe data structures and synchronization primitives

Enhanced ThreadPool

Page 7: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Parallel.For(0, N, i => { Compute(i); }); Parallel.ForEach(data, e => Compute(e));

Task Parallel Library (TPL) Loops

for (int i = 0; i < N; i++) { Compute(i); } foreach (T e in data) Compute(e);

Page 8: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Task Parallel Library (TPL) Loops

DEMO: Raytracer

Page 9: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

ParallelOptions pops = new ParallelOptions()

{

MaxDegreeOfParallelism = 2,

CancellationToken = myToken

};

Parallel.Invoke(pops,

() => A(),

() => B(),

() => C());

Task Parallel Library (TPL) Regions

A(); B(); C();

Page 10: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Task Parallel Library (TPL) Tasks

DEMO: Task examples DEMO: Reversi

Page 11: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Parallel LINQ (PLINQ) Language Integrated Query (LINQ) Overview

Visual Basic C# Others…

.NET Standard Query Operators

LINQ-enabled data sources

LINQ To Objects

LINQ To XML

LINQ To …

Page 12: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

.AsParallel().WithDegreeOfParallelism(4) .WithCancellation(myToken) .WithMergeOptions(ParallelMergeOptions.NotBuffered) .WithExecutionMode(ParallelExecutionMode.ForceParallelism)

Parallel LINQ (PLINQ) LINQ-To-Objects Parallelized

var results = from i in arrayOfIntegers where i % 2 == 0 select i;

var results = from i in arrayOfIntegers.AsParallel() where i % 2 == 0 select i;

Page 13: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Aggregate(3)

All(1)

Any(2)

AsEnumerable(1)

Average(20)

Cast(1)

Concat(1)

Contains(2)

Count(2)

DefaultIfEmpty(2)

Distinct(2)

ElementAt(1)

ElementAtOrDefault(1)

Empty(1)

Except(2)

First(2)

FirstOrDefault(2)

GroupBy(8)

GroupJoin(2)

Intersect(2)

Join(2)

Last(2)

LastOrDefault(2)

LongCount(2)

Max(22)

Min(22)

OfType(1)

OrderBy(2)

OrderByDescending(2)

Range(1)

Repeat(1)

Reverse(1)

Select(2)

SelectMany(4)

SequenceEqual(2)

Single(2)

SingleOrDefault(2)

Skip(1)

SkipWhile(2)

Sum(20)

Take(1)

TakeWhile(2)

ThenBy(2)

ThenByDescending(2)

ToArray(1)

ToDictionary(4)

ToList(1)

ToLookup(4)

Union(2)

Where(2)

Zip(1)

In .NET 4, ~50 operators w/ ~175 overloads

Parallel LINQ (PLINQ) Supported Operators

Page 14: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Parallel LINQ (PLINQ)

DEMO: Baby Names

Page 15: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Thread-safe Data Structures and Sync Primitives

Thread-safe, scalable collections IProducerConsumerCollection<T>

ConcurrentQueue<T> ConcurrentStack<T> ConcurrentBag<T>

ConcurrentDictionary<TKey, TValue>

Phases and work exchange

Barrier BlockingCollection<T> CountdownEvent

Partitioning

{Orderable}Partitioner<T> Partitioner.Create(…)

Initialization

Lazy<T>

ThreadLocal<T>

Locks

ManualResetEventSlim

SemaphoreSlim

SpinLock

SpinWait

Cancellation

CancellationTokenSource

CancellationToken

Page 16: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

ThreadPool in .NET 3.5

Global Queue

Worker Thread 0

Worker Thread N

Other Threads

Work Item 3

Work Item 2 Work Item 1

Page 17: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

ThreadPool in .NET 4

Lock-free Global Queue

Worker Thread 0

Worker Thread N

Other Threads

Local Work-

Stealing Queue

Local Work-

Stealing Queue

Work Item 3 Work Item 4 Work Item 5

Work Item 2 Work Item 1

Page 18: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Agenda Checkpoint

Why

What Managed APIs/runtime (.NET 4)

a.k.a. Parallel Extensions to .NET

Tools (in the VS2010 IDE) Debugging: Parallel Tasks and Parallel Stacks

Concurrency Visualizer

Page 19: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Tools Parallel Debugging

Two new debugger toolwindows

“Parallel Tasks”

“Parallel Stacks”

Page 20: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Tools “Parallel Tasks”

What threads are executing my Tasks? Where are my Tasks running (location, call stack)? What is the status of my Tasks?

Page 21: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Tools “Parallel Stacks”

Multiple call stacks in a single view Easy navigation to any executing method Rich UI (zooming, panning, tooltips, etc.)

Zoom

control Bird’s eye view

Page 22: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Tools Concurrency Visualizer

Page 23: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Tools Concurrency Visualizer: CPU Utilization View

Number of cores

Your Process

Idle Time

Other Processes

Page 24: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Tools Concurrency Visualizer: Threads View

Hide Uninteresting

threads

Active Legend

Call Stacks

Usage Hints

Measure time for interesting

segments

Page 25: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

Tools Concurrency Visualizer: Cores View

Migration visualization

Color per thread

Page 27: Introducing Parallel Programming with the .NET Framework 4 ...parlab.eecs.berkeley.edu/sites/all/parlab/files/Session1.pdf · Igor Ostrovsky Software Engineer Microsoft Corporation

© 2008 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.