Skip to content

Commit 4ca5ba8

Browse files
committed
Add support for nonuniform data structures
1 parent 419f7fd commit 4ca5ba8

File tree

6 files changed

+295
-395
lines changed

6 files changed

+295
-395
lines changed

src/DataLayouts/DataLayouts.jl

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,11 @@ module DataLayouts
6666
import Base: Base, @propagate_inbounds
6767
import StaticArrays: SOneTo, MArray, SArray
6868
import ClimaComms
69-
import UnrolledUtilities: unrolled_map, unrolled_all
7069
import MultiBroadcastFusion as MBF
7170
import Adapt
72-
import UnrolledUtilities: unrolled_foreach, unrolled_all, unrolled_findfirst
71+
using UnrolledUtilities
7372

74-
import ..Utilities: PlusHalf, unionall_type
73+
import ..Utilities: PlusHalf, unionall_type, replace_type_parameter
7574
import ..DebugOnly: call_post_op_callback, post_op_callback
7675
import ..slab, ..slab_args, ..column, ..column_args, ..level, ..level_args
7776
export slab,
@@ -131,6 +130,7 @@ struct IJHMask{B, NT, V} <: AbstractMask
131130
end
132131

133132
include("struct.jl")
133+
include("parent_arrays.jl")
134134

135135
abstract type AbstractData{S} end
136136

@@ -395,7 +395,7 @@ end
395395

396396
function replace_basetype(data::AbstractData{S}, ::Type{T}) where {S, T}
397397
array = parent(data)
398-
S′ = replace_basetype(eltype(array), T, S)
398+
S′ = replace_type_parameter(S, eltype(array), T)
399399
return union_all(singleton(data)){S′, Base.tail(type_params(data))...}(
400400
similar(array, T),
401401
)
@@ -878,17 +878,9 @@ function DataF{S}(::Type{ArrayType}, fun = similar;) where {S, ArrayType}
878878
end
879879

880880
function DataF(x::T) where {T}
881-
if is_valid_basetype(Float64, T)
882-
d = DataF{T}(Array{Float64})
883-
d[] = x
884-
return d
885-
elseif is_valid_basetype(Float32, T)
886-
d = DataF{T}(Array{Float32})
887-
d[] = x
888-
return d
889-
else
890-
check_basetype(Float64, T)
891-
end
881+
d = DataF{T}(Array{default_basetype(T)})
882+
d[] = x
883+
return d
892884
end
893885

894886
Base.@propagate_inbounds function Base.getindex(data::DataF{S}) where {S}
@@ -2270,8 +2262,6 @@ include("fused_copyto.jl")
22702262
include("fill.jl")
22712263
include("mapreduce.jl")
22722264

2273-
include("struct_linear_indexing.jl")
2274-
22752265

22762266
"""
22772267
set_mask_maps!(mask)

src/DataLayouts/parent_arrays.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
parent_array_type(::Type{<:AbstractArray})
3+
4+
Returns the parent array type underlying any wrapper types, with all
5+
dimensionality information removed.
6+
"""
7+
parent_array_type(::Type{<:Array{T}}) where {T} = Array{T}
8+
parent_array_type(::Type{<:MArray{S, T, N, L}}) where {S, T, N, L} =
9+
MArray{S, T}
10+
parent_array_type(::Type{<:SubArray{T, N, A}}) where {T, N, A} =
11+
parent_array_type(A)
12+
13+
# ReshapedArray is needed for converting between arrays and fields for RRTMGP:
14+
parent_array_type(::Type{<:Base.ReshapedArray{T, N, P}}) where {T, N, P} =
15+
parent_array_type(P)
16+
17+
"""
18+
promote_parent_array_type(::Type{<:AbstractArray}, ::Type{<:AbstractArray})
19+
20+
Given two parent array types (without any dimensionality information), promote
21+
both the element types and the array types themselves.
22+
"""
23+
promote_parent_array_type(::Type{Array{T1}}, ::Type{Array{T2}}) where {T1, T2} =
24+
Array{promote_type(T1, T2)}
25+
promote_parent_array_type(
26+
::Type{MArray{S, T1}},
27+
::Type{MArray{S, T2}},
28+
) where {S, T1, T2} = MArray{S, promote_type(T1, T2)}
29+
promote_parent_array_type(
30+
::Type{MArray{S, T1}},
31+
::Type{Array{T2}},
32+
) where {S, T1, T2} = MArray{S, promote_type(T1, T2)}
33+
promote_parent_array_type(
34+
::Type{Array{T1}},
35+
::Type{MArray{S, T2}},
36+
) where {S, T1, T2} = MArray{S, promote_type(T1, T2)}
37+
# Ditch sizes (they're never actually used!)
38+
promote_parent_array_type(
39+
::Type{MArray{S1, T1}},
40+
::Type{MArray{S2, T2}},
41+
) where {S1, T1, S2, T2} = MArray{S, promote_type(T1, T2)} where {S}
42+
promote_parent_array_type(
43+
::Type{MArray{S1, T1} where {S1}},
44+
::Type{MArray{S2, T2}},
45+
) where {T1, S2, T2} = MArray{S, promote_type(T1, T2)} where {S}
46+
promote_parent_array_type(
47+
::Type{MArray{S1, T1}},
48+
::Type{MArray{S2, T2} where {S2}},
49+
) where {S1, T1, T2} = MArray{S, promote_type(T1, T2)} where {S}

0 commit comments

Comments
 (0)