| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "chernykh_s_trapezoidal_integration/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <functional> | ||
| 7 | #include <thread> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "chernykh_s_trapezoidal_integration/common/include/common.hpp" | ||
| 12 | #include "util/include/util.hpp" | ||
| 13 | |||
| 14 | namespace chernykh_s_trapezoidal_integration { | ||
| 15 | |||
| 16 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | ChernykhSTrapezoidalIntegrationSTL::ChernykhSTrapezoidalIntegrationSTL(const InType &in) { |
| 17 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 18 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | GetInput() = in; |
| 19 | 48 | GetOutput() = 0.0; | |
| 20 | 48 | } | |
| 21 | |||
| 22 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | bool ChernykhSTrapezoidalIntegrationSTL::ValidationImpl() { |
| 23 | const auto &input = this->GetInput(); | ||
| 24 |
2/4✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
|
48 | if (input.limits.empty() || input.limits.size() != input.steps.size()) { |
| 25 | return false; | ||
| 26 | } | ||
| 27 | return std::ranges::all_of(input.steps, [](int s) { return s > 0; }); | ||
| 28 | } | ||
| 29 | |||
| 30 | 48 | bool ChernykhSTrapezoidalIntegrationSTL::PreProcessingImpl() { | |
| 31 | 48 | return true; | |
| 32 | } | ||
| 33 | |||
| 34 | 120 | void ChernykhSTrapezoidalIntegrationSTL::MemberWorker(int64_t start, int64_t end, double &local_result, | |
| 35 | const std::vector<double> &h, int dims) { | ||
| 36 | const auto &input = GetInput(); | ||
| 37 | double sum = 0.0; | ||
| 38 | 120 | std::vector<double> point(dims); | |
| 39 | |||
| 40 |
2/2✓ Branch 0 taken 117088 times.
✓ Branch 1 taken 120 times.
|
117208 | for (int64_t i = start; i < end; i++) { |
| 41 | int64_t local_index = i; | ||
| 42 | double weight = 1.0; | ||
| 43 | |||
| 44 |
2/2✓ Branch 0 taken 219992 times.
✓ Branch 1 taken 117088 times.
|
337080 | for (int j = dims - 1; j >= 0; j--) { |
| 45 |
2/2✓ Branch 0 taken 215440 times.
✓ Branch 1 taken 4552 times.
|
219992 | int64_t idx = local_index % (input.steps[j] + 1); |
| 46 | 219992 | local_index /= (input.steps[j] + 1); | |
| 47 | |||
| 48 |
2/2✓ Branch 0 taken 215440 times.
✓ Branch 1 taken 4552 times.
|
219992 | point[j] = input.limits[j].first + (static_cast<double>(idx) * h[j]); |
| 49 | |||
| 50 |
4/4✓ Branch 0 taken 215440 times.
✓ Branch 1 taken 4552 times.
✓ Branch 2 taken 4552 times.
✓ Branch 3 taken 210888 times.
|
219992 | if (idx == 0 || idx == input.steps[j]) { |
| 51 | 9104 | weight *= 0.5; | |
| 52 | } | ||
| 53 | } | ||
| 54 | 117088 | sum += input.func(point) * weight; | |
| 55 | } | ||
| 56 |
1/2✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
|
120 | local_result = sum; |
| 57 | 120 | } | |
| 58 | |||
| 59 | 48 | bool ChernykhSTrapezoidalIntegrationSTL::RunImpl() { | |
| 60 | auto &input = GetInput(); | ||
| 61 | 48 | int dims = static_cast<int>(input.limits.size()); | |
| 62 | int64_t total_point = 1; | ||
| 63 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 48 times.
|
120 | for (int setka : input.steps) { |
| 64 | 72 | total_point *= static_cast<int64_t>(setka) + 1; | |
| 65 | } | ||
| 66 | |||
| 67 | 48 | std::vector<double> h(dims); | |
| 68 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 48 times.
|
120 | for (int i = 0; i < dims; ++i) { |
| 69 | 72 | h[i] = (input.limits[i].second - input.limits[i].first) / static_cast<double>(input.steps[i]); | |
| 70 | } | ||
| 71 | |||
| 72 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | int num_threads = ppc::util::GetNumThreads(); |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (num_threads == 0) { |
| 74 | num_threads = 1; | ||
| 75 | } | ||
| 76 | |||
| 77 |
1/4✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
48 | std::vector<double> result(num_threads, 0.0); |
| 78 |
1/4✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
48 | std::vector<std::thread> threads(num_threads); |
| 79 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<std::pair<int64_t, int64_t>> borders(num_threads); |
| 80 | |||
| 81 | 48 | int64_t points_per_thread = total_point / num_threads; | |
| 82 | 48 | int64_t remainder = total_point % num_threads; | |
| 83 | |||
| 84 | int64_t start = 0; | ||
| 85 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 48 times.
|
168 | for (int i = 0; i < num_threads; i++) { |
| 86 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 70 times.
|
120 | borders[i].first = start; |
| 87 | 120 | start += points_per_thread; | |
| 88 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 70 times.
|
120 | if (i < remainder) { |
| 89 | 50 | start++; | |
| 90 | } | ||
| 91 | 120 | borders[i].second = start; | |
| 92 | } | ||
| 93 | |||
| 94 | 120 | auto work_function = [&](int64_t s, int64_t e, double &res) { MemberWorker(s, e, res, h, dims); }; | |
| 95 | |||
| 96 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 48 times.
|
168 | for (int i = 0; i < num_threads; ++i) { |
| 97 |
1/4✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
240 | threads[i] = std::thread(work_function, borders[i].first, borders[i].second, std::ref(result[i])); |
| 98 | } | ||
| 99 | |||
| 100 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 48 times.
|
168 | for (int i = 0; i < num_threads; ++i) { |
| 101 |
1/2✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
|
120 | threads[i].join(); |
| 102 | } | ||
| 103 | |||
| 104 | double output_result = 0.0; | ||
| 105 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 48 times.
|
168 | for (double loc_res : result) { |
| 106 | 120 | output_result += loc_res; | |
| 107 | } | ||
| 108 | |||
| 109 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 48 times.
|
120 | for (int i = 0; i < dims; i++) { |
| 110 | 72 | output_result *= h[i]; | |
| 111 | } | ||
| 112 | |||
| 113 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | GetOutput() = output_result; |
| 114 | 48 | return true; | |
| 115 | 48 | } | |
| 116 | |||
| 117 | 48 | bool ChernykhSTrapezoidalIntegrationSTL::PostProcessingImpl() { | |
| 118 | 48 | return true; | |
| 119 | } | ||
| 120 | |||
| 121 | } // namespace chernykh_s_trapezoidal_integration | ||
| 122 |