Parallelization issues

Ali Çehreli acehreli at yahoo.com
Thu Mar 8 10:07:02 PST 2012


On 03/08/2012 07:52 AM, bearophile wrote:
> ixid:
>
>> This is a simple merge sorting implementation, a[0] and a[1] are the
>> two halves of the array to be sorted, split into an int[][2] a array.
>> This was done because I wanted to try parallel but that gives these
>> errors:
>>
>> foreach(ref i;parallel(a))
>> mergeSort(i);
>>
>> Error: template std.array.popFront(A)
>
> You are trying to do popFront on a fixed sized array. Try to use
> int[][] instead of int[][2].
>
> Bye,
> bearophile

Indeed. Both of the following work for me:

import std.stdio;
import std.algorithm;
import std.parallelism;

void main()
{
     int[] array = [ 6, 3, -5, 9, 0, -1, 10 ];
     int[][] slices = [ array[0 .. $/2], array[$/2 .. $] ];

     foreach(slice; parallel(slices)) {
         sort(slice);
     }

     writeln("Note: Only partially sorted: ", array);
}

import std.stdio;
import std.algorithm;
import std.parallelism;

void main()
{
     int[] array = [ 6, 3, -5, 9, 0, -1, 10 ];

     auto tasks = [ task!sort(array[0 .. $/2]) ];
     tasks ~= task!sort(array[$/2 .. $]);

     foreach (task; tasks) {
         task.executeInNewThread();
     }

     foreach (task; tasks) {
         task.yieldForce();
     }

     writeln("Note: Only partially sorted: ", array);
}

Ali


More information about the Digitalmars-d-learn mailing list