Fold in Parallelism
Russel Winder
russel at winder.org.uk
Mon Dec 18 20:53:28 UTC 2017
Ali,
Shouldn't this be a pull request for std.parallelism to be extended?
If the function is in std.algorithm, then people should not have to
write it for themselves in std.parallelism.
On Mon, 2017-12-18 at 11:01 -0800, Ali Çehreli via Digitalmars-d-learn
wrote:
>
[…]
> > 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
--
Russel.
===========================================
Dr Russel Winder t: +44 20 7585 2200
41 Buckmaster Road m: +44 7770 465 077
London SW11 1EN, UK w: www.russel.org.uk
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20171218/78d95726/attachment.sig>
More information about the Digitalmars-d-learn
mailing list