Messages
Reusing this material
This work is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International License.
http://creativecommons.org/licenses/by-nc-sa/4.0/deed.en_US
This means you are free to copy and redistribute the material and adapt and build on the
material under the following terms: You must give appropriate credit, provide a link to the license and indicate if changes were made. If you adapt or build on the material you must distribute your work under the same license as the original.
Acknowledge EPCC as follows: EPCC, The Universit of Edinburgh, www.epcc.ed.ac.uk
Note that this presentation contains images owned by others. Please seek their permission before reusing these images.
3
Messages
A message contains a number of elements of some particular datatype.
MPI datatypes:
– Basic types.
– Derived types. 派生类型
Derived types can be built up from basic types. C types are different from Fortran types.
4
MPI Basic Datatypes – C
MPI Datatype
C datatype
MPI_CHAR
signed char
MPI_SHORT
signed short int
MPI_INT
signed int
MPI_LONG
signed long int
MPI_UNSIGNED_CHAR
unsigned char
MPI_UNSIGNED_SHORT
unsigned short int
MPI_UNSIGNED
unsigned int
MPI_UNSIGNED_LONG
unsigned long int
MPI_FLOAT
float
MPI_DOUBLE
double
MPI_LONG_DOUBLE
long double
MPI_BYTE
5
MPI Basic Datatypes – Fortran
MPI Datatype
Fortran Datatype
MPI_INTEGER
INTEGER
MPI_REAL
REAL
MPI_DOUBLE_PRECISION
DOUBLE PRECISION
MPI_COMPLEX
COMPLEX
MPI_LOGICAL
LOGICAL
MPI_CHARACTER
CHARACTER(1)
MPI_BYTE
6
Point-to-Point Communication
Point-to-Point Communication
Communicator
1
3
5
2
Destination
4 Source 0
ausendwgone ioeynaommuninfomm.mu
Communication between two processes.
Source process sends message to destination process. Communication takes place within a communicator.
Destination process is identified by its rank in the communicator.
8
Point-to-point messaging in MPI
Sender calls a SEND routine
– specifying the data that is to be sent – this is called the send buffer
oReceiver calls a RECEIVE routine
– specifying where the incoming data should be stored – this is called the receive buffer
Data goes into the receive buffer
描述消息的元数据也被传输
Metadata describing message also transferred – this is received into separate storage
– this is called the status
9
Communication modes
Sender mode
Synchronous send
Notes
Only completes when the receive has completed.
Buffered send
Aynmnm
Standard send
Receive
–
Completes when a message has arrived.
Always completes (unless an error occurs), tahea 啊 irrespective of receiver. 始终完成(除非发生错误),而不考虑接收方。 垗
Either synchronous or buffered. 时 musge
10
MPI Sender Modes
iStandard send Synchronous send Buffered send Receive
OPERATION
MPI CALL
MPI_Send
MPI_-Ssend MPI_Bsend
MPI_Recv
11
Sending a message
C:
int MPI_Ssend(void *buf, int count,
MPI_Datatype datatype,
data
一一
int dest, int tag, MPI_Comm comm);
Fortran:
MPI_SSEND(BUF, COUNT, DATATYPE, DEST,
TAG, COMM, IERROR)
INTEGER COUNT, DATATYPE, DEST, TAG INTEGER COMM, IERROR
12
Send data from rank 1 to rank 3
// Array of ten integers int x[10];
鰷
dest
tag
…
if (rank == 1)
imn
MPI_Ssend(x, 10, MPI_INT, 3, 0, MPI_COMM_WORLD);
巅𥗕
// Integer scalar
i n 我t x ;
…
if (rank == 1)
MPI_Ssend(&x, 1, MPI_INT, 3, 0, MPI_COMM_WORLD);
。
13
Send data from rank 1 to rank 3
! Array of ten eintegers integer, dimension(10) :: x …
if (rank .eq. 1)
dest
tag
CALL MPI_SSEND(x, 10, MPI_INTEGER, 3, 0,
MPI_COMM_WORLD, ierr)
! Integer scalar
integer :: x
…
if (rank .eq. 1)
CALL MPI_SSEND(x, 1, MPI_INTEGER, 3, 0,
MPI_COMM_WORLD, ierr)
14
Receiving a message
C:
int MPI_Recv(void *buf, int count,
MPI_Comm comm,
MPI_Datatype datatype, int source, int tag,
lhhndatanstructure
Fortran:
MPI_RECV(BUF, COUNT, DATATYPE, SOURCE, TAG, COMM,
STATUS, IERROR)
INTEGER COUNT, DATATYPE, SOURCE, TAG, COMM,
STATUS(MPI_STATUS_SIZE), IERROR
M
t
t
u
PI
)
_S
at
us
*s
at
s
15
Receive data from rank 1 on ranki3
o_o
src
tag
int y[10]; MPI_Status status; …
if (rank == 3)
MPI_Recv(y, 10, MPI_INT, 1, 0, MPI_COMM_WORLD,
&status);
int y;
if (rank == 3)
dat.reseneenougnspaeetoreaiewinta.gr
… ifhisnoenghspae.throwanmr._io MPI_Recv(&y, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, &status);
16
Receive data from rank 1 on rank 3
integer, dimension(10) :: y
integer, dimension(MPI_STATUS_SIZE) :: status
…
if (rank .eq. 3)
src
tag
CALL MPI_RECV(y, 10, MPI_INTEGER, 1, 0,
MPI_COMM_WORLD, status, ierr)
integer :: y
…
if (rank .eq. 3)
CALL MPI_RECV(y, 1, MPI_INTEGER, 1, 0,
MPI_COMM_WORLD, status, ierr)
。
17
Synchronous Blocking Message-Passing
同步阻塞消息传递
同步
。Sender process specifies the synchronous mode.
Blocking: both processes wait until the transaction has completed.
一
Processes synchronise. 进程
18
For a communication to succeed:
← Sender must specify a valid destination rank. Receiver must specify a valid source rank.
The communicator must be the same.
Tags must match.
Message types must match.
Receiver’s buffer must be large enough.
19
-Wildcarding 通 配符方式 。
Receiver can wildcard.
To receive from any source MPI_ANY_SOURCE To receive with any tag MPI_ANY_TAG
Actual source and tag are returned in the receiver’s status parameter.
20
Communication Envelope
Senders Address For the attention of:
Data
Item 1
Item 2 Item 3
Destination Address
21
Commmunication一Envelope Information Envelope information is returned from MPI_RECV as
。status
Information includes:
– Source: status.MPI_SOURCE or status(MPI_SOURCE) – Tag: status.MPI_TAG orstatus(MPI_TAG)
– Count: MPI_Get_count or MPI_GET_COUNT
22
Received Message Count
C:
int MPI_Get_count( MPI_Status *status,
MPI_Datatype datatype, int *count)
Fortran:
MPI_GET_COUNT(STATUS, DATATYPE, COUNT, IERROR)
INTEGER STATUS(MPI_STATUS_SIZE), DATATYPE, COUNT, IERROR
23
消息顺序保存
Message Order Preservation
1
5
3
4
0
2
Communicator
Messages do not overtake each other.
This is true even for non-synchronous sends.
消息不会彼此重叠。
24
Message Matching (i)
Rank 0:
Ssend(msg1, dest=1, tag=1)
Ssend(msg2, dest=1, tag=2)
Rank 1:
Recv(buf1, src=0, tag=1)
Recv(buf2, src=0, tag=2)
buf1 = msg1; buf2 = msg2
Sends and receives correctly matched
25
Message Matching (ii)
Rank 0:
Ssend(msg1, dest=1, tag=1)
Ssend(msg2, dest=1, tag=2)
Rank 1:
Recv(buf2, src=0, tag=2) Recv(buf1, src=0, tag=1)
Deadlock (due to synchronous send)
Sends and receives incorrectly matched
26
Message Matching (iii)
Rank 0:
Bsend(msg1, dest=1, tag=1)
Bsend(msg2, dest=1, tag=1)
Rank 1:
Recv(buf1, src=0, tag=1)
Recv(buf2, src=0, tag=1)
buf1 = msg1; buf2 = msg2
Messages have same tags but matched in order
27
Message Matching (iv)
Rank 0:
Bsend(msg1, dest=1, tag=1) Bsend(msg2, dest=1, tag=2)
Rank 1:
Recv(buf2, src=0, tag=2)
Recv(buf1, src=0, tag=1)
司
buf1 = msg1; buf2 = msg2
Do not have to receive messages in order!
28
Message Matching (v)
Rank 0:
Bsend(msg1, dest=1, tag=1)
Bsend(msg2, dest=1, tag=2)
Rank 1:
Recv(buf1, src=0, tag=MPI_ANY_TAG)
Recv(buf2, src=0, tag=MPI_ANY_TAG)
buf1 = msg1; buf2 = msg2
Messages guaranteed to match in send order
examine status to find out the actual tag values 29
保证消息按照发送顺序匹配
检查状态以找出实际的标记值
Message Order Preservation
If a receive matches multiple messages in the inbox – then the messages will be received in the order they were sent
Only relevant for multiple messages from the same
source
只与来自同一源的多个消息相关
30
Exercise Calculation of Pi
See Exercise 2 on the exercise sheet
Illustrates how to divide work based on rank
– and how to send point-to-point messages in an SPMD code Notes:
– the value of N in the expansion of pi is not the same as the number of processors
– you should expect to write a program such as N=100 running on 4 processors
– your code should be able to run on any number of processors
– do not hard code the number of processors in your program!
If you finish the pi example you may want to try Exercise 3 (ping-pong) but it is not essential
31
Timers
C:
double MPI_Wtime(void);
Fortran:
DOUBLE PRECISION MPI_WTIME()
Time is measured in seconds.
Time to perform a task is measured by consulting the
timer before and after
– subtract values to get elapsed time
执行一项任务的时间是通过前后查看计时器来测量的
Modify your program to measure its execution time and print it out.
减去值以得到经过的时间
32