| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "ovsyannikov_n_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 "ovsyannikov_n_simpson_method/common/include/common.hpp" | ||
| 11 | #include "util/include/util.hpp" | ||
| 12 | |||
| 13 | namespace ovsyannikov_n_simpson_method { | ||
| 14 | |||
| 15 | ✗ | double OvsyannikovNSimpsonMethodSTL::Function(double x, double y) { | |
| 16 | 68448 | return x + y; | |
| 17 | } | ||
| 18 | |||
| 19 | ✗ | double OvsyannikovNSimpsonMethodSTL::GetCoeff(int i, int n) { | |
| 20 | ✗ | if (i == 0 || i == n) { | |
| 21 | return 1.0; | ||
| 22 | } | ||
| 23 |
4/6✓ Branch 0 taken 752 times.
✓ Branch 1 taken 800 times.
✓ Branch 2 taken 31752 times.
✓ Branch 3 taken 33400 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
66704 | return (i % 2 == 1) ? 4.0 : 2.0; |
| 24 | } | ||
| 25 | |||
| 26 | 48 | OvsyannikovNSimpsonMethodSTL::OvsyannikovNSimpsonMethodSTL(const InType &in) { | |
| 27 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 28 | 48 | GetInput() = in; | |
| 29 | 48 | } | |
| 30 | |||
| 31 | 48 | bool OvsyannikovNSimpsonMethodSTL::ValidationImpl() { | |
| 32 |
4/8✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 48 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 48 times.
|
48 | return GetInput().nx > 0 && GetInput().nx % 2 == 0 && GetInput().ny > 0 && GetInput().ny % 2 == 0; |
| 33 | } | ||
| 34 | |||
| 35 | 48 | bool OvsyannikovNSimpsonMethodSTL::PreProcessingImpl() { | |
| 36 | 48 | params_ = GetInput(); | |
| 37 | 48 | res_ = 0.0; | |
| 38 | 48 | return true; | |
| 39 | } | ||
| 40 | |||
| 41 | 48 | bool OvsyannikovNSimpsonMethodSTL::RunImpl() { | |
| 42 | 48 | const int nx_l = params_.nx; | |
| 43 | 48 | const int ny_l = params_.ny; | |
| 44 | 48 | const double ax_l = params_.ax; | |
| 45 | 48 | const double ay_l = params_.ay; | |
| 46 | 48 | const double hx = (params_.bx - params_.ax) / nx_l; | |
| 47 | 48 | const double hy = (params_.by - params_.ay) / ny_l; | |
| 48 | |||
| 49 | 48 | unsigned int num_threads = ppc::util::GetNumThreads(); | |
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (num_threads == 0) { |
| 51 | num_threads = 2; | ||
| 52 | } | ||
| 53 | |||
| 54 | 48 | std::vector<double> partial_results(num_threads, 0.0); | |
| 55 | 48 | std::vector<std::thread> threads; | |
| 56 |
1/2✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
|
48 | threads.reserve(num_threads); |
| 57 | |||
| 58 | 120 | auto worker = [&](int start_i, int end_i, int thread_idx) { | |
| 59 | double local_sum = 0.0; | ||
| 60 |
2/2✓ Branch 0 taken 1648 times.
✓ Branch 1 taken 120 times.
|
1768 | for (int i = start_i; i < end_i; ++i) { |
| 61 | 1648 | const double x = ax_l + (static_cast<double>(i) * hx); | |
| 62 |
2/2✓ Branch 0 taken 1552 times.
✓ Branch 1 taken 96 times.
|
1648 | const double coeff_x = GetCoeff(i, nx_l); |
| 63 | double row_sum = 0.0; | ||
| 64 |
2/2✓ Branch 0 taken 68448 times.
✓ Branch 1 taken 1648 times.
|
70096 | for (int j = 0; j <= ny_l; ++j) { |
| 65 |
2/2✓ Branch 0 taken 65152 times.
✓ Branch 1 taken 3296 times.
|
68448 | const double y = ay_l + (static_cast<double>(j) * hy); |
| 66 | const double coeff_y = GetCoeff(j, ny_l); | ||
| 67 | 68448 | row_sum += coeff_y * Function(x, y); | |
| 68 | } | ||
| 69 | 1648 | local_sum += coeff_x * row_sum; | |
| 70 | } | ||
| 71 | 120 | partial_results[static_cast<std::size_t>(thread_idx)] = local_sum; | |
| 72 | 168 | }; | |
| 73 | |||
| 74 | 48 | const int total_tasks = nx_l + 1; | |
| 75 | 48 | const int chunk_size = total_tasks / static_cast<int>(num_threads); | |
| 76 | 48 | const int extra = total_tasks % static_cast<int>(num_threads); | |
| 77 | |||
| 78 | 48 | int current_start = 0; | |
| 79 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 48 times.
|
168 | for (unsigned int i_thread = 0; i_thread < num_threads; ++i_thread) { |
| 80 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 56 times.
|
120 | int current_end = current_start + chunk_size + (std::cmp_less(i_thread, extra) ? 1 : 0); |
| 81 |
1/2✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
|
120 | threads.emplace_back(worker, current_start, current_end, static_cast<int>(i_thread)); |
| 82 | 120 | current_start = current_end; | |
| 83 | } | ||
| 84 | |||
| 85 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 48 times.
|
168 | for (auto &th : threads) { |
| 86 |
1/2✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
|
120 | if (th.joinable()) { |
| 87 |
1/2✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
|
120 | th.join(); |
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | 48 | res_ = (hx * hy / 9.0) * std::accumulate(partial_results.begin(), partial_results.end(), 0.0); | |
| 92 | 48 | return true; | |
| 93 | 48 | } | |
| 94 | |||
| 95 | 48 | bool OvsyannikovNSimpsonMethodSTL::PostProcessingImpl() { | |
| 96 | 48 | GetOutput() = res_; | |
| 97 | 48 | return true; | |
| 98 | } | ||
| 99 | |||
| 100 | } // namespace ovsyannikov_n_simpson_method | ||
| 101 |