Skip to content

Commit

Permalink
Merge pull request #62 from CliMA/ck/tests
Browse files Browse the repository at this point in the history
Fix macro hygiene
  • Loading branch information
charleskawczynski authored Jul 13, 2023
2 parents 30a78cb + ea983ec commit 5381b8a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
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)

2 comments on commit 5381b8a

@charleskawczynski
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/87452

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.3 -m "<description of version>" 5381b8a8eeb17df9f938941f1e022ac6fb3bd464
git push origin v0.5.3

Please sign in to comment.