Guest User

Untitled

a guest
Feb 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include "tbb/tbb.h"
  3.  
  4. double IntegralSeq(const double a1, const double b1,
  5. const double a2, const double b2,
  6. const double h)
  7. {
  8. int n1, n2;
  9. // локальная переменная
  10. // для подсчета интеграла
  11. double sum = 0.0f;
  12.  
  13. // координата точки сетки по оси x
  14. // координата точки сетки по оси y
  15. double x, y;
  16. // количество точек сетки интегрирования
  17. // n1 - по координате x
  18. // n2 - по координате y
  19. n1 = (int)(b1 - a1) / h;
  20. n2 = (int)(b2 - a2) / h;
  21.  
  22. for(int i = 0; i < n1; ++i)
  23. {
  24. for(int j = 0; j < n2; ++j)
  25. {
  26. //вычисление координат точки
  27. x = a1 + i * h + h / 2;
  28. y = a2 + j * h + h / 2;
  29. //вычисление интеграла
  30. sum += ((exp(sin(x * M_PI) * cos(y * M_PI)) + 1) /
  31. ((b1 - a1) * (b2 - a2))) * h * h;
  32. }
  33. }
  34.  
  35. return sum;
  36.  
  37. }
  38. double IntegralTBB(const double a1, const double b1,
  39. const double a2, const double b2,
  40. const double h)
  41. {
  42. int n1, n2;
  43.  
  44. n1 = (int)(b1 - a1) / h;
  45. n2 = (int)(b2 - a2) / h;
  46.  
  47. return tbb::parallel_reduce(tbb::blocked_range2d<int>(0,n1,0,n2),0.f,[&](const tbb::blocked_range2d<int> & r, float sum)
  48. {
  49. for(int i = r.rows().begin(); i != r.rows().end(); ++i) {
  50. for(int j = r.cols().begin(); j != r.cols().end() ; ++j)
  51. {
  52. double x = a1 + i * h + h / 2;
  53. double y = a2 + j * h + h / 2;
  54. sum += (exp(sin(x * M_PI) * cos(y * M_PI)) + 1) /
  55. ((b1 - a1)*(b2 - a2) * h * h);
  56.  
  57. }
  58.  
  59.  
  60. }
  61. return sum;
  62. },std::plus<int>());
  63.  
  64.  
  65.  
  66.  
  67.  
  68. }
  69. int main()
  70. {
  71. tbb::tick_count t0 = tbb::tick_count::now();
  72.  
  73. double res = IntegralSeq(0.0f, 6.0f, 0.0, 6.0, 0.001);
  74.  
  75. tbb::tick_count t1 = tbb::tick_count::now();
  76.  
  77. std:: cout << " result seq:" << res << " time:" << (t1-t0).seconds();
  78. t0 = tbb::tick_count::now();
  79. res = IntegralTBB(0.0f, 6.0f, 0.0, 6.0, 0.001);
  80.  
  81. t1 = tbb::tick_count::now();
  82.  
  83. std:: cout << " result tbb:" << res << " time:" << (t1-t0).seconds();
  84. return 0;
  85. }
Add Comment
Please, Sign In to add comment