Skip to content

Commit

Permalink
Only call cudaconvert when argument types mismatch.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Aug 23, 2024
1 parent 4a215e3 commit cc0c716
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/compiler/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ macro cuda(ex...)
$kernel_tt = Tuple{map(Core.Typeof, $kernel_args)...}
$kernel = $cufunction($kernel_f, $kernel_tt; $(compiler_kwargs...))
if $launch
$kernel($(var_exprs...); $(call_kwargs...))
$kernel($kernel_args...; $(call_kwargs...))
end
$kernel
end
Expand Down Expand Up @@ -194,6 +194,13 @@ input object `x` as-is.
Do not add methods to this function, but instead extend the underlying Adapt.jl package and
register methods for the the `CUDA.KernelAdaptor` type.
!!! note
As an optimization, the compiler may choose not to call `cudaconvert` when it can prove
that the argument is already converted, by comparing against the type of the value that
the conversion would return. For semantically important conversions, ensure the type
of the value returned by `cudaconvert` is different from the input type.
"""
cudaconvert(arg) = adapt(KernelAdaptor(), arg)

Expand Down Expand Up @@ -238,15 +245,29 @@ function Base.show(io::IO, ::MIME"text/plain", k::AbstractKernel{F,TT}) where {F
print(io, "CUDA.$(nameof(typeof(k))) for $(k.f)($(join(TT.parameters, ", ")))")
end

@inline @generated function call(kernel::AbstractKernel{F,TT}, args...; call_kwargs...) where {F,TT}
@inline @generated function (kernel::AbstractKernel{F,TT})(args...; call_kwargs...) where {F,TT}
sig = Tuple{F, TT.parameters...} # Base.signature_type with a function type
args = (:(kernel.f), (:( args[$i] ) for i in 1:length(args))...)

# determine argument expressions
argexprs = [:(kernel.f)]
for i in 1:length(args)
if args[i] != TT.parameters[i]
# if there's a type mismatch between the argument and the compiled kernel,
# assume we still have to call `cudaconvert`. this is a bit of a hack, but
# it's generally true that `cudaconvert` will change the type of the argument,
# and it makes it possible to pass both original and converted arguments
# to compiled kernel objects, avoiding the need for multiple conversions.
push!(argexprs, :(cudaconvert(args[$i])))
else
push!(argexprs, :(args[$i]))
end
end

# filter out arguments that shouldn't be passed
predicate = dt -> isghosttype(dt) || Core.Compiler.isconstType(dt)
to_pass = map(!predicate, sig.parameters)
call_t = Type[x[1] for x in zip(sig.parameters, to_pass) if x[2]]
call_args = Union{Expr,Symbol}[x[1] for x in zip(args, to_pass) if x[2]]
call_args = Union{Expr,Symbol}[x[1] for x in zip(argexprs, to_pass) if x[2]]

# replace non-isbits arguments (they should be unused, or compilation would have failed)
# alternatively, make it possible to `launch` with non-isbits arguments.
Expand Down Expand Up @@ -386,10 +407,6 @@ end
# cache of kernel instances
const _kernel_instances = Dict{Any, Any}()

function (kernel::HostKernel)(args::Vararg{Any,N}; threads::CuDim=1, blocks::CuDim=1, kwargs...) where {N}
call(kernel, map(cudaconvert, args)...; threads, blocks, kwargs...)
end

make_seed(::HostKernel) = Random.rand(UInt32)


Expand Down Expand Up @@ -420,9 +437,6 @@ No keyword arguments are supported.
DeviceKernel{F,tt}(f, fun, kernel_state())
end

@inline (kernel::DeviceKernel)(args::Vararg{Any,N}; kwargs...) where {N} =
call(kernel, args...; kwargs...)

# re-use the parent kernel's seed to avoid need for the RNG
make_seed(::DeviceKernel) = kernel_state().random_seed

Expand Down

0 comments on commit cc0c716

Please sign in to comment.