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

Commit 9ebe74e

Browse files
committed
Fix RNG finalization.
1 parent 03a8b69 commit 9ebe74e

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/rand/random.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,27 @@ export rand_logn!, rand_poisson!
1414

1515

1616
mutable struct RNG <: Random.AbstractRNG
17-
ptr::curandGenerator_t
17+
handle::curandGenerator_t
18+
ctx::CuContext
1819
typ::Int
1920

2021
function RNG(typ=CURAND_RNG_PSEUDO_DEFAULT)
21-
ptr = Ref{curandGenerator_t}()
22-
@allocates curandCreateGenerator(ptr, typ)
23-
obj = new(ptr[], typ)
24-
finalizer(curandDestroyGenerator, obj)
22+
handle_ref = Ref{curandGenerator_t}()
23+
@allocates curandCreateGenerator(handle_ref, typ)
24+
25+
obj = new(handle_ref[], CuCurrentContext(), typ)
26+
finalizer(unsafe_destroy!, obj)
2527
return obj
2628
end
2729
end
2830

29-
Base.unsafe_convert(::Type{curandGenerator_t}, rng::RNG) = rng.ptr
31+
function unsafe_destroy!(rng::RNG)
32+
if CUDAdrv.isvalid(rng.ctx)
33+
curandDestroyGenerator(e)
34+
end
35+
end
36+
37+
Base.unsafe_convert(::Type{curandGenerator_t}, rng::RNG) = rng.handle
3038

3139

3240
## seeding

0 commit comments

Comments
 (0)