Parrallel programming2

Hello im trying to run this for loop parallel on evwery Process but still getting Error ,I Triyed it in 2 ways, can someone Help please?thanks

using Distributed

addprocs(2)

@everywhere using DistributedArrays

@everywhere using SharedArrays

A = SharedArray[{Float64}(n-1,n-1)

@distributed (+) for i in 1:n-2
     A[i,i+1] = -1
     A[i,i]   = 2
     A[i+1,i] = -1
end

@sync @distributed  for i in 1:n-2
    A[i,i+1] = -1
    A[i,i]   = 2
    A[i+1,i] = -1
end

ERROR: LoadError: SingularException(1)
Stacktrace:
[1] ldiv!(::Diagonal{Float64,Array{Float64,1}}, ::Array{Float64,2}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\LinearAlgebra\src\diagonal.jl:575

The code runs for me, so you must be doing something else that is resulting in the issue. Could you please post a self-contained example that doesn’t work? You might want to run this example in a separate julia session to verify that this works.

julia> using Distributed

julia> addprocs(2)
2-element Array{Int64,1}:
 2
 3

julia> @everywhere using SharedArrays

julia> n=10
10

julia> A = SharedArray{Float64}(n-1,n-1);

julia> @distributed (+) for i in 1:n-2
            A[i,i+1] = -1
            A[i,i]   = 2
            A[i+1,i] = -1
       end
-8

julia> A
9×9 SharedArray{Float64,2}:
  2.0  -1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
 -1.0   2.0  -1.0   0.0   0.0   0.0   0.0   0.0   0.0
  0.0  -1.0   2.0  -1.0   0.0   0.0   0.0   0.0   0.0
  0.0   0.0  -1.0   2.0  -1.0   0.0   0.0   0.0   0.0
  0.0   0.0   0.0  -1.0   2.0  -1.0   0.0   0.0   0.0
  0.0   0.0   0.0   0.0  -1.0   2.0  -1.0   0.0   0.0
  0.0   0.0   0.0   0.0   0.0  -1.0   2.0  -1.0   0.0
  0.0   0.0   0.0   0.0   0.0   0.0  -1.0   2.0  -1.0
  0.0   0.0   0.0   0.0   0.0   0.0   0.0  -1.0   0.0

Thanks for the reply here ist the complete code

FD-Solution of Poisson-Equation on [0,1]

using Distributed
addprocs(1)
@everywhere n=10
@everywhere using LinearAlgebra
@everywhere using SharedArrays
@everywhere using DistributedArrays
using Plots

@everywhere function Plot_Poisson_Equation(n)

A = SharedArray{Float64}(zeros(n-1,n-1))

#creating a 3 Diagonal Matrix

@distributed (+) for i in 1:n-2

    A[i,i+1] = -1

    A[i,i]   = 2

    A[i+1,i] = -1

end

A[n-1,n-1] = 2

return A

end

@everywhere function secondpar_b(n)

h = 1/n

b = h^2 * ones(n-1,1)

end

#calling each function on separate process (Parallel)

end

A = fetch(@spawnat 1 Plot_Poisson_Equation(n))

b = fetch(@spawnat 2 secondpar_b(n))

u = A\b

#adding boundary condition

u = [0 u’ 0]’

h = 1/n

plot(collect(0:h:1),u,background_color= BGR(0.1,0.1,0.1),lw=3)

xlabel!(“x”)

ylabel!(“y”)

title!(“FD-Solution of Poisson-Equation on [0,1]”)

That looks fishy to me.