Title: COMP309/509 - Lecture 20 class: middle, center, inverse
man mpirunHelloworld in MPIOpen MPI reference: http://www.open-mpi.org
See the online manual page: man mpirun
The MPI Standard: http://www.mpi-forum.org
Helloworld in MPI
#include <stdio.h>
#include "mpi.h"
int main(int argc, char** argv) {
MPI_Init(&argc, &argv);
printf("\tHello World!\n");
MPI_Finalize();
return 0;
}
Helloworld in MPI# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
mpicc:Examples> mpicc -Wall helloworld.c -o helloworld
Examples>
Run using mpirun:
Examples> mpirun -np 1 helloworld
Hello World!
Examples> mpirun -np 2 helloworld
Hello World!
Hello World!
Examples>
Helloworld in MPIExamples> mpiexec -np 2 helloworld
Hello World!
Hello World!
Examples>
Helloworld in MPI
all: helloworld whoAmI whereAmI greetings ring
helloworld: helloworld.c
mpicc -Wall helloworld.c -o helloworld
whereAmI: whereAmI.c
mpicc -Wall whereAmI.c -o whereAmI
whoAmI: whoAmI.c
mpicc -Wall whoAmI.c -o whoAmI
greetings: greetings.c
mpicc -Wall greetings.c -o greetings
ring: ring.c
mpicc -Wall ring.c -o ring
clean:
rm -f helloworld whereAmI whoAmI greetings ring
forks, spawns, creates or whatever.MPI_Init – initializingMPI_Comm_Size – number of peersMPI_Comm_Rank – rank amongst peersMPI_Send – sendingMPI_Recv – receivingMPI_Finalize – exiting#include "mpi.h"
int MPI_Init(int *argc, char ***argv)
MPI_Init(&argc, &argv);
MPI_SUCCESS
MPI_SUCCESS indicates success.#include "mpi.h"
int MPI_Finalize()
MPI_INIT or after an MPI_FINALIZE.MPI_Finalize.sizerankMPI_Comm_size.size - 1.whoami.c
#include <stdio.h>
#include "mpi.h"
int main(int argc, char** argv) {
int me, nproc;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &me);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
printf("I'm clone %d out of %d total!\n", me, nproc);
MPI_Finalize();
return 0;
}
Examples> mpirun -np 1 whoAmI
I'm clone 0 out of 1 total!
Examples> mpirun -np 10 whoAmI
I'm clone 0 out of 10 total!
I'm clone 8 out of 10 total!
I'm clone 5 out of 10 total!
I'm clone 6 out of 10 total!
I'm clone 9 out of 10 total!
I'm clone 4 out of 10 total!
I'm clone 2 out of 10 total!
I'm clone 1 out of 10 total!
I'm clone 7 out of 10 total!
I'm clone 3 out of 10 total!
Examples>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include "mpi.h"
int main(int argc, char** argv) {
int me, nproc;
char hostname[PATH_MAX];
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &me);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
if(gethostname(hostname, PATH_MAX) == 0){
printf("I'm clone %d out of %d total located @ %s\n",
me, nproc, hostname);
} else {
printf("I'm a lost clone %d out of %d total!\n",
me, nproc);
}
MPI_Finalize();
return 0;
}
$ cat setup-b1to8
b1
b2
b3
b4
b5
b6
b7
b8
$
$ mpirun -np 5 --map-by node --hostfile setup-b1to8 whereAmI
I'm clone 0 out of 5 total located @ b1
I'm clone 2 out of 5 total located @ b3
I'm clone 4 out of 5 total located @ b5
I'm clone 1 out of 5 total located @ b2
I'm clone 3 out of 5 total located @ b4
$
#include "mpi.h"
int MPI_Comm_rank ( MPI_Comm comm, int *rank )
comm, placed in the location rank.size - 1.MPI_COMM_WORLD as the first argument, for a while.<num> used in the mpirun -np <num> <prog>).
Typical call:MPI_Comm_rank(MPI_COMM_WORLD,&me);
#include "mpi.h"
int MPI_Comm_size ( MPI_Comm comm, int *size )
MPI_COMM_WORLD as the first argument, for a while.MPI_Comm_size(MPI_COMM_WORLD,&nproc);
#include <stdio.h>
#include <string.h>
#include "mpi.h"
int main(int argc, char** argv) {
int my_rank, nproc, src, dst, tag = 50;
char msg[100];
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
if (my_rank != 0) {
sprintf(msg, "Greetings from process %d!",
my_rank);
dst = 0;
MPI_Send(msg,strlen(msg)+1,MPI_CHAR,dst,tag,MPI_COMM_WORLD);
} else {
for (src = 1; src < nproc; src++) {
MPI_Recv(msg,100,MPI_CHAR,src,tag,MPI_COMM_WORLD,&status);
printf("%s\n", msg);
}
}
MPI_Finalize();
return 0;
}
$ mpirun -np 2 --host b2 whoAmI : -np 3 --host b3 whereAmI
I'm clone 1 out of 5 total!
I'm clone 0 out of 5 total!
I'm clone 2 out of 5 total located @ b3
I'm clone 4 out of 5 total located @ b3
I'm clone 3 out of 5 total located @ b3
MPI_COMM_WORLD#include "mpi.h"
int MPI_Send(void *buf,
int count,
MPI_Datatype datatype,
int dest,
int tag,
MPI_Comm comm )
buf is the address of send buffer.count is the number of elements in send buffer.datatype is the datatype of each send buffer elementdest is the rank of destinationtag is the message tag.comm is the communicator.man mpirun for more details on this.#include "mpi.h"
int MPI_Recv(void *buf,
int count,
MPI_Datatype datatype,
int source,
int tag,
MPI_Comm comm,
MPI_Status *status )
count argument indicates the maximum length of a message.MPI_Get_count.buf is the address of receive buffer.status is where the status object will be placed.datatype is the datatype of each buffer element.source is the rank of the source.tag is the message tag.comm is the communicator.| MPI Type | Description |
|---|---|
| MPI_CHAR | corrresponds to C’s signed char |
| MPI_SHORT | corrresponds to C’s signed short |
| MPI_INT | corrresponds to C’s signed int |
| MPI_LONG | corrresponds to C’s signed long int |
| MPI_FLOAT | corrresponds to C’s float |
| MPI_DOUBLE | corrresponds to C’s double |
MPI_BYTE and MPI_PACKED.#include "mpi.h"
int MPI_Get_count(MPI_Status *status,
MPI_Datatype datatype,
int *count)
count.datatype is zero, this routine will return a count of zero.status is not an exact multiple of the size of datatype (so that count would not be an integer), a count of MPI_UNDEFINED is returned instead.*The Token Ring Revisited
#include <stdio.h>
#include "mpi.h"
#define TOKTAG 5
#define VERBOSE 1
int main(int argc, char** argv) {
int me, nproc, src, dst, token;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &me);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
src = (me == nproc - 1) ? 0 : me + 1;
dst = (me == 0) ? nproc - 1 : me - 1;
....
....
if (me == 0) {
token = 0;
fprintf(stderr, "Token = %i sent\n", token);
MPI_Send(&token, 1, MPI_INT, dst, TOKTAG, MPI_COMM_WORLD);
MPI_Recv(&token, 1, MPI_INT, src, TOKTAG, MPI_COMM_WORLD, &status);
fprintf(stderr, "Token = %i arrived back\n", token);
} else {
MPI_Recv(&token, 1, MPI_INT, src, TOKTAG, MPI_COMM_WORLD, &status);
if(VERBOSE)fprintf(stderr, "Node %d passing token = %i\n", me, token);
token++;
MPI_Send(&token, 1, MPI_INT, dst, TOKTAG, MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
class: middle, center, inverse
Helloworld in MPIman mpirun