An Introduction to What’s Available in .NET 4.0 PARALLEL...
Transcript of An Introduction to What’s Available in .NET 4.0 PARALLEL...
![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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/1.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/2.jpg)
About Me
Jeff Barnes
.NET Software Craftsman @ DAXKO
Microsoft MVP in Connected Systems
ALT.NET Supporter
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/3.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/4.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/5.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/6.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/7.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/8.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/9.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/10.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/11.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/12.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/13.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/14.jpg)
.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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/15.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/16.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/17.jpg)
.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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/18.jpg)
.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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/19.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/20.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/21.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/22.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/23.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/24.jpg)
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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/25.jpg)
![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?](https://reader033.fdocuments.in/reader033/viewer/2022051921/600e29111ed5c12a6e364b51/html5/thumbnails/26.jpg)
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