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

Define allowscalar #80

Merged
merged 1 commit into from
Jun 6, 2024
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
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
ClimaComms.jl Release Notes
========================

v0.6.2
-------

- We added a device-agnostic `allowscalar(f, ::AbstractDevice, args...; kwargs...)` to further assist in making CUDA an extension.

v0.6.1
-------

- Macros have been refactored to hopefully fix some code loading issues.

v0.6.0
Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ClimaComms.CUDADevice
ClimaComms.device
ClimaComms.device_functional
ClimaComms.array_type
ClimaComms.allowscalar
ClimaComms.@threaded
ClimaComms.@time
ClimaComms.@elapsed
Expand Down
2 changes: 2 additions & 0 deletions ext/ClimaCommsCUDAExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ function ClimaComms.device_functional(::ClimaComms.CUDADevice)
end

ClimaComms.array_type(::ClimaComms.CUDADevice) = CUDA.CuArray
ClimaComms.allowscalar(f, ::ClimaComms.CUDADevice, args...; kwargs...) =
CUDA.@allowscalar f(args...; kwargs...)

# Extending ClimaComms methods that operate on expressions (cannot use dispatch here)
ClimaComms.cuda_sync(expr) = CUDA.@sync expr
Expand Down
24 changes: 24 additions & 0 deletions src/devices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,27 @@ macro cuda_sync(device, expr)
end
end)
end

"""
allowscalar(f, ::AbstractDevice, args...; kwargs...)

Device-flexible version of `CUDA.@allowscalar`.

Lowers to
```julia
f(args...)
```
for CPU devices and
```julia
CUDA.@allowscalar f(args...)
```
for CUDA devices.

This is usefully written with closures via
```julia
allowscalar(device) do
f()
end
```
"""
allowscalar(f, ::AbstractDevice, args...; kwargs...) = f(args...; kwargs...)
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,13 @@ end
@test ClimaComms.bcast(context, AT(fill(Float64(pid), 3))) ==
AT(fill(Float64(1), 3))
end

@testset "allowscalar" begin
a = AT(rand(3))
local x
ClimaComms.allowscalar(device) do
x = a[1]
end
device isa ClimaComms.CUDADevice && @test_throws ErrorException a[1]
@test x == Array(a)[1]
end
Loading