Skip to content

Commit 1c95960

Browse files
committed
Add AbstractTileArray
1 parent a047d83 commit 1c95960

4 files changed

Lines changed: 36 additions & 21 deletions

File tree

src/broadcast.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ BroadcastStyle(::TiledStyle{N}, ::CuArrayStyle{M}) where {N,M} = TiledStyle{max(
1717
## broadcast interface
1818

1919
function Base.Broadcast.materialize!(dest::Tiled, bc::Broadcasted)
20-
_tiled_broadcast!(parent(dest), bc)
21-
return dest
20+
arr = parent(dest)
21+
_tiled_broadcast!(arr, bc)
22+
return arr
2223
end
2324

2425
function Base.copy(bc::Broadcasted{TiledStyle{N}}) where N
@@ -43,8 +44,8 @@ end
4344

4445
## kernel wrapper
4546

46-
_to_tiled_bc(t::Tiled) = TileArray(parent(t))
47-
_to_tiled_bc(arr::AbstractArray) = TileArray(arr)
47+
_to_tiled_bc(arr::AbstractArray) = cuTileconvert(arr)
48+
_to_tiled_bc(t::Tiled) = _to_tiled_bc(parent(t))
4849
_to_tiled_bc(x::Number) = x
4950
_to_tiled_bc(x) = x # fallback for other types
5051
function _to_tiled_bc(bc::Broadcasted)
@@ -53,7 +54,7 @@ function _to_tiled_bc(bc::Broadcasted)
5354
end
5455

5556
function _tiled_broadcast!(dest::AbstractArray{T,N}, bc::Broadcasted) where {T, N}
56-
dest_ta = TileArray(dest)
57+
dest_ta = _to_tiled_bc(dest)
5758
tiled_bc = _to_tiled_bc(bc)
5859

5960
ts = _compute_tile_sizes(size(dest))
@@ -67,21 +68,24 @@ end
6768

6869
## kernel
6970

70-
@inline _eval_bc(arr::TileArray, bid, tile_size) = cuTile.load(arr, bid, tile_size)
71+
@inline _eval_bc(arr::AbstractTileArray, bid, tile_size) = cuTile.load(arr, bid, tile_size)
7172
@inline _eval_bc(x::Number, bid, tile_size) = x
7273

74+
@inline _bc_func(f) = f
75+
@inline _bc_func(::Constant{Type{T}, T}) where {T} = T
76+
7377
@inline function _eval_bc(bc::Broadcasted, bid, tile_size)
7478
args = _eval_bc_args(bc.args, bid, tile_size)
7579
# Use broadcast to get element-wise semantics (not direct call, which
7680
# would dispatch to e.g. matmul for * on tiles)
77-
broadcast(bc.f, args...)
81+
broadcast(_bc_func(bc.f), args...)
7882
end
7983

8084
@inline _eval_bc_args(::Tuple{}, bid, tile_size) = ()
8185
@inline _eval_bc_args(args::Tuple, bid, tile_size) =
8286
(_eval_bc(args[1], bid, tile_size), _eval_bc_args(Base.tail(args), bid, tile_size)...)
8387

84-
@generated function broadcast_kernel(dest::TileArray{T, N}, bc, tile_size, overflow_grids) where {T, N}
88+
@generated function broadcast_kernel(dest::AbstractTileArray{T, N}, bc, tile_size, overflow_grids) where {T, N}
8589
quote
8690
bids = _unflatten_bids(Val{$N}(), overflow_grids)
8791
result = _eval_bc(bc, bids, tile_size)

src/language/operations.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ Axis is 1-indexed. Equivalent to cld(size(arr, axis), shape[axis]).
313313
Intrinsics.get_index_space_shape(pv, axis - One()) # convert to 0-indexed
314314
end
315315

316+
@inline function num_tiles(arr::AbstractTileArray, axis::Integer, shape::NTuple{<:Any, Int})
317+
Int32(cld(size(arr, axis), shape[axis]))
318+
end
319+
316320
# Match a shape tuple to a target rank N by padding trailing 1s or squeezing trailing singletons.
317321
@generated function _match_shape(::Val{Shape}, ::Val{N}) where {Shape, N}
318322
M = length(Shape)

src/language/types.jl

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
public TileArray, Tile, Constant, TFloat32, similar_type,
1+
public AbstractTileArray, TileArray, Tile, Constant, TFloat32, similar_type,
22
ScalarInt, ScalarFloat, IntTile, FloatTile, TileOrInt, TileOrFloat,
33
TileOrScalar
44

5+
"""
6+
AbstractTileArray{T, N}
7+
8+
Supertype for N-dimensional kernel array arguments with element type `T`.
9+
"""
10+
abstract type AbstractTileArray{T, N} end
11+
12+
Base.eltype(::Type{<:AbstractTileArray{T}}) where T = T
13+
Base.ndims(::Type{<:AbstractTileArray{<:Any,N}}) where N = N
14+
Base.eltype(arr::AbstractTileArray) = eltype(arr)
15+
Base.ndims(arr::AbstractTileArray) = ndims(arr)
16+
17+
518
"""
619
ArraySpec{N}
720
@@ -167,17 +180,11 @@ specializations (e.g., aligned vs unaligned) produce different cubins.
167180
- `sizes::NTuple{N, Int32}`: Size in each dimension
168181
- `strides::NTuple{N, Int32}`: Stride in each dimension (in elements)
169182
"""
170-
struct TileArray{T, N, S}
183+
struct TileArray{T, N, S} <: AbstractTileArray{T, N}
171184
ptr::Ptr{T}
172185
sizes::NTuple{N, Int32}
173186
strides::NTuple{N, Int32}
174187
end
175-
176-
# Type accessors for TileArray
177-
Base.eltype(::Type{<:TileArray{T}}) where {T} = T
178-
Base.eltype(::TileArray{T}) where {T} = T
179-
Base.ndims(::Type{<:TileArray{<:Any, N}}) where {N} = N
180-
Base.ndims(::TileArray{<:Any, N}) where {N} = N
181188
Base.size(arr::TileArray) = arr.sizes
182189
function Base.size(arr::TileArray{<:Any, N}, d::Integer) where N
183190
d < 1 && error("arraysize: dimension out of range") # from Array method

src/mapreduce.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ _atomic_op(::typeof(&), ::Type{<:Integer}) = atomic_and
1111
_atomic_op(_, ::Type) = nothing
1212

1313
@generated function mapreduce_kernel(
14-
dest::TileArray{TD, N}, src::TileArray{TS, N},
14+
dest::AbstractTileArray{TD, N}, src::AbstractTileArray{TS, N},
1515
f, op, tile_size, reduce_dims, overflow_grids, init_val, pad_mode,
1616
reduce_stride, atomic_op
1717
) where {TD, TS, N}
@@ -65,7 +65,7 @@ end
6565

6666
function _mapreducedim!(f, op, R::AbstractArray, A::AbstractArray, reduce_dims::Tuple; init)
6767
N = ndims(A)
68-
src_ta = TileArray(A)
68+
src_ta = cuTileconvert(A)
6969

7070
# Reduced dims first (larger tiles => better hardware reduction)
7171
dim_order = (filter(d -> d in reduce_dims, 1:N)..., filter(d -> !(d in reduce_dims), 1:N)...)
@@ -103,19 +103,19 @@ function _mapreducedim!(f, op, R::AbstractArray, A::AbstractArray, reduce_dims::
103103

104104
if atomic_op !== nothing
105105
fill!(R, init)
106-
_launch_mapreduce!(grid, TileArray(R), src_ta, f, op, ts, reduce_dims,
106+
_launch_mapreduce!(grid, cuTileconvert(R), src_ta, f, op, ts, reduce_dims,
107107
init, pad_mode, reduce_stride, atomic_op)
108108
else
109109
# Two-pass: parallelize along par_dim, then reduce partials
110110
tmp = similar(A, eltype(R), ntuple(_dim_size, N))
111-
_launch_mapreduce!(grid, TileArray(tmp), src_ta, f, op, ts, reduce_dims,
111+
_launch_mapreduce!(grid, cuTileconvert(tmp), src_ta, f, op, ts, reduce_dims,
112112
init, pad_mode, reduce_stride, nothing)
113113
_mapreducedim!(identity, op, R, tmp, (par_dim,); init)
114114
end
115115
else
116116
grid = ntuple(d -> d in reduce_dims ? 1 : cld(size(A, d), ts[d]), N)
117117
reduce_stride = ntuple(d -> Int32(1), N)
118-
_launch_mapreduce!(grid, TileArray(R), src_ta, f, op, ts, reduce_dims,
118+
_launch_mapreduce!(grid, cuTileconvert(R), src_ta, f, op, ts, reduce_dims,
119119
init, pad_mode, reduce_stride, nothing)
120120
end
121121
end

0 commit comments

Comments
 (0)