Skip to content

Support sparse matrices from rocSPARSE #255

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[weakdeps]
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
CliqueTrees = "60701a23-6482-424a-84db-faee86b9b1f8"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"

[extensions]
SparseMatrixColoringsAMDGPUExt = "AMDGPU"
SparseMatrixColoringsCUDAExt = "CUDA"
SparseMatrixColoringsCliqueTreesExt = "CliqueTrees"
SparseMatrixColoringsColorsExt = "Colors"

[compat]
ADTypes = "1.2.1"
AMDGPU = "1.3.3"
CUDA = "5.8.2"
CliqueTrees = "1"
Colors = "0.12.11, 0.13"
DocStringExtensions = "0.8,0.9"
LinearAlgebra = "<0.0.1, 1"
LinearAlgebra = "1.10"
PrecompileTools = "1.2.1"
Random = "<0.0.1, 1"
SparseArrays = "<0.0.1, 1"
Random = "1.10"
SparseArrays = "1.10"
julia = "1.10"
155 changes: 155 additions & 0 deletions ext/SparseMatrixColoringsAMDGPUExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
module SparseMatrixColoringsAMDGPUExt

import SparseMatrixColorings as SMC
using SparseArrays: SparseMatrixCSC, rowvals, nnz, nzrange
using AMDGPU: ROCVector, ROCMatrix
using AMDGPU.rocSPARSE: AbstractROCSparseMatrix, ROCSparseMatrixCSC, ROCSparseMatrixCSR

SMC.matrix_versions(A::AbstractROCSparseMatrix) = (A,)

Check warning on line 8 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L8

Added line #L8 was not covered by tests

## Compression (slow, through CPU)

function SMC.compress(

Check warning on line 12 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L12

Added line #L12 was not covered by tests
A::AbstractROCSparseMatrix, result::SMC.AbstractColoringResult{structure,:column}
) where {structure}
return ROCMatrix(SMC.compress(SparseMatrixCSC(A), result))

Check warning on line 15 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L15

Added line #L15 was not covered by tests
end

function SMC.compress(

Check warning on line 18 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L18

Added line #L18 was not covered by tests
A::AbstractROCSparseMatrix, result::SMC.AbstractColoringResult{structure,:row}
) where {structure}
return ROCMatrix(SMC.compress(SparseMatrixCSC(A), result))

Check warning on line 21 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L21

Added line #L21 was not covered by tests
end

## CSC Result

function SMC.ColumnColoringResult(

Check warning on line 26 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L26

Added line #L26 was not covered by tests
A::ROCSparseMatrixCSC, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer}
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.column_csc_indices(bg, color)
additional_info = (; compressed_indices_gpu_csc=ROCVector(compressed_indices))
return SMC.ColumnColoringResult(

Check warning on line 32 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L29-L32

Added lines #L29 - L32 were not covered by tests
A, bg, color, group, compressed_indices, additional_info
)
end

function SMC.RowColoringResult(

Check warning on line 37 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L37

Added line #L37 was not covered by tests
A::ROCSparseMatrixCSC, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer}
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.row_csc_indices(bg, color)
additional_info = (; compressed_indices_gpu_csc=ROCVector(compressed_indices))
return SMC.RowColoringResult(A, bg, color, group, compressed_indices, additional_info)

Check warning on line 43 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L40-L43

Added lines #L40 - L43 were not covered by tests
end

