introduction to mpi · introduction to mpi preeti malakar ... message passing interface (mpi)...

51
Introduction to MPI Preeti Malakar [email protected] An Introductory Course on High-Performance Computing in Engineering 28 th September 2019 1

Upload: others

Post on 12-Jun-2020

54 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Introduction to MPI

Preeti Malakar

[email protected]

An Introductory Course on High-Performance Computing in Engineering

28th September 2019

1

Page 2: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Parallelism

4 hours due to limitations

60 hours

2

Total parallel time = 20 + 4 = 24, Speedup = 60/24May not always achieve ideal speedup

Instructor

TAs

Page 3: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

This Talk

3

• Shared memory

– OpenMP, Pthreads, …

• Distributed memory

– MPI, UPC, …

• Hybrid

– MPI + OpenMP

Page 4: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

System Model

Memory

• Interconnected systems• Distributed memory• NO centralized server/master

4

host1 host2 host3 host4

Core

Page 5: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Parallel Code – Getting started

Q: How should I write a parallel code to add up a million numbers using 4 processes (on 4 nodes)?

5

• Distribute the numbers to the 4 processes

• Collect the result back at one of the processes for further processing

Page 6: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Message Passing Interface (MPI)

• Standard for message passing in a distributed memory environment

• Efforts began in 1991 by Jack Dongarra, Tony Hey, and David W. Walker

• MPI Forum

– Version 1.0: 1994

– Version 2.0: 1997

– Version 3.0: 2012

6

Page 7: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

MPI Implementations

• MPICH (ANL)

• MVAPICH (OSU)

• Intel MPI

• OpenMPI

7

Page 8: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Parallel Processes

Memory

8

host1 host2 host3 host4

Core

process process process process

Q: How do the processes communicate among each other?

Page 9: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Communication Channels

• Sockets for network communication

• MPI handles communications, progress etc.

Reference: Design and Evaluation of Nemesis, a Scalable, Low-Latency, Message-Passing Communication Subsystem by Buntinas et al. 9

Page 10: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Message Passing Paradigm

• Message sends and receives

• Explicit communication

Communication types

• Blocking

• Non-blocking

10

Page 11: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Getting Started

Function names: MPI_*

How many outputs?

Page 12: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Initialization and Finalization

MPI_Init

• gather information about the parallel job

• set up internal library state

• prepare for communication

MPI_Finalize

• cleanup

12

Page 13: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

MPI_COMM_WORLD

13

Page 14: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

MPI_COMM_WORLD

2

4

3

1

0

14

Process identified by rank/id

Page 15: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Communication Scope

15

Communicator (communication handle)

• Defines the scope

• Specifies communication context

Process • Belongs to a group• Identified by a rank within a group

Identification• MPI_Comm_size – total number of processes in communicator• MPI_Comm_rank – rank in the communicator

Page 16: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Getting Started

Rank of a process

Total number of processes

Page 17: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Executing MPI codes

mpicc –o executable program.c

mpirun –np 4 ./executable

17

Page 18: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

MPI Message

• Data and header/envelope

• Typically, MPI communications send/receive messages

18

Source: Origin of message

Destination: Receiver of message

Communicator

Tag (0:MPI_TAG_UB)

Message Envelope

Page 19: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

MPI Communication Types

Point-to-point Collective

Page 20: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Point-to-point Communication

• MPI_Send

• MPI_Recv

SENDER

RECEIVER

int MPI_Send (const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)

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

Tags should match

Blocking send and receive

Page 21: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

MPI_Datatype

• MPI_BYTE

• MPI_CHAR

• MPI_INT

• MPI_FLOAT

• MPI_DOUBLE

21

Page 22: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Example 1

22

MPI_Comm_rank (MPI_COMM_WORLD, &myrank);

// Sender processif (myrank == 0) /* code for process 0 */{strcpy (message,"Hello, there");MPI_Send (message, strlen(message)+1, MPI_CHAR, 1, 99,

MPI_COMM_WORLD);}

// Receiver processelse if (myrank == 1) /* code for process 1 */{MPI_Recv (message, 20, MPI_CHAR, 0, 99, MPI_COMM_WORLD, &status);printf ("received :%s\n", message);

}

Message tag

Message tag

Page 23: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

MPI_Status

• Source rank

