| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "bortsova_a_integrals_rectangle/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstdint> | ||
| 5 | #include <thread> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "bortsova_a_integrals_rectangle/common/include/common.hpp" | ||
| 9 | #include "util/include/util.hpp" | ||
| 10 | |||
| 11 | namespace bortsova_a_integrals_rectangle { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | BortsovaAIntegralsRectangleSTL::BortsovaAIntegralsRectangleSTL(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | GetInput() = in; |
| 16 | 80 | GetOutput() = 0.0; | |
| 17 | 80 | } | |
| 18 | |||
| 19 |
1/2✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
|
80 | bool BortsovaAIntegralsRectangleSTL::ValidationImpl() { |
| 20 | const auto &input = GetInput(); | ||
| 21 |
3/6✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 80 times.
|
80 | return input.func && !input.lower_bounds.empty() && input.lower_bounds.size() == input.upper_bounds.size() && |
| 22 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | input.num_steps > 0; |
| 23 | } | ||
| 24 | |||
| 25 | 80 | bool BortsovaAIntegralsRectangleSTL::PreProcessingImpl() { | |
| 26 | const auto &input = GetInput(); | ||
| 27 | 80 | func_ = input.func; | |
| 28 | 80 | num_steps_ = input.num_steps; | |
| 29 | 80 | dims_ = static_cast<int>(input.lower_bounds.size()); | |
| 30 | |||
| 31 | 80 | midpoints_.resize(dims_); | |
| 32 | 80 | volume_ = 1.0; | |
| 33 | 80 | total_points_ = 1; | |
| 34 | |||
| 35 |
2/2✓ Branch 0 taken 136 times.
✓ Branch 1 taken 80 times.
|
216 | for (int di = 0; di < dims_; di++) { |
| 36 | 136 | double step = (input.upper_bounds[di] - input.lower_bounds[di]) / static_cast<double>(num_steps_); | |
| 37 | 136 | volume_ *= step; | |
| 38 | 136 | total_points_ *= num_steps_; | |
| 39 | |||
| 40 | 136 | midpoints_[di].resize(num_steps_); | |
| 41 |
2/2✓ Branch 0 taken 23368 times.
✓ Branch 1 taken 136 times.
|
23504 | for (int si = 0; si < num_steps_; si++) { |
| 42 | 23368 | midpoints_[di][si] = input.lower_bounds[di] + ((si + 0.5) * step); | |
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | 80 | return true; | |
| 47 | } | ||
| 48 | |||
| 49 | 200 | double BortsovaAIntegralsRectangleSTL::ComputePartialSum(int64_t begin, int64_t end) { | |
| 50 | 200 | std::vector<int> indices(dims_, 0); | |
| 51 |
1/4✓ Branch 1 taken 200 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
200 | std::vector<double> point(dims_); |
| 52 | |||
| 53 | int64_t temp = begin; | ||
| 54 |
2/2✓ Branch 0 taken 340 times.
✓ Branch 1 taken 200 times.
|
540 | for (int di = dims_ - 1; di >= 0; di--) { |
| 55 | 340 | indices[di] = static_cast<int>(temp % num_steps_); | |
| 56 | 340 | temp /= num_steps_; | |
| 57 | } | ||
| 58 | |||
| 59 | double local_sum = 0.0; | ||
| 60 |
2/2✓ Branch 0 taken 385608 times.
✓ Branch 1 taken 200 times.
|
385808 | for (int64_t pt = begin; pt < end; pt++) { |
| 61 |
2/2✓ Branch 0 taken 881608 times.
✓ Branch 1 taken 385608 times.
|
1267216 | for (int di = 0; di < dims_; di++) { |
| 62 | 881608 | point[di] = midpoints_[di][indices[di]]; | |
| 63 | } | ||
| 64 | 385608 | local_sum += func_(point); | |
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 394728 times.
✓ Branch 1 taken 80 times.
|
394808 | for (int di = dims_ - 1; di >= 0; di--) { |
| 67 |
2/2✓ Branch 0 taken 9200 times.
✓ Branch 1 taken 385528 times.
|
394728 | indices[di]++; |
| 68 |
2/2✓ Branch 0 taken 9200 times.
✓ Branch 1 taken 385528 times.
|
394728 | if (indices[di] < num_steps_) { |
| 69 | break; | ||
| 70 | } | ||
| 71 | 9200 | indices[di] = 0; | |
| 72 | } | ||
| 73 | } | ||
| 74 | 200 | return local_sum; | |
| 75 | } | ||
| 76 | |||
| 77 | 80 | bool BortsovaAIntegralsRectangleSTL::RunImpl() { | |
| 78 | 80 | int num_threads = ppc::util::GetNumThreads(); | |
| 79 | 80 | std::vector<double> partial_sums(num_threads, 0.0); | |
| 80 | |||
| 81 | 80 | int64_t chunk = total_points_ / num_threads; | |
| 82 | 80 | int64_t remainder = total_points_ % num_threads; | |
| 83 | |||
| 84 |
1/4✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
80 | std::vector<std::thread> threads(num_threads - 1); |
| 85 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 80 times.
|
200 | for (int ti = 1; ti < num_threads; ti++) { |
| 86 |
2/2✓ Branch 0 taken 116 times.
✓ Branch 1 taken 4 times.
|
120 | int64_t begin = (ti * chunk) + std::min(static_cast<int64_t>(ti), remainder); |
| 87 |
2/2✓ Branch 0 taken 116 times.
✓ Branch 1 taken 4 times.
|
120 | int64_t end = begin + chunk + (static_cast<int64_t>(ti) < remainder ? 1 : 0); |
| 88 |
2/4✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 120 times.
|
240 | threads[ti - 1] = std::thread([&, ti, begin, end]() { partial_sums[ti] = ComputePartialSum(begin, end); }); |
| 89 | } | ||
| 90 | |||
| 91 |
3/4✓ Branch 0 taken 56 times.
✓ Branch 1 taken 24 times.
✓ Branch 3 taken 80 times.
✗ Branch 4 not taken.
|
136 | partial_sums[0] = ComputePartialSum(0, chunk + (remainder > 0 ? 1 : 0)); |
| 92 | |||
| 93 | double sum = partial_sums[0]; | ||
| 94 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 80 times.
|
200 | for (int ti = 1; ti < num_threads; ti++) { |
| 95 |
1/2✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
|
120 | threads[ti - 1].join(); |
| 96 | 120 | sum += partial_sums[ti]; | |
| 97 | } | ||
| 98 | |||
| 99 | 80 | GetOutput() = sum * volume_; | |
| 100 | 80 | return true; | |
| 101 | 80 | } | |
| 102 | |||
| 103 | 80 | bool BortsovaAIntegralsRectangleSTL::PostProcessingImpl() { | |
| 104 | 80 | return true; | |
| 105 | } | ||
| 106 | |||
| 107 | } // namespace bortsova_a_integrals_rectangle | ||
| 108 |