function SMC.StarSetColoringResult(

Check warning on line 46 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L46

Added line #L46 was not covered by tests
A::ROCSparseMatrixCSC,
ag::SMC.AdjacencyGraph{T},
color::Vector{<:Integer},
star_set::SMC.StarSet{<:Integer},
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.star_csc_indices(ag, color, star_set)
additional_info = (; compressed_indices_gpu_csc=ROCVector(compressed_indices))
return SMC.StarSetColoringResult(

Check warning on line 55 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L52-L55

Added lines #L52 - L55 were not covered by tests
A, ag, color, group, compressed_indices, additional_info
)
end

## CSR Result

function SMC.ColumnColoringResult(

Check warning on line 62 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L62

Added line #L62 was not covered by tests
A::ROCSparseMatrixCSR, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer}
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.column_csc_indices(bg, color)
compressed_indices_csr = SMC.column_csr_indices(bg, color)
additional_info = (; compressed_indices_gpu_csr=ROCVector(compressed_indices_csr))
return SMC.ColumnColoringResult(

Check warning on line 69 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L65-L69

Added lines #L65 - L69 were not covered by tests
A, bg, color, group, compressed_indices, additional_info
)
end

function SMC.RowColoringResult(

Check warning on line 74 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L74

Added line #L74 was not covered by tests
A::ROCSparseMatrixCSR, bg::SMC.BipartiteGraph{T}, color::Vector{<:Integer}
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.row_csc_indices(bg, color)
compressed_indices_csr = SMC.row_csr_indices(bg, color)
additional_info = (; compressed_indices_gpu_csr=ROCVector(compressed_indices_csr))
return SMC.RowColoringResult(A, bg, color, group, compressed_indices, additional_info)

Check warning on line 81 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L77-L81

Added lines #L77 - L81 were not covered by tests
end

function SMC.StarSetColoringResult(

Check warning on line 84 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L84

Added line #L84 was not covered by tests
A::ROCSparseMatrixCSR,
ag::SMC.AdjacencyGraph{T},
color::Vector{<:Integer},
star_set::SMC.StarSet{<:Integer},
) where {T<:Integer}
group = SMC.group_by_color(T, color)
compressed_indices = SMC.star_csc_indices(ag, color, star_set)
additional_info = (; compressed_indices_gpu_csr=ROCVector(compressed_indices))
return SMC.StarSetColoringResult(

Check warning on line 93 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L90-L93

Added lines #L90 - L93 were not covered by tests
A, ag, color, group, compressed_indices, additional_info
)
end

## Decompression

for R in (:ColumnColoringResult, :RowColoringResult)
# loop to avoid method ambiguity
@eval function SMC.decompress!(

Check warning on line 102 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L102

Added line #L102 was not covered by tests
A::ROCSparseMatrixCSC, B::ROCMatrix, result::SMC.$R{<:ROCSparseMatrixCSC}
)
compressed_indices = result.additional_info.compressed_indices_gpu_csc
copyto!(A.nzVal, view(B, compressed_indices))
return A

Check warning on line 107 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L105-L107

Added lines #L105 - L107 were not covered by tests
end

@eval function SMC.decompress!(

Check warning on line 110 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L110

Added line #L110 was not covered by tests
A::ROCSparseMatrixCSR, B::ROCMatrix, result::SMC.$R{<:ROCSparseMatrixCSR}
)
compressed_indices = result.additional_info.compressed_indices_gpu_csr
copyto!(A.nzVal, view(B, compressed_indices))
return A

Check warning on line 115 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L113-L115

Added lines #L113 - L115 were not covered by tests
end
end

function SMC.decompress!(

Check warning on line 119 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L119

Added line #L119 was not covered by tests
A::ROCSparseMatrixCSC,
B::ROCMatrix,
result::SMC.StarSetColoringResult{<:ROCSparseMatrixCSC},
uplo::Symbol=:F,
)
if uplo != :F
throw(

Check warning on line 126 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L125-L126

Added lines #L125 - L126 were not covered by tests
SMC.UnsupportedDecompressionError(
"Single-triangle decompression is not supported on GPU matrices"
),
)
end
compressed_indices = result.additional_info.compressed_indices_gpu_csc
copyto!(A.nzVal, view(B, compressed_indices))
return A

Check warning on line 134 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L132-L134

Added lines #L132 - L134 were not covered by tests
end

function SMC.decompress!(

Check warning on line 137 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L137

Added line #L137 was not covered by tests
A::ROCSparseMatrixCSR,
B::ROCMatrix,
result::SMC.StarSetColoringResult{<:ROCSparseMatrixCSR},
uplo::Symbol=:F,
)
if uplo != :F
throw(

Check warning on line 144 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L143-L144

Added lines #L143 - L144 were not covered by tests
SMC.UnsupportedDecompressionError(
"Single-triangle decompression is not supported on GPU matrices"
),
)
end
compressed_indices = result.additional_info.compressed_indices_gpu_csr
copyto!(A.nzVal, view(B, compressed_indices))
return A

Check warning on line 152 in ext/SparseMatrixColoringsAMDGPUExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/SparseMatrixColoringsAMDGPUExt.jl#L150-L152

Added lines #L150 - L152 were not covered by tests
end

end
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
Expand Down
62 changes: 62 additions & 0 deletions test/rocm.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using AMDGPU.rocSPARSE: ROCSparseMatrixCSC, ROCSparseMatrixCSR
using LinearAlgebra
using SparseArrays
using SparseMatrixColorings
import SparseMatrixColorings as SMC
using StableRNGs
using Test

include("utils.jl")

rng = StableRNG(63)

asymmetric_params = vcat(
[(10, 20, p) for p in (0.0:0.2:0.5)],
[(20, 10, p) for p in (0.0:0.2:0.5)],
[(100, 200, p) for p in (0.01:0.02:0.05)],
[(200, 100, p) for p in (0.01:0.02:0.05)],
)

symmetric_params = vcat(
[(10, p) for p in (0.0:0.2:0.5)], #
[(100, p) for p in (0.01:0.02:0.05)],
)

@testset verbose = true "Column coloring & decompression" begin
problem = ColoringProblem(; structure=:nonsymmetric, partition=:column)
algo = GreedyColoringAlgorithm(; decompression=:direct)
@testset for T in (ROCSparseMatrixCSC, ROCSparseMatrixCSR)
@testset "$((; m, n, p))" for (m, n, p) in asymmetric_params
A0 = T(sprand(rng, m, n, p))
test_coloring_decompression(A0, problem, algo; gpu=true)
end
end
end;

@testset verbose = true "Row coloring & decompression" begin
problem = ColoringProblem(; structure=:nonsymmetric, partition=:row)
algo = GreedyColoringAlgorithm(; decompression=:direct)
@testset for T in (ROCSparseMatrixCSC, ROCSparseMatrixCSR)
@testset "$((; m, n, p))" for (m, n, p) in asymmetric_params
A0 = T(sprand(rng, m, n, p))
test_coloring_decompression(A0, problem, algo; gpu=true)
end
end
end;

@testset verbose = true "Symmetric coloring & direct decompression" begin
problem = ColoringProblem(; structure=:symmetric, partition=:column)
algo = GreedyColoringAlgorithm(; postprocessing=false, decompression=:direct)
@testset for T in (ROCSparseMatrixCSC, ROCSparseMatrixCSR)
@testset "$((; n, p))" for (n, p) in symmetric_params
A0 = T(sparse(Symmetric(sprand(rng, n, n, p))))
test_coloring_decompression(A0, problem, algo; gpu=true)
end
A0 = T(sparse(Diagonal(ones(10))))
result = coloring(A0, problem, algo)
B = compress(A0, result)
@test_throws SMC.UnsupportedDecompressionError decompress!(
similar(A0), B, result, :U
)
end
end;
10 changes: 9 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ include("utils.jl")
if get(ENV, "JULIA_SMC_TEST_GROUP", nothing) == "GPU"
@testset "CUDA" begin
using CUDA
include("cuda.jl")
if CUDA.functional()
include("cuda.jl")
end
end
@testset "ROCm" begin
using AMDGPU
if AMDGPU.functional()
include("rocm.jl")
end
end
else
@testset verbose = true "Code quality" begin
Expand Down
Loading