Fold in Parallelism
Ali Çehreli
acehreli at yahoo.com
Mon Dec 18 19:01:39 UTC 2017
On 12/18/2017 02:18 AM, Vino wrote:
> On Sunday, 17 December 2017 at 20:00:53 UTC, Ali Çehreli wrote:
>> On 12/17/2017 08:11 AM, Vino wrote:
>>
>> > As per the document form std.parallelism it states that we
>> can use
>> > taskPool.reduce so can we use the same for fold
>> (taskPool.fold) as
>> > basically both are same with slight variation on seed values,
>> if
>> > possible can can we define the same in the below lines
>>
>> fold has only been added to std.algorithm. Opened an enhancement request:
>>
>> https://issues.dlang.org/show_bug.cgi?id=18096
>>
>> Ali
>
> Hi Ali,
>
> Thank you very much, may we know if possible as to when this would be
> added.
>
> From,
> Vino.B
The implementation is almost a copy+paste from std.algorithm.fold. You
can simply copy the following fold template to your project and start
using it:
import std.parallelism;
import std.stdio;
int adder(int result, int element) {
return result + element;
}
template fold(fun...)
if (fun.length >= 1)
{
import std.parallelism : TaskPool;
auto fold(R, S...)(TaskPool t, R r, S seed)
{
static if (S.length < 2)
{
return t.reduce!fun(seed, r);
}
else
{
import std.typecons : tuple;
return t.reduce!fun(tuple(seed), r);
}
}
}
unittest {
import std.range;
const N = 10_000;
const expected = N * (N - 1) / 2;
foreach (numThreads; 1 .. 100) {
auto t = new TaskPool(numThreads);
const result = t.fold!adder(N.iota);
assert(result == expected);
t.finish();
}
}
void main() {
}
If you want to provide an explicit seed value, good luck making sense of
how it affects the final result. :) (The documentation of
TaskPool.reduce explains it but I find it too involved to understand.)
Ali
More information about the Digitalmars-d-learn
mailing list