| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "kosolapov_v_calc_mult_integrals_m_rectangles/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <future> | ||
| 7 | #include <tuple> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "kosolapov_v_calc_mult_integrals_m_rectangles/common/include/common.hpp" | ||
| 11 | #include "util/include/util.hpp" | ||
| 12 | |||
| 13 | namespace kosolapov_v_calc_mult_integrals_m_rectangles { | ||
| 14 | |||
| 15 | 64 | KosolapovVCalcMultIntegralsMRectanglesSTL::KosolapovVCalcMultIntegralsMRectanglesSTL(const InType &in) { | |
| 16 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 17 | 64 | GetInput() = InType(in); | |
| 18 | GetOutput() = 0.0; | ||
| 19 | 64 | } | |
| 20 | |||
| 21 | 64 | bool KosolapovVCalcMultIntegralsMRectanglesSTL::ValidationImpl() { | |
| 22 | 64 | int steps = std::get<0>(GetInput()); | |
| 23 | 64 | int func_id = std::get<1>(GetInput()); | |
| 24 |
2/4✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
|
64 | return steps > 0 && func_id >= 1 && func_id <= 4; |
| 25 | } | ||
| 26 | |||
| 27 | 64 | bool KosolapovVCalcMultIntegralsMRectanglesSTL::PreProcessingImpl() { | |
| 28 | 64 | return true; | |
| 29 | } | ||
| 30 | |||
| 31 | 64 | bool KosolapovVCalcMultIntegralsMRectanglesSTL::RunImpl() { | |
| 32 | 64 | int steps = std::get<0>(GetInput()); | |
| 33 | 64 | int func_id = std::get<1>(GetInput()); | |
| 34 | 64 | std::tuple<double, double, double, double> temp = GetBounds(func_id); | |
| 35 | 64 | double a = std::get<0>(temp); | |
| 36 | 64 | double b = std::get<1>(temp); | |
| 37 | 64 | double c = std::get<2>(temp); | |
| 38 | 64 | double d = std::get<3>(temp); | |
| 39 | 64 | double integral = RectanglesIntegral(func_id, steps, a, b, c, d); | |
| 40 | 64 | GetOutput() = integral; | |
| 41 | 64 | return true; | |
| 42 | } | ||
| 43 | |||
| 44 | 64 | bool KosolapovVCalcMultIntegralsMRectanglesSTL::PostProcessingImpl() { | |
| 45 | 64 | return true; | |
| 46 | } | ||
| 47 | |||
| 48 | ✗ | double KosolapovVCalcMultIntegralsMRectanglesSTL::Function1(double x, double y) { | |
| 49 | // f(x,y) = x^2 + y^2 | ||
| 50 | 16000 | return (x * x) + (y * y); | |
| 51 | } | ||
| 52 | ✗ | double KosolapovVCalcMultIntegralsMRectanglesSTL::Function2(double x, double y) { | |
| 53 | // f(x,y) = sin(x) * cos(y) | ||
| 54 | 16000 | return std::sin(x) * std::cos(y); | |
| 55 | } | ||
| 56 | ✗ | double KosolapovVCalcMultIntegralsMRectanglesSTL::Function3(double x, double y) { | |
| 57 | // f(x,y) = exp(-(x^2 + y^2)) | ||
| 58 | 16000 | return std::exp(-((x * x) + (y * y))); | |
| 59 | } | ||
| 60 | ✗ | double KosolapovVCalcMultIntegralsMRectanglesSTL::Function4(double x, double y) { | |
| 61 | // f(x,y) = sin(x + y) | ||
| 62 | 16000 | return std::sin(x + y); | |
| 63 | } | ||
| 64 | 64000 | double KosolapovVCalcMultIntegralsMRectanglesSTL::CallFunction(int func_id, double x, double y) { | |
| 65 |
4/5✓ Branch 0 taken 16000 times.
✓ Branch 1 taken 16000 times.
✓ Branch 2 taken 16000 times.
✓ Branch 3 taken 16000 times.
✗ Branch 4 not taken.
|
64000 | switch (func_id) { |
| 66 | case 1: | ||
| 67 | 16000 | return Function1(x, y); | |
| 68 | case 2: | ||
| 69 | 16000 | return Function2(x, y); | |
| 70 | case 3: | ||
| 71 | 16000 | return Function3(x, y); | |
| 72 | case 4: | ||
| 73 | 16000 | return Function4(x, y); | |
| 74 | default: | ||
| 75 | ✗ | return Function1(x, y); | |
| 76 | } | ||
| 77 | } | ||
| 78 | 64 | std::tuple<double, double, double, double> KosolapovVCalcMultIntegralsMRectanglesSTL::GetBounds(int func_id) { | |
| 79 |
4/5✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
|
64 | switch (func_id) { |
| 80 | 16 | case 1: | |
| 81 | return {0.0, 1.0, 0.0, 1.0}; | ||
| 82 | 16 | case 2: | |
| 83 | return {0.0, kPi, 0.0, kPi / 2.0}; | ||
| 84 | 16 | case 3: | |
| 85 | return {-1.0, 1.0, -1.0, 1.0}; | ||
| 86 | 16 | case 4: | |
| 87 | return {0.0, kPi, 0.0, kPi}; | ||
| 88 | ✗ | default: | |
| 89 | return {0.0, 1.0, 0.0, 1.0}; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | 64 | double KosolapovVCalcMultIntegralsMRectanglesSTL::RectanglesIntegral(int func_id, int steps, double a, double b, | |
| 93 | double c, double d) { | ||
| 94 | 64 | double hx = (b - a) / steps; | |
| 95 | 64 | double hy = (d - c) / steps; | |
| 96 | 64 | size_t total = static_cast<size_t>(steps) * steps; | |
| 97 | 64 | unsigned int num_threads = ppc::util::GetNumThreads(); | |
| 98 | 64 | size_t chunk_size = (total + num_threads - 1) / num_threads; | |
| 99 | 64 | std::vector<std::future<double>> futures; | |
| 100 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | futures.reserve(num_threads); |
| 101 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 64 times.
|
224 | for (unsigned int thread_idx = 0; thread_idx < num_threads; thread_idx++) { |
| 102 | 160 | size_t start = thread_idx * chunk_size; | |
| 103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
|
160 | size_t end = std::min(start + chunk_size, total); |
| 104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
|
160 | if (start >= end) { |
| 105 | ✗ | continue; | |
| 106 | } | ||
| 107 |
1/2✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
|
320 | futures.push_back(std::async(std::launch::async, [=]() -> double { |
| 108 | double local_sum = 0.0; | ||
| 109 |
2/2✓ Branch 0 taken 64000 times.
✓ Branch 1 taken 160 times.
|
64160 | for (size_t idx = start; idx < end; idx++) { |
| 110 | 64000 | int i = static_cast<int>(idx / steps); | |
| 111 | 64000 | int j = static_cast<int>(idx % steps); | |
| 112 | 64000 | double x = a + ((i + 0.5) * hx); | |
| 113 | 64000 | double y = c + ((j + 0.5) * hy); | |
| 114 | 64000 | local_sum += CallFunction(func_id, x, y); | |
| 115 | } | ||
| 116 | 160 | return local_sum; | |
| 117 | })); | ||
| 118 | } | ||
| 119 | double sum = 0.0; | ||
| 120 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 64 times.
|
224 | for (auto &fut : futures) { |
| 121 |
1/2✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
|
160 | sum += fut.get(); |
| 122 | } | ||
| 123 | 64 | return sum * hx * hy; | |
| 124 | 64 | } | |
| 125 | |||
| 126 | } // namespace kosolapov_v_calc_mult_integrals_m_rectangles | ||
| 127 |