November 14, 2012
On Wednesday, November 14, 2012 18:31:07 Joseph Rushton Wakeling wrote:
> Is it possible to use taskPool.map with functions that take more than one input?
> 
> Consider the following code which uses map to convert a given value to its sum with another number:
> 
> import std.algorithm, std.parallelism, std.range, std.stdio;
> 
> real pairsum(real x, real y)
> {
> writeln("Calculating sum of ", x, " + ", y);
> return x + y;
> }
> 
> void main()
> {
> real x = 3.0;
> auto y = iota(0.0, 1.0, 0.05);
> 
> auto psums = map!(a => pairsum(x, a))(y);
> 
> foreach(s; psums)
> writeln(s);
> }
> 
> Simply replacing map! with taskPool.map! results in a compilation error due to the use of the a => notation. But how else to tell it the function arguments that should _not_ be iterated over? Or is taskPool.map! limited to single-argument functions?

map in general is limited to single-argument functions. You have no way to give it the second argument. Either, you use a delegate, and it takes the "second argument" from the state of the function that it's nested in, or if the second argument is different for each element, you create a second range with those second arguments and use zip to pass them to map as tuples. I don't know how well parallel map deals with delegates though. You'd probably have to write it in a way that ensures that multiple threads don't try and write to the same memory with it in the same way that you have to worry about that in the body of a parallel foreach.

- Jonathan M Davis