diff --git a/src/pocl/nanoOpenCL.jl b/src/pocl/nanoOpenCL.jl index d7c811970..b504a0042 100644 --- a/src/pocl/nanoOpenCL.jl +++ b/src/pocl/nanoOpenCL.jl @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index 2d90fe9fc..c0d25240c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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.