Parallelization of a large array

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 10 15:39:30 PDT 2015


On Tuesday, 10 March 2015 at 22:34:34 UTC, Ali Çehreli wrote:
>
> It is possible by accessing the actual range by chunks:
>
> import std.stdio;
> import std.algorithm;
> import std.parallelism;
> import std.range;
> import std.conv;
>
> void main()
> {
>     const size_t elementCount = 895640;
>     int[] a = iota(elementCount)
>               .map!(i => i.to!int)
>               .array;
>
>     const chunkSize = a.length / taskPool.size;
>     auto chunks = a.chunks(chunkSize);
>     bool[] results = chunks.taskPool.map!(chunk => 
> chunk.canFind(895639));
>
>     writeln(results.canFind(true) ? "Yes" : "No");
> }
>
> We can hope to make it simpler by taking advantage of parallel 
> map but it requires a static local function or a global 
> function (a lambda cannot be used):
>
>     static bool canFindIt(Range)(Range range)
>     {
>         return range.canFind(895639);
>     }
>
>     auto results = taskPool.map!canFindIt(chunks);

Thanks.


More information about the Digitalmars-d-learn mailing list