| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "telnov_counting_the_frequency/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <chrono> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <string> | ||
| 9 | |||
| 10 | #include "telnov_counting_the_frequency/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace telnov_counting_the_frequency { | ||
| 13 | |||
| 14 | 6 | TelnovCountingTheFrequencyMPI::TelnovCountingTheFrequencyMPI(const InType &in) { | |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 | 6 | GetInput() = in; | |
| 17 | GetOutput() = 0; | ||
| 18 | 6 | } | |
| 19 | |||
| 20 | 6 | bool TelnovCountingTheFrequencyMPI::ValidationImpl() { | |
| 21 |
2/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 | return (GetInput() > 0) && (GetOutput() == 0); |
| 22 | } | ||
| 23 | |||
| 24 | 6 | bool TelnovCountingTheFrequencyMPI::PreProcessingImpl() { | |
| 25 | 6 | GetOutput() = 0; | |
| 26 | 6 | return true; | |
| 27 | } | ||
| 28 | |||
| 29 | 6 | bool TelnovCountingTheFrequencyMPI::RunImpl() { | |
| 30 | 6 | int rank = 0; | |
| 31 | 6 | int size = 0; | |
| 32 | 6 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 33 | 6 | MPI_Comm_size(MPI_COMM_WORLD, &size); | |
| 34 | |||
| 35 | 6 | uint64_t n = 0; | |
| 36 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank == 0) { |
| 37 | 3 | n = static_cast<uint64_t>(GlobalData::g_data_string.size()); | |
| 38 | } | ||
| 39 | // Броадкаст размера (используем фиксированный тип uint64_t) | ||
| 40 | 6 | MPI_Bcast(&n, 1, MPI_UINT64_T, /*root=*/0, MPI_COMM_WORLD); | |
| 41 | |||
| 42 | // Подготовим буфер на всех рангах | ||
| 43 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if (rank != 0) { |
| 44 | GlobalData::g_data_string.clear(); | ||
| 45 | 3 | GlobalData::g_data_string.resize(static_cast<size_t>(n), '\0'); | |
| 46 | } | ||
| 47 | |||
| 48 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if (n > 0) { |
| 49 | // Передаём данные | ||
| 50 | 6 | MPI_Bcast(const_cast<char *>(GlobalData::g_data_string.data()), static_cast<int>(n), MPI_CHAR, /*root=*/0, | |
| 51 | MPI_COMM_WORLD); | ||
| 52 | } | ||
| 53 | |||
| 54 | // Теперь все ранги имеют одинаковую g_data_string | ||
| 55 | const std::string &s = GlobalData::g_data_string; | ||
| 56 | size_t total_length = s.size(); | ||
| 57 | |||
| 58 | // Разбиение работы между ранги | ||
| 59 | 6 | size_t chunk = total_length / static_cast<size_t>(size); | |
| 60 | 6 | size_t start = static_cast<size_t>(rank) * chunk; | |
| 61 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | size_t end = (rank == size - 1 ? total_length : start + chunk); |
| 62 | |||
| 63 | 6 | int64_t local = 0; | |
| 64 |
2/2✓ Branch 0 taken 6000000 times.
✓ Branch 1 taken 6 times.
|
6000006 | for (size_t i = start; i < end; i++) { |
| 65 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 5999985 times.
|
6000000 | if (s[i] == 'X') { |
| 66 | 15 | local++; | |
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | 6 | int64_t total = 0; | |
| 71 | |||
| 72 | // Сложим локальные счётчики | ||
| 73 | 6 | MPI_Allreduce(&local, &total, 1, MPI_INT64_T, MPI_SUM, MPI_COMM_WORLD); | |
| 74 | |||
| 75 | 6 | GetOutput() = static_cast<int>(total); | |
| 76 | |||
| 77 | using Clock = std::chrono::high_resolution_clock; | ||
| 78 | 6 | auto delay_start = Clock::now(); | |
| 79 |
2/2✓ Branch 1 taken 142249 times.
✓ Branch 2 taken 6 times.
|
142255 | while (std::chrono::duration<double>(Clock::now() - delay_start).count() < 0.001) { |
| 80 | } | ||
| 81 | |||
| 82 | 6 | return true; | |
| 83 | } | ||
| 84 | |||
| 85 | 6 | bool TelnovCountingTheFrequencyMPI::PostProcessingImpl() { | |
| 86 | 6 | return GetOutput() == GetInput(); | |
| 87 | } | ||
| 88 | |||
| 89 | } // namespace telnov_counting_the_frequency | ||
| 90 |