GCC Code Coverage Report


Directory: ./
File: tasks/potashnik_m_char_freq/mpi/src/ops_mpi.cpp
Date: 2025-12-13 04:24:21
Exec Total Coverage
Lines: 53 53 100.0%
Functions: 5 5 100.0%
Branches: 33 48 68.8%

Line Branch Exec Source
1 #include "potashnik_m_char_freq/mpi/include/ops_mpi.hpp"
2
3 #include <mpi.h>
4
5 #include <string>
6 #include <vector>
7
8 #include "potashnik_m_char_freq/common/include/common.hpp"
9
10 namespace potashnik_m_char_freq {
11
12
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 PotashnikMCharFreqMPI::PotashnikMCharFreqMPI(const InType &in) {
13 SetTypeOfTask(GetStaticTypeOfTask());
14
15 20 int rank = 0;
16
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
17
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (rank == 0) {
18 GetInput() = in;
19 }
20
21 20 GetOutput() = 0;
22 20 }
23
24 20 bool PotashnikMCharFreqMPI::ValidationImpl() {
25 20 int rank = 0;
26 20 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
27
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (rank != 0) {
28 return true;
29 }
30 10 return !std::get<0>(GetInput()).empty();
31 }
32
33 20 bool PotashnikMCharFreqMPI::PreProcessingImpl() {
34 20 return true;
35 }
36
37 20 bool PotashnikMCharFreqMPI::RunImpl() {
38 20 int world_size = 0;
39 20 MPI_Comm_size(MPI_COMM_WORLD, &world_size);
40 20 int rank = 0;
41 20 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
42
43 std::string str;
44 20 char chr = 0;
45 20 int string_size = 0;
46
47
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (rank == 0) {
48 str = std::get<0>(GetInput());
49 10 chr = std::get<1>(GetInput());
50 10 string_size = static_cast<int>(str.size());
51 }
52
53
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 MPI_Bcast(&string_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
54
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 MPI_Bcast(&chr, 1, MPI_CHAR, 0, MPI_COMM_WORLD);
55
56 20 int block_size = string_size / world_size;
57 20 int remainder = string_size % world_size;
58
59
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 std::vector<int> local_sizes(world_size, 0);
60
1/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
20 std::vector<int> local_start_positions(world_size, 0);
61
62
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (rank == 0) {
63
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
30 for (int i = 0; i < world_size; i++) {
64
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 if (remainder > i) {
65 2 local_sizes[i] = block_size + 1;
66 } else {
67 18 local_sizes[i] = block_size;
68 }
69 }
70
71
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 for (int i = 1; i < world_size; i++) {
72 10 local_start_positions[i] = local_start_positions[i - 1] + local_sizes[i - 1];
73 }
74 }
75
76
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 int cur_count = 0;
77
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 MPI_Scatter(local_sizes.data(), 1, MPI_INT, &cur_count, 1, MPI_INT, 0, MPI_COMM_WORLD);
78
79
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 std::string cur_str(cur_count, '\0');
80
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (rank == 0) {
81
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 std::vector<char> temp_str(str.begin(), str.end()); // Dont work without const
82
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 MPI_Scatterv(temp_str.data(), local_sizes.data(), local_start_positions.data(), MPI_CHAR, cur_str.data(), cur_count,
83 MPI_CHAR, 0, MPI_COMM_WORLD);
84 ;
85 } else {
86
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 MPI_Scatterv(nullptr, nullptr, nullptr, MPI_CHAR, cur_str.data(), cur_count, MPI_CHAR, 0,
87 MPI_COMM_WORLD); // Just recieving data
88 }
89
90 20 int cur_res = 0;
91
2/2
✓ Branch 0 taken 38136 times.
✓ Branch 1 taken 20 times.
38156 for (char c : cur_str) {
92
2/2
✓ Branch 0 taken 1466 times.
✓ Branch 1 taken 36670 times.
38136 if (c == chr) {
93 1466 cur_res++;
94 }
95 }
96
97 20 int total_res = 0;
98
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 MPI_Allreduce(&cur_res, &total_res, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
99 20 GetOutput() = total_res;
100
101 20 return true;
102 }
103
104 20 bool PotashnikMCharFreqMPI::PostProcessingImpl() {
105 20 return true;
106 }
107
108 } // namespace potashnik_m_char_freq
109