Документ взят из кэша поисковой машины. Адрес оригинального документа : http://angel.cs.msu.su/~popova/Speccourse_2015/Lect2_061015.pdf
Дата изменения: Sat Oct 17 01:50:30 2015
Дата индексирования: Sat Apr 9 23:31:34 2016
Кодировка:

2015 ­ 2016 . . : .., . . .

2 6 2015 .

" ", 2

1


. MPI. .

" ", 2

2





MPI: A Message-Passing Interface Standard

http://www.mpi-forum.org/docs/







(http://parallel.ru/docs/Parallel/mpi1.1/mpi-report.html) .. MPI: . -.: - , 2004.-71 . .., .. .- , : https://computing.llnl.gov/tutorials/mpi, www.parallel.ru, intuit.ru, www.mpi-forum.org, www.open-mpi.org, www.openmp.org http://www.mcs.anl.gov/research/projects/mpi/www/www3

" ", 2

3










, (OpenMP, ...)



- "Message Passing" - Message Passing : MPI ("Message Passing Interface") PVM ("Parallel Virtual Machine") Shmem, MPT ( Cray)
" ", 2

4




#include #define N 1024 int main(int argc, char *argv[]) { double sum; double a[N], b[N]; int status, i, n =N; for (i=0; i " ", 2

5


MPI
#include #include #define N 1024 int main(int argc, char *argv[]) { double sum; double dot_prod; double a[N], b[N]; int i, n =N; int size, myrank; n= n/ size; for (i=myrank*n; i
MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, If ( !myrank) &myrank); printf ("Dot product =%f\n", dot_prod); MPI_Comm_size(MPI_COMM_WORLD, MPI_Finalize(); &size);

MPI_Reduce(& sum,& all_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORL

return 0; }

" ", 2

6


MPI





MPI 1.1 Standard 92-94 MPI 2.0 - 95-97 MPI 2.1 - 2008 MPI 3.0 ­ 2012


http://www.mcs.anl.gov/mpi http://www.mpi-forum.org/docs/docs.html

http://www-unix.mcs.anl.gov/mpi/www/
" ", 2

7


MPI


:






:


" ", 2

8


MPI








MPICH LAM/MPI Mvapich OpenMPI Intel,IBM .
" ", 2 9


MPI



, . MPI . :
.

" ", 2

10


Message passing = +

Process 0

Data

?

Process 1



Data Data Data Data Data Data Data Data

Time




" ", 2 11


MPI-
· SPMD ­ Single Program Multiple Data · · .




:

Processor 0
" ", 2

Processor
12

2.12


MPI-


: mpirun np : : mpirun ­np 3 prog np ()
- , .

0


1

2

MPI- :


np ­ rank [0 ... np-1] ­




" ", 2

13 13


MPI



MPI . .
.
Process 0 Send(data) Receive(data) Process 1



" ", 2

14


MPI



Process 0
Send(data) Receive(data)

Process 1



:




? ? ? ?
" ", 2 15






. . . , .

" ", 2

16


MPI


MPI , . MPI_COMM_WORLD: - MPI_Init : - MPI_COMM_SELF ­ ()
-



MPI_COMM_NULL ­
" ", 2 17


MPI



: (address, count, datatype), datatype :




, (, MPI_INT, MPI_DOUBLE_PRECISION) MPI



MPI , , , (int, float).
" ", 2 18


MPI-
MPI datatype
MPI_CHAR MPI_SHORT MPI_INT MPI_LONG MPI_UNSIGNED_CHAR MPI_UNSIGNED_SHORT MPI_UNSIGNED MPI_UNSIGNED_LONG MPI_FLOAT MPI_DOUBLE

C datatype
signed char signed short int signed int signed long int unsigned char unsigned short int unsigned int unsigned long int float double

MPI_LONG_DOUBLE

long double
19

" ", 2


MPI



MPI_Comm MPI_Status MPI_datatype

" ", 2

20









­ ­ . MPI_ANY_TAG. -MPI . MPI , MPI.

" ", 2

21


C:

MPI helloworld.c

#include #include int main(int argc, char **argv){ MPI_Init(&argc, &argv); printf("Hello, MPI world\n"); MPI_Finalize(); return 0; }

" ", 2

22


MPI-
C ( ): error = MPI_Xxxxx(parameter,...);
MPI_Xxxxx(paramet
er,...);

C++ (case sensitive):
error = MPI::Xxxxx(parameter,...); MPI::Xxxxx(parameter,...); ­ . :

MPI_COMM_WORLD

" ", 2

23



int MPI_Init(int *argc, char ***argv) , int MPI_Comm_size(MPI_Comm comm, int *size) int MPI_Comm_rank(MPI_Comm comm, int *rank) ( 0) int MPI_Finalize() int MPI_Abort (MPI_Comm_size(MPI_Comm comm, int*errorcode)

" ", 2

24


MPI


MPI_Init , C: int MPI_Init(int *argc, char ***argv)

http://www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Init.html

" ", 2

25


MPI-
MPI_SUCCESS

I int error; ...... error = MPI_Init(&argc, &argv)); If (error != MPI_SUCCESS) { fprintf (stderr, " MPI_Init error \n"); return 1; }

" ", 2

26


MPI_Comm_size





int MPI_Comm_size(MPI_Comm comm, int *size)

