Skip to content

Optimize BandedBlockBandedMatrix #74

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

Merged
merged 10 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
name = "DiffEqDiffTools"
uuid = "01453d9d-ee7c-5054-8395-0335cb756afa"
version = "1.2.0"
version = "1.3.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"

[compat]
julia = "1"
ArrayInterface = "1.1"
ArrayInterface = ">= 1.1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
BandedMatrices="aae01518-5342-5314-be14-df237901396f"

[targets]
test = ["Test"]
test = ["Test","BlockBandedMatrices","BandedMatrices"]
2 changes: 1 addition & 1 deletion src/DiffEqDiffTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ __precompile__()

module DiffEqDiffTools

using LinearAlgebra, SparseArrays, StaticArrays, ArrayInterface
using LinearAlgebra, SparseArrays, StaticArrays, ArrayInterface, Requires

import Base: resize!

Expand Down
170 changes: 115 additions & 55 deletions src/jacobians.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,112 @@ function finite_difference_jacobian(f, x::AbstractArray{<:Number},
finite_difference_jacobian(f, x, cache, f_in; relstep=relstep, absstep=absstep, colorvec=colorvec, sparsity=sparsity, dir=dir)
end

@inline function _colorediteration!(J,sparsity,rows_index,cols_index,vfx,colorvec,color_i,ncols)
@inbounds for i in 1:length(cols_index)
if colorvec[cols_index[i]] == color_i
J[rows_index[i],cols_index[i]] = vfx[rows_index[i]]
end
end
end

@inline function _colorediteration!(J,sparsity::SparseMatrixCSC,rows_index,cols_index,vfx,colorvec,color_i,ncols)
@inbounds for col_index in 1:ncols
if colorvec[col_index] == color_i
@inbounds for row_index in view(sparsity.rowval,sparsity.colptr[col_index]:sparsity.colptr[col_index+1]-1)
J[row_index,col_index]=vfx[row_index]
end
end
end
end

#override default setting of using findstructralnz
_use_findstructralnz(sparsity) = ArrayInterface.has_sparsestruct(sparsity)
_use_findstructralnz(::SparseMatrixCSC) = false

function __init__()
@require BlockBandedMatrices="ffab5731-97b5-5995-9138-79e8c1846df0" begin
_use_findstructralnz(::BlockBandedMatrices.BandedBlockBandedMatrix) = false
_use_findstructralnz(::BlockBandedMatrices.BlockBandedMatrix) = false

@inline function _colorediteration!(Jac::BlockBandedMatrices.BandedBlockBandedMatrix,
Copy link
Member

Choose a reason for hiding this comment

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

What about just standard BlockBandedMatrix and BandedMatrix? Are those fine without a special iteration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'm working on that.

sparsity::BlockBandedMatrices.BandedBlockBandedMatrix,
rows_index,cols_index,vfx,colorvec,color_i,ncols)
λ,μ = BlockBandedMatrices.subblockbandwidths(Jac)
rs = BlockBandedMatrices.BlockSizes((BlockBandedMatrices.cumulsizes(Jac,1),)) # column block sizes
cs = BlockBandedMatrices.BlockSizes((BlockBandedMatrices.cumulsizes(Jac,2),))
b = BlockBandedMatrices.BlockArray(vfx,rs)
c = BlockBandedMatrices.BlockArray(colorvec,cs)
@inbounds for J=BlockBandedMatrices.Block.(1:BlockBandedMatrices.nblocks(Jac,2))
c_v = c.blocks[J.n[1]]
@inbounds for K=BlockBandedMatrices.blockcolrange(Jac,J)
V = view(Jac,K,J)
b_v = b.blocks[K.n[1]]
data = BlockBandedMatrices.bandeddata(V)
p = pointer(data)
st = stride(data,2)
m,n = size(V)
@inbounds for j=1:n
if c_v[j] == color_i
@inbounds for k=max(1,j-μ):min(m,j+λ)
unsafe_store!(p, b_v[k], (j-1)*st + μ + k - j + 1)
end
end
end
end
end
end

@inline function _colorediteration!(Jac::BlockBandedMatrices.BlockBandedMatrix,
sparsity::BlockBandedMatrices.BlockBandedMatrix,
rows_index,cols_index,vfx,colorvec,color_i,ncols)
rs = BlockBandedMatrices.BlockSizes((BlockBandedMatrices.cumulsizes(Jac,1),)) # column block sizes
cs = BlockBandedMatrices.BlockSizes((BlockBandedMatrices.cumulsizes(Jac,2),))
b = BlockBandedMatrices.BlockArray(vfx,rs)
c = BlockBandedMatrices.BlockArray(colorvec,cs)
@inbounds for J=BlockBandedMatrices.Block.(1:BlockBandedMatrices.nblocks(Jac,2))
c_v = c.blocks[J.n[1]]
blockcolrange = BlockBandedMatrices.blockcolrange(Jac,J)
_,n = BlockBandedMatrices.blocksize(Jac,(blockcolrange[1].n[1],J.n[1]))
@inbounds for j = 1:n
if c_v[j] == color_i
@inbounds for K = blockcolrange
V = view(Jac,K,J)
b_v = b.blocks[K.n[1]]
m = size(V,1)
@inbounds for k = 1:m
V[k,j] = b_v[k]
end
end
end
end
end
end

end

@require BandedMatrices = "aae01518-5342-5314-be14-df237901396f" begin

_use_findstructralnz(::BandedMatrices.BandedMatrix) = false

@inline function _colorediteration!(Jac::BandedMatrices.BandedMatrix,
sparsity::BandedMatrices.BandedMatrix,
rows_index,cols_index,vfx,colorvec,color_i,ncols)
nrows = size(Jac,1)
l,u = BandedMatrices.bandwidths(Jac)
#data = BandedMatrices.bandeddata(Jac)
@inbounds for col_index in max(1,1-l):min(ncols,ncols+u)
if colorvec[col_index] == color_i
@inbounds for row_index in max(1,col_index-u):min(nrows,col_index+l)
#data[u+row_index-col_index+1,col_index] = vfx[row_index]
Jac[row_index,col_index]=vfx[row_index]
end
end
end
end
end

end

function finite_difference_jacobian(
f,
x,
Expand Down Expand Up @@ -161,7 +267,9 @@ function finite_difference_jacobian!(
end
vfx = vec(fx)

if ArrayInterface.has_sparsestruct(sparsity)
rows_index = nothing
cols_index = nothing
if _use_findstructralnz(sparsity)
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity)
end

Expand Down Expand Up @@ -217,15 +325,7 @@ function finite_difference_jacobian!(
@. vfx1 = (vfx1 - vfx) / epsilon

if ArrayInterface.fast_scalar_indexing(x1)
for i in 1:length(cols_index)
if colorvec[cols_index[i]] == color_i
if J isa SparseMatrixCSC
J.nzval[i] = vfx1[rows_index[i]]
else
J[rows_index[i],cols_index[i]] = vfx1[rows_index[i]]
end
end
end
_colorediteration!(J,sparsity,rows_index,cols_index,vfx1,colorvec,color_i,n)
else
#=
J.nzval[rows_index] .+= (colorvec[cols_index] .== color_i) .* vfx1[rows_index]
Expand All @@ -252,15 +352,7 @@ function finite_difference_jacobian!(
_vfx1 = (vfx1 - vfx) / epsilon

if ArrayInterface.fast_scalar_indexing(x1)
for i in 1:length(cols_index)
if colorvec[cols_index[i]] == color_i
if J isa SparseMatrixCSC
J.nzval[i] = vfx1[rows_index[i]]
else
J[rows_index[i],cols_index[i]] = vfx1[rows_index[i]]
end
end
end
_colorediteration!(J,sparsity,rows_index,cols_index,vfx1,colorvec,color_i,n)
else
#=
J.nzval[rows_index] .+= (colorvec[cols_index] .== color_i) .* vfx1[rows_index]
Expand Down Expand Up @@ -329,15 +421,7 @@ function finite_difference_jacobian!(
@. vfx1 = (vfx1 - vfx) / 2epsilon

if ArrayInterface.fast_scalar_indexing(x1)
for i in 1:length(cols_index)
if colorvec[cols_index[i]] == color_i
if J isa SparseMatrixCSC
J.nzval[i] = vfx1[rows_index[i]]
else
J[rows_index[i],cols_index[i]] = vfx1[rows_index[i]]
end
end
end
_colorediteration!(J,sparsity,rows_index,cols_index,vfx1,colorvec,color_i,n)
else
#=
J.nzval[rows_index] .+= (colorvec[cols_index] .== color_i) .* vfx1[rows_index]
Expand Down Expand Up @@ -369,15 +453,7 @@ function finite_difference_jacobian!(
# vfx1 is the compressed Jacobian column

if ArrayInterface.fast_scalar_indexing(x1)
for i in 1:length(cols_index)
if colorvec[cols_index[i]] == color_i
if J isa SparseMatrixCSC
J.nzval[i] = vfx1[rows_index[i]]
else
J[rows_index[i],cols_index[i]] = vfx1[rows_index[i]]
end
end
end
_colorediteration!(J,sparsity,rows_index,cols_index,vfx1,colorvec,color_i,n)
else
#=
J.nzval[rows_index] .+= (colorvec[cols_index] .== color_i) .* vfx1[rows_index]
Expand Down Expand Up @@ -435,15 +511,7 @@ function finite_difference_jacobian!(
@. vfx = imag(vfx) / epsilon

if ArrayInterface.fast_scalar_indexing(x1)
for i in 1:length(cols_index)
if colorvec[cols_index[i]] == color_i
if J isa SparseMatrixCSC
J.nzval[i] = vfx[rows_index[i]]
else
J[rows_index[i],cols_index[i]] = vfx[rows_index[i]]
end
end
end
_colorediteration!(J,sparsity,rows_index,cols_index,vfx,colorvec,color_i,n)
else
#=
J.nzval[rows_index] .+= (colorvec[cols_index] .== color_i) .* vfx[rows_index]
Expand Down Expand Up @@ -471,15 +539,7 @@ function finite_difference_jacobian!(
vfx = imag(vfx) / epsilon

if ArrayInterface.fast_scalar_indexing(x1)
for i in 1:length(cols_index)
if colorvec[cols_index[i]] == color_i
if J isa SparseMatrixCSC
J.nzval[i] = vfx1[rows_index[i]]
else
J[rows_index[i],cols_index[i]] = vfx1[rows_index[i]]
end
end
end
_colorediteration!(J,sparsity,rows_index,cols_index,vfx1,colorvec,color_i,n)
else
#=
J.nzval[rows_index] .+= (colorvec[cols_index] .== color_i) .* vfx1[rows_index]
Expand Down
35 changes: 33 additions & 2 deletions test/coloring_tests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DiffEqDiffTools, LinearAlgebra, SparseArrays, Test, LinearAlgebra
using DiffEqDiffTools, LinearAlgebra, SparseArrays, Test, LinearAlgebra, BlockBandedMatrices, ArrayInterface, BandedMatrices

fcalls = 0
function f(dx,x)
Expand Down Expand Up @@ -80,4 +80,35 @@ _J2 = similar(_J2)
fcalls = 0
DiffEqDiffTools.finite_difference_jacobian!(_J2,f,rand(30),Val{:complex},colorvec=repeat(1:3,10))
@test fcalls == 3
@test _J2 ≈ _J
@test _J2 ≈ _J

_Jb = BandedMatrices.BandedMatrix(similar(_J2),(1,1))
DiffEqDiffTools.finite_difference_jacobian!(_Jb, f, rand(30), colorvec=colorvec=repeat(1:3,10))
@test _Jb ≈ _J

_Jtri = Tridiagonal(similar(_J2))
DiffEqDiffTools.finite_difference_jacobian!(_Jtri, f, rand(30), colorvec=colorvec=repeat(1:3,10))
@test _Jtri ≈ _J

#https://github.com/JuliaDiffEq/DiffEqDiffTools.jl/issues/67#issuecomment-516871956
function f(out, x)
x = reshape(x, 100, 100)
out = reshape(out, 100, 100)
for i in 1:100
for j in 1:100
out[i, j] = x[i, j] + x[max(i -1, 1), j] + x[min(i+1, size(x, 1)), j] + x[i, max(j-1, 1)] + x[i, min(j+1, size(x, 2))]
end
end
return vec(out)
end
x = rand(10000)
Jbbb = BandedBlockBandedMatrix(Ones(10000, 10000), (fill(100, 100), fill(100, 100)), (1, 1), (1, 1))
Jsparse = sparse(Jbbb)
colorsbbb = ArrayInterface.matrix_colors(Jbbb)
DiffEqDiffTools.finite_difference_jacobian!(Jbbb, f, x, colorvec=colorsbbb)
DiffEqDiffTools.finite_difference_jacobian!(Jsparse, f, x, colorvec=colorsbbb)
@test Jbbb ≈ Jsparse
Jbb = BlockBandedMatrix(similar(Jsparse),(fill(100, 100), fill(100, 100)),(1,1));
colorsbb = ArrayInterface.matrix_colors(Jbb)
DiffEqDiffTools.finite_difference_jacobian!(Jbb, f, x, colorvec=colorsbb)
@test Jbb ≈ Jsparse