-
Notifications
You must be signed in to change notification settings - Fork 1
Implement sparse + sparse matrix addition for CSC, CSR, and COO formats #27
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: albertomercurio <[email protected]>
Co-authored-by: albertomercurio <[email protected]>
albertomercurio
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Overall it seems a good job. I'm wondering if we can also implement the sum among transposed/adjointed matrices, like +(A::DeviceSparseMatrixCSC, B::Transpose{T, <:DeviceSparseMatrixCSC}), and in case how to do it. Should one convert the transposed matrix to CSR? If so, can the sum be efficiently computed between a CSC and CSR?
If not, are there other direct ways to do it, like the methods done for the mul! or dot functions?
| nnz_A = nnz(A) | ||
| nnz_B = nnz(B) | ||
| nnz_total = nnz_A + nnz_B | ||
|
|
||
| # Allocate result arrays | ||
| rowind_C = similar(getrowind(A), nnz_total) | ||
| colind_C = similar(getcolind(A), nnz_total) | ||
| nzval_C = similar(nonzeros(A), Tv, nnz_total) | ||
|
|
||
| # Copy entries from A | ||
| rowind_C[1:nnz_A] .= getrowind(A) | ||
| colind_C[1:nnz_A] .= getcolind(A) | ||
| nzval_C[1:nnz_A] .= nonzeros(A) | ||
|
|
||
| # Copy entries from B | ||
| rowind_C[(nnz_A+1):end] .= getrowind(B) | ||
| colind_C[(nnz_A+1):end] .= getcolind(B) | ||
| nzval_C[(nnz_A+1):end] .= nonzeros(B) | ||
|
|
||
| return DeviceSparseMatrixCOO(m, n, rowind_C, colind_C, nzval_C) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Here you are just concatenating the nonzero values, but they can also be repeated and one should merge them instead. Could you take into account also that?
Check that you merge also the other formats.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit 778e354. COO addition now properly merges duplicate entries by:
- Concatenating and sorting by (row, col)
- Marking unique entries
- Compacting with duplicate merging using
kernel_compact_coo!
All three formats (CSC, CSR, COO) now correctly merge duplicates. Verified with tests.
…ndency Co-authored-by: albertomercurio <[email protected]>
Good question! Adding support for transposed/adjointed matrices would be valuable. Here are the options: Option 1: Convert CSC transpose to CSR (Most efficient)
Option 2: Use existing pattern from mul!/dot (Also efficient)
Option 3: Convert to same format (Simpler but less efficient)
Recommendation: Option 1 (CSC+CSR merge) or Option 2 (direct transpose handling) would be most efficient. Option 2 follows the existing mul! pattern more closely. Should I implement one of these approaches? Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #27 +/- ##
==========================================
+ Coverage 82.64% 85.04% +2.40%
==========================================
Files 14 14
Lines 801 1030 +229
==========================================
+ Hits 662 876 +214
- Misses 139 154 +15 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark 'Benchmark Results'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.
| Benchmark suite | Current: 778e354 | Previous: 862b4d5 | Ratio |
|---|---|---|---|
Kronecker Product/JLArray/CSC |
16593217 ns |
12002090.5 ns |
1.38 |
Kronecker Product/JLArray/COO |
3484290 ns |
2334051 ns |
1.49 |
Kronecker Product/JLArray/CSR |
16622938 ns |
11984809 ns |
1.39 |
This comment was automatically generated by workflow using github-action-benchmark.
Implementation Complete: Sparse Matrix Addition
Successfully implemented the
+(A::AbstractDeviceSparseMatrix, B::AbstractDeviceSparseMatrix)operation for CSC-CSC, CSR-CSR, and COO-COO formats.Completed Tasks:
kernel_count_nnz_per_col_csc!insrc/matrix_csc/matrix_csc_kernels.jlkernel_merge_csc!insrc/matrix_csc/matrix_csc_kernels.jl+(A::DeviceSparseMatrixCSC, B::DeviceSparseMatrixCSC)insrc/matrix_csc/matrix_csc.jlkernel_count_nnz_per_row_csr!insrc/matrix_csr/matrix_csr_kernels.jlkernel_merge_csr!insrc/matrix_csr/matrix_csr_kernels.jl+(A::DeviceSparseMatrixCSR, B::DeviceSparseMatrixCSR)insrc/matrix_csr/matrix_csr.jlkernel_mark_unique_coo!insrc/matrix_coo/matrix_coo_kernels.jlkernel_compact_coo!insrc/matrix_coo/matrix_coo_kernels.jl+(A::DeviceSparseMatrixCOO, B::DeviceSparseMatrixCOO)insrc/matrix_coo/matrix_coo.jltest/shared/matrix_csc.jltest/shared/matrix_csr.jltest/shared/matrix_coo.jlbenchmark_sparse_sparse_add!inbenchmarks/matrix_benchmarks.jlbenchmarks/runbenchmarks.jlImplementation Details:
CSC/CSR Formats: Two-pass merge algorithm
COO Format: Sort + compact algorithm
GPU Compatibility: Avoided scalar indexing using broadcasting operations (e.g.,
colptr_C[1:1] .= one(Ti))Test Results:
✅ 1478 tests passed (Base Array + JLArray backends)
✅ All sparse + sparse addition tests pass
✅ Works correctly with overlapping and non-overlapping entries
✅ Proper dimension checking and error handling
✅ GPU-compatible (no scalar indexing issues)
✅ COO format now properly merges duplicates
Original prompt
AbstractDeviceSparseMatrix#16💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.