Reputation: 2569
I found this suite of benchmarks comparing parallel/distributed processing concepts across languages.
I looked at the code for the .NET TPL benchmark, found it a bit weird and went on to debug it.
It seems we have one task running parallel to the Main task and doing, if I understand it correctly, only an asynchronous reduce (aggregate) over all synchronous and recursive results.
Source code is here:
long limit = 1000000;
var x3 = Task.Run( () => skynetTpl( 0, limit, 10 ) );
...
static void skynetTplRecursion( ITargetBlock<long> src, long num, long size, long div )
{
if( size == 1 )
{
src.SendAsync( num );
return;
}
for( var i = 0; i < div; i++ )
{
var sub_num = num + i * ( size / div );
skynetTplRecursion( src, sub_num, size / div, div );
}
}
static async Task<long> skynetTpl( long num, long size, long div )
{
BatchBlock<long> source = new BatchBlock<long>( 1024 );
long sum = 0;
ActionBlock<long[]> actAggregate = new ActionBlock<long[]>( vals => sum += vals.Sum(),
new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = 1, SingleProducerConstrained = true } );
source.LinkTo( actAggregate, new DataflowLinkOptions() { PropagateCompletion = true } );
skynetTplRecursion( source, num, size, div );
source.Complete();
await actAggregate.Completion;
return sum;
}
Is my understanding correct? If not, why?
UPDATE:
The objective of the code as stated by the author of the repository:
Creates an actor (goroutine, whatever), which spawns 10 new actors, each of them spawns 10 more actors, etc. until one million actors are created on the final level. Then, each of them returns back its ordinal number (from 0 to 999999), which are summed on the previous level and sent back upstream, until reaching the root actor. (The answer should be 499999500000).
Upvotes: 2
Views: 125
Reputation: 244777
Yes, you're right. All the recursive code runs synchronously on a single thread. There is another computation that runs in parallel with that: summing the arrays in the ActionBlock
.
This does not come close to the description of the code.
Upvotes: 4