parallel programming in .net
DESCRIPTION
Parallel Programming in .NET. Kevin Luty. Agenda. History of Parallelism Benefits of Parallel Programming and Designs What to Consider Defining Types of Parallelism Design Patterns and Practices Tools Supporting Libraries. History of Parallelism. Hardware - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/1.jpg)
Parallel Programming in .NET
Kevin Luty
![Page 2: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/2.jpg)
History of Parallelism Benefits of Parallel Programming and
Designs What to Consider Defining Types of Parallelism Design Patterns and Practices Tools Supporting Libraries
Agenda
![Page 3: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/3.jpg)
Hardware 1960-1970’s see parallel hardware in
supercomputers Early 1980’s – super computer built with 64
8086/8087 microprocessors Late 1980’s – Cluster computing power Moore’s Law Amdahl’s Law Most recently – # of cores over clock speeds
History of Parallelism
![Page 4: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/4.jpg)
Moore’s Law
Moore’s Law
![Page 5: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/5.jpg)
Amdahl’s Law
![Page 6: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/6.jpg)
Hardware 1960-1970’s sees parallel hardware in
supercomputers Early 1980’s – super computer built with 64
8086/8087 microprocessors Late 1980’s – Cluster computing power Moore’s Law Amdahl’s Law Most recently – # of cores over clock speeds
History of Parallelism
![Page 7: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/7.jpg)
Software One processor means sequential programs Few APIs that promote/make use of parallel
programming 1990’s – No standards were created for parallel
programming By 2000 – Message Passing Interface (MPI),
POSIX threads (pthreads), Open Mulitprocessing (OpenMP)
History of Parallelism
![Page 8: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/8.jpg)
Task Parallel Library (TPL) for .NET Hardware capabilities Easy to write, PLINQ
You can use all the cores! Timing Tools available for debugging Cost Effective
Benefits of Parallel Programming
![Page 9: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/9.jpg)
Define: What needs to be done? What data is being modified? Current state? Cost Synchronization vs. Asynchronization
Output = What pattern is best
What To Consider
![Page 10: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/10.jpg)
Parallelism – Programming with multiple threads, where it is expected that threads will execute at the same time on multiple processors. Its goal is to increase throughput.
Defining Types of Parallelism
![Page 11: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/11.jpg)
Data Parallelism – When there is a lot of data, and the same operations must be performed on each piece of data
Task Parallelism – There are many different operations that can run simultaneously
Defining Types of Parallelism
![Page 12: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/12.jpg)
![Page 13: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/13.jpg)
Perform the same independent operation for each element Most common problem is not noticing
dependencies How to notice dependencies
Shared variables Using properties of an object
Parallel Loops
![Page 14: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/14.jpg)
Parallel Loops
![Page 15: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/15.jpg)
Helpful Properties of TPL .Break and .Stop CancellationToken MaxDegreeOfParallelism
Exception Handling ExceptionAggregate
Parallel Loops
![Page 16: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/16.jpg)
Partitioning
Oversubscription and Undersubscription
Parallel Loops
![Page 17: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/17.jpg)
Making use of Parallel Loops Makes use of unshared, local variables Multiple inputs, single output
Parallel Aggregation
![Page 18: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/18.jpg)
Simple Example:
Parallel Aggregation
![Page 19: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/19.jpg)
Data Parallelism Also known as Fork/Join Pattern Uses System.Threading.Task namespace
TaskFactory Invoke Wait/WaitAny/WaitAll StartNew
Handling Exceptions
Parallel Tasks
![Page 20: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/20.jpg)
Handling Exceptions Exceptions are deferred until Task is done AggregateException
CancellationTokenSource Can also cancel tasks outside of a Task
Parallel Tasks
![Page 21: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/21.jpg)
Work Stealing
Parallel Tasks
![Page 22: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/22.jpg)
Seen like an assembly line
Pipelines
![Page 23: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/23.jpg)
Uses BlockingCollection<T> CompleteAdding
Most problems in this design due to starvation/blocking
Pipelines
![Page 24: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/24.jpg)
Future dependencies Wait/WaitAny/WaitAll
Sequential/Parallel Example
Futures
![Page 25: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/25.jpg)
Futures
![Page 26: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/26.jpg)
Model-View-ViewModel
Futures
![Page 27: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/27.jpg)
Continuous Task adding Complete Small Tasks then Larger Tasks Binary Trees and Sorting
Dynamic Task Parallelism
![Page 28: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/28.jpg)
.NET Performance Profiler (Red-Gate) JustTrace (Telerik) GlowCode (Electric Software) Performance Profiler (Visual Studio 2010
Ultimate) Concurrency Visualizer CPU Performance Memory Management
Tools
![Page 29: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/29.jpg)
Task Parallel Library PLINQ (Parallel Language Integrated Query)
Easy to learn Rx (Reactive Extensions)
Supportive Libraries for .NET
![Page 30: Parallel Programming in .NET](https://reader035.vdocuments.net/reader035/viewer/2022062309/5681364c550346895d9dcda3/html5/thumbnails/30.jpg)
http://en.wikipedia.org/wiki/Moore%27s_law Campbell, Colin, et al. Parallel Programming with
Microsoft .NET: Design Patterns for Decomposition and Coordination on Multicore Architectures. Microsoft, 2010. Print.
Data Parallelism (n.d.). In Wikipedia. Retrieved from http://en.wikipedia.org/wiki/Data_parallelism
Hillar, Gaston C. Professional Parallel Programming with C#: Master Parallel Extensions with .NET 4. Indiana: Wiley. 2011. Print.
Rx Extensions (n.d.). In Microsoft. Retrieved from http://msdn.microsoft.com/en-us/data/gg577609.aspx.
T. G. Mattson, B. A. Sanders, and B. L. Massingill. Patterns for Parallel Programming. Addison-Wesley, 2004.
References