Question
The simplest harmonic progression is 1/1, 1/2, 1/3, Let S n = S n i = 1 1/ i . Write a parallel program in
The simplest harmonic progression is 1/1, 1/2, 1/3, Let Sn = Sni = 1 1/i.
Write a parallel program in C/C++ using OpenMP that computes these sums to arbitrary precision after the decimal point. For example, S7 = 2.592857142847, to 12 digits of precision after the decimal point.
Below is the MPI version of the code but we need it to work using OpenMP:
main(int argc, char *argc[])
{
int i, n, d, id, p;
double sum;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &id);
MPI_Comm_size(MPI_COMM_WORLD, &p);
if (id == 0) {
printf(Please input n?);
scanf(%d, &n);
printf(Please input d?);
scanf(%d, &d);
}
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
for (i = id; i <= n; i += p) {
sum += 1/i;
}
MPI_Reduce(&sum, &sum, 1, MPI_DOUBLE, MPI_SUM, 0,
MPI_COMM_WORLD);
if (id == 0) {
printf(The sum = %.*d , d, sum);
}
MPI_Finalize();
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started