| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "vlasova_a_simpson_method/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <numeric> | ||
| 6 | #include <thread> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "util/include/util.hpp" | ||
| 11 | #include "vlasova_a_simpson_method/common/include/common.hpp" | ||
| 12 | |||
| 13 | namespace vlasova_a_simpson_method { | ||
| 14 | |||
| 15 | 176 | VlasovaASimpsonMethodSTL::VlasovaASimpsonMethodSTL(InType in) : task_data_(std::move(in)) { | |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 | 176 | GetOutput() = 0.0; | |
| 18 | 176 | } | |
| 19 | |||
| 20 |
1/2✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
|
176 | bool VlasovaASimpsonMethodSTL::ValidationImpl() { |
| 21 | size_t dim = task_data_.a.size(); | ||
| 22 | |||
| 23 |
3/6✓ Branch 0 taken 176 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 176 times.
|
176 | if (dim == 0 || dim != task_data_.b.size() || dim != task_data_.n.size()) { |
| 24 | return false; | ||
| 25 | } | ||
| 26 | |||
| 27 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 176 times.
|
512 | for (size_t i = 0; i < dim; ++i) { |
| 28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
|
336 | if (task_data_.a[i] >= task_data_.b[i]) { |
| 29 | return false; | ||
| 30 | } | ||
| 31 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 336 times.
|
336 | if (task_data_.n[i] <= 0 || task_data_.n[i] % 2 != 0) { |
| 32 | return false; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
|
176 | if (!task_data_.func) { |
| 37 | return false; | ||
| 38 | } | ||
| 39 | |||
| 40 | 176 | return GetOutput() == 0.0; | |
| 41 | } | ||
| 42 | |||
| 43 | 176 | bool VlasovaASimpsonMethodSTL::PreProcessingImpl() { | |
| 44 | 176 | result_ = 0.0; | |
| 45 | 176 | GetOutput() = 0.0; | |
| 46 | |||
| 47 | size_t dim = task_data_.a.size(); | ||
| 48 | 176 | h_.resize(dim); | |
| 49 | 176 | dimensions_.resize(dim); | |
| 50 | |||
| 51 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 176 times.
|
512 | for (size_t i = 0; i < dim; ++i) { |
| 52 | 336 | h_[i] = (task_data_.b[i] - task_data_.a[i]) / task_data_.n[i]; | |
| 53 | 336 | dimensions_[i] = task_data_.n[i] + 1; | |
| 54 | } | ||
| 55 | |||
| 56 | 176 | return true; | |
| 57 | } | ||
| 58 | |||
| 59 | 1241968 | void VlasovaASimpsonMethodSTL::ComputeWeight(const std::vector<int> &index, double &weight) const { | |
| 60 | 1241968 | weight = 1.0; | |
| 61 | size_t dim = index.size(); | ||
| 62 | |||
| 63 |
2/2✓ Branch 0 taken 3714160 times.
✓ Branch 1 taken 1241968 times.
|
4956128 | for (size_t i = 0; i < dim; ++i) { |
| 64 | 3714160 | int idx = index[i]; | |
| 65 | 3714160 | int steps = task_data_.n[i]; | |
| 66 | |||
| 67 |
2/2✓ Branch 0 taken 3530384 times.
✓ Branch 1 taken 183776 times.
|
3714160 | if (idx == 0 || idx == steps) { |
| 68 | weight *= 1.0; | ||
| 69 |
2/2✓ Branch 0 taken 1719248 times.
✓ Branch 1 taken 1811136 times.
|
3530384 | } else if (idx % 2 == 0) { |
| 70 | 1719248 | weight *= 2.0; | |
| 71 | } else { | ||
| 72 | 1811136 | weight *= 4.0; | |
| 73 | } | ||
| 74 | } | ||
| 75 | 1241968 | } | |
| 76 | |||
| 77 | 1241968 | void VlasovaASimpsonMethodSTL::ComputePoint(const std::vector<int> &index, std::vector<double> &point) const { | |
| 78 | size_t dim = index.size(); | ||
| 79 | 1241968 | point.resize(dim); | |
| 80 | |||
| 81 |
2/2✓ Branch 0 taken 3714160 times.
✓ Branch 1 taken 1241968 times.
|
4956128 | for (size_t i = 0; i < dim; ++i) { |
| 82 | 3714160 | point[i] = task_data_.a[i] + (index[i] * h_[i]); | |
| 83 | } | ||
| 84 | 1241968 | } | |
| 85 | |||
| 86 | 20 | void VlasovaASimpsonMethodSTL::ComputePartialSumRange(int start_idx, int end_idx, double &partial_sum) const { | |
| 87 | size_t dim = task_data_.a.size(); | ||
| 88 | 20 | std::vector<int> cur_index(dim, 0); | |
| 89 | 20 | std::vector<double> cur_point; | |
| 90 | double local_sum = 0.0; | ||
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 1061208 times.
✓ Branch 1 taken 20 times.
|
1061228 | for (int idx = start_idx; idx < end_idx; ++idx) { |
| 93 | 1061208 | auto temp_idx = static_cast<size_t>(idx); | |
| 94 |
2/2✓ Branch 0 taken 3183624 times.
✓ Branch 1 taken 1061208 times.
|
4244832 | for (size_t i = 0; i < dim; ++i) { |
| 95 | 3183624 | cur_index[i] = static_cast<int>(temp_idx % static_cast<size_t>(dimensions_[i])); | |
| 96 | 3183624 | temp_idx /= static_cast<size_t>(dimensions_[i]); | |
| 97 | } | ||
| 98 | |||
| 99 | 1061208 | double weight = 0.0; | |
| 100 | 1061208 | ComputeWeight(cur_index, weight); | |
| 101 |
1/2✓ Branch 1 taken 1061208 times.
✗ Branch 2 not taken.
|
1061208 | ComputePoint(cur_index, cur_point); |
| 102 | 1061208 | local_sum += weight * task_data_.func(cur_point); | |
| 103 | } | ||
| 104 | |||
| 105 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | partial_sum = local_sum; |
| 106 | 20 | } | |
| 107 | |||
| 108 | 168 | void VlasovaASimpsonMethodSTL::ProcessSequential(double &sum) const { | |
| 109 | size_t dim = task_data_.a.size(); | ||
| 110 | 168 | std::vector<int> cur_index(dim, 0); | |
| 111 | 168 | std::vector<double> cur_point; | |
| 112 | 168 | sum = 0.0; | |
| 113 | |||
| 114 | size_t total_points = 1; | ||
| 115 |
2/2✓ Branch 0 taken 312 times.
✓ Branch 1 taken 168 times.
|
480 | for (size_t i = 0; i < dim; ++i) { |
| 116 | 312 | total_points *= static_cast<size_t>(dimensions_[i]); | |
| 117 | } | ||
| 118 | |||
| 119 |
2/2✓ Branch 0 taken 180760 times.
✓ Branch 1 taken 168 times.
|
180928 | for (size_t idx = 0; idx < total_points; ++idx) { |
| 120 | auto temp_idx = idx; | ||
| 121 |
2/2✓ Branch 0 taken 530536 times.
✓ Branch 1 taken 180760 times.
|
711296 | for (size_t i = 0; i < dim; ++i) { |
| 122 | 530536 | cur_index[i] = static_cast<int>(temp_idx % static_cast<size_t>(dimensions_[i])); | |
| 123 | 530536 | temp_idx /= static_cast<size_t>(dimensions_[i]); | |
| 124 | } | ||
| 125 | |||
| 126 | 180760 | double weight = 0.0; | |
| 127 | 180760 | ComputeWeight(cur_index, weight); | |
| 128 |
1/2✓ Branch 1 taken 180760 times.
✗ Branch 2 not taken.
|
180760 | ComputePoint(cur_index, cur_point); |
| 129 | 180760 | sum += weight * task_data_.func(cur_point); | |
| 130 | } | ||
| 131 | 168 | } | |
| 132 | |||
| 133 | 8 | void VlasovaASimpsonMethodSTL::ProcessParallel(double &sum) const { | |
| 134 | size_t dim = task_data_.a.size(); | ||
| 135 | |||
| 136 | size_t total_points = 1; | ||
| 137 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
|
32 | for (size_t i = 0; i < dim; ++i) { |
| 138 | 24 | total_points *= static_cast<size_t>(dimensions_[i]); | |
| 139 | } | ||
| 140 | |||
| 141 | 8 | unsigned int num_threads = ppc::util::GetNumThreads(); | |
| 142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (num_threads == 0) { |
| 143 | ✗ | num_threads = std::thread::hardware_concurrency(); | |
| 144 | } | ||
| 145 | ✗ | if (num_threads == 0) { | |
| 146 | num_threads = 2; | ||
| 147 | } | ||
| 148 | |||
| 149 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
|
8 | if (static_cast<size_t>(num_threads) > total_points) { |
| 150 | ✗ | num_threads = static_cast<unsigned int>(total_points); | |
| 151 | } | ||
| 152 | |||
| 153 | 8 | size_t points_per_thread = total_points / num_threads; | |
| 154 | 8 | size_t remainder = total_points % num_threads; | |
| 155 | |||
| 156 | 8 | std::vector<std::thread> threads; | |
| 157 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | std::vector<double> partial_sums(num_threads, 0.0); |
| 158 | |||
| 159 | size_t start_idx = 0; | ||
| 160 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8 times.
|
28 | for (unsigned int thread_idx = 0; thread_idx < num_threads; ++thread_idx) { |
| 161 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 8 times.
|
20 | size_t end_idx = start_idx + points_per_thread + (thread_idx < remainder ? 1 : 0); |
| 162 |
1/4✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
20 | threads.emplace_back([this, start_idx, end_idx, &partial_sums, thread_idx]() { |
| 163 | 20 | double local_sum = 0.0; | |
| 164 | 20 | ComputePartialSumRange(static_cast<int>(start_idx), static_cast<int>(end_idx), local_sum); | |
| 165 | 20 | partial_sums[thread_idx] = local_sum; | |
| 166 | 20 | }); | |
| 167 | start_idx = end_idx; | ||
| 168 | } | ||
| 169 | |||
| 170 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 8 times.
|
28 | for (auto &thread : threads) { |
| 171 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | if (thread.joinable()) { |
| 172 |
1/2✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
|
20 | thread.join(); |
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | sum = std::accumulate(partial_sums.begin(), partial_sums.end(), 0.0); |
| 177 | 8 | } | |
| 178 | |||
| 179 | 176 | bool VlasovaASimpsonMethodSTL::RunImpl() { | |
| 180 | size_t dim = task_data_.a.size(); | ||
| 181 | |||
| 182 | size_t total_points = 1; | ||
| 183 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 176 times.
|
512 | for (size_t i = 0; i < dim; ++i) { |
| 184 | 336 | total_points *= static_cast<size_t>(dimensions_[i]); | |
| 185 | } | ||
| 186 | |||
| 187 | 176 | double sum = 0.0; | |
| 188 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 8 times.
|
176 | if (total_points < 10000) { |
| 189 | 168 | ProcessSequential(sum); | |
| 190 | } else { | ||
| 191 | 8 | ProcessParallel(sum); | |
| 192 | } | ||
| 193 | |||
| 194 | double factor = 1.0; | ||
| 195 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 176 times.
|
512 | for (size_t i = 0; i < dim; ++i) { |
| 196 | 336 | factor *= h_[i] / 3.0; | |
| 197 | } | ||
| 198 | |||
| 199 | 176 | result_ = sum * factor; | |
| 200 | 176 | GetOutput() = result_; | |
| 201 | |||
| 202 | 176 | return true; | |
| 203 | } | ||
| 204 | |||
| 205 | 176 | bool VlasovaASimpsonMethodSTL::PostProcessingImpl() { | |
| 206 | 176 | return std::isfinite(GetOutput()); | |
| 207 | } | ||
| 208 | |||
| 209 | } // namespace vlasova_a_simpson_method | ||
| 210 |