我在Fortran中做一个简单的基于MPI的程序。我已成功估算出部分和,但我在使用MPI_allreduce计算全局和时遇到了问题。
主要代码:
program tst_trap
use iso_fortran_env
use some_functions
implicit none
include 'mpif.h'
integer :: count1, count2, count_rate, i, npts,n,n1
real(kind=8) :: answer, dx, sum, x, xmax, xmin,ssum,ierror
integer(4) :: ista, iend,ierr, iproc, nproc
call MPI_INIT(ierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD, iproc, ierr)
npts = 1000000001
call para_range(1,npts,nproc, iproc, ista, iend)
write(output_unit,'(a)') 'Fortran version (MPI)'
dx = (xmax-xmin)/real(npts-1)
sum=0.0
do i = ista, iend
x = xmin + real(i-1)*dx
sum = sum + g(x)
end do
sum = (sum + 0.5*(g(xmin) + g(xmax)) )*dx
write(6,*) 'Procesor ',iproc,':',' partial sum=', sum
call MPI_allREDUCE(sum,ssum,1,MPI_REAL,MPI_SUM,MPI_COMM_WORLD,ierr)
sum = ssum
if ( iproc .eq. 0) write(6,*) 'global sum =', sum
call MPI_FINALIZE(ierr)
end program tst_trap
输出:
Procesor 0 : partial sum= 7.490350421761612E-002
Procesor 2 : partial sum= 3.94636946947332
Procesor 3 : partial sum= 19.0687865689115
Procesor 1 : partial sum= 0.696046284884674
global sum = 2.114738958711681E-314