Question: Below is a code for my mpi program in c. I just need help filling in where i commented, utilizing send and receive for the
Below is a code for my mpi program in c. I just need help filling in where i commented, utilizing send and receive for the 4 lines of code. Please use the code i show below and add in those 4 lines. I am not sure how to properly use these methods, although i know the rest of my code is executable.
#include
#include
#define N 1000
int main(int argc, char *argv[])
{
int myid, numprocs;
int data[N], i, x, low, high, myresult, result;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
if (myid ==0){
for (i=0; i < N; i++) data [i] = i + 1;
result = 0;
x = N/numprocs;
for (i = 1; i < numprocs; i++)
//Send group of numbers to processor 1,2, numprocs-1 respectively
myresult = 0;
for (i=0; i < x; i++) myresult += data[i];
printf("partial sum from processor %d of total %d processors is %d ", myid, numprocs, myresult);
result = myresult;
int receivedresult;
for (i = 1; i < numprocs; i++) {
//Receive partial sums from processor 1 to numprocs - 1
printf("Received %d from processor %d ", receivedresult, i);
result += receivedresult;
}
}
else {
x = N/numprocs;
int myitem[x];
int localsum;
//Receive group of numbers from processor 0
localsum = 0;
for (i = 0; i < x; i++) localsum += myitem[i];
printf("Partial sum from processor %d of total %d processors is: %d ", myid, numprocs, localsum);
//Send partial sums to processor 0
printf("Send %d to processor 0 by processor %d ", localsum, myid);
}
if (myid == 0) prinf("Total Sum is %d ", result);
MPI_Finalize();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
