Message Passing Programming with MPI Message Passing Programming - - PowerPoint PPT Presentation

message passing programming with mpi
SMART_READER_LITE
LIVE PREVIEW

Message Passing Programming with MPI Message Passing Programming - - PowerPoint PPT Presentation

Message Passing Programming with MPI Message Passing Programming with MPI 1 What is MPI? Message Passing Programming with MPI 2 MPI Forum First message-passing interface standard. Sixty people from forty different organisations.


slide-1
SLIDE 1

Message Passing Programming with MPI 1

Message Passing Programming with MPI

slide-2
SLIDE 2

Message Passing Programming with MPI 2

What is MPI?

slide-3
SLIDE 3

Message Passing Programming with MPI 3

MPI Forum

First message-passing interface standard.

Sixty people from forty different organisations.

Users and vendors represented, from the US and Europe.

Two-year process of proposals, meetings and review.

Message Passing Interface document produced.

slide-4
SLIDE 4

Message Passing Programming with MPI 4

Goals and Scope of MPI

MPI’s prime goals are:

To provide source-code portability. To allow efficient implementation.

It also offers:

A great deal of functionality. Support for heterogeneous parallel architectures.

slide-5
SLIDE 5

Message Passing Programming with MPI 5

Header files

C: #include <mpi.h>

Fortran: include ‘mpif.h’

slide-6
SLIDE 6

Message Passing Programming with MPI 6

MPI Function Format

C: error = MPI_Xxxxx(parameter, ...); MPI_Xxxxx(parameter, ...);

Fortran: CALL MPI_XXXXX(parameter, ..., IERROR)

slide-7
SLIDE 7

Message Passing Programming with MPI 7

Handles

MPI controls its own internal data structures.

