Question
#include #include #include #define generate_data(i,j) (i)+(j)*(j) void main( int argc, char **argv) { int i, j, pid, np, mtag, count, data[100][100], row_sum[100] ; double t0,
#include #include #include #define generate_data(i,j) (i)+(j)*(j) void main( int argc, char **argv) { int i, j, pid, np, mtag, count, data[100][100], row_sum[100] ; double t0, t1 ; MPI_Status status; MPI_Request req_s, req_r; MPI_Init( &argc, &argv ); MPI_Comm_rank(MPI_COMM_WORLD, &pid); MPI_Comm_size(MPI_COMM_WORLD, &np); if(pid == 0) { // generate data[] for(i=0; i<50; i++) for(j=0; j<100; j++) data[i][j] = generate_data(i,j) ; mtag = 1 ; MPI_Send(data, 5000, MPI_INT, 1, mtag, MPI_COMM_WORLD) ; for(i=50; i<100; i++) for(j=0; j<100; j++) data[i][j] = generate_data(i,j) ; for(i=50; i<100; i++) { row_sum[i] = 0 ; for(j=0; j<100; j++) row_sum[i] += data[i][j] ; } /*** receive computed row_sums from pid 1 ***/ mtag = 2 ; MPI_Recv(row_sum, 50, MPI_INT, 1, mtag, MPI_COMM_WORLD, &status) ; for(i=0; i<100; i++) { printf(" %d ", row_sum[i]) ; if(i%10 ==0) printf(" "); } } else { /*** pid == 1 ***/ mtag = 1 ; MPI_Recv(data, 5000, MPI_INT, 0, mtag, MPI_COMM_WORLD, &status) ; for(i=0; i<50; i++) { row_sum[i] = 0 ; for(j=0; j<100; j++) row_sum[i] += data[i][j] ; } /*** Send computed row_sums to pid 0 ***/ mtag = 2 ; MPI_Send(row_sum, 50, MPI_INT, 0, mtag, MPI_COMM_WORLD) ; } /****** End of else ******/ MPI_Finalize(); } /****************** End of function main() ********************/ #include #include #include #define generate_data(i,j) (i)+(j)*(j) void main( int argc, char **argv) { int i, j, pid, np, mtag, count, data[100][100], row_sum[100] ; double t0, t1 ; MPI_Status status; MPI_Request req_s, req_r; MPI_Init( &argc, &argv ); MPI_Comm_rank(MPI_COMM_WORLD, &pid); MPI_Comm_size(MPI_COMM_WORLD, &np); if(pid == 0) { // generate data[] for(i=0; i<50; i++) for(j=0; j<100; j++) data[i][j] = generate_data(i,j) ; mtag = 1 ; MPI_Isend(data, 5000, MPI_INT, 1, mtag, MPI_COMM_WORLD, &req_s) ; for(i=50; i<100; i++) for(j=0; j<100; j++) data[i][j] = generate_data(i,j) ; for(i=50; i<100; i++) { row_sum[i] = 0 ; for(j=0; j<100; j++) row_sum[i] += data[i][j] ; } MPI_Wait(&req_s, &status) ; /*** receive computed row_sums from pid 1 ***/ mtag = 2 ; MPI_Recv(row_sum, 50, MPI_INT, 1, mtag, MPI_COMM_WORLD, &status) ; for(i=0; i<100; i++) { printf(" %d ", row_sum[i]) ; if(i%10 ==0) printf(" "); } } else { /*** pid == 1 ***/ mtag = 1 ; MPI_Recv(data, 5000, MPI_INT, 0, mtag, MPI_COMM_WORLD, &status) ; for(i=0; i<50; i++) { row_sum[i] = 0 ; for(j=0; j<100; j++) row_sum[i] += data[i][j] ; } /*** Send computed row_sums to pid 0 ***/ mtag = 2 ; MPI_Send(row_sum, 50, MPI_INT, 0, mtag, MPI_COMM_WORLD) ; } /****** End of else ******/ MPI_Finalize(); } /****************** End of function main() ********************/ Assignment 1. In this assignment, rewrite the code on page 2 so that there is significant overlapping of communication with computation on pid==1, but do not reassign the computations assigned to the two processes, that is, Pid 0 generates all data, sends rows 0-49 to pid 1, and calculates row sums for rows 50-99. Pid 1 receives row 0-49 from pid 0, does the row sums for row 0 to row 49, and sends the result back to pid 0. The new requirement on pid 1 is to overlap part of the operations for receiving row 0 to row 49 with part of the computation for doing row sums for row 0 through row 49. Hint: To achieve significant overlapping of communication with computation on pid 1, code for pid 0 also needs to be revised.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started