| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "sabirov_s_monte_carlo/tbb/include/ops_tbb.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <random> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "oneapi/tbb/blocked_range.h" | ||
| 10 | #include "oneapi/tbb/global_control.h" | ||
| 11 | #include "oneapi/tbb/parallel_reduce.h" | ||
| 12 | #include "sabirov_s_monte_carlo/common/include/common.hpp" | ||
| 13 | #include "util/include/util.hpp" | ||
| 14 | |||
| 15 | namespace sabirov_s_monte_carlo { | ||
| 16 | |||
| 17 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
52 | SabirovSMonteCarloTBB::SabirovSMonteCarloTBB(const InType &in) { |
| 18 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 19 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
52 | GetInput() = in; |
| 20 | 52 | GetOutput() = 0.0; | |
| 21 | 52 | } | |
| 22 | |||
| 23 |
1/2✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
|
52 | bool SabirovSMonteCarloTBB::ValidationImpl() { |
| 24 | const auto &in = GetInput(); | ||
| 25 |
2/4✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
|
52 | if (in.lower.size() != in.upper.size() || in.lower.empty()) { |
| 26 | return false; | ||
| 27 | } | ||
| 28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
|
52 | if (in.num_samples <= 0) { |
| 29 | return false; | ||
| 30 | } | ||
| 31 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 52 times.
|
132 | for (size_t i = 0; i < in.lower.size(); ++i) { |
| 32 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
|
80 | if (in.lower[i] >= in.upper[i]) { |
| 33 | return false; | ||
| 34 | } | ||
| 35 | } | ||
| 36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
|
52 | if (in.func_type < FuncType::kLinear || in.func_type > FuncType::kQuarticSum) { |
| 37 | return false; | ||
| 38 | } | ||
| 39 | constexpr size_t kMaxDimensions = 10; | ||
| 40 | 52 | return in.lower.size() <= kMaxDimensions; | |
| 41 | } | ||
| 42 | |||
| 43 | 52 | bool SabirovSMonteCarloTBB::PreProcessingImpl() { | |
| 44 | const auto &in = GetInput(); | ||
| 45 | 52 | lower_ = in.lower; | |
| 46 | 52 | upper_ = in.upper; | |
| 47 | 52 | num_samples_ = in.num_samples; | |
| 48 | 52 | func_type_ = in.func_type; | |
| 49 | 52 | GetOutput() = 0.0; | |
| 50 | 52 | return true; | |
| 51 | } | ||
| 52 | |||
| 53 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
52 | bool SabirovSMonteCarloTBB::RunImpl() { |
| 54 | 52 | const int dims = static_cast<int>(lower_.size()); | |
| 55 | |||
| 56 | 52 | std::vector<std::uniform_real_distribution<double>> dists; | |
| 57 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
52 | dists.reserve(static_cast<size_t>(dims)); |
| 58 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 52 times.
|
132 | for (int j = 0; j < dims; ++j) { |
| 59 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | dists.emplace_back(lower_[j], upper_[j]); |
| 60 | } | ||
| 61 | |||
| 62 | double volume = 1.0; | ||
| 63 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 52 times.
|
132 | for (int j = 0; j < dims; ++j) { |
| 64 | 80 | volume *= (upper_[j] - lower_[j]); | |
| 65 | } | ||
| 66 | |||
| 67 | 52 | const FuncType ftype = func_type_; | |
| 68 | 52 | const int n_samples = num_samples_; | |
| 69 | |||
| 70 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
52 | const size_t max_threads = static_cast<size_t>(std::max(1, ppc::util::GetNumThreads())); |
| 71 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
52 | oneapi::tbb::global_control gc(oneapi::tbb::global_control::max_allowed_parallelism, max_threads); |
| 72 | |||
| 73 | 104 | const double sum = tbb::parallel_reduce(tbb::blocked_range<int>(0, n_samples), 0.0, | |
| 74 |
1/2✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
|
52 | [&](const tbb::blocked_range<int> &r, double init) { |
| 75 | double local = init; | ||
| 76 |
1/2✓ Branch 2 taken 16664 times.
✗ Branch 3 not taken.
|
16664 | std::vector<double> point(static_cast<size_t>(dims)); |
| 77 | 16664 | std::seed_seq seed{static_cast<uint32_t>(r.begin()), static_cast<uint32_t>(r.end()), | |
| 78 |
1/4✓ Branch 1 taken 16664 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
16664 | static_cast<uint32_t>(n_samples)}; |
| 79 | std::mt19937 gen(seed); | ||
| 80 |
2/2✓ Branch 0 taken 1680000 times.
✓ Branch 1 taken 16664 times.
|
1696664 | for (int i = r.begin(); i != r.end(); ++i) { |
| 81 |
2/2✓ Branch 0 taken 3480000 times.
✓ Branch 1 taken 1680000 times.
|
5160000 | for (int j = 0; j < dims; ++j) { |
| 82 | 3480000 | point[static_cast<size_t>(j)] = dists[static_cast<size_t>(j)](gen); | |
| 83 | } | ||
| 84 | 1680000 | local += detail::EvaluateAt(ftype, point); | |
| 85 | } | ||
| 86 | 16664 | return local; | |
| 87 | 520 | }, [](double a, double b) { return a + b; }); | |
| 88 | |||
| 89 | 52 | GetOutput() = volume * sum / static_cast<double>(num_samples_); | |
| 90 | 52 | return true; | |
| 91 | } | ||
| 92 | |||
| 93 | 52 | bool SabirovSMonteCarloTBB::PostProcessingImpl() { | |
| 94 | 52 | return true; | |
| 95 | } | ||
| 96 | |||
| 97 | } // namespace sabirov_s_monte_carlo | ||
| 98 |