@@ -2,55 +2,49 @@ module PreallocationTools
22
33using ForwardDiff, ArrayInterface, LabelledArrays
44
5- struct DiffCache{T<: AbstractArray , S<: AbstractArray }
5+ struct DiffCache{T<: AbstractArray ,S<: AbstractArray }
66 du:: T
77 dual_du:: S
88end
99
10+ #= removed dependency on ArrayInterface, because it seemed not necessary anymore;
11+ not sure whether it breaks things that are not in the testset; needs checking. =#
1012function DiffCache (u:: AbstractArray{T} , siz, :: Type{Val{chunk_size}} ) where {T, chunk_size}
11- x = ArrayInterface . restructure (u, zeros (ForwardDiff . Dual{ nothing ,T, chunk_size}, siz... ))
13+ x = zeros (T,( chunk_size+ 1 ) * prod ( siz))
1214 DiffCache (u, x)
1315end
1416
1517"""
1618
1719`dualcache(u::AbstractArray, N = Val{default_cache_size(length(u))})`
1820
19- Builds a `DualCache` object that stores both a version of the cache for `u`
20- and for the `Dual` version of `u`, allowing use of pre-cached vectors with
21+ Builds a `DualCache` object that stores versions of the cache for `u` and for the `Dual` version of `u` allowing use of pre-cached arrays with
2122forward-mode automatic differentiation.
2223
2324"""
2425dualcache (u:: AbstractArray , N= Val{ForwardDiff. pickchunksize (length (u))}) = DiffCache (u, size (u), N)
2526
26- chunksize (:: Type{ForwardDiff.Dual{T,V,N}} ) where {T,V,N} = N
27+ """
28+
29+ `get_tmp(dc::DiffCache, u)`
30+
31+ Returns the `Dual` or normal cache array stored in `dc` based on the type of `u`.
2732
33+ """
2834function get_tmp (dc:: DiffCache , u:: T ) where T<: ForwardDiff.Dual
29- x = reinterpret (T, dc. dual_du)
30- if chunksize (T) === chunksize (eltype (dc. dual_du))
31- x
32- else
33- @view x[axes (dc. du)... ]
34- end
35+ nelem = div (sizeof (T), sizeof (eltype (dc. dual_du)))* prod (size (dc. du))
36+ reshape (reinterpret (T, view (dc. dual_du, 1 : nelem)), size (dc. du))
3537end
3638
3739function get_tmp (dc:: DiffCache , u:: AbstractArray{T} ) where T<: ForwardDiff.Dual
38- x = reinterpret (T, dc. dual_du)
39- if chunksize (T) === chunksize (eltype (dc. dual_du))
40- x
41- else
42- @view x[axes (dc. du)... ]
43- end
40+ nelem = div (sizeof (T), sizeof (eltype (dc. dual_du)))* prod (size (dc. du))
41+ reshape (reinterpret (T, view (dc. dual_du, 1 : nelem)), size (dc. du))
4442end
4543
4644function get_tmp (dc:: DiffCache , u:: LabelledArrays.LArray{T,N,D,Syms} ) where {T,N,D,Syms}
47- x = reinterpret (T, dc. dual_du. __x)
48- _x = if chunksize (T) === chunksize (eltype (dc. dual_du))
49- x
50- else
51- @view x[axes (dc. du)... ]
52- end
53- LabelledArrays. LArray {T,N,D,Syms} (_x)
45+ nelem = div (sizeof (T), sizeof (eltype (dc. dual_du)))* prod (size (dc. du))
46+ _x = reshape (reinterpret (T, view (dc. dual_du, 1 : nelem)), size (dc. du))
47+ LabelledArrays. LArray {T,N,D,Syms} (_x)
5448end
5549
5650get_tmp (dc:: DiffCache , u:: Number ) = dc. du
@@ -62,6 +56,7 @@ get_tmp(dc::DiffCache, u::AbstractArray) = dc.du
6256A lazily allocated buffer object. Given a vector `u`, `b[u]` returns a `Vector` of the
6357same element type and length `f(length(u))` (defaulting to the same length), which is
6458allocated as needed and then cached within `b` for subsequent usage.
59+
6560"""
6661struct LazyBufferCache{F<: Function }
6762 bufs:: Dict # a dictionary mapping types to buffers
0 commit comments