Search code examples
arraysparallel-processingjuliadistributed-computingdistributed

Jacobi in Parallel in Julia


I'm trying to run this Jacobi code in parallel, but it's not working:

using Distributed
addprocs(2)
@everywhere using LinearAlgebra
@everywhere using DistributedArrays

@everywhere  n=5
@everywhere  m=n^2
I=Diagonal(ones(n,n))
A = Tridiagonal([fill(-1, n-2); -1], fill(2, n), [-1; fill(-1, n-2);])
A=kron(A,I)+kron(I,A)
b=ones(m,1)
x=ones(m,1)
d=ones(m,1)

A=distribute(A; dist=(2,1))
b=distribute(b; dist=(2,1))
x=distribute(x; dist=(2,1))
d=distribute(d; dist=(2,1))
D = distribute(ones(size(A,1),1); dist=(nworkers(), 1))  
@everywhere Δx=[]

@sync @distributed for z in 1:2
    for i in 1:8
        D_local=localpart(D)
        x_local=localpart(x)
        A_local=localpart(A)
        b_local=localpart(b)
            for j in 1:16
                if i!== j
                    global xx
                    x_local = x_local + inv(D_local)[i,i].*(b_local - A_local*x_local)
                end
            end
    end
    return xx
end

Solution

  • it is Okay I got it already.thank you

    using Distributed
    addprocs(2)
    @everywhere using LinearAlgebra
    @everywhere using DistributedArrays
    
    @everywhere  n=6
    @everywhere  m=n^2
    I=Diagonal(ones(n,n))
    A = Tridiagonal([fill(-1, n-2); -1], fill(2, n), [-1; fill(-1, n-2);])
    A=kron(A,I)+kron(I,A)
    b=ones(m,1)
    x=ones(m,1)
    d=ones(m,1)
    
    A=distribute(A; dist=(2,1))
    b=distribute(b; dist=(2,1))
    x=distribute(x; dist=(2,1))
    d=distribute(d; dist=(2,1))
    D = distribute(4*ones(size(A,1),1); dist=(2, 1))
    maxit = 100
    for z in 1:maxit
        @sync @distributed for t in 1:2
            for i in 1:length(localindices(A)[1])
                localpart(x)[i] =  localpart(x)[i] + ( localpart(b)[i] -  localpart(A*x)[i])./localpart(D)[i]
            end
            println(x)
        end
    end
    
    using Plots
    plot(convert(Array,x))
    
    x