Parallel Computing in .NET James Rapp
TOPICS • Task Parallel Library • Parallel Debugging • Parallel Profiling
TYPES OF PARALLELISM Data Parallelism Occurs when you carry out the same operation on multiple subsets of the data simultaneously and independently. Examples: • Matrix multiplication • Jacobi Relaxation • Ray Tracing
TYPES OF PARALLELISM Task Parallelism One or more independent tasks running concurrently Examples: • Sorting • Dataflow Networks • Asynchrony
WHY IS PARALLELISM RELEVANT? • Moore’s law: Transistor count doubles every two years • We’ve reached the physical limits of clock speed • Solution: Scale horizontally, not vertically • The free lunch is over: We now must right parallel code if we want to benefit from better hardware
TASK PARALLEL LIBRARY Implementing Thread-Based Parallelism • Tedious and error-prone • Difficult to read • Threads are heavyweight • Wrong abstraction level TPL • Simplifies parallel programming • Raises the level of abstraction • Encapsulates common patterns • Enables fine-grained control
Tasks (System.Threading.Tasks) Task – A lightweight schedulable unit of work. • Represents an asynchronous operation • Higher level of abstraction than a ThreadPool work item Purpose of Tasks • Simplifies low-level details such as cancellation or exception handling. • More control – rich APIs for continuations, custom scheduling, etc.
.NET PARALLELISM OVERVIEW Operating System Threads Concurrency Runtime ThreadPool Task Scheduler Resource Manager Developer Tools Parallel Debugger Concurrency Visualizer Programming Models PLINQ Task Parallel Library
A NOTE ON CHILD TASKS Behavior Detached Attached Parent waits for child to complete No Yes Parent propagates exceptions thrown by child No Yes Status of parent depends on status of child No Yes
CONCURRENT COLLECTIONS (System.Collections.Concurrent) Class Description ConcurrentDictionary Collection of key/value pairs that can be accessed by safely by multiple threads ConcurrentQueue Thread-safe FIFO collection ConcurrentStack Thread-safe LIFO collection Partitioner Provides common partitioning strategies for arrays, lists, and enumerables ConcurrentBag Thread-safe, unordered collection of objects BlockingCollection Provides blocking and bounding capabilities for thread-safe collections that implement IProducerConsumerCollection<T> Etc. • Thread-safe collection classes – Optimized for performance – Should be used instead of System.Collections and System.Collections.Generic
TPA DATAFLOW (System.Threading.Tasks.Dataflow) • Meant for course-grained dataflow or pipeline tasks • Useful for processing data as it becomes available (e.g. red eye reduction on a web cam)
.NET ASYNC • Symplifies asynchronous programming • Improves UI responsiveness and performance async Task<int> AccessTheWebAsync() { HttpClient client = new HttpClient(); Task<string> getStringTask = client.GetStringAsync(“www.geneca.com"); //Work that doesn’t rely on getStringTask DoIndependentWork(); string urlContents = await getStringTask; return urlContents.Length; }
NOT COVERED (Suggestions for Further Research) • Other Parallel Programming Models in .NET – PLINQ • Native Concurrency – Parallel Patterns Library • Data Parallelism on the GPU – C++ AMP
Parallel Computing in .NET

Parallel Computing in .NET

  • 1.
    Parallel Computing in.NET James Rapp
  • 2.
    TOPICS • Task ParallelLibrary • Parallel Debugging • Parallel Profiling
  • 3.
    TYPES OF PARALLELISM DataParallelism Occurs when you carry out the same operation on multiple subsets of the data simultaneously and independently. Examples: • Matrix multiplication • Jacobi Relaxation • Ray Tracing
  • 4.
    TYPES OF PARALLELISM TaskParallelism One or more independent tasks running concurrently Examples: • Sorting • Dataflow Networks • Asynchrony
  • 5.
    WHY IS PARALLELISMRELEVANT? • Moore’s law: Transistor count doubles every two years • We’ve reached the physical limits of clock speed • Solution: Scale horizontally, not vertically • The free lunch is over: We now must right parallel code if we want to benefit from better hardware
  • 6.
    TASK PARALLEL LIBRARY ImplementingThread-Based Parallelism • Tedious and error-prone • Difficult to read • Threads are heavyweight • Wrong abstraction level TPL • Simplifies parallel programming • Raises the level of abstraction • Encapsulates common patterns • Enables fine-grained control
  • 7.
    Tasks (System.Threading.Tasks) Task –A lightweight schedulable unit of work. • Represents an asynchronous operation • Higher level of abstraction than a ThreadPool work item Purpose of Tasks • Simplifies low-level details such as cancellation or exception handling. • More control – rich APIs for continuations, custom scheduling, etc.
  • 8.
    .NET PARALLELISM OVERVIEW OperatingSystem Threads Concurrency Runtime ThreadPool Task Scheduler Resource Manager Developer Tools Parallel Debugger Concurrency Visualizer Programming Models PLINQ Task Parallel Library
  • 9.
    A NOTE ONCHILD TASKS Behavior Detached Attached Parent waits for child to complete No Yes Parent propagates exceptions thrown by child No Yes Status of parent depends on status of child No Yes
  • 10.
    CONCURRENT COLLECTIONS (System.Collections.Concurrent) Class Description ConcurrentDictionaryCollection of key/value pairs that can be accessed by safely by multiple threads ConcurrentQueue Thread-safe FIFO collection ConcurrentStack Thread-safe LIFO collection Partitioner Provides common partitioning strategies for arrays, lists, and enumerables ConcurrentBag Thread-safe, unordered collection of objects BlockingCollection Provides blocking and bounding capabilities for thread-safe collections that implement IProducerConsumerCollection<T> Etc. • Thread-safe collection classes – Optimized for performance – Should be used instead of System.Collections and System.Collections.Generic
  • 11.
    TPA DATAFLOW (System.Threading.Tasks.Dataflow) • Meantfor course-grained dataflow or pipeline tasks • Useful for processing data as it becomes available (e.g. red eye reduction on a web cam)
  • 12.
    .NET ASYNC • Symplifiesasynchronous programming • Improves UI responsiveness and performance async Task<int> AccessTheWebAsync() { HttpClient client = new HttpClient(); Task<string> getStringTask = client.GetStringAsync(“www.geneca.com"); //Work that doesn’t rely on getStringTask DoIndependentWork(); string urlContents = await getStringTask; return urlContents.Length; }
  • 13.
    NOT COVERED (Suggestionsfor Further Research) • Other Parallel Programming Models in .NET – PLINQ • Native Concurrency – Parallel Patterns Library • Data Parallelism on the GPU – C++ AMP