| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kosolapov_v_calc_mult_integrals_m_rectangles/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <tbb/blocked_range.h> | ||
| 4 | #include <tbb/parallel_reduce.h> | ||
| 5 | |||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <functional> | ||
| 9 | #include <tuple> | ||
| 10 | |||
| 11 | #include "kosolapov_v_calc_mult_integrals_m_rectangles/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace kosolapov_v_calc_mult_integrals_m_rectangles { | ||
| 14 | |||
| 15 | 32 | KosolapovVCalcMultIntegralsMRectanglesTBB::KosolapovVCalcMultIntegralsMRectanglesTBB(const InType &in) { | |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 | 32 | GetInput() = InType(in); | |
| 18 | GetOutput() = 0.0; | ||
| 19 | 32 | } | |
| 20 | |||
| 21 | 32 | bool KosolapovVCalcMultIntegralsMRectanglesTBB::ValidationImpl() { | |
| 22 | 32 | int steps = std::get<0>(GetInput()); | |
| 23 | 32 | int func_id = std::get<1>(GetInput()); | |
| 24 |
2/4✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
|
32 | return steps > 0 && func_id >= 1 && func_id <= 4; |
| 25 | } | ||
| 26 | |||
| 27 | 32 | bool KosolapovVCalcMultIntegralsMRectanglesTBB::PreProcessingImpl() { | |
| 28 | 32 | return true; | |
| 29 | } | ||
| 30 | |||
| 31 | 32 | bool KosolapovVCalcMultIntegralsMRectanglesTBB::RunImpl() { | |
| 32 | 32 | int steps = std::get<0>(GetInput()); | |
| 33 | 32 | int func_id = std::get<1>(GetInput()); | |
| 34 | 32 | std::tuple<double, double, double, double> temp = GetBounds(func_id); | |
| 35 | 32 | double a = std::get<0>(temp); | |
| 36 | 32 | double b = std::get<1>(temp); | |
| 37 | 32 | double c = std::get<2>(temp); | |
| 38 | 32 | double d = std::get<3>(temp); | |
| 39 | 32 | double integral = RectanglesIntegral(func_id, steps, a, b, c, d); | |
| 40 | 32 | GetOutput() = integral; | |
| 41 | 32 | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | 32 | bool KosolapovVCalcMultIntegralsMRectanglesTBB::PostProcessingImpl() { | |
| 45 | 32 | return true; | |
| 46 | } | ||
| 47 | |||
| 48 | ✗ | double KosolapovVCalcMultIntegralsMRectanglesTBB::Function1(double x, double y) { | |
| 49 | // f(x,y) = x^2 + y^2 | ||
| 50 | 8000 | return (x * x) + (y * y); | |
| 51 | } | ||
| 52 | ✗ | double KosolapovVCalcMultIntegralsMRectanglesTBB::Function2(double x, double y) { | |
| 53 | // f(x,y) = sin(x) * cos(y) | ||
| 54 | 8000 | return std::sin(x) * std::cos(y); | |
| 55 | } | ||
| 56 | ✗ | double KosolapovVCalcMultIntegralsMRectanglesTBB::Function3(double x, double y) { | |
| 57 | // f(x,y) = exp(-(x^2 + y^2)) | ||
| 58 | 8000 | return std::exp(-((x * x) + (y * y))); | |
| 59 | } | ||
| 60 | ✗ | double KosolapovVCalcMultIntegralsMRectanglesTBB::Function4(double x, double y) { | |
| 61 | // f(x,y) = sin(x + y) | ||
| 62 | 8000 | return std::sin(x + y); | |
| 63 | } | ||
| 64 | 32000 | double KosolapovVCalcMultIntegralsMRectanglesTBB::CallFunction(int func_id, double x, double y) { | |
| 65 |
4/5✓ Branch 0 taken 8000 times.
✓ Branch 1 taken 8000 times.
✓ Branch 2 taken 8000 times.
✓ Branch 3 taken 8000 times.
✗ Branch 4 not taken.
|
32000 | switch (func_id) { |
| 66 | case 1: | ||
| 67 | 8000 | return Function1(x, y); | |
| 68 | case 2: | ||
| 69 | 8000 | return Function2(x, y); | |
| 70 | case 3: | ||
| 71 | 8000 | return Function3(x, y); | |
| 72 | case 4: | ||
| 73 | 8000 | return Function4(x, y); | |
| 74 | default: | ||
| 75 | ✗ | return Function1(x, y); | |
| 76 | } | ||
| 77 | } | ||
| 78 | 32 | std::tuple<double, double, double, double> KosolapovVCalcMultIntegralsMRectanglesTBB::GetBounds(int func_id) { | |
| 79 |
4/5✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
|
32 | switch (func_id) { |
| 80 | 8 | case 1: | |
| 81 | return {0.0, 1.0, 0.0, 1.0}; | ||
| 82 | 8 | case 2: | |
| 83 | return {0.0, kPi, 0.0, kPi / 2.0}; | ||
| 84 | 8 | case 3: | |
| 85 | return {-1.0, 1.0, -1.0, 1.0}; | ||
| 86 | 8 | case 4: | |
| 87 | return {0.0, kPi, 0.0, kPi}; | ||
| 88 | ✗ | default: | |
| 89 | return {0.0, 1.0, 0.0, 1.0}; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | 32 | double KosolapovVCalcMultIntegralsMRectanglesTBB::RectanglesIntegral(int func_id, int steps, double a, double b, | |
| 93 | double c, double d) { | ||
| 94 | 32 | double hx = (b - a) / steps; | |
| 95 | 32 | double hy = (d - c) / steps; | |
| 96 | 32 | size_t total = static_cast<size_t>(steps) * steps; | |
| 97 | |||
| 98 | 64 | double sum = tbb::parallel_reduce(tbb::blocked_range<size_t>(0, total), 0.0, | |
| 99 | 9026 | [&](const tbb::blocked_range<size_t> &range, double local_sum) { | |
| 100 |
2/2✓ Branch 0 taken 32000 times.
✓ Branch 1 taken 8994 times.
|
40994 | for (size_t idx = range.begin(); idx != range.end(); ++idx) { |
| 101 | 32000 | int i = static_cast<int>(idx / steps); | |
| 102 | 32000 | int j = static_cast<int>(idx % steps); | |
| 103 | 32000 | double x = (a + ((i + 0.5) * hx)); | |
| 104 | 32000 | double y = (c + ((j + 0.5) * hy)); | |
| 105 | 32000 | local_sum += CallFunction(func_id, x, y); | |
| 106 | } | ||
| 107 | 8994 | return local_sum; | |
| 108 | 32 | }, std::plus<>()); | |
| 109 | 32 | sum *= (hx * hy); | |
| 110 | 32 | return sum; | |
| 111 | } | ||
| 112 | |||
| 113 | } // namespace kosolapov_v_calc_mult_integrals_m_rectangles | ||
| 114 |