an introduction to what’s available in .net 4.0 parallel...

26
PARALLEL PROGRAMMING ON THE .NET PLATFORM An Introduction to What’s Available in .NET 4.0

Upload: others

Post on 26-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

PARALLEL PROGRAMMING ON THE .NET PLATFORM

An Introduction to What’s Available in .NET 4.0

Page 2: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

About Me

Jeff Barnes

.NET Software Craftsman @ DAXKO

Microsoft MVP in Connected Systems

ALT.NET Supporter

[email protected]

http://jeffbarnes.net/blog

http://twitter.com/jeff_barnes

Page 3: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Disclaimer

I do not claim to be an expert in anything!

I’m here to simply share things that I have learned regarding topics in which I am quite passionate.

Question everything I say and form your own opinions based on the information!

Page 4: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Agenda

What is parallel programming?

Why is it necessary?

Why is it so hard (or perceived to be)?

What can .NET 4.0 do to make it easier?

Page 5: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

What is Parallel Programming?

Basically means “multi-threading” or doing more than one thing at the same time

Often refers to:

Various patterns to achieve parallelism in different scenarios.

Higher level abstractions than simply working with threads.

Page 6: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Do I Really Need To Use It?

In a word…

YES!!!

Page 7: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

But…Be Practical

Not literally every app requires it, but the potential benefit frequently exists.

Current hardware will increasingly necessitate parallelism to achieve desired levels of throughput and scalability

Page 8: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Hardware Limitations

CPUs aren’t getting “faster”

Cores will only increase in the years to come

The Result:

Can’t get away with waiting for CPU to get faster

Serial (traditional) logic won’t scale to multi-cores

Parallelism will no longer be an option

Page 9: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

History of Clock Rate

Page 10: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Serial Code on Multi-Cores

Core 1 Core 2 Core 3 Core 4

Order Queue

foreach (var order in get_orders_from_queue())

{

process(order);

}

Zzzz Zzzz Zzzz

Page 11: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Parallel Code on Multi-Cores

Core 1 Core 2 Core 3 Core 4

Order Queue

foreach (var order in get_orders_from_queue())

{

// Asynchronous Dispatching Goo Goes Here

process(order);

}

Page 12: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Why Is Parallelism So Hard?

Threads can be difficult to grok

Mutable state shared between threads

Synchronization is complex and error prone

Increases level of programming complexity

Simply put: it is not a trivial problem area

Page 13: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Subtle Problems Can Bite You

Increment / Decrement Count

Dictionary Operations

Add To or Removing From a List

Let’s look at an example

Page 14: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

.NET 4.0 To The Rescue

Concurrent Collection Data Structures

New Lock Primitives

Overhaul of the Thread Pool

Parallel LINQ

Task Parallel Library

Debugging Support in VS2010

Profiling Support in VS2010

Page 15: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Concurrent Data Structures

Found in System.Collections.Concurrent

BlockingCollection

ConcurrentBag

ConcurrentDictionary

ConcurrentQueue

ConcurrentStack

Page 16: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Locking Primitives

SemaphoreSlim

SpinWait

SpinLock

CountdownEvent

ManualResetEventSlim

Page 17: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

.NET 3.5 Thread Pool

Core 1 Core 2 Core 3 Core 4

GlobalQueue

ThreadPool.QueueUserWorkItem(…);

1

2

3

4

5

6

7

Page 18: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

.NET 4.0 Thread Pool

Core 1 Core 2 Core 3 Core 4

GlobalQueue

Parallel.For

Parallel.ForEach

Parallel.Invoke

Task…

LocalQueue

LocalQueue

LocalQueue

LocalQueue

1

2

3

4

1

5

6

7

7

6

Page 19: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Parallel Extension Methods

Enables you to easily parallelize iterative blocks of code

Parallel.For

Parallel.ForEach

Parallel.Invoke

Page 20: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Parallel LINQ

Made possible via IParallelEnumerable

Inherits Ienumerable

Adds a few methods (AsParallel)

Page 21: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Lazy Initialization

Lazy<>

Allows first class support for lazily initializing a value upon the first request.

Much easier than hand rolling your own solution.

Page 22: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Tasks

Task represents an asynchronous operation

Higher level abstraction than a thread

Much easier to work with

Supports concepts such as:

Continuations

Tracking Status

Cancellation

Page 23: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

VS2010 Profiling & Debugging

Tasks Window

Threads Window

Profiling for:

CPU

Memory

Concurrency

And More

Page 24: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Key Takeaways

.NET 4.0 makes parallelism much easier

Not a silver bullet

You still have to be aware of common issues

Be mindful of shared state!

Page 25: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?
Page 26: An Introduction to What’s Available in .NET 4.0 PARALLEL ...jeffbarnes.net/download/presentations/parallel_programming.pdf · Agenda What is parallel programming? Why is it necessary?

Resources

http://msdn.microsoft.com/concurrency

From there, you can find:

Links to MSDN Code Gallery

Link to Stephen Toub’s Excellent Paper

Links to other pertinent blogs and samples