#include <stdio.h> #include <string.h> #include <sys/types.h> #include "mpi.h" double f(a) double a; { return (4.0 / (1.0 + a*a)); } int main ( int argc, char **argv ) { int pool_size, my_rank, host_rank, destination, source, node_name_length, *host_rank_ptr, found_flag, int_buffer[BUFSIZ]; char char_buffer[BUFSIZ], my_node_name[BUFSIZ], console_name[BUFSIZ], host_name[BUFSIZ]; MPI_Status status; boolean_t i_am_the_master = B_FALSE; #include "preamble.c" { int n, i; double mypi, pi, h, sum, x, a; if (i_am_the_master) { printf("Enter the number of intervals: "); scanf("%d",&n); if (n==0) n=100; } MPI_Bcast(&n, 1, MPI_INT, host_rank, MPI_COMM_WORLD); h = 1.0 / (double) n; sum = 0.0; for (i = my_rank + 1; i <= n; i += pool_size) { x = h * ((double)i - 0.5); sum += f(x); } mypi = h * sum; MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, host_rank, MPI_COMM_WORLD); if (i_am_the_master) printf("pi is approximately %.16f\n", pi); } MPI_Finalize (); }