Skip to content

Commit a58b35f

Browse files
Merge pull request #186 from JuliaDiff/core
Change to ArrayInterfaceCore
2 parents bcca812 + 60e8c93 commit a58b35f

File tree

8 files changed

+101
-83
lines changed

8 files changed

+101
-83
lines changed

Project.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ version = "1.21.0"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
8-
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
8+
ArrayInterfaceCore = "30b0a656-2188-435a-8636-2ec0e6a096e2"
9+
ArrayInterfaceStaticArrays = "b0d46f97-bff5-4637-a19a-dd75974142cd"
910
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
1011
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
1112
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
@@ -19,7 +20,8 @@ VertexSafeGraphs = "19fa3120-7c27-5ec5-8db8-b0b0aa330d6f"
1920

2021
[compat]
2122
Adapt = "1, 2.0, 3.0"
22-
ArrayInterface = "2.8, 3.0, 4, 5"
23+
ArrayInterfaceCore = "0.1.1"
24+
ArrayInterfaceStaticArrays = "0.1"
2325
Compat = "2.2, 3"
2426
DataStructures = "0.17, 0.18"
2527
FiniteDiff = "2.8.1"
@@ -31,6 +33,8 @@ VertexSafeGraphs = "0.2"
3133
julia = "1.6"
3234

3335
[extras]
36+
ArrayInterfaceBlockBandedMatrices = "5331f1e9-51c7-46b0-a9b0-df4434785e0a"
37+
ArrayInterfaceBandedMatrices = "2e50d22c-5be1-4042-81b1-c572ed69783d"
3438
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
3539
BlockBandedMatrices = "ffab5731-97b5-5995-9138-79e8c1846df0"
3640
IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153"
@@ -43,4 +47,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4347
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
4448

4549
[targets]
46-
test = ["Test", "BandedMatrices", "BlockBandedMatrices", "IterativeSolvers", "Pkg", "Random", "SafeTestsets", "Zygote", "SparsityDetection", "StaticArrays"]
50+
test = ["Test", "ArrayInterfaceBandedMatrices", "ArrayInterfaceBlockBandedMatrices", "BandedMatrices", "BlockBandedMatrices", "IterativeSolvers", "Pkg", "Random", "SafeTestsets", "Zygote", "SparsityDetection", "StaticArrays"]

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ gmres!(res,J,v)
112112

113113
### Matrix Coloring
114114

115-
This library extends the common `ArrayInterface.matrix_colors` function to allow
115+
This library extends the common `ArrayInterfaceCore.matrix_colors` function to allow
116116
for coloring sparse matrices using graphical techniques.
117117

118118
Matrix coloring allows you to reduce the number of times finite differencing
@@ -163,7 +163,7 @@ forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
163163

164164
Notice that if a sparsity pattern is not supplied then the built Jacobian will
165165
be the compressed Jacobian: `sparsity` must be a sparse matrix or a structured matrix
166-
(`Tridiagonal`, `Banded`, etc. conforming to the ArrayInterface.jl specs) with the
166+
(`Tridiagonal`, `Banded`, etc. conforming to the ArrayInterfaceCore.jl specs) with the
167167
appropriate sparsity pattern to allow for decompression.
168168

169169
This call will allocate the cache variables each time. To avoid allocating the
@@ -317,3 +317,14 @@ These all have the same interface, where `J*v` utilizes the out-of-place
317317
Jacobian-vector or Hessian-vector function, whereas `mul!(res,J,v)` utilizes
318318
the appropriate in-place versions. To update the location of differentiation
319319
in the operator, simply mutate the vector `u`: `J.u .= ...`.
320+
321+
# Note about sparse differentiation of GPUArrays, BandedMatrices, and BlockBandedMatrices
322+
323+
These two matrix types need the dependencies ArrayInterfaceBandedMatrices.jl and
324+
ArrayInterfaceBlockBandedMatrices.jl to basically work with any functionality
325+
(anywhere). For now, the right thing to do is to add these libraries and do
326+
`import` on them if you are using BandedMatrices.jl or BlockBandedMatrices.jl
327+
for sparsity patterns. In the future, those two packages should just depend on
328+
ArrayInterface.jl and remove this issue entirely from the user space.
329+
330+
Additionally, GPUs need ArrayInterfaceGPUArrays for proper determination of the indexing.

src/SparseDiffTools.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ using VertexSafeGraphs
1010
using Adapt
1111

1212
using LinearAlgebra
13-
using SparseArrays, ArrayInterface
13+
using SparseArrays, ArrayInterfaceCore
1414

15-
import StaticArrays
15+
import StaticArrays, ArrayInterfaceStaticArrays
1616

1717
using ForwardDiff: Dual, jacobian, partials, DEFAULT_CHUNK_THRESHOLD
1818
using DataStructures: DisjointSets, find_root!, union!
1919

20-
using ArrayInterface: matrix_colors
20+
using ArrayInterfaceCore: matrix_colors
2121

2222
export contract_color,
2323
greedy_d1,

src/coloring/high_level.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
abstract type SparseDiffToolsColoringAlgorithm <: ArrayInterface.ColoringAlgorithm end
1+
abstract type SparseDiffToolsColoringAlgorithm <: ArrayInterfaceCore.ColoringAlgorithm end
22
struct GreedyD1Color <: SparseDiffToolsColoringAlgorithm end
33
struct BacktrackingColor <: SparseDiffToolsColoringAlgorithm end
44
struct ContractionColor <: SparseDiffToolsColoringAlgorithm end
@@ -16,7 +16,7 @@ The coloring defaults to a greedy distance-1 coloring.
1616
Note that if A isa SparseMatrixCSC, the sparsity pattern is defined by structural nonzeroes,
1717
ie includes explicitly stored zeros.
1818
"""
19-
function ArrayInterface.matrix_colors(A::AbstractMatrix, alg::SparseDiffToolsColoringAlgorithm = GreedyD1Color(); partition_by_rows::Bool = false)
19+
function ArrayInterfaceCore.matrix_colors(A::AbstractMatrix, alg::SparseDiffToolsColoringAlgorithm = GreedyD1Color(); partition_by_rows::Bool = false)
2020
_A = A isa SparseMatrixCSC ? A : sparse(A) # Avoid the copy
2121
A_graph = matrix2graph(_A, partition_by_rows)
2222
return color_graph(A_graph, alg)

src/differentiation/compute_jacobian_ad.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ function ForwardColorJacCache(f::F,x,_chunksize = nothing;
4343
else
4444
p = adapt.(parameterless_type(x),generate_chunked_partials(x,colorvec,chunksize))
4545
_t = Dual{T,eltype(x),getsize(chunksize)}.(vec(x),ForwardDiff.Partials.(first(p)))
46-
t = ArrayInterface.restructure(x,_t)
46+
t = ArrayInterfaceCore.restructure(x,_t)
4747
end
4848

4949

5050
if dx isa Nothing
5151
fx = similar(t)
5252
_dx = similar(x)
5353
else
54-
tup = ArrayInterface.allowed_getindex(ArrayInterface.allowed_getindex(p,1),1) .* false
54+
tup = ArrayInterfaceCore.allowed_getindex(ArrayInterfaceCore.allowed_getindex(p,1),1) .* false
5555
_pi = adapt(parameterless_type(dx),[tup for i in 1:length(dx)])
5656
fx = reshape(Dual{T,eltype(dx),length(tup)}.(vec(dx),ForwardDiff.Partials.(_pi)),size(dx)...)
5757
_dx = dx
@@ -134,7 +134,7 @@ end
134134

135135
function forwarddiff_color_jacobian(f::F,x::AbstractArray{<:Number},jac_cache::ForwardColorJacCache,jac_prototype=nothing) where F
136136

137-
if jac_prototype isa Nothing ? ArrayInterface.ismutable(x) : ArrayInterface.ismutable(jac_prototype)
137+
if jac_prototype isa Nothing ? ArrayInterfaceCore.ismutable(x) : ArrayInterfaceCore.ismutable(jac_prototype)
138138
# Whenever J is mutable, we mutate it to avoid allocations
139139
dx = jac_cache.dx
140140
vecx = vec(x)
@@ -164,7 +164,7 @@ function forwarddiff_color_jacobian(J::AbstractMatrix{<:Number},f::F,x::Abstract
164164
nrows,ncols = size(J)
165165

166166
if !(sparsity isa Nothing)
167-
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity)
167+
rows_index, cols_index = ArrayInterfaceCore.findstructralnz(sparsity)
168168
rows_index = [rows_index[i] for i in 1:length(rows_index)]
169169
cols_index = [cols_index[i] for i in 1:length(cols_index)]
170170
end
@@ -232,7 +232,7 @@ function forwarddiff_color_jacobian_immutable(f,x::AbstractArray{<:Number},jac_c
232232
nrows,ncols = size(J)
233233

234234
if !(sparsity isa Nothing)
235-
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity)
235+
rows_index, cols_index = ArrayInterfaceCore.findstructralnz(sparsity)
236236
rows_index = [rows_index[i] for i in 1:length(rows_index)]
237237
cols_index = [cols_index[i] for i in 1:length(cols_index)]
238238
end
@@ -277,7 +277,7 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
277277
x::AbstractArray{<:Number};
278278
dx = similar(x,size(J,1)),
279279
colorvec = 1:length(x),
280-
sparsity = ArrayInterface.has_sparsestruct(J) ? J : nothing)
280+
sparsity = ArrayInterfaceCore.has_sparsestruct(J) ? J : nothing)
281281
forwarddiff_color_jacobian!(J,f,x,ForwardColorJacCache(f,x,dx=dx,colorvec=colorvec,sparsity=sparsity))
282282
end
283283

@@ -305,7 +305,7 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
305305
end
306306

307307
if FiniteDiff._use_findstructralnz(sparsity)
308-
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity)
308+
rows_index, cols_index = ArrayInterfaceCore.findstructralnz(sparsity)
309309
else
310310
rows_index = 1:size(J,1)
311311
cols_index = 1:size(J,2)
@@ -344,7 +344,7 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
344344
dx .= partials.(fx, j)
345345
end
346346

347-
if ArrayInterface.fast_scalar_indexing(dx)
347+
if ArrayInterfaceCore.fast_scalar_indexing(dx)
348348
#dx is implicitly used in vecdx
349349
if sparseCSC_common_sparsity
350350
FiniteDiff._colorediteration!(J,vecdx,colorvec,color_i,ncols)

test/gpu/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[deps]
22
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
3+
ArrayInterfaceGPUArrays = "6ba088a2-8465-4c0a-af30-387133b534db"

0 commit comments

Comments
 (0)