fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <mpi.h>
  5.  
  6. const double a = 0;
  7. const double b = 2000000000;
  8.  
  9. /* Function declarations */
  10. void Get_input(int argc, char* argv[], int my_rank, double* n_p);
  11. double Trap(double left_endpt, double right_endpt, int trap_count,
  12. double base_len);
  13. double f(double x);
  14.  
  15. int main(int argc, char** argv) {
  16. int my_rank, comm_sz, local_n;
  17. double n, h, local_a, local_b;
  18. double local_int, total_int;
  19. double start, finish, loc_elapsed, elapsed;
  20.  
  21. MPI_Init(NULL, NULL);
  22. MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  23. MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
  24.  
  25. Get_input(argc, argv, my_rank, &n); /*Read user input */
  26.  
  27. /*Note: h and local_n are the same for all processes*/
  28. h = (b-a)/n; /* length of each trapezoid */
  29. local_n = n/comm_sz; /* number of trapezoids per process */
  30.  
  31. /* Length of each process' interval of integration = local_n*h. */
  32. local_a = a + my_rank*local_n*h;
  33. local_b = local_a + local_n*h;
  34.  
  35. MPI_Barrier(MPI_COMM_WORLD);
  36. start = MPI_Wtime();
  37. /* Calculate each process' local integral using local endpoints*/
  38. local_int = Trap(local_a, local_b, local_n, h);
  39. finish = MPI_Wtime();
  40. loc_elapsed = finish-start;
  41. MPI_Reduce(&loc_elapsed, &elapsed, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
  42.  
  43. /* Add up the integrals calculated by each process */
  44. MPI_Reduce(&local_int, &total_int, 1, MPI_DOUBLE, MPI_SUM, 0,
  45. MPI_COMM_WORLD);
  46.  
  47.  
  48. if (my_rank == 0) {
  49. printf("With n = %.0f trapezoids, our estimate\n", n);
  50. printf("of the integral from %.0f to %.0f = %.0f\n",
  51. a, b, total_int);
  52. printf("Elapsed time = %f milliseconds \n", elapsed * 1000);
  53. }
  54.  
  55. /* Shut down MPI */
  56. MPI_Finalize();
  57.  
  58. return 0;
  59. } /* main */
  60.  
  61. /*------------------------------------------------------------------
  62.  * Function: Get_input
  63.  * Purpose: Get the user input: the number of trapezoids
  64.  * Input args: my_rank: process rank in MPI_COMM_WORLD
  65.  * comm_sz: number of processes in MPI_COMM_WORLD
  66.  * Output args: n_p: pointer to number of trapezoids
  67.  */
  68. void Get_input(int argc, char* argv[], int my_rank, double* n_p){
  69. if (my_rank == 0) {
  70. if (argc!= 2){
  71. fprintf(stderr, "usage: mpirun -np <N> %s <number of trapezoids> \n", argv[0]);
  72. fflush(stderr);
  73. *n_p = -1;
  74. } else {
  75. *n_p = atoi(argv[1]);
  76. }
  77. }
  78. // Broadcasts value of n to each process
  79. MPI_Bcast(n_p, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  80.  
  81. // negative n ends the program
  82. if (*n_p <= 0) {
  83. MPI_Finalize();
  84. exit(-1);
  85. }
  86. } /* Get_input */
  87.  
  88. /*------------------------------------------------------------------
  89.  * Function: Trap
  90.  * Purpose: Serial function for estimating a definite integral
  91.  * using the trapezoidal rule
  92.  * Input args: left_endpt
  93.  * right_endpt
  94.  * trap_count
  95.  * base_len
  96.  * Return val: Trapezoidal rule estimate of integral from
  97.  * left_endpt to right_endpt using trap_count
  98.  * trapezoids
  99.  */
  100. double Trap(double left_endpt, double right_endpt, int trap_count, double base_len) {
  101. double estimate, x;
  102. int i;
  103.  
  104. estimate = (f(left_endpt) + f(right_endpt))/2.0;
  105. for (i = 1; i <= trap_count-1; i++) {
  106. x = left_endpt + i*base_len;
  107. estimate += f(x);
  108. }
  109. estimate = estimate*base_len;
  110.  
  111. return estimate;
  112. } /* Trap */
  113.  
  114.  
  115. /*------------------------------------------------------------------
  116.  * Function: f
  117.  * Purpose: Compute value of function to be integrated
  118.  * Input args: x
  119.  */
  120. double f(double x) {
  121. return x*x;
  122. } /* f */
Success #stdin #stdout #stderr 0.26s 38528KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "const double"
Execution halted