Arash
Arash

Reputation: 1

how to use mpi_shared memory among nodes?

The idea of mpi_shared memory is to create a window among the processes, this idea could be feasible for the processes in one node in which the processes have shared memory, but is it also possible to create a window for the processes between 2 nodes? I mean creating one window among all the processes in node 1 and node 2? for example I have 2 nodes, each nodes has 4 processes. I have a simple code in which I have used mpi_shared_memory in one node(among 4 processes),each processes will update a part of a 2d array (this array is called matrix in the code) and afterward , all the processes has direct access to this shared array. I mean at the end of this routine,each process will see the complete version of the array.

this code is working for a single node, but I was wonder how to get same result with 2 nodes(among 8 processes)?

program mpi_shared
USE, INTRINSIC :: ISO_C_BINDING, ONLY : C_PTR, C_F_POINTER
use mpi
implicit none
integer :: win,hostcomm,hostrank
INTEGER(KIND=MPI_ADDRESS_KIND) :: windowsize
INTEGER :: disp_unit,my_rank,ierr,total, i
TYPE(C_PTR) :: baseptr,baseptr2
integer, POINTER :: matrix(:,:)
integer,allocatable :: arrayshape(:)

call MPI_INIT( ierr )

call MPI_COMM_RANK(MPI_COMM_WORLD,my_rank, ierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD,total,ierr)
CALL MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, 0,&
           MPI_INFO_NULL, hostcomm,ierr)
CALL MPI_Comm_rank(hostcomm, hostrank,ierr)


allocate(arrayshape(2))
arrayshape=[4,3]
if (hostrank == 0) then
 windowsize = int(10**4,MPI_ADDRESS_KIND)*8_MPI_ADDRESS_KIND
else
  windowsize = 0_MPI_ADDRESS_KIND
end if
disp_unit = 1
CALL MPI_Win_allocate_shared(windowsize, disp_unit, MPI_INFO_NULL,&
  hostcomm, baseptr, win, ierr)


 if (hostrank /= 0) then
 CALL MPI_Win_shared_query(win, 0, windowsize, disp_unit, baseptr,&  
 ierr)
 end if


CALL C_F_POINTER(baseptr, matrix,arrayshape)



if (hostrank == 0) then
 matrix=0
endif

call MPI_WIN_FENCE(0, win, ierr)

  if (hostrank == 0) then
    matrix(1,:)=1
 elseif (hostrank == 1) then
    matrix(2,:)=2
 elseif (hostrank == 2)  then
     matrix(3,:)=3
 elseif (hostrank == 3)  then
     matrix(4,:)=4
  endif

 call MPI_WIN_FENCE(0, win, ierr)
 write(*,*) 'iam rank',hostrank,'with matrix= ',matrix



 call MPI_WIN_FENCE(0, win, ierr)
 call MPI_BARRIER(MPI_COMM_WORLD,ierr)
 call MPI_Win_free(win,ierr)
 call MPI_FINALIZE(IERR)

 end program

Upvotes: 0

Views: 261

Answers (1)

Ian Bush
Ian Bush

Reputation: 7434

In principle a very clever MPI implementation could be able to do this kind of thing by providing a software layer to cover the details of the underlying hardware. But in practice, no, nodes by definition are disjoint areas of memory and as such shared memory windows are limited to a single node. And if you are programming in MPI by default you really should think in terms of distributed objects, this in most cases will lead to more scalable code.

Upvotes: 1

Related Questions