Skip to content
This repository has been archived by the owner on Mar 12, 2021. It is now read-only.

Eagerly put objects back in the pool #270

Closed
maleadt opened this issue Feb 4, 2019 · 3 comments
Closed

Eagerly put objects back in the pool #270

maleadt opened this issue Feb 4, 2019 · 3 comments

Comments

@maleadt
Copy link
Member

maleadt commented Feb 4, 2019

Basically, something like #226, but more careful and with actual uses in CuArrays.jl. Some backstory.

We now depend on the Julia GC to collect CuArray objects before they end up in the pool again. This is not optimal, and compounded by the fact that the Julia GC doens't know about the GPU's memory pressure. As a result, we kind-of rely on hitting a GPU OOM before collecting dead arrays and putting them back into the pool:

CuArrays.jl/src/memory.jl

Lines 254 to 256 in 8525a9d

ex == CUDAdrv.ERROR_OUT_OF_MEMORY || rethrow()
# 3. that failed; make Julia collect objects and check 1. again
gc(true) # full collection

On the other hand, we often know that arrays are unused and could be put back into the pool again. For example:

buffer = CuArray{$elty}(undef, bufSize[])
tau = CuArray{$elty}(undef, min(m, n))
devinfo = CuArray{Cint}(undef, 1)
@check ccall(($(string(fname)),libcusolver), cusolverStatus_t,
(cusolverDnHandle_t, Cint, Cint, Ptr{$elty},
Cint, Ptr{$elty}, Ptr{$elty}, Cint, Ptr{Cint}),
dense_handle(), m, n, A, lda, tau, buffer,
bufSize[], devinfo)
info = _getindex(devinfo, 1)
if info < 0
throw(ArgumentError("The $(info)th parameter is wrong"))
end
A, tau

One option is to call finalize, but that is a pretty slow call and shouldn't be used in performance critical code. Instead, we should have a call to eagerly "free" objects (from the perspective of CuArrays.jl) and put them back into the pool. For example, the geqrf! method above would then free its buffer before returning. To prevent use of early-freed arrays, we should probably wipe the memory buffer handle (eg. set to NULL or some other sentinel value). Care should be taken to deal with ownership (not support this call when we don't own the CuArray? make sure the CUDAdrv refcount is 1?).

@maleadt
Copy link
Member Author

maleadt commented Feb 4, 2019

cc @SuperFluffy -- would be good to have a use-case or benchmark.

@KristofferC
Copy link
Contributor

The common way of doing this is (of course) with the do syntax, but this will likely make the code a lot uglier and more "indent"-y.

@maleadt
Copy link
Member Author

maleadt commented Feb 8, 2019

Implemented both.

@maleadt maleadt closed this as completed Feb 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants