fork download
  1. //Carter Wright
  2. //Program to compute the value of Pi
  3. //demonstrating parallel computing capabilities
  4.  
  5.  
  6. // Include necessary libraries
  7. #include <mpi.h>
  8. #include <cmath>
  9. #include <cstdlib>
  10. #include <iostream>
  11.  
  12. // Function to compute pi using the sum of the reciprocals of the squares method
  13. double compute_pi_reciprocal_squares(int rank, int size, int num_terms) {
  14. double sum = 0.0;
  15. // Each process computes a part of the sum
  16. for (int i = rank + 1; i <= num_terms; i += size) {
  17. sum += 1.0 / (i * i);
  18. }
  19. return sum;
  20. }
  21.  
  22. // Function to compute pi using the trapezoid rule
  23. double compute_pi_trapezoid(int rank, int size, int num_intervals) {
  24. double h = 1.0 / num_intervals;
  25. double sum = 0.0;
  26. // Each process computes a part of the sum
  27. for (int i = rank + 1; i <= num_intervals; i += size) {
  28. double x = h * (i - 0.5);
  29. sum += 4.0 / (1.0 + x * x);
  30. }
  31. return h * sum;
  32. }
  33.  
  34. int main(int argc, char *argv[]) {
  35. // Initialize MPI and get the rank and size
  36. int rank, size;
  37. MPI_Init(&argc, &argv);
  38. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  39. MPI_Comm_size(MPI_COMM_WORLD, &size);
  40.  
  41. // Process 0 asks the user for the method and the number of terms or intervals
  42. if (rank == 0) {
  43. std::cout << "Enter the method you wish to use (1=reciprocal squares, 2=trapezoid rule): ";
  44. int method;
  45. std::cin >> method;
  46. // Broadcast the method to all processes
  47. MPI_Bcast(&method, 1, MPI_INT, 0, MPI_COMM_WORLD);
  48.  
  49. std::cout << "Enter the number of terms or intervals: ";
  50. int num;
  51. std::cin >> num;
  52. // Broadcast the number of terms or intervals to all processes
  53. MPI_Bcast(&num, 1, MPI_INT, 0, MPI_COMM_WORLD);
  54. }
  55.  
  56. // All processes receive the method and the number of terms or intervals
  57. int method, num;
  58. MPI_Bcast(&method, 1, MPI_INT, 0, MPI_COMM_WORLD);
  59. MPI_Bcast(&num, 1, MPI_INT, 0, MPI_COMM_WORLD);
  60.  
  61. // Depending on the method, compute the local sum
  62. double local_sum;
  63. if (method == 1) {
  64. local_sum = compute_pi_reciprocal_squares(rank, size, num);
  65. } else if (method == 2) {
  66. local_sum = compute_pi_trapezoid(rank, size, num);
  67. }
  68.  
  69. // Reduce the local sums to a global sum
  70. double global_sum;
  71. MPI_Reduce(&local_sum, &global_sum, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  72.  
  73. // Process 0 computes and prints the value of Pi
  74. if (rank == 0) {
  75. double pi;
  76. if (method == 1) {
  77. pi = sqrt(6.0 * global_sum);
  78. } else if (method == 2) {
  79. pi = global_sum;
  80. }
  81. printf("Computed value of Pi: %f\n", pi);
  82. }
  83.  
  84. // Finalize MPI and end the program
  85. MPI_Finalize();
  86. return 0;
  87. }
  88.  
  89.  
Success #stdin #stdout #stderr 0.25s 40816KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected '/' in "/"
Execution halted