| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "afanasyev_a_integ_rect_method/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <functional> | ||
| 5 | #include <numeric> | ||
| 6 | #include <thread> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "afanasyev_a_integ_rect_method/common/include/common.hpp" | ||
| 10 | #include "util/include/util.hpp" | ||
| 11 | |||
| 12 | namespace afanasyev_a_integ_rect_method { | ||
| 13 | namespace { | ||
| 14 | |||
| 15 | 4320000 | double ExampleIntegrand(const std::vector<double> &x) { | |
| 16 | double s = 0.0; | ||
| 17 |
2/2✓ Branch 0 taken 12960000 times.
✓ Branch 1 taken 4320000 times.
|
17280000 | for (double xi : x) { |
| 18 | 12960000 | s += xi * xi; | |
| 19 | } | ||
| 20 | 4320000 | return std::exp(-s); | |
| 21 | } | ||
| 22 | |||
| 23 | 60 | void ComputePartialSum(int start, int end, int n, int k_dim, double h, double &local_sum) { | |
| 24 | 60 | std::vector<int> idx(k_dim, 0); | |
| 25 |
1/4✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
60 | std::vector<double> x(k_dim, 0.0); |
| 26 | 60 | local_sum = 0.0; | |
| 27 | |||
| 28 |
2/2✓ Branch 0 taken 960 times.
✓ Branch 1 taken 60 times.
|
1020 | for (int i0 = start; i0 < end; ++i0) { |
| 29 | 960 | idx[0] = i0; | |
| 30 |
2/2✓ Branch 0 taken 59200 times.
✓ Branch 1 taken 960 times.
|
60160 | for (int i1 = 0; i1 < n; ++i1) { |
| 31 | 59200 | idx[1] = i1; | |
| 32 |
2/2✓ Branch 0 taken 4320000 times.
✓ Branch 1 taken 59200 times.
|
4379200 | for (int i2 = 0; i2 < n; ++i2) { |
| 33 | 4320000 | idx[2] = i2; | |
| 34 |
2/2✓ Branch 0 taken 12960000 times.
✓ Branch 1 taken 4320000 times.
|
17280000 | for (int dim = 0; dim < k_dim; ++dim) { |
| 35 | 12960000 | x[dim] = (static_cast<double>(idx[dim]) + 0.5) * h; | |
| 36 | } | ||
| 37 | 4320000 | local_sum += ExampleIntegrand(x); | |
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | 60 | } | |
| 42 | |||
| 43 | } // namespace | ||
| 44 | |||
| 45 | 24 | AfanasyevAIntegRectMethodSTL::AfanasyevAIntegRectMethodSTL(const InType &in) { | |
| 46 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 47 | 24 | GetInput() = in; | |
| 48 | GetOutput() = 0.0; | ||
| 49 | 24 | } | |
| 50 | |||
| 51 | 24 | bool AfanasyevAIntegRectMethodSTL::ValidationImpl() { | |
| 52 | 24 | return (GetInput() > 0); | |
| 53 | } | ||
| 54 | |||
| 55 | 24 | bool AfanasyevAIntegRectMethodSTL::PreProcessingImpl() { | |
| 56 | 24 | return true; | |
| 57 | } | ||
| 58 | |||
| 59 | 24 | bool AfanasyevAIntegRectMethodSTL::RunImpl() { | |
| 60 | 24 | const int n = GetInput(); | |
| 61 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (n <= 0) { |
| 62 | return false; | ||
| 63 | } | ||
| 64 | |||
| 65 | 24 | const int k_dim = 3; | |
| 66 | 24 | const double h = 1.0 / static_cast<double>(n); | |
| 67 | |||
| 68 | 24 | const int num_threads = ppc::util::GetNumThreads(); | |
| 69 | 24 | std::vector<std::thread> threads; | |
| 70 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<double> partial_sums(num_threads, 0.0); |
| 71 | |||
| 72 | 24 | int chunk_size = n / num_threads; | |
| 73 | 24 | int remainder = n % num_threads; | |
| 74 | |||
| 75 | 24 | int start = 0; | |
| 76 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
|
84 | for (int thread_id = 0; thread_id < num_threads; ++thread_id) { |
| 77 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 14 times.
|
60 | int end = start + chunk_size + (thread_id < remainder ? 1 : 0); |
| 78 |
1/4✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
60 | threads.emplace_back(ComputePartialSum, start, end, n, k_dim, h, std::ref(partial_sums[thread_id])); |
| 79 | 60 | start = end; | |
| 80 | } | ||
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
|
84 | for (auto &th : threads) { |
| 83 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | th.join(); |
| 84 | } | ||
| 85 | |||
| 86 | double sum = std::accumulate(partial_sums.begin(), partial_sums.end(), 0.0); | ||
| 87 | const double volume = std::pow(h, k_dim); | ||
| 88 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | GetOutput() = sum * volume; |
| 89 | |||
| 90 | return true; | ||
| 91 | 24 | } | |
| 92 | |||
| 93 | 24 | bool AfanasyevAIntegRectMethodSTL::PostProcessingImpl() { | |
| 94 | 24 | return true; | |
| 95 | } | ||
| 96 | |||
| 97 | } // namespace afanasyev_a_integ_rect_method | ||
| 98 |