Need answers for the questions of the 2 parallel programs given below. Needed within 18-20 hours ans it shouldn't be a problem for someone who did parallel programming. Max. $20 will be accepted.
Program 1: Consider the following fragment of a parallel program.
MPI_Comm_rank(MPI_COMM_WORLD, &procnum);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
// COMMENT 1
right = procnum + 1; if (right == numprocs) right = 0;
left = procnum - 1; if (left == -1) left = numprocs-1;
sum = 0;
value = procnum;
for (i = 0; i < numprocs; i++) {
// COMMENT 2
MPI_Send(&value,1,MPI_INT,right,tag,MPI_COMM_WORLD);
MPI_Recv(&new_value,1,MPI_INT,left,tag,MPI_COMM_WORLD,&status);
// COMMENT 3
sum = sum + new_value;
value = new_value;
}
// COMMENT 4
if (procnum == 0) {
printf ("%d\n", sum);
}
(a) For each of the four lines listed as comments, such as COMMENT 1 etc,
write down the comment number and then a short explanation of the
lines of code underneath that comment. For example, for COMMENT
1, explain the two subsequent lines, before the blank line.
(b) Explain the purpose of the for loop.
(c) Draw a diagram showing the flow of messages between processes in
an execution of the program. Assume four processes.
Program 2:Consider the following parallel program (as presented in lectures):
#include mpi.h
#include <stdio.h>
#include <math.h>
#define MAXSIZE 1000
void main(int argc, char *argv[]){
int myid,numprocs;
int data[MAXSIZE], i, x, low, high, myresult,
result;
char fn[255];
char *fp;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
if (my_id == 0){
strcpy(fn,getenv(HOME));
strcat(fn,/MPI/[login to view URL]);
if((fp = fopen(fn,r)) == NULL){
printf(Cant open the input file);
exit(1);
}
for (i=0; i < MAXSIZE; i++)
fscanf(fp,%d,&data[i]);
MPI_Bcast(data,MAXSIZE,MPI_INT,0,MPI_COMM_WORLD);
x = MAXSIZE / numprocs;
low = myid * x;
high = low + x;
for(i = low; i < high; i++)
myresult += data[i];
printf(I got %d from %d\n,myresult, myid);
MPI_Reduce(&myresult,&result,1,MPI_INT,MPI_SUM,0,
MPI_COMM_WORLD);
if (myid == 0) printf(the sum is %d.\n,result);
MPI_Finalize();
}}
(a) Show how the program can be initiated with five processes in use.
(b) Identify clearly which code is executed by which processes.
(c) What is the significance of the communicator in the call to MPI Bcast?
(d) Draw a diagram to show the process structure in the executing program.
(e) Draw a series of diagrams to show how the computation proceeds.
Identify parts executed in parallel, and code executed sequentially.
(f) Can the final reduce be done in parallel? Explain.
Parallel programmer with experience in MPI.
I can answer the question and explain the code.
I know MPI from university, where we used MPICH2 to run distributed programs.