1
1
# GPU memory management and pooling
2
2
3
+ using Printf
4
+ using TimerOutputs
5
+
6
+
3
7
# # allocation statistics
4
8
5
9
mutable struct AllocStats
@@ -28,6 +32,10 @@ Base.copy(alloc_stats::AllocStats) =
28
32
29
33
# # CUDA allocator
30
34
35
+ const alloc_to = TimerOutput ()
36
+
37
+ alloc_timings () = (show (alloc_to; allocations= false , sortby= :name ); println ())
38
+
31
39
const usage = Ref (0 )
32
40
const usage_limit = Ref {Union{Nothing,Int}} (nothing )
33
41
@@ -42,8 +50,9 @@ function actual_alloc(bytes)
42
50
# try the actual allocation
43
51
try
44
52
alloc_stats. actual_time += Base. @elapsed begin
45
- buf = Mem. alloc (Mem. Device, bytes)
53
+ @timeit alloc_to " alloc " buf = Mem. alloc (Mem. Device, bytes)
46
54
end
55
+ @assert sizeof (buf) == bytes
47
56
alloc_stats. actual_nalloc += 1
48
57
alloc_stats. actual_alloc += bytes
49
58
usage[] += bytes
@@ -61,25 +70,24 @@ function actual_free(buf)
61
70
usage[] -= sizeof (buf)
62
71
63
72
if CUDAdrv. isvalid (buf. ctx)
64
- alloc_stats. actual_time += Base. @elapsed Mem. free (buf)
73
+ @timeit alloc_to " free" begin
74
+ alloc_stats. actual_time += Base. @elapsed Mem. free (buf)
75
+ end
65
76
end
66
77
67
78
return
68
79
end
69
80
70
81
71
- # # pool timings
72
-
73
- using TimerOutputs
82
+ # # memory pool
74
83
75
84
const pool_to = TimerOutput ()
76
85
77
86
macro pool_timeit (args... )
78
87
TimerOutputs. timer_expr (__module__, false , :($ CuArrays. pool_to), args... )
79
88
end
80
89
81
-
82
- # # pool implementations
90
+ pool_timings () = (show (pool_to; allocations= false , sortby= :name ); println ())
83
91
84
92
# API:
85
93
# - init()
@@ -137,56 +145,23 @@ end
137
145
return
138
146
end
139
147
140
- function __init_memory__ ()
141
- if haskey (ENV , " CUARRAYS_MEMORY_LIMIT" )
142
- usage_limit[] = parse (Int, ENV [" CUARRAYS_MEMORY_LIMIT" ])
143
- end
144
-
145
- if haskey (ENV , " CUARRAYS_MEMORY_POOL" )
146
- memory_pool! (
147
- if ENV [" CUARRAYS_MEMORY_POOL" ] == " binned"
148
- BinnedPool
149
- elseif ENV [" CUARRAYS_MEMORY_POOL" ] == " simple"
150
- SimplePool
151
- elseif ENV [" CUARRAYS_MEMORY_POOL" ] == " split"
152
- SplittingPool
153
- elseif ENV [" CUARRAYS_MEMORY_POOL" ] == " none"
154
- DummyPool
155
- else
156
- error (" Invalid allocator selected" )
157
- end )
158
- else
159
- memory_pool! ()
160
- end
161
-
162
- # if the user hand-picked an allocator, be a little verbose
163
- if haskey (ENV , " CUARRAYS_MEMORY_POOL" )
164
- atexit (()-> begin
165
- Core. println ("""
166
- CuArrays.jl $(nameof (pool[])) statistics:
167
- - $(alloc_stats. pool_nalloc) pool allocations: $(Base. format_bytes (alloc_stats. pool_alloc)) in $(round (alloc_stats. pool_time; digits= 2 )) s
168
- - $(alloc_stats. actual_nalloc) CUDA allocations: $(Base. format_bytes (alloc_stats. actual_alloc)) in $(round (alloc_stats. actual_time; digits= 2 )) s""" )
169
- end )
170
- end
171
- end
172
-
173
148
function memory_pool! (mod:: Module = BinnedPool)
174
149
if pool[] != = nothing
175
150
pool[]. deinit ()
176
151
end
177
152
178
- TimerOutputs . reset_timer! (pool_to )
153
+ reset_timers! ( )
179
154
180
155
pool[] = mod
181
156
mod. init ()
182
157
183
158
return
184
159
end
185
160
161
+ pool_dump () = pool[]. dump ()
186
162
187
- # # utilities
188
163
189
- using Printf
164
+ # # utilities
190
165
191
166
macro allocated (ex)
192
167
quote
@@ -308,6 +283,43 @@ function memory_status()
308
283
end
309
284
end
310
285
311
- pool_timings () = (show (pool_to; allocations= false , sortby= :name ); println ())
312
286
313
- pool_dump () = pool[]. dump ()
287
+ # # init
288
+
289
+ function __init_memory__ ()
290
+ if haskey (ENV , " CUARRAYS_MEMORY_LIMIT" )
291
+ usage_limit[] = parse (Int, ENV [" CUARRAYS_MEMORY_LIMIT" ])
292
+ end
293
+
294
+ if haskey (ENV , " CUARRAYS_MEMORY_POOL" )
295
+ memory_pool! (
296
+ if ENV [" CUARRAYS_MEMORY_POOL" ] == " binned"
297
+ BinnedPool
298
+ elseif ENV [" CUARRAYS_MEMORY_POOL" ] == " simple"
299
+ SimplePool
300
+ elseif ENV [" CUARRAYS_MEMORY_POOL" ] == " split"
301
+ SplittingPool
302
+ elseif ENV [" CUARRAYS_MEMORY_POOL" ] == " none"
303
+ DummyPool
304
+ else
305
+ error (" Invalid allocator selected" )
306
+ end )
307
+ else
308
+ memory_pool! ()
309
+ end
310
+
311
+ # if the user hand-picked an allocator, be a little verbose
312
+ if haskey (ENV , " CUARRAYS_MEMORY_POOL" )
313
+ atexit (()-> begin
314
+ Core. println ("""
315
+ CuArrays.jl $(nameof (pool[])) statistics:
316
+ - $(alloc_stats. pool_nalloc) pool allocations: $(Base. format_bytes (alloc_stats. pool_alloc)) in $(round (alloc_stats. pool_time; digits= 2 )) s
317
+ - $(alloc_stats. actual_nalloc) CUDA allocations: $(Base. format_bytes (alloc_stats. actual_alloc)) in $(round (alloc_stats. actual_time; digits= 2 )) s""" )
318
+ end )
319
+ end
320
+ end
321
+
322
+ function reset_timers! ()
323
+ TimerOutputs. reset_timer! (alloc_to)
324
+ TimerOutputs. reset_timer! (pool_to)
325
+ end
0 commit comments