MPI releases `handles’ to allow programmers to refer to these.

C handles are of defined typedefs.

Fortran handles are INTEGERs.

slide-8
SLIDE 8

Message Passing Programming with MPI 8

Initialising MPI

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

Fortran: MPI_INIT(IERROR) INTEGER IERROR

Must be the first MPI procedure called.

slide-9
SLIDE 9

Message Passing Programming with MPI 9

MPI_COMM_WORLD

Communicators 1 3 2 4 5 6

MPI_COMM_WORLD

slide-10
SLIDE 10

Message Passing Programming with MPI 10

Rank

How do you identify different processes in a communicator? MPI_Comm_rank(MPI_Comm comm, int *rank) MPI_COMM_RANK(COMM, RANK, IERROR) INTEGER COMM, RANK, IERROR

The rank is not the PE number.

slide-11
SLIDE 11

Message Passing Programming with MPI 11

Size

How many processes are contained within a communicator? MPI_Comm_size(MPI_Comm comm, int *size) MPI_COMM_SIZE(COMM, SIZE, IERROR) INTEGER COMM, SIZE, IERROR

slide-12
SLIDE 12

Message Passing Programming with MPI 12

Exiting MPI

C: int MPI_Finalize()

Fortran: MPI_FINALIZE(IERROR) INTEGER IERROR

Must be the last MPI procedure called.

slide-13
SLIDE 13

Message Passing Programming with MPI 13

Exercise: Hello World

The minimal MPI program ❑

Write a minimal MPI program which prints ``hello world’’.

Compile it.

Run it on a single processor.

Run it on several processors in parallel.

Modify your program so that only the process ranked 0 in MPI_COMM_WORLD prints out.

Modify your program so that the number of processes is printed out.

slide-14
SLIDE 14

Message Passing Programming with MPI 14

Messages

slide-15
SLIDE 15

Message Passing Programming with MPI 15

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.

slide-16
SLIDE 16

Message Passing Programming with MPI 16

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 MPI_PACKED

slide-17
SLIDE 17

Message Passing Programming with MPI 17

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 MPI_PACKED

slide-18
SLIDE 18

Message Passing Programming with MPI 18

Point-to-Point Communication

slide-19
SLIDE 19

Message Passing Programming with MPI 19

Point-to-Point Communication

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.

4 2 3 5 1 communicator source dest

slide-20
SLIDE 20

Message Passing Programming with MPI 20

Communication modes

Sender mode Notes

Synchronous send Only completes when the receive has completed. Buffered send Always completes (unless an error

  • ccurs), irrespective of receiver.

Standard send Either synchronous or buffered. Ready send Always completes (unless an error

  • ccurs), irrespective of whether the

receive has completed. Receive Completes when a message has arrived.

slide-21
SLIDE 21

Message Passing Programming with MPI 21

MPI Sender Modes

OPERATION MPI CALL Standard send MPI_SEND Synchronous send MPI_SSEND Buffered send MPI_BSEND Ready send MPI_RSEND Receive MPI_RECV

slide-22
SLIDE 22

Message Passing Programming with MPI 22

Sending a message

C: int MPI_Ssend(void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)

Fortran: MPI_SSEND(BUF, COUNT, DATATYPE, DEST, TAG,COMM, IERROR) <type> BUF(*) INTEGER COUNT, DATATYPE, DEST, TAG INTEGER COMM, IERROR

slide-23
SLIDE 23

Message Passing Programming with MPI 23

Receiving a message

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

Fortran: MPI_RECV(BUF, COUNT, DATATYPE, SOURCE, TAG, COMM, STATUS, IERROR) <type> BUF(*) INTEGER COUNT, DATATYPE, SOURCE, TAG, COMM, STATUS(MPI_STATUS_SIZE),IERROR

slide-24
SLIDE 24

Message Passing Programming with MPI 24

Synchronous Blocking Message-Passing

Processes synchronise.

Sender process specifies the synchronous mode.

Blocking – both processes wait until the transaction has completed.

slide-25
SLIDE 25

Message Passing Programming with MPI 25

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.

slide-26
SLIDE 26

Message Passing Programming with MPI 26

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.

slide-27
SLIDE 27

Message Passing Programming with MPI 27

Communication Envelope

Destination Address For the attention of : Data

Item 1 Item 2 Item 3

Sender’s Address

slide-28
SLIDE 28

Message Passing Programming with MPI 28

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 or status(MPI_TAG) Count: MPI_Get_count or MPI_GET_COUNT

slide-29
SLIDE 29

Message Passing Programming with MPI 29

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

slide-30
SLIDE 30

Message Passing Programming with MPI 30

Message Order Preservation

Messages do not overtake each other.

This is true even for non-synchronous sends.

4 2 3 5 1 communicator

slide-31
SLIDE 31

Message Passing Programming with MPI 31

Exercise - Ping pong

Write a program in which two processes repeatedly pass a message back and forth.

Insert timing calls to measure the time taken for one message.

Investigate how the time taken varies with the size of the message.

slide-32
SLIDE 32

Message Passing Programming with MPI 32

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.

Modify your program to measure its execution time and print it out.

slide-33
SLIDE 33

Message Passing Programming with MPI 33

Non-Blocking Communications

slide-34
SLIDE 34

Message Passing Programming with MPI 34

Deadlock

4 2 3 5 1 communicator

slide-35
SLIDE 35

Message Passing Programming with MPI 35

Non-Blocking Communications

Separate communication into three phases:

Initiate non-blocking communication.

Do some work (perhaps involving other communications?)

Wait for non-blocking communication to complete.

slide-36
SLIDE 36

Message Passing Programming with MPI 36

Non-Blocking Send

4 2 3 5 1

  • ut

in

communicator

slide-37
SLIDE 37

Message Passing Programming with MPI 37

Non-Blocking Receive

4 2 3 5 1

  • ut

in

communicator

slide-38
SLIDE 38

Message Passing Programming with MPI 38

Handles used for Non-blocking Comms ❑

datatype – same as for blocking (MPI_Datatype or INTEGER).

communicator – same as for blocking (MPI_Comm or INTEGER).

request – MPI_Request or INTEGER.

A request handle is allocated when a communication is initiated.

slide-39
SLIDE 39

Message Passing Programming with MPI 39

Non-blocking Synchronous Send ❑

C: int MPI_Issend(void* buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request *request) int MPI_Wait(MPI_Request *request, MPI_Status *status)

Fortran: MPI_ISSEND(buf, count, datatype, dest, tag, comm, request, ierror) MPI_WAIT(request, status, ierror)

slide-40
SLIDE 40

Message Passing Programming with MPI 40

Non-blocking Receive

C: int MPI_Irecv(void* buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, MPI_Request *request) int MPI_Wait(MPI_Request *request, MPI_Status *status)

Fortran: MPI_IRECV(buf, count, datatype, src, tag,comm, request, ierror) MPI_WAIT(request, status, ierror)

slide-41
SLIDE 41

Message Passing Programming with MPI 41

Blocking and Non-Blocking

Send and receive can be blocking or non-blocking.

A blocking send can be used with a non-blocking receive, and vice-versa.

Non-blocking sends can use any mode - synchronous, buffered, standard, or ready.

Synchronous mode affects completion, not initiation.

slide-42
SLIDE 42

Message Passing Programming with MPI 42

Communication Modes

NON-BLOCKING OPERATION MPI CALL Standard send MPI_ISEND Synchronous send MPI_ISSEND Buffered send MPI_IBSEND Ready send MPI_IRSEND Receive MPI_IRECV

slide-43
SLIDE 43

Message Passing Programming with MPI 43

Completion

Waiting versus Testing.

C: int MPI_Wait(MPI_Request *request, MPI_Status *status) int MPI_Test(MPI_Request *request, int *flag, MPI_Status *status)

Fortran: MPI_WAIT(handle, status, ierror) MPI_TEST(handle, flag, status, ierror)

slide-44
SLIDE 44

Message Passing Programming with MPI 44

Multiple Communications

Test or wait for completion of one message.

Test or wait for completion of all messages.

Test or wait for completion of as many messages as possible.

slide-45
SLIDE 45

Message Passing Programming with MPI 45

Testing Multiple Non-Blocking Comms

in in in

process

slide-46
SLIDE 46

Message Passing Programming with MPI 46

Exercise

Rotating information around a ring ❑

Arrange processes to communicate round a ring.

Each process stores a copy of its rank in an integer variable.

Each process communicates this value to its right neighbour, and receives a value from its left neighbour.

Each process computes the sum of all the values received.

Repeat for the number of processes involved and print out the sum stored at each process.

slide-47
SLIDE 47

Mesage Passing Programming with MPI 47

Derived Datatypes

slide-48
SLIDE 48

Mesage Passing Programming with MPI 48

MPI Datatypes

Basic types

Derived types

vectors structs

  • thers
slide-49
SLIDE 49

Mesage Passing Programming with MPI 49

Derived Datatypes - Type Maps

basic datatype 0 displacement of datatype 0 basic datatype 1 displacement of datatype 1 ... ... basic datatype n-1 displacement of datatype n-1

slide-50
SLIDE 50

Mesage Passing Programming with MPI 50

Contiguous Data

The simplest derived datatype consists of a number of contiguous items of the same datatype.

C: int MPI_Type_contiguous(int count, MPI_Datatype oldtype, MPI_Datatype *newtype)

Fortran: MPI_TYPE_CONTIGUOUS(COUNT, OLDTYPE, NEWTYPE, IERROR) INTEGER COUNT, OLDTYPE, NEWTYPE, IERROR

slide-51
SLIDE 51

Mesage Passing Programming with MPI 51

Vector Datatype Example

A 3X2 block of a 5X5 Fortran array ❑

count = 2

stride = 5

blocklength = 3

  • ldtype

newtype 5 element stride between blocks 3 elements per block 2 blocks

slide-52
SLIDE 52

Mesage Passing Programming with MPI 52

Constructing a Vector Datatype

C: int MPI_Type_vector (int count, int blocklength, int stride, MPI_Datatype oldtype, MPI_Datatype *newtype)

Fortran: MPI_TYPE_VECTOR (COUNT, BLOCKLENGTH, STRIDE, OLDTYPE, NEWTYPE, IERROR)

slide-53
SLIDE 53

Mesage Passing Programming with MPI 53

Extent of a Datatatype

C: int MPI_Type_extent (MPI_Datatype datatype, MPI_Aint *extent)

Fortran: MPI_TYPE_EXTENT( DATATYPE, EXTENT, IERROR) INTEGER DATATYPE, EXTENT, IERROR

slide-54
SLIDE 54

Mesage Passing Programming with MPI 54

Address of a Variable

C: int MPI_Address (void *location, MPI_Aint *address)

Fortran: MPI_ADDRESS( LOCATION, ADDRESS, IERROR) <type> LOCATION (*) INTEGER ADDRESS, IERROR

slide-55
SLIDE 55

Mesage Passing Programming with MPI 55

Struct Datatype Example

count = 2

array_of_blocklengths[0] = 1

array_of_types[0] = MPI_INT

array_of_blocklengths[1] = 3

array_of_types[1] = MPI_DOUBLE

newtype MPI_DOUBLE MPI_INT block 0 block 1 array_of_displacements[0] array_of_displacements[1]

slide-56
SLIDE 56

Mesage Passing Programming with MPI 56

Constructing a Struct Datatype

C: int MPI_Type_struct (int count, int *array_of_blocklengths, MPI_Aint *array_of_displacements, MPI_Datatype *array_of_types, MPI_Datatype *newtype)

Fortran: MPI_TYPE_STRUCT (COUNT, ARRAY_OF_BLOCKLENGTHS, ARRAY_OF_DISPLACEMENTS, ARRAY_OF_TYPES, NEWTYPE, IERROR)

slide-57
SLIDE 57

Mesage Passing Programming with MPI 57

Committing a datatype

Once a datatype has been constructed, it needs to be committed before it is used.

This is done using MPI_TYPE_COMMIT

C: int MPI_Type_commit (MPI_Datatype *datatype)

Fortran: MPI_TYPE_COMMIT (DATATYPE, IERROR) INTEGER DATATYPE, IERROR

slide-58
SLIDE 58

Mesage Passing Programming with MPI 58

Exercise

Derived Datatypes ❑

Modify the passing-around-a-ring exercise.

Calculate two separate sums:

rank integer sum, as before rank floating point sum

Use a struct datatype for this.

slide-59
SLIDE 59

Message Passing Programming with MPI 59

Virtual Topologies

slide-60
SLIDE 60

Message Passing Programming with MPI 60

Virtual Topologies

Convenient process naming.

Naming scheme to fit the communication pattern.

Simplifies writing of code.

Can allow MPI to optimise communications.

slide-61
SLIDE 61

Message Passing Programming with MPI 61

How to use a Virtual Topology

Creating a topology produces a new communicator.

MPI provides ``mapping functions’’.

Mapping functions compute processor ranks, based on the topology naming scheme.

slide-62
SLIDE 62

Message Passing Programming with MPI 62

Example

A 2-dimensional Cylinder

(0,0) 1 (0,1) 2 (0,2) 3 (0,3) 4 (1,0) 5 (1,1) 6 (1,2) 7 (1,3) 8 (2,0) 9 (2,1) 10 (2,2) (2,3) 11

slide-63
SLIDE 63

Message Passing Programming with MPI 63

Topology types

Cartesian topologies

each process is ‘‘connected’’ to its neighbours in a virtual grid. boundaries can be cyclic, or not. processes are identified by cartesian coordinates.

Graph topologies

general graphs not covered here

slide-64
SLIDE 64

Message Passing Programming with MPI 64

Creating a Cartesian Virtual Topology ❑

C: int MPI_Cart_create(MPI_Comm comm_old, int ndims, int *dims, int *periods, int reorder, MPI_Comm *comm_cart)

Fortran: MPI_CART_CREATE(COMM_OLD, NDIMS, DIMS, PERIODS, REORDER, COMM_CART, IERROR) INTEGER COMM_OLD, NDIMS, DIMS(*), COMM_CART, IERROR LOGICAL PERIODS(*), REORDER

slide-65
SLIDE 65

Message Passing Programming with MPI 65

Balanced Processor Distribution ❑

C: int MPI_Dims_create(int nnodes, int ndims, int *dims)

Fortran: MPI_DIMS_CREATE(NNODES, NDIMS, DIMS, IERROR) INTEGER NNODES, NDIMS, DIMS(*), IERROR

slide-66
SLIDE 66

Message Passing Programming with MPI 66

Example

Call tries to set dimensions as close to each other as possible.

Non zero values in dims sets the number of processors required in that direction.

WARNING:- make sure dims is set to 0 before the call! dims before the call function call dims

  • n return

(0, 0) MPI_DIMS_CREATE( 6, 2, dims) (3, 2) (0, 0) MPI_DIMS_CREATE( 7, 2, dims) (7, 1) (0, 3, 0) MPI_DIMS_CREATE( 6, 3, dims) (2, 3, 1) (0, 3, 0) MPI_DIMS_CREATE( 7, 3, dims) erroneous call

slide-67
SLIDE 67

Message Passing Programming with MPI 67

Cartesian Mapping Functions

Mapping process grid coordinates to ranks ❑

C: int MPI_Cart_rank(MPI_Comm comm, int *coords, int *rank)

Fortran: MPI_CART_RANK (COMM, COORDS, RANK, IERROR) INTEGER COMM, COORDS(*), RANK, IERROR

slide-68
SLIDE 68

Message Passing Programming with MPI 68

Cartesian Mapping Functions

Mapping ranks to process grid coordinates ❑

C: int MPI_Cart_coords(MPI_Comm comm, int rank, int maxdims, int *coords)

Fortran: MPI_CART_COORDS(COMM, RANK, MAXDIMS, COORDS, IERROR) INTEGER COMM, RANK, MAXDIMS, COORDS(*), IERROR

slide-69
SLIDE 69

Message Passing Programming with MPI 69

Cartesian Mapping Functions

Computing ranks of neighbouring processes ❑

C: int MPI_Cart_shift(MPI_Comm comm, int direction, int disp, int *rank_source, int *rank_dest)

Fortran: MPI_CART_SHIFT(COMM, DIRECTION, DISP, RANK_SOURCE, RANK_DEST, IERROR) INTEGER COMM, DIRECTION, DISP,RANK_SOURCE, RANK_DEST, IERROR

slide-70
SLIDE 70

Message Passing Programming with MPI 70

Cartesian Partitioning

Cut a grid up into `slices’.

A new communicator is produced for each slice.

Each slice can then perform its own collective communications.

MPI_Cart_sub and MPI_CART_SUB generate new communicators for the slices.

slide-71
SLIDE 71

Message Passing Programming with MPI 71

Partitioning with MPI_CART_SUB ❑

C: int MPI_Cart_sub (MPI_Comm comm, int *remain_dims, MPI_Comm *newcomm)

Fortran: MPI_CART_SUB (COMM, REMAIN_DIMS, NEWCOMM, IERROR) INTEGER COMM, NEWCOMM, IERROR LOGICAL REMAIN_DIMS(*)

slide-72
SLIDE 72

Message Passing Programming with MPI 72

Exercise

Rewrite the exercise passing numbers round the ring using a one-dimensional ring topology.

Rewrite the exercise in two dimensions, as a torus. Each row of the torus should compute its own separate result.

slide-73
SLIDE 73

Message Passing Programming with MPI 73

Collective Communications

slide-74
SLIDE 74

Message Passing Programming with MPI 74

Collective Communication

Communications involving a group of processes.

Called by all processes in a communicator.

Examples:

Barrier synchronisation. Broadcast, scatter, gather. Global sum, global maximum, etc.

slide-75
SLIDE 75

Message Passing Programming with MPI 75

Characteristics of Collective Comms ❑

