Pmap for Threads?

Is there an equivalent function to pmap for Threads?

I want to map a collection of parameters over a function and collect the results in an array. I could do this with Threads.@threads and a for loop, but that requires a bit of boilerplate.

For example, how would I do the following with threads?

f(x) = (x, 2*x)
pmap(f, 1:8)

There is unregistered KissThreading.jl which provides a function called tmap! which is like a threaded version of map!. But similar to pmap, tmap! sends tasks to threads one-by-one (or a size given by batch_size argument) rather than dividing the work at the beginning of the loop as in Threads.@threads. The actual behavior of tmap! would be different than, for example, the following loop:

let a = similar(b)
    Threads.@threads for i = 1:length(a)
        a[i] = 2 * b[i]
    end
    println(a)
end

This is similar to the difference between pmap and a @distributed loop. pmap dynamically balances load while @distributed divides the tasks at the beginning. I would guess that tmap! might be a better option performance-wise for long and uneven tasks, otherwise going for @threads-loop would probably be better.

3 Likes