Skip to content

Commit

Permalink
Merge #377
Browse files Browse the repository at this point in the history
377: Add some `@inbounds` r=charleskawczynski a=charleskawczynski

This PR sprinkles some ``@inbounds`` in some loops.

Co-authored-by: Charles Kawczynski <kawczynski.charles@gmail.com>
  • Loading branch information
bors[bot] and charleskawczynski authored Jun 26, 2023
2 parents aef9e49 + 37af9ab commit c87476a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RRTMGP"
uuid = "a01a1ee8-cea4-48fc-987c-fc7878d79da1"
authors = ["Climate Modeling Alliance"]
version = "0.9.0"
version = "0.9.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
18 changes: 9 additions & 9 deletions src/optics/CloudOptics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ function pade_eval(ibnd, re, irad, m, n, pade_coeffs, irgh::Union{Int, Nothing}
end

denom = coeffs[ibnd, irad, n + m]
for i in (n + m - 1):-1:(1 + m)
@inbounds for i in (n + m - 1):-1:(1 + m)
denom = coeffs[ibnd, irad, i] + re * denom
end
denom = FT(1) + re * denom

numer = coeffs[ibnd, irad, m]
for i in (m - 1):-1:2
@inbounds for i in (m - 1):-1:2
numer = coeffs[ibnd, irad, i] + re * numer
end
numer = coeffs[ibnd, irad, 1] + re * numer
Expand All @@ -214,40 +214,40 @@ function build_cloud_mask!(cld_mask, cld_frac, random_arr, gcol, ::MaxRandomOver

Random.rand!(local_rand)
start = 0
for ilay in 1:nlay # for GPU compatibility (start = findfirst(local_cld_frac .> FT(0)))
@inbounds for ilay in 1:nlay # for GPU compatibility (start = findfirst(local_cld_frac .> FT(0)))
if local_cld_frac[ilay] > FT(0)
start = ilay
break
end
end

if start == 0 # no clouds in entire column
for ilay in 1:nlay, igpt in 1:ngpt
@inbounds for ilay in 1:nlay, igpt in 1:ngpt
local_cld_mask[igpt, ilay] = false
end
else
# finish = findlast(local_cld_frac .> FT(0)) # for GPU compatibility
finish = start
for ilay in nlay:-1:(start + 1)
@inbounds for ilay in nlay:-1:(start + 1)
if local_cld_frac[ilay] > FT(0)
finish = ilay
break
end
end
# set cloud mask for non-cloudy layers
for ilay in 1:(start - 1), igpt in 1:ngpt
@inbounds for ilay in 1:(start - 1), igpt in 1:ngpt
local_cld_mask[igpt, ilay] = false
end
for ilay in (finish + 1):nlay, igpt in 1:ngpt
@inbounds for ilay in (finish + 1):nlay, igpt in 1:ngpt
local_cld_mask[igpt, ilay] = false
end
# set cloud mask for cloudy layers
# last layer
# RRTMG uses local_rand[igpt, finish] > (FT(1) - local_cld_frac[finish]), we change > to >= to address edge cases
for igpt in 1:ngpt
@inbounds for igpt in 1:ngpt
local_cld_mask[igpt, finish] = local_rand[igpt, finish] >= (FT(1) - local_cld_frac[finish])
end
for ilay in (finish - 1):-1:start
@inbounds for ilay in (finish - 1):-1:start
if local_cld_frac[ilay] > FT(0)
for igpt in 1:ngpt
if local_cld_mask[igpt, ilay + 1]
Expand Down
4 changes: 2 additions & 2 deletions src/optics/GrayAtmosphericStates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function setup_gray_as_alt_grid(
pts = (pts .+ FT(1)) ./ FT(2)
Δz_cell = zend / ncls

for icol in 1:ncol
@inbounds for icol in 1:ncol
#---------bot_at_1---------------------------------------
z_lev[1, icol] = zst
z_lev[nlev, icol] = zend
Expand Down Expand Up @@ -277,7 +277,7 @@ function setup_gray_as_pr_grid_kernel!(
t_lev[1, gcol] = tt * (FT(1) + d0[gcol] * (p_lev[1, gcol] / p0)^α)^FT(0.25)
z_lev[1, gcol] = FT(0)

for ilay in 1:nlay
@inbounds for ilay in 1:nlay
# if step == "linear"
p_lev[ilay + 1, gcol] = p_lev[ilay, gcol] - Δp
# else
Expand Down
36 changes: 18 additions & 18 deletions src/optics/LookUpTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function LookUpLW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
idx_scaling_gas_lower = zeros(Int, n_min_absrb_lower)
idx_scaling_gas_upper = zeros(Int, n_min_absrb_upper)

for igas in 1:n_maj_absrb
@inbounds for igas in 1:n_maj_absrb
gases_major[igas] = strip(String(ds["gas_names"][:, igas]))
idx_gases[gases_major[igas]] = Int(igas)
end
Expand All @@ -198,12 +198,12 @@ function LookUpLW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}

n_gases = n_maj_absrb

for igas in 1:n_min_absrb
@inbounds for igas in 1:n_min_absrb
gases_minor[igas] = strip(String(ds["gas_minor"][:, igas]))
id_minor[igas] = strip(String(ds["identifier_minor"][:, igas]))
end

for igas in 1:n_min_absrb_lower
@inbounds for igas in 1:n_min_absrb_lower
gases_minor_lower[igas] = strip(String(ds["minor_gases_lower"][:, igas]))
scaling_gas_lower[igas] = strip(String(ds["scaling_gas_lower"][:, igas]))
if ~isempty(gases_minor_lower[igas])
Expand All @@ -216,7 +216,7 @@ function LookUpLW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
idx_gases_minor_lower = DA(idx_gases_minor_lower)
idx_scaling_gas_lower = DA(idx_scaling_gas_lower)

for igas in 1:n_min_absrb_upper
@inbounds for igas in 1:n_min_absrb_upper
gases_minor_upper[igas] = strip(String(ds["minor_gases_upper"][:, igas]))
scaling_gas_upper[igas] = strip(String(ds["scaling_gas_upper"][:, igas]))
if ~isempty(gases_minor_upper[igas])
Expand All @@ -230,7 +230,7 @@ function LookUpLW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
idx_scaling_gas_upper = DA(idx_scaling_gas_upper)

key_species = ds["key_species"][:]
for j in 1:size(key_species, 3)
@inbounds for j in 1:size(key_species, 3)
for i in 1:size(key_species, 2)
if key_species[1, i, j] == 0 && key_species[2, i, j] == 0
key_species[1:2, i, j] .= 2
Expand All @@ -254,7 +254,7 @@ function LookUpLW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
bnd_lims_wn = FTA2D(ds["bnd_limits_wavenumber"][:])
#-----------------------
major_gpt2bnd = Array{UI8, 1}(undef, n_gpt)
for i in 1:n_bnd
@inbounds for i in 1:n_bnd
major_gpt2bnd[bnd_lims_gpt[1, i]:bnd_lims_gpt[2, i]] .= UI8(i)
end
#-----------------------
Expand All @@ -272,7 +272,7 @@ function LookUpLW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
minor_upper_gpt_sh = Array{Int, 1}(undef, n_min_absrb_upper)

minor_lower_gpt_sh[1] = 0
for i in 1:n_min_absrb_lower
@inbounds for i in 1:n_min_absrb_lower
minor_lower_bnd[i] = major_gpt2bnd[minor_lower_gpt_lims[1, i]]
if i > 1
minor_lower_gpt_sh[i] =
Expand All @@ -281,7 +281,7 @@ function LookUpLW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
end

minor_upper_gpt_sh[1] = 0
for i in 1:n_min_absrb_upper
@inbounds for i in 1:n_min_absrb_upper
minor_upper_bnd[i] = major_gpt2bnd[minor_upper_gpt_lims[1, i]]
if i > 1
minor_upper_gpt_sh[i] =
Expand All @@ -292,7 +292,7 @@ function LookUpLW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
minor_lower_bnd_st[1] = 1
minor_upper_bnd_st[1] = 1

for ibnd in 2:(n_bnd + 1)
@inbounds for ibnd in 2:(n_bnd + 1)
loc_low = findlast(isequal(UI8(ibnd - 1)), minor_lower_bnd)
loc_upp = findlast(isequal(UI8(ibnd - 1)), minor_upper_bnd)
if isnothing(loc_low)
Expand Down Expand Up @@ -575,7 +575,7 @@ function LookUpSW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
idx_scaling_gas_lower = zeros(Int, n_min_absrb_lower)
idx_scaling_gas_upper = zeros(Int, n_min_absrb_upper)

for igas in 1:n_maj_absrb
@inbounds for igas in 1:n_maj_absrb
gases_major[igas] = strip(String(ds["gas_names"][:, igas]))
idx_gases[gases_major[igas]] = Int(igas)
end
Expand All @@ -586,12 +586,12 @@ function LookUpSW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}

n_gases = n_maj_absrb

for igas in 1:n_min_absrb
@inbounds for igas in 1:n_min_absrb
gases_minor[igas] = strip(String(ds["gas_minor"][:, igas]))
id_minor[igas] = strip(String(ds["identifier_minor"][:, igas]))
end

for igas in 1:n_min_absrb_lower
@inbounds for igas in 1:n_min_absrb_lower
gases_minor_lower[igas] = strip(String(ds["minor_gases_lower"][:, igas]))
scaling_gas_lower[igas] = strip(String(ds["scaling_gas_lower"][:, igas]))
if ~isempty(gases_minor_lower[igas])
Expand All @@ -602,7 +602,7 @@ function LookUpSW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
end
end

for igas in 1:n_min_absrb_upper
@inbounds for igas in 1:n_min_absrb_upper
gases_minor_upper[igas] = strip(String(ds["minor_gases_upper"][:, igas]))
scaling_gas_upper[igas] = strip(String(ds["scaling_gas_upper"][:, igas]))
if ~isempty(gases_minor_upper[igas])
Expand All @@ -614,7 +614,7 @@ function LookUpSW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
end

key_species = ds["key_species"][:]
for j in 1:size(key_species, 3)
@inbounds for j in 1:size(key_species, 3)
for i in 1:size(key_species, 2)
if key_species[1, i, j] == 0 && key_species[2, i, j] == 0
key_species[1:2, i, j] .= 2
Expand All @@ -634,7 +634,7 @@ function LookUpSW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
bnd_lims_wn = FTA2D(ds["bnd_limits_wavenumber"][:])
#-----------------------
major_gpt2bnd = Array{UI8, 1}(undef, n_gpt)
for i in 1:n_bnd
@inbounds for i in 1:n_bnd
major_gpt2bnd[bnd_lims_gpt[1, i]:bnd_lims_gpt[2, i]] .= UI8(i)
end
#-----------------------
Expand All @@ -652,7 +652,7 @@ function LookUpSW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
minor_upper_gpt_sh = Array{Int, 1}(undef, n_min_absrb_upper)

minor_lower_gpt_sh[1] = 0
for i in 1:n_min_absrb_lower
@inbounds for i in 1:n_min_absrb_lower
minor_lower_bnd[i] = major_gpt2bnd[minor_lower_gpt_lims[1, i]]
if i > 1
minor_lower_gpt_sh[i] =
Expand All @@ -661,7 +661,7 @@ function LookUpSW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
end

minor_upper_gpt_sh[1] = 0
for i in 1:n_min_absrb_upper
@inbounds for i in 1:n_min_absrb_upper
minor_upper_bnd[i] = major_gpt2bnd[minor_upper_gpt_lims[1, i]]
if i > 1
minor_upper_gpt_sh[i] =
Expand All @@ -672,7 +672,7 @@ function LookUpSW(ds, ::Type{FT}, ::Type{DA}) where {FT <: AbstractFloat, DA}
minor_lower_bnd_st[1] = 1
minor_upper_bnd_st[1] = 1

for ibnd in 2:(n_bnd + 1)
@inbounds for ibnd in 2:(n_bnd + 1)
loc_low = findlast(isequal(UI8(ibnd - 1)), minor_lower_bnd)
loc_upp = findlast(isequal(UI8(ibnd - 1)), minor_upper_bnd)
if isnothing(loc_low)
Expand Down
8 changes: 4 additions & 4 deletions src/optics/Optics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function compute_optical_props!(
lkp_cld::Union{AbstractLookUp, Nothing} = nothing,
) where {FT <: AbstractFloat}
nlay = as.nlay
for ilay in 1:nlay
@inbounds for ilay in 1:nlay
if lkp_cld isa Nothing
compute_optical_props_kernel!(op, as, ilay, gcol, sf, igpt, lkp)
else
Expand Down Expand Up @@ -197,7 +197,7 @@ function compute_optical_props!(
lkp_cld::Union{AbstractLookUp, Nothing} = nothing,
) where {FT <: AbstractFloat}
nlay = as.nlay
for ilay in 1:nlay
@inbounds for ilay in 1:nlay
if lkp_cld isa Nothing
compute_optical_props_kernel!(op, as, ilay, gcol, igpt, lkp)
else
Expand Down Expand Up @@ -228,7 +228,7 @@ function compute_optical_props!(
lkp_cld::Union{AbstractLookUp, Nothing} = nothing,
) where {FT <: AbstractFloat}
nlay = as.nlay
for ilay in 1:nlay
@inbounds for ilay in 1:nlay
compute_optical_props_kernel!(op, as, ilay, gcol, sf)
end
return nothing
Expand All @@ -253,7 +253,7 @@ function compute_optical_props!(
lkp_cld::Union{AbstractLookUp, Nothing} = nothing,
) where {FT <: AbstractFloat}
nlay = as.nlay
for ilay in 1:nlay
@inbounds for ilay in 1:nlay
compute_optical_props_kernel!(op, as, ilay, gcol)
end
return nothing
Expand Down
8 changes: 4 additions & 4 deletions src/rte/RTESolver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function rte_lw_noscat_solve_CUDA!(
nlev = nlay + 1
n_gpt = length(major_gpt2bnd)
if gcol ncol
for igpt in 1:n_gpt
@inbounds for igpt in 1:n_gpt
igpt == 1 && set_flux_to_zero!(flux_lw, gcol)
compute_optical_props!(op, as, src_lw, gcol, igpt, lookup_lw, lookup_lw_cld)
rte_lw_noscat_source!(src_lw, op, gcol, nlay)
Expand Down Expand Up @@ -229,7 +229,7 @@ function rte_lw_2stream_solve_CUDA!(
if as isa AtmosphericState && as.cld_mask_type isa AbstractCloudMask
Optics.build_cloud_mask!(as.cld_mask_lw, as.cld_frac, as.random_lw, gcol, as.cld_mask_type)
end
for igpt in 1:n_gpt
@inbounds for igpt in 1:n_gpt
igpt == 1 && set_flux_to_zero!(flux_lw, gcol)
compute_optical_props!(op, as, src_lw, gcol, igpt, lookup_lw, lookup_lw_cld)
rte_lw_2stream_combine_sources!(src_lw, gcol, nlev, ncol)
Expand Down Expand Up @@ -377,7 +377,7 @@ function rte_sw_noscat_solve_CUDA!(
n_gpt = length(solar_src_scaled)
# setting references for flux_sw
if gcol ncol
for igpt in 1:n_gpt
@inbounds for igpt in 1:n_gpt
igpt == 1 && set_flux_to_zero!(flux_sw, gcol)
compute_optical_props!(op, as, gcol, igpt, lookup_sw, lookup_sw_cld)
rte_sw_noscat_solve_kernel!(flux, op, bcs_sw, igpt, solar_src_scaled, gcol, nlev)
Expand Down Expand Up @@ -485,7 +485,7 @@ function rte_sw_2stream_solve_CUDA!(
if as isa AtmosphericState && as.cld_mask_type isa AbstractCloudMask
Optics.build_cloud_mask!(as.cld_mask_sw, as.cld_frac, as.random_sw, gcol, as.cld_mask_type)
end
for igpt in 1:n_gpt
@inbounds for igpt in 1:n_gpt
igpt == 1 && set_flux_to_zero!(flux_sw, gcol)
compute_optical_props!(op, as, gcol, igpt, lookup_sw, lookup_sw_cld)
sw_two_stream!(op, src_sw, bcs_sw, gcol, nlay) # Cell properties: transmittance and reflectance for direct and diffuse radiation
Expand Down

2 comments on commit c87476a

@charleskawczynski
Copy link
Member

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/86242

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

Please sign in to comment.