­
http://www-unix.mcs.anl.gov/mpi/www/www3/MPI_Comm_size.html

" ", 2

27


MPI_Comm_rank (process rank)




Process ID 0 (n-1), n ­ int MPI_Comm_rank(MPI_Comm comm, int *rank) ­

" ", 2

28


MPI-
MPI C:

int MPI_Finalize() int MPI_Abort (MPI_Comm_size(MPI_Comm comm, int*errorcode)

- MPI_Finalize, .
" ", 2 29


Hello, MPI world! (2)
#include #include "mpi.h" int main(int argc, char **argv){ int rank, size; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); printf("Hello, MPI world! I am %d of %d\n",rank,size); MPI_Finalize(); return 0; }
" ", 2 30


MPI-




mpicc ­o <_> <>.c <> : mpicc ­o hw helloworld.c mpirun ­np 128 hw

" ", 2

31


MPI-




mpicc ­o hw helloworld.c mpisubmit ­n 128 hw mpisubmit -help

" ", 2

32


mpisubmit -help

" ", 2

33




" ", 2

34


hello_world

" ", 2

35


«-»



,
" ", 2

36


«-»





" ", 2

37


MPI «-»

" ", 2

38


«»
1 4


communicator 5 2 destination 3 0 source




-(Source process) - (Destination process ) - -
" ", 2

39





"" , , , , .. Send: , , Receive: , ,

" ", 2

40


MPI () Send
: MPI_SEND (buf, count, datatype, dest, tag, comm)


(start, count, datatype).




(dest) (rank) (comm) . .
MPI_Send(buf, count, datatype, dest, tag, comm)
Address of Datatype of Message tag send buffer each item Number of items Rank of destination Communicator to send process
" ", 2

41


MPI () Receive
MPI_RECV(buf, count, datatype, source, tag, comm, status)


, source tag source ­ MPI_ANY_SOURCE. comm





status

MPI_Recv(buf, count, datatype, src, tag, comm, status)
Address of Datatype of receive buffer each item Maximum number Rank of source Communicator of items to receive process
" ", 2

Status Message tag after operation

42


MPI_Send
int MPI_Send(void *buf,int count, MPI_Datatype datatype,int dest, int tag, MPI_Comm comm)
buf count Datatype dest tag comm MPI datatype rank - , MPI-

: MPI_Send(data,500,MPI_FLOAT,6,33,MPI_COMM_WORLD)

http://www-unix.mcs.anl.gov/mpi/www/www3/MPI_Send.html

" ", 2

43


MPI_Recv
int MPI_Recv(void *buf,int count, MPI_Datatype datatype,int source, int tag, MPI_Comm comm, MPI_Status *status )
buf count Datatype source tag comm status MPI datatype rank - , MPI-,

: MPI_Recv(data,500,MPI_FLOAT,6,33,MPI_COMM_WORLD,&stat)

" ", 2

44


: MPI Send/Receive (1)
#include #include int main(int argc, char *argv[]){ int numtasks, rank, dest, source, rc, tag=1; char inmsg, outmsg='X'; MPI_Status Stat; MPI_Init (&argc,&argv); MPI_Comm_size (MPI_COMM_WORLD, &numtasks); MPI_Comm_rank (MPI_COMM_WORLD, &rank); if (rank == 0) { dest = 1; rc = MPI_Send (&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD); printf("Rank0 sent: %c\n", outmsg); source = 1; rc = MPI_Recv (&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat); }

" ", 2

45


: MPI Send/Receive (2)
else if (rank == 1) { source = 0; rc = MPI_Recv (&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat); printf("Rank1 received: %c\n", inmsg); dest = 0; rc = MPI_Send (&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD); } MPI_Finalize(); }

" ", 2

46


Wildcarding ()


MPI_ANY_SOURCE MPI_ANY_TAG
- status





" ", 2

47





MPI_Recv status
: Source: status.MPI_SOURCE Tag: status.MPI_TAG Count: MPI_Get_count



" ", 2

48






, MPI_Recv count ­

C:
int MPI_Get_count (MPI_Status *status, MPI_Datatype datatype, int *count)

" ", 2

49



int recvd_tag, recvd_from, recvd_count; MPI_Status status; MPI_Recv (..., MPI_ANY_SOURCE, MPI_ANY_TAG, ..., &status ) recvd_tag = status.MPI_TAG; recvd_from = status.MPI_SOURCE; MPI_Get_count( &status, datatype, &recvd_count );

" ", 2

50


«-»


rank rank -

" ", 2

51


MPI_Probe
int MPI_Probe(int source, int tag, MPI_Comm comm, MPI_Status* status)
. MPI_Recv

" ", 2

52



if (rank == 0) { // Send size of integers to process 1 MPI_Send(buf, size, MPI_INT, 1, 0, MPI_COMM_WORLD); printf("0 sent %d numbers to 1\n", size); } else if (rank == 1) { MPI_Status status; // Probe for an incoming message from process MPI_Probe (0, 0, MPI_COMM_WORLD, &status); MPI_Get_count (&status, MPI_INT, &size); int* number_buf = (int*)malloc(sizeof(int) * size); // Now receive the message with the allocated buffer MPI_Recv (number_buf, size, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); printf("1 dynamically received %d numbers from 0.\n", number_amount); free(number_buf); }

" ", 2

53