Skip to content

Commit e41c040

Browse files
Merge pull request #54 from thomvet/thomvet-patch-1
Bring back exported dualcache
2 parents 1668aa0 + 1ddeeec commit e41c040

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PreallocationTools"
22
uuid = "d236fae5-4411-538c-8e31-a6e3d9e00b46"
33
authors = ["Chris Rackauckas <[email protected]>"]
4-
version = "0.4.5"
4+
version = "0.4.6"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

src/PreallocationTools.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ end
2121
2222
`FixedSizeDiffCache(u::AbstractArray, N = Val{default_cache_size(length(u))})`
2323
24-
Builds a `DualCache` object that stores both a version of the cache for `u`
24+
Builds a `FixedSizeDiffCache` object that stores both a version of the cache for `u`
2525
and for the `Dual` version of `u`, allowing use of pre-cached vectors with
2626
forward-mode automatic differentiation.
2727
"""
@@ -100,7 +100,7 @@ DiffCache(u::AbstractArray, N::AbstractArray{<:Int}) = DiffCache(u, size(u), N)
100100
function DiffCache(u::AbstractArray, ::Type{Val{N}}; levels::Int = 1) where {N}
101101
DiffCache(u, N; levels)
102102
end
103-
DiffCache(u::AbstractArray, ::Val{N}; levels::Int = 1) where {N} = dualcache(u, N; levels)
103+
DiffCache(u::AbstractArray, ::Val{N}; levels::Int = 1) where {N} = DiffCache(u, N; levels)
104104

105105
# Legacy deprecate later
106106
const dualcache = DiffCache
@@ -115,15 +115,15 @@ Returns the `Dual` or normal cache array stored in `dc` based on the type of `u`
115115
function get_tmp(dc::DiffCache, u::T) where {T <: ForwardDiff.Dual}
116116
nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du)
117117
if nelem > length(dc.dual_du)
118-
enlargedualcache!(dc, nelem)
118+
enlargediffcache!(dc, nelem)
119119
end
120120
_restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem)))
121121
end
122122

123123
function get_tmp(dc::DiffCache, u::AbstractArray{T}) where {T <: ForwardDiff.Dual}
124124
nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du)
125125
if nelem > length(dc.dual_du)
126-
enlargedualcache!(dc, nelem)
126+
enlargediffcache!(dc, nelem)
127127
end
128128
_restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem)))
129129
end
@@ -150,11 +150,11 @@ function _restructure(normal_cache::AbstractArray, duals)
150150
ArrayInterfaceCore.restructure(normal_cache, duals)
151151
end
152152

153-
function enlargedualcache!(dc, nelem) #warning comes only once per dualcache.
153+
function enlargediffcache!(dc, nelem) #warning comes only once per DiffCache.
154154
chunksize = div(nelem, length(dc.du)) - 1
155-
@warn "The supplied dualcache was too small and was enlarged. This incurs allocations
155+
@warn "The supplied DiffCache was too small and was enlarged. This incurs allocations
156156
on the first call to `get_tmp`. If few calls to `get_tmp` occur and optimal performance is essential,
157-
consider changing 'N'/chunk size of this dualcache to $chunksize."
157+
consider changing 'N'/chunk size of this DiffCache to $chunksize."
158158
resize!(dc.dual_du, nelem)
159159
end
160160

@@ -183,7 +183,7 @@ function Base.getindex(b::LazyBufferCache, u::T) where {T <: AbstractArray}
183183
return buf
184184
end
185185

186-
export FixedSizeDiffCache, DiffCache, LazyBufferCache
186+
export FixedSizeDiffCache, DiffCache, LazyBufferCache, dualcache
187187
export get_tmp
188188

189189
end

test/core_dispatch.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ function test(u0, dual, chunk_size)
1616
result_dual2
1717
end
1818

19+
function structequal(struct1, struct2)
20+
if typeof(struct1) == typeof(struct2)
21+
fn = fieldnames(typeof(struct1))
22+
all(getfield(a, fn[i]) == getfield(b, fn[i]) for i in eachindex(fn))
23+
else
24+
return false
25+
end
26+
end
27+
1928
#Setup Base Array tests
2029
chunk_size = 5
2130
u0 = ones(5, 5)
@@ -140,3 +149,9 @@ tmp_dual_du_APN = get_tmp(cache_AP, dual_AP[1])
140149
@test size(tmp_dual_du_APN) == size(u0_AP)
141150
@test_broken typeof(tmp_dual_du_APN) == typeof(dual_AP)
142151
@test eltype(tmp_dual_du_APN) == eltype(dual_AP)
152+
153+
a = PreallocationTools.DiffCache(zeros(4), 4)
154+
b = PreallocationTools.DiffCache(zeros(4), Val{4}())
155+
c = PreallocationTools.DiffCache(zeros(4), Val{4})
156+
@test structequal(a, b)
157+
@test structequal(a, b)

0 commit comments

Comments
 (0)