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); }
Write a parallel version of the
#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);
}
C Language
Dennis M. Ritchie created the general-purpose, functional, imperative programming language for computers known as C in 1972 now at Bell Telephone Laboratories. to create the UNIX operating system. The most often used computer language is C. Along with Java, which is likewise extremely popular and utilised by the majority of contemporary software programmers, it consistently ranks at the top of the popularity scale.
Step by step
Solved in 3 steps with 1 images