Skip to content

Instantly share code, notes, and snippets.

@nerdyworm
Created September 5, 2010 19:58
Show Gist options
  • Save nerdyworm/566274 to your computer and use it in GitHub Desktop.
Save nerdyworm/566274 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include "mpi.h"
main(int argc, char **argv )
{
char buffer[100];
int i, rank, size, type=99;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if(rank == 0) {
printf("Master: Hello slaves give me your messages\n");
for (i=1; i<size; i++) {
MPI_Send("hi msg plz", 10, MPI_CHAR, i, type, MPI_COMM_WORLD);
}
int message_count = 0;
while(message_count < size - 1) {
MPI_Recv(buffer, 100, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
message_count++;
printf("Message reveived from process: %s\n", buffer);
}
} else {
MPI_Recv(buffer, 10, MPI_CHAR, 0, type, MPI_COMM_WORLD, &status);
char hostname[80]; gethostname(&hostname, 80);
sprintf(buffer, "Hi, my name is %s and my rank is: %d", hostname, rank);
MPI_Send(buffer, 100, MPI_CHAR, 0, type, MPI_COMM_WORLD);
}
MPI_Finalize();
}
grid01:4
grid02:4
grid03:4
grid04:4
[brhodes6@coit-grid01 mpi_assign]$ mpiexec -machinefile machines -np 4 ./a.out
Master: Hello slaves give me your messages
Message reveived from process: Hi, my name is coit-grid01.uncc.edu and my rank is: 1
Message reveived from process: Hi, my name is coit-grid01.uncc.edu and my rank is: 2
Message reveived from process: Hi, my name is coit-grid01.uncc.edu and my rank is: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment