Different types of data blocks in TPL dataflow (TDF) part 1

11 ביוני 2012

אין תגובות

Hey

With the new release of the release candidate of visual
studio 11 (now known as visual studio 12), I would like to explain the
difference of the TPL dataflow (TDF) built in data blocks and give a short
description about them. In TPL dataflow library there are 3 groups of building
blocks. In this part I will talk about the first group which contains 3 unique
blocks and the most commons ones: The execution blocks.

The thing that combines all those blocks together as a group is that each time a massage in inserted to one of those blocks a delegate will be executed on the massage on exiting the block. When a user wants to create an exaction block he needs to provide a code that will be executed on a massage
that is provided to the block. Let's look on examples and I hope it will be clearer:

First block: Action Block <T>

This block executes a delegate given by the creator of the
block for each massage posted to the block, sample:

 

var actionBlock  = new ActionBlock<string> (i => {  

       Console.WriteLine("The Massage was {0}", i);}

);

We have created a new Action Block that get massages of type
'string' and write the massage to the console. This block is the most common from
the execution blocks group, and probably the most simple.

Posting to this queue will write to the console the string we
have entered.

actionBlock.Post<string>("Hello");

actionBlock.Post<string>("TDF");

The console will show:

Hello

TFD

Second Block: Transform Block <T, T>

This block, like the Action Block will execute a delegate
given by the creator on each massage posted to the block but it also has a
return value, the delegate should return a value from type declared in the
Transform Block return type, sample:

var transformBlock  = new TransformBlock<int, string> (i => {

     i.ToString());

});

We have created a Transform block that get a value from type
'int' and convert it to type 'string', and if we will link this block to the
action block we have created in before

transformBlock.LinkTo<string>(actionBlock); //Link the result of the delegate to the action block.

The output of the transform block will be inserted to the action block, so if we will
post a value of type 'int' it will transform in to a value of type 'string' and
automatically will be posted in the action block we have created before.

transformBlock.Post<int>(1);

transformBlock.Post<int>(2);

transformBlock.Post<int>(3);

And the console will show:

1

2

3

Third block : TransformManyBlock<T, T>

This block is very similar to the second block, but the key
difference is that the results of the delegate given to the block can be more
one result per input. If in Transform Block the delegate of this block got 1
input and created for him 1 output (in out sample convert an value of 'int'
type to a string 'type') the Transform Many Block gets a single input but the
results can 0 value, 1 value or number of values.

var transformManyBlock  = new TransformManyBlock<int, int> (i =>

{

     var retList = new List<int>() { i , i * 100 };

     return retList;

});

We have created a transformManyBlock that will get an type 'int' value and create and
returns a list with 2 values the initial value and his multiply by 100 value. Now
we will connect this transformManyBlock with the transformBlock
we have created before ( remember he is still
connected to the action block we have created in the beginning )

transformManyBlock.LinkTo<int>(transformBlock)

So
the outout of the transformManyBlock
will automatically be inserted into the transformBlock
block for farther processing, now if we will post some values into this block

transformManyBlock.Post<int>(1);

transformManyBlock.Post<int>(2);

transformManyBlock.Post<int>(3);

The console will show:

1

100

2

200

3

300

Although I have created really simple and clear delegates I
hope you can see above it, for the some of power of TPL Dataflow ( still haven't talked about Parallelism of the blocks, high performance, greed-not greed behaviors of the blocks and more) This was my first post about TPL dataflow blocks, the first
group, execution blocks, hopefully in the coming weeks I will post about buffer blocks and on the another post about the joining blocks,
stay tuned.

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *