GCC Code Coverage Report


Directory: ./
File: tasks/volkov_a_sparse_mat_mul_ccs/omp/src/ops_omp.cpp
Date: 2026-05-11 08:26:31
Exec Total Coverage
Lines: 25 25 100.0%
Functions: 5 5 100.0%
Branches: 7 12 58.3%

Line Branch Exec Source
1 #include "volkov_a_sparse_mat_mul_ccs/omp/include/ops_omp.hpp"
2
3 #include <omp.h>
4
5 #include <cmath>
6 #include <tuple>
7 #include <vector>
8
9 #include "volkov_a_sparse_mat_mul_ccs/common/include/common.hpp"
10
11 namespace volkov_a_sparse_mat_mul_ccs {
12
13
1/2
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 VolkovASparseMatMulCcsOmp::VolkovASparseMatMulCcsOmp(const InType &in) {
14 SetTypeOfTask(GetStaticTypeOfTask());
15 GetInput() = in;
16 20 }
17
18 20 bool VolkovASparseMatMulCcsOmp::ValidationImpl() {
19 const auto &matrix_a = std::get<0>(GetInput());
20 const auto &matrix_b = std::get<1>(GetInput());
21 20 return (matrix_a.cols_count == matrix_b.rows_count);
22 }
23
24 20 bool VolkovASparseMatMulCcsOmp::PreProcessingImpl() {
25 20 return true;
26 }
27
28 20 bool VolkovASparseMatMulCcsOmp::RunImpl() {
29 const auto &matrix_a = std::get<0>(GetInput());
30 const auto &matrix_b = std::get<1>(GetInput());
31 auto &matrix_c = GetOutput();
32
33 20 matrix_c.rows_count = matrix_a.rows_count;
34 20 matrix_c.cols_count = matrix_b.cols_count;
35 20 matrix_c.col_ptrs.assign(matrix_c.cols_count + 1, 0);
36
37 // Временные массивы для хранения результатов каждого столбца до их слияния
38 20 std::vector<std::vector<int>> cols_res_rows(matrix_b.cols_count);
39
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 std::vector<std::vector<double>> cols_res_values(matrix_b.cols_count);
40
41 // Параллельный регион. Всё, что объявлено внутри, становится локальным для потока!
42 20 #pragma omp parallel default(none) shared(matrix_a, matrix_b, cols_res_rows, cols_res_values)
43 {
44 // Изящный подход: потоколокальный аккумулятор.
45 // Никаких omp_get_thread_num() и сложной индексации!
46 std::vector<double> local_accumulator(matrix_a.rows_count, 0.0);
47
48 // Распределяем итерации по потокам (dynamic лучше для разреженных матриц)
49 #pragma omp for schedule(dynamic)
50 for (int j = 0; j < matrix_b.cols_count; ++j) {
51 int b_start = matrix_b.col_ptrs[j];
52 int b_end = matrix_b.col_ptrs[j + 1];
53
54 for (int k = b_start; k < b_end; ++k) {
55 int b_row = matrix_b.row_indices[k];
56 double b_val = matrix_b.values[k];
57
58 int a_start = matrix_a.col_ptrs[b_row];
59 int a_end = matrix_a.col_ptrs[b_row + 1];
60
61 for (int idx = a_start; idx < a_end; ++idx) {
62 int a_row = matrix_a.row_indices[idx];
63 double a_val = matrix_a.values[idx];
64
65 local_accumulator[a_row] += a_val * b_val;
66 }
67 }
68
69 // Переписываем ненулевые элементы из локального аккумулятора в вектор столбца
70 for (int i = 0; i < matrix_a.rows_count; ++i) {
71 if (std::abs(local_accumulator[i]) > 1e-10) {
72 cols_res_rows[j].push_back(i);
73 cols_res_values[j].push_back(local_accumulator[i]);
74 }
75 // Сразу обнуляем для вычисления следующего столбца в этом же потоке
76 local_accumulator[i] = 0.0;
77 }
78 }
79 }
80
81 // Однопоточное вычисление префиксных сумм для col_ptrs
82 int current_non_zeros = 0;
83
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 20 times.
56 for (int j = 0; j < matrix_b.cols_count; ++j) {
84 36 matrix_c.col_ptrs[j] = current_non_zeros;
85 36 current_non_zeros += static_cast<int>(cols_res_values[j].size());
86 }
87
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 matrix_c.col_ptrs[matrix_b.cols_count] = current_non_zeros;
88 20 matrix_c.non_zeros = current_non_zeros;
89
90
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 matrix_c.row_indices.resize(current_non_zeros);
91
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 matrix_c.values.resize(current_non_zeros);
92
93 // Параллельное копирование временных данных в итоговый одномерный массив формата CCS
94 20 #pragma omp parallel for default(none) schedule(static) shared(matrix_b, matrix_c, cols_res_rows, cols_res_values)
95 for (int j = 0; j < matrix_b.cols_count; ++j) {
96 int offset = matrix_c.col_ptrs[j];
97 int elements_in_col = static_cast<int>(cols_res_values[j].size());
98 for (int k = 0; k < elements_in_col; ++k) {
99 matrix_c.row_indices[offset + k] = cols_res_rows[j][k];
100 matrix_c.values[offset + k] = cols_res_values[j][k];
101 }
102 }
103
104 20 return true;
105 20 }
106
107 20 bool VolkovASparseMatMulCcsOmp::PostProcessingImpl() {
108 20 return true;
109 }
110
111 } // namespace volkov_a_sparse_mat_mul_ccs
112