Recursive AI

Allocate a packed boolean tensor buffer on the GPU in C#

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

Use PaddedBoolLength to allocate eight bytes for five flags: one byte per flag, padded to whole 32-bit words. The example uses a discrete GPU with Vulkan 1.3.

This example also requires RecursiveAI.OneGpu.Backends to create the GPU device.

Terminal
dotnet add package RecursiveAI.OneGpu.Backends
dotnet add package RecursiveAI.OneGpu.Kernels

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

C#
using System;
using RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Kernels;
 
using var gpu = GpuBackends.CreateDevice(
    new() { Adapter = "discrete" });
byte[] flags = [1, 0, 1, 1, 0];
int count = flags.Length;
int length =
    GpuKernelBuffers.PaddedBoolLength(count);
using var input =
    gpu.CreateBuffer<byte>(length);
input.Write(flags);
Console.WriteLine(
    $"{count} flags; {input.Length} bytes");

Unzip the example, then dotnet run.

Prints:

Text
5 flags; 8 bytes

The full program

The input flags are [1, 0, 1, 1, 0]. The program passes the logical count of five to EncodeNot and reads back just five bytes. A five-byte allocation can hold the flags, but EncodeNot rejects it with GpuKernelShapeException before dispatch because the last word is incomplete.

C#
using System;
using RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Kernels;
 
using var gpu = GpuBackends.CreateDevice(
    new() { Adapter = "discrete" });
byte[] flags = [1, 0, 1, 1, 0];
int count = flags.Length;
int length = GpuKernelBuffers.PaddedBoolLength(count);
using var input = gpu.CreateBuffer<byte>(length);
input.Write(flags);
Console.WriteLine($"{count} flags; {input.Length} bytes");
 
using var output = gpu.CreateBuffer<byte>(length);
using var kernels = new IntegerKernels(gpu);
var commands = gpu.BeginCommands();
kernels.EncodeNot(commands, input, 0, output, 0, count);
gpu.Submit(commands);
var inverted = new byte[count];
await output.ReadAsync(inverted);
Console.WriteLine("NOT: " + string.Join(", ", inverted));
 
using var unpadded = gpu.CreateBuffer<byte>(flags);
try
{
    kernels.EncodeNot(gpu.BeginCommands(), unpadded, 0,
        output, 0, count);
}
catch (GpuKernelShapeException error)
{
    Console.WriteLine(error.GetType().Name + ": " + error.Message);
}
Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}");

Output from dotnet run:

Text
5 flags; 8 bytes
NOT: 0, 1, 0, 0, 1
GpuKernelShapeException: Not: a: the buffer of 5 bytes ends inside a 32-bit word; allocate GpuKernelBuffers.PaddedBoolLength(n) elements (Parameter 'a')
Vulkan: NVIDIA GeForce RTX 4070

Variations

Allocate and upload in one call

Replace the allocation and Write call at the top with this, using the same flags and padded length:

C#
var storage = new byte[length];
flags.CopyTo(storage, 0);
using var input = gpu.CreateBuffer<byte>(storage);

The buffer still prints 5 flags; 8 bytes. The copied flags occupy the first five bytes; the array’s extra three bytes start at zero.

The short answer

Use GpuKernelBuffers.PaddedBoolLength(count) to size a GpuBuffer<byte>. Upload the logical bytes as 0 or 1. Pass the original element count or tensor shape to the kernel: the spare bytes are storage, not extra boolean values.

Is a boolean one bit or one byte in a GPU tensor?

In OneGpu’s boolean tensor layout, one flag occupies one byte. “Packed” here means there are four byte-sized flags per 32-bit word, not 32 one-bit flags per word. Use GpuBuffer<byte> for this layout, with 0 for false and 1 for true. The kernels treat any nonzero input byte as true and write 0 or 1 on output.

With five flags, bytes zero through four are the tensor. Bytes five through seven are padding. If your input is shaped [1, 5], it stays [1, 5] after allocation. You can read only the logical bytes back, as the example does, so the padding doesn’t enter your CPU result either.

Why does a packed bool buffer need four-byte padding?

The kernels access whole 32-bit words. The fifth flag starts a second word, so the buffer must also contain the three bytes that finish that word. PaddedBoolLength rounds up to a multiple of four; a count that’s already a multiple of four doesn’t grow. Pad both the input and output if both hold packed booleans.

The exception checks the word touched by the requested range. If you store a tensor at a nonzero byte offset in a larger buffer, size the allocation for the end of that range rounded up to a whole word, not just for the tensor’s length. Don’t insert padding between rows of one dense tensor. OneGpu’s bool kernels can write a range starting inside a word while preserving the neighboring bytes; padding is needed at the end of the storage the kernel will touch.

All onegpu recipes