[MPI] Point to point communication
- MPI_Send : send data to another process
- MPI_Send(buf, count, data_type, dest, tag, comm)
-Examples:
C/C++: MPI_Send(&x,1,MPI_INT,5,0,MPI_COMM_WORLD);
Fortran:MPI_Send(x,1,MPI_INTEGER,5,0,MPI_COMM_WORLD,ierr)
- MPI_Recv : receive data from another process
- MPI_Recv(buf, count, data_type, src, tag, comm, status)
- Examples
C/C++: MPI_Recv(&x,1,MPI_INT,5,0,MPI_COMM_WORLD,&stat);
Fortran: MPI_Recv(x,1,MPI_INTEGER,5,0,MPI_COMM_WORLD,stat,ierr)
* Notes on MPI_Recv
- A message is received when the followings are matched:
- Source (sending process ID/rank)
- Tag
- Communicator (e.g. MPI_COMM_WORLD)
- Wildcard values may be used:
- MPI_ANY_TAG : don't care what the tag value is
- MPI_ANY_SOURCE : don't care where it comes from; always receive
- Send/Recv Fortran90 Example Source Code
program hello
use mpi
integer :: f(10)
integer :: status(MPI_STATUS_SIZE)
integer :: rank, src=0, dest=1, ierr, i
call mpi_init(ierr)
call MPI_Comm_rank( MPI_COMM_WORLD, rank,ierr);
if (rank == src) then !process “dest” ignores this
call MPI_Send(f, 10, MPI_INT, dest, 0, MPI_COMM_WORLD,ierr)
do i = 1, 10
f(i) = f(i) - i
end do
end if
if (rank == dest) then !process “src” ignores this
call MPI_Recv(f, 10, MPI_INT, src, 0, MPI_COMM_WORLD, status,ierr)
do i = 1, 10
f(i) = f(i) + i
end do
end if
print *, f
call mpi_finalize(ierr)
end program hello
-Compile & Out put
>>mpiifort mpicomm.f90
>>mpiexec -n 2 mpicomm.exe
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
1 2 3 4 5 6 7 8 9 10
- MPI_Send(buf, count, data_type, dest, tag, comm)
Arguments
|
Meanings
|
buf
|
Starting address of send buffer
|
count
|
# of elements
|
data_type
|
Data type of each send buffer
element
|
dest
|
Processor ID (rank) destination
|
tag
|
Message tag
|
comm
|
communicator
|
-Examples:
C/C++: MPI_Send(&x,1,MPI_INT,5,0,MPI_COMM_WORLD);
Fortran:MPI_Send(x,1,MPI_INTEGER,5,0,MPI_COMM_WORLD,ierr)
- MPI_Recv : receive data from another process
- MPI_Recv(buf, count, data_type, src, tag, comm, status)
Arguments
|
Meanings
|
buf
|
Starting address of send buffer
|
count
|
# of elements
|
data_type
|
Data type of each send buffer
element
|
src
|
Processor ID (rank) destination
|
tag
|
Message tag
|
comm
|
communicator
|
status
|
Status object (an integer array in
Fortran)
|
- Examples
C/C++: MPI_Recv(&x,1,MPI_INT,5,0,MPI_COMM_WORLD,&stat);
Fortran: MPI_Recv(x,1,MPI_INTEGER,5,0,MPI_COMM_WORLD,stat,ierr)
* Notes on MPI_Recv
- A message is received when the followings are matched:
- Source (sending process ID/rank)
- Tag
- Communicator (e.g. MPI_COMM_WORLD)
- Wildcard values may be used:
- MPI_ANY_TAG : don't care what the tag value is
- MPI_ANY_SOURCE : don't care where it comes from; always receive
- Send/Recv Fortran90 Example Source Code
program hello
use mpi
integer :: f(10)
integer :: status(MPI_STATUS_SIZE)
integer :: rank, src=0, dest=1, ierr, i
call mpi_init(ierr)
call MPI_Comm_rank( MPI_COMM_WORLD, rank,ierr);
if (rank == src) then !process “dest” ignores this
call MPI_Send(f, 10, MPI_INT, dest, 0, MPI_COMM_WORLD,ierr)
do i = 1, 10
f(i) = f(i) - i
end do
end if
if (rank == dest) then !process “src” ignores this
call MPI_Recv(f, 10, MPI_INT, src, 0, MPI_COMM_WORLD, status,ierr)
do i = 1, 10
f(i) = f(i) + i
end do
end if
print *, f
call mpi_finalize(ierr)
end program hello
-Compile & Out put
>>mpiifort mpicomm.f90
>>mpiexec -n 2 mpicomm.exe
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
1 2 3 4 5 6 7 8 9 10
댓글
댓글 쓰기