forked from JuliaGPU/CUDA.jl
/
initialization.jl
169 lines (137 loc) · 3.59 KB
/
initialization.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
@test has_cuda(true)
@test has_cuda_gpu(true)
# the API shouldn't have been initialized
@test !has_context()
@test !has_device()
ctx = context()
dev = device()
# querying Julia's side of things shouldn't cause initialization
@test !has_context()
@test !has_device()
# now cause initialization
a = CuArray([42])
@test has_context()
@test current_context() == ctx
@test has_device()
@test current_device() == dev
# ... on a different task
task = @async begin
context()
end
@test ctx == fetch(task)
device!(CuDevice(0))
@test device!(()->true, CuDevice(0))
@inferred device!(()->42, CuDevice(0))
context!(ctx)
@test context!(()->true, ctx)
@inferred context!(()->42, ctx)
@test_throws ErrorException device!(0, CUDA.CU_CTX_SCHED_YIELD)
if CUDA.can_reset_device()
# NVIDIA bug #3240770
device_reset!()
device!(0, CUDA.CU_CTX_SCHED_YIELD)
# reset on a different task
let ctx = context()
@test CUDA.isvalid(ctx)
@test ctx == fetch(@async context())
@sync @async device_reset!()
@test CUDA.isvalid(context())
@test ctx != context()
end
end
# test the device selection functionality
if length(devices()) > 1
device!(0)
device!(1) do
@test device() == CuDevice(1)
end
@test device() == CuDevice(0)
device!(1)
@test device() == CuDevice(1)
end
# test that each task can work with devices independently from other tasks
if length(devices()) > 1
device!(0)
@test device() == CuDevice(0)
task = @async begin
device!(1)
@test device() == CuDevice(1)
end
fetch(task)
@test device() == CuDevice(0)
# reset on a task
task = @async begin
device!(1)
device_reset!()
end
fetch(task)
@test device() == CuDevice(0)
# math_mode
old_mm = CUDA.math_mode()
old_prec = CUDA.math_precision()
CUDA.math_mode!(CUDA.PEDANTIC_MATH)
@test CUDA.math_mode() == CUDA.PEDANTIC_MATH
CUDA.math_mode!(CUDA.PEDANTIC_MATH; precision=:Float16)
@test CUDA.math_precision() == :Float16
CUDA.math_mode!(old_mm; precision=old_prec)
# ensure the values we tested here aren't the defaults
@test CUDA.math_mode() != CUDA.PEDANTIC_MATH
@test CUDA.math_precision() != :Float16
# tasks on multiple threads
Threads.@threads for d in 0:1
for x in 1:100 # give threads a chance to trample over each other
device!(d)
yield()
@test device() == CuDevice(d)
yield()
sleep(rand(0.001:0.001:0.01))
device!(1-d)
yield()
@test device() == CuDevice(1-d)
yield()
end
end
@test device() == CuDevice(0)
end
@test deviceid() >= 0
@test deviceid(CuDevice(0)) == 0
if length(devices()) > 1
@test deviceid(CuDevice(1)) == 1
end
## default streams
default_s = stream()
s = CuStream()
@test s != default_s
# test stream switching
let
stream!(s)
@test stream() == s
stream!(default_s)
@test stream() == default_s
end
stream!(s) do
@test stream() == s
end
@test stream() == default_s
# default stream in task
task = @async begin
stream()
end
@test fetch(task) != default_s
@test stream() == default_s
# test stream switching in tasks
task = @async begin
stream!(s)
stream()
end
@test fetch(task) == s
@test stream() == default_s
@testset "issue 1331: repeated initialization failure should stick" begin
script = """
using CUDA, Test
@test !CUDA.functional()
@test !CUDA.functional()
"""
proc, out, err = julia_exec(`-e $script`, "CUDA_VISIBLE_DEVICES"=>"-1")
@test success(proc)
end