Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/pocl/backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function threads_to_workgroupsize(threads, ndrange)
end
end

function (obj::KA.Kernel{POCLBackend})(args...; ndrange = nothing, workgroupsize = nothing)
function (obj::KA.Kernel{POCLBackend})(args::Vararg{Any, N}; ndrange = nothing, workgroupsize = nothing) where {N}
ndrange, workgroupsize, iterspace, dynamic =
KA.launch_config(obj, ndrange, workgroupsize)

Expand Down
15 changes: 7 additions & 8 deletions src/pocl/compiler/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ end
## argument conversion

struct KernelAdaptor
svm_pointers::Vector{Ptr{Cvoid}}
svm_pointers::Union{Nothing, Vector{Ptr{Cvoid}}}
end

# # assume directly-passed pointers are SVM pointers
Expand Down Expand Up @@ -137,7 +137,7 @@ register methods for the the `OpenCL.KernelAdaptor` type.
The `pointers` argument is used to collect pointers to indirect SVM buffers, which need to
be registered with OpenCL before invoking the kernel.
"""
function clconvert(arg, pointers::Vector{Ptr{Cvoid}} = Ptr{Cvoid}[])
function clconvert(arg, pointers::Union{Nothing, Vector{Ptr{Cvoid}}} = nothing)
return adapt(KernelAdaptor(pointers), arg)
end

Expand All @@ -149,11 +149,11 @@ abstract type AbstractKernel{F, TT} end
pass_arg(@nospecialize dt) = !(GPUCompiler.isghosttype(dt) || Core.Compiler.isconstType(dt))

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

# filter out ghost arguments that shouldn't be passed
to_pass = map(pass_arg, sig.parameters)
Expand All @@ -175,8 +175,7 @@ pass_arg(@nospecialize dt) = !(GPUCompiler.isghosttype(dt) || Core.Compiler.isco
call_tt = Base.to_tuple_type(call_t)

return quote
svm_pointers = Ptr{Cvoid}[]
$cl.clcall(kernel.fun, $call_tt, $(call_args...); svm_pointers, kernel.rng_state, call_kwargs...)
$cl.clcall(kernel.fun, $call_tt, $(call_args...); global_size, local_size, kernel.rng_state)
end
end

Expand Down
63 changes: 49 additions & 14 deletions src/pocl/nanoOpenCL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,23 @@ end
)::cl_int
end

# sizes passed as tuples, which `ccall` copies to the stack
@checked function clEnqueueNDRangeKernel(
command_queue, kernel, work_dim,
global_work_size::NTuple{3, Csize_t}, local_work_size::NTuple{3, Csize_t}, event::Ref{cl_event}
)
@ccall libopencl.POclEnqueueNDRangeKernel(
command_queue::cl_command_queue,
kernel::cl_kernel, work_dim::cl_uint,
C_NULL::Ptr{Csize_t},
global_work_size::Ref{NTuple{3, Csize_t}},
local_work_size::Ref{NTuple{3, Csize_t}},
0::cl_uint,
C_NULL::Ptr{cl_event},
event::Ref{cl_event}
)::cl_int
end

@checked function clEnqueueNDRangeKernel(
command_queue, kernel, work_dim,
global_work_offset, global_work_size,
Expand Down Expand Up @@ -1254,9 +1271,10 @@ function set_arg!(k::Kernel, idx::Integer, arg::LocalMem)
end

function set_arg!(k::Kernel, idx::Integer, arg::T) where {T}
ref = Ref(arg)
tsize = sizeof(ref)
err = unchecked_clSetKernelArg(k, cl_uint(idx - 1), tsize, ref)
# `Ref{T}` makes `ccall` pass a pointer to a stack copy of `arg`
err = @ccall libopencl.POclSetKernelArg(
k::cl_kernel, cl_uint(idx - 1)::cl_uint, sizeof(T)::Csize_t, arg::Ref{T}
)::cl_int
if err == CL_INVALID_ARG_SIZE
error(
"""Mismatch between Julia and OpenCL type for kernel argument $idx.
Expand All @@ -1275,19 +1293,36 @@ function set_arg!(k::Kernel, idx::Integer, arg::T) where {T}
return k
end

function set_args!(k::Kernel, args...)
for (i, a) in enumerate(args)
set_arg!(k, i, a)
end
return
set_args!(k::Kernel, args::Vararg{Any, N}) where {N} = set_args!(k, 1, args...)
@inline set_args!(k::Kernel, i::Int) = nothing
@inline function set_args!(k::Kernel, i::Int, arg, args::Vararg{Any, N}) where {N}
set_arg!(k, i, arg)
return set_args!(k, i + 1, args...)
end

# work sizes padded to the three dimensions OpenCL devices support
const WorkSize = NTuple{3, Csize_t}
@inline work_size(sizes) = ntuple(i -> i <= length(sizes) ? Csize_t(sizes[i]) : Csize_t(0), Val(3))

function enqueue_kernel(
k::Kernel, global_work_size, local_work_size = nothing;
global_work_offset = nothing, rng_state = false, nargs = nothing
)
max_work_dim = device().max_work_item_dims
work_dim = length(global_work_size)

if global_work_offset === nothing && local_work_size !== nothing && !rng_state && work_dim <= 3
if length(local_work_size) != work_dim
throw(ArgumentError("global_work_size and local_work_size have differing dims"))
end
ret_event = Ref{cl_event}()
clEnqueueNDRangeKernel(
queue(), k, cl_uint(work_dim),
work_size(global_work_size), work_size(local_work_size), ret_event
)
return Event(ret_event[])
end

max_work_dim = device().max_work_item_dims
if work_dim > max_work_dim
throw(ArgumentError("global_work_size has max dim of $max_work_dim"))
end
Expand Down Expand Up @@ -1354,19 +1389,19 @@ function enqueue_kernel(
end

function call(
k::Kernel, args...; global_size = (1,), local_size = nothing,
k::Kernel, args::Vararg{Any, N}; global_size = (1,), local_size = nothing,
global_work_offset = nothing,
svm_pointers::Vector{Ptr{Cvoid}} = Ptr{Cvoid}[],
svm_pointers::Union{Nothing, Vector{Ptr{Cvoid}}} = nothing,
rng_state = false
)
) where {N}
set_args!(k, args...)
if !isempty(svm_pointers)
if svm_pointers !== nothing && !isempty(svm_pointers)
clSetKernelExecInfo(
k, CL_KERNEL_EXEC_INFO_SVM_PTRS,
sizeof(svm_pointers), svm_pointers
)
end
return enqueue_kernel(k, global_size, local_size; global_work_offset, rng_state, nargs = length(args))
return enqueue_kernel(k, global_size, local_size; global_work_offset, rng_state, nargs = N)
end

# convert the argument values to match the kernel's signature (specified by the user)
Expand Down
Loading