1
votes

I'm trying to run this code, but why I'm I getting these 2 rows in the Middle with 00000, can someone help me, to get that fixed, please?

using Distributed #Bereitstellung der Bibliothekee zur Parallelen Programieru
addprocs(2)
@everywhere using LinearAlgebra #Bereitstellung der LinearAlgebra Bibliotheke
@everywhere using DistributedArrays #Bereitstellung der DistributedArrays 
@everywhere T =(zeros(n,n))
T[:,1].=10 #Randbedingungen T_links =10
T[:,end].=10 #Randbedingungen T_rechts =10
T = distribute(T; dist=(2,1))
@everywhere maxit = 100 #maximale Iterrationsanzahl
@everywhere function Poissons_2D(T)
    for w in 1:maxit
        @sync @distributed  for p in 1:nworkers()
            for i in 2:length(localindices(T)[1])-1
                for j in 2:length(localindices(T)[2])-1
                    localpart(T)[i,j] = (1/4 * (localpart(T)[i-1,j] + localpart(T)[i+1,j] + localpart(T)[i,j-1] + localpart(T)[i,j+1]))
                end
            end
        end
    end
    return T
end

Poissons_2D(T)
10×10 DArray{Float64,2,Array{Float64,2}}:
 10.0  0.0      0.0      0.0      …  0.0      0.0      0.0      10.0
 10.0  4.33779  2.00971  1.01077     1.01077  2.00971  4.33779  10.0
 10.0  5.34146  2.69026  1.40017     1.40017  2.69026  5.34146  10.0
 10.0  4.33779  2.00971  1.01077     1.01077  2.00971  4.33779  10.0
 10.0  0.0      0.0      0.0         0.0      0.0      0.0      10.0
 10.0  0.0      0.0      0.0      …  0.0      0.0      0.0      10.0
 10.0  4.33779  2.00971  1.01077     1.01077  2.00971  4.33779  10.0
 10.0  5.34146  2.69026  1.40017     1.40017  2.69026  5.34146  10.0
 10.0  4.33779  2.00971  1.01077     1.01077  2.00971  4.33779  10.0
 10.0  0.0      0.0      0.0         0.0      0.0      0.0      10.0
2
This is the entire codeHyacy K-Charel

2 Answers

2
votes

The first cleanup could look like this:

a =(zeros(10,10)) 
a[:,[1,end]] .= 10 
a = distribute(a; dist=(nworkers(),1))

function Poissons_2D(a::DArray, maxit::Int=100)
    for w in 1:maxit
        @sync @distributed  for p in 1:nworkers()
            local_a = localpart(a)
            local_ind = localindices(a)
            for iix in 1:length(local_ind[1])
                i = local_ind[1][iix]
        (i==1 || i==size(a,1)) && continue
                for j in local_ind[2][2:end-1]
                   local_a[iix,j] = (1/4 * (a[i-1,j] + a[i+1,j] + a[i,j-1] + a[i,j+1]))
                end
            end
        end
    end
    a
end

Some remarks:

  • Do not use @everywhere in front of T - you do not want to define it on all workers
  • in Julia you use by convention T to denote parametric types so use a, or some T-like LaTeX symbol

However, your function takes values from all adjacent cells to calculate new values. I do not know how do you plan to handle situation when the value does not exist yet.

In particular if each row requires value from the previous row and previous column it is not possible to parallelize this computation at all (because you need to wait for the previous value to get the next one).

julia> Poissons_2D(a)
10×10 DArray{Float64,2,Array{Float64,2}}:
 10.0  0.0      0.0      0.0      0.0      0.0      0.0      0.0      0.0      10.0
 10.0  4.99998  3.05213  2.20861  1.87565  1.87565  2.20862  3.05214  4.99999  10.0
 10.0  6.9478   4.99994  3.90669  3.41834  3.41834  3.9067   4.99995  6.94781  10.0
 10.0  7.7913   6.09315  4.99989  4.47269  4.4727   4.99991  6.09317  7.79131  10.0
 10.0  8.12425  6.58148  5.52707  4.99987  4.99988  5.52709  6.58151  8.12427  10.0
 10.0  8.12425  6.58148  5.52707  4.99987  4.99988  5.52709  6.58151  8.12427  10.0
 10.0  7.7913   6.09316  4.99991  4.47271  4.47271  4.99992  6.09317  7.79131  10.0
 10.0  6.94781  4.99995  3.90671  3.41835  3.41836  3.90672  4.99996  6.94782  10.0
 10.0  4.99999  3.05214  2.20862  1.87566  1.87566  2.20863  3.05215  4.99999  10.0
 10.0  0.0      0.0      0.0      0.0      0.0      0.0      0.0      0.0      10.0
0
votes

I think the problem are the ranges of for with I and j. You range is from 2 to N-1, avoiding the extremes. It is right because you are missing the information to calculate them, because it is stored in a different process. However you need to transfer the limits information. In MPI, for instance you could send redundant information to avoid that, but in Distributed I am not sure. I see the cause, but the solution is not easy. At least I hope to have helped a little.