he following code isn't working correctly and I am unable to figure out why. The for loop is never reached in the first if statement and the program never terminates. May someone please explain to me why that is.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.4: A Case Study: Rectangular To Polar Coordinate Conversion
Problem 9E: (Numerical) Write a program that tests the effectiveness of the rand() library function. Start by...
icon
Related questions
Question

The following code isn't working correctly and I am unable to figure out why. The for loop is never reached in the first if statement and the program never terminates. May someone please explain to me why that is.

 

 

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <mpi.h>
int main(void)
{
int sum, comm_sz, my_rank, i, next, x;
int powTwo = 2;
int diff = 1;

MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
srandom((unsigned)time(NULL) + my_rank);
x = random() % 10;
if (my_rank % powTwo == 0)
{
printf("IF----");
printf("Process %d generates: %d\n", my_rank, x);
for (i = 0; i < comm_sz; i++)
{
MPI_Recv(&x, 1, MPI_INT, i, my_rank , MPI_COMM_WORLD, MPI_STATUS_IGNORE);
sum += x;
printf("Current Sum=: %d\n", sum);
}

printf("The new divisor is:%d\n", powTwo);
powTwo *= 2;
diff *= 2;

} else if (my_rank % powTwo != 0) {
printf("ELSE----");
printf("Process %d generates: %d\n", my_rank, x);
MPI_Send(&x, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
}else if (my_rank==0){
printf("Sum=: %d\n", sum);
}
MPI_Finalize();
return 0;
}

In the following figure shows how to distribute amount of work between 8 processes (See
lecture 7, page 21):
Processes
1
3
4
6
7
5
9.
11
Write an MPI program to generate a random number in the range 0-10 for each process.
Your MPI program must works for 1, 2, 4, 8, 16, 32, 64, 128 process.
Note: Assume the user enters power of 2 for the numbers of processes.
The following is just a sample from my program for 128 processes. Your program must
work for any number of processes that is a power of 2:
Transcribed Image Text:In the following figure shows how to distribute amount of work between 8 processes (See lecture 7, page 21): Processes 1 3 4 6 7 5 9. 11 Write an MPI program to generate a random number in the range 0-10 for each process. Your MPI program must works for 1, 2, 4, 8, 16, 32, 64, 128 process. Note: Assume the user enters power of 2 for the numbers of processes. The following is just a sample from my program for 128 processes. Your program must work for any number of processes that is a power of 2:
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr