# Parallel Computations

**URL:** <https://discourse.julialang.org/t/parallel-computations/21070>\
**Category:** Julia at Scale\
**Created:** [February 22, 2019, 9:38am UTC](https://discourse.julialang.org/t/parallel-computations/21070 "2019-02-22T09:38:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ennvvy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ennvvy/32/7816_2.png) [@ennvvy](https://discourse.julialang.org/u/ennvvy)\
**Post date:** [February 22, 2019, 9:38am UTC](https://discourse.julialang.org/t/parallel-computations/21070/1 "2019-02-22T09:38:19Z")

</div>

I have migrated to Julia very recently. To be able to lower computation time, I have been trying to implement parallel programming concepts in my serial code.

> @time begin
> 
> ```
> @everywhere N = 100 ;
> 
> @everywhere agents = collect(1:1:N) ; 
> 
> @everywhere Nᵣ = 100 ;
> 
> @everywhere Nₗ = 0 ;
> 
> @everywhere rᵣ = round(10*pi/180,sigdigits = 4) ;
> 
> @everywhere rₗ = round(40*pi/180,sigdigits = 4) ; 
> 
> @everywhere μ = 90 ; 
> 
> @everywhere σ = 10 ;
> 
> @everywhere P = 0.25 ; 
> 
> @everywhere Pᵢ = 0.5 ;
> 
> @everywhere Pᵣ = 0.25 ; 
> 
> @everywhere K = 100 ; 
> 
> @everywhere maxd = 25 ; 
> 
> # Simulation Related
> 
> @everywhere mcf = 10 ; 
> 
> @everywhere h = 1 ; # Step-size
> 
> @everywhere tf = 100 ; # Final time
> 
> @everywhere n = Int32(1+tf/h) ; # Total number of instances
> 
> @everywhere θₜ = zeros(Float64,n,N) ;
> 
> θₜ = MonteCarlo(N,agents,Nᵣ,Nₗ,rᵣ,rₗ,μ,σ,P,Pᵢ,Pᵣ,K,maxd,n,mcf)
> 
> ```
> 
> end

Where, The function MonteCarlo is

> @everywhere function MonteCarlo(N,agents,Nᵣ,Nₗ,rᵣ,rₗ,μ,σ,P,Pᵢ,Pᵣ,K,maxd,n,mcf)
> 
> for mc = 1:1:mcf
> 
> ```
> C = zeros(Float64,N,1) ;
> 
> θ = zeros(Float64,N,1) ;
> 
> R = zeros(Float64,N,1) ;
> 
> con = zeros(Int32,Nᵣ,1) ;
> 
> lib = zeros(Int32,Nₗ,1) ;
> 
> upcount = zeros(Int32,N,1) ;
> 
> upagents = zeros(Int32,N,1) ;
> 
> θₜ = zeros(Float64,n,N) ;
> 
> θMC = zeros(Float64,mcf,n,N) ;
> 
> NC = zeros(Int32,N,N) ;    
> 
> W = zeros(Float64,N,N) ;
> 
> Asep = zeros(Int32,N,N) ;
> 
> A = zeros(Int32,N,N) ;
> 
> neigh = zeros(Int32,N,N) ;
> 
> dc = zeros(Int32,N,N) ;
> 
> hop = zeros(Int32,N,N) ;
> 
> hopscore = zeros(Float64,N,N) ;
> 
> all = zeros(Int32,N,N) ;
> 
> (R,con,lib,θ) = initial(N,Nᵣ,rᵣ,rₗ,μ,σ) ;
> 
> Asep = separation(R,θ,N) ;
> 
> (g,gbef,gaf,A,Abef,Aaf,o,obef,oaf) = graphgeneration(N,Pᵣ,Asep) ;
> 
> C = kcentrality(g) ;
> 
> W = initialweights(A,C,N) ;
> 
> for t = 1:1:n
> 
> θₜ[t,:] = θ' ;
>     
> (upagents,upcount,neigh,dc,hop,all,hopscore,NC,A,W,Asep) = timeloop(A,obef,Asep,NC,W,all,hopscore,hop,upcount,upagents,R,C,θ,N,P,Pᵢ) ;
>     
> end
>  
> θ[mc,:,:] = θₜ ;
>     
> end
> 
> return θₜ
> 
> ```
> 
> end

I would like each worker to execute one outer-loop (corresponding to one set of initial conditions).

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [February 22, 2019, 9:56am UTC](https://discourse.julialang.org/t/parallel-computations/21070/2 "2019-02-22T09:56:44Z")

</div>

xref: [https://stackoverflow.com/questions/54823104/sharedarrays-in-julia](https://stackoverflow.com/questions/54823104/sharedarrays-in-julia)

Please try to avoid double posts.

---

<div class="post-metadata">

**Author:** ![ennvvy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ennvvy/32/7816_2.png) [@ennvvy](https://discourse.julialang.org/u/ennvvy)\
**Post date:** [February 22, 2019, 9:57am UTC](https://discourse.julialang.org/t/parallel-computations/21070/3 "2019-02-22T09:57:41Z")

</div>

I have now taken it off from stack overflow.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [February 22, 2019, 10:15am UTC](https://discourse.julialang.org/t/parallel-computations/21070/4 "2019-02-22T10:15:43Z")

</div>

you should use `pmap`

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [February 22, 2019, 10:21am UTC](https://discourse.julialang.org/t/parallel-computations/21070/5 "2019-02-22T10:21:45Z")

</div>

I gave you the recommendation on SO before you have posted your question here on Discourse, so does that solution work for you or you need something else?

---

<div class="post-metadata">

**Author:** ![ennvvy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ennvvy/32/7816_2.png) [@ennvvy](https://discourse.julialang.org/u/ennvvy)\
**Post date:** [February 22, 2019, 10:34am UTC](https://discourse.julialang.org/t/parallel-computations/21070/6 "2019-02-22T10:34:33Z")

</div>

I have now edited my question with the actual code. Can anyone please provide suggestions on how to make it parallel computing compliant?

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [February 22, 2019, 2:07pm UTC](https://discourse.julialang.org/t/parallel-computations/21070/7 "2019-02-22T14:07:54Z")

</div>

Instead of `@everywhere` in front of every global variable, you can simply write a function containing all these global variables and calls other functions, and put `@everywhere` in front of it. Also many functions are missing for people here to test, where is `initial`, `graphgeneration`, or `timeloop`?