Collective action over a communicator.

All processes must communicate.

Synchronisation may or may not occur.

All collective operations are blocking.

No tags.

Receive buffers must be exactly the right size.

slide-76
SLIDE 76

Message Passing Programming with MPI 76

Barrier Synchronisation

C: int MPI_Barrier (MPI_Comm comm)

Fortran: MPI_BARRIER (COMM, IERROR) INTEGER COMM, IERROR

slide-77
SLIDE 77

Message Passing Programming with MPI 77

Broadcast

C: int MPI_Bcast (void *buffer, int count, MPI_Datatype datatype, int root, MPI_Comm comm)

Fortran: MPI_BCAST (BUFFER, COUNT, DATATYPE, ROOT, COMM, IERROR) <type> BUFFER(*) INTEGER COUNT, DATATYPE, ROOT, COMM, IERROR

slide-78
SLIDE 78

Message Passing Programming with MPI 78

Scatter

A B C D E A B C D E A B C D E

slide-79
SLIDE 79

Message Passing Programming with MPI 79

Scatter

C: int MPI_Scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)

Fortran: MPI_SCATTER(SENDBUF, SENDCOUNT, SENDTYPE, RECVBUF, RECVCOUNT, RECVTYPE, ROOT, COMM, IERROR) <type> SENDBUF, RECVBUF INTEGER SENDCOUNT, SENDTYPE, RECVCOUNT INTEGER RECVTYPE, ROOT, COMM, IERROR

