Recursive AI

Check GPU compute limits before dispatch in C#

OneGpu is a free, open-source GPU compute library for .NET from Recursive AI. MIT or Apache-2.0, no bundled native binaries.

On Vulkan 1.3, read Capabilities.Compute to check the dispatch size, and use Int8DotProduct as an example of checking for an optional shader feature.

Terminal
dotnet add package RecursiveAI.OneGpu.Backends

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using System;
using RecursiveAI.OneGpu;
 
using var gpu = GpuBackends.CreateDevice(
    new() { Adapter = "discrete" });
var caps = gpu.Capabilities.Compute;
const uint threads = 128, groups = 32;
// A 1D group still needs one thread on Y and Z.
bool fits = threads <= caps.MaxWorkgroupSizeX
    && caps.MaxWorkgroupSizeY >= 1
    && caps.MaxWorkgroupSizeZ >= 1
    && threads <= caps.MaxWorkgroupInvocations
    && groups <= caps.MaxWorkgroupCount;
bool useDot = fits && caps.Int8DotProduct;
Console.WriteLine($"Grid fits: {fits}");
Console.WriteLine(
    $"Use packed int8 dot: {useDot}");

Unzip the example, then dotnet run.

Prints:

Text
Grid fits: True
Use packed int8 dot: True

For integrated Vulkan graphics, use Adapter = "integrated"; CreateDevice throws GpuUnsupportedException if no adapter matches or the selected backend is unavailable, and browser WebGPU requires CreateDeviceAsync instead.

The full program

The zip prints the adapter and limits as well as the decision. It checks a proposed one-dimensional dispatch of 32 groups with 128 threads each. It doesn’t load or dispatch a shader. If the checks fail, choose a supported shader or a different grid before recording the dispatch.

C#
using System;
using RecursiveAI.OneGpu;
 
using var gpu = GpuBackends.CreateDevice(
    new() { Adapter = "discrete" });
var caps = gpu.Capabilities.Compute;
Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}");
Console.WriteLine($"Max threads along X: {caps.MaxWorkgroupSizeX}");
Console.WriteLine($"Max threads per group: {caps.MaxWorkgroupInvocations}");
Console.WriteLine($"Max workgroups (any axis): {caps.MaxWorkgroupCount}");
Console.WriteLine($"Packed int8 dot supported: {caps.Int8DotProduct}");
 
const uint threads = 128, groups = 32;
bool fits = threads <= caps.MaxWorkgroupSizeX
    && caps.MaxWorkgroupSizeY >= 1
    && caps.MaxWorkgroupSizeZ >= 1
    && threads <= caps.MaxWorkgroupInvocations
    && groups <= caps.MaxWorkgroupCount;
bool useDot = fits && caps.Int8DotProduct;
Console.WriteLine($"Grid fits: {fits}");
Console.WriteLine($"Use packed int8 dot: {useDot}");

Output:

Text
Vulkan: NVIDIA GeForce RTX 4070
Max threads along X: 1024
Max threads per group: 1024
Max workgroups (any axis): 65535
Packed int8 dot supported: True
Grid fits: True
Use packed int8 dot: True

Variations

Reject a workgroup that exceeds the limit

Change the constants in the snippet at the top to test 2,048 threads in one group:

C#
const uint threads = 2048, groups = 32;

On the same adapter, the grid now fails the size check even though the dot-product feature is present:

Text
Grid fits: False
Use packed int8 dot: False

The short answer

Read gpu.Capabilities.Compute after you open the adapter. Check the shader’s local size against the X, Y and Z limits and the product of those dimensions against MaxWorkgroupInvocations. Separately check the number of groups per dispatch axis against MaxWorkgroupCount. Gate any optional shader variant on the features that variant actually requires.

What’s the difference between workgroup size and dispatch count?

The Vulkan Guide calls the threads within a group invocations. The workgroup size is the number of threads the shader puts in each group. The dispatch count is how many groups the host asks the GPU to run. A shader with a 128 × 1 × 1 local size dispatched as 32 × 1 × 1 launches 4,096 invocations, but the per-group limit is checked against 128, not 4,096. Changing the dispatch count doesn’t change the shader’s local size.

For a two-dimensional shader, check both axes and their product. For example, a 64 × 32 × 1 group has 2,048 threads, so it would fail the 1,024-invocation limit printed above even if each dimension passed on its own. On Vulkan, OneGpu reports the smallest of the three hardware dispatch-count limits as MaxWorkgroupCount, so the same bound can be used along every axis.

How do you check what a Vulkan compute shader can use?

Look at the selected device’s reported capabilities rather than guessing from the GPU name. In this example, Int8DotProduct gates a variant that needs the packed operation. If that variant also needs 8-bit values in storage buffers, check Integer8 separately. For shared memory and storage buffers, check MaxWorkgroupMemoryBytes and MaxStorageBufferBindingBytes against your shader and its bindings. A zero numeric limit means unreported, not unlimited.

OneGpu’s compute docs describe an additional check: CreateComputeKernel checks reflected requirements such as local size, scalar types and subgroup operations before creating the pipeline. The preflight here lets your application choose a variant before making that call; it isn’t a substitute for checking all requirements of the shader you choose.

All onegpu recipes