forked from JuliaGPU/CUDA.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.jl
361 lines (301 loc) · 10.4 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
using LinearAlgebra
import Adapt
@testset "constructors" begin
xs = CuArray{Int}(undef, 2, 3)
@test collect(CuArray([1 2; 3 4])) == [1 2; 3 4]
@test collect(cu[1, 2, 3]) == [1, 2, 3]
@test collect(cu([1, 2, 3])) == [1, 2, 3]
@test testf(vec, rand(5,3))
@test cu(1:3) === 1:3
@test Base.elsize(xs) == sizeof(Int)
@test CuArray{Int, 2}(xs) === xs
# test aggressive conversion to Float32, but only for floats, and only with `cu`
@test cu([1]) isa AbstractArray{Int}
@test cu(Float64[1]) isa AbstractArray{Float32}
@test cu(ComplexF64[1+1im]) isa AbstractArray{ComplexF32}
@test Adapt.adapt(CuArray, Float64[1]) isa AbstractArray{Float64}
@test Adapt.adapt(CuArray, ComplexF64[1]) isa AbstractArray{ComplexF64}
@test Adapt.adapt(CuArray{Float16}, Float64[1]) isa AbstractArray{Float16}
@test_throws ArgumentError Base.unsafe_convert(Ptr{Int}, xs)
@test_throws ArgumentError Base.unsafe_convert(Ptr{Float32}, xs)
# unsafe_wrap
@test Base.unsafe_wrap(CuArray, CU_NULL, 1; own=false).state == CUDA.ARRAY_UNMANAGED
@test Base.unsafe_wrap(CuArray, CU_NULL, 2) == CuArray{Nothing,1}(CU_NULL, (2,))
@test Base.unsafe_wrap(CuArray{Nothing}, CU_NULL, 2) == CuArray{Nothing,1}(CU_NULL, (2,))
@test Base.unsafe_wrap(CuArray{Nothing,1}, CU_NULL, 2) == CuArray{Nothing,1}(CU_NULL, (2,))
@test Base.unsafe_wrap(CuArray, CU_NULL, (1,2)) == CuArray{Nothing,2}(CU_NULL, (1,2))
@test Base.unsafe_wrap(CuArray{Nothing}, CU_NULL, (1,2)) == CuArray{Nothing,2}(CU_NULL, (1,2))
@test Base.unsafe_wrap(CuArray{Nothing,2}, CU_NULL, (1,2)) == CuArray{Nothing,2}(CU_NULL, (1,2))
@test collect(CUDA.zeros(2, 2)) == zeros(Float32, 2, 2)
@test collect(CUDA.ones(2, 2)) == ones(Float32, 2, 2)
@test collect(CUDA.fill(0, 2, 2)) == zeros(Float32, 2, 2)
@test collect(CUDA.fill(1, 2, 2)) == ones(Float32, 2, 2)
end
@testset "adapt" begin
A = rand(Float32, 3, 3)
dA = CuArray(A)
@test Adapt.adapt(Array, dA) == A
@test Adapt.adapt(CuArray, A) isa CuArray
@test Array(Adapt.adapt(CuArray, A)) == A
end
@testset "view" begin
@test testf(rand(5)) do x
y = x[2:4]
y .= 1
x
end
@test testf(rand(5)) do x
y = view(x, 2:4)
y .= 1
x
end
@test testf(x->view(x, :, 1:4, 3), rand(Float32, 5, 4, 3))
let x = CUDA.rand(Float32, 5, 4, 3)
@test_throws BoundsError view(x, :, :, 1:10)
end
# bug in parentindices conversion
let x = CuArray{Int}(undef, 1, 1)
x[1,:] .= 42
@test Array(x)[1,1] == 42
end
# bug in conversion of indices (#506)
show(devnull, cu(view(ones(1), [1])))
# performance loss due to Array indices
let x = CuArray{Int}(undef, 1)
i = [1]
y = view(x, i)
@test parent(y) isa CuArray
@test parentindices(y) isa Tuple{CuArray}
end
@testset "GPU array source" begin
a = rand(3)
i = rand(1:3, 2)
@test testf(view, a, i)
@test testf(view, a, view(i, 2:2))
end
@testset "CPU array source" begin
a = rand(3)
i = rand(1:3, 2)
@test testf(view, a, i)
@test testf(view, a, view(i, 2:2))
end
end
@testset "reshape" begin
A = [1 2 3 4
5 6 7 8]
gA = reshape(CuArray(A),1,8)
_A = reshape(A,1,8)
_gA = Array(gA)
@test all(_A .== _gA)
A = [1,2,3,4]
gA = reshape(CuArray(A),4)
end
@testset "Dense derivatives" begin
a = CUDA.rand(Int64, 5, 4, 3)
@test a isa CuArray
# Contiguous views should return new CuArray
@test view(a, :, 1, 2) isa CuVector{Int64}
@test view(a, 1:4, 1, 2) isa CuVector{Int64}
@test view(a, :, 1:4, 3) isa CuMatrix{Int64}
@test view(a, :, :, 1) isa CuMatrix{Int64}
@test view(a, :, :, :) isa CuArray{Int64,3}
@test view(a, :) isa CuVector{Int64}
@test view(a, 1:3) isa CuVector{Int64}
@test view(a, 1, 1, 1) isa CuArray{Int64}
# Non-contiguous views should fall back to base's SubArray
@test view(a, 1:3, 1:3, 3) isa SubArray
@test view(a, 1, :, 3) isa SubArray
@test view(a, 1, 1:4, 3) isa SubArray
@test view(a, :, 1, 1:3) isa SubArray
@test view(a, :, 1:2:4, 1) isa SubArray
@test view(a, 1:2:5, 1, 1) isa SubArray
b = reshape(a, (6,10))
@test b isa CuArray
@test b isa StridedCuArray
@test view(b, :, :, 1) isa DenseCuArray
b = reshape(a, :)
@test b isa CuArray
b = reinterpret(Float64, a)
@test b isa CuArray
@test b isa StridedCuArray
@test view(b, :, :, 1) isa DenseCuArray
end
@testset "StridedArray" begin
a = CUDA.rand(Int64, 2,2,2)
@test a isa StridedCuArray
@test view(a, :, :, 1) isa StridedCuArray
@test view(a, :, 1, :) isa StridedCuArray
@test view(a, 1, :, :) isa StridedCuArray
b = reshape(a, (2,4))
@test b isa CuArray
@test b isa StridedCuArray
@test view(b, :, 1, :) isa StridedCuArray
b = reinterpret(Float64, a)
@test b isa CuArray
@test b isa StridedCuArray
@test view(b, :, 1, :) isa StridedCuArray
end
@testset "accumulate" begin
for n in (0, 1, 2, 3, 10, 10_000, 16384, 16384+1) # small, large, odd & even, pow2 and not
@test testf(x->accumulate(+, x), rand(n))
end
# multidimensional
for (sizes, dims) in ((2,) => 2,
(3,4,5) => 2,
(1, 70, 50, 20) => 3)
@test testf(x->accumulate(+, x; dims=dims), rand(Int, sizes))
end
# using initializer
for (sizes, dims) in ((2,) => 2,
(3,4,5) => 2,
(1, 70, 50, 20) => 3)
@test testf(x->accumulate(+, x; dims=dims, init=100.), rand(Int, sizes))
end
# in place
@test testf(x->(accumulate!(+, x, copy(x)); x), rand(2))
# specialized
@test testf(cumsum, rand(2))
@test testf(cumprod, rand(2))
end
@testset "logical indexing" begin
@test CuArray{Int}(undef, 2)[CuArray{Bool}(undef, 2)] isa CuArray
@test CuArray{Int}(undef, 2, 2)[CuArray{Bool}(undef, 2, 2)] isa CuArray
@test CuArray{Int}(undef, 2, 2, 2)[CuArray{Bool}(undef, 2, 2, 2)] isa CuArray
@test CuArray{Int}(undef, 2)[Array{Bool}(undef, 2)] isa CuArray
@test CuArray{Int}(undef, 2, 2)[Array{Bool}(undef, 2, 2)] isa CuArray
@test CuArray{Int}(undef, 2, 2, 2)[Array{Bool}(undef, 2, 2, 2)] isa CuArray
@test testf((x,y)->x[y], rand(2), rand(Bool, 2))
@test testf((x,y)->x[y], rand(2, 2), rand(Bool, 2, 2))
@test testf((x,y)->x[y], rand(2, 2, 2), rand(Bool, 2, 2, 2))
@test testf(x -> x[x .> 0.5], rand(2))
@test testf(x -> x[x .> 0.5], rand(2,2))
@test testf(x -> x[x .> 0.5], rand(2,2,2))
@test testf(x -> filter(y->y .> 0.5, x), rand(2))
@test testf(x -> filter(y->y .> 0.5, x), rand(2,2))
@test testf(x -> filter(y->y .> 0.5, x), rand(2,2,2))
end
@testset "reverse" begin
# 1-d out-of-place
@test testf(x->reverse(x), rand(1000))
@test testf(x->reverse(x, 10), rand(1000))
@test testf(x->reverse(x, 10, 90), rand(1000))
# 1-d in-place
@test testf(x->reverse!(x), rand(1000))
@test testf(x->reverse!(x, 10), rand(1000))
@test testf(x->reverse!(x, 10, 90), rand(1000))
# n-d out-of-place
for shape in ([1, 2, 4, 3], [4, 2], [5], [2^5, 2^5, 2^5]),
dim in 1:length(shape)
@test testf(x->reverse(x; dims=dim), rand(shape...))
cpu = rand(shape...)
gpu = CuArray(cpu)
reverse!(gpu; dims=dim)
@test Array(gpu) == reverse(cpu; dims=dim)
end
# wrapped array
@test testf(x->reverse(x), reshape(rand(2,2), 4))
end
@testset "findall" begin
# 1D
@test testf(x->findall(x), rand(Bool, 100))
@test testf(x->findall(y->y>0.5, x), rand(100))
# ND
let x = rand(Bool, 10, 10)
@test findall(x) == Array(findall(CuArray(x)))
end
let x = rand(10, 10)
@test findall(y->y>0.5, x) == Array(findall(y->y>0.5, CuArray(x)))
end
end
@testset "findfirst" begin
# 1D
@test testf(x->findfirst(x), rand(Bool, 100))
@test testf(x->findfirst(y->y>0.5, x), rand(100))
let x = fill(false, 10)
@test findfirst(x) == findfirst(CuArray(x))
end
# ND
let x = rand(Bool, 10, 10)
@test findfirst(x) == findfirst(CuArray(x))
end
let x = rand(10, 10)
@test findfirst(y->y>0.5, x) == findfirst(y->y>0.5, CuArray(x))
end
end
@testset "findmax & findmin" begin
let x = rand(Float32, 100)
@test findmax(x) == findmax(CuArray(x))
@test findmax(x; dims=1) == Array.(findmax(CuArray(x); dims=1))
end
let x = rand(Float32, 10, 10)
@test findmax(x) == findmax(CuArray(x))
@test findmax(x; dims=1) == Array.(findmax(CuArray(x); dims=1))
@test findmax(x; dims=2) == Array.(findmax(CuArray(x); dims=2))
end
let x = rand(Float32, 10, 10, 10)
@test findmax(x) == findmax(CuArray(x))
@test findmax(x; dims=1) == Array.(findmax(CuArray(x); dims=1))
@test findmax(x; dims=2) == Array.(findmax(CuArray(x); dims=2))
@test findmax(x; dims=3) == Array.(findmax(CuArray(x); dims=3))
end
let x = rand(Float32, 100)
@test findmin(x) == findmin(CuArray(x))
@test findmin(x; dims=1) == Array.(findmin(CuArray(x); dims=1))
end
let x = rand(Float32, 10, 10)
@test findmin(x) == findmin(CuArray(x))
@test findmin(x; dims=1) == Array.(findmin(CuArray(x); dims=1))
@test findmin(x; dims=2) == Array.(findmin(CuArray(x); dims=2))
end
let x = rand(Float32, 10, 10, 10)
@test findmin(x) == findmin(CuArray(x))
@test findmin(x; dims=1) == Array.(findmin(CuArray(x); dims=1))
@test findmin(x; dims=2) == Array.(findmin(CuArray(x); dims=2))
@test findmin(x; dims=3) == Array.(findmin(CuArray(x); dims=3))
end
end
@testset "argmax & argmin" begin
@test testf(argmax, rand(Int, 10))
@test testf(argmax, -rand(Int, 10))
@test testf(argmin, rand(Int, 10))
@test testf(argmin, -rand(Int, 10))
end
@testset "issue #543" begin
x = CUDA.rand(ComplexF32, 1)
@test x isa CuArray{Complex{Float32}}
y = exp.(x)
@test y isa CuArray{Complex{Float32}}
end
@testset "resizing" begin
a = CuArray([1,2,3])
resize!(a, 3)
@test length(a) == 3
@test Array(a) == [1,2,3]
resize!(a, 5)
@test length(a) == 5
@test Array(a)[1:3] == [1,2,3]
resize!(a, 2)
@test length(a) == 2
@test Array(a)[1:2] == [1,2]
GC.@preserve a begin
b = unsafe_wrap(CuArray{Int}, pointer(a), 2)
@test_throws ArgumentError resize!(b, 3)
end
end
@testset "aliasing" begin
x = CuArray([1,2])
y = view(x, 2:2)
@test Base.mightalias(x, x)
@test Base.mightalias(x, y)
z = view(x, 1:1)
@test Base.mightalias(x, z)
@test !Base.mightalias(y, z)
a = copy(y)::typeof(x)
@test !Base.mightalias(x, a)
a .= 3
@test Array(y) == [2]
b = Base.unaliascopy(y)::typeof(y)
@test !Base.mightalias(x, b)
b .= 3
@test Array(y) == [2]
end