slide-80
SLIDE 80

Message Passing Programming with MPI 80

Gather

A B C D E A B C D E A B C D E

slide-81
SLIDE 81

Message Passing Programming with MPI 81

Gather

C: int MPI_Gather(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)

Fortran: MPI_GATHER(SENDBUF, SENDCOUNT, SENDTYPE, RECVBUF, RECVCOUNT, RECVTYPE, ROOT, COMM, IERROR) <type> SENDBUF, RECVBUF INTEGER SENDCOUNT, SENDTYPE, RECVCOUNT INTEGER RECVTYPE, ROOT, COMM, IERROR

slide-82
SLIDE 82

Message Passing Programming with MPI 82

Global Reduction Operations

Used to compute a result involving data distributed over a group of processes.

Examples:

global sum or product global maximum or minimum global user-defined operation

slide-83
SLIDE 83

Message Passing Programming with MPI 83

Predefined Reduction Operations

MPI Name Function MPI_MAX Maximum MPI_MIN Minimum MPI_SUM Sum MPI_PROD Product MPI_LAND Logical AND MPI_BAND Bitwise AND MPI_LOR Logical OR MPI_BOR Bitwise OR MPI_LXOR Logical exclusive OR MPI_BXOR Bitwise exclusive OR MPI_MAXLOC Maximum and location MPI_MINLOC Minimum and location

