Skip to content
This repository was archived by the owner on Sep 22, 2025. It is now read-only.

Commit 04191d7

Browse files
author
Mikolaj Komar
committed
Add test for reduce and fix type error in sparse matrix local
1 parent 4cfb110 commit 04191d7

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

include/dr/mp/containers/matrix_formats/csr_eq_segment.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ template <typename DSM> class csr_eq_segment_iterator {
240240
const auto my_process_segment_index = dsm_->rows_backend_.getrank();
241241
assert(my_process_segment_index == segment_index_);
242242
if (dsm_->local_view == nullptr) {
243-
return nullptr;
243+
throw std::runtime_error("Requesting not existing local segment");
244244
}
245245
return dsm_->local_view->begin();
246246
}

include/dr/mp/containers/matrix_formats/csr_row_segment.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ template <typename DSM> class csr_row_segment_iterator {
228228
const auto my_process_segment_index = dsm_->vals_backend_.getrank();
229229
assert(my_process_segment_index == segment_index_);
230230
if (dsm_->local_view == nullptr) {
231-
return nullptr;
231+
throw std::runtime_error("Requesting not existing local segment");
232232
}
233233
return dsm_->local_view->begin();
234234
}

test/gtest/mp/sparse_matrix.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ auto testMatrixIter(auto &src, auto &matrix) {
1414
}
1515
}
1616

17+
auto testMatrixReduce(auto &src, auto &matrix) {
18+
EXPECT_TRUE(src.size() == matrix.size());
19+
long sum = 0;
20+
for (auto [index, val] : src) {
21+
auto [x, y] = index;
22+
sum += (long)(val + x + y);
23+
}
24+
auto transformer = [](auto entry) {
25+
auto [index, val] = entry;
26+
auto [x, y] = index;
27+
return (long)(val + x + y);
28+
};
29+
auto transformed = dr::transform_view(matrix, transformer);
30+
long reduced = dr::mp::reduce(transformed, 0, std::plus<long>{});
31+
EXPECT_TRUE((sum == reduced));
32+
}
33+
1734
TEST(SparseMatrix, staticAssertEq) {
1835
std::size_t m = 100;
1936
std::size_t k = 100;
@@ -73,3 +90,25 @@ TEST(SparseMatrix, IterEq) {
7390
a(csr, 0);
7491
testMatrixIter(csr, a);
7592
}
93+
94+
TEST(SparseMatrix, ReduceRow) {
95+
std::size_t m = 100;
96+
std::size_t k = 100;
97+
auto csr = dr::generate_random_csr({m, k}, 0.1f);
98+
dr::mp::distributed_sparse_matrix<
99+
float, unsigned long, dr::mp::MpiBackend,
100+
dr::mp::csr_row_distribution<float, unsigned long, dr::mp::MpiBackend>>
101+
a(csr, 0);
102+
testMatrixReduce(csr, a);
103+
}
104+
105+
TEST(SparseMatrix, ReduceEq) {
106+
std::size_t m = 100;
107+
std::size_t k = 100;
108+
auto csr = dr::generate_random_csr({m, k}, 0.1f);
109+
dr::mp::distributed_sparse_matrix<
110+
float, unsigned long, dr::mp::MpiBackend,
111+
dr::mp::csr_eq_distribution<float, unsigned long, dr::mp::MpiBackend>>
112+
a(csr, 0);
113+
testMatrixReduce(csr, a);
114+
}

0 commit comments

Comments
 (0)