本帖最后由 kyra 于 2020-3-25 09:11 编辑
初步学些fortran mpi 并行编程,采用的工具为vs2010+ivf2011 已经构造好mpi环境,并且已经测试通过了 hello world
今天学习了规约函数MPI_REDUCE的使用
你解决的问题和编程思路:
变量homephi=1.0;
开辟4个进程 利用规约函数 MPI_REDUCE(SENDBUF, RECVBUF, COUNT, DATATYPE, OP, ROOT, COMM,IERROR)结合mpi_in_place
计算 4个homephi 的和
理想结果应该为 4.0
程序如下
[Fortran] 纯文本查看 复制代码 program main
include 'mpif.h'
character * (MPI_MAX_PROCESSOR_NAME) processor_name
integer myid, numprocs, namelen, rc,ierr
real*8 homepi
call MPI_INIT( ierr )
call MPI_COMM_RANK( MPI_COMM_WORLD, myid, ierr )
call MPI_COMM_SIZE( MPI_COMM_WORLD, numprocs, ierr )
call MPI_GET_PROCESSOR_NAME(processor_name, namelen, ierr)
homepi=1.0
if(myid.NE.0)then
call MPI_REDUCE(homepi, homepi,1, MPI_DOUBLE_PRECISION,
& MPI_SUM, 0, MPI_COMM_WORLD, ierr )
else
call MPI_REDUCE(MPI_IN_PLACE, homepi,1, MPI_DOUBLE_PRECISION,
& MPI_SUM, 0, MPI_COMM_WORLD, ierr )
write(*,33) homepi
33 format(' sum value of pi = ',f10.8)
endif
call MPI_FINALIZE(rc)
end
程序调试正常,结合cmd命令窗输入如下命令
mpiexec -n 4 mpitest_f77
计算的结果为
sum value of pi =3.00000000
显然不对呀,理论结果应该是4呀!!!!!!!!!!!!!!!!!!!
请教高手指教,程序出错的原因,以及修改思路,先谢谢了!!
|