Skip to content

Commit

Permalink
Merge pull request #73 from CliMA/ck/maybe_cuda_sync
Browse files Browse the repository at this point in the history
Define cuda_sync macro
  • Loading branch information
charleskawczynski authored Mar 23, 2024
2 parents 08f4842 + b902919 commit 13f332d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = [
"Jake Bolewski <[email protected]>",
"Gabriele Bozzola <[email protected]>",
]
version = "0.5.7"
version = "0.5.8"

[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ClimaComms.@threaded
ClimaComms.@time
ClimaComms.@elapsed
ClimaComms.@sync
ClimaComms.@cuda_sync
```

## Contexts
Expand Down
48 changes: 48 additions & 0 deletions src/devices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,25 @@ for CPU devices and
CUDA.@sync expr
```
for CUDA devices.
An example use-case of this might be:
```julia
BenchmarkTools.@benchmark begin
if ClimaComms.device() isa ClimaComms.CUDADevice
CUDA.@sync begin
launch_cuda_kernels_or_spawn_tasks!(...)
end
elseif ClimaComms.device() isa ClimaComms.CPUMultiThreading
Base.@sync begin
launch_cuda_kernels_or_spawn_tasks!(...)
end
end
end
```
If the CPU version of the above example does not leverage
spawned tasks (which require using `Base.sync` or `Threads.wait`
to synchronize), then you may want to simply use [`@cuda_sync`](@ref).
"""
macro sync(device, expr)
# https://github.com/JuliaLang/julia/issues/28979#issuecomment-1756145207
Expand All @@ -194,3 +213,32 @@ macro sync(device, expr)
end
end)
end

"""
@cuda_sync device expr
Device-flexible `CUDA.@sync`.
Lowers to
```julia
expr
```
for CPU devices and
```julia
CUDA.@sync expr
```
for CUDA devices.
"""
macro cuda_sync(device, expr)
# https://github.com/JuliaLang/julia/issues/28979#issuecomment-1756145207
return esc(quote
if $(device) isa $CUDADevice
$CUDA.@sync begin
$(expr)
end
else
@assert $(device) isa $AbstractDevice
$(expr)
end
end)
end
4 changes: 4 additions & 0 deletions test/hygiene.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ function test_macro_hyhiene(dev)
CC.@sync dev for i in 1:n
sin.(rand(10))
end

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

Expand Down

2 comments on commit 13f332d

@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/103455

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.8 -m "<description of version>" 13f332d3770dd2591a79e2852933bc45f315f6ed
git push origin v0.5.8

Please sign in to comment.