-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
PreallocationTools.jl
289 lines (243 loc) · 9.02 KB
/
PreallocationTools.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
module PreallocationTools
using ForwardDiff, ArrayInterface, Adapt
struct FixedSizeDiffCache{T <: AbstractArray, S <: AbstractArray}
du::T
dual_du::S
any_du::Vector{Any}
end
function FixedSizeDiffCache(u::AbstractArray{T}, siz,
::Type{Val{chunk_size}}) where {T, chunk_size}
x = ArrayInterface.restructure(u,
zeros(ForwardDiff.Dual{nothing, T, chunk_size},
siz...))
xany = Any[]
FixedSizeDiffCache(deepcopy(u), x, xany)
end
"""
`FixedSizeDiffCache(u::AbstractArray, N = Val{default_cache_size(length(u))})`
Builds a `FixedSizeDiffCache` object that stores both a version of the cache for `u`
and for the `Dual` version of `u`, allowing use of pre-cached vectors with
forward-mode automatic differentiation.
"""
function FixedSizeDiffCache(u::AbstractArray,
::Type{Val{N}} = Val{ForwardDiff.pickchunksize(length(u))}) where {
N,
}
FixedSizeDiffCache(u, size(u), Val{N})
end
function FixedSizeDiffCache(u::AbstractArray, N::Integer)
FixedSizeDiffCache(u, size(u), Val{N})
end
chunksize(::Type{ForwardDiff.Dual{T, V, N}}) where {T, V, N} = N
function get_tmp(dc::FixedSizeDiffCache, u::T) where {T <: ForwardDiff.Dual}
x = reinterpret(T, dc.dual_du)
if chunksize(T) === chunksize(eltype(dc.dual_du))
x
else
@view x[axes(dc.du)...]
end
end
function get_tmp(dc::FixedSizeDiffCache, u::Type{T}) where {T <: ForwardDiff.Dual}
x = reinterpret(T, dc.dual_du)
if chunksize(T) === chunksize(eltype(dc.dual_du))
x
else
@view x[axes(dc.du)...]
end
end
function get_tmp(dc::FixedSizeDiffCache, u::AbstractArray{T}) where {T <: ForwardDiff.Dual}
x = reinterpret(T, dc.dual_du)
if chunksize(T) === chunksize(eltype(dc.dual_du))
x
else
@view x[axes(dc.du)...]
end
end
function get_tmp(dc::FixedSizeDiffCache, u::Union{Number, AbstractArray})
if promote_type(eltype(dc.du), eltype(u)) <: eltype(dc.du)
dc.du
else
if length(dc.du) > length(dc.any_du)
resize!(dc.any_du, length(dc.du))
end
_restructure(dc.du, dc.any_du)
end
end
function get_tmp(dc::FixedSizeDiffCache, ::Type{T}) where {T <: Number}
if promote_type(eltype(dc.du), T) <: eltype(dc.du)
dc.du
else
if length(dc.du) > length(dc.any_du)
resize!(dc.any_du, length(dc.du))
end
_restructure(dc.du, dc.any_du)
end
end
# DiffCache
struct DiffCache{T <: AbstractArray, S <: AbstractArray}
du::T
dual_du::S
any_du::Vector{Any}
end
function DiffCache(u::AbstractArray{T}, siz, chunk_sizes) where {T}
x = adapt(ArrayInterface.parameterless_type(u),
zeros(T, prod(chunk_sizes .+ 1) * prod(siz)))
xany = Any[]
DiffCache(u, x, xany)
end
"""
`DiffCache(u::AbstractArray, N::Int = ForwardDiff.pickchunksize(length(u)); levels::Int = 1)`
`DiffCache(u::AbstractArray; N::AbstractArray{<:Int})`
Builds a `DiffCache` object that stores both a version of the cache for `u`
and for the `Dual` version of `u`, allowing use of pre-cached vectors with
forward-mode automatic differentiation. Supports nested AD via keyword `levels`
or specifying an array of chunk_sizes.
"""
function DiffCache(u::AbstractArray, N::Int = ForwardDiff.pickchunksize(length(u));
levels::Int = 1)
DiffCache(u, size(u), N * ones(Int, levels))
end
DiffCache(u::AbstractArray, N::AbstractArray{<:Int}) = DiffCache(u, size(u), N)
function DiffCache(u::AbstractArray, ::Type{Val{N}}; levels::Int = 1) where {N}
DiffCache(u, N; levels)
end
DiffCache(u::AbstractArray, ::Val{N}; levels::Int = 1) where {N} = DiffCache(u, N; levels)
# Legacy deprecate later
const dualcache = DiffCache
"""
`get_tmp(dc::DiffCache, u)`
Returns the `Dual` or normal cache array stored in `dc` based on the type of `u`.
"""
function get_tmp(dc::DiffCache, u::T) where {T <: ForwardDiff.Dual}
if isbitstype(T)
nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du)
if nelem > length(dc.dual_du)
enlargediffcache!(dc, nelem)
end
_restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem)))
else
_restructure(dc.du, zeros(T, size(dc.du)))
end
end
function get_tmp(dc::DiffCache, ::Type{T}) where {T <: ForwardDiff.Dual}
if isbitstype(T)
nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du)
if nelem > length(dc.dual_du)
enlargediffcache!(dc, nelem)
end
_restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem)))
else
_restructure(dc.du, zeros(T, size(dc.du)))
end
end
function get_tmp(dc::DiffCache, u::AbstractArray{T}) where {T <: ForwardDiff.Dual}
if isbitstype(T)
nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du)
if nelem > length(dc.dual_du)
enlargediffcache!(dc, nelem)
end
_restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem)))
else
_restructure(dc.du, zeros(T, size(dc.du)))
end
end
function get_tmp(dc::DiffCache, u::Union{Number, AbstractArray})
if promote_type(eltype(dc.du), eltype(u)) <: eltype(dc.du)
dc.du
else
if length(dc.du) > length(dc.any_du)
resize!(dc.any_du, length(dc.du))
end
_restructure(dc.du, dc.any_du)
end
end
function get_tmp(dc::DiffCache, ::Type{T}) where {T <: Number}
if promote_type(eltype(dc.du), T) <: eltype(dc.du)
dc.du
else
if length(dc.du) > length(dc.any_du)
resize!(dc.any_du, length(dc.du))
end
_restructure(dc.du, dc.any_du)
end
end
get_tmp(dc, u) = dc
function _restructure(normal_cache::Array, duals)
reshape(duals, size(normal_cache)...)
end
function _restructure(normal_cache::AbstractArray, duals)
ArrayInterface.restructure(normal_cache, duals)
end
function enlargediffcache!(dc, nelem) #warning comes only once per DiffCache.
chunksize = div(nelem, length(dc.du)) - 1
@warn "The supplied DiffCache was too small and was enlarged. This incurs allocations
on the first call to `get_tmp`. If few calls to `get_tmp` occur and optimal performance is essential,
consider changing 'N'/chunk size of this DiffCache to $chunksize." maxlog=1
resize!(dc.dual_du, nelem)
end
# LazyBufferCache
"""
b = LazyBufferCache(f = identity; initializer! = identity)
A lazily allocated buffer object. Given an array `u`, `b[u]` returns an array of the
same type and size `f(size(u))` (defaulting to the same size), which is allocated as
needed and then cached within `b` for subsequent usage.
By default the created buffers are not initialized, but a function `initializer!`
can be supplied which is applied to the buffer when it is created, for instance `buf -> fill!(buf, 0.0)`.
Optionally, the size can be explicitly given at calltime using `b[u,s]`, which will
return a cache of size `s`.
"""
struct LazyBufferCache{F <: Function, I <: Function}
bufs::Dict{Any, Any} # a dictionary mapping (type, size) pairs to buffers
sizemap::F
initializer!::I
function LazyBufferCache(
f::F = identity; initializer!::I = identity) where {
F <: Function, I <: Function}
new{F, I}(Dict(), f, initializer!)
end # start with empty dict
end
similar_type(x::AbstractArray, s::Integer) = similar_type(x, (s,))
function similar_type(x::AbstractArray{T}, s::NTuple{N, Integer}) where {T, N}
# The compiler is smart enough to not allocate
# here for simple types like Array and SubArray
typeof(similar(x, ntuple(Returns(1), N)))
end
function get_tmp(
b::LazyBufferCache, u::T, s = b.sizemap(size(u))) where {T <: AbstractArray}
get!(b.bufs, (T, s)) do
buffer = similar(u, s) # buffer to allocate if it was not found in b.bufs
b.initializer!(buffer)
buffer
end::similar_type(u, s) # declare type since b.bufs dictionary is untyped
end
# override the [] method
function Base.getindex(
b::LazyBufferCache, u::T, s = b.sizemap(size(u))) where {T <: AbstractArray}
get_tmp(b, u, s)
end
# GeneralLazyBufferCache
"""
b = GeneralLazyBufferCache(f=identity)
A lazily allocated buffer object. Given an array `u`, `b[u]` returns a cache object
generated by `f(u)`, but the generator is only run the first time (and all subsequent
times it reuses the same cache)
## Limitation
The main limitation of this method is that its return is not type-inferred, and thus
it can be slower than some other preallocation techniques. However, if used
correct using things like function barriers, then this is a general technique that
is sufficiently fast.
"""
struct GeneralLazyBufferCache{F <: Function}
bufs::Dict{Any, Any} # a dictionary mapping types to buffers
f::F
GeneralLazyBufferCache(f::F = identity) where {F <: Function} = new{F}(Dict(), f) # start with empty dict
end
function get_tmp(b::GeneralLazyBufferCache, u::T) where {T}
get!(b.bufs, T) do
b.f(u)
end
end
Base.getindex(b::GeneralLazyBufferCache, u::T) where {T} = get_tmp(b, u)
export GeneralLazyBufferCache, FixedSizeDiffCache, DiffCache, LazyBufferCache, dualcache
export get_tmp
end