Skip to content

Commit

Permalink
[Experimental] Add memory recording which can then be bulk-freed (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
pxl-th authored Nov 18, 2024
1 parent 4385ed9 commit 98e7084
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AMDGPU"
uuid = "21141c5a-9bdb-4563-92ae-f87d6854732e"
authors = ["Julian P Samaroo <[email protected]>", "Valentin Churavy <[email protected]>", "Anton Smirnov <[email protected]>"]
version = "1.1.0"
version = "1.1.1"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
1 change: 1 addition & 0 deletions src/AMDGPU.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ include("tls.jl")
include("highlevel.jl")
include("reflection.jl")
include("array.jl")
include("memory_record.jl")
include("conversions.jl")
include("broadcast.jl")
include("exception_handler.jl")
Expand Down
6 changes: 4 additions & 2 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ mutable struct ROCArray{T, N, B} <: AbstractGPUArray{T, N}
) where {T, N, B <: Mem.AbstractAMDBuffer}
@assert isbitstype(T) "ROCArray only supports bits types"
data = DataRef(pool_free, pool_alloc(B, prod(dims) * sizeof(T)))
xs = new{T, N, B}(data, dims, 0)
return finalizer(unsafe_free!, xs)
x = new{T, N, B}(data, dims, 0)
x = finalizer(unsafe_free!, x)
RECORD_MEMORY[] && record!(x)
return x
end

function ROCArray{T, N}(
Expand Down
48 changes: 48 additions & 0 deletions src/memory_record.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# NOTE: EXPERIMENTAL API.

const MemoryRecords = LockedObject(Dict{UInt64, ROCArray}())

const RECORD_MEMORY::Ref{Bool} = Ref(false)

function record_memory!(rec::Bool; free::Bool = true, sync::Bool = false)
RECORD_MEMORY[] = rec
if !rec
free && free_records!(; sync)
end
return
end

record_memory() = RECORD_MEMORY[]

function record!(x)
Base.lock(records -> records[_hash(x)] = x, MemoryRecords)
return
end

function free_records!(; sync::Bool = false)
Base.lock(MemoryRecords) do records
for (k, x) in records
unsafe_free!(x)
end
empty!(records)
end
sync && AMDGPU.synchronize()
return
end

function remove_record!(x)
record_memory() || return

k = _hash(x)
Base.lock(MemoryRecords) do records
if k in records.keys
pop!(records, k)
end
end
return
end

_hash(x::ROCArray) =
Base.hash(x.buf.rc.obj.mem.ptr,
Base.hash(x.offset,
Base.hash(x.dims)))

2 comments on commit 98e7084

@pxl-th
Copy link
Member Author

@pxl-th pxl-th commented on 98e7084 Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/119674

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.1 -m "<description of version>" 98e7084acf3e29934ad4742af38fbbad6aa7b915
git push origin v1.1.1

Please sign in to comment.