Skip to content

Commit b41a859

Browse files
Merge pull request #172 from longemen3000/1.9-extensions-finitediff
add 1.9 extensions
2 parents 5afccd9 + bd43535 commit b41a859

File tree

5 files changed

+119
-85
lines changed

5 files changed

+119
-85
lines changed

Project.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
1010
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1111
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1212

13+
[weakdeps]
14+
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
15+
BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
16+
17+
[extensions]
18+
FiniteDiffBandedMatricesExt = "BandedMatrices"
19+
FiniteDiffBlockBandedMatricesExt = "BlockBandedMatrices"
20+
1321
[compat]
1422
ArrayInterface = "7"
1523
Requires = "1.0"

ext/FiniteDiffBandedMatricesExt.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module FiniteDiffBandedMatricesExt
2+
3+
if isdefined(Base, :get_extension)
4+
using FiniteDiff: FiniteDiff, ArrayInterface
5+
using BandedMatrices: BandedMatrices
6+
else
7+
using ..FiniteDiff: FiniteDiff, ArrayInterface
8+
using ..BandedMatrices: BlockBandedMatrices
9+
end
10+
11+
FiniteDiff._use_findstructralnz(::BandedMatrices.BandedMatrix) = false
12+
13+
@inline function FiniteDiff._colorediteration!(Jac::BandedMatrices.BandedMatrix,
14+
sparsity::BandedMatrices.BandedMatrix,
15+
rows_index,cols_index,vfx,colorvec,color_i,ncols)
16+
nrows = size(Jac,1)
17+
l,u = BandedMatrices.bandwidths(Jac)
18+
#data = BandedMatrices.bandeddata(Jac)
19+
@inbounds for col_index in max(1,1-l):min(ncols,ncols+u)
20+
if colorvec[col_index] == color_i
21+
@inbounds for row_index in max(1,col_index-u):min(nrows,col_index+l)
22+
#data[u+row_index-col_index+1,col_index] = vfx[row_index]
23+
Jac[row_index,col_index]=vfx[row_index]
24+
end
25+
end
26+
end
27+
end
28+
29+
end #module
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module FiniteDiffBlockBandedMatricesExt
2+
3+
if isdefined(Base, :get_extension)
4+
using FiniteDiff: FiniteDiff, ArrayInterface
5+
using BlockBandedMatrices: BlockBandedMatrices
6+
using BlockBandedMatrices.BlockArrays
7+
else
8+
using ..FiniteDiff: FiniteDiff, ArrayInterface
9+
using ..BlockBandedMatrices: BlockBandedMatrices
10+
using ..BlockBandedMatrices.BlockArrays
11+
end
12+
13+
FiniteDiff._use_findstructralnz(::BlockBandedMatrices.BandedBlockBandedMatrix) = false
14+
FiniteDiff._use_findstructralnz(::BlockBandedMatrices.BlockBandedMatrix) = false
15+
16+
@inline function FiniteDiff._colorediteration!(Jac::BlockBandedMatrices.BandedBlockBandedMatrix,
17+
sparsity::BlockBandedMatrices.BandedBlockBandedMatrix,
18+
rows_index,cols_index,vfx,colorvec,color_i,ncols)
19+
λ,μ = BlockBandedMatrices.subblockbandwidths(Jac)
20+
rs = BlockArrays.blocklengths(BlockArrays.axes(Jac,1)) # column block sizes
21+
cs = BlockArrays.blocklengths(BlockArrays.axes(Jac,1))
22+
b = BlockBandedMatrices.BlockArray(vfx,rs)
23+
c = BlockBandedMatrices.BlockArray(colorvec,cs)
24+
@inbounds for J=BlockArrays.blockaxes(Jac,2)
25+
c_v = c.blocks[J.n[1]]
26+
@inbounds for K=BlockBandedMatrices.blockcolrange(Jac,J)
27+
V = view(Jac,K,J)
28+
b_v = b.blocks[K.n[1]]
29+
data = BlockBandedMatrices.bandeddata(V)
30+
p = pointer(data)
31+
st = stride(data,2)
32+
m,n = size(V)
33+
@inbounds for j=1:n
34+
if c_v[j] == color_i
35+
@inbounds for k=max(1,j-μ):min(m,j+λ)
36+
unsafe_store!(p, b_v[k], (j-1)*st + μ + k - j + 1)
37+
end
38+
end
39+
end
40+
end
41+
end
42+
end
43+
44+
@inline function FiniteDiff._colorediteration!(Jac::BlockBandedMatrices.BlockBandedMatrix,
45+
sparsity::BlockBandedMatrices.BlockBandedMatrix,
46+
rows_index,cols_index,vfx,colorvec,color_i,ncols)
47+
rs = BlockArrays.blocklengths(BlockArrays.axes(Jac,1)) # column block sizes
48+
cs = BlockArrays.blocklengths(BlockArrays.axes(Jac,1))
49+
b = BlockBandedMatrices.BlockArray(vfx,rs)
50+
c = BlockBandedMatrices.BlockArray(colorvec,cs)
51+
@inbounds for J=BlockArrays.blockaxes(Jac,2)
52+
c_v = c.blocks[J.n[1]]
53+
blockcolrange = BlockBandedMatrices.blockcolrange(Jac,J)
54+
_,n = length.(getindex.(axes(Jac), (blockcolrange[1], J)))
55+
@inbounds for j = 1:n
56+
if c_v[j] == color_i
57+
@inbounds for K = blockcolrange
58+
V = view(Jac,K,J)
59+
b_v = b.blocks[K.n[1]]
60+
m = size(V,1)
61+
@inbounds for k = 1:m
62+
V[k,j] = b_v[k]
63+
end
64+
end
65+
end
66+
end
67+
end
68+
end
69+
70+
71+
end #module

