To conclude, one needs dots, and also initialize the matrices where the results are stored.
For example, without dots at the equal sign ( B .=
),
using Distributed, DistributedArrays
# Add 4 processors only once
if nprocs() == 1
addprocs(4)
else
println("Only add 4 processors")
end
@everywhere using Distributed, DistributedArrays
println("Total number of processors including master")
display(nprocs()) # Verify the total number of processors
A = DArray(I->fill(myid(), length.(I)), (4, 4), workers()[1:4], [4,1])
println("---------------A-------------------")
display(A)
println("Checking: Array stored in proc 2")
display(fetch(@spawnat 2 A.localpart))
println("----------------------------------")
#println("Array stored in proc 3")
#display(fetch(@spawnat 3 A.localpart))
#println("Array stored in proc 4")
#display(fetch(@spawnat 4 A.localpart))
#println("Checking: Array stored in proc 5")
#display(fetch(@spawnat 5 A.localpart))
# Do some operations
# Addition
# B = DArray(I->fill(myid(), length.(I)), (4, 4), workers()[1:4], [4,1])
B = A .+ A
println("---------------B-------------------")
display(B)
println("Checking Matrix B: After addition new array stored in proc 2")
display(fetch(@spawnat 2 B.localpart))
println("----------------------------------")
# Multiplication
#C = DArray(I->fill(myid(), length.(I)), (4, 4), workers()[1:4], [4,1])
C = A .* A
println("---------------C-------------------")
display(C)
println("Checking Matrix C: After multiplication new array stored in proc 2")
display(fetch(@spawnat 2 C.localpart))
println("----------------------------------")
I get the result-1 where Arrays A, and B and C are stored differently by different processors. This is not what I want.
julia> include("darraytest.jl")
Only add 4 processors
Total number of processors including master
5
---------------A-------------------
4×4 DArray{Int64, 2, Matrix{Int64}}:
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
Checking: Array stored in proc 2
1×4 Matrix{Int64}:
2 2 2 2
----------------------------------
---------------B-------------------
4×4 DArray{Int64, 2, Matrix{Int64}}:
4 4 4 4
6 6 6 6
8 8 8 8
10 10 10 10
Checking Matrix B: After addition new array stored in proc 2
2×2 Matrix{Int64}:
4 4
6 6
----------------------------------
---------------C-------------------
4×4 DArray{Int64, 2, Matrix{Int64}}:
4 4 4 4
9 9 9 9
16 16 16 16
25 25 25 25
Checking Matrix C: After multiplication new array stored in proc 2
2×2 Matrix{Int64}:
4 4
9 9
----------------------------------
With dots, but without initialisation, I get error for multiplying matrices.
using Distributed, DistributedArrays
# Add 4 processors only once
if nprocs() == 1
addprocs(4)
else
println("Only add 4 processors")
end
@everywhere using Distributed, DistributedArrays
println("Total number of processors including master")
display(nprocs()) # Verify the total number of processors
A = DArray(I->fill(myid(), length.(I)), (4, 4), workers()[1:4], [4,1])
println("---------------A-------------------")
display(A)
println("Checking: Array stored in proc 2")
display(fetch(@spawnat 2 A.localpart))
println("----------------------------------")
#println("Array stored in proc 3")
#display(fetch(@spawnat 3 A.localpart))
#println("Array stored in proc 4")
#display(fetch(@spawnat 4 A.localpart))
#println("Checking: Array stored in proc 5")
#display(fetch(@spawnat 5 A.localpart))
# Do some operations
# Addition
# B = DArray(I->fill(myid(), length.(I)), (4, 4), workers()[1:4], [4,1])
B .= A .+ A
println("---------------B-------------------")
display(B)
println("Checking Matrix B: After addition new array stored in proc 2")
display(fetch(@spawnat 2 B.localpart))
println("----------------------------------")
# Multiplication
#C = DArray(I->fill(myid(), length.(I)), (4, 4), workers()[1:4], [4,1])
C .= A .* A
println("---------------C-------------------")
display(C)
println("Checking Matrix C: After multiplication new array stored in proc 2")
display(fetch(@spawnat 2 C.localpart))
println("----------------------------------")
I get the result-2 which throws an error
julia> include("darraytest.jl")
Total number of processors including master
5
---------------A-------------------
4×4 DArray{Int64, 2, Matrix{Int64}}:
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
Checking: Array stored in proc 2
1×4 Matrix{Int64}:
2 2 2 2
----------------------------------
ERROR: LoadError: UndefVarError: B not defined
Stacktrace:
[1] top-level scope
@ ~/Desktop/mydocuments/RESEARCH/juliacodes/gausslegendre/darraytest.jl:30
[2] include(fname::String)
@ Base.MainInclude ./client.jl:444
[3] top-level scope
@ none:1
in expression starting at /home/debasish/Desktop/mydocuments/RESEARCH/juliacodes/gausslegendre/darraytest.jl:30
Strangely, the above code will work for B = A + A
!!
Finally, the last code gives the desired result, where we use dots and also initialise the matrices before hand.
using Distributed, DistributedArrays
# Add 4 processors only once
if nprocs() == 1
addprocs(4)
else
println("Only add 4 processors")
end
@everywhere using Distributed, DistributedArrays
println("Total number of processors including master")
display(nprocs()) # Verify the total number of processors
A = DArray(I->fill(myid(), length.(I)), (4, 4), workers()[1:4], [4,1])
println("---------------A-------------------")
display(A)
println("Checking: Array stored in proc 2")
display(fetch(@spawnat 2 A.localpart))
println("----------------------------------")
#println("Array stored in proc 3")
#display(fetch(@spawnat 3 A.localpart))
#println("Array stored in proc 4")
#display(fetch(@spawnat 4 A.localpart))
#println("Checking: Array stored in proc 5")
#display(fetch(@spawnat 5 A.localpart))
# Do some operations
# Addition
B = DArray(I->fill(myid(), length.(I)), (4, 4), workers()[1:4], [4,1])
B .= A .+ A
println("---------------B-------------------")
display(B)
println("Checking Matrix B: After addition new array stored in proc 2")
display(fetch(@spawnat 2 B.localpart))
println("----------------------------------")
# Multiplication
C = DArray(I->fill(myid(), length.(I)), (4, 4), workers()[1:4], [4,1])
C .= A .* A
println("---------------C-------------------")
display(C)
println("Checking Matrix C: After multiplication new array stored in proc 2")
display(fetch(@spawnat 2 C.localpart))
println("----------------------------------")
The result-3 is
julia> include("darraytest.jl")
Total number of processors including master
5
---------------A-------------------
4×4 DArray{Int64, 2, Matrix{Int64}}:
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
Checking: Array stored in proc 2
1×4 Matrix{Int64}:
2 2 2 2
----------------------------------
---------------B-------------------
4×4 DArray{Int64, 2, Matrix{Int64}}:
4 4 4 4
6 6 6 6
8 8 8 8
10 10 10 10
Checking Matrix B: After addition new array stored in proc 2
1×4 Matrix{Int64}:
4 4 4 4
----------------------------------
---------------C-------------------
4×4 DArray{Int64, 2, Matrix{Int64}}:
4 4 4 4
9 9 9 9
16 16 16 16
25 25 25 25
Checking Matrix C: After multiplication new array stored in proc 2
1×4 Matrix{Int64}:
4 4 4 4
----------------------------------
This is the desired result. Hence, I think if one does element-by-element operation, for e.g. in a for-loop, things should be okay. But for broadcasting operations, one needs to be careful.