我正在编写一个纯MPI代码,其中需要在共享内存通信器中的所有任务之间共享多个变量。 使用其他视窗是否安全?例如:这些窗口在物理内存中有任何个风险重叠吗?
! number: number of elements in var1 and var2, number=n1*n2
! varsize: size_of(double), size of each element of var1 and var2
! shape: shape of var1 and var2, shape[n1,n2]
type(c_ptr) :: baseptr
integer(kind=mpi_address_kind) :: varsize,lb
integer :: dispunit
integer :: number
integer :: shape(2)
double precision, pointer, dimension(:,:) :: var1,var2
integer :: window_1,window_2
if(rank.eq.0)then
! only #0 allocates memory, others get zero memory
call mpi_type_get_extent(mpi_double_precision,lb,varsize,ierr)
dispunit=varsize
elseif(rank.gt.leader)then
varsize=0
dispunit=1
endif
! allocate var1
call mpi_win_allocate_shared(number*varsize,dispunit,mpi_info_null,part_comm,baseptr,window_1,ierr)
! get location of memory segment
if(rank.gt.0)then
call mpi_win_shared_query(window_1,0,number*varsize,dispunit,baseptr,ierr)
endif
! associate C pointer to Fortran pointer
call c_f_pointer(baseptr,var1,shape)
! allocate var2
call mpi_win_allocate_shared(number*varsize,dispunit,mpi_info_null,part_comm,baseptr,window_2,ierr)
! get location of memory segment
if(rank.gt.0)then
call mpi_win_shared_query(window_2,0,number*varsize,dispunit,baseptr,ierr)
endif
! associate C pointer to Fortran pointer
call c_f_pointer(baseptr,var2,shape)
var1和var2是否有可能彼此覆盖(内存中的相同物理地址)?
答案 0 :(得分:0)
我快速浏览了一下,但我在标准中找不到任何内容,但是var1和var2 必须不重叠,否则将无法编写任何有用的代码。如果var1和var2是普通的可分配数组,则可以保证在Fortran分配调用之后分配的内存将完全不同。 MPI_Win_allocate_shared()也必须如此,它才能以任何方式发挥作用。