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

Simplify arguments for solve_lw! and solve_sw! #493

Merged
merged 1 commit into from
Jun 11, 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
10 changes: 10 additions & 0 deletions ext/RRTMGPCUDAExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ import RRTMGP.RTESolver: rte_sw_noscat!
import RRTMGP.RTESolver: rte_sw_noscat_solve!
import CUDA: threadIdx, blockIdx, blockDim, @cuda

_max_threads_cuda() = 256

function _configure_threadblock(max_threads, nitems)
nthreads = min(max_threads, nitems)
nblocks = cld(nitems, nthreads)
return (nthreads, nblocks)
end

_configure_threadblock(nitems) = _configure_threadblock(_max_threads_cuda(), nitems)

include(joinpath("cuda", "gray_atmospheric_states.jl"))
include(joinpath("cuda", "rte_longwave_2stream.jl"))
include(joinpath("cuda", "optics.jl"))
Expand Down
4 changes: 1 addition & 3 deletions ext/cuda/gray_atmospheric_states.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

function setup_gray_as_pr_grid!(::ClimaComms.CUDADevice, ncol, args...)
max_threads = 256
tx = min(ncol, max_threads)
bx = cld(ncol, tx)
tx, bx = _configure_threadblock(ncol)
@cuda always_inline = true threads = (tx) blocks = (bx) _setup_gray_as_pr_grid_kernel!(ncol, args...)
end

