Skip to content

Special Dispatch for Static Array #264

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 1 commit into from
Oct 9, 2023
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
1 change: 1 addition & 0 deletions src/SparseDiffTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import ForwardDiff: Dual, jacobian, partials, DEFAULT_CHUNK_THRESHOLD
using ArrayInterface, SparseArrays
import ArrayInterface: matrix_colors
import StaticArrays
import StaticArrays: StaticArray
# Others
using SciMLOperators, LinearAlgebra, Random
import DataStructures: DisjointSets, find_root!, union!
Expand Down
5 changes: 5 additions & 0 deletions src/highlevel/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ function init_jacobian(c::AbstractMaybeSparseJacobianCache)
return init_jacobian(__getfield(c, Val(:jac_prototype)), T, c.fx, c.x)
end
init_jacobian(::Nothing, ::Type{T}, fx, x) where {T} = similar(fx, T, length(fx), length(x))
function init_jacobian(::Nothing, ::Type{T}, fx::StaticArray, x::StaticArray) where {T}
# We want to construct a MArray to preserve types
J = StaticArrays.MArray{Tuple{length(fx), length(x)}, T}(undef)
Copy link
Member

Choose a reason for hiding this comment

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

use Size so it's inferred?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is already compile time rn.

julia> @code_warntype init_jacobian(nothing, Float32, y, x)
MethodInstance for SparseDiffTools.init_jacobian(::Nothing, ::Type{Float32}, ::MVector{30, Float64}, ::SVector{30, Float64})
  from init_jacobian(::Nothing, ::Type{T}, fx::StaticArray, x::StaticArray) where T @ SparseDiffTools /mnt/research/nlsolve/SparseDiffTools.jl/src/highlevel/common.jl:262
Static Parameters
  T = Float32
Arguments
  #self#::Core.Const(SparseDiffTools.init_jacobian)
  _::Core.Const(nothing)
  _::Core.Const(Float32)
  fx::MVector{30, Float64}
  x::SVector{30, Float64}
Locals
  J::MMatrix{30, 30, Float32, 900}
Body::MMatrix{30, 30, Float32, 900}
1%1 = StaticArrays.MArray::Core.Const(MArray)
│   %2 = SparseDiffTools.Tuple::Core.Const(Tuple)
│   %3 = SparseDiffTools.length(fx)::Core.Const(30)
│   %4 = SparseDiffTools.length(x)::Core.Const(30)
│   %5 = Core.apply_type(%2, %3, %4)::Core.Const(Tuple{30, 30})
│   %6 = Core.apply_type(%1, %5, $(Expr(:static_parameter, 1)))::Core.Const(MArray{Tuple{30, 30}, Float32})
│        (J = (%6)(SparseDiffTools.undef))
└──      return J

return J
end
init_jacobian(J, ::Type{T}, _, _) where {T} = similar(J, T, size(J, 1), size(J, 2))
init_jacobian(J::SparseMatrixCSC, ::Type{T}, _, _) where {T} = T.(J)

Expand Down