slide-84
SLIDE 84

Message Passing Programming with MPI 84

MPI_REDUCE

C: int MPI_Reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm)

Fortran: MPI_REDUCE(SENDBUF, RECVBUF, COUNT, DATATYPE, OP, ROOT, COMM, IERROR) <type> SENDBUF, RECVBUF INTEGER SENDCOUNT, SENDTYPE, RECVCOUNT INTEGER RECVTYPE, ROOT, COMM, IERROR

slide-85
SLIDE 85

Message Passing Programming with MPI 85

MPI_REDUCE

1 2 3 4 RANK

ROOT A B C D

MPI_REDUCE

Q R S T F G H E F K L I N J P M N N O A B C D Q R S T F G H E F K L I N J P M N N O

AoEoIoMoQ

slide-86
SLIDE 86

Message Passing Programming with MPI 86

Example of Global Reduction

Integer global sum ❑

C: MPI_Reduce(&x, &result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD)

Fortran: CALL MPI_REDUCE(x, result, 1, MPI_INTEGER, MPI_SUM, 0, MPI_COMM_WORLD, IERROR)

Sum of all the x values is placed in result.

The result is only placed there on processor 0.

slide-87
SLIDE 87

Message Passing Programming with MPI 87

User-Defined Reduction Operators ❑

Reducing using an arbitrary operator, ■

C - function of type MPI_User_function: void my_op (void *invec, void *inoutvec,int *len, MPI_Datatype *datatype)

Fortran - external subprogram of type SUBROUTINE MY_OP(INVEC(*),INOUTVEC(*), LEN, DATATYPE) <type> INVEC(LEN), INOUTVEC(LEN) INTEGER LEN, DATATYPE

slide-88
SLIDE 88

Message Passing Programming with MPI 88

Reduction Operator Functions

Operator function for ■ must act as: for (i = 1 to len) inoutvec(i) = inoutvec(i)

■ invec(i)

Operator ■ need not commute but must be associative.

slide-89
SLIDE 89

