Created
December 31, 2015 18:44
-
-
Save MinhasKamal/13b765f28fc94fcba02e to your computer and use it in GitHub Desktop.
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 <mpi.h> | |
void printHostName(); | |
int main(int argc, char **argv) { | |
int rank; | |
int number[] = {10, 20, 30, 40, 50, 60}; | |
int slave = 0; | |
int master = 0; | |
MPI_Init(&argc, &argv); | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
MPI_Status status; | |
if (rank==0) { //slave pc | |
int i; | |
for(i=0; i<3; i++){ | |
slave += number[i]; | |
} | |
printHostName(); | |
printf("\n %d \n", slave); | |
MPI_Send(&slave, 1, MPI_INT, 1, 123, MPI_COMM_WORLD); | |
} | |
else if(rank==1) { //master pc | |
int i; | |
for(i=3; i<6; i++){ | |
master += number[i]; | |
} | |
printHostName(); | |
printf("\n %d \n", master); | |
MPI_Recv(&slave, 1, MPI_INT, 0, 123, MPI_COMM_WORLD, &status); | |
master += slave; | |
printf("\n\n\n %d", master); | |
} | |
MPI_Finalize(); | |
return 0; | |
} | |
void printHostName(){ | |
char hostname[1024]; | |
gethostname(hostname, 1024); | |
puts(hostname); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment