|
| 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 Result |
| 25 | + |
| 26 | +function SMC.ColumnColoringResult( |
| 27 | + A::CuSparseMatrixCSC, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer} |
| 28 | +) where {T<:Integer} |
| 29 | + group = SMC.group_by_color(T, color) |
| 30 | + compressed_indices = SMC.column_csc_indices(bg, color) |
| 31 | + additional_info = (; compressed_indices_gpu_csc=CuVector(compressed_indices)) |
| 32 | + return SMC.ColumnColoringResult( |
| 33 | + A, bg, color, group, compressed_indices, additional_info |
| 34 | + ) |
| 35 | +end |
| 36 | + |
| 37 | +function SMC.RowColoringResult( |
| 38 | + A::CuSparseMatrixCSC, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer} |
| 39 | +) where {T<:Integer} |
| 40 | + group = SMC.group_by_color(T, color) |
| 41 | + compressed_indices = SMC.row_csc_indices(bg, color) |
| 42 | + additional_info = (; compressed_indices_gpu_csc=CuVector(compressed_indices)) |
| 43 | + return SMC.RowColoringResult(A, bg, color, group, compressed_indices, additional_info) |
| 44 | +end |
| 45 | + |
| 46 | +function SMC.StarSetColoringResult( |
| 47 | + A::CuSparseMatrixCSC, |
| 48 | + ag::SMC.AdjacencyGraph{T}, |
| 49 | + color::Vector{<:Integer}, |
| 50 | + star_set::SMC.StarSet{<:Integer}, |
| 51 | +) where {T<:Integer} |
| 52 | + group = SMC.group_by_color(T, color) |
| 53 | + compressed_indices = SMC.star_csc_indices(ag, color, star_set) |
| 54 | + additional_info = (; compressed_indices_gpu_csc=CuVector(compressed_indices)) |
| 55 | + return SMC.StarSetColoringResult( |
| 56 | + A, ag, color, group, compressed_indices, additional_info |
| 57 | + ) |
| 58 | +end |
| 59 | + |
| 60 | +## CSR Result |
| 61 | + |
| 62 | +function SMC.ColumnColoringResult( |
| 63 | + A::CuSparseMatrixCSR, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer} |
| 64 | +) where {T<:Integer} |
| 65 | + group = SMC.group_by_color(T, color) |
| 66 | + compressed_indices = SMC.column_csc_indices(bg, color) |
| 67 | + compressed_indices_csr = SMC.column_csr_indices(bg, color) |
| 68 | + additional_info = (; compressed_indices_gpu_csr=CuVector(compressed_indices_csr)) |
| 69 | + return SMC.ColumnColoringResult( |
| 70 | + A, bg, color, group, compressed_indices, additional_info |
| 71 | + ) |
| 72 | +end |
| 73 | + |
| 74 | +function SMC.RowColoringResult( |
| 75 | + A::CuSparseMatrixCSR, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer} |
| 76 | +) where {T<:Integer} |
| 77 | + group = SMC.group_by_color(T, color) |
| 78 | + compressed_indices = SMC.row_csc_indices(bg, color) |
| 79 | + compressed_indices_csr = SMC.row_csr_indices(bg, color) |
| 80 | + additional_info = (; compressed_indices_gpu_csr=CuVector(compressed_indices_csr)) |
| 81 | + return SMC.RowColoringResult(A, bg, color, group, compressed_indices, additional_info) |
| 82 | +end |
| 83 | + |
| 84 | +function SMC.StarSetColoringResult( |
| 85 | + A::CuSparseMatrixCSR, |
| 86 | + ag::SMC.AdjacencyGraph{T}, |
| 87 | + color::Vector{<:Integer}, |
| 88 | + star_set::SMC.StarSet{<:Integer}, |
| 89 | +) where {T<:Integer} |
| 90 | + group = SMC.group_by_color(T, color) |
| 91 | + compressed_indices = SMC.star_csc_indices(ag, color, star_set) |
| 92 | + additional_info = (; compressed_indices_gpu_csr=CuVector(compressed_indices)) |
| 93 | + return SMC.StarSetColoringResult( |
| 94 | + A, ag, color, group, compressed_indices, additional_info |
| 95 | + ) |
| 96 | +end |
| 97 | + |
| 98 | +## Decompression |
| 99 | + |
| 100 | +for R in (:ColumnColoringResult, :RowColoringResult) |
| 101 | + # loop to avoid method ambiguity |
| 102 | + @eval function SMC.decompress!( |
| 103 | + A::CuSparseMatrixCSC, B::CuMatrix, result::SMC.$R{<:CuSparseMatrixCSC} |
| 104 | + ) |
| 105 | + compressed_indices = result.additional_info.compressed_indices_gpu_csc |
| 106 | + copyto!(A.nzVal, view(B, compressed_indices)) |
| 107 | + return A |
| 108 | + end |
| 109 | + |
| 110 | + @eval function SMC.decompress!( |
| 111 | + A::CuSparseMatrixCSR, B::CuMatrix, result::SMC.$R{<:CuSparseMatrixCSR} |
| 112 | + ) |
| 113 | + compressed_indices = result.additional_info.compressed_indices_gpu_csr |
| 114 | + copyto!(A.nzVal, view(B, compressed_indices)) |
| 115 | + return A |
| 116 | + end |
| 117 | +end |
| 118 | + |
| 119 | +function SMC.decompress!( |
| 120 | + A::CuSparseMatrixCSC, |
| 121 | + B::CuMatrix, |
| 122 | + result::SMC.StarSetColoringResult{<:CuSparseMatrixCSC}, |
| 123 | + uplo::Symbol=:F, |
| 124 | +) |
| 125 | + if uplo != :F |
| 126 | + throw( |
| 127 | + SMC.UnsupportedDecompressionError( |
| 128 | + "Single-triangle decompression is not supported on GPU matrices" |
| 129 | + ), |
| 130 | + ) |
| 131 | + end |
| 132 | + compressed_indices = result.additional_info.compressed_indices_gpu_csc |
| 133 | + copyto!(A.nzVal, view(B, compressed_indices)) |
| 134 | + return A |
| 135 | +end |
| 136 | + |
| 137 | +function SMC.decompress!( |
| 138 | + A::CuSparseMatrixCSR, |
| 139 | + B::CuMatrix, |
| 140 | + result::SMC.StarSetColoringResult{<:CuSparseMatrixCSR}, |
| 141 | + uplo::Symbol=:F, |
| 142 | +) |
| 143 | + if uplo != :F |
| 144 | + throw( |
| 145 | + SMC.UnsupportedDecompressionError( |
| 146 | + "Single-triangle decompression is not supported on GPU matrices" |
| 147 | + ), |
| 148 | + ) |
| 149 | + end |
| 150 | + compressed_indices = result.additional_info.compressed_indices_gpu_csr |
| 151 | + copyto!(A.nzVal, view(B, compressed_indices)) |
| 152 | + return A |
| 153 | +end |
| 154 | + |
| 155 | +end |
0 commit comments