Skip to content
Open
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
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ merged pull requests, see the [GitHub
releases](https://github.com/JuliaGPU/cuTile.jl/releases).


## v1.2.1 (unreleased)

*Bug fixes*:

- A `@cuda backend=cuTile stream=s` or `cuTile.launch(...; stream=s)` launch is
ordered after earlier work on its `CuArray` arguments (and strided views of
them) on other streams, and later work on them waits for it, like a CUDA.jl
launch. Before, a launch on a stream other than the task's could read data
that was still being written, or have its results read before it finished.
cuTile.jl now requires CUDACore 6.5.


## v1.2 (December 2026)

*Breaking changes*:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ MicrofloatsExt = "Microfloats"
[compat]
Adapt = "4.4"
BFloat16s = "0.6"
CUDACore = "6.3"
CUDACore = "6.5"
CUDA_Compiler_jll = "0.6.3"
CompilerCaching = "0.6.1"
DLFP8Types = "0.1.0"
Expand Down
16 changes: 6 additions & 10 deletions src/language/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,22 +359,18 @@ This method works with any array type that supports:
- `size(arr)` - returns array dimensions
- `strides(arr)` - returns array strides
"""
function TileArray(arr::AbstractArray{T, N}; index=nothing) where {T, N}
arr_sizes = size(arr)
arr_strides = strides(arr)
I = check_indextype(something(index, array_indextype(arr_sizes, arr_strides)))
sizes = NTuple{N, I}(I.(arr_sizes))
strides_val = NTuple{N, I}(I.(arr_strides))
TileArray(device_pointer(arr), sizes, strides_val)
end
TileArray(arr::AbstractArray; index=nothing) = tilearray(arr, device_pointer(arr), index)
TileArray(arr::PermutedDimsArray; index=nothing) =
tilearray(arr, device_pointer(parent(arr)), index)

function TileArray(arr::PermutedDimsArray{T, N}; index=nothing) where {T, N}
# `arr`, whose memory starts at `ptr`, with index type `index`.
function tilearray(arr::AbstractArray{T, N}, ptr::Ptr{T}, index) where {T, N}
arr_sizes = size(arr)
arr_strides = strides(arr)
I = check_indextype(something(index, array_indextype(arr_sizes, arr_strides)))
sizes = NTuple{N, I}(I.(arr_sizes))
strides_val = NTuple{N, I}(I.(arr_strides))
TileArray(device_pointer(parent(arr)), sizes, strides_val)
TileArray(ptr, sizes, strides_val)
end

# Device arrays hand out device pointer types (e.g. `CuPtr`), which TileArray
Expand Down
57 changes: 45 additions & 12 deletions src/launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# per job configuration on the kernel's inference result.

using ObjectFile: ObjectFile, readmeta, Sections
using CUDACore: CUDACore, CuArray, CuModule, CuFunction, CuContext, context, cudacall,
using CUDACore: CUDACore, CuArray, CuPtr, Managed, CuModule, CuFunction, CuContext, context, cudacall,
device, capability, AbstractBackend, AbstractKernel, kernel_convert,
kernel_compile
using CUDA_Compiler_jll
Expand All @@ -22,22 +22,55 @@ using Adapt: Adapt, adapt
`Adapt.jl` adaptor used to convert host-side launch arguments into their
kernel-side counterparts. `AbstractArray`s become `TileArray`s; `Type`
values become `Constant`s. User-defined structs containing arrays compose
naturally via `Adapt.adapt_structure`.
naturally via `Adapt.adapt_structure`. A rule that converts an array itself
should do so with `Adapt.adapt(to, array)`, so that a launch knows which
memory the kernel uses.

This is the cuTile analogue of `CUDACore.KernelAdaptor`.
"""
struct KernelAdaptor end
struct KernelAdaptor
# The memory of the converted arrays, for a launch to acquire on its stream
# (see `CUDACore.with_managed`); without it, conversion takes the pointers
# for use on the task's stream.
managed::Union{Nothing, Vector{Managed}}
end
KernelAdaptor() = KernelAdaptor(nothing)

Adapt.adapt_storage(::KernelAdaptor, arr::AbstractArray) = TileArray(arr)
Adapt.adapt_storage(to::KernelAdaptor, arr::AbstractArray) = kernel_tilearray(arr, arr, to.managed)
Adapt.adapt_storage(::KernelAdaptor, t::Type) = Constant(t)

# Adapt's defaults for `PermutedDimsArray` and `SubArray` recurse by
# rebuilding the wrapper around `adapt(parent)`. We can't follow that
# pattern because `TileArray` isn't `<:AbstractArray` — strided-wrapper
# state is absorbed into its `sizes`/`strides` fields directly. Short-circuit
# the recursion so the whole wrapper becomes a single TileArray.
Adapt.adapt_structure(::KernelAdaptor, arr::PermutedDimsArray) = TileArray(arr)
Adapt.adapt_structure(::KernelAdaptor, arr::SubArray) = TileArray(arr)
Adapt.adapt_structure(to::KernelAdaptor, arr::PermutedDimsArray) =
kernel_tilearray(arr, parent(arr), to.managed)
Adapt.adapt_structure(to::KernelAdaptor, arr::SubArray) = kernel_tilearray(arr, arr, to.managed)

# `arr`, whose memory starts at that of `base`.
kernel_tilearray(arr, base, ::Nothing) = tilearray(arr, device_pointer(base), nothing)
kernel_tilearray(arr, base, managed::Vector{Managed}) =
tilearray(arr, managed_pointer(base, managed), nothing)

# The device address of `arr`, whose memory is recorded in `managed` rather than
# taken for use on the task's stream: like CUDACore's conversion of a `CuArray`
# for its own kernels, so that the launch orders the kernel after earlier uses
# of the memory on other streams. Other arrays take their pointer as usual.
function managed_pointer(arr::CuArray{T}, managed::Vector{Managed}) where {T}
memory = CUDACore.check_capture(arr.data)[]
push!(managed, memory)
return reinterpret(Ptr{T}, convert(CuPtr{T}, memory.mem) + arr.offset)
end
# (as `Base.unsafe_convert(::Type{CuPtr{T}}, ::SubArray)` does for these views)
managed_pointer(V::SubArray{T, N, <:CuArray, <:Tuple{Vararg{Base.RangeIndex}}},
managed::Vector{Managed}) where {T, N} =
managed_pointer(parent(V), managed) + Base._memory_offset(V.parent, map(first, V.indices)...)
managed_pointer(V::SubArray{T, N, <:CuArray,
<:Tuple{Vararg{Union{Base.RangeIndex, Base.ReshapedUnitRange}}}},
managed::Vector{Managed}) where {T, N} =
managed_pointer(parent(V), managed) + (Base.first_index(V) - 1) * CUDACore.aligned_sizeof(T)
managed_pointer(arr::AbstractArray, ::Vector{Managed}) = device_pointer(arr)

"""
cuTileconvert(x)
Expand Down Expand Up @@ -76,6 +109,8 @@ The default cuTile backend, looked up by `@cuda backend=cuTile`. Returns a
DefaultBackend() = TileBackend()

CUDACore.kernel_convert(::TileBackend, x) = cuTileconvert(x)
CUDACore.kernel_convert(::TileBackend, x, managed::Vector{Managed}) =
adapt(KernelAdaptor(managed), x)

CUDACore.kernel_compile(::TileBackend, f::F, tt::TT=Tuple{}; kwargs...) where {F,TT} =
cufunction(f, tt; kwargs...)
Expand Down Expand Up @@ -1140,12 +1175,10 @@ function launch(@nospecialize(f), grid, args...;
dependent::Bool=false,
stream=CUDACore.stream(),
name::Union{String, Nothing}=nothing)
f = cuTileconvert(f)
converted = map(cuTileconvert, args)
tt = Tuple{map(Core.Typeof, converted)...}
kernel = cufunction(f, tt; sm_arch, opt_level, num_ctas, occupancy, num_worker_warps, name,
alias_groups=alias_groups(converted...))
kernel(converted...; blocks=grid, dependent, stream)
# As `@cuda` does, so that the launch acquires its arrays' memory on `stream`.
CUDACore.kernel_pipeline(TileBackend(), f, Val(true), args...;
launch_kwargs=(; blocks=grid, dependent, stream),
sm_arch, opt_level, num_ctas, occupancy, num_worker_warps, name)
return nothing
end

Expand Down
63 changes: 63 additions & 0 deletions test/device/compilation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,66 @@ end
kernel(conv(dst), conv(view(src, 2:n+1)); blocks=n÷256)
@test Array(dst) == Float32.(2:n+1)
end

@testset "`@cuda` records the memory of converted arrays" begin
function copy_tiles(dst, src)
i = ct.bid(1)
ct.store(dst, i, ct.load(src, i, (256,)))
return
end
src = CUDA.rand(Float32, 1025)
dst = CUDA.zeros(Float32, 1024)
call = CUDA.CUDACore.KernelCall(copy_tiles, dst, view(src, 2:1025);
backend=ct.TileBackend())
@test call.managed == [dst.data[], src.data[]]
@test call.arguments[2].ptr == reinterpret(Ptr{Float32}, pointer(src, 2))
copy_from(src) = dst -> copy_tiles(dst, src)
call = CUDA.CUDACore.KernelCall(copy_from(src), dst; backend=ct.TileBackend())
@test call.managed == [src.data[], dst.data[]]
end

@testset "`@cuda stream=...` orders the launch after other uses of its arrays" begin
function copy_tiles(dst, src)
i = ct.bid(1)
ct.store(dst, i, ct.load(src, i, (256,)))
return
end
function spin_then_fill(a, cycles)
t0 = clock(UInt64)
while clock(UInt64) - t0 < cycles end
i = threadIdx().x + (blockIdx().x - 1) * blockDim().x
i <= length(a) && (a[i] = 1f0)
return
end
function spin(cycles)
t0 = clock(UInt64)
while clock(UInt64) - t0 < cycles end
return
end
cycles = UInt64(200_000_000)
s = CuStream(; flags=CUDA.STREAM_NON_BLOCKING)
# Compiled up front, so that compiling during the test can't outlast the delay.
a = CUDA.zeros(Float32, 256); b = CUDA.zeros(Float32, 256)
@cuda threads=256 spin_then_fill(a, UInt64(0))
@cuda spin(UInt64(0))
@cuda backend=cuTile copy_tiles(b, a)

launches = [(b, a) -> @cuda(backend=cuTile, stream=s, copy_tiles(b, a)),
(b, a) -> ct.launch(copy_tiles, 1, b, a; stream=s)]
for copy! in launches
# written on the task's stream, read on `s`
a = CUDA.zeros(Float32, 256); b = CUDA.zeros(Float32, 256)
device_synchronize()
@cuda threads=256 spin_then_fill(a, cycles)
copy!(b, a)
device_synchronize()
@test all(==(1f0), Array(b))

# written on `s` behind other work, read on the task's stream
a = CUDA.ones(Float32, 256); b = CUDA.zeros(Float32, 256)
device_synchronize()
@cuda stream=s spin(cycles)
copy!(b, a)
@test all(==(1f0), Array(b))
end
end