Recursive AI

Batched matrix multiplication on the GPU 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.

Multiply two pairs of matrices in one GEMM (general matrix multiply) call to Gemm.Encode. This example uses a Vulkan 1.3 driver on Linux or Windows.

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 RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Kernels;
 
using var gpu = GpuBackends.CreateDevice();
using var a = gpu.CreateBuffer<float>(
    [1, 2, 3, 4,  2, 0, 0, 2]);
using var b = gpu.CreateBuffer<float>(
    [5, 6, 7, 8,  9, 1, 3, 4]);
using var c = gpu.CreateBuffer<float>(8);
using var gemm = new Gemm(
    gpu,
    GemmElementType.F32);
var shape = new GemmProblem(2, 2, 2) {
    Batch = 2,
    StrideA = 4,
    StrideB = 4,
    StrideC = 4
};
var commands = gpu.BeginCommands();
gemm.Encode(commands, shape, a, b, c);
gpu.Submit(commands);
var result = new float[8];
await c.ReadAsync(result);
Console.WriteLine(string.Join(", ", result[..4]));
Console.WriteLine(string.Join(", ", result[4..]));

Unzip the example, then dotnet run.

Prints one flattened 2 × 2 product per line, in row order:

Text
19, 22, 43, 50
18, 2, 6, 8

The full program

The downloadable program also prints the selected backend and adapter. If a matrix range doesn’t fit its buffer or the outputs overlap, Gemm.Encode throws GpuKernelShapeException before recording work.

C#
using RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Kernels;
 
using var gpu = GpuBackends.CreateDevice();
using var a = gpu.CreateBuffer<float>(
    [1, 2, 3, 4,  2, 0, 0, 2]);
using var b = gpu.CreateBuffer<float>(
    [5, 6, 7, 8,  9, 1, 3, 4]);
using var c = gpu.CreateBuffer<float>(8);
using var gemm = new Gemm(
    gpu,
    GemmElementType.F32);
var shape = new GemmProblem(2, 2, 2) {
    Batch = 2,
    StrideA = 4,
    StrideB = 4,
    StrideC = 4
};
var commands = gpu.BeginCommands();
gemm.Encode(commands, shape, a, b, c);
gpu.Submit(commands);
var result = new float[8];
await c.ReadAsync(result);
Console.WriteLine(string.Join(", ", result[..4]));
Console.WriteLine(string.Join(", ", result[4..]));
 
Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}");
Text
19, 22, 43, 50
18, 2, 6, 8
Vulkan: NVIDIA GeForce RTX 4070

Variations

Multiply every A by the same B

Replace the shape in the snippet at the top with this one. StrideB = 0 reads the first four values of b for both products; the last four are unused and can be removed.

C#
var shape = new GemmProblem(2, 2, 2) {
    Batch = 2,
    StrideA = 4,
    StrideB = 0,
    StrideC = 4
};
Text
19, 22, 43, 50
10, 12, 14, 16

The short answer

Use GemmProblem(M, N, K) to describe a single product’s shape, set Batch to the number of independent products, and specify the number of elements between matrices in each buffer. In this example the GPU reads A₀ and B₀ at offset 0 and writes A₀ × B₀ at offset 0 in c. It reads A₁ and B₁ at offset 4 and writes A₁ × B₁ at offset 4. It doesn’t multiply A₀ by B₁ or combine the two results.

What is batched GEMM?

GEMM is general matrix multiplication. OneGpu’s GEMM kernel supports scaling the product by Alpha and adding the old output scaled by Beta. The GemmProblem constructor sets Alpha = 1 and leaves Beta = 0, so the example just multiplies and doesn’t need any existing values in the output buffer. The same shape and scaling apply to every item in the batch. If your matrix pairs have different shapes, group matching pairs together or record separate calls.

The batch stride is the distance from the start of one matrix to the next, not the distance from one row to the next. A compact row-major 2 × 2 matrix has two elements per row and four elements per matrix, so the next matrix begins four elements later, not two elements later or 16 bytes later. For padded rows use Lda, Ldb, or Ldc to set the row distance in elements, then set the batch stride to reach the next matrix’s start. Unlike the input buffers, c can’t share a matrix across the batch: it needs room for every product.

Can you multiply a 2 × 3 matrix by a 3 × 3 matrix?

Yes. The columns of A and the rows of B are both three, so K is three. The output takes its two rows from A and its three columns from B. That’s new GemmProblem(2, 3, 3): the constructor order is output rows, output columns, inner dimension. For a batch of two compact products of that shape, use strides of 6 for A, 9 for B, and 6 for C, and allocate 12 floats for the output. The stride for B is different because each B has three rows, not two.

All onegpu recipes