# How to modify nprocs with DistributedArrays inside a function

**URL:** <https://discourse.julialang.org/t/how-to-modify-nprocs-with-distributedarrays-inside-a-function/28796>\
**Category:** General Usage\
**Tags:** parallel\
**Created:** [September 15, 2019, 6:30pm UTC](https://discourse.julialang.org/t/how-to-modify-nprocs-with-distributedarrays-inside-a-function/28796 "2019-09-15T18:30:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [September 15, 2019, 6:30pm UTC](https://discourse.julialang.org/t/how-to-modify-nprocs-with-distributedarrays-inside-a-function/28796/1 "2019-09-15T18:30:35Z")

</div>

Hi,  
I want to draw speed-up plots with `Distributed` and `DistributedArrays`.  
I use `Distributed.addprocs` inside my benchmark function but I should also  
use `@everywhere using DistributedArrays` that cannot (true ?) be called inside a function…  
Any hints ?

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [September 16, 2019, 6:53am UTC](https://discourse.julialang.org/t/how-to-modify-nprocs-with-distributedarrays-inside-a-function/28796/2 "2019-09-16T06:53:30Z")

</div>

I found a semi-solution here [How do you load a module @everywhere inside a function in Julia - Stack Overflow](https://stackoverflow.com/questions/47685536/how-do-you-load-a-module-everywhere-inside-a-function-in-julia/47691491)

It is possible to use `using` inside a function with the expression:

```julia
    eval(macroexpand(Distributed,quote @everywhere using DistributedArrays end))

```

Not exactly pretty IMHO…

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [September 16, 2019, 7:44pm UTC](https://discourse.julialang.org/t/how-to-modify-nprocs-with-distributedarrays-inside-a-function/28796/3 "2019-09-16T19:44:16Z")

</div>

The following solution is almost the same, but arguably prettier:

```julia
julia> using Distributed

julia> function fun()
           addprocs(1)
           @eval @everywhere using Statistics
           @spawnat 2 mean([1,2,3])
       end
fun (generic function with 1 method)

julia> nprocs()
1

julia> fut = fun()
Future(2, 1, 5, nothing)

julia> fetch(fut)
2.0

julia> nprocs()
2

```
