On Saturday, 20 September 2014 at 06:46:43 UTC, Nordlöw wrote:
On Thursday, 18 September 2014 at 19:49:00 UTC, Atila Neves wrote:
I had to roll my own parallel map today, but at least I did get a nice 3x speedup.

Is your own parallel map public somewhere? It would be interesting to see it.

I just did the simplest, stupidest thing that would work, so it's probably buggy. It works where I used it (and was faster) so that's all I needed to know. To even think of releasing this I'd use atomics instead of the mutex and try to break it in all sorts of ways. But here it is anyway:

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

Oh, and the reason I don't just append to `values` is that I need to preserve the original order.

Atila

Reply via email to