我在Fortran中运行计算,我正在使用MPI来并行化 do 循环。有4个外部输入数组(X,Y,zeta_list和M_list),但为了便于测试,我使用调用random_number 来生成输入数组。我使用MPI_Scatter来分配循环运行的数组 zeta_list 中的元素。
PROGRAM TEST_MPI
use mpi
IMPLICIT NONE
DOUBLE PRECISION,DIMENSION(1,10)::M_list
DOUBLE PRECISION,DIMENSION(11,11)::X,Y,z_m_z_x,z_m_z_y,dist_z_m_z,alpha_x, alpha_y, sub_alpha_x, sub_alpha_y
DOUBLE PRECISION,DIMENSION(2,10)::zeta_list
DOUBLE PRECISION,ALLOCATABLE,DIMENSION(:,:)::sub_zeta_list
INTEGER::i,myid,ntasks,ierror,n_elements,chunk
call MPI_INIT(ierror)
call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierror)
call MPI_COMM_SIZE(MPI_COMM_WORLD, ntasks, ierror)
if (myid == 0) then
call random_number(X)
call random_number(Y)
call random_number(zeta_list)
call random_number(M_list)
alpha_x = 0
alpha_y = 0
sub_alpha_x = 0
sub_alpha_y = 0
z_m_z_x = 0
z_m_z_y = 0
dist_z_m_z = 0
end if
n_elements = size(zeta_list,1)*size(zeta_list,2)
chunk = n_elements/ntasks
allocate(sub_zeta_list(2,chunk/2))
sub_zeta_list = 0
call MPI_Scatter(zeta_list,chunk,MPI_DOUBLE_PRECISION,sub_zeta_list,chunk,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD)
do i=1,chunk/2,1
z_m_z_x = X - sub_zeta_list(1,i)
z_m_z_y = Y - sub_zeta_list(2,i)
dist_z_m_z = z_m_z_x**2 + z_m_z_y**2
sub_alpha_x = sub_alpha_x + (M_list(1,i)* z_m_z_x / dist_z_m_z)
end do
call MPI_REDUCE(sub_alpha_x,alpha_x,121,MPI_DOUBLE_PRECISION,MPI_SUM,0,MPI_COMM_WORLD,ierror)
if (myid == 0) then
write(*,*)alpha_x
end if
call MPI_FINALIZE(ierror)
stop
END PROGRAM TEST_MPI
当我用mpif90 -g test.f90编译并执行mpirun -np 1 a.out时,我得到以下内容。第39行是我使用MPI_Scatter的地方,我检查了数组大小。我的Scatter功能可能出现什么问题?我想也许这种情况正在发生,因为输入数组zeta_list是2D,但我不完全确定。非常感谢任何帮助!
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0x7F1EC6382E08
#1 0x7F1EC6381F90
#2 0x7F1EC5FD24AF
#3 0x7F1EC66BC857
#4 0x401225 in MAIN__ at test9.f90:39
===================================================================================
= BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
= PID 9284 RUNNING AT juggernaut
= EXIT CODE: 139
= CLEANING UP REMAINING PROCESSES
= YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
===================================================================================
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)
This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions