|
| 1 | +module PreallocationToolsSparseConnectivityTracerExt |
| 2 | + |
| 3 | +using PreallocationTools |
| 4 | +isdefined(Base, :get_extension) ? (import SparseConnectivityTracer) : |
| 5 | +(import ..SparseConnectivityTracer) |
| 6 | + |
| 7 | +function PreallocationTools.get_tmp( |
| 8 | + dc::DiffCache, u::T) where {T <: SparseConnectivityTracer.Dual} |
| 9 | + if isbitstype(T) |
| 10 | + nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) |
| 11 | + if nelem > length(dc.dual_du) |
| 12 | + PreallocationTools.enlargediffcache!(dc, nelem) |
| 13 | + end |
| 14 | + PreallocationTools._restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) |
| 15 | + else |
| 16 | + PreallocationTools._restructure(dc.du, zeros(T, size(dc.du))) |
| 17 | + end |
| 18 | +end |
| 19 | + |
| 20 | +function PreallocationTools.get_tmp( |
| 21 | + dc::DiffCache, ::Type{T}) where {T <: SparseConnectivityTracer.Dual} |
| 22 | + if isbitstype(T) |
| 23 | + nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) |
| 24 | + if nelem > length(dc.dual_du) |
| 25 | + PreallocationTools.enlargediffcache!(dc, nelem) |
| 26 | + end |
| 27 | + PreallocationTools._restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) |
| 28 | + else |
| 29 | + PreallocationTools._restructure(dc.du, zeros(T, size(dc.du))) |
| 30 | + end |
| 31 | +end |
| 32 | + |
| 33 | +function PreallocationTools.get_tmp( |
| 34 | + dc::DiffCache, u::AbstractArray{T}) where {T <: SparseConnectivityTracer.Dual} |
| 35 | + if isbitstype(T) |
| 36 | + nelem = div(sizeof(T), sizeof(eltype(dc.dual_du))) * length(dc.du) |
| 37 | + if nelem > length(dc.dual_du) |
| 38 | + PreallocationTools.enlargediffcache!(dc, nelem) |
| 39 | + end |
| 40 | + PreallocationTools._restructure(dc.du, reinterpret(T, view(dc.dual_du, 1:nelem))) |
| 41 | + else |
| 42 | + PreallocationTools._restructure(dc.du, zeros(T, size(dc.du))) |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +end |
0 commit comments