32
Parallel programming in modern world .NET TECHNICS

Parallel programming in modern world .net technics shared

Embed Size (px)

Citation preview

Page 1: Parallel programming in modern world .net technics   shared

Parallel programming in modern world.NET TECHNICS

Page 2: Parallel programming in modern world .net technics   shared

parallelism vs concurrency

Concurrency existence of multiple threads of execution

goal of concurrency is to prevent thread starvation

concurrency is required operationally

Parallelism concurrent threads execute at the same time on multiple cores

parallelism is only about throughput

It is an optimization, not a functional requirement

Page 3: Parallel programming in modern world .net technics   shared

Limitations to linear speedup of parallel code

Serial code

Overhead from parallelization

Synchronization

Sequential input/output

Page 4: Parallel programming in modern world .net technics   shared

Parallel Speedup Calculation

Amdahl’s Law:

Gustafson’s Law: - 0n

Page 5: Parallel programming in modern world .net technics   shared

Phases of parallel development

Finding Concurrency Task Decomposition pattern

Data Decomposition pattern

Group Tasks Pattern

Order Tasks Pattern

Data Sharing pattern

Algorithm Structures

Support Structures

Implementation Mechanisms

Page 6: Parallel programming in modern world .net technics   shared

The Algorithm Structure Pattern

Task Parallelism Pattern

Divide and Conquer Pattern

Geometric Decomposition Pattern

Recursive Data Pattern

Pipeline Pattern

Page 7: Parallel programming in modern world .net technics   shared

The Supporting Structures Pattern

SPMD (Single Program/Multiple Data)

Master/Worker

Loop Parallelism

Fork/Join

Page 8: Parallel programming in modern world .net technics   shared

Data Parallelism

Search for Loops

Unroll Sequential Loops

Evaluating Performance Considerations: Conduct performance benchmarks to confirm potential performance

improvements

When there is minimal or no performance gain, one solution is to change the chunk size

Parallel.For and Parallel.ForEach ParallelLoopState for breaking

Page 9: Parallel programming in modern world .net technics   shared

Reduction/Aggregation

Page 10: Parallel programming in modern world .net technics   shared

Variations of Reduce

Scan pattern - each iteration of a loop depends on data computed in the previous iteration.

Pack pattern - uses a parallel loop to select elements to retain or discard => The result is a subset of the original input.

Map Reduce

Page 11: Parallel programming in modern world .net technics   shared

MapReduce Pattern

Elements:

1) input - a collection of key and value pairs;

2) intermediate collection - a non-unique collection of key and value pairs;

3) third collection - a reduction of the non-unique keys from the intermediate collection.

Page 12: Parallel programming in modern world .net technics   shared

MapReduce Example

Counting Words across multiple documents

Page 13: Parallel programming in modern world .net technics   shared

Map/reduce via PLINQ

Page 14: Parallel programming in modern world .net technics   shared

Futures

Future is a stand-in for a computational result that is initially unknown but becomes available at a later time.

A future in .NET is a Task<TResult> that returns a value.

A .NET continuation task is a task that automatically starts when other tasks, known as its antecedents, complete.

Page 15: Parallel programming in modern world .net technics   shared

Futures example

Page 16: Parallel programming in modern world .net technics   shared

Dynamic Task Parallelism

Dynamic Tasks (decomposition or “divide and conquer”) - tasks that are dynamically added to the work queue as the computation proceeds.

Most known instance – recursion.

Page 17: Parallel programming in modern world .net technics   shared

Dynamic Task example

Page 18: Parallel programming in modern world .net technics   shared

Pipelines

Each task implements a stage of the pipeline, and the queues act as buffers that allow the stages of the pipeline to execute concurrently, even though the values are processed in order.

The buffers BlockingCollection<T>

Page 19: Parallel programming in modern world .net technics   shared

Pipeline example

Page 20: Parallel programming in modern world .net technics   shared

C# 5 : async and await

asynchronous pattern,

event-based asynchronous pattern,

task-based asynchronous pattern (TAP) : async&await!

Page 21: Parallel programming in modern world .net technics   shared

Asynchronous Pattern

Page 22: Parallel programming in modern world .net technics   shared

Event-Based Asynchronous Pattern

Page 23: Parallel programming in modern world .net technics   shared

Task-Based Asynchronous Pattern

Page 24: Parallel programming in modern world .net technics   shared

Using Multiple Asynchronous Methods

Using Multiple Asynchronous Methods:

vs

Using Combinators:

Page 25: Parallel programming in modern world .net technics   shared

Converting the Asynchronous Pattern

The TaskFactory class defines the FromAsync method that allows converting methods using the asynchronous pattern to the TAP.

Page 26: Parallel programming in modern world .net technics   shared

ERROR HANDLING

Page 27: Parallel programming in modern world .net technics   shared

Multiple tasks error handling

Page 28: Parallel programming in modern world .net technics   shared

CANCELLATION

Cancellation with Framework Features

Page 29: Parallel programming in modern world .net technics   shared

CANCELLATION

Cancellation with custom tasks

Page 30: Parallel programming in modern world .net technics   shared

Literature Concurrent Programming on Windows

“MapReduce: Simplified Data Processing on Large Clusters” by Jeffrey Dean and Sanjay Ghemawat. 2004

Parallel Programming with Microsoft Visual Studio 2010 Step by Step

Parallel Programming with Microsoft®.NET: Design Patterns for Decomposition and Coordination on Multicore Architectures

Pro .NET 4 Parallel Programming in C# [Adam_Freeman]

Professional Parallel Programming with C# [Gaston_Hillar]

Professional Parallel Programming with C# Master Parallel Extensions with NET 4

.NET 4.5 Parallel Extensions Cookbook | Packt Publishing

Page 31: Parallel programming in modern world .net technics   shared

Questions?

Parallel programming in modern world .NET technics

Page 32: Parallel programming in modern world .net technics   shared

Thanks!