Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is a code for my mpi program in c. I just need help filling in where i com

ID: 3601157 • Letter: B

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 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<stdio.h>

#include<mpi.h>

#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();

}

  

Explanation / Answer

Find below code, i have filled with send/recv code at four places where you have commented.

#include<stdio.h>

#include<mpi.h>

#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

MPI_Send( &data[i], numprocs, MPI_INT,i, send_data_tag, MPI_COMM_GROUP);

  

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

receivedresult = MPI_Recv( &result, numprocs-1, MPI_FLOAT,i, MPI_ANY_TAG, MPI_COMM_RECV, MPI_Status);

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];

MPI_Recv( &localsum, x-1, MPI_INT,i, MPI_ANY_TAG, MPI_COMM_RECV, MPI_Status);

printf("Partial sum from processor %d of total %d processors is: %d ", myid, numprocs, localsum);

//Send partial sums to processor 0

MPI_Send( &localsum, x, MPI_INT,i, send_data_tag, MPI_COMM_SUM);

printf("Send %d to processor 0 by processor %d ", localsum, myid);

}

if (myid == 0) prinf("Total Sum is %d ", result);

MPI_Finalize();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote