# When to use pmap vs Threads.@threads?

**URL:** https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805
**Category:** Julia at Scale
**Tags:** jump, parallel, multithreading, pmap
**Created:** [December 9, 2021, 5:29am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805 "2021-12-09T05:29:48Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![chelseas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chelseas/32/12148_2.png) [@chelseas](https://discourse.julialang.org/u/chelseas)
#### Post date: [December 9, 2021, 5:29am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/1 "2021-12-09T05:29:48Z")

</div>

Some of my processes may take a minute, and others miliseconds, so load balancing seems like a good idea. Do both do this?  
Any other things I should know about when using pmap vs. @threads?

I’ve tagged jump because I will be copying a JuMP model to each thread using copy\_model.

---

<div class="post-metadata">

### Author: ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)
#### Post date: [December 9, 2021, 6:14am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/2 "2021-12-09T06:14:34Z")

</div>

It depends on your application. If you allocate / deallocate a lot of memory in thread (workers), you might prefer `pmap`, since GC is distributed there. On the other hand if you need access and writes to shared structures, you might want `Threads.@threads`. Also, if each task can take different length of time, something like `FLoops` might be better then the static scheduler of `Threads.@threads`.

More information would be needed for a recommendation, but the best is always to test both approaches.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [December 9, 2021, 7:43am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/3 "2021-12-09T07:43:21Z")

</div>

> [@chelseas](#):
>
> so load balancing seems like a good idea. Do both do this?

No. `pmap` does, `@threads` does not. However, you could use `Threads.@spawn` (see [https://nbviewer.org/github/carstenbauer/JuliaNRWSS21/blob/main/backup/load\_balancing.ipynb](https://nbviewer.org/github/carstenbauer/JuliaNRWSS21/blob/main/backup/load_balancing.ipynb) for an example/comparison).

Note that you are comparing multithreading to multi-processing here.

---

<div class="post-metadata">

### Author: ![chelseas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chelseas/32/12148_2.png) [@chelseas](https://discourse.julialang.org/u/chelseas)
#### Post date: [December 9, 2021, 8:07am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/4 "2021-12-09T08:07:48Z")

</div>

Thanks for the info! To be honest I’m not super sure what the difference between multi-threading and multi-processing is…and which would be better for my application.  
It might be nice to have access to shared data structures (for reading) but I could also just duplicate these data structures. I don’t need access to any shared containers for writing.

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [December 9, 2021, 8:23am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/5 "2021-12-09T08:23:20Z")

</div>

One way to think about it, especially with Distributed on Julia, is pmap _could_ be running on _entirely different computers_ - maybe separated by an ocean.

Your Threads all run on the local machine.

---

<div class="post-metadata">

### Author: ![chelseas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chelseas/32/12148_2.png) [@chelseas](https://discourse.julialang.org/u/chelseas)
#### Post date: [December 9, 2021, 8:31am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/6 "2021-12-09T08:31:45Z")

</div>

Ok, good to know. Can I actually run pmap across several servers?

Also is there anything wrong with doing this? Slash does this construct have a name?:

```julia
function wrapper(problem)
    function myfun(cell)
        # call functions that use info from problem and from cell
    end
    return myfun
end
pmap(wrapper(problem), cells)

```

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [December 9, 2021, 8:38am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/7 "2021-12-09T08:38:18Z")

</div>

I don’t know if that pattern has a specific name in Julia.

The general term for things like that is “Function Factory”.

[https://adv-r.hadley.nz/function-factories.html](https://adv-r.hadley.nz/function-factories.html)

And I hope they are OK. I use them all the time.

---

<div class="post-metadata">

### Author: ![chelseas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chelseas/32/12148_2.png) [@chelseas](https://discourse.julialang.org/u/chelseas)
#### Post date: [December 9, 2021, 8:58am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/8 "2021-12-09T08:58:18Z")

</div>

Does this “across an ocean” thing mean that I can’t access global variables? In addition to some copy-able variables, I have some really big data tables that I shouldn’t be copying with every cell that I pass to pmap.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [December 9, 2021, 8:59am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/9 "2021-12-09T08:59:45Z")

</div>

> [@chelseas](#):
>
> To be honest I’m not super sure what the difference between multi-threading and multi-processing is…and which would be better for my application.

There have been many discussions on this here on discourse (and, of course, elsewhere). See for example [Multi-threading or multi-processing, how to know which to use and when?](https://discourse.julialang.org/t/multi-threading-or-multi-processing-how-to-know-which-to-use-and-when/72278)

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [December 9, 2021, 10:32am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/10 "2021-12-09T10:32:57Z")

</div>

Yes, that was my point of describing it like that.

If you want separate Processes to have access to data, they need their own copy somehow.

So two processes needing the same 1Gb of in-memory data, means 2Gb of RAM is required.

Whereas Threads can all read the same 1Gb.

Of course, the downside to that is the Threads can all (possibly) _write_ to it too (if it is mutable), and orchestrating that becomes your problem to solve.

---

<div class="post-metadata">

### Author: ![chelseas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chelseas/32/12148_2.png) [@chelseas](https://discourse.julialang.org/u/chelseas)
#### Post date: [December 16, 2021, 7:40am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/11 "2021-12-16T07:40:55Z")

</div>

Oooo what if I use BOTH Threads@threads and pmap in different parts of my code? Is it bad to start julia with both multiple threads: -t 20 and multiple processes: -p 20 ? (for example)  
And then in the main process, I first do something with shared memory using @threads and then later in the program I send work to my 20 workers?  
Is there like a lot of overhead involved if I set both -t and -p \>1 ?

---

<div class="post-metadata">

### Author: ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)
#### Post date: [December 16, 2021, 8:28am UTC](https://discourse.julialang.org/t/when-to-use-pmap-vs-threads-threads/72805/12 "2021-12-16T08:28:31Z")

</div>

No, do whatever solves your problem, there is no “one way to do things”.

In fact, mixing and matching will help you learn which to use next time.

And be sure to test your assumptions by running things on a single Thread / Process. What often seems like a good candidate to parallelize sees no benefit and may actually run slower because of the communication overhead and context switching.
