[dmd-concurrency] How to use parallel foreach to process a part of array?

Steven Schveighoffer schveiguy at yahoo.com
Tue Oct 29 13:56:15 PDT 2013


Right, I didn't check over the full code.

The code is broken with or without my change, but with my change it's shorter broken code :)

-Steve

On Oct 29, 2013, at 4:33 PM, Brad Roberts <braddr at puremagic.com> wrote:

> That will eliminate the if, but over looks a larger issue.. parallel assumes that there's no interdependencies and that the order of execution is irrelevant.  That's very much not the case in the code below.  Each value in that code depends on other elements.  Unless you notice that all values are 0 and can thus just be replaced with the foreach entirely removed.
> 
> On 10/29/13 6:41 AM, Steven Schveighoffer wrote:
>> Slice it.
>> 
>> foreach(i, ref el; parallel(data[10..$-10]))
>> 
>> -Steve
>> 
>> On Oct 29, 2013, at 3:49 AM, Ilya Sazonov <poxvuibr at gmail.com> wrote:
>> 
>>> Suppose I have the following piece of code
>>> 
>>> import std.parallelism;
>>> 
>>> void main() {
>>>     int[] data = new int[1000_000_00];
>>> 
>>>     foreach(i, ref el; parallel(data)) {
>>>         if (i > 10 && i < 1000_000_00 - 10)
>>>             data[i] = data[i-10] + data[i+10];
>>>     }
>>> }
>>> 
>>> That's right, I want to process all the elements of the array, except for first 10 and last 10.
>>> Is there a way to do that without if statement?
>>> 
>>> I mean something like foreach starting from element with index 10 and ending with lendth - 10
>>> 
> 
> _______________________________________________
> dmd-concurrency mailing list
> dmd-concurrency at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-concurrency



More information about the dmd-concurrency mailing list