22
PARALLEL & ASYNC PROCESSING USING TPL DATAFLOW Petru Rebeja

Parallel & async processing using tpl dataflow

Embed Size (px)

Citation preview

Page 1: Parallel & async processing using tpl dataflow

PARALLEL & ASYNCPROCESSING USING TPL DATAFLOW

Petru Rebeja

Page 2: Parallel & async processing using tpl dataflow
Page 3: Parallel & async processing using tpl dataflow

AGENDA

• What is Dataflow?

• When to use it?

• How to use it?

• Q&A

Page 4: Parallel & async processing using tpl dataflow

THE BIG PICTURE

CLR Thread Pool

Tasks

PLINQ Parallel Loops

Concurrent Collections

Dataflow

Page 5: Parallel & async processing using tpl dataflow

DATAFLOW BENEFITS

• Effortless use of multi-threading

• Performance boost via painless optimization

• Development focus is on the ‘what’ rather than ‘how’

Page 6: Parallel & async processing using tpl dataflow

DATAFLOW USAGES

High throughput, low-latency scenarios

Robotics

Manufacturing

Imaging Biology

Oil & Gas

Finance

Page 7: Parallel & async processing using tpl dataflow

PROGRAMMING MODEL

• Actor-based programming

• In-process message passing

• Components (blocks) for creating data processing pipelines

Page 8: Parallel & async processing using tpl dataflow

ARCHITECTURE

IDataflowBlock

ISourceBlock<TOutput> ITargetBlock<TInput>

IPropagatorBlock<Tinput,Toutput>

Page 9: Parallel & async processing using tpl dataflow

COMPOSITION

Source

Target

Propagator

OptionalTransform

Page 10: Parallel & async processing using tpl dataflow

BUFFERING BLOCKS

BufferBlock<T>

BroadcastBlock<T>

WriteOnceBlock<T>

Page 11: Parallel & async processing using tpl dataflow

EXECUTION BLOCKS

ActionBlock<T>

TransformBlock<T,V>

TransformManyBlock<T,V>

Page 12: Parallel & async processing using tpl dataflow

GROUPING BLOCKS

BatchBlock<T>

JoinBlock<T1,T2,…>

BatchedJoinBlock<T1,T2>

Page 13: Parallel & async processing using tpl dataflow

BEHAVIOR CONFIGURATION OPTIONS

• BufferBlock<T>

• BroadcastBlock<T>

• WriteOnceBlock<T>

DataflowBlockOptions

• ActionBlock<T>

• TransformBlock<TIn, TOut>

• TransformManyBlock<TIn, TOut>

ExecutionDataflowBlockOptions

• BatchBlock<T>

• JoinBlock<T1, T2[, T3]>

• BatchedJoinBlock<T1, T2>

GroupingDataflowBlockOptions

Page 14: Parallel & async processing using tpl dataflow

COMPLETION & CANCELLATION

• To know when a block completes await block.Completion

or add a continuation task to it

• To propagate completion from source to target, set

DataflowLinkOptions.PropagateCompletion when

linking

• Set DataflowBlockOptions.CancellationToken to

enable cancellation

Page 15: Parallel & async processing using tpl dataflow

ERROR HANDLING

• If the exception does not affect the integrity of the

pipeline – use a try/catch inside the block

• Otherwise, handle errors outside of the pipeline by

• Adding a continuation to block.Completion

• Propagating errors through the pipeline

Page 16: Parallel & async processing using tpl dataflow

DEALING WITH CONCURRENCY

• Rule of thumb: avoid shared state whenever possible.

• Use ConcurrentExclusiveSchedulerPair to perform

updates on shared state

• Be aware of the caveats with

ConcurrentExclusiveSchedulerPair

Page 17: Parallel & async processing using tpl dataflow

CREATING CUSTOM BLOCKS

The easy way:

DataflowBlock.Encapsulate<TInput, TOutput>(

target, source)

Page 18: Parallel & async processing using tpl dataflow

CREATING CUSTOM BLOCKS

The hard(core) way:

class CustomBlock:

IPropagatorBlock<TInput, TOutput>

{

}

Page 19: Parallel & async processing using tpl dataflow

CREATING CUSTOM BLOCKS

Either way you choose, don’t forget to:

• Propagate completion

• Pool for cancellation

Page 20: Parallel & async processing using tpl dataflow

REFERENCES & FURTHER READING

Dataflow (Task Parallel Library) http://msdn.microsoft.com/en-us/library/hh228603(v=vs.110).aspx

Stephen Toub

TPL Dataflow Tourhttp://channel9.msdn.com/posts/TPL-Dataflow-Tour

Joseph Albahari

The Future of .NET Parallel Programming

http://channel9.msdn.com/events/TechEd/Australia/Tech-Ed-Australia-2011/DEV308

Stephen Toub

Inside TPL Dataflowhttp://channel9.msdn.com/Shows/Going+Deep/Stephen-Toub-Inside-TPL-Dataflow

Alexey Kursov

Pipeline TPL Dataflow Usage exampleshttps://www.youtube.com/watch?v=AI9KxgDF43khttps://www.youtube.com/watch?v=AI9KxgDF43k

Richard Blewett, Andrew Clymer

Pro Asynchronous Programming with .NET

APRESS 2013ISBN: 978-1430259206

AKKA.NET http://getakka.net/

Page 21: Parallel & async processing using tpl dataflow

QUESTIONS?

Page 22: Parallel & async processing using tpl dataflow

THANK YOU!

[email protected]

Parallel & Async Processing using TPL Dataflow