forked from JuliaGPU/CUDA.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.jl
203 lines (154 loc) · 4.86 KB
/
context.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
# Context management
export
CuContext, CuCurrentContext, activate,
synchronize, device
## construction and destruction
"""
CuContext(dev::CuDevice, flags::CUctx_flags=CTX_SCHED_AUTO)
CuContext(f::Function, ...)
Create a CUDA context for device. A context on the GPU is analogous to a process on the CPU,
with its own distinct address space and allocated resources. When a context is destroyed,
the system cleans up the resources allocated to it.
When you are done using the context, call [`unsafe_destroy!`](@ref) to mark it for deletion,
or use do-block syntax with this constructor.
!!! warning
Contexts are unique, and should be compared using `===` and not `isequal` or `==`: With
primary device contexts, identical handles might be returned after resetting the context
(device) and all associated resources.
"""
mutable struct CuContext
handle::CUcontext
function CuContext(handle::CUcontext)
handle == C_NULL && return new(C_NULL)
return get!(valid_contexts, handle) do
new(handle)
end
end
end
# the `valid` bit serves two purposes: make sure we don't double-free a context (in case we
# early-freed it ourselves before the GC kicked in), and to make sure we don't free derived
# resources after the owning context has been destroyed (which can happen due to
# out-of-order finalizer execution)
const valid_contexts = Dict{CUcontext,CuContext}()
isvalid(ctx::CuContext) = any(x->x===ctx, values(valid_contexts))
# NOTE: we can't just look up by the handle, because contexts derived from a primary one
# have the same handle even though they might have been destroyed in the meantime.
function invalidate!(ctx::CuContext)
delete!(valid_contexts, ctx.handle)
return
end
"""
unsafe_destroy!(ctx::CuContext)
Immediately destroy a context, freeing up all resources associated with it. This does not
respect any users of the context, and might make other objects unusable.
"""
function unsafe_destroy!(ctx::CuContext)
if isvalid(ctx)
cuCtxDestroy(ctx)
invalidate!(ctx)
end
end
Base.unsafe_convert(::Type{CUcontext}, ctx::CuContext) = ctx.handle
Base.:(==)(a::CuContext, b::CuContext) = a.handle == b.handle
Base.hash(ctx::CuContext, h::UInt) = hash(ctx.handle, h)
@enum_without_prefix CUctx_flags CU_
function CuContext(dev::CuDevice, flags::CUctx_flags=CTX_SCHED_AUTO)
handle_ref = Ref{CUcontext}()
cuCtxCreate(handle_ref, flags, dev)
return CuContext(handle_ref[])
end
"""
CuCurrentContext()
Return the current context, or `nothing` if there is no active context.
"""
function CuCurrentContext()
handle_ref = Ref{CUcontext}()
cuCtxGetCurrent(handle_ref)
if handle_ref[] == C_NULL
return nothing
else
return CuContext(handle_ref[])
end
end
"""
push!(CuContext, ctx::CuContext)
Pushes a context on the current CPU thread.
"""
Base.push!(::Type{CuContext}, ctx::CuContext) = cuCtxPushCurrent(ctx)
"""
pop!(CuContext)
Pops the current CUDA context from the current CPU thread, and returns that context.
"""
function Base.pop!(::Type{CuContext})
handle_ref = Ref{CUcontext}()
cuCtxPopCurrent(handle_ref)
CuContext(handle_ref[])
end
"""
activate(ctx::CuContext)
Binds the specified CUDA context to the calling CPU thread.
"""
activate(ctx::CuContext) = cuCtxSetCurrent(ctx)
function CuContext(f::Function, dev::CuDevice, args...)
ctx = CuContext(dev, args...) # implicitly pushes
try
f(ctx)
finally
@assert pop!(CuContext) == ctx
unsafe_destroy!(ctx)
end
end
## context properties
"""
device()
device(ctx::Cucontext)
Returns the device for a context.
"""
function device(ctx::CuContext)
push!(CuContext, ctx)
device_ref = Ref{CUdevice}()
cuCtxGetDevice(device_ref)
pop!(CuContext)
return CuDevice(Bool, device_ref[])
end
function device()
device_ref = Ref{CUdevice}()
cuCtxGetDevice(device_ref)
return CuDevice(Bool, device_ref[])
end
"""
synchronize()
Block for the current context's tasks to complete.
"""
synchronize() = cuCtxSynchronize()
## cache config
export cache_config, cache_config!
@enum_without_prefix CUfunc_cache CU_
function cache_config()
config = Ref{CUfunc_cache}()
cuCtxGetCacheConfig(config)
return config[]
end
function cache_config!(config::CUfunc_cache)
cuCtxSetCacheConfig(config)
end
## shared memory config
export shmem_config, shmem_config!
@enum_without_prefix CUsharedconfig CU_
function shmem_config()
config = Ref{CUsharedconfig}()
cuCtxGetSharedMemConfig(config)
return config[]
end
function shmem_config!(config::CUsharedconfig)
cuCtxSetSharedMemConfig(config)
end
## limits
export limit, limit!
@enum_without_prefix CUlimit CU_
function limit(lim::CUlimit)
val = Ref{Csize_t}()
cuCtxGetLimit(val, lim)
return Int(val[])
end
limit!(lim::CUlimit, val) = cuCtxSetLimit(lim, val)