Skip to content

Commit

Permalink
Merge pull request #1724 from CliMA/ck/fix_mbf2
Browse files Browse the repository at this point in the history
Add more fixes and support for FusedMultiBroadcasts
  • Loading branch information
charleskawczynski authored May 11, 2024
2 parents 52d0b43 + 6685573 commit b1fc161
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ClimaCore"
uuid = "d414da3d-4745-48bb-8d80-42e94e092884"
authors = ["CliMA Contributors <clima-software@caltech.edu>"]
version = "0.14.3"
version = "0.14.4"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
65 changes: 61 additions & 4 deletions ext/cuda/data_layouts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,16 @@ Base.@propagate_inbounds function rcopyto_at!(
v,
)
dest, bc = pair.first, pair.second
if v <= size(dest, 4)
if 1 v <= size(dest, 4)
dest[I] = isascalar(bc) ? bc[] : bc[I]
end
return nothing
end
Base.@propagate_inbounds function rcopyto_at!(pair::Pair{<:DataF, <:Any}, I, v)
dest, bc = pair.first, pair.second
if 1 v <= size(dest, 4)
bcI = isascalar(bc) ? bc[] : bc[I]
dest[I] = bcI
dest[] = bcI
end
return nothing
end
Expand Down Expand Up @@ -233,10 +240,9 @@ function fused_copyto!(
if Nv > 0 && Nh > 0
Nv_per_block = min(Nv, fld(256, Nij * Nij))
Nv_blocks = cld(Nv, Nv_per_block)
args = (fmbc,)
auto_launch!(
knl_fused_copyto!,
args,
(fmbc,),
dest1;
threads_s = (Nij, Nij, Nv_per_block),
blocks_s = (Nh, Nv_blocks),
Expand All @@ -245,6 +251,57 @@ function fused_copyto!(
return nothing
end

function fused_copyto!(
fmbc::FusedMultiBroadcast,
dest1::IJFH{S, Nij},
::ClimaComms.CUDADevice,
) where {S, Nij}
_, _, _, _, Nh = size(dest1)
if Nh > 0
auto_launch!(
knl_fused_copyto!,
(fmbc,),
dest1;
threads_s = (Nij, Nij),
blocks_s = (Nh, 1),
)
end
return nothing
end
function fused_copyto!(
fmbc::FusedMultiBroadcast,
dest1::VF{S},
::ClimaComms.CUDADevice,
) where {S}
_, _, _, Nv, Nh = size(dest1)
if Nv > 0 && Nh > 0
auto_launch!(
knl_fused_copyto!,
(fmbc,),
dest1;
threads_s = (1, 1),
blocks_s = (Nh, Nv),
)
end
return nothing
end

function fused_copyto!(
fmbc::FusedMultiBroadcast,
dest1::DataF{S},
::ClimaComms.CUDADevice,
) where {S}
auto_launch!(
knl_fused_copyto!,
(fmbc,),
dest1;
threads_s = (1, 1),
blocks_s = (1, 1),
)
return nothing
end


adapt_f(to, f::F) where {F} = Adapt.adapt(to, f)
adapt_f(to, ::Type{F}) where {F} = (x...) -> F(x...)

Expand Down
44 changes: 42 additions & 2 deletions src/DataLayouts/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -606,14 +606,26 @@ isascalar(
} = true
isascalar(bc) = false


# Fused multi-broadcast entry point for DataLayouts
function Base.copyto!(
fmbc::FusedMultiBroadcast{T},
) where {N, T <: NTuple{N, Pair{<:AbstractData, <:Any}}}
dest1 = first(fmbc.pairs).first
fmb_inst = FusedMultiBroadcast(
map(fmbc.pairs) do pair
bc = pair.second
bc′ = if isascalar(bc)
Base.Broadcast.instantiate(
Base.Broadcast.Broadcasted(bc.style, bc.f, bc.args, ()),
)
else
bc
end
Pair(pair.first, bc′)
end,
)
# check_fused_broadcast_axes(fmbc) # we should already have checked the axes
fused_copyto!(fmbc, dest1, ClimaComms.device(dest1))
fused_copyto!(fmb_inst, dest1, ClimaComms.device(dest1))
end

function fused_copyto!(
Expand All @@ -633,6 +645,23 @@ function fused_copyto!(
return nothing
end

function fused_copyto!(
fmbc::FusedMultiBroadcast,
dest1::IJFH{S, Nij, A},
::ClimaComms.AbstractCPUDevice,
) where {S, Nij, A}
# copy contiguous columns
_, _, _, Nv, Nh = size(dest1)
for (dest, bc) in fmbc.pairs
@inbounds for h in 1:Nh, j in 1:Nij, i in 1:Nij
I = CartesianIndex(i, j, 1, 1, h)
bcI = isascalar(bc) ? bc[] : bc[I]
dest[I] = convert(eltype(dest), bcI)
end
end
return nothing
end

function fused_copyto!(
fmbc::FusedMultiBroadcast,
dest1::VIFH{S, Ni, A},
Expand Down Expand Up @@ -666,6 +695,17 @@ function fused_copyto!(
return nothing
end

function fused_copyto!(
fmbc::FusedMultiBroadcast,
dest::DataF{S},
::ClimaComms.AbstractCPUDevice,
) where {S}
for (dest, bc) in fmbc.pairs
@inbounds dest[] = convert(S, bc[])
end
return dest
end

# we've already diagonalized dest, so we only need to make
# sure that all the broadcast axes are compatible.
# Logic here is similar to Base.Broadcast.instantiate
Expand Down
9 changes: 1 addition & 8 deletions src/Fields/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,7 @@ function Base.copyto!(
fmb_data = FusedMultiBroadcast(
map(fmbc.pairs) do pair
bc = Base.Broadcast.instantiate(todata(pair.second))
bc′ = if isascalar(bc)
Base.Broadcast.instantiate(
Base.Broadcast.Broadcasted(bc.style, bc.f, bc.args, ()),
)
else
bc
end
Pair(field_values(pair.first), bc′)
Pair(field_values(pair.first), bc)
end,
)
check_mismatched_spaces(fmbc)
Expand Down
78 changes: 72 additions & 6 deletions test/Fields/field_multi_broadcast_fusion.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#=
julia --check-bounds=yes --project=test
julia -g2 --check-bounds=yes --project=test
julia --project=test
using Revise; include(joinpath("test", "Fields", "field_multi_broadcast_fusion.jl"))
=#
Expand All @@ -12,7 +13,7 @@ using OrderedCollections
using StaticArrays, IntervalSets
import ClimaCore
import ClimaCore.Utilities: PlusHalf
import ClimaCore.DataLayouts: IJFH
import ClimaCore.DataLayouts: IJFH, VF, DataF
import ClimaCore.DataLayouts
import ClimaCore:
Fields,
Expand All @@ -39,6 +40,8 @@ if !(@isdefined(TU))
import .TestUtilities as TU
end

@show ClimaComms.device()

function CenterExtrudedFiniteDifferenceSpaceLineHSpace(
::Type{FT};
zelem = 10,
Expand Down Expand Up @@ -174,6 +177,11 @@ function rand_field!(f)
parent(f) .= map(x -> rand(), parent(f))
return f
end
struct SomeData{FT}
a::FT
b::FT
c::FT
end

@testset "FusedMultiBroadcast - restrict to only similar fields" begin
FT = Float64
Expand All @@ -197,11 +205,6 @@ end
nothing
end

struct SomeData{FT}
a::FT
b::FT
c::FT
end
@testset "FusedMultiBroadcast - restrict to only similar broadcast types" begin
FT = Float64
dev = ClimaComms.device()
Expand Down Expand Up @@ -312,3 +315,66 @@ end
nothing
end
end

@testset "FusedMultiBroadcast IJFH" begin
FT = Float64
device = ClimaComms.device()
ArrayType = device isa ClimaComms.CUDADevice ? CuArray : Array
sem_space =
TU.SphereSpectralElementSpace(FT; context = ClimaComms.context(device))
IJFH_data() = Fields.Field(FT, sem_space)
X = Fields.FieldVector(;
x1 = IJFH_data(),
x2 = IJFH_data(),
x3 = IJFH_data(),
)
Y = Fields.FieldVector(;
y1 = IJFH_data(),
y2 = IJFH_data(),
y3 = IJFH_data(),
)
test_kernel!(; fused!, unfused!, X, Y)
benchmark_kernel!(unfused!, X, Y)
benchmark_kernel!(fused!, X, Y)
nothing
end

@testset "FusedMultiBroadcast VF" begin
FT = Float64
device = ClimaComms.device()
ArrayType = device isa ClimaComms.CUDADevice ? CuArray : Array
colspace = TU.ColumnCenterFiniteDifferenceSpace(
FT;
zelem = 3,
context = ClimaComms.context(device),
)
VF_data() = Fields.Field(FT, colspace)

X = Fields.FieldVector(; x1 = VF_data(), x2 = VF_data(), x3 = VF_data())
Y = Fields.FieldVector(; y1 = VF_data(), y2 = VF_data(), y3 = VF_data())
test_kernel!(; fused!, unfused!, X, Y)
benchmark_kernel!(unfused!, X, Y)
benchmark_kernel!(fused!, X, Y)
nothing
end

@testset "FusedMultiBroadcast DataF" begin
FT = Float64
device = ClimaComms.device()
ArrayType = device isa ClimaComms.CUDADevice ? CuArray : Array
DataF_data() = DataF{FT}(ArrayType(ones(FT, 2)))
X = Fields.FieldVector(;
x1 = DataF_data(),
x2 = DataF_data(),
x3 = DataF_data(),
)
Y = Fields.FieldVector(;
y1 = DataF_data(),
y2 = DataF_data(),
y3 = DataF_data(),
)
test_kernel!(; fused!, unfused!, X, Y)
benchmark_kernel!(unfused!, X, Y)
benchmark_kernel!(fused!, X, Y)
nothing
end

2 comments on commit b1fc161

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

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.14.4 -m "<description of version>" b1fc1614a4c5cf6d5a4cddf047be1be37e6d1508
git push origin v0.14.4

Please sign in to comment.