forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genericmemory.jl
267 lines (225 loc) · 8.48 KB
/
genericmemory.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# This file is a part of Julia. License is MIT: https://julialang.org/license
## genericmemory.jl: Managed Memory
"""
GenericMemory{kind::Symbol, T, addrspace::Int} <: AbstractVector{T}
One-dimensional dense array with elements of type `T`.
"""
GenericMemory
"""
Memory{T} == GenericMemory{:not_atomic, T, Core.CPU}
One-dimensional dense array with elements of type `T`.
"""
Memory
## Basic functions ##
using Core: memoryrefoffset, memoryref_isassigned # import more functions which were not essential
size(a::GenericMemory, d::Int) =
d < 1 ? error("dimension out of range") :
d == 1 ? length(a) :
1
size(a::GenericMemory, d::Integer) = size(a, convert(d, Int))
size(a::GenericMemory) = (length(a),)
IndexStyle(::Type{<:GenericMemory}) = IndexLinear()
pointer(mem::GenericMemoryRef) = unsafe_convert(Ptr{Cvoid}, mem) # no bounds check, even for empty array
_unsetindex!(A::Memory, i::Int) = (@_propagate_inbounds_meta; _unsetindex!(GenericMemoryRef(A, i)); A)
function _unsetindex!(A::MemoryRef{T}) where T
@_terminates_locally_meta
@_propagate_inbounds_meta
@inline
@boundscheck GenericMemoryRef(A, 1)
mem = A.mem
MemT = typeof(mem)
arrayelem = datatype_arrayelem(MemT)
elsz = datatype_layoutsize(MemT)
isboxed = 1; isunion = 2
t = @_gc_preserve_begin mem
p = Ptr{Ptr{Cvoid}}(@inbounds pointer(A))
if arrayelem == isboxed
Intrinsics.atomic_pointerset(p, C_NULL, :monotonic)
elseif arrayelem != isunion
if !datatype_pointerfree(T::DataType)
for j = 1:Core.sizeof(Ptr{Cvoid}):elsz
Intrinsics.atomic_pointerset(p + j - 1, C_NULL, :monotonic)
end
end
end
@_gc_preserve_end t
return A
end
elsize(@nospecialize _::Type{A}) where {T,A<:GenericMemory{<:Any,T}} = aligned_sizeof(T)
sizeof(a::GenericMemory) = Core.sizeof(a)
# multi arg case will be overwritten later. This is needed for bootstrapping
function isassigned(a::Memory, i::Int)
@inline
@boundscheck (i - 1)%UInt < length(a)%UInt || return false
return @inbounds memoryref_isassigned(GenericMemoryRef(a, i), :not_atomic, false)
end
isassigned(a::GenericMemoryRef) = memoryref_isassigned(a, :not_atomic, @_boundscheck)
## copy ##
function unsafe_copyto!(dest::MemoryRef{T}, src::MemoryRef{T}, n) where {T}
@_terminates_globally_meta
n == 0 && return dest
@boundscheck GenericMemoryRef(dest, n), GenericMemoryRef(src, n)
ccall(:jl_genericmemory_copyto, Cvoid, (Any, Ptr{Cvoid}, Any, Ptr{Cvoid}, Int), dest.mem, dest.ptr_or_offset, src.mem, src.ptr_or_offset, Int(n))
return dest
end
function unsafe_copyto!(dest::GenericMemoryRef, src::GenericMemoryRef, n)
n == 0 && return dest
@boundscheck GenericMemoryRef(dest, n), GenericMemoryRef(src, n)
unsafe_copyto!(dest.mem, memoryrefoffset(dest), src.mem, memoryrefoffset(src), n)
return dest
end
function unsafe_copyto!(dest::Memory{T}, doffs, src::Memory{T}, soffs, n) where{T}
n == 0 && return dest
unsafe_copyto!(GenericMemoryRef(dest, doffs), GenericMemoryRef(src, soffs), n)
return dest
end
function unsafe_copyto!(dest::Memory, doffs, src::Memory, soffs, n)
@_terminates_locally_meta
n == 0 && return dest
# use pointer math to determine if they are deemed to alias
destp = pointer(dest, doffs)
srcp = pointer(src, soffs)
endp = pointer(src, soffs + n - 1)
@inbounds if destp < srcp || destp > endp
for i = 1:n
if isassigned(src, soffs + i - 1)
dest[doffs + i - 1] = src[soffs + i - 1]
else
_unsetindex!(dest, doffs + i - 1)
end
end
else
for i = n:-1:1
if isassigned(src, soffs + i - 1)
dest[doffs + i - 1] = src[soffs + i - 1]
else
_unsetindex!(dest, doffs + i - 1)
end
end
end
return dest
end
copy(a::T) where {T<:Memory} = ccall(:jl_genericmemory_copy, Ref{T}, (Any,), a)
function copyto!(dest::Memory, doffs::Integer, src::Memory, soffs::Integer, n::Integer)
n < 0 && _throw_argerror("Number of elements to copy must be non-negative.")
unsafe_copyto!(dest, doffs, src, soffs, n)
return dest
end
## Constructors ##
similar(a::Memory{T}) where {T} = Memory{T}(undef, length(a))
similar(a::Memory{T}, S::Type) where {T} = Memory{S}(undef, length(a))
similar(a::Memory{T}, m::Int) where {T} = Memory{T}(undef, m)
similar(a::Memory, T::Type, dims::Dims{1}) = Memory{T}(undef, dims[1])
similar(a::Memory{T}, dims::Dims{1}) where {T} = Memory{T}(undef, dims[1])
function fill!(a::Union{Memory{UInt8}, Memory{Int8}}, x::Integer)
t = @_gc_preserve_begin a
p = unsafe_convert(Ptr{Cvoid}, a)
T = eltype(a)
memset(p, x isa T ? x : convert(T, x), length(a))
@_gc_preserve_end t
return a
end
## Conversions ##
convert(::Type{T}, a::AbstractArray) where {T<:GenericMemory} = a isa T ? a : T(a)::T
promote_rule(a::Type{Memory{T}}, b::Type{Memory{S}}) where {T,S} = el_same(promote_type(T,S), a, b)
promote_rule(a::Type{GenericMemory{:atomic,T,Core.CPU}}, b::Type{GenericMemory{:atomic,S,Core.CPU}}) where {T,S} = el_same(promote_type(T,S), a, b)
## Constructors ##
if nameof(@__MODULE__) === :Base # avoid method overwrite
# constructors should make copies
Memory{T}(x::AbstractArray{S,1}) where {T,S} = copyto_axcheck!(Memory{T}(undef, size(x)), x)
end
## copying iterators to containers
## Iteration ##
iterate(A::Memory, i=1) = (@inline; (i - 1)%UInt < length(A)%UInt ? (@inbounds A[i], i + 1) : nothing)
## Indexing: getindex ##
# Faster contiguous indexing using copyto! for AbstractUnitRange and Colon
function getindex(A::Memory, I::AbstractUnitRange{<:Integer})
@inline
@boundscheck checkbounds(A, I)
lI = length(I)
X = similar(A, axes(I))
if lI > 0
copyto!(X, firstindex(X), A, first(I), lI)
end
return X
end
# getindex for carrying out logical indexing for AbstractUnitRange{Bool} as Bool <: Integer
getindex(a::Memory, r::AbstractUnitRange{Bool}) = getindex(a, to_index(r))
getindex(A::Memory, c::Colon) = copy(A)
## Indexing: setindex! ##
function setindex!(A::Memory{T}, x, i1::Int) where {T}
val = x isa T ? x : convert(T,x)::T
ref = memoryref(memoryref(A), i1, @_boundscheck)
memoryrefset!(ref, val, :not_atomic, @_boundscheck)
return A
end
function setindex!(A::Memory{T}, x, i1::Int, i2::Int, I::Int...) where {T}
@inline
@boundscheck (i2 == 1 && all(==(1), I)) || throw_boundserror(A, (i1, i2, I...))
setindex!(A, x, i1)
end
# Faster contiguous setindex! with copyto!
function setindex!(A::Memory{T}, X::Memory{T}, I::AbstractUnitRange{Int}) where T
@inline
@boundscheck checkbounds(A, I)
lI = length(I)
@boundscheck setindex_shape_check(X, lI)
if lI > 0
unsafe_copyto!(A, first(I), X, 1, lI)
end
return A
end
function setindex!(A::Memory{T}, X::Memory{T}, c::Colon) where T
@inline
lI = length(A)
@boundscheck setindex_shape_check(X, lI)
if lI > 0
unsafe_copyto!(A, 1, X, 1, lI)
end
return A
end
# use memcmp for cmp on byte arrays
function cmp(a::Memory{UInt8}, b::Memory{UInt8})
ta = @_gc_preserve_begin a
tb = @_gc_preserve_begin b
pa = unsafe_convert(Ptr{Cvoid}, a)
pb = unsafe_convert(Ptr{Cvoid}, b)
c = memcmp(pa, pb, min(length(a),length(b)))
@_gc_preserve_end ta
@_gc_preserve_end tb
return c < 0 ? -1 : c > 0 ? +1 : cmp(length(a),length(b))
end
const BitIntegerMemory{N} = Union{map(T->Memory{T}, BitInteger_types)...}
# use memcmp for == on bit integer types
function ==(a::M, b::M) where {M <: BitIntegerMemory}
if length(a) == length(b)
ta = @_gc_preserve_begin a
tb = @_gc_preserve_begin b
pa = unsafe_convert(Ptr{Cvoid}, a)
pb = unsafe_convert(Ptr{Cvoid}, b)
c = memcmp(pa, pb, sizeof(eltype(M)) * length(a))
@_gc_preserve_end ta
@_gc_preserve_end tb
return c == 0
else
return false
end
end
function findall(pred::Fix2{typeof(in),<:Union{Memory{<:Real},Real}}, x::Memory{<:Real})
if issorted(x, Sort.Forward) && issorted(pred.x, Sort.Forward)
return _sortedfindin(x, pred.x)
else
return _findin(x, pred.x)
end
end
# Copying subregions
function indcopy(sz::Dims, I::GenericMemory)
n = length(I)
s = sz[n]
for i = n+1:length(sz)
s *= sz[i]
end
dst = eltype(I)[_findin(I[i], i < n ? (1:sz[i]) : (1:s)) for i = 1:n]
src = eltype(I)[I[i][_findin(I[i], i < n ? (1:sz[i]) : (1:s))] for i = 1:n]
dst, src
end