-
Notifications
You must be signed in to change notification settings - Fork 9
Optimized implementation for structured matrices V2 #319
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
ErikQQY
wants to merge
2
commits into
JuliaDiff:main
Choose a base branch
from
ErikQQY:qqy/structured3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,45 @@ | ||
| module SparseMatrixColoringsBandedMatricesExt | ||
|
|
||
| using BandedMatrices: BandedMatrix, bandrange, bandwidths, colrange, rowrange | ||
| using SparseMatrixColorings: | ||
| BipartiteGraph, | ||
| ColoringProblem, | ||
| ColumnColoringResult, | ||
| StructuredColoringAlgorithm, | ||
| RowColoringResult, | ||
| column_colors, | ||
| cycle_range, | ||
| row_colors | ||
| import SparseMatrixColorings as SMC | ||
|
|
||
| #= | ||
| This code is partly taken from ArrayInterface.jl and FiniteDiff.jl | ||
| https://github.com/JuliaArrays/ArrayInterface.jl | ||
| https://github.com/JuliaDiff/FiniteDiff.jl | ||
| =# | ||
|
|
||
| function SMC.coloring( | ||
| A::BandedMatrix, | ||
| ::ColoringProblem{:nonsymmetric,:column}, | ||
| ::StructuredColoringAlgorithm; | ||
| kwargs..., | ||
| ) | ||
| width = length(bandrange(A)) | ||
| color = cycle_range(width, size(A, 2)) | ||
| bg = BipartiteGraph(A) | ||
| return ColumnColoringResult(A, bg, color) | ||
| end | ||
|
|
||
| function SMC.coloring( | ||
| A::BandedMatrix, | ||
| ::ColoringProblem{:nonsymmetric,:row}, | ||
| ::StructuredColoringAlgorithm; | ||
| kwargs..., | ||
| ) | ||
| width = length(bandrange(A)) | ||
| color = cycle_range(width, size(A, 1)) | ||
| bg = BipartiteGraph(A) | ||
| return RowColoringResult(A, bg, color) | ||
| 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,97 @@ | ||
| module SparseMatrixColoringsBlockBandedMatricesExt | ||
|
|
||
| using BlockArrays: blockaxes, blockfirsts, blocklasts, blocksize, blocklengths | ||
| using BlockBandedMatrices: | ||
| BandedBlockBandedMatrix, | ||
| BlockBandedMatrix, | ||
| blockbandrange, | ||
| blockbandwidths, | ||
| blocklengths, | ||
| blocksize, | ||
| subblockbandwidths | ||
| using SparseMatrixColorings: | ||
| BipartiteGraph, | ||
| ColoringProblem, | ||
| ColumnColoringResult, | ||
| StructuredColoringAlgorithm, | ||
| RowColoringResult, | ||
| column_colors, | ||
| cycle_range, | ||
| row_colors | ||
| import SparseMatrixColorings as SMC | ||
|
|
||
| #= | ||
| This code is partly taken from ArrayInterface.jl and FiniteDiff.jl | ||
| https://github.com/JuliaArrays/ArrayInterface.jl | ||
| https://github.com/JuliaDiff/FiniteDiff.jl | ||
| =# | ||
|
|
||
| function subblockbandrange(A::BandedBlockBandedMatrix) | ||
| u, l = subblockbandwidths(A) | ||
| return (-l):u | ||
| end | ||
|
|
||
| function blockbanded_coloring( | ||
| A::Union{BlockBandedMatrix,BandedBlockBandedMatrix}, dim::Integer | ||
| ) | ||
| # consider blocks of columns or rows (let's call them vertices) depending on `dim` | ||
| nb_blocks = blocksize(A, dim) | ||
| nb_in_block = blocklengths(axes(A, dim)) | ||
| first_in_block = blockfirsts(axes(A, dim)) | ||
| last_in_block = blocklasts(axes(A, dim)) | ||
| color = zeros(Int, size(A, dim)) | ||
|
|
||
| # give a macroscopic color to each block, so that 2 blocks with the same macro color are orthogonal | ||
| # same idea as for BandedMatrices | ||
| nb_macrocolors = length(blockbandrange(A)) | ||
| macrocolor = cycle_range(nb_macrocolors, nb_blocks) | ||
|
|
||
| width = if A isa BandedBlockBandedMatrix | ||
| # vertices within a block are colored cleverly using bands | ||
| length(subblockbandrange(A)) | ||
| else | ||
| # vertices within a block are colored naively with distinct micro colors (~ infinite band width) | ||
| typemax(Int) | ||
| end | ||
|
|
||
| # for each macroscopic color, count how many microscopic colors will be needed | ||
| nb_colors_in_macrocolor = zeros(Int, nb_macrocolors) | ||
| for mc in 1:nb_macrocolors | ||
| largest_nb_in_macrocolor = maximum(nb_in_block[mc:nb_macrocolors:nb_blocks]; init=0) | ||
| nb_colors_in_macrocolor[mc] = min(width, largest_nb_in_macrocolor) | ||
| end | ||
| color_shift_in_macrocolor = vcat(0, cumsum(nb_colors_in_macrocolor)[1:(end - 1)]) | ||
|
|
||
| # assign a microscopic color to each column as a function of its macroscopic color and its position within the block | ||
| for b in 1:nb_blocks | ||
| block_color = cycle_range(width, nb_in_block[b]) | ||
| shift = color_shift_in_macrocolor[macrocolor[b]] | ||
| color[first_in_block[b]:last_in_block[b]] .= shift .+ block_color | ||
| end | ||
|
|
||
| return color | ||
| end | ||
|
|
||
| function SMC.coloring( | ||
| A::Union{BlockBandedMatrix,BandedBlockBandedMatrix}, | ||
| ::ColoringProblem{:nonsymmetric,:column}, | ||
| ::StructuredColoringAlgorithm; | ||
| kwargs..., | ||
| ) | ||
| color = blockbanded_coloring(A, 2) | ||
| bg = BipartiteGraph(A) | ||
| return ColumnColoringResult(A, bg, color) | ||
| end | ||
|
|
||
| function SMC.coloring( | ||
| A::Union{BlockBandedMatrix,BandedBlockBandedMatrix}, | ||
| ::ColoringProblem{:nonsymmetric,:row}, | ||
| ::StructuredColoringAlgorithm; | ||
| kwargs..., | ||
| ) | ||
| color = blockbanded_coloring(A, 1) | ||
| bg = BipartiteGraph(A) | ||
| return RowColoringResult(A, bg, color) | ||
| 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
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
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.
There was a problem hiding this comment.
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
transposeto be GPU-compatiable to allow colorings and decompression. Do these implementations look resonable to you, if do, I will continue on this direction.