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
25 changes: 21 additions & 4 deletions src/pocl/nanoOpenCL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ const CL_BUILD_ERROR = -2

const CL_BUILD_IN_PROGRESS = -3

const CL_KERNEL_NUM_ARGS = 0x1191

const CL_KERNEL_WORK_GROUP_SIZE = 0x11b0

const CL_KERNEL_COMPILE_WORK_GROUP_SIZE = 0x11b1
Expand Down Expand Up @@ -657,6 +659,15 @@ end
)::cl_int
end

@checked function clGetKernelInfo(
kernel, param_name, param_value_size, param_value, param_value_size_ret
)
@gcsafe_ccall libopencl.POclGetKernelInfo(
kernel::cl_kernel, param_name::cl_uint, param_value_size::Csize_t,
param_value::Ptr{Cvoid}, param_value_size_ret::Ptr{Csize_t}
)::cl_int
end

@checked function clGetKernelWorkGroupInfo(
kernel, device, param_name, param_value_size,
param_value, param_value_size_ret
Expand Down Expand Up @@ -1236,6 +1247,12 @@ end

Base.unsafe_convert(::Type{cl_kernel}, k::Kernel) = k.id

function num_args(k::Kernel)
n = Ref{cl_uint}()
clGetKernelInfo(k, CL_KERNEL_NUM_ARGS, sizeof(cl_uint), n, C_NULL)
return Int(n[])
end

function Kernel(p::Program, kernel_name::String)
for (dev, status) in p.build_status
if status != CL_BUILD_SUCCESS
Expand Down Expand Up @@ -1413,11 +1430,11 @@ function enqueue_kernel(
num_sub_groups = KernelSubGroupInfo(k, device(), Csize_t[]).max_num_sub_groups
end
if nargs === nothing
nargs = k.num_args - 2
nargs = num_args(k) - 2
end
rng_state_size = sizeof(UInt32) * num_sub_groups
set_arg!(k, nargs + 1, LocalMem(UInt32, rng_state_size))
set_arg!(k, nargs + 2, LocalMem(UInt32, rng_state_size))
# one UInt32 per sub-group
set_arg!(k, nargs + 1, LocalMem(UInt32, num_sub_groups))
set_arg!(k, nargs + 2, LocalMem(UInt32, num_sub_groups))
end

n_events = cl_uint(0)
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,18 @@ end
)
end

# the RNG state takes two UInt32s of local memory per sub-group
@testset "POCL device RNG: local memory" begin
rng_kernel(A) = (A[POCL.get_global_id()] = rand(Float32); return)
A = zeros(Float32, 32)
kernel = POCL.clfunction(rng_kernel, Tuple{typeof(POCL.clconvert(A))})
@test kernel.rng_state
kernel(A; global_size = 32, local_size = 32)
num_sub_groups = POCL.cl.sub_group_info(kernel.fun, POCL.device(), Csize_t[32]).sub_group_count
local_mem = POCL.cl.work_group_info(kernel.fun, POCL.device()).local_mem_size
@test local_mem == 2 * sizeof(UInt32) * num_sub_groups
end

# `randn`/`randexp` for Float16 route through Random's table-free fallback, whose polar
# transform overflows in Float16 and whose `log1p` isn't available for Float16 on the
# device. The device overlays compute in Float32 and convert, so results stay finite.
Expand Down
Loading