Expand Down
4 changes: 1 addition & 3 deletions ext/cuda/optics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ function compute_col_gas!(
param_set::RP.ARP,
vmr_h2o::Union{AbstractArray{FT, 2}, Nothing} = nothing,
lat::Union{AbstractArray{FT, 1}, Nothing} = nothing,
max_threads::Int = Int(256),
) where {FT}
nlay, ncol = size(col_dry)
mol_m_dry = RP.molmass_dryair(param_set)
mol_m_h2o = RP.molmass_water(param_set)
avogadro = RP.avogad(param_set)
helmert1 = RP.grav(param_set)
args = (p_lev, mol_m_dry, mol_m_h2o, avogadro, helmert1, vmr_h2o, lat)
tx = min(nlay * ncol, max_threads)
bx = cld(nlay * ncol, tx)
tx, bx = _configure_threadblock(nlay * ncol)
@cuda always_inline = true threads = (tx) blocks = (bx) compute_col_gas_CUDA!(col_dry, args...)
return nothing
end
Expand Down
8 changes: 2 additions & 6 deletions ext/cuda/optics_gray_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ function update_profile_lw!(
nlev,
ncol,
)
max_threads = 256
tx = min(ncol, max_threads)
bx = cld(ncol, tx)
tx, bx = _configure_threadblock(ncol)
@cuda always_inline = true threads = (tx) blocks = (bx) _update_profile_lw_kernel!(
sbc,
ncol,
Expand All @@ -43,9 +41,7 @@ end

function compute_gray_heating_rate!(device::ClimaComms.CUDADevice, hr_lay, p_lev, ncol, nlay, flux_net, cp_d_, grav_)
args = (hr_lay, flux_net, p_lev, grav_, cp_d_, nlay)
max_threads = 256
tx = min(ncol, max_threads)
bx = cld(ncol, tx)
tx, bx = _configure_threadblock(ncol)
@cuda always_inline = true threads = (tx) blocks = (bx) compute_gray_heating_rate_CUDA!(ncol, args...)
return nothing
end
Expand Down
8 changes: 2 additions & 6 deletions ext/cuda/rte_longwave_1scalar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ function rte_lw_noscat_solve!(
src_lw::SourceLWNoScat,
bcs_lw::LwBCs,
op::OneScalar,
max_threads,
as::GrayAtmosphericState,
)
nlay, ncol = AtmosphericStates.get_dims(as)
nlev = nlay + 1
tx = min(ncol, max_threads)
bx = cld(ncol, tx)
tx, bx = _configure_threadblock(ncol)
args = (flux_lw, src_lw, bcs_lw, op, nlay, ncol, as)
@cuda always_inline = true threads = (tx) blocks = (bx) rte_lw_noscat_solve_CUDA!(args...)
return nothing
Expand Down Expand Up @@ -48,15 +46,13 @@ function rte_lw_noscat_solve!(
src_lw::SourceLWNoScat,
bcs_lw::LwBCs,
op::OneScalar,
max_threads,
as::AtmosphericState,
lookup_lw::LookUpLW,
lookup_lw_cld::Union{LookUpCld, PadeCld, Nothing} = nothing,
)
nlay, ncol = AtmosphericStates.get_dims(as)
nlev = nlay + 1
tx = min(ncol, max_threads)
bx = cld(ncol, tx)
tx, bx = _configure_threadblock(ncol)
args = (flux, flux_lw, src_lw, bcs_lw, op, nlay, ncol, as, lookup_lw, lookup_lw_cld)
@cuda always_inline = true threads = (tx) blocks = (bx) rte_lw_noscat_solve_CUDA!(args...)
return nothing
Expand Down
8 changes: 2 additions & 6 deletions ext/cuda/rte_longwave_2stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ function rte_lw_2stream_solve!(
src_lw::SourceLW2Str,
bcs_lw::LwBCs,
op::TwoStream,
max_threads,
as::GrayAtmosphericState,
)
nlay, ncol = AtmosphericStates.get_dims(as)
nlev = nlay + 1
tx = min(ncol, max_threads)
bx = cld(ncol, tx)
tx, bx = _configure_threadblock(ncol)
args = (flux_lw, src_lw, bcs_lw, op, nlay, ncol, as)
@cuda always_inline = true threads = (tx) blocks = (bx) rte_lw_2stream_solve_CUDA!(args...)
return nothing
Expand Down Expand Up @@ -48,15 +46,13 @@ function rte_lw_2stream_solve!(
src_lw::SourceLW2Str,
bcs_lw::LwBCs,
op::TwoStream,
max_threads,
as::AtmosphericState,
lookup_lw::LookUpLW,
lookup_lw_cld::Union{LookUpCld, PadeCld, Nothing} = nothing,
)
nlay, ncol = AtmosphericStates.get_dims(as)
nlev = nlay + 1
tx = min(ncol, max_threads)
bx = cld(ncol, tx)
tx, bx = _configure_threadblock(ncol)
args = (flux, flux_lw, src_lw, bcs_lw, op, nlay, ncol, as, lookup_lw, lookup_lw_cld)
@cuda always_inline = true threads = (tx) blocks = (bx) rte_lw_2stream_solve_CUDA!(args...)
return nothing
Expand Down
8 changes: 2 additions & 6 deletions ext/cuda/rte_shortwave_1scalar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ function rte_sw_noscat_solve!(
flux_sw::FluxSW,
op::OneScalar,
bcs_sw::SwBCs,
max_threads,
as::GrayAtmosphericState,
)
nlay, ncol = AtmosphericStates.get_dims(as)
nlev = nlay + 1
tx = min(ncol, max_threads)
bx = cld(ncol, tx)
tx, bx = _configure_threadblock(ncol)
args = (flux_sw, op, bcs_sw, nlay, ncol, as)
@cuda always_inline = true threads = (tx) blocks = (bx) rte_sw_noscat_solve_CUDA!(args...)
return nothing
Expand Down Expand Up @@ -44,14 +42,12 @@ function rte_sw_noscat_solve!(
flux_sw::FluxSW,
op::OneScalar,
bcs_sw::SwBCs,
max_threads,
as::AtmosphericState,
lookup_sw::LookUpSW,
)
nlay, ncol = AtmosphericStates.get_dims(as)
nlev = nlay + 1
tx = min(ncol, max_threads)
bx = cld(ncol, tx)
tx, bx = _configure_threadblock(ncol)
args = (flux, flux_sw, op, bcs_sw, nlay, ncol, as, lookup_sw)
@cuda always_inline = true threads = (tx) blocks = (bx) rte_sw_noscat_solve_CUDA!(args...)
return nothing
Expand Down
8 changes: 2 additions & 6 deletions ext/cuda/rte_shortwave_2stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ function rte_sw_2stream_solve!(
op::TwoStream,
bcs_sw::SwBCs,
src_sw::SourceSW2Str,
max_threads,
as::GrayAtmosphericState,
)
nlay, ncol = AtmosphericStates.get_dims(as)
nlev = nlay + 1
tx = min(ncol, max_threads)
bx = cld(ncol, tx)
tx, bx = _configure_threadblock(ncol)
args = (flux_sw, op, bcs_sw, src_sw, nlay, ncol, as)
@cuda always_inline = true threads = (tx) blocks = (bx) rte_sw_2stream_solve_CUDA!(args...)
return nothing
Expand Down Expand Up @@ -65,16 +63,14 @@ function rte_sw_2stream_solve!(
op::TwoStream,
bcs_sw::SwBCs,
src_sw::SourceSW2Str,
max_threads,
as::AtmosphericState,
lookup_sw::LookUpSW,
lookup_sw_cld::Union{LookUpCld, PadeCld, Nothing} = nothing,
)
nlay, ncol = AtmosphericStates.get_dims(as)
nlev = nlay + 1
n_gpt = length(lookup_sw.solar_src_scaled)
tx = min(ncol, max_threads)
bx = cld(ncol, tx)
tx, bx = _configure_threadblock(ncol)
args = (flux, flux_sw, op, bcs_sw, src_sw, nlay, ncol, as, lookup_sw, lookup_sw_cld)
@cuda always_inline = true threads = (tx) blocks = (bx) rte_sw_2stream_solve_CUDA!(args...)
return nothing
Expand Down
60 changes: 29 additions & 31 deletions perf/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@ import Logging
include(joinpath(root_dir, "test", "gray_atm_utils.jl"))
gray_atmos_lw_equil(ClimaComms.context(), OneScalar, NoScatLWRTE, FT; exfiltrate = true)
end
(; slv_lw, gray_as, max_threads) = Infiltrator.exfiltrated
(; slv_lw, gray_as) = Infiltrator.exfiltrated
@info "gray_atm lw"
solve_lw!(slv_lw, gray_as, max_threads) # compile first
solve_lw!(slv_lw, gray_as) # compile first
device = ClimaComms.device(ClimaComms.context())
trial = if device isa ClimaComms.CUDADevice
using CUDA
@benchmark CUDA.@sync solve_lw!($slv_lw, $gray_as, $max_threads)
@benchmark CUDA.@sync solve_lw!($slv_lw, $gray_as)
else
@benchmark solve_lw!($slv_lw, $gray_as, $max_threads)
@benchmark solve_lw!($slv_lw, $gray_as)
end
show(stdout, MIME("text/plain"), trial)
println()

gray_atmos_sw_test(ClimaComms.context(), OneScalar, NoScatSWRTE, FT, 1; exfiltrate = true)
(; slv_sw, as, max_threads) = Infiltrator.exfiltrated
solve_sw!(slv_sw, as, max_threads) # compile first
(; slv_sw, as) = Infiltrator.exfiltrated
solve_sw!(slv_sw, as) # compile first
@info "gray_atm sw"
device = ClimaComms.device(ClimaComms.context())
trial = if device isa ClimaComms.CUDADevice
using CUDA
@benchmark CUDA.@sync solve_sw!($slv_sw, $as, $max_threads)
@benchmark CUDA.@sync solve_sw!($slv_sw, $as)
else
@benchmark solve_sw!($slv_sw, $as, $max_threads)
@benchmark solve_sw!($slv_sw, $as)
end
show(stdout, MIME("text/plain"), trial)
println()
Expand All @@ -51,30 +51,28 @@ include(joinpath(root_dir, "test", "clear_sky_utils.jl"))
context = ClimaComms.context()
clear_sky(ClimaComms.context(), TwoStream, TwoStream, TwoStreamLWRTE, TwoStreamSWRTE, VmrGM, FT; exfiltrate = true)
# end
(; slv_lw, slv_sw, as, max_threads, lookup_sw, lookup_lw) = Infiltrator.exfiltrated
(; slv_lw, slv_sw, as, lookup_sw, lookup_lw) = Infiltrator.exfiltrated

@info "clear_sky lw"
lookup_lw_cld = nothing
solve_lw!(slv_lw, as, max_threads, lookup_lw, lookup_lw_cld) # compile first
solve_lw!(slv_lw, as, lookup_lw) # compile first
device = ClimaComms.device(ClimaComms.context())
trial = if device isa ClimaComms.CUDADevice
using CUDA
@benchmark CUDA.@sync solve_lw!($slv_lw, $as, $max_threads, $lookup_lw, $lookup_lw_cld)
@benchmark CUDA.@sync solve_lw!($slv_lw, $as, $lookup_lw)
else
@benchmark solve_lw!($slv_lw, $as, $max_threads, $lookup_lw, $lookup_lw_cld)
@benchmark solve_lw!($slv_lw, $as, $lookup_lw)
end
show(stdout, MIME("text/plain"), trial)
println()

@info "clear_sky sw"
lookup_sw_cld = nothing
solve_sw!(slv_sw, as, max_threads, lookup_sw, lookup_sw_cld) # compile first
solve_sw!(slv_sw, as, lookup_sw) # compile first
device = ClimaComms.device(ClimaComms.context())
trial = if device isa ClimaComms.CUDADevice
using CUDA
@benchmark CUDA.@sync solve_sw!($slv_sw, $as, $max_threads, $lookup_sw, $lookup_sw_cld)
@benchmark CUDA.@sync solve_sw!($slv_sw, $as, $lookup_sw)
else
@benchmark solve_sw!($slv_sw, $as, $max_threads, $lookup_sw, $lookup_sw_cld)
@benchmark solve_sw!($slv_sw, $as, $lookup_sw)
end
show(stdout, MIME("text/plain"), trial)
println()
Expand All @@ -95,28 +93,28 @@ all_sky(
)
# end

(; slv_lw, slv_sw, as, max_threads, lookup_sw, lookup_sw_cld, lookup_lw, lookup_lw_cld) = Infiltrator.exfiltrated
(; slv_lw, slv_sw, as, lookup_sw, lookup_sw_cld, lookup_lw, lookup_lw_cld) = Infiltrator.exfiltrated

solve_sw!(slv_sw, as, max_threads, lookup_sw, lookup_sw_cld) # compile first
solve_lw!(slv_lw, as, max_threads, lookup_lw, lookup_lw_cld) # compile first
solve_sw!(slv_sw, as, lookup_sw, lookup_sw_cld) # compile first
solve_lw!(slv_lw, as, lookup_lw, lookup_lw_cld) # compile first

@info "all_sky, lw, use_lut=true"
device = ClimaComms.device(ClimaComms.context())
trial = if device isa ClimaComms.CUDADevice
using CUDA
@benchmark CUDA.@sync solve_lw!($slv_lw, $as, $max_threads, $lookup_lw, $lookup_lw_cld)
@benchmark CUDA.@sync solve_lw!($slv_lw, $as, $lookup_lw, $lookup_lw_cld)
else
@benchmark solve_lw!($slv_lw, $as, $max_threads, $lookup_lw, $lookup_lw_cld)
@benchmark solve_lw!($slv_lw, $as, $lookup_lw, $lookup_lw_cld)
end
show(stdout, MIME("text/plain"), trial)
println()
@info "all_sky, sw, use_lut=true"
device = ClimaComms.device(ClimaComms.context())
trial = if device isa ClimaComms.CUDADevice
using CUDA
@benchmark CUDA.@sync solve_sw!($slv_sw, $as, $max_threads, $lookup_sw, $lookup_sw_cld)
@benchmark CUDA.@sync solve_sw!($slv_sw, $as, $lookup_sw, $lookup_sw_cld)
else
@benchmark solve_sw!($slv_sw, $as, $max_threads, $lookup_sw, $lookup_sw_cld)
@benchmark solve_sw!($slv_sw, $as, $lookup_sw, $lookup_sw_cld)
end
show(stdout, MIME("text/plain"), trial)
println()
Expand All @@ -135,28 +133,28 @@ all_sky(
)
# end

(; slv_lw, slv_sw, as, max_threads, lookup_sw, lookup_sw_cld, lookup_lw, lookup_lw_cld) = Infiltrator.exfiltrated
(; slv_lw, slv_sw, as, lookup_sw, lookup_sw_cld, lookup_lw, lookup_lw_cld) = Infiltrator.exfiltrated

solve_sw!(slv_sw, as, max_threads, lookup_sw, lookup_sw_cld) # compile first
solve_lw!(slv_lw, as, max_threads, lookup_lw, lookup_lw_cld) # compile first
solve_sw!(slv_sw, as, lookup_sw, lookup_sw_cld) # compile first
solve_lw!(slv_lw, as, lookup_lw, lookup_lw_cld) # compile first

@info "all_sky, lw, use_lut=false"
device = ClimaComms.device(ClimaComms.context())
trial = if device isa ClimaComms.CUDADevice
using CUDA
@benchmark CUDA.@sync solve_lw!($slv_lw, $as, $max_threads, $lookup_lw, $lookup_lw_cld)
@benchmark CUDA.@sync solve_lw!($slv_lw, $as, $lookup_lw, $lookup_lw_cld)
else
@benchmark solve_lw!($slv_lw, $as, $max_threads, $lookup_lw, $lookup_lw_cld)
@benchmark solve_lw!($slv_lw, $as, $lookup_lw, $lookup_lw_cld)
end
show(stdout, MIME("text/plain"), trial)
println()
@info "all_sky, sw, use_lut=false"
device = ClimaComms.device(ClimaComms.context())
trial = if device isa ClimaComms.CUDADevice
using CUDA
@benchmark CUDA.@sync solve_sw!($slv_sw, $as, $max_threads, $lookup_sw, $lookup_sw_cld)
@benchmark CUDA.@sync solve_sw!($slv_sw, $as, $lookup_sw, $lookup_sw_cld)
else
@benchmark solve_sw!($slv_sw, $as, $max_threads, $lookup_sw, $lookup_sw_cld)
@benchmark solve_sw!($slv_sw, $as, $lookup_sw, $lookup_sw_cld)
end
show(stdout, MIME("text/plain"), trial)
println()
Loading
Loading