On Tuesday, 10 March 2015 at 20:41:14 UTC, 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;

You forgot a couple "import"s here.


void main() {

        int[] a = new int[1000000];

        foreach (i, ref elem; a)
                elem = i;

Type mismatch here. i is a size_t, but elem is an int.


        /*if (find(parallel(a), 895639).length != 0)
                writeln("Yes");
        else
                writeln("No");*/
}

I guess you'd have to write your own "find". Since std.algorithm.find is just linear search, that shouldn't be hard. Something like this:

bool found = false;
foreach(x; parallel(a))
{
    if(x == 895639)
    {
        found = true;
        /* Maybe figure out how to break all parallel loops. */
    }
}

std.algorithm.find would work on mere input ranges, and it would return the tail of the range and not just a bool. Both of those don't make sense with a parallel search, though.

Also, with a trivial predicate like integer equality, parallelization might not buy you anything.

Reply via email to