Skip to content

Commit a8bb216

Browse files
committed
Add memory record
1 parent 98e7084 commit a8bb216

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/array.jl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1+
const MemoryRecords = LockedObject(Dict{UInt64, Any}())
2+
3+
# TODO make TLS
4+
const RECORD_MEMORY::Ref{Bool} = Ref(false)
5+
6+
function record_memory!(rec::Bool; free::Bool = true, sync::Bool = false)
7+
RECORD_MEMORY[] = rec
8+
if !rec
9+
free && free_records!(; sync)
10+
end
11+
return
12+
end
13+
14+
record_memory() = RECORD_MEMORY[]
15+
16+
function record!(x)
17+
# @info "Recording $(typeof(x)) $(size(x))"
18+
Base.lock(records -> records[_hash(x)] = x, MemoryRecords)
19+
return
20+
end
21+
22+
function free_records!(; sync::Bool = false)
23+
Base.lock(MemoryRecords) do records
24+
# @info "Freeing `$(length(records))` records"
25+
for (k, x) in records
26+
unsafe_free!(x)
27+
end
28+
empty!(records)
29+
end
30+
sync && AMDGPU.synchronize()
31+
return
32+
end
33+
34+
function remove_record!(x)
35+
record_memory() || return
36+
37+
k = _hash(x)
38+
Base.lock(MemoryRecords) do records
39+
if k in records.keys
40+
# @info "Removing record"
41+
pop!(records, k)
42+
end
43+
end
44+
return
45+
end
46+
147
mutable struct ROCArray{T, N, B} <: AbstractGPUArray{T, N}
248
buf::DataRef{Managed{B}}
349
dims::Dims{N}
@@ -23,6 +69,18 @@ mutable struct ROCArray{T, N, B} <: AbstractGPUArray{T, N}
2369
end
2470
end
2571

72+
function _hash(x::ROCArray)
73+
# @info "_hash"
74+
# @show x.buf.rc.obj.mem.ptr
75+
# @show x.offset
76+
# @show x.dims
77+
r = Base.hash(x.buf.rc.obj.mem.ptr,
78+
Base.hash(x.offset,
79+
Base.hash(x.dims)))
80+
# @show r
81+
return r
82+
end
83+
2684
GPUArrays.storage(a::ROCArray) = a.buf
2785

2886
function GPUArrays.derive(

src/memory.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ end
258258

259259
function pool_alloc(::Type{B}, bytesize) where B
260260
s = AMDGPU.stream()
261+
# @info "[pool_alloc] $(Base.format_bytes(bytesize))"
262+
# display(stacktrace()); println()
263+
# println()
264+
# println()
261265
Managed(B(bytesize; stream=s); stream=s)
262266
end
263267

t.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using AMDGPU
2+
3+
function main()
4+
@show Base.format_bytes(AMDGPU.memory_stats().live)
5+
AMDGPU.record_memory!(true)
6+
7+
res = nothing
8+
9+
for i in 1:2
10+
x = AMDGPU.rand(Float32, 1024)
11+
y = sum(x; dims=1)
12+
if i == 1
13+
res = y
14+
AMDGPU.remove_record!(res)
15+
end
16+
end
17+
18+
AMDGPU.record_memory!(false)
19+
@show Base.format_bytes(AMDGPU.memory_stats().live)
20+
println("Done")
21+
22+
@show res
23+
return
24+
end
25+
main()

0 commit comments

Comments
 (0)