| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "savva_d_monte_carlo/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <random> | ||
| 7 | #include <thread> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "savva_d_monte_carlo/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace savva_d_monte_carlo { | ||
| 14 | |||
| 15 |
1/2✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
|
136 | SavvaDMonteCarloSTL::SavvaDMonteCarloSTL(const InType &in) { |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 |
1/2✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
|
136 | GetInput() = in; |
| 18 | 136 | GetOutput() = 0.0; | |
| 19 | 136 | } | |
| 20 | |||
| 21 | 136 | bool SavvaDMonteCarloSTL::ValidationImpl() { | |
| 22 | const auto &input = GetInput(); | ||
| 23 | |||
| 24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (input.count_points == 0) { |
| 25 | return false; | ||
| 26 | } | ||
| 27 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (!input.f) { |
| 28 | return false; | ||
| 29 | } | ||
| 30 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 136 times.
|
136 | if (input.Dimension() == 0) { |
| 31 | return false; | ||
| 32 | } | ||
| 33 | |||
| 34 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 136 times.
|
472 | for (size_t i = 0; i < input.Dimension(); ++i) { |
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (input.lower_bounds[i] > input.upper_bounds[i]) { |
| 36 | return false; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | return true; | ||
| 41 | } | ||
| 42 | |||
| 43 | 136 | bool SavvaDMonteCarloSTL::PreProcessingImpl() { | |
| 44 | 136 | return true; | |
| 45 | } | ||
| 46 | |||
| 47 |
1/2✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
|
136 | bool SavvaDMonteCarloSTL::RunImpl() { |
| 48 | const auto &input = GetInput(); | ||
| 49 | auto &result = GetOutput(); | ||
| 50 | |||
| 51 |
1/2✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
|
136 | const size_t dim = input.Dimension(); |
| 52 | const double vol = input.Volume(); | ||
| 53 | 136 | const auto n = static_cast<int64_t>(input.count_points); | |
| 54 | 136 | const auto &func = input.f; | |
| 55 | |||
| 56 | 136 | unsigned int num_threads = std::thread::hardware_concurrency(); | |
| 57 | |||
| 58 |
2/4✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
|
272 | num_threads = std::max(1U, std::min<unsigned int>(static_cast<unsigned int>(n), num_threads)); |
| 59 | |||
| 60 | 136 | std::vector<double> partial_sums(num_threads, 0.0); | |
| 61 | 136 | std::vector<std::thread> threads; | |
| 62 |
1/2✓ Branch 1 taken 136 times.
✗ Branch 2 not taken.
|
136 | threads.reserve(num_threads); |
| 63 | |||
| 64 | 136 | const int64_t points_per_thread = n / num_threads; | |
| 65 | 136 | const int64_t tail = n % num_threads; | |
| 66 | |||
| 67 | 544 | auto worker = [&](int thread_id, int64_t pts) { | |
| 68 |
1/2✓ Branch 1 taken 544 times.
✗ Branch 2 not taken.
|
544 | std::minstd_rand gen(1337 + thread_id); |
| 69 | |||
| 70 | 544 | std::vector<std::uniform_real_distribution<double>> dists; | |
| 71 |
1/2✓ Branch 1 taken 544 times.
✗ Branch 2 not taken.
|
544 | dists.reserve(dim); |
| 72 |
2/2✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 544 times.
|
1888 | for (size_t i = 0; i < dim; ++i) { |
| 73 |
1/2✓ Branch 1 taken 1344 times.
✗ Branch 2 not taken.
|
1344 | dists.emplace_back(input.lower_bounds[i], input.upper_bounds[i]); |
| 74 | } | ||
| 75 | |||
| 76 |
1/4✓ Branch 1 taken 544 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
544 | std::vector<double> point(dim); |
| 77 | |||
| 78 | double local_sum = 0.0; | ||
| 79 | |||
| 80 |
2/2✓ Branch 0 taken 120808000 times.
✓ Branch 1 taken 544 times.
|
120808544 | for (int64_t i = 0; i < pts; ++i) { |
| 81 |
2/2✓ Branch 0 taken 305616000 times.
✓ Branch 1 taken 120808000 times.
|
426424000 | for (size_t j = 0; j < dim; ++j) { |
| 82 | 305616000 | point[j] = dists[j](gen); | |
| 83 | } | ||
| 84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 120808000 times.
|
241616000 | local_sum += func(point); |
| 85 | } | ||
| 86 | |||
| 87 |
1/2✓ Branch 0 taken 544 times.
✗ Branch 1 not taken.
|
544 | partial_sums[thread_id] = local_sum; |
| 88 | 544 | }; | |
| 89 | |||
| 90 |
2/2✓ Branch 0 taken 544 times.
✓ Branch 1 taken 136 times.
|
680 | for (unsigned int i = 0; i < num_threads; ++i) { |
| 91 | 544 | int64_t pts = points_per_thread + (std::cmp_less(i, tail) ? 1 : 0); | |
| 92 |
1/2✓ Branch 1 taken 544 times.
✗ Branch 2 not taken.
|
544 | threads.emplace_back(worker, i, pts); |
| 93 | } | ||
| 94 | |||
| 95 |
2/2✓ Branch 0 taken 544 times.
✓ Branch 1 taken 136 times.
|
680 | for (auto &t : threads) { |
| 96 |
1/2✓ Branch 1 taken 544 times.
✗ Branch 2 not taken.
|
544 | t.join(); |
| 97 | } | ||
| 98 | |||
| 99 | double total_sum = 0.0; | ||
| 100 |
2/2✓ Branch 0 taken 544 times.
✓ Branch 1 taken 136 times.
|
680 | for (double s : partial_sums) { |
| 101 | 544 | total_sum += s; | |
| 102 | } | ||
| 103 | |||
| 104 | 136 | result = vol * total_sum / static_cast<double>(n); | |
| 105 | 136 | return true; | |
| 106 | 136 | } | |
| 107 | |||
| 108 | 136 | bool SavvaDMonteCarloSTL::PostProcessingImpl() { | |
| 109 | 136 | return true; | |
| 110 | } | ||
| 111 | |||
| 112 | } // namespace savva_d_monte_carlo | ||
| 113 |