| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "samoylenko_i_integral_trapezoid/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <functional> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "oneapi/tbb/blocked_range.h" | ||
| 10 | #include "oneapi/tbb/parallel_reduce.h" | ||
| 11 | #include "samoylenko_i_integral_trapezoid/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace samoylenko_i_integral_trapezoid { | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | 20 | std::function<double(const std::vector<double> &)> GetIntegrationFunction(int64_t choice) { | |
| 17 |
4/5✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
20 | switch (choice) { |
| 18 | case 0: | ||
| 19 | return [](const std::vector<double> &values) { | ||
| 20 | double sum = 0.0; | ||
| 21 |
2/2✓ Branch 0 taken 85612 times.
✓ Branch 1 taken 44808 times.
|
130420 | for (double val : values) { |
| 22 | 85612 | sum += val; | |
| 23 | } | ||
| 24 | return sum; | ||
| 25 | }; | ||
| 26 | case 1: | ||
| 27 | return [](const std::vector<double> &values) { | ||
| 28 | double mult = 1.0; | ||
| 29 |
2/2✓ Branch 0 taken 81608 times.
✓ Branch 1 taken 40804 times.
|
122412 | for (double val : values) { |
| 30 | 81608 | mult *= val; | |
| 31 | } | ||
| 32 | return mult; | ||
| 33 | }; | ||
| 34 | case 2: | ||
| 35 | return [](const std::vector<double> &values) { | ||
| 36 | double sum = 0.0; | ||
| 37 |
2/2✓ Branch 0 taken 1591812 times.
✓ Branch 1 taken 530604 times.
|
2122416 | for (double val : values) { |
| 38 | 1591812 | sum += val * val; | |
| 39 | } | ||
| 40 | return sum; | ||
| 41 | }; | ||
| 42 | case 3: | ||
| 43 | return [](const std::vector<double> &values) { | ||
| 44 | double sum = 0.0; | ||
| 45 |
2/2✓ Branch 0 taken 4004 times.
✓ Branch 1 taken 4004 times.
|
8008 | for (double val : values) { |
| 46 | 4004 | sum += val; | |
| 47 | } | ||
| 48 | 4004 | return std::sin(sum); | |
| 49 | }; | ||
| 50 | default: | ||
| 51 | return [](const std::vector<double> &) { return 0.0; }; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } // namespace | ||
| 55 | |||
| 56 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | SamoylenkoIIntegralTrapezoidTBB::SamoylenkoIIntegralTrapezoidTBB(const InType &in) { |
| 57 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 58 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | GetInput() = in; |
| 59 | 20 | GetOutput() = 0.0; | |
| 60 | 20 | } | |
| 61 | |||
| 62 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | bool SamoylenkoIIntegralTrapezoidTBB::ValidationImpl() { |
| 63 | const auto &in = GetInput(); | ||
| 64 | |||
| 65 |
3/6✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 20 times.
|
20 | if (in.a.empty() || in.a.size() != in.b.size() || in.a.size() != in.n.size()) { |
| 66 | return false; | ||
| 67 | } | ||
| 68 | |||
| 69 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 20 times.
|
56 | for (size_t i = 0; i < in.a.size(); ++i) { |
| 70 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 36 times.
|
36 | if (in.n[i] <= 0 || in.a[i] >= in.b[i]) { |
| 71 | return false; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | 20 | return in.function_choice >= 0 && in.function_choice <= 3; | |
| 76 | } | ||
| 77 | |||
| 78 | 20 | bool SamoylenkoIIntegralTrapezoidTBB::PreProcessingImpl() { | |
| 79 | 20 | GetOutput() = 0.0; | |
| 80 | 20 | return true; | |
| 81 | } | ||
| 82 | |||
| 83 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | bool SamoylenkoIIntegralTrapezoidTBB::RunImpl() { |
| 84 | const auto &in = GetInput(); | ||
| 85 | 20 | const int dimensions = static_cast<int>(in.a.size()); | |
| 86 | 20 | auto integral_function = GetIntegrationFunction(in.function_choice); | |
| 87 | |||
| 88 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<double> h(dimensions); |
| 89 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 20 times.
|
56 | for (int i = 0; i < dimensions; i++) { |
| 90 | 36 | h[i] = (in.b[i] - in.a[i]) / in.n[i]; | |
| 91 | } | ||
| 92 | |||
| 93 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | std::vector<int64_t> dim_sizes(dimensions); |
| 94 | int64_t points = 1; | ||
| 95 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 20 times.
|
56 | for (int i = 0; i < dimensions; i++) { |
| 96 | 36 | dim_sizes[i] = in.n[i] + 1; | |
| 97 | 36 | points *= dim_sizes[i]; | |
| 98 | } | ||
| 99 | |||
| 100 | 40 | double sum = tbb::parallel_reduce(tbb::blocked_range<int64_t>(0, points), 0.0, | |
| 101 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | [&h, &dimensions, &dim_sizes, &in, &integral_function]( |
| 102 | const tbb::blocked_range<int64_t> &range, double local_sum) -> double { | ||
| 103 | 6220 | std::vector<double> current_point(dimensions); | |
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 620220 times.
✓ Branch 1 taken 6220 times.
|
626440 | for (int64_t pnt = range.begin(); pnt != range.end(); ++pnt) { |
| 106 | int64_t rem_index = pnt; | ||
| 107 | int weight = 1; | ||
| 108 | |||
| 109 |
2/2✓ Branch 0 taken 1763036 times.
✓ Branch 1 taken 620220 times.
|
2383256 | for (int dim = 0; dim < dimensions; dim++) { |
| 110 |
2/2✓ Branch 0 taken 1730200 times.
✓ Branch 1 taken 32836 times.
|
1763036 | int dim_coord = static_cast<int>(rem_index % dim_sizes[dim]); |
| 111 | 1763036 | rem_index /= dim_sizes[dim]; | |
| 112 | |||
| 113 |
2/2✓ Branch 0 taken 1730200 times.
✓ Branch 1 taken 32836 times.
|
1763036 | current_point[dim] = in.a[dim] + (dim_coord * h[dim]); |
| 114 | |||
| 115 |
4/4✓ Branch 0 taken 1730200 times.
✓ Branch 1 taken 32836 times.
✓ Branch 2 taken 1697364 times.
✓ Branch 3 taken 32836 times.
|
1763036 | if (dim_coord > 0 && dim_coord < in.n[dim]) { |
| 116 | 1697364 | weight *= 2; | |
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 620220 times.
|
1240440 | local_sum += integral_function(current_point) * weight; |
| 121 | } | ||
| 122 | |||
| 123 | 6220 | return local_sum; | |
| 124 | }, | ||
| 125 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
152 | [](double local_sum1, double local_sum2) { return local_sum1 + local_sum2; }); |
| 126 | |||
| 127 | double h_mult = 1.0; | ||
| 128 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 20 times.
|
56 | for (int i = 0; i < dimensions; i++) { |
| 129 | 36 | h_mult *= h[i]; | |
| 130 | } | ||
| 131 | |||
| 132 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | GetOutput() = sum * (h_mult / std::pow(2.0, dimensions)); |
| 133 | |||
| 134 | 20 | return true; | |
| 135 | } | ||
| 136 | |||
| 137 | 20 | bool SamoylenkoIIntegralTrapezoidTBB::PostProcessingImpl() { | |
| 138 | 20 | return true; | |
| 139 | } | ||
| 140 | |||
| 141 | } // namespace samoylenko_i_integral_trapezoid | ||
| 142 |