• Message tag

• Message length

– MPI_Get_count

23

Page 24: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

MPI_ANY_*

• MPI_ANY_SOURCE

– Receiver may specify wildcard value for source

• MPI_ANY_TAG

– Receiver may specify wildcard value for tag

24

Page 25: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Example 2

25

MPI_Comm_rank (MPI_COMM_WORLD, &myrank);

// Sender processif (myrank == 0 || myrank == 2) /* process 0 and 2 */{sprintf (message,"Hello, there from %d", myrank);MPI_Send (message, strlen(message)+1, MPI_CHAR, 1, 99,

MPI_COMM_WORLD);}

// Receiver processelse if (myrank == 1) /* process 1 */{MPI_Recv (message, 40, MPI_CHAR, MPI_ANY_SOURCE, 99,

MPI_COMM_WORLD, &status);printf ("received :%s\n", message);

}

Bug?

Page 26: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Example 2 (correct)

26

MPI_Comm_rank (MPI_COMM_WORLD, &myrank);

// Sender processif (myrank == 0 || myrank == 2) /* process 0 and 2 */{sprintf (message,"Hello, there from %d", myrank);MPI_Send (message, strlen(message)+1, MPI_CHAR, 1, 99,

MPI_COMM_WORLD);}

// Receiver processelse if (myrank == 1) /* process 1 */{MPI_Recv (message, 20, MPI_CHAR, MPI_ANY_SOURCE, 99,

MPI_COMM_WORLD, &status);MPI_Recv (message, 20, MPI_CHAR, MPI_ANY_SOURCE, 99,

MPI_COMM_WORLD, &status);}

Page 27: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

MPI_Send (Blocking)

• Does not return until buffer can be reused

• Message buffering

• Implementation-dependent

• Standard communication mode

SENDER

RECEIVER

27

Page 28: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Buffering

[Source: Cray presentation] 28

Page 29: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Message Protocols

• Short

– Message sent with envelope/header

• Eager

– Send completes without acknowledgement from destination

– Small messages – typically 128 KB (at least in MPICH)

– MPIR_CVAR_CH3_EAGER_MAX_MSG_SIZE (check mpivars)

• Rendezvous

– Requires an acknowledgement from a matching receive

– Large messages

29

Page 30: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Other Send Modes

• MPI_Bsend Buffered

– May complete before matching receive is posted

• MPI_Ssend Synchronous

– Completes only if a matching receive is posted

• MPI_Rsend Ready

– Started only if a matching receive is posted

30

Page 31: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Non-blocking Point-to-Point

• MPI_Isend (buf, count, datatype, dest, tag, comm, request)

• MPI_Irecv (buf, count, datatype, source, tag, comm, request)

• MPI_Wait (request, status)

31

0 1

MPI_Isend

MPI_Recv

MPI_Isend

MPI_RecvSafe

Page 32: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Computation Communication Overlap

32

0 1

MPI_Isend

MPI_Recv

MPI_Wait

compute

compute

compute

compute

compute

Time

Page 33: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Collective Communications

• Must be called by all processes that are part of the communicator

Types

• Synchronization (MPI_Barrier)

• Global communication (MPI_Bcast, MPI_Gather, …)

• Global reduction (MPI_Reduce, …)

33

Page 34: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Barrier

• Synchronization across all group members

• Collective call

• Blocks until all processes have entered the call

• MPI_Barrier (comm)

34

Page 35: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Broadcast

• Root process sends message to all processes

• Any process can be root process but has to be the same in all processes

• int MPI_Bcast (buffer, count, datatype, root, comm)

• Number of elements in buffer – count

Q: Can you use point-to-point communication

for the same?

35

X X

X

X

X

X

X

X

X

Page 36: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Example 3

36

int rank, size, color;MPI_Status status;

MPI_Init (&argc, &argv);MPI_Comm_rank (MPI_COMM_WORLD, &rank);MPI_Comm_size (MPI_COMM_WORLD, &size);

color = rank + 2;int oldcolor = color;MPI_Bcast (&color, 1, MPI_INT, 0, MPI_COMM_WORLD);

printf ("%d: %d color changed to %d\n", rank, oldcolor, color);

0: 2 color changed to 21: 3 color changed to 22: 4 color changed to 2

Page 37: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Gather

