forked from JuliaGPU/CUDA.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreading.jl
73 lines (64 loc) · 1.6 KB
/
threading.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
# FIXME: these tests regularly triggers illegal memory accesses
# after having moved to distributed test execution,
# regardless of the memory pool or system.
@testset "threaded execution" begin
function kernel(a, tid, id)
a[1] = tid
a[2] = id
return
end
test_lock = ReentrantLock()
Threads.@threads for id in 1:10
da = CuArray{Int}(undef, 2)
tid = Threads.threadid()
@cuda kernel(da, tid, id)
a = Array(da)
lock(test_lock) do
@test a == [tid, id]
end
end
end
@testset "threaded arrays" begin
test_lock = ReentrantLock()
Threads.@threads for i in 1:Threads.nthreads()*100
# uses libraries (rand, gemm) to test library handles
# allocates and uses unsafe_free to cover the allocator
da = CUDA.rand(64, 64)
db = CUDA.rand(64, 64)
yield()
dc = da * db
yield()
# @testset is not thread safe
a = Array(da)
b = Array(db)
c = Array(dc)
lock(test_lock) do
@test c ≈ a * b
end
yield()
CUDA.unsafe_free!(da)
CUDA.unsafe_free!(db)
end
end
@testset "threaded device usage" begin
test_lock = ReentrantLock()
Threads.@threads for i in 1:Threads.nthreads()*100
dev = rand(1:length(devices()))
device!(dev-1) do
da = CUDA.rand(64, 64)
db = CUDA.rand(64, 64)
yield()
dc = da * (db .* 2)
yield()
a = Array(da)
b = Array(db)
c = Array(dc)
lock(test_lock) do
@test c ≈ a * (b .* 2)
end
yield()
CUDA.unsafe_free!(da)
CUDA.unsafe_free!(db)
end
end
end