| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "pylaeva_s_inc_contrast_img_by_lsh/all/include/ops_all.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cmath> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <cstdint> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | #include "pylaeva_s_inc_contrast_img_by_lsh/common/include/common.hpp" | ||
| 13 | |||
| 14 | namespace pylaeva_s_inc_contrast_img_by_lsh { | ||
| 15 | |||
| 16 |
1/2✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
|
34 | PylaevaSIncContrastImgByLshALL::PylaevaSIncContrastImgByLshALL(const InType &in) { |
| 17 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 18 |
1/2✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
|
34 | GetInput() = in; |
| 19 | GetOutput().clear(); | ||
| 20 | 34 | } | |
| 21 | |||
| 22 | 34 | bool PylaevaSIncContrastImgByLshALL::ValidationImpl() { | |
| 23 | 34 | bool status = true; | |
| 24 | 34 | int rank = 0; | |
| 25 | 34 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 26 | |||
| 27 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
|
34 | if (rank == 0) { |
| 28 | 17 | status = !GetInput().empty(); | |
| 29 | } | ||
| 30 | |||
| 31 | 34 | MPI_Bcast(&status, 1, MPI_C_BOOL, 0, MPI_COMM_WORLD); | |
| 32 | 34 | return status; | |
| 33 | } | ||
| 34 | |||
| 35 | 34 | bool PylaevaSIncContrastImgByLshALL::PreProcessingImpl() { | |
| 36 | 34 | int rank = 0; | |
| 37 | 34 | int n = 0; | |
| 38 | |||
| 39 | 34 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 40 | 34 | MPI_Comm_size(MPI_COMM_WORLD, &n); | |
| 41 | |||
| 42 | 34 | size_t size = 0; | |
| 43 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
|
34 | if (rank == 0) { |
| 44 | 17 | size = GetInput().size(); | |
| 45 | } | ||
| 46 | 34 | MPI_Bcast(&size, 1, MPI_UNSIGNED_LONG_LONG, 0, MPI_COMM_WORLD); | |
| 47 | 34 | GetOutput().resize(size); | |
| 48 | |||
| 49 | 34 | size_t base_chunk = size / n; | |
| 50 | 34 | size_t remainder = size % n; | |
| 51 | |||
| 52 |
1/2✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
|
34 | local_size_ = base_chunk + (std::cmp_less(rank, remainder) ? 1 : 0); |
| 53 | |||
| 54 | 34 | recv_counts_.resize(n); | |
| 55 | 34 | recv_displs_.resize(n); | |
| 56 | |||
| 57 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
|
34 | if (rank == 0) { |
| 58 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 17 times.
|
51 | for (int i = 0; i < n; i++) { |
| 59 | 34 | recv_counts_[i] = static_cast<int>(base_chunk + (std::cmp_less(i, remainder) ? 1 : 0)); | |
| 60 | } | ||
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
|
34 | for (int i = 1; i < n; i++) { |
| 63 | 17 | recv_displs_[i] = recv_displs_[i - 1] + recv_counts_[i - 1]; | |
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | 34 | MPI_Bcast(recv_counts_.data(), n, MPI_INT, 0, MPI_COMM_WORLD); | |
| 68 | 34 | MPI_Bcast(recv_displs_.data(), n, MPI_INT, 0, MPI_COMM_WORLD); | |
| 69 | |||
| 70 | 34 | local_data_.resize(local_size_); | |
| 71 | |||
| 72 | 34 | MPI_Scatterv(rank == 0 ? GetInput().data() : nullptr, recv_counts_.data(), recv_displs_.data(), MPI_UNSIGNED_CHAR, | |
| 73 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
|
34 | local_data_.data(), static_cast<int>(local_size_), MPI_UNSIGNED_CHAR, 0, MPI_COMM_WORLD); |
| 74 | |||
| 75 | 34 | local_out_ = OutType(local_size_); | |
| 76 | 34 | return !GetOutput().empty(); | |
| 77 | } | ||
| 78 | |||
| 79 | 34 | bool PylaevaSIncContrastImgByLshALL::RunImpl() { | |
| 80 | 34 | uint8_t loc_min_v = 255; | |
| 81 | 34 | uint8_t loc_max_v = 0; | |
| 82 | |||
| 83 | 34 | #pragma omp parallel for default(none) reduction(min : loc_min_v) reduction(max : loc_max_v) | |
| 84 | for (size_t i = 0; i < local_size_; i++) { | ||
| 85 | loc_min_v = std::min(loc_min_v, local_data_[i]); | ||
| 86 | loc_max_v = std::max(loc_max_v, local_data_[i]); | ||
| 87 | } | ||
| 88 | |||
| 89 | 34 | uint8_t global_min = 255; | |
| 90 | 34 | uint8_t global_max = 0; | |
| 91 | |||
| 92 | 34 | MPI_Allreduce(static_cast<void *>(&loc_min_v), static_cast<void *>(&global_min), 1, MPI_UNSIGNED_CHAR, MPI_MIN, | |
| 93 | MPI_COMM_WORLD); | ||
| 94 | 34 | MPI_Allreduce(static_cast<void *>(&loc_max_v), static_cast<void *>(&global_max), 1, MPI_UNSIGNED_CHAR, MPI_MAX, | |
| 95 | MPI_COMM_WORLD); | ||
| 96 | |||
| 97 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 22 times.
|
34 | if (global_min == global_max) { |
| 98 | 12 | #pragma omp parallel for default(none) | |
| 99 | for (size_t i = 0; i < local_size_; i++) { | ||
| 100 | local_out_[i] = local_data_[i]; | ||
| 101 | } | ||
| 102 | 12 | return true; | |
| 103 | } | ||
| 104 | |||
| 105 | 22 | const int diff = global_max - global_min; | |
| 106 | 22 | const double scale = 255.0 / diff; | |
| 107 | |||
| 108 | 22 | #pragma omp parallel for default(none) shared(global_min, scale) | |
| 109 | for (size_t i = 0; i < local_size_; i++) { | ||
| 110 | int pixel = local_data_[i]; | ||
| 111 | int result = static_cast<int>(std::lround((pixel - global_min) * scale)); | ||
| 112 | local_out_[i] = static_cast<uint8_t>(std::clamp(result, 0, 255)); | ||
| 113 | } | ||
| 114 | |||
| 115 | 22 | return true; | |
| 116 | } | ||
| 117 | |||
| 118 | 34 | bool PylaevaSIncContrastImgByLshALL::PostProcessingImpl() { | |
| 119 | 34 | MPI_Allgatherv(local_out_.data(), static_cast<int>(local_size_), MPI_UNSIGNED_CHAR, GetOutput().data(), | |
| 120 | recv_counts_.data(), recv_displs_.data(), MPI_UNSIGNED_CHAR, MPI_COMM_WORLD); | ||
| 121 | 34 | return true; | |
| 122 | } | ||
| 123 | |||
| 124 | } // namespace pylaeva_s_inc_contrast_img_by_lsh | ||
| 125 |