# Parallel Computations

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).

Please try to avoid double posts.

I have now taken it off from stack overflow.

you should use `pmap`

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?

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

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`?

2 Likes