diff --git a/docs/src/index.md b/docs/src/index.md index dbc75910c..5d98eb2b6 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -169,11 +169,10 @@ end - The `CPU` backend runs kernels on as many threads as Julia was started with (`julia -t`), like the thread-based `CPU` backend of 0.9 did, and no longer on one thread per hardware thread. Set `JULIA_KA_CPU_THREADS` to use a different number of threads; see [`CPU`](@ref). -- A `CPU` launch without a workgroup size is split into about one workgroup per thread, like - PoCL does when an OpenCL launch doesn't give a local size, with workgroups of up to 4096 - work-items, instead of into workgroups of 1024 work-items. Small launches therefore use - more threads, and large ones larger workgroups. The default assumes that the work is - balanced across the work-items; see [Quickstart](@ref). +- A `CPU` launch without a workgroup size is split into more than four workgroups per thread, + with workgroups of up to 4096 work-items, instead of into workgroups of 1024 work-items. + Small launches therefore use more threads, and large ones larger workgroups. The default + assumes that the work is balanced across the work-items; see [Quickstart](@ref). - `@private` arrays are a `KernelAbstractions.PrivateArray` in stack storage on every backend, instead of a `StaticArrays.MArray` on most GPU backends, which was heap-allocated when passed to a function that isn't inlined. It is still a `StaticArray`, but static-array arithmetic, diff --git a/docs/src/quickstart.md b/docs/src/quickstart.md index bd82ac3dc..03a847473 100644 --- a/docs/src/quickstart.md +++ b/docs/src/quickstart.md @@ -13,10 +13,10 @@ number of items to iterate over in a loop. Like other GPU programming models, KernelAbstractions assumes that the work is roughly balanced across the work-items. A launch without a workgroup size gets one chosen under that assumption: on the CPU, each workgroup runs on one thread and is vectorized across its -work-items, so the default splits a launch into about one workgroup per thread, as long as -the workgroups stay large enough to vectorize. If the cost of a work-item varies a lot, e.g., -with a triangular loop bound, pass a smaller workgroup size, so that threads that finish -early can pick up more workgroups. +work-items, so the default splits a launch into a few workgroups per thread, as long as the +workgroups stay large enough to vectorize. Threads that finish early then pick up more. If +the cost of a work-item varies a lot, e.g., with a triangular loop bound, pass a smaller +workgroup size, so that there are more workgroups to pick up. ## Writing your first kernel diff --git a/src/pocl/backend.jl b/src/pocl/backend.jl index c821a3c3a..a1a53ca07 100644 --- a/src/pocl/backend.jl +++ b/src/pocl/backend.jl @@ -346,14 +346,22 @@ function largest_divisor(n::Int, x::Int, m::Int) return 0 end -# PoCL's own choice when a launch doesn't give a local size (`pocl_default_local_size_optimizer` -# in `lib/CL/devices/pocl_local_size.c`), in one dimension: start from the largest work-group, -# and split it while there are at most half as many work-groups as threads, but not below -# 4 times the preferred work-group size multiple. two differences: PoCL only uses local sizes -# that divide the global size; KernelAbstractions supports partial work-groups, so this uses -# powers of two instead, which divide the extents of `ndrange`s that are powers of two. and -# PoCL doesn't split at all if splitting over all threads would go below the minimum, which -# leaves e.g. 1000 work-items on 64 threads in one work-group; this splits down to the +# the number of work-groups per thread that the default work-group size aims to exceed +const WORKGROUPS_PER_THREAD = 4 + +# Based on PoCL's own choice when a launch doesn't give a local size +# (`pocl_default_local_size_optimizer` in `lib/CL/devices/pocl_local_size.c`), in one +# dimension: start from the largest work-group, and halve it while there are at most +# `WORKGROUPS_PER_THREAD` work-groups per thread, but not below 4 times the preferred +# work-group size multiple. three differences: PoCL stops splitting once there are more than +# half as many work-groups as threads, which leaves threads idle when the work-groups don't +# divide evenly among them (e.g. 132x132 work-items in 6 work-groups on 8 threads), and gives +# threads that finish early nothing to pick up; several work-groups per thread keep the +# threads busy at the cost of a few more, still vectorizable, work-groups. PoCL only uses local +# sizes that divide the global size; KernelAbstractions supports partial work-groups, so this +# uses powers of two instead, which divide the extents of `ndrange`s that are powers of two. +# and PoCL doesn't split at all if splitting over all threads would go below the minimum, +# which leaves e.g. 1000 work-items on 64 threads in one work-group; this splits down to the # minimum instead. function cpu_workgroupsize(nitems, threads, max_items, multiple) nitems === nothing && return max_items @@ -363,10 +371,8 @@ function cpu_workgroupsize(nitems, threads, max_items, multiple) # one work-group, which isn't split (without dividing, for small launches) items >= nitems && items < 2 * min_items && return items groups = cld_extent(nitems, items) - while threads >= 2 * groups && items >= 2 * min_items - splits = min(threads ÷ groups, items ÷ min_items) - # PoCL splits by the largest factor of at most `splits` that divides the work-group - items >>= trailing_zeros(prevpow(2, splits)) + while groups <= WORKGROUPS_PER_THREAD * threads && items >= 2 * min_items + items >>= 1 groups = cld_extent(nitems, items) end return items diff --git a/test/runtests.jl b/test/runtests.jl index 1eb576fd7..82d2d7fe9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1555,10 +1555,15 @@ end @test checks(bounds_checked!(CPU(), 8), 16) == expected(16, 8) @test checks(bounds_checked!(CPU(), 8), 12) == expected(12, 8) # also with a tuned workgroup size, which is a power of two on PoCL, unless a single - # workgroup covers the ndrange (e.g. on a single thread). an odd ndrange larger than - # the largest workgroup needs partial workgroups, however many threads PoCL uses. + # workgroup covers the ndrange (e.g. on a single thread). a prime ndrange larger than + # the largest workgroup needs partial workgroups, however many threads PoCL uses: fitting + # can't find a divisor (4097 = 17 * 241 would fit 241 work-items on 4 threads). @test all(==(0), checks(bounds_checked!(CPU()), 1024)) - A = checks(bounds_checked!(CPU()), POCL.device_limits().max_work_group_size + 1) + n = POCL.device_limits().max_work_group_size + 1 + while any(d -> n % d == 0, 2:isqrt(n)) + n += 1 + end + A = checks(bounds_checked!(CPU()), n) @test A[end] == 1 @test A[1] == 0 @test all(==(0), checks(bounds_checked!(CPU()), (64, 32))) @@ -1569,7 +1574,7 @@ end @test POCL.compilations[] == compilations + 1 # check the whole first partial launch so kernels used once compile once. later launches # compile an unchecked variant for full workgroups. - A = fill(-1, 4097) + A = fill(-1, n) partial_bounds_checked!(CPU())(A; ndrange = length(A)) @test all(==(1), A) @test POCL.compilations[] == compilations + 2 @@ -1665,19 +1670,20 @@ end @test 1 <= items <= max_items # a power of two, unless limited by `max_items` ispow2(max_items) && @test ispow2(items) - # splitting stops with more than half as many workgroups as threads, or at the - # minimum of 4 times the preferred multiple + # splitting stops with more than 4 workgroups per thread, or at the minimum of 4 times + # the preferred multiple if nitems !== nothing - @test 2 * cld(max(nitems, 1), items) > threads || items < 2 * min(32, max_items) + @test cld(max(nitems, 1), items) > 4 * threads || items < 2 * min(32, max_items) end end - # PoCL's choice for the same launches (`pocl_default_local_size_optimizer`) for (nitems, threads, items) in ( - (1024, 1, 1024), (1024, 4, 256), (1024, 8, 128), (4096, 4, 1024), (4096, 8, 512), - (16384, 4, 4096), (16384, 8, 2048), (65536, 8, 4096), (4096, 64, 64), + (1024, 1, 128), (1024, 4, 32), (1024, 8, 32), (4096, 4, 128), (4096, 8, 64), + (16384, 4, 512), (16384, 8, 256), (65536, 8, 1024), (4096, 64, 32), (2^20, 8, 4096), ) @test workgroupsize(nitems, threads, 4096, 8) == items end + # a 132x132 launch on 8 threads isn't left in 5 workgroups, which leave threads idle + @test cld(132 * 132, workgroupsize(132 * 132, 8, 4096, 8)) > 32 # the floor follows the preferred multiple @test workgroupsize(1024, 32, 4096, 8) == 32 @test workgroupsize(1024, 32, 4096, 16) == 64