Skip to content
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

Remove redundant calls to mtlconvert #409

Closed
wants to merge 2 commits 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
17 changes: 11 additions & 6 deletions src/compiler/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ macro metal(ex...)
$kernel_tt = Tuple{map(Core.Typeof, $kernel_args)...}
$kernel = $mtlfunction($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 @@ -203,7 +203,7 @@ const _kernel_instances = Dict{UInt, Any}()

## kernel launching and argument encoding

@inline @generated function encode_arguments!(cce, kernel, args...)
@inline @generated function encode_arguments!(cce, kernel::HostKernel{F,TT}, args...) where {F,TT}
ex = quote
bufs = MTLBuffer[]
end
Expand All @@ -216,6 +216,8 @@ const _kernel_instances = Dict{UInt, Any}()
idx = 1
for (argidx, argtyp) in enumerate(args)
argex = :(args[$argidx])
argTT = :(TT.parameters[$argidx])

if argtyp <: MTLBuffer
# top-level buffers are passed as a pointer-valued argument
push!(ex.args, :(set_buffer!(cce, $argex, 0, $idx)))
Expand All @@ -227,7 +229,11 @@ const _kernel_instances = Dict{UInt, Any}()
else
# everything else is passed by reference, in an argument buffer
append!(ex.args, (quote
buf = encode_argument!(kernel, mtlconvert($(argex), cce))
buf = if $argtyp != $argTT
encode_argument!(kernel, mtlconvert($(argex), cce))
else
encode_argument!(kernel, $argex)
end
set_buffer!(cce, buf, 0, $idx)
push!(bufs, buf)
end).args)
Expand Down Expand Up @@ -259,8 +265,7 @@ end
return argument_buffer
end

@autoreleasepool function (kernel::HostKernel)(args...; groups=1, threads=1,
queue=global_queue(device()))
@autoreleasepool function (kernel::HostKernel)(args...; groups=1, threads=1, queue=global_queue(device()))
groups = MTLSize(groups)
threads = MTLSize(threads)
(groups.width>0 && groups.height>0 && groups.depth>0) ||
Expand All @@ -276,7 +281,7 @@ end
cce = MTLComputeCommandEncoder(cmdbuf)
argument_buffers = try
MTL.set_function!(cce, kernel.pipeline)
bufs = encode_arguments!(cce, kernel, kernel.f, args...)
bufs = encode_arguments!(cce, kernel, args...)
MTL.append_current_function!(cce, groups, threads)
bufs
finally
Expand Down
19 changes: 19 additions & 0 deletions test/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ end
k(; threads=1)

# TODO: kernel introspection
d_a = MtlArray([1.0f0, 2.0f0, 3.0f0, 4.0f0])
d_b = MtlArray([4.0f0, 3.0f0, 2.0f0, 1.0f0])
d_c = MtlArray([0.0f0, 0.0f0, 0.0f0, 0.0f0])

function vadd(a, b, c)
i = thread_position_in_grid_1d()
c[i] = a[i] + b[i]
return
end

kernel = @metal launch=false vadd(d_a, d_b, d_c)
kernel(d_a, d_b, d_c; threads=4)
@test all(d_c .== 5.0f0)

d_d = MtlArray([1.0f0, 2.0f0, 3.0f0, 4.0f0, 5.0f0])
d_e = MtlArray([5.0f0, 4.0f0, 3.0f0, 2.0f0, 1.0f0])
d_f = MtlArray([0.0f0, 0.0f0, 0.0f0, 0.0f0, 0.0f0])
kernel(d_d, d_e, d_f; threads=5)
@test all(d_f .== 6.0f0)
end

@testset "inference" begin
Expand Down