fortran - 16 字节实数的 MPI_AllReduce 的奇怪结果

标签 fortran mpi openmpi quadruple-precision

编译器:gfortran-4.8.5

MPI 库:OpenMPI-1.7.2(预装 OpenSuSE 13.2)

这个程序:

  use mpi
  implicit none

  real*16 :: x
  integer :: ierr, irank, type16

  call MPI_Init(ierr)

  call MPI_Comm_Rank(MPI_Comm_World, irank, ierr)

  if (irank+1==1) x = 2.1
  if (irank+1==8) x = 2.8
  if (irank+1==7) x = 5.2
  if (irank+1==4) x = 6.7
  if (irank+1==6) x = 6.5
  if (irank+1==3) x = 5.7
  if (irank+1==2) x = 4.0
  if (irank+1==5) x = 6.8

  print '(a,i0,a,f3.1)', "rank+1: ",irank+1," x: ",x

  call MPI_AllReduce(MPI_IN_PLACE, x, 1, MPI_REAL16, MPI_MAX, MPI_Comm_World, ierr)

  if (irank==0) print '(i0,a,f3.1)', irank+1," max x: ", x

  call MPI_Finalize(ierr)
end

我还尝试了real(16)real(kind(1.q0))。对于此编译器,real(real128) 实际上相当于 real*10

结果是:

> mpif90 reduce16.f90 
> mpirun -n 8 ./a.out 
rank+1: 1 x: 2.1
rank+1: 2 x: 4.0
rank+1: 3 x: 5.7
rank+1: 4 x: 6.7
rank+1: 5 x: 6.8
rank+1: 6 x: 6.5
rank+1: 7 x: 5.2
rank+1: 8 x: 2.8
1 max x: 2.8

程序找到 real*10 保持 MPI_REAL16 的真实最大值。 MPI 规范(3.1,第 628 和 674 页)不太清楚 MPI_REAL16 是否对应于 real*16real(real128) 如果这些不同。

此外,假设 MPI_REAL16 实际上是 real(real128) 并尝试在程序中使用它会导致不同的问题:

Error: There is no specific subroutine for the generic 'mpi_recv' at (1)
Error: There is no specific subroutine for the generic 'mpi_send' at (1)

这对于 real*16 不会发生。 (忽略应该能够传递任何位模式,因此此检查是多余的)

使用 16 字节实数的正确方法是什么? OpenMPI 库是否有错误?

最佳答案

虽然这应该在每个 MPI 实现中都能正常工作,但一个简单的解决方法是为这种用 Fortran 编写的类型实现用户定义的归约,因此在 C 中实现它没有问题(这就是 MPICH 和OpenMPI 尝试做所有事情,因此当 C 无法重现 Fortran 的行为时会出现问题)。

下面是实现这一点的尝试。这是 Fortran 中用户定义的缩减。我确信经验丰富的现代 Fortran 程序员可以做得更好。

  subroutine sum_real16(iv,iov,n)
    implicit none
    integer, intent(in) ::  n
    real*16, intent(in) :: iv(:)
    real*16, intent(inout) :: iov(:)
    integer :: i
    do i = 1,n
      iov(i) = iov(i) + iv(i)
    enddo
  end subroutine sum_real16
  subroutine reduce_sum_real16(iv, iov, n, dt)
    use, intrinsic ::  iso_c_binding, only : c_ptr
    use mpi_f08
    implicit none
    type(c_ptr), value ::  iv, iov
    integer ::  n
    type(MPI_Datatype) ::  dt
    if ( dt .eq. MPI_REAL16 ) then
        call sum_real16(iv,iov,n)
    endif
  end subroutine reduce_sum_real16
  program test_reduce_sum_real16
    use, intrinsic ::  iso_c_binding
    use mpi_f08
    implicit none
    integer, parameter ::  n = 10
    real*16 :: output(n)
    real*16 :: input(n)
    real*16 :: error
    integer :: me, np
    procedure(MPI_User_function) :: reduce_sum_real16
    type(MPI_Op) :: mysum
    integer :: i
    call MPI_Init()
    call MPI_Comm_rank(MPI_COMM_WORLD,me)
    call MPI_Comm_size(MPI_COMM_WORLD,np)
    output = 0.0
    input  = 1.0*me
    call MPI_Op_create(reduce_sum_real16,.true.,mysum)
    call MPI_Allreduce(input,output,n,MPI_REAL16,mysum,MPI_COMM_WORLD)
    error = 0.0
    do i = 1,n
      error = error + (output(i)-1.0*np)
    enddo
    if (error.gt.0.0) then
        print*,'SAD PANDA = ',error
        call MPI_Abort(MPI_COMM_SELF,1)
    endif
    call MPI_Op_free(mysum)
    call MPI_Finalize()
  end program test_reduce_sum_real16

此程序使用 Intel 16 Fortran 编译器和 MPICH 3.2+ 正常返回。显然我没有正确使用 I/O,所以我对这个程序正确性的信心不如我可以将所有结果写入 stdout 那样高。

关于fortran - 16 字节实数的 MPI_AllReduce 的奇怪结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33109040/

相关文章:

memory - 使用 Fortran 查找可用的显卡内存

c - MPI_Gather C 中的结构数组

c - 运行 openmpi 时出现段错误

memory - htop 显示即使调用了 deallocate 内存仍然在使用

if-statement - 这个 If 语句是如何工作的?

Fortran 接口(interface)本身会产生恼人的错误

c - 为什么这两个 MPI 发送的行为不同?

c++ - super 计算机上的 OpenMP

c - 为什么 mpirun 不尊重我对 BTL 的选择?

linux - 带有 Open MPI 1.8.4 的 DRBL 集群