-
Notifications
You must be signed in to change notification settings - Fork 8
Support mixed CSR/CSC operations #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
de12da2
Implement `csc_view`, implement `transposed`.
BenBrock ae5af30
Fix tests---should be recovered from force push.
BenBrock 6632825
Add CSC SpMV test.
BenBrock 3750a1e
Make `scaled_view` work with `csc_view`.
BenBrock e1f2cd2
Update MKL to support CSC.
BenBrock 3fde64c
Merge branch 'main' into dev/brock/implement-transposed
BenBrock 1f2be4a
Suppose operations with CSC in ArmPL.
BenBrock dd7bfb4
Update include/spblas/vendor/onemkl_sycl/detail/create_matrix_handle.hpp
BenBrock 907539b
Update examples/spmm_csc.cpp
BenBrock 273ae73
Fix formatting
BenBrock cf16b4c
Add CSC View SpMV BScaled test.
BenBrock 7a8cb29
First stab at implementing CSR x CSC -> CSR.
BenBrock 53a59c0
Merge branch 'main' into dev/brock/mixed-csr-csc
BenBrock 543ed24
Support mixed CSR/CSC in MKL.
BenBrock 91c017e
Implement dot product with accumulator to avoid having to sort indices
BenBrock a83b0a0
Bugfix: wrong dimension.
BenBrock dae6de6
Implement outer product SpGEMM.
BenBrock b13e998
Remove unnecessary transposed inner product implementations.
BenBrock 4a2e3a1
Implement "scattered Gustavson's," add testing, ensure works in ArmPL.
BenBrock 3a77817
Commit missing files
BenBrock 0ca3447
Enable CSC output in MKL.
BenBrock 1188be9
Use `hash_accumulator` for outer product, use `sparse_intersection` for
BenBrock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #pragma once | ||
|
|
||
| #include <optional> | ||
|
|
||
| #include <spblas/backend/spa_accumulator.hpp> | ||
|
|
||
| namespace spblas { | ||
|
|
||
| namespace __detail { | ||
|
|
||
| template <typename T, typename I, typename A, typename B> | ||
| std::optional<T> sparse_dot_product(__backend::spa_accumulator<T, I>& acc, | ||
| A&& a, B&& b) { | ||
| acc.clear(); | ||
|
|
||
| for (auto&& [i, v] : a) { | ||
| acc[i] = v; | ||
| } | ||
|
|
||
| T sum = 0; | ||
| bool implicit_zero = true; | ||
| for (auto&& [i, v] : b) { | ||
| if (acc.contains(i)) { | ||
| sum += acc[i] * v; | ||
| implicit_zero = false; | ||
| } | ||
| } | ||
|
|
||
| if (implicit_zero) { | ||
| return {}; | ||
| } else { | ||
| return sum; | ||
| } | ||
| } | ||
|
|
||
| } // namespace __detail | ||
|
|
||
| } // namespace spblas | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #pragma once | ||
|
|
||
| #include "spgemm_gustavsons.hpp" | ||
| #include "spgemm_innerproduct.hpp" | ||
| #include "spgemm_outerproduct.hpp" |
217 changes: 217 additions & 0 deletions
217
include/spblas/algorithms/detail/spgemm/spgemm_gustavsons.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| #pragma once | ||
|
|
||
| #include <spblas/backend/backend.hpp> | ||
| #include <spblas/concepts.hpp> | ||
| #include <spblas/detail/log.hpp> | ||
|
|
||
| #include <spblas/algorithms/transposed.hpp> | ||
| #include <spblas/backend/csr_builder.hpp> | ||
| #include <spblas/backend/spa_accumulator.hpp> | ||
| #include <spblas/detail/operation_info_t.hpp> | ||
|
|
||
| namespace spblas { | ||
|
|
||
| // C = AB | ||
| // CSR * CSR -> CSR | ||
| // SpGEMM (Gustavson's Algorithm) | ||
| template <matrix A, matrix B, matrix C> | ||
| requires(__backend::row_iterable<A> && __backend::row_iterable<B> && | ||
| __detail::is_csr_view_v<C>) | ||
| void multiply(A&& a, B&& b, C&& c) { | ||
| log_trace(""); | ||
| if (__backend::shape(a)[0] != __backend::shape(c)[0] || | ||
| __backend::shape(b)[1] != __backend::shape(c)[1] || | ||
| __backend::shape(a)[1] != __backend::shape(b)[0]) { | ||
| throw std::invalid_argument( | ||
| "multiply: matrix dimensions are incompatible."); | ||
| } | ||
|
|
||
| using T = tensor_scalar_t<C>; | ||
| using I = tensor_index_t<C>; | ||
|
|
||
| __backend::spa_accumulator<T, I> c_row(__backend::shape(c)[1]); | ||
| __backend::csr_builder c_builder(c); | ||
|
|
||
| for (auto&& [i, a_row] : __backend::rows(a)) { | ||
| c_row.clear(); | ||
| for (auto&& [k, a_v] : a_row) { | ||
| for (auto&& [j, b_v] : __backend::lookup_row(b, k)) { | ||
| c_row[j] += a_v * b_v; | ||
| } | ||
| } | ||
| c_row.sort(); | ||
|
|
||
| try { | ||
| c_builder.insert_row(i, c_row.get()); | ||
| } catch (...) { | ||
| throw std::runtime_error("multiply: SpGEMM ran out of memory."); | ||
| } | ||
| } | ||
| c.update(c.values(), c.rowptr(), c.colind(), c.shape(), | ||
| c.rowptr()[c.shape()[0]]); | ||
| } | ||
|
|
||
| // C = AB | ||
| // CSR * CSR -> CSR | ||
| // SpGEMM (Gustavson's Algorithm) | ||
| template <matrix A, matrix B, matrix C> | ||
| requires(__backend::row_iterable<A> && __backend::row_iterable<B> && | ||
| __detail::is_csr_view_v<C>) | ||
| operation_info_t multiply_compute(A&& a, B&& b, C&& c) { | ||
| log_trace(""); | ||
| if (__backend::shape(a)[0] != __backend::shape(c)[0] || | ||
| __backend::shape(b)[1] != __backend::shape(c)[1] || | ||
| __backend::shape(a)[1] != __backend::shape(b)[0]) { | ||
| throw std::invalid_argument( | ||
| "multiply: matrix dimensions are incompatible."); | ||
| } | ||
|
|
||
| using T = tensor_scalar_t<C>; | ||
| using I = tensor_index_t<C>; | ||
| using O = tensor_offset_t<C>; | ||
|
|
||
| O nnz = 0; | ||
| __backend::spa_set<I> c_row(__backend::shape(c)[1]); | ||
|
|
||
| for (auto&& [i, a_row] : __backend::rows(a)) { | ||
| c_row.clear(); | ||
|
|
||
| for (auto&& [k, a_v] : a_row) { | ||
| for (auto&& [j, b_v] : __backend::lookup_row(b, k)) { | ||
| c_row.insert(j); | ||
| } | ||
| } | ||
|
|
||
| nnz += c_row.size(); | ||
| } | ||
|
|
||
| return operation_info_t{__backend::shape(c), nnz}; | ||
| } | ||
|
|
||
| // C = AB | ||
| // CSC * CSC -> CSC | ||
| // SpGEMM (Gustavson's Algorithm, transposed) | ||
| template <matrix A, matrix B, matrix C> | ||
| requires(__backend::column_iterable<A> && __backend::column_iterable<B> && | ||
| __detail::is_csc_view_v<C>) | ||
| void multiply(A&& a, B&& b, C&& c) { | ||
| log_trace(""); | ||
| if (__backend::shape(a)[0] != __backend::shape(c)[0] || | ||
| __backend::shape(b)[1] != __backend::shape(c)[1] || | ||
| __backend::shape(a)[1] != __backend::shape(b)[0]) { | ||
| throw std::invalid_argument( | ||
| "multiply: matrix dimensions are incompatible."); | ||
| } | ||
| multiply(transposed(b), transposed(a), transposed(c)); | ||
| } | ||
|
|
||
| // C = AB | ||
| // CSC * CSC -> CSC | ||
| // SpGEMM (Gustavson's Algorithm, transposed) | ||
| template <matrix A, matrix B, matrix C> | ||
| requires(__backend::column_iterable<A> && __backend::column_iterable<B> && | ||
| __detail::is_csc_view_v<C>) | ||
| operation_info_t multiply_compute(A&& a, B&& b, C&& c) { | ||
| log_trace(""); | ||
| if (__backend::shape(a)[0] != __backend::shape(c)[0] || | ||
| __backend::shape(b)[1] != __backend::shape(c)[1] || | ||
| __backend::shape(a)[1] != __backend::shape(b)[0]) { | ||
| throw std::invalid_argument( | ||
| "multiply: matrix dimensions are incompatible."); | ||
| } | ||
|
|
||
| auto info = multiply_compute(transposed(b), transposed(a), transposed(c)); | ||
| info.update_impl_({info.result_shape()[1], info.result_shape()[0]}, | ||
| info.result_nnz()); | ||
| return info; | ||
| } | ||
|
|
||
| // C = AB | ||
| // CSR * CSR -> CSC | ||
| // SpGEMM (Gustavson's Algorithm, scattered) | ||
| template <matrix A, matrix B, matrix C> | ||
| requires(__backend::row_iterable<A> && __backend::row_iterable<B> && | ||
| __detail::is_csc_view_v<C>) | ||
| void multiply(A&& a, B&& b, C&& c) { | ||
| log_trace(""); | ||
| if (__backend::shape(a)[0] != __backend::shape(c)[0] || | ||
| __backend::shape(b)[1] != __backend::shape(c)[1] || | ||
| __backend::shape(a)[1] != __backend::shape(b)[0]) { | ||
| throw std::invalid_argument( | ||
| "multiply: matrix dimensions are incompatible."); | ||
| } | ||
|
|
||
| using T = tensor_scalar_t<C>; | ||
| using I = tensor_index_t<C>; | ||
|
|
||
| __backend::spa_accumulator<T, I> c_row(__backend::shape(c)[1]); | ||
|
|
||
| std::vector<std::vector<std::pair<I, T>>> columns(__backend::shape(c)[1]); | ||
|
|
||
| for (auto&& [i, a_row] : __backend::rows(a)) { | ||
| c_row.clear(); | ||
| for (auto&& [k, a_v] : a_row) { | ||
| for (auto&& [j, b_v] : __backend::lookup_row(b, k)) { | ||
| c_row[j] += a_v * b_v; | ||
| } | ||
| } | ||
| for (auto&& [j, v] : c_row.get()) { | ||
| columns[j].push_back({i, v}); | ||
| } | ||
| } | ||
|
|
||
| __backend::csc_builder c_builder(c); | ||
|
|
||
| for (std::size_t j = 0; j < columns.size(); j++) { | ||
| auto&& column = columns[j]; | ||
| std::sort(column.begin(), column.end(), | ||
| [](auto&& a, auto&& b) { return a.first < b.first; }); | ||
|
|
||
| try { | ||
| c_builder.insert_column(j, column); | ||
| } catch (...) { | ||
| throw std::runtime_error("multiply: SpGEMM ran out of memory."); | ||
| } | ||
| } | ||
| c.update(c.values(), c.colptr(), c.rowind(), c.shape(), | ||
| c.colptr()[c.shape()[1]]); | ||
| } | ||
|
|
||
| // C = AB | ||
| // CSR * CSR -> CSC | ||
| // SpGEMM (Gustavson's Algorithm, scattered) | ||
| template <matrix A, matrix B, matrix C> | ||
| requires(__backend::row_iterable<A> && __backend::row_iterable<B> && | ||
| __detail::is_csc_view_v<C>) | ||
| operation_info_t multiply_compute(A&& a, B&& b, C&& c) { | ||
| log_trace(""); | ||
| if (__backend::shape(a)[0] != __backend::shape(c)[0] || | ||
| __backend::shape(b)[1] != __backend::shape(c)[1] || | ||
| __backend::shape(a)[1] != __backend::shape(b)[0]) { | ||
| throw std::invalid_argument( | ||
| "multiply: matrix dimensions are incompatible."); | ||
| } | ||
|
|
||
| using T = tensor_scalar_t<C>; | ||
| using I = tensor_index_t<C>; | ||
| using O = tensor_offset_t<C>; | ||
|
|
||
| O nnz = 0; | ||
| __backend::spa_set<I> c_row(__backend::shape(c)[1]); | ||
|
|
||
| for (auto&& [i, a_row] : __backend::rows(a)) { | ||
| c_row.clear(); | ||
|
|
||
| for (auto&& [k, a_v] : a_row) { | ||
| for (auto&& [j, b_v] : __backend::lookup_row(b, k)) { | ||
| c_row.insert(j); | ||
| } | ||
| } | ||
|
|
||
| nnz += c_row.size(); | ||
| } | ||
|
|
||
| return operation_info_t{__backend::shape(c), nnz}; | ||
| } | ||
|
|
||
| } // namespace spblas |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.