www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - taskPool.reduce() help

reply Ish <ratta1i cmich.edu> writes:
The following code does not compile (with gdc) but if the line 
containing taskPool.reduce is reduce it does compile. Any 
pointers will be appreciated.

import std.stdio;
import std.math;
import std.algorithm;
import std.parallelism; // does not work!!
import core.thread;

double aCalculation(double result, int element) {

     double r = 4.0*(pow(-1.0, element)/(2.0*cast(double)element + 
1.0));
     result += r;

     return result;
}

void main() {
     writefln("Result: %7.2f.", taskPool.reduce!aCalculation(0.0, 
[0, 1, 2, 3, 4]));
}

-ish
Nov 28 2015
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/28/2015 01:57 PM, Ish wrote:
 The following code does not compile (with gdc) but if the line
 containing taskPool.reduce is reduce it does compile. Any pointers will
 be appreciated.

 import std.stdio;
 import std.math;
 import std.algorithm;
 import std.parallelism; // does not work!!
 import core.thread;

 double aCalculation(double result, int element) {

      double r = 4.0*(pow(-1.0, element)/(2.0*cast(double)element + 1.0));
      result += r;

      return result;
 }

 void main() {
      writefln("Result: %7.2f.", taskPool.reduce!aCalculation(0.0, [0, 1,
 2, 3, 4]));
 }

 -ish
I had observed this difference before and had explained it here: http://ddili.org/ders/d.en/parallelism.html <quote> However, there are important differences in the way taskPool.reduce() works. Like the other parallel algorithms, taskPool.reduce() executes the functions in parallel by using elements in different tasks. Each task works on the elements that it is assigned to and calculates a result that corresponds to the elements of that task. Since reduce() is called with only a single initial value, every task must use that same initial value to initialize its own result (the parameter 0 above). The final values of the results that each task produces are themselves used in the same result calculation one last time. These final calculations are executed sequentially, not in parallel. For that reason, taskPool.reduce() may execute slower in short examples as in this chapter as will be observed in the following output. The fact that the same initial value is used for all of the tasks, effectively being used in the calculations multiple times, taskPool.reduce() may calculate a result that is different from what std.algorithm.reduce() calculates. For that reason, the initial value must be the identity value for the calculation that is being performed, e.g. the 0 in this example which does not have any effect in addition. Additionally, as the results are used by the same functions one last time in the sequential calculations, the types of the parameters that the functions take must be compatible with the types of the values that the functions return. </quote> So, since the result type is double, the function must be able to take (double, double) for those final sequential calculations. Ali
Nov 28 2015