Advertisement
osipyonok

Inegration_Par_prog_lab1

Apr 18th, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. // Par_prog_lab1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <cmath>
  7. #include <mpi.h>
  8. #include <ctime>
  9.  
  10. using namespace std;
  11.  
  12. double f(double x);
  13.  
  14. double rectangle_method(double a, double b, int n) {
  15.     double h = (b - a) / n;
  16.     double sum = 0.0;
  17.     for (int i = 0; i < n; ++i) {
  18.         double x = a + h * (double)i;
  19.         sum += f(0.5 * (x + x + h));
  20.     }
  21.     return h * sum;
  22. }
  23.  
  24. double f(double x) {
  25.     return sin(x);
  26. }
  27.  
  28. inline void solve(double a, double b, int n) {
  29.     long long l = clock();
  30.     double ans = rectangle_method(a, b, n);
  31.     cout << "Sequential I(x) = " << ans << endl;
  32.     cout << "Sequential time: " << (double)(clock() - l) / CLOCKS_PER_SEC << endl << endl;
  33. }
  34.  
  35. int main(int argc, char** argv)
  36. {
  37.     cout.precision(5);
  38.     cout.setf(ios::fixed);
  39.     double a, b , n;
  40.     a = 0;
  41.     b = 10000;
  42.     n = 1e9;
  43.  
  44.     MPI_Status status;
  45.  
  46.  
  47.  
  48.     MPI_Init(&argc, &argv);
  49.  
  50.     int cur_proc, total;
  51.  
  52.     MPI_Comm_size(MPI_COMM_WORLD, &total);
  53.     MPI_Comm_rank(MPI_COMM_WORLD, &cur_proc);
  54.  
  55.     if (cur_proc == 0) {
  56.         solve(a, b, n);
  57.     }
  58.  
  59.     double cur_a, cur_b;
  60.     int cur_n;
  61.     double sum = 0, part = 0;
  62.  
  63.     long long start = clock();
  64.  
  65.     if (cur_proc == 0) {
  66.         double step = (b - a) / total;
  67.         cur_a = a;
  68.         cur_b = a + step;
  69.         cur_n = (int)n / total;
  70.         for (int i = 1; i < total; ++i) {
  71.             MPI_Send(&cur_a, 1, MPI_DOUBLE, i, 100, MPI_COMM_WORLD);
  72.             MPI_Send(&cur_b, 1, MPI_DOUBLE, i, 101, MPI_COMM_WORLD);
  73.             MPI_Send(&cur_n, 1, MPI_INT, i, 102, MPI_COMM_WORLD);
  74.             cur_a = cur_b;
  75.             cur_b += step;
  76.         }
  77.     }
  78.     else {
  79.         MPI_Recv(&cur_a, 1, MPI_DOUBLE, 0, 100, MPI_COMM_WORLD, &status);
  80.         MPI_Recv(&cur_b, 1, MPI_DOUBLE, 0, 101, MPI_COMM_WORLD, &status);
  81.         MPI_Recv(&cur_n, 1, MPI_INT, 0, 102, MPI_COMM_WORLD, &status);
  82.     }
  83.     //cout << "Hello from " << cur_proc << " process" << endl;
  84.     part = rectangle_method(cur_a, cur_b, cur_n);
  85.     if (cur_proc == 0) {
  86.         sum = part;
  87.         for (int i = 1; i < total; ++i) {
  88.             MPI_Recv(&part, 1, MPI_DOUBLE, i, 103, MPI_COMM_WORLD, &status);
  89.             sum += part;
  90.         }
  91.     }
  92.     else {
  93.         MPI_Send(&part, 1, MPI_DOUBLE, 0, 103, MPI_COMM_WORLD);
  94.     }
  95.  
  96.     if (cur_proc == 0) {
  97.         long long end = clock();
  98.         cout << "Paralell I(x) = " << sum << endl;
  99.         cout << "Paralell time: " << (double)(end - start) / CLOCKS_PER_SEC << endl;
  100.         system("pause");
  101.     }
  102.  
  103.     MPI_Finalize();
  104.    
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement