Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: SIMD/Vectorial operations on arrays/Spans #60559

Closed
vpenades opened this issue Oct 18, 2021 · 4 comments
Closed

[API Proposal]: SIMD/Vectorial operations on arrays/Spans #60559

vpenades opened this issue Oct 18, 2021 · 4 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Numerics
Milestone

Comments

@vpenades
Copy link

Background and motivation

In image processing and probably many other fields, it is fairly common to perform basic operations on large arrays, for example:

var left = new float[10000];
var right = new float[10000];

var result = new float[10000];

for(int i=0; i < 10000; ++i) result[i] = left[i] * right[i];

These operations can be made a lot faster if the operands are casted to Vector4, which make the whole operation much faster. But probably there's even more performance improvements that can be done at low level.

API Proposal

Something like this:

static class BulkOperations
{
// sum 'value' to all destination elements
public static void Sum(float value, Span<float> destination);

// sum all 'values' to destination
public static void Sum(ReadOnlySpan<float> values, Span<float> destination);

// sum left and right into destination
public static void Sum(ReadOnlySpan<float> left, float right, Span<float> destination);

// sum left and right into destination
public static void Sum(ReadOnlySpan<float> left, ReadOnlySpan<float> right, Span<float> destination);

// multiply left and right into destination
public static void Multiply(ReadOnlySpan<float> left, ReadOnlySpan<float> right, Span<float> destination);

// cast source into destination, applying a mean and scale factor
// this is typically used to convert float values in the 0-1 range to bytes in the 0-255 range
public static void Cast(ReadOnlySpan<float> source, float mean, float scale, Span<byte> destination);
}

This is just a small example of the functions the API would have, obviously there would be much more: multiply, division, absolute values, casting, byte shifting, and all kinds of bulk operations that can be greatly enhanced with simd/vectorial instrinsics.

API Usage

var left = new float[10000];
var right = new float[10000];

var result = new float[10000];

BulkOperations.Sum(left, right, result);

Alternative Designs

No response

Risks

No response

@vpenades vpenades added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Oct 18, 2021
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Numerics untriaged New issue has not been triaged by the area owner labels Oct 18, 2021
@AaronRobinsonMSFT
Copy link
Member

/cc @tannergooding

@jeffhandley
Copy link
Member

Tagging @dotnet/area-system-numerics since the bot didn't.

@tannergooding tannergooding added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed untriaged New issue has not been triaged by the area owner labels Jul 15, 2022
@tannergooding tannergooding added this to the Future milestone Jul 15, 2022
@huoyaoyuan
Copy link
Member

This is covered by TensorPrimitives in #89639, for example TensorPrimitives.Add(left, right, result).

@vpenades
Copy link
Author

then I think we can close this issue as resolved.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 12, 2023
@tannergooding tannergooding removed the needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration label Jun 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Numerics
Projects
None yet
Development

No branches or pull requests

5 participants