GCC Code Coverage Report


Directory: ./
File: tasks/yakimov_i_multiplication_of_sparse_matrices_crs_storage_format/seq/src/ops_seq.cpp
Date: 2026-01-27 01:59:34
Exec Total Coverage
Lines: 83 85 97.6%
Functions: 11 11 100.0%
Branches: 45 66 68.2%

Line Branch Exec Source
1 #include "yakimov_i_multiplication_of_sparse_matrices_crs_storage_format/seq/include/ops_seq.hpp"
2
3 #include <algorithm>
4 #include <cstddef>
5 #include <fstream>
6 #include <iostream>
7 #include <string>
8 #include <vector>
9
10 #include "util/include/util.hpp"
11 #include "yakimov_i_multiplication_of_sparse_matrices_crs_storage_format/common/include/common.hpp"
12
13 namespace yakimov_i_multiplication_of_sparse_matrices_crs_storage_format {
14
15 namespace {
16
17 180 bool ReadDimensions(std::ifstream &file, MatrixCRS &matrix) {
18 bool success = true;
19 180 file >> matrix.rows;
20 180 file >> matrix.cols;
21
22
2/4
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 180 times.
180 if (matrix.rows <= 0 || matrix.cols <= 0) {
23 success = false;
24 }
25
26 180 return success;
27 }
28
29 2260 bool ReadRowData(std::ifstream &file, MatrixCRS &matrix, int row_index) {
30 2260 int nonzeros = 0;
31 2260 file >> nonzeros;
32
33
2/2
✓ Branch 0 taken 5374 times.
✓ Branch 1 taken 2260 times.
7634 for (int j = 0; j < nonzeros; ++j) {
34 5374 int col_idx = 0;
35 5374 file >> col_idx;
36
2/2
✓ Branch 0 taken 4856 times.
✓ Branch 1 taken 518 times.
5374 matrix.col_indices.push_back(col_idx);
37 }
38
39
2/2
✓ Branch 0 taken 5374 times.
✓ Branch 1 taken 2260 times.
7634 for (int j = 0; j < nonzeros; ++j) {
40 5374 double value = 0.0;
41 file >> value;
42
2/2
✓ Branch 0 taken 4856 times.
✓ Branch 1 taken 518 times.
5374 matrix.values.push_back(value);
43 }
44
45 2260 matrix.row_pointers[static_cast<size_t>(row_index) + 1] =
46 2260 matrix.row_pointers[static_cast<size_t>(row_index)] + nonzeros;
47
48 2260 return true;
49 }
50
51 180 bool ReadMatrixFromFileImpl(const std::string &filename, MatrixCRS &matrix) {
52 180 std::ifstream file(filename);
53
54
1/2
✓ Branch 1 taken 180 times.
✗ Branch 2 not taken.
180 bool success = ReadDimensions(file, matrix);
55
56
1/2
✓ Branch 1 taken 180 times.
✗ Branch 2 not taken.
180 matrix.row_pointers.resize(static_cast<size_t>(matrix.rows) + 1);
57 180 matrix.row_pointers[0] = 0;
58
59
2/2
✓ Branch 0 taken 2260 times.
✓ Branch 1 taken 180 times.
2440 for (int i = 0; i < matrix.rows; ++i) {
60
2/4
✓ Branch 0 taken 2260 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2260 times.
✗ Branch 4 not taken.
2260 success = success && ReadRowData(file, matrix, i);
61 }
62
1/2
✓ Branch 1 taken 180 times.
✗ Branch 2 not taken.
180 file.close();
63 180 return success;
64 180 }
65
66
1/2
✓ Branch 0 taken 1122 times.
✗ Branch 1 not taken.
1122 void ProcessRowMultiplication(const MatrixCRS &a, const MatrixCRS &b, int row_index, std::vector<double> &row_values) {
67 std::ranges::fill(row_values, 0.0);
68 1122 int row_start_a = a.row_pointers[static_cast<std::size_t>(row_index)];
69 1122 int row_end_a = a.row_pointers[static_cast<std::size_t>(row_index) + 1];
70
2/2
✓ Branch 0 taken 778 times.
✓ Branch 1 taken 1122 times.
1900 for (int k = row_start_a; k < row_end_a; ++k) {
71 778 int col_a = a.col_indices[static_cast<std::size_t>(k)];
72 778 double val_a = a.values[static_cast<std::size_t>(k)];
73 778 int row_start_b = b.row_pointers[static_cast<std::size_t>(col_a)];
74 778 int row_end_b = b.row_pointers[static_cast<std::size_t>(col_a) + 1];
75
2/2
✓ Branch 0 taken 1152 times.
✓ Branch 1 taken 778 times.
1930 for (int idx = row_start_b; idx < row_end_b; ++idx) {
76 1152 int col_b = b.col_indices[static_cast<std::size_t>(idx)];
77 1152 double val_b = b.values[static_cast<std::size_t>(idx)];
78 1152 row_values[static_cast<std::size_t>(col_b)] += val_a * val_b;
79 }
80 }
81 1122 }
82
83 1122 void CollectRowResult(const std::vector<double> &row_values, MatrixCRS &result, int row_index) {
84
2/2
✓ Branch 0 taken 6074 times.
✓ Branch 1 taken 1122 times.
7196 for (size_t j = 0; j < row_values.size(); ++j) {
85
2/2
✓ Branch 0 taken 992 times.
✓ Branch 1 taken 5082 times.
6074 if (row_values[j] != 0.0) {
86
2/2
✓ Branch 0 taken 752 times.
✓ Branch 1 taken 240 times.
992 result.values.push_back(row_values[j]);
87 992 result.col_indices.push_back(static_cast<int>(j));
88 }
89 }
90
91 1122 result.row_pointers[static_cast<size_t>(row_index) + 1] = static_cast<int>(result.values.size());
92 1122 }
93
94 90 MatrixCRS MultiplyMatricesImpl(const MatrixCRS &a, const MatrixCRS &b) {
95 90 MatrixCRS result;
96
97
2/4
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
90 if (a.rows <= 0 || b.cols <= 0) {
98 return result;
99 }
100
101 90 result.rows = a.rows;
102 90 result.cols = b.cols;
103
1/2
✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
90 result.row_pointers.resize(static_cast<size_t>(result.rows) + 1);
104
105
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 if (!result.row_pointers.empty()) {
106 90 result.row_pointers[0] = 0;
107 }
108
109
1/2
✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
90 std::vector<double> row_values(static_cast<size_t>(result.cols), 0.0);
110
111
2/2
✓ Branch 0 taken 1122 times.
✓ Branch 1 taken 90 times.
1212 for (int i = 0; i < a.rows; ++i) {
112 1122 ProcessRowMultiplication(a, b, i, row_values);
113
1/2
✓ Branch 1 taken 1122 times.
✗ Branch 2 not taken.
1122 CollectRowResult(row_values, result, i);
114 }
115
116 return result;
117 }
118
119 double SumMatrixElementsImpl(const MatrixCRS &matrix) {
120 double sum = 0.0;
121
122
2/2
✓ Branch 0 taken 992 times.
✓ Branch 1 taken 90 times.
1082 for (double value : matrix.values) {
123 992 sum += value;
124 }
125
126 return sum;
127 }
128
129 } // namespace
130
131 90 YakimovIMultiplicationOfSparseMatricesSEQ::YakimovIMultiplicationOfSparseMatricesSEQ(const InType &in) {
132 SetTypeOfTask(GetStaticTypeOfTask());
133 90 GetInput() = in;
134 GetOutput() = 0.0;
135
136
2/4
✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 90 times.
✗ Branch 5 not taken.
180 matrix_A_filename_ = ppc::util::GetAbsoluteTaskPath("yakimov_i_multiplication_of_sparse_matrices_crs_storage_format",
137 180 "A_" + std::to_string(GetInput()) + ".txt");
138
2/4
✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 90 times.
✗ Branch 5 not taken.
180 matrix_B_filename_ = ppc::util::GetAbsoluteTaskPath("yakimov_i_multiplication_of_sparse_matrices_crs_storage_format",
139 180 "B_" + std::to_string(GetInput()) + ".txt");
140 90 }
141
142 90 bool YakimovIMultiplicationOfSparseMatricesSEQ::ValidationImpl() {
143 90 bool input_valid = (GetInput() > 0);
144 90 bool output_valid = (GetOutput() == 0.0);
145 90 return input_valid && output_valid;
146 }
147
148 90 bool YakimovIMultiplicationOfSparseMatricesSEQ::PreProcessingImpl() {
149 bool success = true;
150
151
1/2
✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
90 success = success && ReadMatrixFromFileImpl(matrix_A_filename_, matrix_A_);
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 90 times.
90 success = success && ReadMatrixFromFileImpl(matrix_B_filename_, matrix_B_);
153
154 if (!success) {
155 return false;
156 }
157
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
90 if (matrix_A_.cols != matrix_B_.rows) {
159 return false;
160 }
161 return true;
162 }
163
164 90 bool YakimovIMultiplicationOfSparseMatricesSEQ::RunImpl() {
165 90 result_matrix_ = MultiplyMatricesImpl(matrix_A_, matrix_B_);
166 90 return true;
167 }
168
169 90 bool YakimovIMultiplicationOfSparseMatricesSEQ::PostProcessingImpl() {
170 double sum = SumMatrixElementsImpl(result_matrix_);
171 90 GetOutput() = sum;
172 90 return true;
173 }
174 } // namespace yakimov_i_multiplication_of_sparse_matrices_crs_storage_format
175