# Pmap for Threads?

**URL:** <https://discourse.julialang.org/t/pmap-for-threads/23631>\
**Category:** General Usage\
**Created:** [April 28, 2019, 9:54pm UTC](https://discourse.julialang.org/t/pmap-for-threads/23631 "2019-04-28T21:54:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [April 28, 2019, 9:54pm UTC](https://discourse.julialang.org/t/pmap-for-threads/23631/1 "2019-04-28T21:54:56Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![hckr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hckr/32/4836_2.png) [@hckr](https://discourse.julialang.org/u/hckr)\
**Post date:** [April 28, 2019, 10:13pm UTC](https://discourse.julialang.org/t/pmap-for-threads/23631/2 "2019-04-28T22:13:26Z")

</div>

There is unregistered [KissThreading.jl](https://github.com/mohamed82008/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:

```julia
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.
