Skip to content

Launching a kernel with 32 or more arguments allocates 10–40 KB on the host #769

In review

Description

@michel2323

A kernel launch allocates between 350 B (1 array argument) and 2.8 KB (31 array arguments) on the host. At 32 arguments the cost jumps to 10 KB and then grows by about 400 B per argument. Through KernelAbstractions the step is at 31 arguments (the context is the 32nd) and the launch costs 16 KB there and 43 KB at 56 arguments.

#687 noted that such kernels still dispatch, "because map over such tuples isn't inferred; that predates this PR and doesn't come up often". This issue is the allocation side of that, from a case where it does come up: SparseDirectSolver.jl has a factorization kernel with 56 arguments, launched once per factorization, and that one launch is 42 KB of the 100 KB a refactorization and solve allocate on the host.

Reproducer

using oneAPI

for N in (1, 8, 16, 24, 31, 32, 33, 40, 48, 56)
    args = [Symbol(:a, i) for i in 1:N]
    @eval function $(Symbol(:kernel, N))($(args...))
        i = get_global_id()
        @inbounds a1[i] = $(args[end])[i] + 1f0
        return
    end
    @eval launch(::Val{$N}, v) = @oneapi items = 32 $(Symbol(:kernel, N))($((:(v[$i]) for i in 1:N)...))
end

function per_launch(n::Val, v, reps = 200)
    launch(n, v); oneAPI.synchronize()
    launch(n, v); oneAPI.synchronize()
    b = @allocated for _ in 1:reps
        launch(n, v)
    end
    oneAPI.synchronize()
    return b / reps
end

v = [oneAPI.zeros(Float32, 32) for _ in 1:56]
for N in (1, 8, 16, 24, 31, 32, 33, 40, 48, 56)
    println(N, " arguments: ", per_launch(Val(N), v), " B per launch")
end

On a Data Center GPU Max 1550 (Aurora, ONEAPI_LTS=1):

arguments (all oneVector{Float32}) @oneapi, B per launch KernelAbstractions kernel, B per launch
1 360 352
8 977 960
16 2351
24 2614 2576
30 2768
31 2833 16224
32 10510 16608
33 10703
40 13877 24704
48 18465 35320
56 20454 42920

The numbers are the same with ONEAPI_SYNC_EACH_SUBMISSION=1 (within 80 B). The KernelAbstractions column is a @kernel with the same arguments, called as kernel(backend)(v[1], …, v[N]; ndrange = 32).

Other observations, all with KernelAbstractions kernels:

  • Scalars cost too once past the step: 1 array and 29 Int32 arguments allocate 352 B, 1 array and 30 Int32 6464 B, 1 array and 55 Int32 17248 B.
  • Arrays inside a tuple argument avoid it: 1 array and a tuple of 55 arrays allocate 7208 B, against 42920 B for the same 56 arrays as separate arguments.
  • Profile.Allocs on the 56-argument launch attributes most of the bytes to launch_managed, at the line converted = map(arg -> kernel_convert(arg, refs), args): 58 allocations of the argument tuple type (about 515 B each, 30 KB in total), one per element. map(identity, t) on a 56-tuple of Refs alone allocates 1 KB, so the cost is not in map as such but in what the uninferred call does per element here.

Where

launch_managed in src/compiler/execution.jl (the same on master):

converted = map(arg -> kernel_convert(arg, refs), args)

For a tuple of 32 or more elements this is Base's map(f, t::Any32). A conversion that stays inferred for long tuples (a generated function, or recursion over the tuple's head and tail as the kernel argument setters do) would presumably bring the launch back to the cost per argument seen below the step, and remove the dispatch #687 mentions.

Versions

  • oneAPI.jl 2.10.0, GPUArrays 12.2.0, GPUCompiler 2.22.2, KernelAbstractions 0.10.7, KernelInterface 0.4.6, SPIRVIntrinsics 1.6.0, LLVM.jl 10.4.3
  • Julia 1.13.3
  • NEO 26.35.39758+2, libigc 2.41.5+5, driver v1.3.33578 (API v1.6.0)
  • Intel(R) Data Center GPU Max 1550

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions