Recursive AI

Generate an arange tensor 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 EncodeRange to fill a GPU buffer with evenly spaced indices. Requires Vulkan 1.3 on Windows or Linux.

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();
using var kernels = new IntegerKernels(gpu);
int start = 8, limit = 0, step = -2;
int count = IntegerKernels.RangeLength(
    start, limit, step);
using var indices =
    gpu.CreateBuffer<int>(count);
var commands = gpu.BeginCommands();
kernels.EncodeRange(commands, indices,
    0, count, start, step);
gpu.Submit(commands);
 
var values = new int[count];
await indices.ReadAsync(values);
Console.WriteLine(string.Join(", ", values));

Unzip the example, then dotnet run.

Prints:

Text
8, 6, 4, 2

The full program

RecursiveAI.OneGpu.Backends creates the device; RecursiveAI.OneGpu.Kernels supplies IntegerKernels. The program compares the GPU readback with a descending C# loop. RangeLength throws GpuKernelShapeException for a zero step.

C#
using System;
using RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Kernels;
 
using var gpu = GpuBackends.CreateDevice();
using var kernels = new IntegerKernels(gpu);
int start = 8, limit = 0, step = -2;
int count = IntegerKernels.RangeLength(
    start, limit, step);
using var indices =
    gpu.CreateBuffer<int>(count);
var commands = gpu.BeginCommands();
kernels.EncodeRange(commands, indices,
    0, count, start, step);
gpu.Submit(commands);
 
var values = new int[count];
await indices.ReadAsync(values);
Console.WriteLine(string.Join(", ", values));
 
int i = 0;
for (int value = start; value > limit; value += step)
{
    if (i >= values.Length || values[i++] != value)
        throw new InvalidOperationException("GPU and CPU ranges differ.");
}
if (i != values.Length)
    throw new InvalidOperationException("GPU range has extra values.");
Console.WriteLine("Matches the CPU sequence.");
Console.WriteLine($"{gpu.Backend}: {gpu.Adapter.Name}");

Output from dotnet run:

Text
8, 6, 4, 2
Matches the CPU sequence.
Vulkan: NVIDIA GeForce RTX 4070

Variations

Count up from zero

In the snippet at the top, replace the three range values with these to print 0, 2, 4, 6:

C#
int start = 0, limit = 8, step = 2;

The short answer

Size a GpuBuffer<int> with IntegerKernels.RangeLength(start, limit, step). Use EncodeRange to record the fill, then submit the commands. The buffer is the storage for a one-dimensional tensor of indices. No CPU array of index values is uploaded; the array in the example is only for reading the result back. If the next operation is on the GPU, pass the same buffer to it and skip the readback.

What does arange do?

It generates a one-dimensional sequence given a first value, a stopping bound and a step. In OneGpu, the range kernel and the length calculation are separate. RangeLength calculates the element count on the CPU. EncodeRange doesn’t take the limit: it writes start + i * step for exactly that many elements after you submit the commands. Its offset is measured in elements, not bytes. An offset of zero starts at the first slot; a nonzero offset lets you place the sequence in part of an existing buffer.

Use an int buffer for 32-bit indices or long for 64-bit values. The range API also supports float for a fractional step. For float, the count is computed in double precision from the float inputs; the GPU rounds multiplication and addition separately in float32. For exact integer indices, use an integer buffer rather than casting a float range.

Is the arange stop value included with a negative step?

No. With a negative step, the range contains values greater than the limit; with a positive step, it contains values less than the limit. Starting at 8 and stepping by -2 toward 0 yields four values: 8, 6, 4, 2. Changing the limit to -1 would also include 0. The limit need not land on a step. For example, starting at 0 with a limit of 7 and a step of 2 gives 0, 2, 4, 6.

Equal start and limit gives a count of zero. So does a step pointing away from the limit, such as start = 0, limit = 8, step = -2. If you accept these inputs from a caller, check the count before allocating a GPU buffer and return an empty result instead of dispatching a fill.

All onegpu recipes