Skip to content

Instantly share code, notes, and snippets.

@vlandau
Last active July 19, 2019 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlandau/78e0c411c39554701659a5cf8012bd85 to your computer and use it in GitHub Desktop.
Save vlandau/78e0c411c39554701659a5cf8012bd85 to your computer and use it in GitHub Desktop.
Parallel distributed processing in Julia
using Distributed
addprocs(2)
q = fill(rand(9), 3, 3)
@everywhere nrows = @eval size($q, 1)
@everywhere ncols = @eval size($q, 2)
for i in workers()
@spawnat i eval(:(a = fill(0, nrows, ncols)))
end
@everywhere function add_n!(a, n)
a[2,2] = a[2,2] .+ n
end
pmap(x -> add_n!(a, x), [1,1,1,1,1,1,1,1,1,1])
function my_sum()
summed = fill(0, 3, 3)
for i in workers()
summed = summed .+ @fetchfrom i a
end
summed
end
my_sum()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment