andreybotanic

integral

Apr 2nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. int main(int argc, char** argv)
  2. {
  3.     float fTimeStart = clock() / (float)CLOCKS_PER_SEC;
  4.     MPI_Init(&argc, &argv);
  5.     int rank;
  6.     int process_count;
  7.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  8.     MPI_Comm_size(MPI_COMM_WORLD, &process_count);
  9.     double a = 0;
  10.     double b = PI/2;
  11.     double length = b - a;
  12.     int NUMBER_OF_INTERVALS = 1000000000;
  13.     double dx = (b - a) / NUMBER_OF_INTERVALS;
  14.     int intervals_per_one_rank = NUMBER_OF_INTERVALS / process_count;
  15.     double length_per_rank = (b - a) / process_count;
  16.  
  17.     double my_a = a + rank * length_per_rank;
  18.     double my_b = a + (rank + 1) * length_per_rank;
  19.     double sum = 0;
  20.     for (int i = 0; i < intervals_per_one_rank; ++i)
  21.     {
  22.         sum += F(my_a + i * dx) * dx;
  23.     }
  24.     double total;
  25.     MPI_Reduce(&sum, &total, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  26.     if (rank == 0)
  27.     {
  28.         printf("%.10lf\n", total);
  29.         float fTimeStop = clock() / (float)CLOCKS_PER_SEC;
  30.         printf("Working time = %f", -fTimeStart + fTimeStop);
  31.     }
  32.     MPI_Finalize();
  33. }
Advertisement
Add Comment
Please, Sign In to add comment