微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

雅各比与朱莉娅平行

如何解决雅各比与朱莉娅平行

我正在尝试并行运行此Jacobi代码,但是它不起作用:

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

解决方法

没关系,我已经知道了。谢谢

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


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。