0

我附上了我正在尝试编写的 MPI 程序的示例。当我使用“mpirun -np 4 a.out”运行这个程序时,我的输出是:

 Sender:            1
 Data received from            1
 Sender:            2
 Data received from            1
 Sender:            2

运行挂在那里。我不明白为什么发送者变量在 MPI_recv 之后会改变它的值?有任何想法吗?

谢谢你,普拉迪普

` 程序 mpi_test

    include  'mpif.h'

!----------------( Initialize variables )-------------------- 
  integer, dimension(3) :: recv, send

  integer :: sender, np, rank, ierror

  call  mpi_init( ierror )
  call  mpi_comm_rank( mpi_comm_world, rank, ierror )
  call  mpi_comm_size( mpi_comm_world, np, ierror )

!----------------( Main program )-------------------- 

!     receive the data from the other processors
  if (rank.eq.0) then
     do sender = 1, np-1
        print *, "Sender: ", sender
        call mpi_recv(recv, 3, mpi_int, sender, 1,
 &       mpi_comm_world, status, ierror)
        print *, "Data received from ",sender
     end do
  end if

!   send the data to the main processor
  if (rank.ne.0) then
     send(1) = 3
     send(2) = 4
     send(3) = 4
     call mpi_send(send, 3, mpi_int, 0, 1, mpi_comm_world, ierr)
  end if


!----------------( clean up )-------------------- 
  call mpi_finalize(ierror)

  return
  end program mpi_test`
4

1 回答 1

1

这是典型的堆栈粉碎场景。您尚未声明该status变量,因此编译器会自动REAL为您创建一个变量。但status应该是一个元素INTEGER数组:MPI_STATUS_SIZE

integer, dimension(MPI_STATUS_SIZE) :: status

在您的情况下发生的情况是,status它太小而无法保存真正的 MPI 状态对象,因此其他一些堆栈变量会被覆盖。只需声明status应该声明即可解决问题。

另一件事 - 现代 Fortran 支持该IMPLICIT NONE语句,该语句禁用未声明变量的自动声明。如果您implicit none紧跟在include语句之后,编译器会生成一条错误消息。

于 2013-02-18T16:35:50.193 回答