Reputation: 13
I'm looking for help with julia programming. I'm a newbie in programming, and I know little about the structure of computer and programming, so please pardon me if I'm asking some stupid questions.
I have to do heavy calculation, so I wanna use parallel computing for the double for statement. The code is following:
using Distributed
@everywhere using DistributedArrays
addprocs(4)
function FreeSpace_2D(profile, x_prime, y_prime, d, x, y)
# profile is a 2D array, and x_prime and y_prime are 1D arrays. d, x, y is real numbers.
Nx = length(x_prime)
Ny = length(y_prime)
array = dzeros((Nx, Ny), workers()[1:4], [1, 4])
@distributed for i in 1:Nx
@distributed for j in 1:Ny
localpart(array)[i, j] = ( profile[i, j]*exp(-1im*0.5*k/d*((x-x_prime[i])^2+(y-y_prime[j])^2)) )
end
end
return array
end
but the code does not initialize the 'array'.
When I search internet, there are a few methods for initialization of 1D array using distributed, but they did not work on a 2D array.
I would appreciate for any help.
Upvotes: 1
Views: 969
Reputation: 42194
There are four problems with your code:
addprocs
and then call @everywhere
. Otherwise your @everywhere
will be called only on the master process@distributed
loops by putting @sync
macro before @distributed or provide an aggregator function that is: @distributed (some_func) for i in 1:1000
@distributed
loop distributes code across all available workers. Hence it does not make sense to include one @distributed
loop within another. I recommend putting @distributed
only in front of the external loop Here is a sample code that I tried to make similar to your goals and that works correctly:
using Distributed
addprocs(4)
@everywhere using Distributed
@everywhere using DistributedArrays
a = dzeros(Int, (4, 8), workers()[1:4], [1, 4])
@sync @distributed for j in 1:8
a_local = localpart(a)
for i in 1:4
a_local[i, ((j+1) % 2)+1 ] = 100j + 10i + myid()
end
end
Looking at the Array
a
reveals what has happened:
julia> a
4×8 DArray{Int64,2,Array{Int64,2}}:
112 212 313 413 514 614 715 815
122 222 323 423 524 624 725 825
132 232 333 433 534 634 735 835
142 242 343 443 544 644 745 845
Upvotes: 2