Message Passing Programming with MPI 89

Registering User-Defined Operator ❑

Operator handles have type MPI_Op or INTEGER

C: int MPI_Op_create(MPI_User_function *my_op, int commute, MPI_Op *op)

Fortran:

MPI_OP_CREATE (MY_OP, COMMUTE, MPI_OP, IERROR) EXTERNAL FUNC LOGICAL COMMUTE INTEGER OP, IERROR

slide-90
SLIDE 90

Message Passing Programming with MPI 90

Variants of MPI_REDUCE

MPI_ALLREDUCE – no root process

MPI_REDUCE_SCATTER – result is scattered

MPI_SCAN – ‘‘parallel prefix’’

slide-91
SLIDE 91

Message Passing Programming with MPI 91

MPI_ALLREDUCE

1 2 3 4 RANK

A B C D Q R S T F G H E F K L I N J P M N N O A B C D Q R S T F G H E F K L I N J P M N N O

MPI_ALLREDUCE AoEoIoMoQ

slide-92
SLIDE 92

Message Passing Programming with MPI 92

MPI_ALLREDUCE

Integer global sum ❑

C: int MPI_Allreduce(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)

Fortran: MPI_ALLREDUCE(SENDBUF, RECVBUF, COUNT, DATATYPE, OP, COMM, IERROR)

slide-93
SLIDE 93

Message Passing Programming with MPI 93

MPI_SCAN

1 2 3 4 RANK

A B C D Q R S T F G H E F K L I N J P M N N O A B C D Q R S T F G H E F K L I N J P M N N O

MPI_SCAN AoEoIoMoQ A AoE AoEoI AoEoIoM

slide-94
SLIDE 94

Message Passing Programming with MPI 94

MPI_SCAN

Integer partial sum ❑

C: int MPI_Scan(void* sendbuf, void* recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)

Fortran: MPI_SCAN(SENDBUF, RECVBUF, COUNT, DATATYPE, OP, COMM, IERROR)

slide-95
SLIDE 95

Message Passing Programming with MPI 95

Exercise

Rewrite the pass-around-the-ring program to use MPI global reduction to perform its global sums.

Then rewrite it so that each process computes a partial sum.

Then rewrite this so that each process prints out its partial result, in the correct order (process 0, then process 1, etc.).

slide-96
SLIDE 96

Message Passing Programming with MPI 96

Casestudy Towards Life

slide-97
SLIDE 97

Message Passing Programming with MPI 97

The Story so Far....

This course has:

Introduced the basic concepts/primitives in MPI. Allowed you to examine the standard in a comprehensive manner. Not all the standard has been covered but you should now be in a good position to do so yourself.

However the examples have been rather simple. This case study will:

Allow you to use all the techniques that you have learnt in one application. Teach you some basic aspects of domain decomposition: how you go about parallelising a code. ... other courses in EPCC do this in more detail ...

slide-98
SLIDE 98

Message Passing Programming with MPI 98

Overview

Three part case study that puts into practice all that you learnt in this course to build a real application

each part is self contained (having completed the previous part) later parts build on from earlier parts extra exercises extend material and are independent

If all parts completed you should end up with a fully working version of the Game of Life

Detailed description on how to do the casestudy in notes

start from scratch – some pseudo code provided

slide-99
SLIDE 99

Message Passing Programming with MPI 99

Part 1: Master–slave Model

Create a master–slave model

master outputs data to file (also does work!) perform a domain decomposition of large 2d array create a chess board pattern – processors colour local domains

  • utput pgm files – graphical result

Can view the result using xv

YSIZE XSIZE

slide-100
SLIDE 100

Message Passing Programming with MPI 100

Details

Basically what you want to do for this part is:-

Create a cartesian virtual topology Decompose a global array across processors Processor colours in its segment according to its position Create derived data type(s) to receive local arrays at the master processor – these arrays must be inserted at the correct location. May need to create derived data types at the slave processors to send data to the master processor.

slide-101
SLIDE 101

Message Passing Programming with MPI 101

Details cont.

All processors write their data back to the master processor Master processor writes data to file in pgm (portable graymap) format

view the results using xv to make sure it works

