Download presentation
Presentation is loading. Please wait.
Published byHendra Susanto Modified over 5 years ago
1
Hello, world in MPI #include <stdio.h> #include "mpi.h"
int main( int argc, char *argv[]) { int rank, size; MPI_Init( &argc, &argv ); MPI_Comm_size( MPI_COMM_WORLD, &size ); MPI_Comm_rank( MPI_COMM_WORLD, &rank ); printf( "Hello world from process %d of %d\n", rank, size ); MPI_Finalize(); return 0; }
2
MPI in nine routines (all you really need)
MPI_Init Initialize MPI_Finalize Finalize MPI_Comm_size How many processes? MPI_Comm_rank Which process am I? MPI_Wtime Timer MPI_Send Send data to one proc MPI_Recv Receive data from one proc MPI_Bcast Broadcast data to all procs MPI_Reduce Combine data from all procs
3
Ten more MPI routines (sometimes useful)
More group routines (like Bcast and Reduce): MPI_Alltoall, MPI_Alltoallv MPI_Scatter, MPI_Gather Non-blocking send and receive: MPI_Isend, MPI_Irecv MPI_Wait, MPI_Test, MPI_Probe, MPI_Iprobe Synchronization: MPI_Barrier
4
Example: Send an integer x from proc 0 to proc 1
MPI_Comm_rank(MPI_COMM_WORLD,&myrank); /* get rank */ int msgtag = 1; if (myrank == 0) { int x = 17; MPI_Send(&x, 1, MPI_INT, 1, msgtag, MPI_COMM_WORLD); } else if (myrank == 1) { int x; MPI_Recv(&x, 1, MPI_INT,0,msgtag,MPI_COMM_WORLD,&status); }
5
Some MPI Concepts Communicator
A set of processes that are allowed to communicate among themselves. Kind of like a “radio channel”. Default communicator: MPI_COMM_WORLD A library can use its own communicator, separated from that of a user program.
6
Some MPI Concepts Data Type What kind of data is being sent/recvd?
Mostly just names for C data types MPI_INT, MPI_CHAR, MPI_DOUBLE, etc.
7
Some MPI Concepts Message Tag Arbitrary (integer) label for a message
Tag of Send must match tag of Recv Useful for error checking & debugging
8
Parameters of blocking send
MPI_Send(buf, count, datatype, dest, tag, comm) Address of Datatype of Message tag send b uff er each item Number of items Rank of destination Comm unicator to send process
9
Parameters of blocking receive
MPI_Recv(buf, count, datatype, src, tag, comm, status) Status Address of Datatype of Message tag after oper ation receiv e b uff er each item Maxim um n umber Rank of source Comm unicator of items to receiv e process
10
Example: Send an integer x from proc 0 to proc 1
MPI_Comm_rank(MPI_COMM_WORLD,&myrank); /* get rank */ int msgtag = 1; if (myrank == 0) { int x; MPI_Send(&x, 1, MPI_INT, 1, msgtag, MPI_COMM_WORLD); } else if (myrank == 1) { MPI_Recv(&x, 1, MPI_INT,0,msgtag,MPI_COMM_WORLD,&status); }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.