Skip to content

Optimized implementation for structured matrices V2#319

Draft
ErikQQY wants to merge 2 commits into
JuliaDiff:mainfrom
ErikQQY:qqy/structured3
Draft

Optimized implementation for structured matrices V2#319
ErikQQY wants to merge 2 commits into
JuliaDiff:mainfrom
ErikQQY:qqy/structured3

Conversation

@ErikQQY
Copy link
Copy Markdown

@ErikQQY ErikQQY commented Apr 29, 2026

Basically #139 with new implementations

end
return SMC.BipartiteGraph(S1, S2)
end

Copy link
Copy Markdown
Author

@ErikQQY ErikQQY Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gdalle For now I just want to make sure the structured coloring algorithms working for Diagonal, Bidiagonal and Tridiagonal matrices, but it turns out we still need to make transpose to be GPU-compatiable to allow colorings and decompression. Do these implementations look resonable to you, if do, I will continue on this direction.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, I think I need additional context to understand what you did here

@gdalle gdalle marked this pull request as draft May 10, 2026 08:54
Copy link
Copy Markdown
Member

@gdalle gdalle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for getting this started!

Comment on lines +274 to +276
B_rcolor_indices = mod1.(
(row_shift + 1):(row_shift + maximum(row_colors(res))), length(colorscheme)
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, it's better to avoid formatting changes that only add noise

Comment thread src/graph.jl
Comment on lines +20 to +21
colptr::AbstractVector{Ti}
rowval::AbstractVector{Ti}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really bad idea because it will introduce type instability throughout the coloring and decompression algorithms. Can you explain why you deem it necessary?
I hope the tests catch it, if not we need more type stability tests with JET

silent && set_silent(model)
# one variable per vertex to color, removing some renumbering symmetries
@variable(model, 1 <= color[i=1:n] <= i, Int)
@variable(model, 1 <= color[i = 1:n] <= i, Int)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about formatting noise

Comment thread test/utils.jl
if VERSION >= v"1.10" || A isa Union{Diagonal,Bidiagonal,Tridiagonal}
# banded matrices not supported by ArrayInterface on Julia 1.6
# @test color == ArrayInterface.matrix_colors(A) # TODO: uncomment
if algo isa StructuredColoringAlgorithm
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also restrict this to the matrix types that ArrayInterface supports

Comment thread test/utils.jl
# banded matrices not supported by ArrayInterface on Julia 1.6
# @test color == ArrayInterface.matrix_colors(A) # TODO: uncomment
if algo isa StructuredColoringAlgorithm
@test color == ArrayInterface.matrix_colors(A)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may also want to test that decompression returns a matrix of the correct type

Comment thread src/structured.jl
Comment on lines +69 to +75
function decompress!(A::Diagonal, B::AbstractMatrix, result::RowColoringResult)
color = row_colors(result)
for i in axes(A, 1)
A[i, i] = B[color[i], i]
end
return A
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work for a Diagonal matrix stored on GPU. My idea was that we could vectorize index accesses like we do for the CSC/CSR formats on GPU, and then do something like

diag(A) .= getindex.(B, color, 1:n)

Comment on lines +14 to +20
for R in (:Diagonal, :Bidiagonal, :Tridiagonal)
@eval function SMC.BipartiteGraph(
A::$R{T,<:CuArray}; symmetric_pattern::Bool=false
) where {T}
return SMC.BipartiteGraph(CuSparseMatrixCSC(A); symmetric_pattern)
end
end
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? The coloring will take place on the CPU anyway so I don't see why we would need a GPU-based graph structure

end
return SMC.BipartiteGraph(S1, S2)
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, I think I need additional context to understand what you did here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants