# Pmap in function/module

**URL:** https://discourse.julialang.org/t/pmap-in-function-module/25896
**Category:** New to Julia
**Created:** [July 1, 2019, 3:12pm UTC](https://discourse.julialang.org/t/pmap-in-function-module/25896 "2019-07-01T15:12:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jmboehm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmboehm/32/2263_2.png) [@jmboehm](https://discourse.julialang.org/u/jmboehm)
#### Post date: [July 1, 2019, 3:12pm UTC](https://discourse.julialang.org/t/pmap-in-function-module/25896/1 "2019-07-01T15:12:58Z")

</div>

I’m having trouble running `pmap` when it’s inside a function in a module. Here is a MWE:

In `myModule.jl`

```julia
module myModule

    using Distributed

    function myOuterFunction()

        myVec = collect(1.0:0.1:10.0)

        @everywhere function f(x::Float64)
            return x*x
        end

        retVec = pmap(x->f(x), myVec)
        return retVec
    end
end

```

and in `myscript.jl`:

```julia
using Distributed
addprocs(8)

include("myModule.jl")

myModule.myOuterFunction()

```

yields an error:

```julia
ERROR: On worker 2:
UndefVarError: myModule not defined

```

If I instead do

```julia
@everywhere include("myModule.jl")

```

I get the error

```julia
ERROR: On worker 3:
UndefVarError: f not defined

```

even though the function definition is `@everywhere`’d.

Any help is much appreciated.

---

<div class="post-metadata">

### Author: ![MaximilianJHuber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maximilianjhuber/32/2579_2.png) [@MaximilianJHuber](https://discourse.julialang.org/u/MaximilianJHuber)
#### Post date: [July 1, 2019, 3:48pm UTC](https://discourse.julialang.org/t/pmap-in-function-module/25896/2 "2019-07-01T15:48:15Z")

</div>

It seems that the `@everywhere` definition of the function `f` also sends namespace information to the workers, and does not just define a function `f` in the global scope.

This works:

```julia
@everywhere module myModule

    using Distributed

    function f(x::Float64)
        return x*x
    end

    function myOuterFunction()

        myVec = collect(1.0:0.1:10.0)
            
        retVec = pmap(x->f(x), myVec)
        return retVec
    end
end

```

---

<div class="post-metadata">

### Author: ![jmboehm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmboehm/32/2263_2.png) [@jmboehm](https://discourse.julialang.org/u/jmboehm)
#### Post date: [July 1, 2019, 6:01pm UTC](https://discourse.julialang.org/t/pmap-in-function-module/25896/3 "2019-07-01T18:01:34Z")

</div>

Thanks a lot. I guess this makes it hard (impossible?) to add workers within the function, do the pmap, and then destroy them again. Is it encouraged to start the workers at the very beginning and keep them throughout?

---

<div class="post-metadata">

### Author: ![alvrg](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@alvrg](https://discourse.julialang.org/u/alvrg)
#### Post date: [July 1, 2019, 6:23pm UTC](https://discourse.julialang.org/t/pmap-in-function-module/25896/4 "2019-07-01T18:23:41Z")

</div>

Yo can remove workers with `rmprocs()`, as stated in the [docs](https://docs.julialang.org/en/v1/manual/parallel-computing/#Starting-and-managing-worker-processes-1). Nevertheless, you will have some overhead if you want to load the workers again.

About removing them after the pmap, I would leave them until the script finishes, I don’t think they will add some overhead.
