Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix macro hygiene #62

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ClimaComms"
uuid = "3a4d1b5c-c61d-41fd-a00a-5873ba7a1b0d"
authors = ["CliMA Contributors <[email protected]>"]
version = "0.5.2"
version = "0.5.3"

[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
Expand Down
16 changes: 10 additions & 6 deletions src/devices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ that this is statically inferred.
- https://discourse.julialang.org/t/threads-threads-with-one-thread-how-to-remove-the-overhead/58435
- https://discourse.julialang.org/t/overhead-of-threads-threads/53964
"""
macro threaded(device, expr)
return quote
macro threaded(device, loop)
quote
if $(esc(device)) isa CPUMultiThreaded
Threads.@threads $(expr)
Threads.@threads $(Expr(
loop.head,
Expr(loop.args[1].head, esc.(loop.args[1].args)...),
esc(loop.args[2]),
))
else
@assert $(esc(device)) isa AbstractDevice
$(esc(expr))
$(esc(loop))
end
end
end
Expand Down Expand Up @@ -152,10 +156,10 @@ for CUDA devices.
macro elapsed(device, expr)
return quote
if $(esc(device)) isa CUDADevice
CUDA.@elapsed $(expr)
CUDA.@elapsed $(esc(expr))
else
@assert $(esc(device)) isa AbstractDevice
Base.@elapsed $(expr)
Base.@elapsed $(esc(expr))
end
end
end
25 changes: 16 additions & 9 deletions test/hygiene.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import ClimaComms as CC
dev = CC.device()
CC.@threaded dev for i in 1:2
1
end
function test_macro_hyhiene(dev)
n = 3 # tests that we can reach variables defined in scope
CC.@threaded dev for i in 1:n
if Threads.nthreads() > 1
@show Threads.threadid()
end
end

CC.@time dev for i in 1:2
sin.(rand(10))
end
CC.@time dev for i in 1:n
sin.(rand(10))
end

CC.@elapsed dev for i in 1:2
sin.(rand(10))
CC.@elapsed dev for i in 1:n
sin.(rand(10))
end
end
dev = CC.device()

test_macro_hyhiene(dev)
Loading