fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <mpi.h>
  4. #include <fstream>
  5. #include <vector>
  6. #include <cstdlib> // for system function
  7.  
  8. using namespace std;
  9.  
  10. void writeDataToFile(const string& filename, int x, long double y) {
  11. ofstream file(filename, ios::app);
  12. if (file.is_open()) {
  13. printf("size and time: %f",y);
  14. file << x << " " << y<<" \n" << endl;
  15. file.close();
  16. }
  17. else {
  18. cerr << "Unable to open file for writing." <<endl;
  19. }
  20. }
  21.  
  22. double f(double x) {
  23. // Функция, которую мы интегрируем
  24. return cos(6*x)*sin(5*x) + 4;
  25. }
  26.  
  27. double rectangle_integration(double a, double b, int n) {
  28.  
  29. double simpson(double a, double b, int n, double h) {
  30. double sum = f(a) + f(b);
  31.  
  32. for (int i = 1; i < n; i += 2) {
  33. double x = a + i * h;
  34. sum += 4 * f(x);
  35. }
  36.  
  37. for (int i = 2; i < n - 1; i += 2) {
  38. double x = a + i * h;
  39. sum += 2 * f(x);
  40. }
  41.  
  42. return sum * h / 3.0;
  43. }
  44.  
  45.  
  46.  
  47. }
  48.  
  49. int main(int argc, char** argv) {
  50.  
  51. const double a = 0.0;
  52. const double b = 1;
  53. const int n = 1000000;
  54.  
  55. int rank, size=10;
  56. long double min_start = 1000000, max_end = 0, start, end, local_result, total_result;
  57.  
  58. MPI_Init(&argc, &argv);
  59. MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  60. MPI_Comm_size(MPI_COMM_WORLD, &size);
  61. MPI_Barrier(MPI_COMM_WORLD);
  62.  
  63. start = MPI_Wtime();
  64.  
  65. cout << "Process " << rank << " of " << size << " is running." << endl;
  66.  
  67.  
  68. // Вычисляем локальный результат для каждого процесса
  69. int local_n = n / size;
  70. double local_a = a + rank * (b - a) / size;
  71. double local_b = local_a + (b - a) / size;
  72. double h = (b - a) / n;
  73. //local_result = rectangle_integration(local_a, local_b, local_n);
  74. //local_result = simpson(local_a, local_b, local_n, h);
  75. local_result = trapezoidal(local_a, local_b, local_n, h);
  76.  
  77. MPI_Reduce(&local_result, &total_result, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  78. MPI_Barrier(MPI_COMM_WORLD);
  79. end = MPI_Wtime();
  80.  
  81.  
  82. if (rank == 0) {
  83. cout << "Total result: " << total_result << endl;
  84. }
  85.  
  86. int numproc;
  87.  
  88. numproc = size;
  89.  
  90.  
  91.  
  92. // Find max time
  93. if (min_start > start) {
  94. min_start = start;
  95. }
  96.  
  97. if (max_end < end) {
  98. max_end = end;
  99. }
  100.  
  101. if (rank == 0) {
  102. printf("Time taken to process: %f", (max_end - min_start));
  103. }
  104. if (rank == 0) {
  105. writeDataToFile("data.txt", numproc, (max_end - min_start));
  106. }
  107. MPI_Finalize();
  108.  
  109.  
  110.  
  111.  
  112. return 0;
  113. }
  114.  
Success #stdin #stdout #stderr 0.27s 40768KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error: unexpected symbol in "using namespace"
Execution halted