-
Notifications
You must be signed in to change notification settings - Fork 2
Implement device-native conversions between CSR/CSC and COO sparse matrix formats #22
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
+443
−0
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
81b6505
Initial plan
Copilot c3a772b
Implement direct conversions between CSR/CSC and COO formats
Copilot 34a82c7
Format code with JuliaFormatter
Copilot 80c5d6d
Add comprehensive docstrings to conversion functions
Copilot b7fd8df
Reimplement conversions without CPU transfers using kernels and Accel…
Copilot c2bad25
Move conversion functions to separate conversions/ folder with tests …
Copilot 68cf40f
Fix conversions to work with all backends by using CPU sorting and cu…
Copilot ebf8d88
Move kernels outside functions and use AcceleratedKernels with JLBack…
Copilot 41830c2
Remove JLArray dispatch and fix scalar indexing issue
albertomercurio 6df326c
Fix errors in Metal
albertomercurio 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """ | ||
| benchmark_conversions!(SUITE, array_constructor, array_type_name; N=10000, T=Float64) | ||
|
|
||
| Benchmark sparse matrix format conversions (CSC, CSR, COO). | ||
|
|
||
| # Arguments | ||
| - `SUITE`: The BenchmarkGroup to add benchmarks to | ||
| - `array_constructor`: Function to construct arrays (e.g., `Array`, `JLArray`, `CuArray`) | ||
| - `array_type_name`: String name for the array type (for display) | ||
|
|
||
| # Keyword Arguments | ||
| - `N`: Size of the matrix (default: 10000) | ||
| - `T`: Element type (default: Float64) | ||
| """ | ||
| function benchmark_conversions!( | ||
| SUITE, | ||
| array_constructor, | ||
| array_type_name; | ||
| N = 10000, | ||
| T = Float64, | ||
| ) | ||
| # Create sparse matrix with 1% density | ||
| sm_csc_std = sprand(T, N, N, 0.01) | ||
|
|
||
| # Convert to different formats | ||
| sm_csc = DeviceSparseMatrixCSC(sm_csc_std) | ||
| sm_csr = DeviceSparseMatrixCSR(sm_csc_std) | ||
| sm_coo = DeviceSparseMatrixCOO(sm_csc_std) | ||
|
|
||
| # Adapt to device | ||
| dsm_csc = adapt(array_constructor, sm_csc) | ||
| dsm_csr = adapt(array_constructor, sm_csr) | ||
| dsm_coo = adapt(array_constructor, sm_coo) | ||
|
|
||
| # CSC → COO conversion | ||
| SUITE["Format Conversions"][array_type_name]["CSC → COO"] = | ||
| @benchmarkable DeviceSparseMatrixCOO($dsm_csc) | ||
|
|
||
| # COO → CSC conversion | ||
| SUITE["Format Conversions"][array_type_name]["COO → CSC"] = | ||
| @benchmarkable DeviceSparseMatrixCSC($dsm_coo) | ||
|
|
||
| # CSR → COO conversion | ||
| SUITE["Format Conversions"][array_type_name]["CSR → COO"] = | ||
| @benchmarkable DeviceSparseMatrixCOO($dsm_csr) | ||
|
|
||
| # COO → CSR conversion | ||
| SUITE["Format Conversions"][array_type_name]["COO → CSR"] = | ||
| @benchmarkable DeviceSparseMatrixCSR($dsm_coo) | ||
|
|
||
| return nothing | ||
| end |
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
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,35 @@ | ||
| # Kernel for converting CSC to COO format | ||
| @kernel inbounds=true function kernel_csc_to_coo!( | ||
| rowind, | ||
| colind, | ||
| nzval_out, | ||
| @Const(colptr), | ||
| @Const(rowval), | ||
| @Const(nzval_in), | ||
| ) | ||
| col = @index(Global) | ||
|
|
||
| @inbounds for j = colptr[col]:(colptr[col+1]-1) | ||
| rowind[j] = rowval[j] | ||
| colind[j] = col | ||
| nzval_out[j] = nzval_in[j] | ||
| end | ||
| end | ||
|
|
||
| # Kernel for converting CSR to COO format | ||
| @kernel inbounds=true function kernel_csr_to_coo!( | ||
| rowind, | ||
| colind, | ||
| nzval_out, | ||
| @Const(rowptr), | ||
| @Const(colval), | ||
| @Const(nzval_in), | ||
| ) | ||
| row = @index(Global) | ||
|
|
||
| @inbounds for j = rowptr[row]:(rowptr[row+1]-1) | ||
| rowind[j] = row | ||
| colind[j] = colval[j] | ||
| nzval_out[j] = nzval_in[j] | ||
| end | ||
| end |
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,250 @@ | ||
| # Conversions between CSC, CSR, and COO sparse matrix formats | ||
| # All conversions operate entirely on-device without CPU transfers | ||
|
|
||
| # ============================================================================ | ||
| # CSC ↔ COO Conversions | ||
| # ============================================================================ | ||
|
|
||
| """ | ||
| DeviceSparseMatrixCOO(A::DeviceSparseMatrixCSC) | ||
|
|
||
| Convert a Compressed Sparse Column (CSC) matrix to Coordinate (COO) format. | ||
|
|
||
| The conversion preserves all matrix data and maintains backend compatibility. | ||
| The result will be on the same backend (CPU/GPU) as the input matrix. | ||
|
|
||
| # Examples | ||
| ```julia | ||
| using DeviceSparseArrays, SparseArrays | ||
|
|
||
| # Create a CSC matrix | ||
| A_sparse = sparse([1, 2, 3], [1, 2, 3], [1.0, 2.0, 3.0], 3, 3) | ||
| A_csc = DeviceSparseMatrixCSC(A_sparse) | ||
|
|
||
| # Convert to COO format | ||
| A_coo = DeviceSparseMatrixCOO(A_csc) | ||
| ``` | ||
| """ | ||
| function DeviceSparseMatrixCOO(A::DeviceSparseMatrixCSC{Tv,Ti}) where {Tv,Ti} | ||
| m, n = size(A) | ||
| nnz_count = nnz(A) | ||
|
|
||
| backend = get_backend(A.nzval) | ||
|
|
||
| # Allocate output arrays on the same backend | ||
| rowind = similar(A.rowval, Ti, nnz_count) | ||
| colind = similar(A.rowval, Ti, nnz_count) | ||
| nzval = similar(A.nzval, Tv, nnz_count) | ||
|
|
||
| # Use kernel to convert CSC to COO | ||
| kernel! = kernel_csc_to_coo!(backend) | ||
| kernel!(rowind, colind, nzval, A.colptr, A.rowval, A.nzval; ndrange = (n,)) | ||
|
|
||
| return DeviceSparseMatrixCOO(m, n, rowind, colind, nzval) | ||
| end | ||
|
|
||
| """ | ||
| DeviceSparseMatrixCSC(A::DeviceSparseMatrixCOO) | ||
|
|
||
| Convert a Coordinate (COO) matrix to Compressed Sparse Column (CSC) format. | ||
|
|
||
| The conversion sorts the COO entries by column (then by row within each column) | ||
| and builds the column pointer structure. The result maintains backend compatibility | ||
| with the input matrix. | ||
|
|
||
| # Examples | ||
| ```julia | ||
| using DeviceSparseArrays, SparseArrays | ||
|
|
||
| # Create a COO matrix | ||
| A_sparse = sparse([1, 2, 3], [1, 2, 3], [1.0, 2.0, 3.0], 3, 3) | ||
| A_coo = DeviceSparseMatrixCOO(A_sparse) | ||
|
|
||
| # Convert to CSC format | ||
| A_csc = DeviceSparseMatrixCSC(A_coo) | ||
| ``` | ||
| """ | ||
| function DeviceSparseMatrixCSC(A::DeviceSparseMatrixCOO{Tv,Ti}) where {Tv,Ti} | ||
| m, n = size(A) | ||
| nnz_count = nnz(A) | ||
|
|
||
| backend = get_backend(A.nzval) | ||
|
|
||
| # Create keys for sorting: column first, then row | ||
| # We use n * rowind + colind to create a unique sortable key | ||
| keys = similar(A.rowind, Ti, nnz_count) | ||
|
|
||
| # Create keys on device | ||
| @kernel inbounds=true function make_keys!( | ||
| keys, | ||
| @Const(rowind), | ||
| @Const(colind), | ||
| @Const(n) | ||
| ) | ||
| i = @index(Global) | ||
| keys[i] = colind[i] * n + rowind[i] | ||
| end | ||
|
|
||
| kernel! = make_keys!(backend) | ||
| kernel!(keys, A.rowind, A.colind, n; ndrange = (nnz_count,)) | ||
|
|
||
| # Sort - collect to CPU and use Base.sortperm since AcceleratedKernels | ||
| # doesn't work reliably on all backends (e.g., JLBackend) | ||
| keys_cpu = collect(keys) | ||
| perm_cpu = sortperm(keys_cpu) | ||
| # Adapt back to the original backend | ||
| perm = Adapt.adapt_structure(backend, perm_cpu) | ||
albertomercurio marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Apply permutation to get sorted arrays | ||
| rowind_sorted = A.rowind[perm] | ||
| colind_sorted = A.colind[perm] | ||
| nzval_sorted = A.nzval[perm] | ||
|
|
||
| # Build colptr on device using a histogram approach | ||
| colptr = similar(A.colind, Ti, n + 1) | ||
| fill!(colptr, zero(Ti)) | ||
|
|
||
| # Count entries per column | ||
| @kernel inbounds=true function count_per_col!(colptr, @Const(colind_sorted)) | ||
| i = @index(Global) | ||
| col = colind_sorted[i] | ||
| @atomic colptr[col+1] += 1 | ||
| end | ||
|
|
||
| kernel! = count_per_col!(backend) | ||
| kernel!(colptr, colind_sorted; ndrange = (nnz_count,)) | ||
|
|
||
| # Build cumulative sum on CPU (collect, compute, adapt back) | ||
| colptr_cpu = collect(colptr) | ||
| colptr_cpu[1] = 1 | ||
| for i = 2:(n+1) | ||
| colptr_cpu[i] += colptr_cpu[i-1] | ||
| end | ||
| colptr = Adapt.adapt_structure(backend, colptr_cpu) | ||
|
|
||
| return DeviceSparseMatrixCSC(m, n, colptr, rowind_sorted, nzval_sorted) | ||
| end | ||
|
|
||
| # ============================================================================ | ||
| # CSR ↔ COO Conversions | ||
| # ============================================================================ | ||
|
|
||
| """ | ||
| DeviceSparseMatrixCOO(A::DeviceSparseMatrixCSR) | ||
|
|
||
| Convert a Compressed Sparse Row (CSR) matrix to Coordinate (COO) format. | ||
|
|
||
| The conversion preserves all matrix data and maintains backend compatibility. | ||
| The result will be on the same backend (CPU/GPU) as the input matrix. | ||
|
|
||
| # Examples | ||
| ```julia | ||
| using DeviceSparseArrays, SparseArrays | ||
|
|
||
| # Create a CSR matrix | ||
| A_sparse = sparse([1, 2, 3], [1, 2, 3], [1.0, 2.0, 3.0], 3, 3) | ||
| A_csr = DeviceSparseMatrixCSR(A_sparse) | ||
|
|
||
| # Convert to COO format | ||
| A_coo = DeviceSparseMatrixCOO(A_csr) | ||
| ``` | ||
| """ | ||
| function DeviceSparseMatrixCOO(A::DeviceSparseMatrixCSR{Tv,Ti}) where {Tv,Ti} | ||
| m, n = size(A) | ||
| nnz_count = nnz(A) | ||
|
|
||
| backend = get_backend(A.nzval) | ||
|
|
||
| # Allocate output arrays on the same backend | ||
| rowind = similar(A.colval, Ti, nnz_count) | ||
| colind = similar(A.colval, Ti, nnz_count) | ||
| nzval = similar(A.nzval, Tv, nnz_count) | ||
|
|
||
| # Use kernel to convert CSR to COO | ||
| kernel! = kernel_csr_to_coo!(backend) | ||
| kernel!(rowind, colind, nzval, A.rowptr, A.colval, A.nzval; ndrange = (m,)) | ||
|
|
||
| return DeviceSparseMatrixCOO(m, n, rowind, colind, nzval) | ||
| end | ||
|
|
||
| """ | ||
| DeviceSparseMatrixCSR(A::DeviceSparseMatrixCOO) | ||
|
|
||
| Convert a Coordinate (COO) matrix to Compressed Sparse Row (CSR) format. | ||
|
|
||
| The conversion sorts the COO entries by row (then by column within each row) | ||
| and builds the row pointer structure. The result maintains backend compatibility | ||
| with the input matrix. | ||
|
|
||
| # Examples | ||
| ```julia | ||
| using DeviceSparseArrays, SparseArrays | ||
|
|
||
| # Create a COO matrix | ||
| A_sparse = sparse([1, 2, 3], [1, 2, 3], [1.0, 2.0, 3.0], 3, 3) | ||
| A_coo = DeviceSparseMatrixCOO(A_sparse) | ||
|
|
||
| # Convert to CSR format | ||
| A_csr = DeviceSparseMatrixCSR(A_coo) | ||
| ``` | ||
| """ | ||
| function DeviceSparseMatrixCSR(A::DeviceSparseMatrixCOO{Tv,Ti}) where {Tv,Ti} | ||
| m, n = size(A) | ||
| nnz_count = nnz(A) | ||
|
|
||
| backend = get_backend(A.nzval) | ||
|
|
||
| # Create keys for sorting: row first, then column | ||
| # We use m * colind + rowind to create a unique sortable key | ||
| keys = similar(A.rowind, Ti, nnz_count) | ||
|
|
||
| # Create keys on device | ||
| @kernel inbounds=true function make_keys!( | ||
| keys, | ||
| @Const(rowind), | ||
| @Const(colind), | ||
| @Const(m) | ||
| ) | ||
| i = @index(Global) | ||
| keys[i] = rowind[i] * m + colind[i] | ||
| end | ||
|
|
||
| kernel! = make_keys!(backend) | ||
| kernel!(keys, A.rowind, A.colind, m; ndrange = (nnz_count,)) | ||
albertomercurio marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Sort - collect to CPU and use Base.sortperm since AcceleratedKernels | ||
| # doesn't work reliably on all backends (e.g., JLBackend) | ||
| keys_cpu = collect(keys) | ||
| perm_cpu = sortperm(keys_cpu) | ||
| # Adapt back to the original backend | ||
| perm = Adapt.adapt_structure(backend, perm_cpu) | ||
|
|
||
| # Apply permutation to get sorted arrays | ||
| rowind_sorted = A.rowind[perm] | ||
| colind_sorted = A.colind[perm] | ||
| nzval_sorted = A.nzval[perm] | ||
|
|
||
| # Build rowptr on device using a histogram approach | ||
| rowptr = similar(A.rowind, Ti, m + 1) | ||
| fill!(rowptr, zero(Ti)) | ||
|
|
||
| # Count entries per row | ||
| @kernel inbounds=true function count_per_row!(rowptr, @Const(rowind_sorted)) | ||
| i = @index(Global) | ||
| row = rowind_sorted[i] | ||
| @atomic rowptr[row+1] += 1 | ||
| end | ||
|
|
||
| kernel! = count_per_row!(backend) | ||
| kernel!(rowptr, rowind_sorted; ndrange = (nnz_count,)) | ||
|
|
||
| # Build cumulative sum on CPU (collect, compute, adapt back) | ||
| rowptr_cpu = collect(rowptr) | ||
| rowptr_cpu[1] = 1 | ||
| for i = 2:(m+1) | ||
| rowptr_cpu[i] += rowptr_cpu[i-1] | ||
| end | ||
| rowptr = Adapt.adapt_structure(backend, rowptr_cpu) | ||
|
|
||
| return DeviceSparseMatrixCSR(m, n, rowptr, colind_sorted, nzval_sorted) | ||
| end | ||
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
Oops, something went wrong.
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.