Created
November 26, 2020 16:48
-
-
Save PaluMacil/807dc7a6b22e599ba26c60a3a63bf8e5 to your computer and use it in GitHub Desktop.
"round" is an OpenMPI ring ping to demonstrate a message getting passed between each node and modified one at a time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <mpi.h> | |
#define MASTER 0 | |
#define TAG 0 | |
#define MSGSIZE 100000 | |
#define MAX 25 | |
int main(int argc, char* argv[]) | |
{ | |
int my_rank, num_nodes, last_node; | |
char my_host[MAX]; | |
char message[MSGSIZE]; | |
MPI_Init(&argc, &argv); | |
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); | |
MPI_Comm_size(MPI_COMM_WORLD, &num_nodes); | |
last_node = num_nodes-1; | |
gethostname (my_host, MAX); | |
if (my_rank == MASTER) { | |
printf("there are %d nodes for this job\n", num_nodes); | |
sprintf(message, "this note has been seen by: %s(%d)", my_host, my_rank); | |
MPI_Send(message, strlen(message) + 1, MPI_CHAR, 1, TAG, MPI_COMM_WORLD); | |
// get "signed note" back from the last node | |
MPI_Recv(message, MSGSIZE, MPI_CHAR, last_node, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | |
printf("%s\n", message); | |
} else { | |
MPI_Recv(message, MSGSIZE, MPI_CHAR, my_rank-1, TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE); | |
sprintf(message, "%s, %s(%d)", message, my_host, my_rank); | |
if (my_rank < last_node) { | |
// this is a node (and not the last node) | |
MPI_Send(message, strlen(message) + 1, MPI_CHAR, my_rank+1, TAG, MPI_COMM_WORLD); | |
} else { | |
// this is the last node; send message to master | |
MPI_Send(message, strlen(message) + 1, MPI_CHAR, MASTER, TAG, MPI_COMM_WORLD); | |
} | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compile with
mpicc round.c -o round
. Run withmpirun -np 80 --hostfile ../my_hosts round
. You will need to add an--oversubscribe
flag or reduce the nodes if you don't have 80. The hostfile flag should point to an actual file with lines likemachine01 slots=8
where the first part of each line is a machine name and the second is the number of threads to use (typically you can use 2 threads per core, but you might want to leave room for other jobs). You should also make sure your user is able to run ssh commands on the other nodes via proper ssh configuration.