• Gathers values from all processes to a root process

• int MPI_Gather (sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm)

• Arguments recv* not relevant on non-root processes

37

A0 A1 A2

A0

A1

A2

DATA

PR

OC

ESSE

S

Page 38: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Example 4

38

MPI_Comm_rank (MPI_COMM_WORLD, &rank);MPI_Comm_size (MPI_COMM_WORLD, &size);

color = rank + 2;

int colors[size];MPI_Gather (&color, 1, MPI_INT, colors, 1, MPI_INT, 0,

MPI_COMM_WORLD);

if (rank == 0)for (i=0; i<size; i++)printf ("color from %d = %d\n", i, colors[i]);

Page 39: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Scatter

• Scatters values to all processes from a root process

• int MPI_Scatter (sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm)

• Arguments send* not relevant on non-root processes

• Output parameter – recvbuf

39

A0 A1 A2

A0

A1

A2

DATA

PR

OC

ESSE

S

Page 40: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Allgather

• All processes gather values from all processes

• int MPI_Allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm)

40

A0 A1 A2

A0 A1 A2

A0 A1 A2

A0

A1

A2

DATA

PR

OC

ESSE

S

Page 41: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Reduce

• MPI_Reduce (inbuf, outbuf, count, datatype, op, root, comm)

• Combines element in inbuf of each process

• Combined value in outbuf of root

• op: MIN, MAX, SUM, PROD, …

41

0 1 2 3 4 5 6 2 1 2 3 2 5 2 0 1 1 0 1 1 0

2 1 2 3 4 5 6 MAX at root

Page 42: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

MPI_Reduce Example

4 hours due to limitations

60 hours

42

Instructor

TAs

int local_marks[number_of_TAs];

// compute local marks in parallel

MPI_Reduce (&local_marks, &total_marks, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD);

Page 43: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Allreduce

• MPI_Allreduce (inbuf, outbuf, count, datatype, op, comm)

• op: MIN, MAX, SUM, PROD, …

• Combines element in inbuf of each process

• Combined value in outbuf of each process

43

0 1 2 3 4 5 6 2 1 2 3 2 5 2 0 1 1 0 1 1 0

2 1 2 3 4 5 6

MAX

2 1 2 3 4 5 62 1 2 3 4 5 6

Page 44: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Sub-communicator

- Logical subset- Different contexts

44

Page 45: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

MPI_COMM_SPLIT

MPI_Comm_split (MPI_Comm oldcomm, intcolor, int key, MPI_Comm *newcomm)

• Collective call

• Logically divides based on color

– Same color processes form a group

– Some processes may not be part of newcomm(MPI_UNDEFINED)

• Rank assignment based on key

45

Page 46: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Logical subsets of processes

1

6

3

2

8

12

510

11

15

14

7

1316

17

19

4

0

18

9

How do you assign one color to odd processes and another color to even processes ?color = rank % 2

0 02 14 2…

1 03 15 2…

46

Page 47: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

How to run an MPI program on a cluster?

MPI process MPI process MPI process MPI process

47

MPI process MPI process MPI process MPI process

4 nodes, ppn=2

mpiexec –n <number of processes> -f <hostfile> ./exe

<hostfile>

host1:2host2:2host3:2…

Page 48: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

How to run an MPI program on a managed cluster/supercomputer?

MPI process MPI process MPI process MPI process

48

MPI process MPI process MPI process MPI process

4 nodes, ppn=2

Execution on HPC2010: qsub sub.sh

Page 49: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

How to run an MPI program on your lab cluster?

49

Install MPICH or MVAPICH (open source)

Where to install?- Shared file system- Mount on other systems

<hostfile>

host1:2host2:2host3:2…

Page 50: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Reference Material

• Marc Snir, Steve W. Otto, Steven Huss-Lederman, David W. Walker and Jack Dongarra, MPI - The Complete Reference, Second Edition, Volume 1, The MPI Core.

• William Gropp, Ewing Lusk, Anthony Skjellum, Using MPI : portable parallel programming with the message-passing interface, 3rd Ed., Cambridge MIT Press, 2014.

50

Page 51: Introduction to MPI · Introduction to MPI Preeti Malakar ... Message Passing Interface (MPI) •Standard for message passing in a distributed memory environment •Efforts began

Thank [email protected]

51