@@ -5,7 +5,7 @@ module Mem
5
5
import .. CLArrays: context
6
6
using OpenCL
7
7
import Base: pointer, eltype, cconvert, convert, unsafe_copy!
8
- using GPUArrays: supports_double, device
8
+ using GPUArrays: supports_double, device, global_memory
9
9
using GPUArrays
10
10
11
11
immutable OwnedPtr{T}
@@ -28,19 +28,40 @@ function retain(p::OwnedPtr)
28
28
return
29
29
end
30
30
31
+
32
+
33
+
34
+ const current_allocated_mem = Ref (0 )
31
35
function free (p:: OwnedPtr )
32
36
cl. @check_release cl. api. clReleaseMemObject (p)
33
37
return
34
38
end
35
39
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
+
36
55
function alloc (T, elems:: Integer , ctx:: cl.Context , flags = cl. CL_MEM_READ_WRITE)
37
56
dev = GPUArrays. device (ctx)
38
57
if T == Float64 && ! supports_double (dev)
39
58
error (" Float64 is not supported by your device: $dev . Make sure to convert all types for the GPU to Float32" )
40
59
end
60
+ nbytes = cl. cl_uint (elems * sizeof (T))
61
+ pressure_gc! (dev, nbytes)
41
62
err_code = Ref {cl.CL_int} ()
42
63
mem_id = cl. api. clCreateBuffer (
43
- ctx. id, flags, cl . cl_uint (elems * sizeof (T)) ,
64
+ ctx. id, flags, nbytes ,
44
65
C_NULL ,
45
66
err_code
46
67
)
0 commit comments