0

考虑以下:

subroutine send_to_friend(a,b,c,request)
  implicit none
  include 'mpif.h'  !use mpi if you've built the mpif90 bindings...

  real a,b,c
  real buff(3)
  integer tag,dest,ierr,request

  tag = 50
  dest = 0

  buff(1) = a
  buff(2) = b
  buff(3) = c
  call MPI_Isend(buff,3,MPI_REAL,dest,tag,MPI_COMM_WORLD,request,ierr)
return
end subroutine send_to_friend

这可能不起作用,因为buff它将被放入堆栈(无论如何使用大多数现代编译器),但只要子例程退出,它就会被清除干净。分配数组也无济于事,因为根据此处(第 10 节),当您退出过程时,分配的数组会自动解除分配——在 C 中,这将是内存泄漏(也很糟糕)。做这样的事情的正确方法是什么?save我应该用属性声明数组吗?(static在 C 中)。这种设计一开始就存在缺陷吗?

4

1 回答 1

0

SAVE should be fine (best combine with allocatable), provided you do not come to other problems, which would require Fortran to pass the array via copy-in copy-out, it has to be done by reference (do not try to send non-contiguous data using nonblocking MPI).

If you have save variables in the routine, also do not try to run it from more threads, if you combine MPI/OpenMP.

于 2012-12-11T15:48:32.663 回答