Write a parallel version of the program below using MPI. The output should be essentially the same as the sequential version; the only exception is that the perfect numbers might be found in a different order. Your program should report the total number of perfect numbers found and the time, exactly as the sequential version does. #include #include #include   double mytime() { struct timeval t; gettimeofday(&t, NULL); return t.tv_sec + t.tv_usec/1000000.0; }   _Bool is_perfect(int n) { if (n < 2) return 0; int sum = 1, i = 2; while (1) { const int i_squared = i*i; if (i_squared < n) { if (n%i == 0) sum += i + n/i; i++; } else {    if (i_squared == n) sum += i; break; } } return sum == n; }   int main(int argc, char * argv[]) { int num_perfect = 0; double start_time = mytime();   if (argc != 2) {    printf("Usage: perfect.exec bound\n"); exit(1); } int bound = atoi(argv[1]); for (int i=1; i<=bound; i++) { if (i%1000000 == 0) { printf("i = %d\n", i); fflush(stdout); } if (is_perfect(i)) { printf("Found a perfect number: %d\n", i);    num_perfect++; } } printf("Number of perfect numbers less than or equal to %d: %d. Time = %lf\n\ ",    bound, num_perfect, mytime() - start_time); }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Write a parallel version of the program below using MPI. The output should be essentially the same as the sequential version; the only exception is that the perfect numbers might be found in a different order. Your program should report the total number of perfect numbers found and the time, exactly as the sequential version does.

#include <stdio.h>

#include <stdlib.h>

#include <sys/time.h>

 

double mytime() {

struct timeval t;

gettimeofday(&t, NULL);

return t.tv_sec + t.tv_usec/1000000.0;

}

 

_Bool is_perfect(int n) {

if (n < 2) return 0;

int sum = 1, i = 2;

while (1) {

const int i_squared = i*i;

if (i_squared < n) {

if (n%i == 0) sum += i + n/i;

i++;

} else {

  

if (i_squared == n) sum += i;

break;

}

}

return sum == n;

}

 

int main(int argc, char * argv[]) {

int num_perfect = 0;

double start_time = mytime();

 

if (argc != 2) {

  

printf("Usage: perfect.exec bound\n");

exit(1);

}

int bound = atoi(argv[1]);

for (int i=1; i<=bound; i++) {

if (i%1000000 == 0) {

printf("i = %d\n", i);

fflush(stdout);

}

if (is_perfect(i)) {

printf("Found a perfect number: %d\n", i);

  

num_perfect++;

}

}

printf("Number of perfect numbers less than or equal to %d: %d. Time = %lf\n\

",

   bound, num_perfect, mytime() - start_time);

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY