-
Notifications
You must be signed in to change notification settings - Fork 36
/
array.jl
371 lines (280 loc) · 11.6 KB
/
array.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# host array
export MtlArray, MtlVector, MtlMatrix, MtlVecOrMat
function hasfieldcount(@nospecialize(dt))
try
fieldcount(dt)
catch
return false
end
return true
end
function contains_double(T)
if T === Float64
return true
elseif T isa Union
for U in Base.uniontypes(T)
contains_double(U) && return true
end
elseif hasfieldcount(T)
for U in fieldtypes(T)
contains_double(U) && return true
end
end
return false
end
mutable struct MtlArray{T,N} <: AbstractGPUArray{T,N}
buffer::MtlBuffer
maxsize::Int # maximum data size; excluding any selector bytes
offset::Int # offset of the data in the buffer, in number of elements
dims::Dims{N}
function MtlArray{T,N}(::UndefInitializer, dims::Dims{N}; storage=Shared) where {T,N}
Base.allocatedinline(T) || error("MtlArray only supports element types that are stored inline")
contains_double(T) && @warn "Metal does not support Float64 values, try using Float32 instead" maxlog=1
maxsize = prod(dims) * sizeof(T)
bufsize = if Base.isbitsunion(T)
# type tag array past the data
maxsize + prod(dims)
else
maxsize
end
dev = current_device()
if bufsize > 0
buf = alloc(dev, bufsize; storage=storage)
buf.label = "MtlArray{$(T),$(N)}(dims=$dims)"
else
buf = MtlBuffer(C_NULL)
end
obj = new(buf, maxsize, 0, dims)
finalizer(obj) do arr
free(arr.buffer)
end
return obj
end
function MtlArray{T,N}(buffer::MtlBuffer, dims::Dims{N};
maxsize::Int=prod(dims) * sizeof(T), offset::Int=0) where {T,N}
Base.allocatedinline(T) || error("MtlArray only supports element types that are stored inline")
MTL.mtRetain(buffer.handle)
obj = new{T,N}(buffer, maxsize, offset, dims)
finalizer(obj) do arr
free(arr.buffer)
end
return obj
end
end
device(A::MtlArray) = A.buffer.device
## aliases
const MtlVector{T} = MtlArray{T,1}
const MtlMatrix{T} = MtlArray{T,2}
const MtlVecOrMat{T} = Union{MtlVector{T},MtlMatrix{T}}
## constructors
# type and dimensionality specified, accepting dims as series of Ints
MtlArray{T,N}(::UndefInitializer, dims::Integer...) where {T,N} =
MtlArray{T,N}(undef, Dims(dims))
# type but not dimensionality specified
MtlArray{T}(::UndefInitializer, dims::Dims{N}) where {T,N} = MtlArray{T,N}(undef, dims)
MtlArray{T}(::UndefInitializer, dims::Integer...) where {T} =
MtlArray{T}(undef, convert(Tuple{Vararg{Int}}, dims))
# empty vector constructor
MtlArray{T,1}() where {T} = MtlArray{T,1}(undef, 0)
Base.similar(a::MtlArray{T,N}) where {T,N} = MtlArray{T,N}(undef, size(a))
Base.similar(a::MtlArray{T}, dims::Base.Dims{N}) where {T,N} = MtlArray{T,N}(undef, dims)
Base.similar(a::MtlArray, ::Type{T}, dims::Base.Dims{N}) where {T,N} =
MtlArray{T,N}(undef, dims)
function Base.copy(a::MtlArray{T,N}) where {T,N}
b = similar(a)
@inbounds copyto!(b, a)
end
## array interface
Base.elsize(::Type{<:MtlArray{T}}) where {T} = sizeof(T)
Base.size(x::MtlArray) = x.dims
Base.sizeof(x::MtlArray) = Base.elsize(x) * length(x)
Base.pointer(x::MtlArray{T}) where {T} = Base.unsafe_convert(MtlPointer{T}, x)
@inline function Base.pointer(x::MtlArray{T}, i::Integer) where T
Base.unsafe_convert(MtlPointer{T}, x) + Base._memory_offset(x, i)
end
Base.unsafe_convert(::Type{Ptr{S}}, x::MtlArray{T}) where {S, T} =
throw(ArgumentError("cannot take the CPU address of a $(typeof(x))"))
Base.unsafe_convert(t::Type{MtlPointer{T}}, x::MtlArray) where {T} =
MtlPointer{T}(x.buffer, x.offset*Base.elsize(x))
## interop with other arrays
@inline function MtlArray{T,N}(xs::AbstractArray{T,N}) where {T,N}
A = MtlArray{T,N}(undef, size(xs))
copyto!(A, convert(Array{T}, xs))
return A
end
MtlArray{T,N}(xs::AbstractArray{S,N}) where {T,N,S} = MtlArray{T,N}(map(T, xs))
# underspecified constructors
MtlArray{T}(xs::AbstractArray{S,N}) where {T,N,S} = MtlArray{T,N}(xs)
(::Type{MtlArray{T,N} where T})(x::AbstractArray{S,N}) where {S,N} = MtlArray{S,N}(x)
MtlArray(A::AbstractArray{T,N}) where {T,N} = MtlArray{T,N}(A)
# idempotency
MtlArray{T,N}(xs::MtlArray{T,N}) where {T,N} = xs
## derived types
# wrapped arrays: can be used in kernels
const WrappedMtlArray{T,N} = Union{MtlArray{T,N}, WrappedArray{T,N,MtlArray,MtlArray{T,N}}}
const WrappedMtlVector{T} = WrappedMtlArray{T,1}
const WrappedMtlMatrix{T} = WrappedMtlArray{T,2}
const WrappedMtlVecOrMat{T} = Union{WrappedMtlVector{T}, WrappedMtlMatrix{T}}
## conversions
Base.convert(::Type{T}, x::T) where T <: MtlArray = x
## interop with C libraries
Base.unsafe_convert(::Type{<:Ptr}, x::MtlArray) =
throw(ArgumentError("cannot take the host address of a $(typeof(x))"))
Base.unsafe_convert(t::Type{MTL.MTLBuffer}, x::MtlArray) = x.buffer
## interop with CPU arrays
Base.unsafe_wrap(t::Type{<:Array}, arr::MtlArray, dims; own=false) =
unsafe_wrap(t, arr.buffer, dims; own=own)
# We don't convert isbits types in `adapt`, since they are already
# considered GPU-compatible.
Adapt.adapt_storage(::Type{MtlArray}, xs::AbstractArray) =
isbits(xs) ? xs : convert(MtlArray, xs)
# if an element type is specified, convert to it
Adapt.adapt_storage(::Type{<:MtlArray{T}}, xs::AbstractArray) where {T} =
isbits(xs) ? xs : convert(MtlArray{T}, xs)
Adapt.adapt_storage(::Type{Array}, xs::MtlArray) = convert(Array, xs)
Base.collect(x::MtlArray{T,N}) where {T,N} = copyto!(Array{T,N}(undef, size(x)), x)
## memory copying
function Base.copyto!(dest::MtlArray{T}, doffs::Integer, src::Array{T}, soffs::Integer,
n::Integer) where T
(n==0 || sizeof(T) == 0) && return dest
@boundscheck checkbounds(dest, doffs)
@boundscheck checkbounds(dest, doffs+n-1)
@boundscheck checkbounds(src, soffs)
@boundscheck checkbounds(src, soffs+n-1)
unsafe_copyto!(device(dest), dest, doffs, src, soffs, n)
return dest
end
Base.copyto!(dest::MtlArray{T}, src::Array{T}) where {T} =
copyto!(dest, 1, src, 1, length(src))
function Base.copyto!(dest::Array{T}, doffs::Integer, src::MtlArray{T}, soffs::Integer,
n::Integer) where T
(n==0 || sizeof(T) == 0) && return dest
@boundscheck checkbounds(dest, doffs)
@boundscheck checkbounds(dest, doffs+n-1)
@boundscheck checkbounds(src, soffs)
@boundscheck checkbounds(src, soffs+n-1)
unsafe_copyto!(device(src), dest, doffs, src, soffs, n)
return dest
end
Base.copyto!(dest::Array{T}, src::MtlArray{T}) where {T} =
copyto!(dest, 1, src, 1, length(src))
function Base.copyto!(dest::MtlArray{T}, doffs::Integer, src::MtlArray{T}, soffs::Integer,
n::Integer) where T
(n==0 || sizeof(T) == 0) && return dest
@boundscheck checkbounds(dest, doffs)
@boundscheck checkbounds(dest, doffs+n-1)
@boundscheck checkbounds(src, soffs)
@boundscheck checkbounds(src, soffs+n-1)
# TODO: which device to use here?
if device(dest) == device(src)
unsafe_copyto!(device(dest), dest, doffs, src, soffs, n)
else
error("Copy between different devices not implemented")
end
return dest
end
Base.copyto!(dest::MtlArray{T}, src::MtlArray{T}) where {T} =
copyto!(dest, 1, src, 1, length(src))
function Base.unsafe_copyto!(dev::MtlDevice, dest::MtlArray{T}, doffs, src::Array{T}, soffs, n) where T
# these copies are implemented using pure memcpy's, not API calls, so aren't ordered.
synchronize()
GC.@preserve src dest unsafe_copyto!(dev, pointer(dest, doffs), pointer(src, soffs), n)
if Base.isbitsunion(T)
# copy selector bytes
error("Not implemented")
end
return dest
end
function Base.unsafe_copyto!(dev::MtlDevice, dest::Array{T}, doffs, src::MtlArray{T}, soffs, n) where T
# these copies are implemented using pure memcpy's, not API calls, so aren't ordered.
synchronize()
GC.@preserve src dest unsafe_copyto!(dev, pointer(dest, doffs), pointer(src, soffs), n)
if Base.isbitsunion(T)
# copy selector bytes
error("Not implemented")
end
return dest
end
function Base.unsafe_copyto!(dev::MtlDevice, dest::MtlArray{T}, doffs, src::MtlArray{T}, soffs, n) where T
# these copies are implemented using pure memcpy's, not API calls, so aren't ordered.
synchronize()
GC.@preserve src dest unsafe_copyto!(dev, pointer(dest, doffs), pointer(src, soffs), n)
if Base.isbitsunion(T)
# copy selector bytes
error("Not implemented")
end
return dest
end
## utilities
zeros(T::Type, dims...) = fill!(MtlArray{T}(undef, dims...), 0)
ones(T::Type, dims...) = fill!(MtlArray{T}(undef, dims...), 1)
zeros(dims...) = zeros(Float32, dims...)
ones(dims...) = Mtls(Float32, dims...)
fill(v, dims...) = fill!(MtlArray{typeof(v)}(undef, dims...), v)
fill(v, dims::Dims) = fill!(MtlArray{typeof(v)}(undef, dims...), v)
# optimized implementation of `fill!` for types that are directly supported by fillbuffer
function Base.fill!(A::MtlArray{T}, val) where T <: Union{UInt8,Int8}
B = convert(T, val)
unsafe_fill!(device(A), pointer(A), B, length(A))
A
end
## views
device(a::SubArray) = device(parent(a))
# we don't really want an array, so don't call `adapt(Array, ...)`,
# but just want MtlArray indices to get downloaded back to the CPU.
# this makes sure we preserve array-like containers, like Base.Slice.
struct BackToCPU end
Adapt.adapt_storage(::BackToCPU, xs::MtlArray) = convert(Array, xs)
@inline function Base.view(A::MtlArray, I::Vararg{Any,N}) where {N}
J = to_indices(A, I)
@boundscheck begin
# Base's boundscheck accesses the indices, so make sure they reside on the CPU.
# this is expensive, but it's a bounds check after all.
J_cpu = map(j->adapt(BackToCPU(), j), J)
checkbounds(A, J_cpu...)
end
J_gpu = map(j->adapt(MtlArray, j), J)
Base.unsafe_view(Base._maybe_reshape_parent(A, Base.index_ndims(J_gpu...)), J_gpu...)
end
# pointer conversions
## contiguous
function Base.unsafe_convert(::Type{MTL.MTLBuffer}, V::SubArray{T,N,P,<:Tuple{Vararg{Base.RangeIndex}}}) where {T,N,P}
return Base.unsafe_convert(MTL.MTLBuffer, parent(V)) +
Base._memory_offset(V.parent, map(first, V.indices)...)
end
## reshaped
function Base.unsafe_convert(::Type{MTL.MTLBuffer}, V::SubArray{T,N,P,<:Tuple{Vararg{Union{Base.RangeIndex,Base.ReshapedUnitRange}}}}) where {T,N,P}
return Base.unsafe_convert(MTL.MTLBuffer, parent(V)) +
(Base.first_index(V)-1)*sizeof(T)
end
## PermutedDimsArray
device(a::Base.PermutedDimsArray) = device(parent(a))
Base.unsafe_convert(::Type{MTL.MTLBuffer}, A::PermutedDimsArray) =
Base.unsafe_convert(MTL.MTLBuffer, parent(A))
## reshape
function Base.reshape(a::MtlArray{T,M}, dims::NTuple{N,Int}) where {T,N,M}
if prod(dims) != length(a)
throw(DimensionMismatch("new dimensions $(dims) must be consistent with array size $(size(a))"))
end
if N == M && dims == size(a)
return a
end
_derived_array(T, N, a, dims)
end
# create a derived array (reinterpreted or reshaped) that's still a MtlArray
@inline function _derived_array(::Type{T}, N::Int, a::MtlArray, osize::Dims) where {T}
offset = (a.offset * Base.elsize(a)) ÷ sizeof(T)
MtlArray{T,N}(a.buffer, osize; a.maxsize, offset)
end
## reinterpret
device(a::Base.ReinterpretArray) = device(parent(a))
Base.unsafe_convert(::Type{MTL.MTLBuffer}, a::Base.ReinterpretArray{T,N,S} where N) where {T,S} =
MTL.MTLBuffer(Base.unsafe_convert(ZePtr{S}, parent(a)))
## unsafe_wrap
function Base.unsafe_wrap(t::Type{<:Array{T}}, buf::MtlBuffer, dims; own=false) where T
ptr = convert(Ptr{T}, contents(buf))
return unsafe_wrap(t, ptr, dims; own)
end
function Base.unsafe_wrap(t::Type{<:Array{T}}, ptr::MtlPointer{T}, dims; own=false) where T
return unsafe_wrap(t, contents(ptr), dims; own)
end