Try different numbers of processors to make sure the program works properly.

processor 0 processor 1 processor 2 processor 3 processor 0

slide-102
SLIDE 102

Message Passing Programming with MPI 102

Part 2: Boundary Swaps

Part 1 achieves the beginning of a decomposition.

Lots of applications require data located on the other processor, e.g. finite differences.

Instead of communicating each element of data as is needed all elements necessary are copied across. This is called a halo region.

Internal points can thus be calculated without further

  • communications. Here we will practice boundary swaps.

Processor 0 Processor 1

slide-103
SLIDE 103

Message Passing Programming with MPI 103

Outline Sketch

Create a halo region.

Perform halo swaps across processor domains.

Processor 0 Processor 3 Processor 2 Processor 1

slide-104
SLIDE 104

Message Passing Programming with MPI 104

Boundary Swaps

To achieve this – Cheat.

Update internal regions of processor domains only. Create derived data types to do the boundary swaps. Halo region should be exterior to data storage – artificial. Here we want to see the result of the boundary swaps hence the halo is contained inside the data region. This will have to be undone for the final part of this case study.

You will be able to visualise whether the boundary swaps are being done correctly.

Internal Region Boundary

slide-105
SLIDE 105

Message Passing Programming with MPI 105

The Result

Can see the result of performing the boundary swaps

Can make sure that the boundary swap are correct

The underlying mechanism used here can be used in any future codes you might write....

slide-106
SLIDE 106

Message Passing Programming with MPI 106

Part 3: The game of Life

Have all routines necessary to construct Game of Life.

Simple Cellular Automata in a 2d space. State of cells at the next time step determined from a simple set of rules:

dead if cell has less than two live neighbours – lonely maintain state if cell has exactly two live neighbours – content cell is born if the cell has exactly three live neighbours – ... ❤ die if the cell has more than three live neighbours – overcrowding

slide-107
SLIDE 107

Message Passing Programming with MPI 107

Procedure

Rewrite the code from part 2 so that the halo lies

  • utside the processor’s subdomain

Will need to write derived data types to transfer the internal regions, excluding the halo, of the processors to the master processor

Must devise a mapping from local processor coordinates to global coordinates

Allows global initial conditions to be output.

Global Data

(urx,ury) (llx,lly)

Local Data

(dx,dy) (1,1) Halo Region
slide-108
SLIDE 108

Message Passing Programming with MPI 108

Results

Output the state of the frame in pgm format at every iteration.

Can animate the result using xv:

xv -expand 10 -wait 0.5 -wloop -raw *.pgm

Steps 0, 5 and 10 in the evolution of a 128x128 simulation.

Good Luck.....!!

slide-109
SLIDE 109

Message Passing Programming with MPI 109

MPI on lomond

slide-110
SLIDE 110

Message Passing Programming with MPI 110

Compiling MPI Programs on lomond ❑

Fortran programmers use the Fortran 90 compiler

Must include MPI library: tmcc -o hello hello.c -lmpi tmf90 -o hello hello.f -lmpi

slide-111
SLIDE 111

Message Passing Programming with MPI 111

Running MPI Programs on lomond ❑

To interactively run the executable hello on two processors in the fe-int queue:

lomond$ bsub -I -q fe-int -n 2 pam ./hello

To run the executable hello on four processors in the 8-course queue:

lomond$ bsub -q 8-course -c 00:10 -n 4 pam ./hello

Use -o logfile to store the output in logfile

The pam MPI job starter software is mandatory for all queues.

The -c switch is mandatory in all queues except fe-int

slide-112
SLIDE 112

Message Passing Programming with MPI 112

Issues for Fortran Programmers

You should use the Fortran 90 compiler - this is the preferred option, but:

Use Fortran 90 features with care - MPI is a FORTRAN 77 library.

In particular:

Do not pass array sections - whole arrays only Do not use user defined data types

You may however use Fortran 90 free-format layout for source files.

slide-113
SLIDE 113

Message Passing Programming with MPI 113

Compiling MPI Programs on lomond ❑

Example MPI makefiles are shown in Appendix A of the course notes.

Similar to other makefiles.