Skip to content

Commit 1a58153

Browse files
committed
Add docstring to _reinterpret_unsafe
1 parent e0d6bb3 commit 1a58153

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/Nonlinear/ReverseAD/utils.jl

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,41 @@ function _UnsafeLowerTriangularMatrixView(x::Vector{Float64}, N::Int)
164164
return _UnsafeLowerTriangularMatrixView(N, pointer(x))
165165
end
166166

167+
"""
168+
_reinterpret_unsafe(::Type{T}, x::Vector{R}) where {T,R}
169+
170+
Return an `_UnsafeVectorView` that act as a vector of element type
171+
`T` over the same bytes as `x`. Note that if `length(x) * sizeof(R)` is not
172+
a multiple of `sizeof(T)`, the last bits will be ignored. This is a key
173+
difference with `reinterpret` which errors in that case.
174+
175+
Given a vector of `Float64` of length equal to the maximum number of nodes of a
176+
set of expressions time the maximum chunk size, this function is used to
177+
reinterpret it as a vector of `ForwardDiff.Partials{N,T}` where `N` is the
178+
chunk size of one of the expressions of the set. In that case, we know that
179+
the vector has enough bytes and we don't care about the leftover bytes at the
180+
end.
181+
182+
## Examples
183+
184+
```jldoctest
185+
julia> x = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
186+
3-element Vector{Tuple{Int64, Int64, Int64}}:
187+
(1, 2, 3)
188+
(4, 5, 6)
189+
(7, 8, 9)
190+
191+
julia> MOI.Nonlinear.ReverseAD._reinterpret_unsafe(NTuple{2,Int}, x)
192+
4-element MathOptInterface.Nonlinear.ReverseAD._UnsafeVectorView{Tuple{Int64, Int64}}:
193+
(1, 2)
194+
(3, 4)
195+
(5, 6)
196+
(7, 8)
197+
```
198+
"""
167199
function _reinterpret_unsafe(::Type{T}, x::Vector{R}) where {T,R}
168-
# how many T's fit into x?
169200
@assert isbitstype(T) && isbitstype(R)
201+
# how many T's fit into x?
170202
len = length(x) * sizeof(R)
171203
p = reinterpret(Ptr{T}, pointer(x))
172204
return _UnsafeVectorView(0, div(len, sizeof(T)), p)

0 commit comments

Comments
 (0)