-
Notifications
You must be signed in to change notification settings - Fork 29
/
arrays.jl
412 lines (356 loc) · 16.7 KB
/
arrays.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
###############################
# CUDA Array and memory types #
###############################
# Abstract CUDA array types
abstract AbstractCudaArray{T,N}
typealias AbstractCudaVector{T} AbstractCudaArray{T,1}
typealias AbstractCudaMatrix{T} AbstractCudaArray{T,2}
# copy method for AbstractCudaArray
copy(a::AbstractCudaArray; stream=null_stream) = copy!(similar(a),a;stream=stream)
# Set the following to true to store a backtrace
# at allocation time, to identify where each array comes from
const debugMemory = false
# Contiguous arrays on the GPU
if !debugMemory
type CudaArray{T,N} <: AbstractCudaArray{T,N}
ptr::CudaPtr{T}
dims::NTuple{N,Int}
dev::Int
end
else
type CudaArray{T,N} <: AbstractCudaArray{T,N}
ptr::CudaPtr{T}
dims::NTuple{N,Int}
dev::Int
bt
function CudaArray(ptr::CudaPtr{T}, dims::NTuple{N,Int}, dev::Integer)
new(ptr, dims, dev, backtrace())
end
end
end
# Vector and matrix aliases
typealias CudaVector{T} CudaArray{T,1}
typealias CudaMatrix{T} CudaArray{T,2}
typealias CudaVecOrMat{T} Union{CudaVector{T}, CudaMatrix{T}}
# Layout-optimized 1-, 2-, and 3-dimensional arrays
if !debugMemory
type CudaPitchedArray{T,N} <: AbstractCudaArray{T,N}
ptr::rt.cudaPitchedPtr
dims::NTuple{N,Int}
dev::Int
end
else
type CudaPitchedArray{T,N} <: AbstractCudaArray{T,N}
ptr::rt.cudaPitchedPtr
dims::NTuple{N,Int}
dev::Int
bt
function CudaPitchedArray(ptr::rt.cudaPitchedPtr, dims::NTuple{N,Int}, dev::Integer)
new(ptr, dims, dev, backtrace())
end
end
end
# Pinned memory on the host
type HostArray{T,N} <: AbstractArray{T,N}
ptr::Ptr{Void}
data::Array{T,N}
end
typealias CdArray{T} Union{DenseArray{T},HostArray{T},AbstractCudaArray{T}}
typealias ContiguousArray{T} Union{Array{T},HostArray{T},CudaArray{T}}
###################
# Implementations #
###################
length(g::AbstractCudaArray) = prod(g.dims)
size(g::AbstractCudaArray) = g.dims
function size{T,N}(g::AbstractCudaArray{T,N}, d::Integer)
d >= 1 ? (d <= N ? g.dims[d] : 1) : error("Invalid index of dimension.")
end
ndims{T,N}(g::AbstractCudaArray{T,N}) = N
eltype{T,N}(g::AbstractCudaArray{T,N}) = T
device(A::AbstractCudaArray) = A.dev
device(A::AbstractArray) = -1 # for host
pointer(g::AbstractCudaArray) = g.ptr
to_host{T}(g::AbstractCudaArray{T}) = copy!(Array(T, size(g)), g)
summary(g::AbstractCudaArray) = string(g)
######################################
# CudaArray: contiguous array on GPU #
######################################
CudaArray(T::Type, dims::Integer...) = CudaArray(T, dims)
function CudaArray(T::Type, dims::Dims)
n = prod(dims)
p = malloc(T, n)
CudaArray{T,length(dims)}(p, dims, device())
end
CudaArray{T,N}(a::Array{T,N}; stream=null_stream) = copy!(CudaArray(T, size(a)), a; stream=stream)
CudaArray{T,N}(a::HostArray{T,N}; stream=null_stream) = copy!(CudaArray(T, size(a)), a; stream=stream)
CudaArray{T,N}(a::AbstractArray{T,N}) = CudaArray(convert(Array{T,N}, a))
stride(g::CudaArray, dim::Integer) = prod(size(g)[1:dim-1])
similar(g::CudaArray, T, dims::Int...) = similar(g,T,dims)
similar(g::CudaArray, T, dims::Dims) = CudaArray(T, dims)
similar(g::CudaArray) = CudaArray(eltype(g), size(g))
similar(g::CudaArray, T) = CudaArray(T, size(g))
similar(g::CudaArray, dims::Dims) = CudaArray(eltype(g), dims)
unsafe_convert{T}(::Type{Ptr{T}}, g::CudaArray) = unsafe_convert(Ptr{T}, pointer(g))
# convert{T,N}(::Type{CudaArray{T,N}}, g::CudaArray{T,N}) = g
# convert{R,S,N}(::Type{CudaArray{R,N}}, g::CudaArray{S,N}) = CudaArray{R,N}(g.ptr, g.dims, g.dev)
reinterpret{T}(::Type{T}, g::CudaArray{T}) = g
function reinterpret{R,S}(::Type{R}, g::CudaArray{S})
if sizeof(R) != sizeof(S)
error("result shape not specified")
end
CudaArray{R,ndims(g)}(g.ptr, g.dims, g.dev)
end
function reinterpret{R,S}(::Type{R}, g::CudaArray{S}, dims::Dims)
lenR = div(sizeof(S)*length(g),sizeof(R))
if prod(dims) != lenR
throw(DimensionMismatch("New dimensions $dims must be consistent with array of length $lenR"))
end
CudaArray{R,length(dims)}(g.ptr, dims, g.dev)
end
free(g::CudaArray) = free(pointer(g))
if debugMemory
function show(io::IO, g::CudaArray)
println(io, typeof(g))
println(io, " ptr: ", g.ptr)
println(io, " dims: ", g.dims)
println(io, " dev: ", g.dev)
print(io, "Allocated from:")
Base.show_backtrace(io, g.bt)
end
end
function copy!{T}(dst::ContiguousArray{T}, src::ContiguousArray{T}; stream=null_stream)
if length(dst) != length(src)
throw(ArgumentError("Inconsistent array length."))
end
nbytes = length(src) * sizeof(T)
rt.cudaMemcpyAsync(dst, src, nbytes, cudamemcpykind(dst, src), stream)
return dst
end
function fill!{T}(X::CudaArray{T}, val; stream=null_stream)
valT = convert(T, val)
func = ptxdict[(device(), "fill_contiguous", T)]
nsm = attribute(device(), rt.cudaDevAttrMultiProcessorCount)
mul = min(32, ceil(Int, length(X)/(256*nsm)))
launch(func, mul*nsm, 256, (X, length(X), valT); stream=stream)
X
end
#############################################################################
# CudaPitchedArray: layout-optimized 1-, 2- and 3-dimensional arrays on GPU #
#############################################################################
CudaPitchedArray(T::Type, dims::Integer...) = CudaPitchedArray(T, dims)
function CudaPitchedArray(T::Type, dims::Dims)
nd = length(dims)
1 <= nd <= 3 || error("Supports only dimensions 1, 2, or 3")
p = Array(rt.cudaPitchedPtr, 1)
ext = CudaExtent(T, dims)
rt.cudaMalloc3D(p, ext)
pp = p[1]
pp = rt.cudaPitchedPtr(pp, dims[1]) # correct the xsize
cptr = pp.ptr
dev = device()
cuda_ptrs[WeakRef(cptr)] = dev
g = CudaPitchedArray{T,length(dims)}(pp, dims, dev)
finalizer(g, free)
g
end
CudaPitchedArray{T,N}(a::Array{T,N}) = copy!(CudaPitchedArray(T, size(a)), a)
CudaPitchedArray{T,N}(a::SharedArray{T,N}) = CudaPitchedArray(sdata(a))
CudaPitchedArray{T,N}(a::AbstractArray{T,N}) = CudaPitchedArray(convert(Array{T,N}, a))
stride(g::CudaPitchedArray, dim::Integer) = dim == 1 ? 1 :
dim == 2 ? pitchel(g) :
dim == 3 ? pitchel(g)*size(g,2) : pitchel(g)*size(g,2)*size(g,3)
similar(g::CudaPitchedArray, T, dims::Dims) = CudaPitchedArray(T, dims)
similar(g::CudaPitchedArray) = CudaPitchedArray(eltype(g), size(g))
similar(g::CudaPitchedArray, T) = CudaPitchedArray(T, size(g))
similar(g::CudaPitchedArray, dims::Dims) = CudaPitchedArray(eltype(g), dims)
unsafe_convert(::Type{rt.cudaPitchedPtr}, g::CudaPitchedArray) = pointer(g)
unsafe_convert{T}(::Type{Ptr{T}}, g::CudaPitchedArray{T}) = unsafe_convert(Ptr{T}, rawpointer(g))
# convert{T,N}(::Type{CudaPitchedArray{T,N}}, g::CudaPitchedArray{T,N}) = g
# convert{R,S,N}(::Type{CudaPitchedArray{R,N}}, g::CudaPitchedArray{S,N}) = CudaPitchedArray{R,N}(g.ptr, g.dims, g.dev)
reinterpret{T}(::Type{T}, g::CudaPitchedArray{T}) = g
function reinterpret{R,S}(::Type{R}, g::CudaPitchedArray{S})
if sizeof(R) != sizeof(S)
error("result shape not specified")
end
CudaPitchedArray{R,ndims(g)}(g.ptr, g.dims, g.dev)
end
function reinterpret{R,S}(::Type{R}, g::CudaPitchedArray{S}, dims::Dims)
if length(g) == 0 && prod(dims) == 0
return CudaPitchedArray{R,length(dims)}(g.ptr, dims, g.dev)
end
# Because of the pitch, we don't need the first dimension to agree exactly
if dims[1]*sizeof(R) > size(g,1)*sizeof(S)
throw(DimensionMismatch("First dimension $(dims[1]) is too large for $(size(g,1)*sizeof(S)) bytes"))
end
# Require agreement for dimensions 2 and 3
nd = length(dims)
lg, ld = 1, 1
for d = 2:nd
lg *= size(g,d)
ld *= dims[d]
end
for d = nd+1:ndims(g)
lg *= size(g,d)
end
if ld != lg
throw(DimensionMismatch("New dimensions $dims must be consistent---in all dimensions except the first---with array of dimensions with product $lg"))
end
pptr = g.ptr
newptr = rt.cudaPitchedPtr(pptr.ptr, pptr.pitch, dims[1], nd>1?dims[2]:1)
CudaPitchedArray{R,length(dims)}(newptr, dims, g.dev)
end
function free(g::CudaPitchedArray)
p = g.ptr.ptr
if p != C_NULL && haskey(cuda_ptrs, p)
delete!(cuda_ptrs, p)
rt.cudaFree(p)
g.ptr = rt.cudaPitchedPtr()
end
end
if debugMemory
function show(io::IO, g::CudaPitchedArray)
println(io, typeof(g))
println(io, " ptr: ", g.ptr)
println(io, " dims: ", g.dims)
println(io, " dev: ", g.dev)
print(io, "Allocated from:")
Base.show_backtrace(io, g.bt)
end
end
pitchedptr(g::CudaPitchedArray) = pointer(g)
function pitchedptr(a::ContiguousArray)
1 <= ndims(a) <= 3 || error("Supports only dimensions 1, 2, or 3")
rt.cudaPitchedPtr(pointer(a), size(a,1)*sizeof(eltype(a)), size(a,1), size(a,2))
end
function pitchedptr{T}(p::Ptr{T}, sz)
1 <= length(sz) <= 3 || error("Supports only dimensions 1, 2, or 3")
rt.cudaPitchedPtr(p, sz[1]*sizeof(T), sz[1], length(sz) > 1 ? sz[2] : 1)
end
pitchbytes(g::CudaArray) = size(g,1)*sizeof(eltype(g))
pitchbytes(g::CudaPitchedArray) = g.ptr.pitch
pitchel(g::AbstractCudaArray) = div(pitchbytes(g),sizeof(eltype(g)))
rawpointer(g::CudaArray) = g.ptr
rawpointer(g::CudaPitchedArray) = g.ptr.ptr
CudaExtent{T}(a::AbstractCudaArray{T}) = CudaExtent(pitchbytes(a), size(a,2), size(a,3))
copy!{T}(dst::AbstractCudaArray{T}, src::AbstractCudaArray{T}; kwargs...) = cudacopy!(dst, src; kwargs...)
copy!{T}(dst::CdArray{T}, src::AbstractCudaArray{T}; kwargs...) = cudacopy!(dst, src; kwargs...)
copy!{T}(dst::AbstractCudaArray{T}, src::CdArray{T}; kwargs...) = cudacopy!(dst, src; kwargs...)
function cudacopy!{T}(dst::CdArray{T}, src::CdArray{T}; kwargs...)
if size(dst) != size(src)
throw(DimensionMismatch("Size $(size(dst)) of dst is not equal to $(size(src)) of src"))
end
copy!(dst, map(d->1:d, size(dst)), src, map(d->1:d, size(src)); kwargs...)
end
copy!{T}(dst::AbstractCudaArray{T}, dstI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}, src::AbstractCudaArray{T}, srcI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}; stream=null_stream) =
cudacopy!(dst, dstI, src, srcI, stream=stream)
copy!{T}(dst::CdArray{T}, dstI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}, src::AbstractCudaArray{T}, srcI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}; stream=null_stream) =
cudacopy!(dst, dstI, src, srcI, stream=stream)
copy!{T}(dst::AbstractCudaArray{T}, dstI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}, src::CdArray{T}, srcI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}; stream=null_stream) =
cudacopy!(dst, dstI, src, srcI, stream=stream)
function cudacopy!{T}(dst::CdArray{T}, dstI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}, src::CdArray{T}, srcI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}; stream=null_stream)
nd = length(srcI)
if length(dstI) != nd
throw(DimensionMismatch("Destination of $(length(dstI)) dimensions differs from dimensionality $nd of src"))
end
for i = 1:nd
length(dstI[i]) == length(srcI[i]) || throw(DimensionMismatch("In dimension $i, destination range of length $(length(dstI[i])) but source range has length $(length(srcI[i]))"))
first(dstI[i]) >= 1 && last(dstI[i]) <= size(dst, i) || throw(DimensionMismatch("In dimension $i, destination range of $(dstI[i]) is not within array dimension of $(size(dst,i))"))
first(srcI[i]) >= 1 && last(srcI[i]) <= size(src, i) || throw(DimensionMismatch("In dimension $i, source range of $(srcI[i]) is not within array dimension of $(size(src,i))"))
end
checkstrides_pitched(dst)
checkstrides_pitched(src)
ext = CudaExtent(eltype(src),map(length, srcI))
srcpos = CudaPos(eltype(src),map(first, srcI))
dstpos = CudaPos(eltype(dst),map(first, dstI))
params = [rt.cudaMemcpy3DParms(C_NULL, srcpos, pitchedptr(src), C_NULL, dstpos, pitchedptr(dst), ext, cudamemcpykind(dst, src))]
rt.cudaMemcpy3DAsync(params, stream)
dst
end
function copy!{T}(dst::CdArray{T}, src::AbstractCudaArray{T}, srcI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}; stream=null_stream)
nd = length(srcI)
for i = 1:nd
first(srcI[i]) >= 1 && last(srcI[i]) <= size(src, i) || throw(DimensionMismatch("In dimension $i, source range of $(srcI[i]) is not within array dimension of $(size(src,i))"))
length(srcI[i]) == size(dst, i) || throw(DimensionMismatch("In dimension $i, the size $(size(dst, i)) of the destination is inconsistent with the size $(length(srcI[i])) being copied"))
end
checkstrides_pitched(dst)
sz = map(length, srcI)
ext = CudaExtent(eltype(src),sz)
srcpos = CudaPos(eltype(src),map(first, srcI))
dstpos = CudaPos(0, 0, 0)
pdst = pitchedptr(dst)
params = [rt.cudaMemcpy3DParms(C_NULL, srcpos, pitchedptr(src), C_NULL, dstpos, pdst, ext, rt.cudaMemcpyDeviceToHost)]
rt.cudaMemcpy3DAsync(params, stream)
dst
end
function cudaget!{T}(dst::CdArray{T}, src::CdArray{T}, srcI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}, default; kwargs...)
nd = length(srcI)
if ndims(dst) != nd
throw(DimensionMismatch("Destination of $(ndims(dst)) dimensions differs from dimensionality $nd of src"))
end
for i = 1:nd
size(dst,i) == length(srcI[i]) || throw(DimensionMismatch("In dimension $i, destination has dimension $(size(dst,i)) but source range has length $(length(srcI[i]))"))
end
srcII = ntuple(i->isa(srcI[i], Int) ? (srcI[i]:srcI[i]) : srcI[i], nd)
dstIc, srcIc = Base.indcopy(size(src), srcII)
if srcIc != srcII
fill!(dst, default; kwargs...)
copy!(dst, dstIc, src, srcIc; kwargs...)
else
copy!(dst, dstIc, src, srcIc; kwargs...)
end
dst
end
get!{T}(dst::AbstractCudaArray{T}, src::AbstractCudaArray{T}, srcI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}, default; kwargs...) = cudaget!(dst, src, srcI, default; kwargs...)
get!{T}(dst::AbstractCudaArray{T}, src::CdArray{T}, srcI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}, default; kwargs...) = cudaget!(dst, src, srcI, default; kwargs...)
get!{T}(dst::CdArray{T}, src::AbstractCudaArray{T}, srcI::Tuple{Vararg{Union{Int,UnitRange{Int}}}}, default; kwargs...) = cudaget!(dst, src, srcI, default; kwargs...)
function fill!{T}(X::CudaPitchedArray{T}, val; stream=null_stream)
valT = convert(T, val)
func = ptxdict[(device(), "fill_pitched", T)]
nsm = attribute(device(), rt.cudaDevAttrMultiProcessorCount)
mul = min(32, ceil(Int, length(X)/(256*nsm)))
blockspergrid, threadsperblock = ndims(X) == 1 ? (mul*nsm, 256) : (mul*nsm, (16,16))
launch(func, blockspergrid, threadsperblock, (X, size(X,1), size(X,2), size(X,3), pitchel(X), valT); stream=stream)
X
end
#####################################
# HostArray (pinned memory on host) #
#####################################
HostArray{T}(::Type{T}, sz...; flags::Integer=rt.cudaHostAllocDefault) = HostArray(T, sz, flags=flags)
function HostArray{T}(::Type{T}, sz::Dims; flags::Integer=rt.cudaHostAllocDefault)
p = Ptr{Void}[C_NULL]
rt.cudaHostAlloc(p, prod(sz)*sizeof(T), flags)
ptr = p[1]
data = pointer_to_array(unsafe_convert(Ptr{T}, ptr), sz, false)
ha = HostArray(ptr, data)
finalizer(ha, free)
cuda_ptrs[WeakRef(ptr)] = device()
ha
end
function free(ha::HostArray)
if ha.ptr != C_NULL && haskey(cuda_ptrs, ha.ptr)
rt.cudaFreeHost(ha.ptr)
ha.ptr = C_NULL
ha.data = Array(eltype(ha), ntuple(d->0, ndims(ha)))
end
end
size(ha::HostArray) = size(ha.data)
size(ha::HostArray, d) = size(ha.data, d)
ndims{T,N}(ha::HostArray{T,N}) = N
eltype{T,N}(ha::HostArray{T,N}) = T
getindex(ha::HostArray, i::Real) = getindex(ha.data, i)
getindex(ha::HostArray, i::Union{Real,AbstractVector}) = getindex(ha.data, i)
getindex(ha::HostArray, i::Union{Real,AbstractVector}, j::Union{Real,AbstractVector}) = getindex(ha.data, i, j)
getindex(ha::HostArray, i::Union{Real,AbstractVector}...) = getindex(ha.data, i...)
setindex!(ha::HostArray, val, i::Real) = setindex!(ha.data, val, i)
setindex!(ha::HostArray, val, i::Union{Real,AbstractVector}) = setindex!(ha.data, val, i)
setindex!(ha::HostArray, val, i::Union{Real,AbstractVector}, j::Union{Real,AbstractVector}) = setindex!(ha.data, val, i, j)
setindex!(ha::HostArray, val, i...) = setindex!(ha.data, val, i...)
pointer(ha::HostArray) = pointer(ha.data)
rawpointer(ha::HostArray) = pointer(ha)
pitch(ha::HostArray) = size(ha,1)*sizeof(eltype(ha))
unsafe_convert{T}(::Type{Ptr{Void}}, ha::HostArray{T}) = ha.ptr
fill!(ha::HostArray, val) = fill!(ha.data, val)
function checkstrides_pitched(A)
stride(A, 1) == 1 || error("A must have a stride of 1 along the first dimension")
stride(A, 3) == stride(A, 2) * size(A, 2) || error("A must be contiguous for dimension 2")
end