Skip to content

Commit

Permalink
Complete colorvec transition
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisRackauckas committed Aug 2, 2019
1 parent c04f12c commit c1729f2
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 50 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -113,31 +113,31 @@ 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
forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
f,
x::AbstractArray{<:Number};
dx = nothing,
color = eachindex(x),
colorvec = eachindex(x),
sparsity = nothing)
```

Expand All @@ -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)
```

Expand Down
8 changes: 4 additions & 4 deletions src/coloring/backtracking_coloring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions src/coloring/greedy_star1_coloring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions src/coloring/greedy_star2_coloring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/coloring/high_level.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/test_ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,31 @@ _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

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
4 changes: 2 additions & 2 deletions test/test_gpu_ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions test/test_integration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/test_specialmatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c1729f2

Please sign in to comment.