|
| 1 | +module SparseMatrixColoringsCUDAExt |
| 2 | + |
| 3 | +import SparseMatrixColorings as SMC |
| 4 | +using SparseArrays: SparseMatrixCSC, rowvals, nnz, nzrange |
| 5 | +using CUDA: CuVector, CuMatrix |
| 6 | +using CUDA.CUSPARSE: AbstractCuSparseMatrix, CuSparseMatrixCSC, CuSparseMatrixCSR |
| 7 | + |
| 8 | +SMC.matrix_versions(A::AbstractCuSparseMatrix) = (A,) |
| 9 | + |
| 10 | +## Compression (slow, through CPU) |
| 11 | + |
| 12 | +function SMC.compress( |
| 13 | + A::AbstractCuSparseMatrix, result::SMC.AbstractColoringResult{structure,:column} |
| 14 | +) where {structure} |
| 15 | + return CuMatrix(SMC.compress(SparseMatrixCSC(A), result)) |
| 16 | +end |
| 17 | + |
| 18 | +function SMC.compress( |
| 19 | + A::AbstractCuSparseMatrix, result::SMC.AbstractColoringResult{structure,:row} |
| 20 | +) where {structure} |
| 21 | + return CuMatrix(SMC.compress(SparseMatrixCSC(A), result)) |
| 22 | +end |
| 23 | + |
| 24 | +## CSC |
| 25 | + |
| 26 | +function SMC.ColumnColoringResult( |
| 27 | + A::CuSparseMatrixCSC, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer} |
| 28 | +) where {T<:Integer} |
| 29 | + A_cpu = SparseMatrixCSC(A) |
| 30 | + result_cpu = SMC.ColumnColoringResult(A_cpu, bg, color) |
| 31 | + compressed_indices = CuVector(result_cpu.compressed_indices) |
| 32 | + return SMC.ColumnColoringResult(A, bg, color, result_cpu.group, compressed_indices) |
| 33 | +end |
| 34 | + |
| 35 | +function SMC.RowColoringResult( |
| 36 | + A::CuSparseMatrixCSC, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer} |
| 37 | +) where {T<:Integer} |
| 38 | + A_cpu = SparseMatrixCSC(A) |
| 39 | + result_cpu = SMC.RowColoringResult(A_cpu, bg, color) |
| 40 | + compressed_indices = CuVector(result_cpu.compressed_indices) |
| 41 | + return SMC.RowColoringResult(A, bg, color, result_cpu.group, compressed_indices) |
| 42 | +end |
| 43 | + |
| 44 | +function SMC.StarSetColoringResult( |
| 45 | + A::CuSparseMatrixCSC, |
| 46 | + ag::SMC.AdjacencyGraph{T}, |
| 47 | + color::Vector{<:Integer}, |
| 48 | + star_set::SMC.StarSet{<:Integer}, |
| 49 | +) where {T<:Integer} |
| 50 | + A_cpu = SparseMatrixCSC(A) |
| 51 | + result_cpu = SMC.StarSetColoringResult(A_cpu, ag, color, star_set) |
| 52 | + compressed_indices = CuVector(result_cpu.compressed_indices) |
| 53 | + return SMC.StarSetColoringResult(A, ag, color, result_cpu.group, compressed_indices) |
| 54 | +end |
| 55 | + |
| 56 | +function SMC.decompress!( |
| 57 | + A::CuSparseMatrixCSC, B::CuMatrix, result::SMC.ColumnColoringResult{<:CuSparseMatrixCSC} |
| 58 | +) |
| 59 | + A.nzVal .= getindex.(Ref(B), result.compressed_indices) |
| 60 | + return A |
| 61 | +end |
| 62 | + |
| 63 | +function SMC.decompress!( |
| 64 | + A::CuSparseMatrixCSC, B::CuMatrix, result::SMC.RowColoringResult{<:CuSparseMatrixCSC} |
| 65 | +) |
| 66 | + A.nzVal .= getindex.(Ref(B), result.compressed_indices) |
| 67 | + return A |
| 68 | +end |
| 69 | + |
| 70 | +function SMC.decompress!( |
| 71 | + A::CuSparseMatrixCSC, |
| 72 | + B::CuMatrix, |
| 73 | + result::SMC.StarSetColoringResult{<:CuSparseMatrixCSC}, |
| 74 | +) |
| 75 | + A.nzVal .= getindex.(Ref(B), result.compressed_indices) |
| 76 | + return A |
| 77 | +end |
| 78 | + |
| 79 | +end |
0 commit comments