www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17020] New: std.parallelism.taskpool amap should accept lambdas

https://issues.dlang.org/show_bug.cgi?id=17020

          Issue ID: 17020
           Summary: std.parallelism.taskpool amap should accept lambdas
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: greeenify gmail.com

void main()
{
    auto w = iota(0,1_000_000);
    int[] foo;

    // Not OK, dmd can't infer lambda isn't a delegate
    // foo = taskPool().amap!(a => a + 1)(w);

    // OK:
    foo = taskPool().amap!`a+1`(w); // string lambdas, yeah!
    foo = taskPool().amap!(function int(int a) => a + 1)(w);
    static int func(int a) { return a + 1; }
    foo = taskPool().amap!func(w);
}


In the forum thread a quick & dirty solution was posted:


private auto pmap(alias fun, R)(R range) if(isInputRange!R) {
    import std.parallelism;
    import core.sync.mutex;

    static __gshared Mutex mutex;
    if(mutex is null) mutex = new Mutex;
    typeof(fun(range.front))[] values;
    foreach(i, value; range.parallel) {
        auto newValue = fun(value);
        synchronized(mutex) {
            if(values.length < i + 1) values.length = i + 1;
            values[i] = newValue;
        }
    }

    return values;
}


http://forum.dlang.org/post/zkyhyjtyjdyshxyqoryk forum.dlang.org

--
Dec 22 2016