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

Brad Roberts braddr at puremagic.com
Tue Oct 29 13:33:55 PDT 2013


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



More information about the dmd-concurrency mailing list