# Parrallel programming2

**URL:** https://discourse.julialang.org/t/parrallel-programming2/42027
**Category:** Performance
**Tags:** question
**Created:** [June 25, 2020, 7:33am UTC](https://discourse.julialang.org/t/parrallel-programming2/42027 "2020-06-25T07:33:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hyacykcharel](https://avatars.discourse-cdn.com/v4/letter/h/58956e/32.png) [@hyacykcharel](https://discourse.julialang.org/u/hyacykcharel)
#### Post date: [June 25, 2020, 7:33am UTC](https://discourse.julialang.org/t/parrallel-programming2/42027/1 "2020-06-25T07:33:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)
#### Post date: [June 25, 2020, 8:14am UTC](https://discourse.julialang.org/t/parrallel-programming2/42027/2 "2020-06-25T08:14:26Z")

</div>

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
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

```

---

<div class="post-metadata">

### Author: ![hyacykcharel](https://avatars.discourse-cdn.com/v4/letter/h/58956e/32.png) [@hyacykcharel](https://discourse.julialang.org/u/hyacykcharel)
#### Post date: [June 25, 2020, 8:33am UTC](https://discourse.julialang.org/t/parrallel-programming2/42027/3 "2020-06-25T08:33:53Z")

</div>

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]”)

---

<div class="post-metadata">

### Author: ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)
#### Post date: [June 25, 2020, 10:46am UTC](https://discourse.julialang.org/t/parrallel-programming2/42027/4 "2020-06-25T10:46:40Z")

</div>

That looks fishy to me.
