chundoong-lab-ta/SHPC2022/hw4_answer/riemannsum/riemannsum.c

37 lines
1.1 KiB
C

#include <mpi.h>
#include "riemannsum.h"
#include "util.h"
double riemannsum(int num_intervals, int mpi_rank, int mpi_world_size,
int threads_per_process) {
double pi = 0;
double h = 1.0 / (double)num_intervals;
// TODO: Parallelize the code using mpi_world_size processes (1 process per
// node.
// In total, (mpi_world_size * threads_per_process) threads will collaborate
// to compute the Riemann sum.
int from =
1 + ((num_intervals + mpi_world_size - 1) / mpi_world_size) * mpi_rank;
int to =
((num_intervals + mpi_world_size - 1) / mpi_world_size) * (mpi_rank + 1);
if (num_intervals < to)
to = num_intervals;
#pragma omp parallel for num_threads(threads_per_process) reduction(+ : pi)
for (int i = from; i <= to; i++) {
double x = h * ((double)i - 0.5);
pi += h * f(x);
}
double pi_sum = 0.0;
MPI_Reduce(&pi, &pi_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
pi = pi_sum;
// Rank 0 should return the estimated PI value
// Other processes can return any value (don't care)
return pi;
}