| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "tsibareva_e_integral_calculate_trapezoid_method/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <thread> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "tsibareva_e_integral_calculate_trapezoid_method/common/include/common.hpp" | ||
| 9 | #include "util/include/util.hpp" | ||
| 10 | |||
| 11 | namespace tsibareva_e_integral_calculate_trapezoid_method { | ||
| 12 | |||
| 13 | 48 | TsibarevaEIntegralCalculateTrapezoidMethodSTL::TsibarevaEIntegralCalculateTrapezoidMethodSTL(const InType &in) | |
| 14 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | : BaseTask() { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | GetInput() = in; |
| 17 | 48 | GetOutput() = 0.0; | |
| 18 | 48 | } | |
| 19 | |||
| 20 | 48 | bool TsibarevaEIntegralCalculateTrapezoidMethodSTL::ValidationImpl() { | |
| 21 | 48 | return true; | |
| 22 | } | ||
| 23 | |||
| 24 | 48 | bool TsibarevaEIntegralCalculateTrapezoidMethodSTL::PreProcessingImpl() { | |
| 25 | 48 | GetOutput() = 0.0; | |
| 26 | 48 | return true; | |
| 27 | } | ||
| 28 | |||
| 29 | 48 | bool TsibarevaEIntegralCalculateTrapezoidMethodSTL::RunImpl() { | |
| 30 | 48 | int dim = GetInput().dim; | |
| 31 | |||
| 32 | 48 | std::vector<double> h(dim); | |
| 33 |
1/4✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
48 | std::vector<int> sizes(dim); |
| 34 | 48 | int total_nodes = 1; | |
| 35 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
|
144 | for (int i = 0; i < dim; ++i) { |
| 36 | 96 | h[i] = (GetInput().hi[i] - GetInput().lo[i]) / GetInput().steps[i]; | |
| 37 | 96 | sizes[i] = GetInput().steps[i] + 1; | |
| 38 | 96 | total_nodes *= sizes[i]; | |
| 39 | } | ||
| 40 | |||
| 41 |
5/6✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 42 times.
✓ Branch 5 taken 30 times.
✓ Branch 6 taken 18 times.
|
54 | const int num_threads = std::max<int>(1, std::min<int>(ppc::util::GetNumThreads(), total_nodes)); |
| 42 |
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); |
| 43 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | std::vector<double> partial_sums(num_threads, 0.0); |
| 44 | |||
| 45 | 108 | auto worker = [&](int thread_id, int start, int end) { MWork(thread_id, start, end, sizes, h, partial_sums, dim); }; | |
| 46 | |||
| 47 | 48 | int nodes_per_thread = total_nodes / num_threads; | |
| 48 | 48 | int remainder_nodes = total_nodes % num_threads; | |
| 49 | 48 | int start = 0; | |
| 50 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 48 times.
|
156 | for (int tid = 0; tid < num_threads; ++tid) { |
| 51 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 32 times.
|
108 | int end = start + nodes_per_thread + (tid < remainder_nodes ? 1 : 0); |
| 52 |
2/6✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
108 | threads[tid] = std::thread(worker, tid, start, end); |
| 53 | 108 | start = end; | |
| 54 | } | ||
| 55 | |||
| 56 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 48 times.
|
156 | for (auto &t : threads) { |
| 57 |
1/2✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
|
108 | t.join(); |
| 58 | } | ||
| 59 | |||
| 60 | double global_sum = 0.0; | ||
| 61 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 48 times.
|
156 | for (double s : partial_sums) { |
| 62 | 108 | global_sum += s; | |
| 63 | } | ||
| 64 | |||
| 65 | double res_h = 1.0; | ||
| 66 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
|
144 | for (int i = 0; i < dim; ++i) { |
| 67 | 96 | res_h *= h[i]; | |
| 68 | } | ||
| 69 | |||
| 70 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | GetOutput() = global_sum * res_h; |
| 71 | 48 | return true; | |
| 72 | 48 | } | |
| 73 | |||
| 74 | 48 | bool TsibarevaEIntegralCalculateTrapezoidMethodSTL::PostProcessingImpl() { | |
| 75 | 48 | return true; | |
| 76 | } | ||
| 77 | |||
| 78 | 108 | void TsibarevaEIntegralCalculateTrapezoidMethodSTL::MWork(int thread_id, int start, int end, | |
| 79 | const std::vector<int> &sizes, const std::vector<double> &h, | ||
| 80 | std::vector<double> &partial_sums, int dim) { | ||
| 81 | double local_sum = 0.0; | ||
| 82 | |||
| 83 |
2/2✓ Branch 0 taken 1715968 times.
✓ Branch 1 taken 108 times.
|
1716076 | for (int node = start; node < end; ++node) { |
| 84 | int remainder = node; | ||
| 85 | double node_weight = 1.0; | ||
| 86 | 1715968 | std::vector<double> point(dim); | |
| 87 | |||
| 88 |
2/2✓ Branch 0 taken 5044496 times.
✓ Branch 1 taken 1715968 times.
|
6760464 | for (int i = dim - 1; i >= 0; --i) { |
| 89 |
2/2✓ Branch 0 taken 4939120 times.
✓ Branch 1 taken 105376 times.
|
5044496 | int idx = remainder % sizes[i]; |
| 90 | 5044496 | remainder /= sizes[i]; | |
| 91 | |||
| 92 |
4/4✓ Branch 0 taken 4939120 times.
✓ Branch 1 taken 105376 times.
✓ Branch 2 taken 105376 times.
✓ Branch 3 taken 4833744 times.
|
5044496 | if (idx == 0 || idx == GetInput().steps[i]) { |
| 93 | 210752 | node_weight *= 0.5; | |
| 94 | } | ||
| 95 | |||
| 96 | 5044496 | point[i] = GetInput().lo[i] + (idx * h[i]); | |
| 97 | } | ||
| 98 | |||
| 99 |
2/2✓ Branch 0 taken 1715960 times.
✓ Branch 1 taken 8 times.
|
1715968 | local_sum += node_weight * GetInput().f(point); |
| 100 | } | ||
| 101 | 108 | partial_sums[thread_id] = local_sum; | |
| 102 | 108 | } | |
| 103 | |||
| 104 | } // namespace tsibareva_e_integral_calculate_trapezoid_method | ||
| 105 |