# How to include a Distributed into a package?

**URL:** <https://discourse.julialang.org/t/how-to-include-a-distributed-into-a-package/81561>\
**Category:** General Usage\
**Created:** [May 24, 2022, 9:16am UTC](https://discourse.julialang.org/t/how-to-include-a-distributed-into-a-package/81561 "2022-05-24T09:16:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [May 24, 2022, 9:16am UTC](https://discourse.julialang.org/t/how-to-include-a-distributed-into-a-package/81561/1 "2022-05-24T09:16:18Z")

</div>

I am writing a package where at some point a big loop appears.  
What is the proper way to allow using the `Distributed.jl` package to replace `map` by `pmap`?

1. Should we do like that:

```julia
module MyPackage
using Distributed

### code
fit_function() = pmap(long function)
### code
export fit_function
end

```

and then

```julia
using MyPackage
fit_function()

```

when we are using personal laptop with no multiprocessor.  
On a cluster

```julia
using Distributed
@everywhere using MyPackage
addproc!(10)
fit_function()

```

1. is there another preferable architecture like two different packages with/without `Distributed` `pmap`/`map` ?
2. maybe always do `using Distributed` but defining two functions `fit_function()` with `pmap` and another one `pfit_function()` with `pmap`.

What is the best way to go in terms of

- “code”
- Julia spirtit,
- Dependency,
- Performances (from what I recall `pmap` was slightly longer than `map` when used on one proc).

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [September 13, 2024, 10:01am UTC](https://discourse.julialang.org/t/how-to-include-a-distributed-into-a-package/81561/2 "2024-09-13T10:01:57Z")

</div>

I still wonder how one can allow pmap on some operations conditionally to loading Distributed.jl  
Would the new package extension be suitable for that?

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [September 13, 2024, 1:52pm UTC](https://discourse.julialang.org/t/how-to-include-a-distributed-into-a-package/81561/3 "2024-09-13T13:52:51Z")

</div>

To anyone interested and maybe related to some [other questions](https://discourse.julialang.org/t/should-you-use-spawnat-everywhere-within-a-package-distributed-jl/112134), what works for me is to add

```julia
module MyPkg
using Distributed
f(x) = ...
function big(...)
  ....
  return pmap(f, iterator)
end

export big
end

```

Then outside the pkg I just need to do

```julia
using Distributed
addprocs(10)
using MyPkg
@everywhere using MyPkg
big(...) # works and is faster!

```

It is very simple to modify existing code to make it distributed that way.  
If I don’t want distributed, I just do not load `using Distributed` and `pmap` performs as a regular `map`.
