我正在尝试用 jacobi 迭代解决 ax=b,我的串行代码工作正常,但 MPI 版本甚至无法运行。谁能帮我?
串行
program jacobis
implicit none
integer, parameter :: n=10
integer :: i,j,k,ni,s,seed
double precision :: tol,t1,t2,sig
double precision, dimension(0:n-1,0:n-1) :: A
double precision, dimension(0:n-1) :: B, x, xb, buff
ni=1000
seed=time()
call srand(seed)
do i=0, n-1
do j=0, n-1
A(i,j)=rand(0)
B(i)=rand(0)
end do
end do
do i = 0, n-1
A(i,i) = sum(A(i,:)) + 1
enddo
!do i=0,n-1
!A(i,i)=4
!end do
print *, "a", A
print *, "b", B
x=B
call cpu_time(t1)
do k=1,ni
xb=x
do i=0,n-1
s=0
do j=0,n-1
if (j/=i) then
s=s+A(i,j)*xb(j)
endif
end do
x(i)=(B(i)-s)/A(i,i)
sig=(x(i)-xb(i))*(x(i)-xb(i))
tol=tol+sig
tol=sqrt(tol)
end do
print *, "x", x
!print *, "tol=", tol
print *, "iter =",k
if (tol<1.000001) EXIT
if (k==(ni-1)) then
print *, "Numero Maximo de Iteracoes"
EXIT
endif
end do
call cpu_time(t2)
print *, "t=",t2-t1
end
MPI 版本
program jacobis
use mpi
implicit none
integer, parameter :: n=2
integer :: i_local,i_global,j,k,ni,s,m
double precision :: tol,t,t2,sig
double precision, dimension(:,:), ALLOCATABLE :: A_local
double precision, dimension(:), ALLOCATABLE :: B_local, x_local, x_temp1,x_temp2,x_old,x_new, buff
INTEGER, DIMENSION (MPI_STATUS_SIZE) :: STATUS
integer :: rank,procs,tag,ierror
CALL MPI_INIT(ierror)
CALL MPI_COMM_RANK(MPI_COMM_WORLD,rank,ierror)
CALL MPI_COMM_SIZE(MPI_COMM_WORLD,procs,ierror)
ni=100
m=n/procs
ALLOCATE (A_local(0:n-1,0:n-1))
ALLOCATE (B_local(0:m-1))
ALLOCATE (x_temp1(0:m-1))
ALLOCATE (x_temp2(0:m-1))
A_local=0
B_local=2
do i_global=0,n-1
A_local(i_global,i_global)=2
end do
CALL MPI_ALLGATHER(B_local, m, MPI_DOUBLE, x_temp1, m, MPI_DOUBLE, MPI_COMM_WORLD,ierror)
x_new=x_temp1
x_old=x_temp2
print *, "a", A_local
print *, "b", B_local
t=mpi_wtime()
do k=1,ni
x_old=x_new
do i_local=0,m-1
i_global=i_local+rank*m
!x_local(i_local)=b_local(i_local)
s=0
do j=0,n-1
if (j/=i_local) then
s=s+A_local(i_local,j)*x_old(j)
endif
end do
x_local(i_local)=(B_local(i_local)-s)/A_local(i_local,i_global)
end do
CALL MPI_ALLGATHER(x_local,m, MPI_DOUBLE, x_new, m, MPI_DOUBLE, MPI_COMM_WORLD,ierror)
do i_global=0,n-1
sig=(x_new(i_global)-x_old(i_global))*(x_new(i_global)-x_old(i_global))
tol=tol+sig
tol=sqrt(tol)
end do
print *, "x", x_local
print *, "tol=", tol
print *, "iter =",k
if (tol<1.000001) EXIT
if (k==(ni-1)) then
print *, "Numero Maximo de Iteracoes"
EXIT
endif
end do
t2=mpi_wtime()-t;
print *, "t=",t2
CALL MPI_FINALIZE(ierror)
end
谁能指出我做错了什么?是索引问题吗?请我今天真的需要解决这个问题,否则我会不及格。我在这上面花了无数个小时,却无法让它发挥作用。
好吧,你是对的!现在我有一个分段错误,但找不到它!已将代码替换为新版本