Skip to content

Fix unrolling over field broadcast arguments #2350

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

Merged
merged 1 commit into from
Jun 28, 2025
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 src/Fields/Fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import ..Utilities: PlusHalf, half
using ..RecursiveApply
using ClimaComms
import Adapt
import UnrolledUtilities: unrolled_map, unrolled_findfirst
import UnrolledUtilities: unrolled_map, unrolled_mapreduce, unrolled_findfirst

import StaticArrays, LinearAlgebra, Statistics, InteractiveUtils

Expand Down
14 changes: 14 additions & 0 deletions src/Fields/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ Base.Broadcast.BroadcastStyle(
::FieldStyle{DS2},
) where {DS1, DS2} = FieldStyle(Base.Broadcast.BroadcastStyle(DS1(), DS2()))

# Override the recursive unrolling used in combine_styles (which can lead to
# inference failures in broadcast expressions with more than 10 arguments) with
# manual unrolling (which can have higher latency but is always inferrable).
Base.Broadcast.combine_styles(
arg1::Union{Field, Base.Broadcast.Broadcasted{<:AbstractFieldStyle}},
arg2,
arg3,
args...,
) = unrolled_mapreduce(
Base.Broadcast.combine_styles,
Base.Broadcast.result_style,
(arg1, arg2, arg3, args...),
)

Base.Broadcast.broadcastable(field::Field) = field

Base.eltype(bc::Base.Broadcast.Broadcasted{<:AbstractFieldStyle}) =
Expand Down
Loading