Guest User

Untitled

a guest
Feb 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 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. return sum;
  35. }
  36.  
  37. double IntegralTBB(const double a1, const double b1,
  38. const double a2, const double b2,
  39. const double h)
  40. {
  41. int n1, n2;
  42.  
  43. n1 = (int)(b1 - a1) / h;
  44. n2 = (int)(b2 - a2) / h;
  45.  
  46. return tbb::parallel_reduce(tbb::blocked_range2d<int>(0,n1,0,n2),0.f,[&](const tbb::blocked_range2d<int> & r, float sum)
  47. {
  48. for(int i = r.rows().begin(); i != r.rows().end(); ++i) {
  49. for(int j = r.cols().begin(); j != r.cols().end() ; ++j)
  50. {
  51. double x = a1 + i * h + h / 2;
  52. double y = a2 + j * h + h / 2;
  53. sum += (exp(sin(x * M_PI) * cos(y * M_PI)) + 1) /
  54. ((b1 - a1)*(b2 - a2) * h * h);
  55.  
  56. }
  57. }
  58. return sum;
  59. },std::plus<int>());
  60. }
  61.  
  62. int main()
  63. {
  64. tbb::tick_count t0 = tbb::tick_count::now();
  65.  
  66. double res = IntegralSeq(0.0f, 6.0f, 0.0, 6.0, 0.001);
  67.  
  68. tbb::tick_count t1 = tbb::tick_count::now();
  69.  
  70. std:: cout << " result seq:" << res << " time:" << (t1-t0).seconds();
  71. t0 = tbb::tick_count::now();
  72. res = IntegralTBB(0.0f, 6.0f, 0.0, 6.0, 0.001);
  73.  
  74. t1 = tbb::tick_count::now();
  75.  
  76. std:: cout << " result tbb:" << res << " time:" << (t1-t0).seconds();
  77. return 0;
  78. }
Add Comment
Please, Sign In to add comment