Julia 中的并行雅可比
Jacobi in Parallel in Julia
我正在尝试并行 运行 这个 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
没关系我知道了already.thank你
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
我正在尝试并行 运行 这个 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
没关系我知道了already.thank你
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