[API Proposal]: TensorPrimitives.Sequential

Open
#111,141 10 comments 0 reactions 1 assignee View on GitHub

@tannergooding is already working on this.

Since Jan 16, 2025.

Assessment

This issue has not been assessed yet.

Description

api-suggestion area-System.Numerics.Tensors needs-further-triage
Background and motivation

A common operation initializes a Span/Array with sequential values, e.g. arr[i] == i.
This is something that can be easily vectorized and seems useful even if perf is not critical for the consumer (e.g. it will be a correct implementation of something trivial, but even trivial code sometimes has bugs).

API Proposal

public static void Sequential(Span<int> x, int startValue = 0);

API Usage

var indices = new int[data.Length]
var values = data.Select(x => x.SortKey).ToArray();
TensorPrimitives.Sequential(indices, 0);
MemoryExtensions.Sort(values, indices);
Console.WriteLine($"The first sorted entry has index {indices[0]}");

Alternative Designs

Enumerable.Range() already exists, but it is not very performant/can't work with an already allocated array or span

The method itself could be called Range instead of Sequential.

Risks

Anyone can write a for loop to initialize these spans/arrays or use Enumerable.Range(...).ToArray(). The vectorization gain might be small depending on the context (e.g, sorting in the example above would most likely take a lot more time than what you save on the initialization.

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.