Skip to content

Instantly share code, notes, and snippets.

@Gaurav-Singh-97
Created September 7, 2017 21:46
Show Gist options
  • Save Gaurav-Singh-97/eaa87829ceeae86b001182669cc1518c to your computer and use it in GitHub Desktop.
Save Gaurav-Singh-97/eaa87829ceeae86b001182669cc1518c to your computer and use it in GitHub Desktop.
MPI program in C to find sum of an array of 8 elements using 3-d cube topology
#include<stdio.h>
#include<mpi.h>
int main(int argc, char **argv)
{
int rank=0, n, src, dest;
MPI_Init(&argc, &argv);
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &n);
int tag = 100;
int receive;
int array[8] = {10, 20, 30, 40, 50, 60, 70, 80};
if (rank&1)
MPI_Send(&array[rank], 1, MPI_INT, rank-1, tag, MPI_COMM_WORLD);
else
{
MPI_Recv(&receive, 1, MPI_INT, rank+1, tag, MPI_COMM_WORLD, &status);
array[rank] += receive;
if (rank&2)
MPI_Send(&array[rank], 1, MPI_INT, rank-2, tag, MPI_COMM_WORLD);
else
{
MPI_Recv(&receive, 1, MPI_INT, rank+2, tag, MPI_COMM_WORLD, &status);
array[rank] += receive;
if (rank&4)
MPI_Send(&array[rank], 1, MPI_INT, rank-4, tag, MPI_COMM_WORLD);
else
{
MPI_Recv(&receive, 1, MPI_INT, rank+4, tag, MPI_COMM_WORLD, &status);
array[rank] += receive;
printf("\n%d \n", array[0]);
}
}
}
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment