Skip to content
Draft
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
12 changes: 9 additions & 3 deletions src/PreallocationTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,14 @@ function get_tmp(dc::FixedSizeDiffCache, u::Union{Number, AbstractArray})
return get_tmp(dc, eltype(u))
end

function _promotes_to_primal(::Type{P}, ::Type{T}) where {P, T}
# Query the requested type first so its custom rule can avoid an ambiguous reverse rule.
promoted = Base.promote_rule(T, P)
return promoted === Union{} ? promote_type(P, T) <: P : promoted <: P
end

function get_tmp(dc::FixedSizeDiffCache, ::Type{T}) where {T <: Number}
return if promote_type(eltype(dc.du), T) <: eltype(dc.du)
return if _promotes_to_primal(eltype(dc.du), T)
dc.du
else
_typed_tmp(dc, T)
Expand Down Expand Up @@ -394,15 +400,15 @@ function _typed_tmp(dc, ::Type{T}) where {T}
end

function get_tmp(dc::DiffCache, u::Union{Number, AbstractArray})
return if promote_type(eltype(dc.du), eltype(u)) <: eltype(dc.du)
return if _promotes_to_primal(eltype(dc.du), eltype(u))
dc.du
else
_typed_tmp(dc, eltype(u))
end
end

function get_tmp(dc::DiffCache, ::Type{T}) where {T <: Number}
return if promote_type(eltype(dc.du), T) <: eltype(dc.du)
return if _promotes_to_primal(eltype(dc.du), T)
dc.du
else
_typed_tmp(dc, T)
Expand Down
12 changes: 12 additions & 0 deletions test/sparse_connectivity_tracer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ module TestSparseConnectivityTracer

using PreallocationTools, SparseConnectivityTracer, ForwardDiff, SparseArrays, Test

@testset "BigFloat cache" begin
T = jacobian_eltype(BigFloat[1], TracerLocalSparsityDetector())
input = Vector{T}(undef, 1)

for cache in (DiffCache(BigFloat[1]), FixedSizeDiffCache(BigFloat[1], 1))
type_workspace = @inferred get_tmp(cache, T)
array_workspace = @inferred get_tmp(cache, input)
@test eltype(type_workspace) === T
@test Base.mightalias(type_workspace, array_workspace)
end
end

function f1(u, cache)
c = get_tmp(cache, u)
# This will throw if a fallback definition is used
Expand Down
Loading