Parallelization of a large array

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 10 15:34:34 PDT 2015


On 03/10/2015 01:41 PM, Dennis Ritchie wrote:
> Hi.
> How to parallelize a large array to check for the presence of an element
> matching the value with the data?
>
> std.stdio;
> std.algorithm;
> std.parallelism;
>
> void main() {
>
>      int[] a = new int[1000000];
>
>      foreach (i, ref elem; a)
>          elem = i;
>
>      /*if (find(parallel(a), 895639).length != 0)
>          writeln("Yes");
>      else
>          writeln("No");*/
> }

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);

Ali



More information about the Digitalmars-d-learn mailing list