@@ -35,23 +35,41 @@ Returns the `Dual` or normal cache array stored in `dc` based on the type of `u`
35
35
"""
36
36
function get_tmp (dc:: DiffCache , u:: T ) where T<: ForwardDiff.Dual
37
37
nelem = div (sizeof (T), sizeof (eltype (dc. dual_du)))* length (dc. du)
38
+ if nelem > length (dc. dual_du)
39
+ enlargedualcache! (dc, nelem)
40
+ end
38
41
ArrayInterface. restructure (dc. du, reinterpret (T, view (dc. dual_du, 1 : nelem)))
39
42
end
40
43
41
44
function get_tmp (dc:: DiffCache , u:: AbstractArray{T} ) where T<: ForwardDiff.Dual
42
45
nelem = div (sizeof (T), sizeof (eltype (dc. dual_du)))* length (dc. du)
46
+ if nelem > length (dc. dual_du)
47
+ enlargedualcache! (dc, nelem)
48
+ end
43
49
ArrayInterface. restructure (dc. du, reinterpret (T, view (dc. dual_du, 1 : nelem)))
44
50
end
45
51
46
52
function get_tmp (dc:: DiffCache , u:: LabelledArrays.LArray{T,N,D,Syms} ) where {T,N,D,Syms}
47
53
nelem = div (sizeof (T), sizeof (eltype (dc. dual_du)))* length (dc. du)
54
+ if nelem > length (dc. dual_du)
55
+ enlargedualcache! (dc, nelem)
56
+ end
48
57
_x = ArrayInterface. restructure (dc. du, reinterpret (T, view (dc. dual_du, 1 : nelem)))
49
58
LabelledArrays. LArray {T,N,D,Syms} (_x)
50
59
end
51
60
52
61
get_tmp (dc:: DiffCache , u:: Number ) = dc. du
53
62
get_tmp (dc:: DiffCache , u:: AbstractArray ) = dc. du
54
63
64
+ function enlargedualcache! (dc, nelem) # warning comes only once per dualcache.
65
+ chunksize = div (nelem, length (dc. du)) - 1
66
+ @warn " The supplied dualcache was too small and was enlarged. This incurrs allocations
67
+ on the first call to get_tmp. If few calls to get_tmp occur and optimal performance is essential,
68
+ consider changing 'N'/chunk size of this dualcache to $chunksize ."
69
+ resize! (dc. dual_du, nelem)
70
+ end
71
+
72
+
55
73
"""
56
74
b = LazyBufferCache(f=identity)
57
75
0 commit comments