Skip to content

Commit 796c1bb

Browse files
committed
Add disk cache infrastructure back with tests
Apply suggestions from code review
1 parent 7e0a6ff commit 796c1bb

File tree

8 files changed

+118
-16
lines changed

8 files changed

+118
-16
lines changed

Manifest.toml

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ deps = ["ArgTools", "LibCURL", "NetworkOptions"]
2323
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
2424

2525
[[ExprTools]]
26-
git-tree-sha1 = "c1d06d129da9f55715c6c212866f5b1bddc5fa00"
26+
git-tree-sha1 = "56559bbef6ca5ea0c0818fa5c90320398a6fbf8d"
2727
uuid = "e2ba6199-217a-4e67-a87a-7c52f15ade04"
28-
version = "0.1.9"
28+
version = "0.1.8"
2929

3030
[[InteractiveUtils]]
3131
deps = ["Markdown"]
@@ -39,17 +39,15 @@ version = "1.4.1"
3939

4040
[[LLVM]]
4141
deps = ["CEnum", "LLVMExtra_jll", "Libdl", "Printf", "Unicode"]
42-
git-tree-sha1 = "ee3374fcbdcd90fb10b27f1d847af7ade3a3213b"
43-
repo-rev = "master"
44-
repo-url = "https://github.com/maleadt/LLVM.jl.git"
42+
git-tree-sha1 = "df115c31f5c163697eede495918d8e85045c8f04"
4543
uuid = "929cbde3-209d-540e-8aea-75f648917ca0"
46-
version = "5.0.0"
44+
version = "4.16.0"
4745

4846
[[LLVMExtra_jll]]
49-
deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl", "TOML"]
50-
git-tree-sha1 = "e46e3a40daddcbe851f86db0ec4a4a3d4badf800"
47+
deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl", "Pkg", "TOML"]
48+
git-tree-sha1 = "7718cf44439c676bc0ec66a87099f41015a522d6"
5149
uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab"
52-
version = "0.0.19+0"
50+
version = "0.0.16+2"
5351

5452
[[LazyArtifacts]]
5553
deps = ["Artifacts", "Pkg"]
@@ -116,12 +114,6 @@ uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
116114
[[SHA]]
117115
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
118116

119-
[[Scratch]]
120-
deps = ["Dates"]
121-
git-tree-sha1 = "30449ee12237627992a99d5e30ae63e4d78cd24a"
122-
uuid = "6c6a2e73-6563-6170-7368-637461726353"
123-
version = "1.2.0"
124-
125117
[[Serialization]]
126118
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
127119

Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
99
LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"
1010
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
1111
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
12+
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
1213
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
14+
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
15+
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
1316
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
1417
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
1518

src/GPUCompiler.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ using ExprTools: splitdef, combinedef
99

1010
using Libdl
1111

12+
using Serialization
1213
using Scratch: @get_scratch!
14+
using Preferences
1315

1416
include("utils.jl")
1517

src/cache.jl

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,39 @@ end
127127
true)))
128128
end
129129

130+
const disk_cache = parse(Bool, @load_preference("disk_cache", "false"))
131+
const cache_key = @load_preference("cache_key", "")
132+
133+
"""
134+
enable_cache!(state=true)
135+
136+
Activate the GPUCompiler disk cache in the current environment.
137+
You will need to restart your Julia environment for it to take effect.
138+
139+
!!! warning
140+
The disk cache is not automatically invalidated. It is sharded upon
141+
`cache_key` (see [`set_cache_key``](@ref)), the GPUCompiler version
142+
and your Julia version.
143+
"""
144+
function enable_cache!(state=true)
145+
@set_preferences!("disk_cache"=>state)
146+
end
147+
148+
"""
149+
set_cache_key(key)
150+
151+
If you are deploying an application it is recommended that you use your
152+
application name and version as a cache key. To minimize the risk of
153+
encountering spurious cache hits.
154+
"""
155+
function set_cache_key(key)
156+
@set_preferences!("cache_key"=>key)
157+
end
158+
159+
key(ver::VersionNumber) = "$(ver.major)_$(ver.minor)_$(ver.patch)"
160+
cache_path() = @get_scratch!(cache_key * "-kernels-" * key(VERSION) * "-" * key(pkg_version))
161+
clear_disk_cache!() = rm(cache_path(); recursive=true, force=true)
162+
130163
const cache_lock = ReentrantLock()
131164

132165
"""
@@ -173,7 +206,18 @@ end
173206
job = CompilerJob(src, cfg)
174207

175208
asm = nothing
176-
# TODO: consider loading the assembly from an on-disk cache here
209+
# can we load from the disk cache?
210+
if disk_cache
211+
path = joinpath(cache_path(), "$key.jls")
212+
if isfile(path)
213+
try
214+
asm = deserialize(path)
215+
@debug "Loading compiled kernel for $spec from $path"
216+
catch ex
217+
@warn "Failed to load compiled kernel at $path" exception=(ex, catch_backtrace())
218+
end
219+
end
220+
end
177221

178222
# compile
179223
if asm === nothing
@@ -182,6 +226,10 @@ end
182226
end
183227

184228
asm = compiler(job)
229+
230+
if disk_cache && !isfile(path)
231+
serialize(path, asm)
232+
end
185233
end
186234

187235
# link (but not if we got here because of forced compilation,

test/CacheEnv/LocalPreferences.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[GPUCompiler]
2+
disk_cache = true
3+
cache_key = "test"

test/CacheEnv/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[extras]
2+
GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55"

test/cache.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using GPUCompiler
2+
using Test
3+
4+
const TOTAL_KERNELS = 1
5+
6+
clear = parse(Bool, ARGS[1])
7+
8+
@test GPUCompiler.disk_cache == true
9+
10+
if clear
11+
GPUCompiler.clear_disk_cache!()
12+
@test length(readdir(GPUCompiler.cache_path())) == 0
13+
else
14+
@test length(readdir(GPUCompiler.cache_path())) == TOTAL_KERNELS
15+
end
16+
17+
using LLVM, LLVM.Interop
18+
19+
include("util.jl")
20+
include("definitions/native.jl")
21+
22+
kernel() = return
23+
24+
const runtime_cache = Dict{UInt, Any}()
25+
26+
function compiler(job)
27+
return GPUCompiler.compile(:asm, job)
28+
end
29+
30+
function linker(job, asm)
31+
asm
32+
end
33+
34+
let (job, kwargs) = native_job(kernel, Tuple{})
35+
GPUCompiler.cached_compilation(runtime_cache, job, compiler, linker)
36+
end
37+
38+
@test length(readdir(GPUCompiler.cache_path())) == TOTAL_KERNELS

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,18 @@ include("examples.jl")
2929

3030
haskey(ENV, "CI") && GPUCompiler.timings()
3131

32+
@testset "Disk cache" begin
33+
@test GPUCompiler.disk_cache == false
34+
35+
cmd = Base.julia_cmd()
36+
if Base.JLOptions().project != C_NULL
37+
cmd = `$cmd --project=$(unsafe_string(Base.JLOptions().project))`
38+
end
39+
40+
withenv("JULIA_LOAD_PATH" => "$(get(ENV, "JULIA_LOAD_PATH", "")):$(joinpath(@__DIR__, "CacheEnv"))") do
41+
@test success(pipeline(`$cmd cache.jl true`, stderr=stderr, stdout=stdout))
42+
@test success(pipeline(`$cmd cache.jl false`, stderr=stderr, stdout=stdout))
43+
end
44+
end
45+
3246
end

0 commit comments

Comments
 (0)