From c1729f2690f227dd062d8e4f338a7d8cb395238d Mon Sep 17 00:00:00 2001 From: Chris Rackauckas Date: Fri, 2 Aug 2019 14:46:20 -0400 Subject: [PATCH] Complete colorvec transition --- README.md | 24 ++++++++++++------------ src/coloring/backtracking_coloring.jl | 8 ++++---- src/coloring/greedy_star1_coloring.jl | 22 +++++++++++----------- src/coloring/greedy_star2_coloring.jl | 20 ++++++++++---------- src/coloring/high_level.jl | 2 +- test/runtests.jl | 2 +- test/test_ad.jl | 10 +++++----- test/test_gpu_ad.jl | 4 ++-- test/test_integration.jl | 4 ++-- test/test_specialmatrices.jl | 4 ++-- 10 files changed, 50 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 0beccad0..42b1a856 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ sparsity_pattern = sparsity!(f,output,input) jac = Float64.(sparse(sparsity_pattern)) ``` -Now we call `matrix_colors` to get the color vector for that matrix: +Now we call `matrix_colors` to get the colorvec vector for that matrix: ```julia colors = matrix_colors(jac) @@ -54,14 +54,14 @@ Since `maximum(colors)` is 3, this means that finite differencing can now compute the Jacobian in just 4 `f`-evaluations: ```julia -DiffEqDiffTools.finite_difference_jacobian!(jac, f, rand(30), color=colors) +DiffEqDiffTools.finite_difference_jacobian!(jac, f, rand(30), colorvec=colors) @show fcalls # 4 ``` In addition, a faster forward-mode autodiff call can be utilized as well: ```julia -forwarddiff_color_jacobian!(jac, f, x, color = colors) +forwarddiff_color_jacobian!(jac, f, x, colorvec = colors) ``` If one only need to compute products, one can use the operators. For example, @@ -102,7 +102,7 @@ requires an `f` call to `maximum(colors)+1`, or reduces automatic differentiatio to using `maximum(colors)` partials. Since normally these values are `length(x)`, this can be significant savings. -The API for computing the color vector is: +The API for computing the colorvec vector is: ```julia matrix_colors(A::AbstractMatrix,alg::ColoringAlgorithm = GreedyD1Color(); @@ -113,23 +113,23 @@ The first argument is the abstract matrix which represents the sparsity pattern of the Jacobian. The second argument is the optional choice of coloring algorithm. It will default to a greedy distance 1 coloring, though if your special matrix type has more information, like is a `Tridiagonal` or `BlockBandedMatrix`, the -color vector will be analytically calculated instead. The keyword argument +colorvec vector will be analytically calculated instead. The keyword argument `partition_by_rows` allows you to partition the Jacobian on the basis of rows instead of columns and generate a corresponding coloring vector which can be used for reverse-mode AD. Default value is false. -The result is a vector which assigns a color to each column (or row) of the matrix. +The result is a vector which assigns a colorvec to each column (or row) of the matrix. -### Color-Assisted Differentiation +### Colorvec-Assisted Differentiation -Color-assisted differentiation for numerical differentiation is provided by +Colorvec-assisted differentiation for numerical differentiation is provided by DiffEqDiffTools.jl and for automatic differentiation is provided by ForwardDiff.jl. -For DiffEqDiffTools.jl, one simply has to use the provided `color` keyword +For DiffEqDiffTools.jl, one simply has to use the provided `colorvec` keyword argument. See [the DiffEqDiffTools Jacobian documentation](https://github.com/JuliaDiffEq/DiffEqDiffTools.jl#jacobians) for more details. -For forward-mode automatic differentiation, use of a color vector is provided +For forward-mode automatic differentiation, use of a colorvec vector is provided by the following function: ```julia @@ -137,7 +137,7 @@ forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number}, f, x::AbstractArray{<:Number}; dx = nothing, - color = eachindex(x), + colorvec = eachindex(x), sparsity = nothing) ``` @@ -147,7 +147,7 @@ cache, construct the cache in advance: ```julia ForwardColorJacCache(f,x,_chunksize = nothing; dx = nothing, - color=1:length(x), + colorvec=1:length(x), sparsity = nothing) ``` diff --git a/src/coloring/backtracking_coloring.jl b/src/coloring/backtracking_coloring.jl index 4f4e8cb3..f1d60f6c 100644 --- a/src/coloring/backtracking_coloring.jl +++ b/src/coloring/backtracking_coloring.jl @@ -20,7 +20,7 @@ function color_graph(g::LightGraphs.AbstractGraph, ::BacktrackingColor) start = 1 - #optimal color number + #optimal chromatic number opt = v + 1 #current vertex to be colored @@ -135,7 +135,7 @@ end opt::Integer) Returns set of free colors of x which are less -than optimal color number (opt) +than optimal chromatic number (opt) Arguments: @@ -192,7 +192,7 @@ end least_index(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, opt::Integer) Returns least index i such that color of vertex -A[i] is equal to `opt` (optimal color number) +A[i] is equal to `opt` (optimal chromatic number) """ function least_index(F::AbstractVector{<:Integer}, A::AbstractVector{<:Integer}, opt::Integer) for i in eachindex(A) @@ -218,7 +218,7 @@ end remove_higher_colors(U::AbstractVector{<:Integer}, opt::Integer) Remove all the colors which are greater than or -equal to the `opt` (optimal color number) from +equal to the `opt` (optimal chromatic number) from the set of colors U """ function remove_higher_colors(U::AbstractVector{<:Integer}, opt::Integer) diff --git a/src/coloring/greedy_star1_coloring.jl b/src/coloring/greedy_star1_coloring.jl index b0286760..172b47ce 100644 --- a/src/coloring/greedy_star1_coloring.jl +++ b/src/coloring/greedy_star1_coloring.jl @@ -23,26 +23,26 @@ """ function color_graph(g::LightGraphs.AbstractGraph, ::GreedyStar1Color) v = nv(g) - color = zeros(Int, v) + colorvec = zeros(Int, v) forbidden_colors = zeros(Int, v+1) for vertex_i = vertices(g) for w in inneighbors(g, vertex_i) - if color[w] != 0 - forbidden_colors[color[w]] = vertex_i + if colorvec[w] != 0 + forbidden_colors[colorvec[w]] = vertex_i end for x in inneighbors(g, w) - if color[x] != 0 - if color[w] == 0 - forbidden_colors[color[x]] = vertex_i + if colorvec[x] != 0 + if colorvec[w] == 0 + forbidden_colors[colorvec[x]] = vertex_i else for y in inneighbors(g, x) - if color[y] != 0 - if y != w && color[y] == color[w] - forbidden_colors[color[x]] = vertex_i + if colorvec[y] != 0 + if y != w && colorvec[y] == colorvec[w] + forbidden_colors[colorvec[x]] = vertex_i break end end @@ -52,10 +52,10 @@ function color_graph(g::LightGraphs.AbstractGraph, ::GreedyStar1Color) end end - color[vertex_i] = find_min_color(forbidden_colors, vertex_i) + colorvec[vertex_i] = find_min_color(forbidden_colors, vertex_i) end - color + colorvec end function find_min_color(forbidden_colors::AbstractVector, vertex_i::Integer) diff --git a/src/coloring/greedy_star2_coloring.jl b/src/coloring/greedy_star2_coloring.jl index 477e2211..e0c5d714 100644 --- a/src/coloring/greedy_star2_coloring.jl +++ b/src/coloring/greedy_star2_coloring.jl @@ -26,32 +26,32 @@ """ function color_graph(g::LightGraphs.AbstractGraph, :: GreedyStar2Color) v = nv(g) - color = zeros(Int, v) + colorvec = zeros(Int, v) forbidden_colors = zeros(Int, v+1) for vertex_i = vertices(g) for w in inneighbors(g, vertex_i) - if color[w] != 0 - forbidden_colors[color[w]] = vertex_i + if colorvec[w] != 0 + forbidden_colors[colorvec[w]] = vertex_i end for x in inneighbors(g, w) - if color[x] != 0 - if color[w] == 0 - forbidden_colors[color[x]] = vertex_i + if colorvec[x] != 0 + if colorvec[w] == 0 + forbidden_colors[colorvec[x]] = vertex_i else - if color[x] < color[w] - forbidden_colors[color[x]] = vertex_i + if colorvec[x] < colorvec[w] + forbidden_colors[colorvec[x]] = vertex_i end end end end end - color[vertex_i] = find_min_color(forbidden_colors, vertex_i) + colorvec[vertex_i] = find_min_color(forbidden_colors, vertex_i) end - color + colorvec end diff --git a/src/coloring/high_level.jl b/src/coloring/high_level.jl index 83a16c44..25077205 100644 --- a/src/coloring/high_level.jl +++ b/src/coloring/high_level.jl @@ -8,7 +8,7 @@ struct GreedyStar2Color <: SparseDiffToolsColoringAlgorithm end """ matrix_colors(A,alg::ColoringAlgorithm = GreedyD1Color()) - Returns the color vector for the matrix A using the chosen coloring + Returns the colorvec vector for the matrix A using the chosen coloring algorithm. If a known analytical solution exists, that is used instead. The coloring defaults to a greedy distance-1 coloring. diff --git a/test/runtests.jl b/test/runtests.jl index df43cf34..4b1aaab5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,7 +9,7 @@ if GROUP == "All" @time @safetestset "Greedy distance-1 coloring" begin include("test_greedy_d1.jl") end @time @safetestset "Greedy star coloring" begin include("test_greedy_star.jl") end @time @safetestset "Matrix to graph conversion" begin include("test_matrix2graph.jl") end - @time @safetestset "AD using color vector" begin include("test_ad.jl") end + @time @safetestset "AD using colorvec vector" begin include("test_ad.jl") end @time @safetestset "Integration test" begin include("test_integration.jl") end @time @safetestset "Special matrices" begin include("test_specialmatrices.jl") end @time @safetestset "Jac Vecs and Hes Vecs" begin include("test_jaches_products.jl") end diff --git a/test/test_ad.jl b/test/test_ad.jl index 29207476..f88a551e 100644 --- a/test/test_ad.jl +++ b/test/test_ad.jl @@ -33,12 +33,12 @@ _J = sparse(J) fcalls = 0 _J1 = similar(_J) -forwarddiff_color_jacobian!(_J1, f, x, color = repeat(1:3,10)) +forwarddiff_color_jacobian!(_J1, f, x, colorvec = repeat(1:3,10)) @test _J1 ≈ J @test fcalls == 1 fcalls = 0 -jac_cache = ForwardColorJacCache(f,x,color = repeat(1:3,10), sparsity = _J1) +jac_cache = ForwardColorJacCache(f,x,colorvec = repeat(1:3,10), sparsity = _J1) forwarddiff_color_jacobian!(_J1, f, x, jac_cache) @test _J1 ≈ J @test fcalls == 1 @@ -46,18 +46,18 @@ forwarddiff_color_jacobian!(_J1, f, x, jac_cache) fcalls = 0 _J1 = similar(_J) _denseJ1 = collect(_J1) -forwarddiff_color_jacobian!(_denseJ1, f, x, color = repeat(1:3,10), sparsity = _J1) +forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3,10), sparsity = _J1) @test _denseJ1 ≈ J @test fcalls == 1 fcalls = 0 _J1 = similar(_J) _denseJ1 = collect(_J1) -jac_cache = ForwardColorJacCache(f,x,color = repeat(1:3,10), sparsity = _J1) +jac_cache = ForwardColorJacCache(f,x,colorvec = repeat(1:3,10), sparsity = _J1) forwarddiff_color_jacobian!(_denseJ1, f, x, jac_cache) @test _denseJ1 ≈ J @test fcalls == 1 _Jt = similar(Tridiagonal(J)) -forwarddiff_color_jacobian!(_Jt, f, x, color = repeat(1:3,10), sparsity = _Jt) +forwarddiff_color_jacobian!(_Jt, f, x, colorvec = repeat(1:3,10), sparsity = _Jt) @test _Jt ≈ J diff --git a/test/test_gpu_ad.jl b/test/test_gpu_ad.jl index d2955e73..cc230414 100644 --- a/test/test_gpu_ad.jl +++ b/test/test_gpu_ad.jl @@ -13,6 +13,6 @@ x = cu(rand(30)) CuArrays.allowscalar(false) forwarddiff_color_jacobian!(_denseJ1, f, x) forwarddiff_color_jacobian!(_denseJ1, f, x, sparsity = _J1) -forwarddiff_color_jacobian!(_denseJ1, f, x, color = repeat(1:3,10), sparsity = _J1) +forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3,10), sparsity = _J1) _Jt = similar(Tridiagonal(_J1)) -@test_broken forwarddiff_color_jacobian!(_denseJ1, f, x, color = repeat(1:3,10), sparsity = _Jt) +@test_broken forwarddiff_color_jacobian!(_denseJ1, f, x, colorvec = repeat(1:3,10), sparsity = _Jt) diff --git a/test/test_integration.jl b/test/test_integration.jl index a4bbf85b..b420f505 100644 --- a/test/test_integration.jl +++ b/test/test_integration.jl @@ -39,13 +39,13 @@ J = finite_difference_jacobian(f, rand(30)) #Jacobian computed with coloring vectors fcalls = 0 _J = similar(true_jac) -finite_difference_jacobian!(_J, f, rand(30), color = colors) +finite_difference_jacobian!(_J, f, rand(30), colorvec = colors) @test fcalls == 4 @test _J ≈ J fcalls = 0 _J = similar(true_jac) _denseJ = collect(_J) -finite_difference_jacobian!(_denseJ, f, rand(30), color = colors, sparsity=_J) +finite_difference_jacobian!(_denseJ, f, rand(30), colorvec = colors, sparsity=_J) @test fcalls == 4 @test _denseJ ≈ J diff --git a/test/test_specialmatrices.jl b/test/test_specialmatrices.jl index ccebab32..4d29b6ec 100644 --- a/test/test_specialmatrices.jl +++ b/test/test_specialmatrices.jl @@ -41,8 +41,8 @@ bandedblockbanded3 = BandedBlockBandedMatrix(Zeros(2 * 4 , 2 * 4), ([4, 4] ,[4, function _testvalidity(A) colorvec=matrix_colors(A) ncolor=maximum(colorvec) - for color in 1:ncolor - subA=A[:,findall(x->x==color,colorvec)] + for colorvec in 1:ncolor + subA=A[:,findall(x->x==colorvec,colorvec)] @test maximum(sum(subA,dims=2))<=1.0 end end