# Parallel sampling

**URL:** https://discourse.julialang.org/t/parallel-sampling/62310
**Category:** Performance
**Created:** [June 3, 2021, 12:06am UTC](https://discourse.julialang.org/t/parallel-sampling/62310 "2021-06-03T00:06:16Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)
#### Post date: [June 3, 2021, 12:06am UTC](https://discourse.julialang.org/t/parallel-sampling/62310/1 "2021-06-03T00:06:17Z")

</div>

it is the first time i’m trying the parallel computing and before i go to my actual code, i decided to try it on a simple code to see the speed of the execution of the code

```julia
@everywhere function sampler(x, y)
    
    z= x.*y
    return z
end
m=[]
@time @everywhere for i= 1:1_000_000_000
    x=rand(500);
    y=rand(500);
    z= sampler(x,y)   
    
end

```

of course this function after `addprocs(4)` but when i run it for 1\_000\_000 it takes a very long time so i want to know what am i doing wrong here and how can i make it faster?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [June 3, 2021, 12:20am UTC](https://discourse.julialang.org/t/parallel-sampling/62310/2 "2021-06-03T00:20:06Z")

</div>

How long does it take and why do you think it SHOULD take less time

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [June 3, 2021, 2:11am UTC](https://discourse.julialang.org/t/parallel-sampling/62310/3 "2021-06-03T02:11:46Z")

</div>

`@everywhere for` executes the same loop on all processes, hence your code will take the same amount of time regardless of how many processors your throw at it:

```julia
julia> nprocs()
1

julia> @time @everywhere sum(rand() for i = 1:1_000_000_000)
  3.822825 seconds (52.07 k allocations: 3.278 MiB, 1.59% compilation time)

julia> addprocs(1);

julia> @time @everywhere sum(rand() for i = 1:1_000_000_000)
  3.969405 seconds (52.14 k allocations: 3.277 MiB, 1.50% compilation time)

```

If you want each worker process to deal with part of the loop, use `@distributed`:

```julia
julia> workers()
1-element Vector{Int64}:
 1

julia> @time @distributed (+) for i = 1:1_000_000_000; rand(); end;
  3.504719 seconds (46.90 k allocations: 2.665 MiB, 2.19% compilation time)

julia> addprocs(4); workers()
4-element Vector{Int64}:
 2
 3
 4
 5

julia> @time @distributed (+) for i = 1:1_000_000_000; rand(); end;
  1.847140 seconds (48.06 k allocations: 2.754 MiB, 0.88% compilation time)

```

---

<div class="post-metadata">

### Author: ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)
#### Post date: [June 3, 2021, 2:25pm UTC](https://discourse.julialang.org/t/parallel-sampling/62310/4 "2021-06-03T14:25:19Z")

</div>

it takes more than 5 minutes and since the purpose of the parallelism is to speed up the computation i’m expecting it to be some seconds and not minutes, as i said i’m just trying it before applying it on my actual code

---

<div class="post-metadata">

### Author: ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)
#### Post date: [June 3, 2021, 4:57pm UTC](https://discourse.julialang.org/t/parallel-sampling/62310/5 "2021-06-03T16:57:24Z")

</div>

it does work faster, but is there any other way to make it more faster?

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [June 4, 2021, 12:57am UTC](https://discourse.julialang.org/t/parallel-sampling/62310/6 "2021-06-04T00:57:35Z")

</div>

I doubt it. Most basic operations take around 1 nanosecond on a modern computer, so a back-of-the-envelope estimate for the runtime of your original piece of code is

500 \times 10^9 \times 1 \text{ nanosecond} \approx 8.3 \text{ minutes}.

This matches quite well with the five minutes runtime you observed.
