Skip to content

Commit 3dd3091

Browse files
committed
[Code] Add seq reduce impl && fix cuda reduce impl
1 parent 1c7a67d commit 3dd3091

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ as well as python high-level wrapper with automated resources management.
3030
- [X] CSR submatrix
3131
- [X] CSR matrix reduce
3232
- [X] CSR slicing
33-
- [ ] Sequential fallback backend for CPU
33+
- [X] Sequential fallback backend for CPU
3434
- [ ] IO matrix loading from mtx file
3535
- [ ] IO matrix saving into mtx file
3636
- [ ] IO matrix saving into gviz format

cubool/sources/cuda/kernels/spreduce.cuh

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,16 @@ namespace cubool {
5050
MatrixType operator()(const MatrixType& a) {
5151
auto nrows = a.m_rows;
5252
auto ncols = a.m_cols;
53-
auto nvals = a.m_vals;
5453

5554
ContainerType<IndexType> rowIndex(nrows + 1);
5655
ContainerType<IndexType> tmpRowIndex(nrows + 1);
5756

58-
// Assign for each row 0 (empty)
59-
thrust::fill(tmpRowIndex.begin(), tmpRowIndex.end(), (IndexType) 0);
60-
61-
// For each value find row and set this row in 1
62-
thrust::for_each(thrust::counting_iterator<IndexType>(0), thrust::counting_iterator<IndexType>(nvals),
63-
[aRowIndex = a.m_row_index.data(),
64-
tmpRowIndex = tmpRowIndex.data(),
65-
nrows]
66-
__device__ (IndexType valuedIdx) {
67-
auto rowId = findNearestRowIdx(valuedIdx, nrows, aRowIndex);
68-
atomicOr((tmpRowIndex + rowId).get(), (IndexType) 1);
57+
// For each row if not empty then row will have 1 value
58+
thrust::for_each(thrust::counting_iterator<IndexType>(0), thrust::counting_iterator<IndexType>(nrows),
59+
[aRowIndex = a.m_row_index.data(), tmpRowIndex = tmpRowIndex.data()]
60+
__device__ (IndexType i) {
61+
auto nnz = aRowIndex[i + 1] - aRowIndex[i];
62+
tmpRowIndex[i] = nnz > 0? 1: 0;
6963
}
7064
);
7165

cubool/sources/sequential/sq_matrix.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <sequential/sq_kronecker.hpp>
2929
#include <sequential/sq_ewiseadd.hpp>
3030
#include <sequential/sq_spgemm.hpp>
31+
#include <sequential/sq_reduce.hpp>
3132
#include <sequential/sq_exclusive_scan.hpp>
3233
#include <core/error.hpp>
3334
#include <algorithm>
@@ -173,7 +174,17 @@ namespace cubool {
173174
CHECK_RAISE_ERROR(other != nullptr, InvalidArgument, "Provided matrix does not belongs to sequential matrix class");
174175

175176
auto M = other->getNrows();
176-
auto N = other->getNcols();
177+
auto N = 1;
178+
179+
assert(M == this->getNrows());
180+
assert(N == this->getNcols());
181+
182+
CsrData out;
183+
out.nrows = this->getNrows();
184+
out.ncols = this->getNcols();
185+
186+
sq_reduce(other->mData, out);
187+
this->mData = std::move(out);
177188
}
178189

179190
void SqMatrix::multiply(const MatrixBase &aBase, const MatrixBase &bBase, bool accumulate) {

cubool/sources/sequential/sq_reduce.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,26 @@
2323
/**********************************************************************************/
2424

2525
#include <sequential/sq_reduce.hpp>
26+
#include <sequential/sq_exclusive_scan.hpp>
2627

2728
namespace cubool {
2829

30+
void sq_reduce(const CsrData& a, CsrData& out) {
31+
out.rowOffsets.resize(a.nrows + 1);
32+
33+
for (index i = 0; i < a.nrows; i++) {
34+
index nnz = a.rowOffsets[i + 1] - a.rowOffsets[i];
35+
36+
if (nnz > 0) {
37+
out.rowOffsets[i] = 1;
38+
}
39+
}
40+
41+
sq_exclusive_scan(out.rowOffsets.begin(), out.rowOffsets.end(), 0);
42+
43+
out.nvals = out.rowOffsets.back();
44+
out.colIndices.clear();
45+
out.colIndices.resize(out.nvals, 0);
46+
}
47+
2948
}

cubool/sources/sequential/sq_reduce.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,18 @@
2525
#ifndef CUBOOL_SP_REDUCE_HPP
2626
#define CUBOOL_SP_REDUCE_HPP
2727

28+
#include <sequential/sq_csr_data.hpp>
29+
2830
namespace cubool {
2931

32+
/**
33+
* Reduce matrix `a` to column vector `out`
34+
*
35+
* @param a Input matrix
36+
* @param[out] out Where to store result
37+
*/
38+
void sq_reduce(const CsrData& a, CsrData& out);
39+
3040
}
3141

3242
#endif //CUBOOL_SP_REDUCE_HPP

cubool/tests/test_matrix_reduce.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,22 @@ TEST(cuBool_Matrix, ReduceLarge) {
7474
testRun(m, n, step, CUBOOL_HINT_NO);
7575
}
7676

77+
TEST(cuBool_Matrix, ReduceSmallFallback) {
78+
cuBool_Index m = 100, n = 200;
79+
float step = 0.05f;
80+
testRun(m, n, step, CUBOOL_HINT_CPU_BACKEND);
81+
}
82+
83+
TEST(cuBool_Matrix, ReduceMediumFallback) {
84+
cuBool_Index m = 400, n = 700;
85+
float step = 0.05f;
86+
testRun(m, n, step, CUBOOL_HINT_CPU_BACKEND);
87+
}
88+
89+
TEST(cuBool_Matrix, ReduceLargeFallback) {
90+
cuBool_Index m = 2000, n = 4000;
91+
float step = 0.01f;
92+
testRun(m, n, step, CUBOOL_HINT_CPU_BACKEND);
93+
}
94+
7795
CUBOOL_GTEST_MAIN

0 commit comments

Comments
 (0)