Skip to content

Implement data-specific cartesian index #1900

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions ext/ClimaCoreCUDAExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import ClimaCore.Utilities: cart_ind, linear_ind
import ClimaCore.RecursiveApply:
⊠, ⊞, ⊟, radd, rmul, rsub, rdiv, rmap, rzero, rmin, rmax
import ClimaCore.DataLayouts: get_N, get_Nv, get_Nij, get_Nij, get_Nh
import ClimaCore.DataLayouts: DataSpecificCartesianIndex, array_size

include(joinpath("cuda", "cuda_utils.jl"))
include(joinpath("cuda", "data_layouts.jl"))
Expand Down
5 changes: 3 additions & 2 deletions ext/cuda/data_layouts_fill.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ function knl_fill_flat!(dest::AbstractData, val, us)
@inbounds begin
tidx = thread_index()
if tidx ≤ get_N(us)
n = size(dest)
I = kernel_indexes(tidx, n)
n = array_size(dest)
CIS = CartesianIndices(map(x -> Base.OneTo(x), n))
I = DataSpecificCartesianIndex(CIS[tidx])
@inbounds dest[I] = val
end
end
Expand Down
13 changes: 13 additions & 0 deletions src/DataLayouts/DataLayouts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@ function VIJFH{S, Nv, Nij, Nh}(
array::AbstractArray{T, 5},
) where {S, Nv, Nij, Nh, T}
check_basetype(T, S)
@assert size(array, 1) == Nv
@assert size(array, 2) == size(array, 3) == Nij
@assert size(array, 4) == typesize(T, S)
@assert size(array, 5) == Nh
Expand Down Expand Up @@ -1271,6 +1272,7 @@ function VIFH{S, Nv, Ni, Nh}(
array::AbstractArray{T, 4},
) where {S, Nv, Ni, Nh, T}
check_basetype(T, S)
@assert size(array, 1) == Nv
@assert size(array, 2) == Ni
@assert size(array, 3) == typesize(T, S)
@assert size(array, 4) == Nh
Expand Down Expand Up @@ -1568,6 +1570,16 @@ get_Nij(::IFH{S, Nij}) where {S, Nij} = Nij
get_Nij(::IJF{S, Nij}) where {S, Nij} = Nij
get_Nij(::IF{S, Nij}) where {S, Nij} = Nij

@inline field_dim(::IJKFVH) = 4
@inline field_dim(::IJFH) = 3
@inline field_dim(::IFH) = 2
@inline field_dim(::DataF) = 1
@inline field_dim(::IJF) = 3
@inline field_dim(::IF) = 2
@inline field_dim(::VF) = 2
@inline field_dim(::VIJFH) = 4
@inline field_dim(::VIFH) = 3

Base.ndims(data::AbstractData) = Base.ndims(typeof(data))
Base.ndims(::Type{T}) where {T <: AbstractData} =
Base.ndims(parent_array_type(T))
Expand Down Expand Up @@ -1641,5 +1653,6 @@ include("copyto.jl")
include("fused_copyto.jl")
include("fill.jl")
include("mapreduce.jl")
include("cartesian_index.jl")

end # module
46 changes: 46 additions & 0 deletions src/DataLayouts/cartesian_index.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
abstract type AbstractDataSpecificCartesianIndex{N} <:
Base.AbstractCartesianIndex{N} end

struct DataSpecificCartesianIndex{N} <: AbstractDataSpecificCartesianIndex{N}
I::CartesianIndex{N}
end

# Generic fallback
@propagate_inbounds Base.getindex(x, I::DataSpecificCartesianIndex) =
Base.getindex(x, I.I)

@propagate_inbounds Base.setindex!(x, val, I::DataSpecificCartesianIndex) =
Base.setindex!(x, val, I.I)

# Datalayouts
@propagate_inbounds function Base.getindex(
data::AbstractData{S},
I::DataSpecificCartesianIndex,
) where {S}
@inbounds get_struct(parent(data), S, Val(field_dim(data)), I.I)
end
@propagate_inbounds function Base.setindex!(
data::AbstractData{S},
val,
I::DataSpecificCartesianIndex,
) where {S}
@inbounds set_struct!(
parent(data),
convert(S, val),
Val(field_dim(data)),
I.I,
)
end

@inline array_size(::IJKFVH{S, Nij, Nk, Nv, Nh}) where {S, Nij, Nk, Nv, Nh} =
(Nij, Nij, Nk, 1, Nv, Nh)
@inline array_size(::IJFH{S, Nij, Nh}) where {S, Nij, Nh} = (Nij, Nij, 1, Nh)
@inline array_size(::IFH{S, Ni, Nh}) where {S, Ni, Nh} = (Ni, 1, Nh)
@inline array_size(::DataF{S}) where {S} = (1,)
@inline array_size(::IJF{S, Nij}) where {S, Nij} = (Nij, Nij, 1)
@inline array_size(::IF{S, Ni}) where {S, Ni} = (Ni, 1)
@inline array_size(::VF{S, Nv}) where {S, Nv} = (Nv, 1)
@inline array_size(::VIJFH{S, Nv, Nij, Nh}) where {S, Nv, Nij, Nh} =
(Nv, Nij, Nij, 1, Nh)
@inline array_size(::VIFH{S, Nv, Ni, Nh}) where {S, Nv, Ni, Nh} =
(Nv, Ni, 1, Nh)
Loading