Below question needs a small code to complete this problem (C language). Problem
ID: 3773298 • Letter: B
Question
Below question needs a small code to complete this problem (C language).
Problem 5 (Dot product for multicore machines ): Complete the SPMD (single program
multiple data) program below for computing dot product on multicore machines using the
collective MPI API’s listed below. The steps are: compute size, scatter part of vectors x and y to
other machines using MPI_Scatter, compute dot product in parallel on all machines and collect
partial products using MPI_Gather, add the partial products to come up with a single final product
by the master machine. No neeed to initialize the vectors.
MPI_Scatter(src_lst,m,MPI_INT,dst_lst,m,MPI_INT,MASTER,world);
MPI_Gather(src_buf,number,MPI_INT,dst_buf,number,MPI_INT,MASTER,world);
int main(int argc, char *argv[]) {
int i,n,nprocs,pid,size,prod=0;
int vector_x[NELMS],vector_y[NELMS],partial_prods[MAXPROCS];
MPI_Status status;
MPI_Comm world;
n = atoi(argv[1]); /* number of elements */
MPI_Init(&argc, &argv);
world = MPI_COMM_WORLD;
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &pid);
/////////////////////// Feel code here .....
///////////////////////
MPI_Finalize();
}
int dot_product(int s,int e,int *x,int *y){
int i,prod=0;
for (i=s;i
return prod;
}
Explanation / Answer
Answer:
MPI_Scatter(src_lst,m,MPI_INT,dst_lst,m,MPI_INT,MASTER,world);
MPI_Gather(src_buf,number,MPI_INT,dst_buf,number,MPI_INT,MASTER,world);
int main(int argc, char *argv[])
{
int i,n,nprocs,pid,size,prod=0;
int vector_x[NELMS],vector_y[NELMS],partial_prods[MAXPROCS];
MPI_Status status;
MPI_Comm world;
n = atoi(argv[1]); /* number of elements */
MPI_Init(&argc, &argv);
world = MPI_COMM_WORLD;
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &pid);
size=n/nprocs;
MPI_Scatter(vector_x,size,MPI_INT,partial_prods,m,MPI_INT,MASTER,world);
MPI_Scatter(vector_y,size,MPI_INT,partial_prods,m,MPI_INT,MASTER,world);
prod=dot_product(1,NELMS,vector_x,vector_y);
MPI_Gather(&prod,1,MPI_INT,partial_prods,1,MPI_INT,MASTER,world);
printf(" DotProduct value is %d",prod);
MPI_Finalize();
}
int dot_product(int s,int e,int *x,int *y)
{
int i,prod=0;
for (i=s;i<=e;i++)
prod=prod+s[i]*y[i];
return prod;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.