| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "maslova_u_char_frequency_count/mpi/include/ops_mpi.hpp" | ||
| 2 | |||
| 3 | #include <mpi.h> | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <climits> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <cstdint> | ||
| 9 | #include <string> | ||
| 10 | #include <utility> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #include "maslova_u_char_frequency_count/common/include/common.hpp" | ||
| 14 | |||
| 15 | namespace maslova_u_char_frequency_count { | ||
| 16 | |||
| 17 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | MaslovaUCharFrequencyCountMPI::MaslovaUCharFrequencyCountMPI(const InType &in) { |
| 18 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 19 | GetInput() = in; | ||
| 20 | 30 | GetOutput() = 0; | |
| 21 | 30 | } | |
| 22 | |||
| 23 | 30 | bool MaslovaUCharFrequencyCountMPI::ValidationImpl() { | |
| 24 | 30 | int rank = 0; | |
| 25 | 30 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
| 26 | 30 | int flag = 0; // 0 - всё ок, 1 - ошибка | |
| 27 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (rank == 0) { |
| 28 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
|
15 | if (GetInput().first.size() > static_cast<size_t>(INT_MAX)) { |
| 29 | ✗ | flag = 1; | |
| 30 | } | ||
| 31 | } | ||
| 32 | 30 | MPI_Bcast(&flag, 1, MPI_INT, 0, MPI_COMM_WORLD); | |
| 33 | 30 | return (flag == 0); | |
| 34 | } | ||
| 35 | |||
| 36 | 30 | bool MaslovaUCharFrequencyCountMPI::PreProcessingImpl() { | |
| 37 | 30 | return true; | |
| 38 | } | ||
| 39 | |||
| 40 | 30 | bool MaslovaUCharFrequencyCountMPI::RunImpl() { | |
| 41 | 30 | int rank = 0; | |
| 42 | 30 | int proc_size = 0; | |
| 43 | 30 | MPI_Comm_rank(MPI_COMM_WORLD, &rank); // id процесса | |
| 44 | 30 | MPI_Comm_size(MPI_COMM_WORLD, &proc_size); // количество процессов | |
| 45 | |||
| 46 | std::string input_string; | ||
| 47 | 30 | char input_char = 0; | |
| 48 | size_t input_str_size = 0; | ||
| 49 | |||
| 50 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (rank == 0) { |
| 51 |
1/2✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
|
15 | input_string = GetInput().first; |
| 52 | 15 | input_char = GetInput().second; | |
| 53 | input_str_size = input_string.size(); // получили данные | ||
| 54 | } | ||
| 55 | |||
| 56 | 30 | uint64_t size_for_mpi = 0; | |
| 57 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (rank == 0) { |
| 58 | 15 | size_for_mpi = static_cast<uint64_t>(input_str_size); // явное приведение перед передачей | |
| 59 | } | ||
| 60 | |||
| 61 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | MPI_Bcast(&size_for_mpi, 1, MPI_UINT64_T, 0, MPI_COMM_WORLD); // отправляем размер строки |
| 62 | |||
| 63 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (rank != 0) { |
| 64 | 15 | input_str_size = static_cast<size_t>(size_for_mpi); // возращаем обратно для удобного использования в дальнейшем | |
| 65 | } | ||
| 66 | |||
| 67 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28 times.
|
30 | if (input_str_size == 0) { |
| 68 | 2 | GetOutput() = 0; // ставим для всех процессов | |
| 69 | 2 | return true; | |
| 70 | } | ||
| 71 | |||
| 72 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | MPI_Bcast(&input_char, 1, MPI_CHAR, 0, MPI_COMM_WORLD); // отправляем нужный символ |
| 73 | |||
| 74 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | std::vector<int> send_counts(proc_size); // здесь размеры всех порций |
| 75 |
1/4✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
28 | std::vector<int> displs(proc_size); // смещения |
| 76 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
|
28 | if (rank == 0) { |
| 77 | 14 | size_t part = input_str_size / proc_size; | |
| 78 | 14 | size_t rem = input_str_size % proc_size; | |
| 79 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | for (size_t i = 0; std::cmp_less(i, proc_size); ++i) { |
| 80 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 4 times.
|
52 | send_counts[i] = static_cast<int>(part + (i < rem ? 1 : 0)); // общий размер, включающий остаток, если он входит |
| 81 | } | ||
| 82 | 14 | displs[0] = 0; | |
| 83 |
1/2✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
|
28 | for (size_t i = 1; std::cmp_less(i, proc_size); ++i) { |
| 84 | 14 | displs[i] = displs[i - 1] + send_counts[i - 1]; | |
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | MPI_Bcast(send_counts.data(), proc_size, MPI_INT, 0, MPI_COMM_WORLD); // отправляем размеры порций |
| 89 |
3/6✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
28 | std::vector<char> local_str(send_counts[rank]); |
| 90 |
3/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14 times.
✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
|
42 | MPI_Scatterv((rank == 0) ? input_string.data() : nullptr, send_counts.data(), displs.data(), MPI_CHAR, |
| 91 | local_str.data(), static_cast<int>(local_str.size()), MPI_CHAR, 0, MPI_COMM_WORLD // распределяем данные | ||
| 92 | ); | ||
| 93 | |||
| 94 | 28 | size_t local_count = std::count(local_str.begin(), local_str.end(), input_char); | |
| 95 | 28 | auto local_count_for_mpi = static_cast<uint64_t>(local_count); | |
| 96 | 28 | uint64_t global_count = 0; | |
| 97 |
1/2✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
|
28 | MPI_Allreduce(&local_count_for_mpi, &global_count, 1, MPI_UINT64_T, MPI_SUM, |
| 98 | MPI_COMM_WORLD); // собрали данные со всех процессов | ||
| 99 | |||
| 100 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 2 times.
|
28 | GetOutput() = static_cast<size_t>(global_count); // вывели результат, при этом приведя его к нужному нам типу |
| 101 | |||
| 102 | return true; | ||
| 103 | } | ||
| 104 | |||
| 105 | 30 | bool MaslovaUCharFrequencyCountMPI::PostProcessingImpl() { | |
| 106 | 30 | return true; | |
| 107 | } | ||
| 108 | |||
| 109 | } // namespace maslova_u_char_frequency_count | ||
| 110 |