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

Commit ec3d9ec

Browse files
committed
add some per device gc pressure
1 parent 9b85e14 commit ec3d9ec

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/memory.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Mem
55
import ..CLArrays: context
66
using OpenCL
77
import Base: pointer, eltype, cconvert, convert, unsafe_copy!
8-
using GPUArrays: supports_double, device
8+
using GPUArrays: supports_double, device, global_memory
99
using GPUArrays
1010

1111
immutable OwnedPtr{T}
@@ -28,19 +28,40 @@ function retain(p::OwnedPtr)
2828
return
2929
end
3030

31+
32+
33+
34+
const current_allocated_mem = Ref(0)
3135
function free(p::OwnedPtr)
3236
cl.@check_release cl.api.clReleaseMemObject(p)
3337
return
3438
end
3539

40+
function pressure_gc!(device, bytes)
41+
# If we used up 80% of our device. lets free see if we can free stuff with a gc swipe.
42+
# try three times, first only with a an unforced swipe
43+
mem = current_allocated_mem[] + bytes
44+
if (global_memory(device) * 0.8) < mem
45+
println("calling unforced gc")
46+
gc(false) # non force
47+
end
48+
if (global_memory(device) * 0.8) < mem
49+
println("force swipe")
50+
gc() # non forced
51+
end
52+
current_allocated_mem[] += bytes
53+
end
54+
3655
function alloc(T, elems::Integer, ctx::cl.Context, flags = cl.CL_MEM_READ_WRITE)
3756
dev = GPUArrays.device(ctx)
3857
if T == Float64 && !supports_double(dev)
3958
error("Float64 is not supported by your device: $dev. Make sure to convert all types for the GPU to Float32")
4059
end
60+
nbytes = cl.cl_uint(elems * sizeof(T))
61+
pressure_gc!(dev, nbytes)
4162
err_code = Ref{cl.CL_int}()
4263
mem_id = cl.api.clCreateBuffer(
43-
ctx.id, flags, cl.cl_uint(elems * sizeof(T)),
64+
ctx.id, flags, nbytes,
4465
C_NULL,
4566
err_code
4667
)

0 commit comments

Comments
 (0)