|
3 | 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this
|
4 | 4 | # file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
5 | 5 |
|
6 |
| -# Lightweight unsafe view for vectors. It seems that the only way to avoid |
7 |
| -# triggering allocations is to have only bitstype fields, so we store a pointer. |
8 |
| -struct _VectorView{T} <: DenseVector{T} |
| 6 | +""" |
| 7 | + _UnsafeVectorView(offset, len, ptr) |
| 8 | +
|
| 9 | +Lightweight unsafe view for vectors. |
| 10 | +
|
| 11 | +## Motivation |
| 12 | +
|
| 13 | +`_UnsafeVectorView` is needed as an allocation-free equivalent of `view`. Other |
| 14 | +alternatives, like `view(x, 1:len)` or a struct like |
| 15 | +``` |
| 16 | +struct _SafeView{T} |
| 17 | + x::Vector{T} |
| 18 | + len::Int |
| 19 | +end |
| 20 | +``` |
| 21 | +will allocate so that `x` can be tracked by Julia's GC. |
| 22 | +
|
| 23 | +`_UnsafeVectorView` relies on the fact that the use-cases of `_UnsafeVectorView` |
| 24 | +only temporarily wrap a long-lived vector like `d.jac_storage` so that we don't |
| 25 | +have to worry about the GC removing `d.jac_storage` while `_UnsafeVectorView` |
| 26 | +exists. This lets us use a `Ptr{T}` and create a struct that is `isbitstype` and |
| 27 | +therefore does not allocate. |
| 28 | +
|
| 29 | +## Example |
| 30 | +
|
| 31 | +Instead of `view(x, 1:10)`, use `_UnsafeVectorView(0, 10, pointer(x))`. |
| 32 | +
|
| 33 | +## Unsafe behavior |
| 34 | +
|
| 35 | +`_UnsafeVectorView` is unsafe because it assumes that the vector `x` that the |
| 36 | +pointer `ptr` refers to remains valid during the usage of `_UnsafeVectorView`. |
| 37 | +""" |
| 38 | +struct _UnsafeVectorView{T} <: DenseVector{T} |
9 | 39 | offset::Int
|
10 | 40 | len::Int
|
11 | 41 | ptr::Ptr{T}
|
12 | 42 | end
|
13 | 43 |
|
14 |
| -Base.getindex(v::_VectorView, idx::Integer) = unsafe_load(v.ptr, idx + v.offset) |
| 44 | +Base.getindex(x::_UnsafeVectorView, i) = unsafe_load(x.ptr, i + x.offset) |
15 | 45 |
|
16 |
| -function Base.setindex!(v::_VectorView, value, idx::Integer) |
17 |
| - unsafe_store!(v.ptr, value, idx + v.offset) |
| 46 | +function Base.setindex!(x::_UnsafeVectorView, value, i) |
| 47 | + unsafe_store!(x.ptr, value, i + x.offset) |
18 | 48 | return value
|
19 | 49 | end
|
20 | 50 |
|
21 |
| -Base.length(v::_VectorView) = v.len |
| 51 | +Base.length(x::_UnsafeVectorView) = x.len |
| 52 | + |
| 53 | +function _UnsafeVectorView(x::Vector, N::Int) |
| 54 | + if length(x) < N |
| 55 | + resize!(x, N) |
| 56 | + end |
| 57 | + return _UnsafeVectorView(0, N, pointer(x)) |
| 58 | +end |
| 59 | + |
| 60 | +""" |
| 61 | + _UnsafeHessianView(x, N) |
| 62 | +
|
| 63 | +Lightweight unsafe view that converts a vector `x` into the upper-triangular |
| 64 | +component of a symmetric `N`-by-`N` matrix. |
| 65 | +
|
| 66 | +## Motivation |
| 67 | +
|
| 68 | +`_UnsafeHessianView` is needed as an allocation-free equivalent of `view`. Other |
| 69 | +alternatives, like `reshape(view(x, 1:N^2), N, N)` or a struct like |
| 70 | +``` |
| 71 | +struct _SafeView{T} |
| 72 | + x::Vector{T} |
| 73 | + len::Int |
| 74 | +end |
| 75 | +``` |
| 76 | +will allocate so that `x` can be tracked by Julia's GC. |
| 77 | +
|
| 78 | +`_UnsafeHessianView` relies on the fact that the use-cases of |
| 79 | +`_UnsafeHessianView` only temporarily wrap a long-lived vector like |
| 80 | +`d.jac_storage` so that we don't have to worry about the GC removing |
| 81 | +`d.jac_storage` while `_UnsafeHessianView` exists. This lets us use a `Ptr{T}` |
| 82 | +and create a struct that is `isbitstype` and therefore does not allocate. |
| 83 | +
|
| 84 | +## Unsafe behavior |
| 85 | +
|
| 86 | +`_UnsafeHessianView` is unsafe because it assumes that the vector `x` remains |
| 87 | +valid during the usage of `_UnsafeHessianView`. |
| 88 | +""" |
| 89 | +struct _UnsafeHessianView <: AbstractMatrix{Float64} |
| 90 | + N::Int |
| 91 | + x::_UnsafeVectorView{T} |
| 92 | +end |
| 93 | + |
| 94 | +Base.size(x::_UnsafeHessianView) = (x.N, x.N) |
| 95 | + |
| 96 | +_linear_index(i, j) = i >= j ? div((i - 1) * i, 2) + j : _linear_index(j, i) |
| 97 | + |
| 98 | +Base.getindex(x::_UnsafeHessianView, i, j) = x.x[_linear_index(i, j)] |
| 99 | + |
| 100 | +function Base.setindex!(x::_UnsafeHessianView, value, i, j) |
| 101 | + return x[_linear_index(i, j)] = value |
| 102 | +end |
| 103 | + |
| 104 | +function _UnsafeHessianView(x::Vector, N::Int) |
| 105 | + z = div(N * (N + 1), 2) |
| 106 | + if length(x) < z |
| 107 | + resize!(x, z) |
| 108 | + end |
| 109 | + for i in 1:z |
| 110 | + x[i] = 0.0 |
| 111 | + end |
| 112 | + return _UnsafeHessianView(N, _UnsafeVectorView(0, z, pointer(x))) |
| 113 | +end |
| 114 | + |
| 115 | +""" |
| 116 | + _reinterpret_unsafe(::Type{T}, x::Vector{R}) where {T,R} |
22 | 117 |
|
| 118 | +Return an `_UnsafeVectorView` that is the result of re-interpreting the vector |
| 119 | +`x` as having the element type `T`. |
| 120 | +""" |
23 | 121 | function _reinterpret_unsafe(::Type{T}, x::Vector{R}) where {T,R}
|
24 | 122 | # how many T's fit into x?
|
25 | 123 | @assert isbitstype(T) && isbitstype(R)
|
26 | 124 | len = length(x) * sizeof(R)
|
27 | 125 | p = reinterpret(Ptr{T}, pointer(x))
|
28 |
| - return _VectorView(0, div(len, sizeof(T)), p) |
| 126 | + return _UnsafeVectorView(0, div(len, sizeof(T)), p) |
29 | 127 | end
|
0 commit comments