|
| 1 | +using ADTypes: column_coloring, row_coloring, symmetric_coloring |
| 2 | +using JET |
| 3 | +using LinearAlgebra |
| 4 | +using SparseArrays |
| 5 | +using SparseMatrixColorings |
| 6 | +using SparseMatrixColorings: respectful_similar |
| 7 | +using StableRNGs |
| 8 | +using Test |
| 9 | + |
| 10 | +rng = StableRNG(63) |
| 11 | + |
| 12 | +@testset "Coloring" begin |
| 13 | + n = 10 |
| 14 | + A = sprand(rng, n, n, 5 / n) |
| 15 | + |
| 16 | + # ADTypes |
| 17 | + @testset "ADTypes" begin |
| 18 | + @test_opt target_modules = (SparseMatrixColorings,) column_coloring( |
| 19 | + A, GreedyColoringAlgorithm() |
| 20 | + ) |
| 21 | + @test_opt target_modules = (SparseMatrixColorings,) row_coloring( |
| 22 | + A, GreedyColoringAlgorithm() |
| 23 | + ) |
| 24 | + @test_opt target_modules = (SparseMatrixColorings,) symmetric_coloring( |
| 25 | + Symmetric(A), GreedyColoringAlgorithm() |
| 26 | + ) |
| 27 | + end |
| 28 | + |
| 29 | + @testset "$structure - $partition - $decompression" for ( |
| 30 | + structure, partition, decompression |
| 31 | + ) in [ |
| 32 | + (:nonsymmetric, :column, :direct), |
| 33 | + (:nonsymmetric, :row, :direct), |
| 34 | + (:symmetric, :column, :direct), |
| 35 | + (:symmetric, :column, :substitution), |
| 36 | + ] |
| 37 | + @test_opt target_modules = (SparseMatrixColorings,) coloring( |
| 38 | + A, |
| 39 | + ColoringProblem(; structure, partition), |
| 40 | + GreedyColoringAlgorithm(; decompression), |
| 41 | + ) |
| 42 | + end |
| 43 | +end |
| 44 | + |
| 45 | +@testset "Decompression" begin |
| 46 | + n = 10 |
| 47 | + A0 = sparse(Symmetric(sprand(rng, n, n, 5 / n))) |
| 48 | + |
| 49 | + @testset "$structure - $partition - $decompression" for ( |
| 50 | + structure, partition, decompression |
| 51 | + ) in [ |
| 52 | + (:nonsymmetric, :column, :direct), |
| 53 | + (:nonsymmetric, :row, :direct), |
| 54 | + (:symmetric, :column, :direct), |
| 55 | + (:symmetric, :column, :substitution), |
| 56 | + ] |
| 57 | + @testset "A::$(typeof(A))" for A in matrix_versions(A0) |
| 58 | + result = coloring( |
| 59 | + A0, |
| 60 | + ColoringProblem(; structure, partition), |
| 61 | + GreedyColoringAlgorithm(; decompression); |
| 62 | + decompression_eltype=eltype(A), |
| 63 | + ) |
| 64 | + B = compress(A, result) |
| 65 | + @testset "Full decompression" begin |
| 66 | + @test_opt compress(A, result) |
| 67 | + @test_opt decompress(B, result) ≈ A0 |
| 68 | + @test_opt decompress!(respectful_similar(A), B, result) |
| 69 | + end |
| 70 | + @testset "Single-color decompression" begin |
| 71 | + if decompression == :direct |
| 72 | + b = if partition == :column |
| 73 | + B[:, 1] |
| 74 | + else |
| 75 | + B[1, :] |
| 76 | + end |
| 77 | + @test_opt decompress_single_color!(respectful_similar(A), b, 1, result) |
| 78 | + end |
| 79 | + end |
| 80 | + @testset "Triangle decompression" begin |
| 81 | + if structure == :symmetric |
| 82 | + @test_opt decompress!(respectful_similar(A), B, result, :U) |
| 83 | + end |
| 84 | + end |
| 85 | + @testset "Single-color triangle decompression" begin |
| 86 | + if structure == :symmetric && decompression == :direct |
| 87 | + @test_opt decompress_single_color!( |
| 88 | + respectful_similar(A), B[:, 1], 1, result, :U |
| 89 | + ) |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | +end; |
0 commit comments