# How am I supposed to use pmap?

**URL:** <https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943>\
**Category:** General Usage\
**Created:** [May 26, 2017, 7:22pm UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943 "2017-05-26T19:22:11Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [May 26, 2017, 7:22pm UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/1 "2017-05-26T19:22:11Z")

</div>

I have a large project where one function includes a pmap call, and I’m not sure the right way to get the code in scope on all the workers.

1. `@everywhere include("code.jl")` where code.jl uses all the packages and includes my functions. Then I run a function from my code. This doesn’t work and gives me a lot of warnings/errors about reloading modules
2. Wrapping my whole code in a module `MyModule` doing `include("MyModule.jl")` then `using MyModule`. This doesn’t work, `MyModule` isn’t defined on the workers.
3. `@everywhere include("MyModule.jl")`, then `using MyModule`. The first line gives a ton of warnings about replacing modules from packages, which surprised me because I haven’t used `MyModule` yet. It works fine except for all the warnings.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 27, 2017, 6:58pm UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/3 "2017-05-27T18:58:46Z")

</div>

Works fine with me.

```julia
julia> @everywhere module dummyModule
           function dummyFunction(i)
               i^2
           end
       end

julia> pmap(dummyModule.dummyFunction, 1:10)
10-element Array{Any,1}:
   1
   4
   9
  16
  25
  36
  49
  64
  81
 100

```

You may have forgotten to use `@everwhere workspace()` to clear the previously defined modules from previous failed attempts. `include()` just copies the code from the file, so it defines the module again everytime you use it.

---

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [May 28, 2017, 2:36am UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/4 "2017-05-28T02:36:39Z")

</div>

To avoid warnings, `import EveryPackage` first, then `@everywhere using EachPackage`.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [May 28, 2017, 11:58pm UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/5 "2017-05-28T23:58:39Z")

</div>

Alternatively, you can add the path containing your module to LOAD\_PATH:

```julia
push!(LOAD_PATH,"YourPath")
@everywhere using MyModule

```

I think there is a way to permanently add a path to the LOAD\_PATH instead.

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [May 29, 2017, 1:48am UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/6 "2017-05-29T01:48:16Z")

</div>

> [@Christopher\_Fisher](#):
>
> I think there is a way to permanently add a path to the LOAD\_PATH instead.

You can add the `push!(LOAD_PATH,...)` statement to your julia startup file (i.e. `~/.juliarc.jl` on Linux).

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [May 29, 2017, 7:15pm UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/7 "2017-05-29T19:15:08Z")

</div>

I tried adding the import statement for every package inside my module, then using the packages. It didn’t help. Do you mean the module should use `@everywhere using`` ?

I also tried `push!(LOAD_PATH, pwd())` because the current directory contains mymodule.jl, but `using MyModule` doesn’t work (MyModule not found in current path).

It is working fine except for the warnings, so this isn’t really a problem that needs to be solved, but I feel like I’m not approaching this in the proper manner.

---

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [May 30, 2017, 1:57am UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/8 "2017-05-30T01:57:50Z")

</div>

Not inside module, but in the script you run. e.g.,

```julia
import Plots
import MyModule
@everywhere using Plots
@everywhere using MyModule

```

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [May 30, 2017, 2:28am UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/9 "2017-05-30T02:28:50Z")

</div>

The warnings appear when I load the code for the module.

```julia
@everywhere include("mymodule.jl")
WARNING: replacing module ForwardDiff.
WARNING: replacing module ForwardDiff.
WARNING: replacing module ForwardDiff.
WARNING: replacing module ForwardDiff.
WARNING: replacing module BenchmarkTools
WARNING: replacing module BenchmarkTools
WARNING: replacing module BenchmarkTools
WARNING: replacing module BenchmarkTools
......
using MyModule

```

After that it works. I don’t find `@everywhere using` to be necessary.

---

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [May 30, 2017, 3:04am UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/10 "2017-05-30T03:04:47Z")

</div>

```julia
include("mymodule.jl")
@everywhere using MyModule

```

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [May 30, 2017, 3:21am UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/11 "2017-05-30T03:21:24Z")

</div>

```julia
julia> include("mymodule.jl")

julia> @everywhere using MyModule
ERROR: On worker 2:
ArgumentError: Module MyModule not found in current path.
Run `Pkg.add("MyModule")` to install the MyModule package.

```

Doesn’t work.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [June 3, 2017, 4:58am UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/12 "2017-06-03T04:58:56Z")

</div>

Do you have a minimal example?

I typically use this approach:

1. Put everything you need distributed to workers into a Module.
2. Save Module to filesystem
3. Ensure path to Module is in `LOAD_PATH`
4. `addprocs()`
5. `using Module`

Notes:

- It is important to `addprocs` _before_ `using Module`
- You shouldn’t need `@everywhere`
- You shouldn’t get warnings about replacing module.

Here’s an example:

```julia
module ModuleA
    export testA
    function testA(x)
        println(x, " -> ", x*x)
        return x*x
    end
end

```

Save this to file `ModuleA.jl` (or `ModuleA/src/ModuleA.jl`)

Now run this:

```julia
addprocs()

# ensure module is in LOAD_PATH
# in this example, module A was saved to working directory (but can be any directory)
thisDir = dirname(@ __FILE__ ())
any(path -> path == thisDir, LOAD_PATH) || push!(LOAD_PATH, thisDir)

using ModuleA

pmap(testA, 1:10)

```

And here’s the output:

```julia
Julia-0.5.2> include("loading_pmap.jl")
        From worker 2: 1 -> 1
        From worker 3: 4 -> 16
        From worker 5: 2 -> 4
        From worker 4: 3 -> 9
        From worker 2: 5 -> 25
        From worker 5: 6 -> 36
        From worker 4: 7 -> 49
        From worker 3: 8 -> 64
        From worker 2: 9 -> 81
        From worker 4: 10 -> 100
10-element Array{Any,1}:
   1
   4
   9
  16
  25
  36
  49
  64
  81
 100

```

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [June 4, 2017, 1:13am UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/13 "2017-06-04T01:13:27Z")

</div>

This worked well thanks. I realized my problem with the LOAD\_PATH approach I tried earlier. The module file needs to have the same name as the module, so ModuleA.jl for ModuleA.

I did get a mysterious error after running my code though.

```julia
using PyPlot
WARNING: An error occurred during inference. Type inference is now partially disabled.
Base.MethodError(f=typeof(Core.Inference.convert)(), args=(Base.AssertionError, "invalid age range update"), world=0x0000000000000abf)
... hundreds of lines of errors

```

This is probably unrelated to the current situation though. Maybe an 0.6 RC1 bug.

---

<div class="post-metadata">

**Author:** ![Glen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glen/32/3583_2.png) [@Glen](https://discourse.julialang.org/u/Glen)\
**Post date:** [June 8, 2017, 8:07pm UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/14 "2017-06-08T20:07:37Z")

</div>

Using the example from @greg_plowman works for me too but if I change the `pmap` command to be:

```nohighlight
pmap(t -> testA(t), 1:10)

```

Then the worker cannot find `testA`. Any idea how to resolve this?

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [June 9, 2017, 4:10am UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/15 "2017-06-09T04:10:43Z")

</div>

`pmap(t -> ModuleA.testA(t), 1:10)`

---

<div class="post-metadata">

**Author:** ![Glen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glen/32/3583_2.png) [@Glen](https://discourse.julialang.org/u/Glen)\
**Post date:** [June 9, 2017, 3:03pm UTC](https://discourse.julialang.org/t/how-am-i-supposed-to-use-pmap/3943/16 "2017-06-09T15:03:19Z")

</div>

Thanks. Using `@everywhere using ModuleA` instead also works.