src/FiniteDiff.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ include("gradients.jl")
3838
include("jacobians.jl")
3939
include("hessians.jl")
4040

41-
41+
if !isdefined(Base,:get_extension)
42+
using Requires
43+
function __init__()
44+
@require BandedMatrices="aae01518-5342-5314-be14-df237901396f" begin
45+
include("../ext/FiniteDiffBandedMatricesExt.jl")
46+
end
47+
@require BlockBandedMatrices="ffab5731-97b5-5995-9138-79e8c1846df0" begin
48+
include("../ext/FiniteDiffBlockBandedMatricesExt.jl")
49+
end
50+
end
51+
end
4252

4353
end # module

src/iteration_utils.jl

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -37,87 +37,3 @@ _use_sparseCSC_common_sparsity(J, sparsity) = false
3737
_use_sparseCSC_common_sparsity(J::SparseMatrixCSC, sparsity::SparseMatrixCSC) =
3838
((J.colptr == sparsity.colptr) && (J.rowval == sparsity.rowval))
3939

40-
function __init__()
41-
@require BlockBandedMatrices="ffab5731-97b5-5995-9138-79e8c1846df0" begin
42-
@require BlockArrays="8e7c35d0-a365-5155-bbbb-fb81a777f24e" begin
43-
_use_findstructralnz(::BlockBandedMatrices.BandedBlockBandedMatrix) = false
44-
_use_findstructralnz(::BlockBandedMatrices.BlockBandedMatrix) = false
45-
46-
@inline function _colorediteration!(Jac::BlockBandedMatrices.BandedBlockBandedMatrix,
47-
sparsity::BlockBandedMatrices.BandedBlockBandedMatrix,
48-
rows_index,cols_index,vfx,colorvec,color_i,ncols)
49-
λ,μ = BlockBandedMatrices.subblockbandwidths(Jac)
50-
rs = BlockArrays.blocklengths(BlockArrays.axes(Jac,1)) # column block sizes
51-
cs = BlockArrays.blocklengths(BlockArrays.axes(Jac,1))
52-
b = BlockBandedMatrices.BlockArray(vfx,rs)
53-
c = BlockBandedMatrices.BlockArray(colorvec,cs)
54-
@inbounds for J=BlockArrays.blockaxes(Jac,2)
55-
c_v = c.blocks[J.n[1]]
56-
@inbounds for K=BlockBandedMatrices.blockcolrange(Jac,J)
57-
V = view(Jac,K,J)
58-
b_v = b.blocks[K.n[1]]
59-
data = BlockBandedMatrices.bandeddata(V)
60-
p = pointer(data)
61-
st = stride(data,2)
62-
m,n = size(V)
63-
@inbounds for j=1:n
64-
if c_v[j] == color_i
65-
@inbounds for k=max(1,j-μ):min(m,j+λ)
66-
unsafe_store!(p, b_v[k], (j-1)*st + μ + k - j + 1)
67-
end
68-
end
69-
end
70-
end
71-
end
72-
end
73-
74-
@inline function _colorediteration!(Jac::BlockBandedMatrices.BlockBandedMatrix,
75-
sparsity::BlockBandedMatrices.BlockBandedMatrix,
76-
rows_index,cols_index,vfx,colorvec,color_i,ncols)
77-
rs = BlockArrays.blocklengths(BlockArrays.axes(Jac,1)) # column block sizes
78-
cs = BlockArrays.blocklengths(BlockArrays.axes(Jac,1))
79-
b = BlockBandedMatrices.BlockArray(vfx,rs)
80-
c = BlockBandedMatrices.BlockArray(colorvec,cs)
81-
@inbounds for J=BlockArrays.blockaxes(Jac,2)
82-
c_v = c.blocks[J.n[1]]
83-
blockcolrange = BlockBandedMatrices.blockcolrange(Jac,J)
84-
_,n = length.(getindex.(axes(Jac), (blockcolrange[1], J)))
85-
@inbounds for j = 1:n
86-
if c_v[j] == color_i
87-
@inbounds for K = blockcolrange
88-
V = view(Jac,K,J)
89-
b_v = b.blocks[K.n[1]]
90-
m = size(V,1)
91-
@inbounds for k = 1:m
92-
V[k,j] = b_v[k]
93-
end
94-
end
95-
end
96-
end
97-
end
98-
end
99-
100-
end
101-
102-
@require BandedMatrices = "aae01518-5342-5314-be14-df237901396f" begin
103-
104-
_use_findstructralnz(::BandedMatrices.BandedMatrix) = false
105-
106-
@inline function _colorediteration!(Jac::BandedMatrices.BandedMatrix,
107-
sparsity::BandedMatrices.BandedMatrix,
108-
rows_index,cols_index,vfx,colorvec,color_i,ncols)
109-
nrows = size(Jac,1)
110-
l,u = BandedMatrices.bandwidths(Jac)
111-
#data = BandedMatrices.bandeddata(Jac)
112-
@inbounds for col_index in max(1,1-l):min(ncols,ncols+u)
113-
if colorvec[col_index] == color_i
114-
@inbounds for row_index in max(1,col_index-u):min(nrows,col_index+l)
115-
#data[u+row_index-col_index+1,col_index] = vfx[row_index]
116-
Jac[row_index,col_index]=vfx[row_index]
117-
end
118-
end
119-
end
120-
end
121-
end
122-
end
123-
end

0 commit comments

Comments
 (0)