Skip to content

Add SpanExtensions.AsVector #23688

Closed
Closed

Description

@KrzysztofCwalina

Final/Approved Proposal

https://github.com/dotnet/corefx/issues/24343#issuecomment-349418624

namespace System.Numerics
{
    public struct Vector<T>
    {
        // Existing API
        public Vector(T[] values);
        // Proposed API
        public Vector(Span<T> values);
    }
}

Original Proposal

It's quite common to want to convert a Span to Vector. It's possible to open code such conversion, but the code is a bit tricky and people do it commonly enough that we shoudl just add a helper to SpanExtensions

public static Vector<T> AsVector<T>(this Span<T> slice) {
    if (slice.Length < Vector<T>.Count) throw new ArgumentOutOfRangeException();
    return Unsafe.ReadUnaligned<Vector<T>>(ref Unsafe.As<T, byte>(ref slice.DangerousGetPinnableReference()));
}

cc: @jkotas, @mellinoe, @ahsonkhan, @benaadams, @stephentoub

Activity

  1. mellinoe commented on Sep 29, 2017

    @mellinoe
    Contributor

    Is converting your Span<T> into a Span<Vector<T>> a viable / safe option? That would let you use the span normally, through the indexer.

  2. benaadams commented on Sep 29, 2017

    @benaadams
    Member

    Don't need the ReadUnaligned?

    public static Vector<T> AsVector<T>(this Span<T> slice) where T : struct
    {
        if (slice.Length < Vector<T>.Count) throw new ArgumentOutOfRangeException();
        return Unsafe.As<T, Vector<T>>(ref slice.DangerousGetPinnableReference());
    }

    What would the usage be? Something like:

    while (span.Length >= Vector<byte>.Count)
    {
        var vec = span.AsVector();
        // stuff with vec
        span = span.Slice(0, Vector<byte>.Count);
    }

    Is converting your Span into a Span<Vector> a viable / safe option? That would let you use the span normally, through the indexer.

    Something like?

    var vectorSpan = span.NonPortableCast<byte, Vector<byte>>();
    for(var i = 0; i < vectorSpan.Length; i++)
    {
        var vec = vectorSpan[i];
        // stuff with vec
    }
  3. mellinoe commented on Sep 29, 2017

    @mellinoe
    Contributor

    Something like?

    Right, and then you could just read and write directly to the indexer.

    vectorSpan[i] ^= mask;
    vectorSpan[i] += vectorSpan[i + 1];
    // Etc.
  4. benaadams commented on Sep 29, 2017

    @benaadams
    Member

    Merging the two... What about AsVectorSpan so you don't need to invoke NonPortableCast (as a user)?

    public static Span<Vector<T>> AsVectorSpan<T>(this Span<T> slice) where T : struct
    {
        return slice.NonPortableCast<T, Vector<T>>();
    }
  5. stephentoub commented on Sep 29, 2017

    @stephentoub
    Member

    With the earlier example:

    while (span.Length >= Vector<byte>.Count)
    {
        Vector<byte> vec = span.AsVector();
        // stuff with vec
        span = span.Slice(0, Vector<byte>.Count);
    }

    presumably it's followed by a loop on the remaining bytes:

    while (span.Length >= Vector<byte>.Count)
    {
        Vector<byte> vec = span.AsVector();
        // stuff with vec
        span = span.Slice(0, Vector<byte>.Count);
    }
    
    for (int i = 0; i < span.Length; i++)
    {
        // stuff with span[i]
    }

    What does that look like with this Span<Vector<T>> version?

  6. mellinoe commented on Sep 29, 2017

    @mellinoe
    Contributor

    I think it would be something like this:

    Span<T> span;
    Span<Vector<T>> vSpan = Convert(span);
    for (int i = 0; i < vSpan.Length; i++)
    {
        // Do stuff with vSpan[i]
    }
    
    span = span.Slice(Vector<T>.Count * vSpan.Length);
    for (int i = 0; i < span.Length; i++)
    {
        // stuff with span[i]
    }
  7. benaadams commented on Sep 30, 2017

    @benaadams
    Member

    What does that look like with this Span<Vector> version?

    Depends if slice first or hard fail on conversion

    var vSpan = span.Slice(0, span.Length % Vector<byte>.Count).AsVectorSpan();
    for(var i = 0; i < vSpan.Length; i++)
    {
        var vec = vSpan[i];
        // stuff with vec
    }
    
    span = span.Slice(Vector<byte>.Count * vSpan.Length);
    for (int i = 0; i < span.Length; i++)
    {
        // stuff with span[i]
    }

    or as part of a Try call that does a partial amount

    if (span.TryAsVectorSpan(out var vSpan))
    {
        for (var i = 0; i < vSpan.Length; i++)
        {
            var vec = vSpan[i];
            // stuff with vec
        }
    
        span = span.Slice(Vector<byte>.Count * vSpan.Length);
    }
    
    for (int i = 0; i < span.Length; i++)
    {
        // stuff with span[i]
    }

    With a TryAsVectorSpan

    public static bool TryAsVectorSpan<T>(this Span<T> slice, out Span<Vector<T>> vectorSpan) where T : struct
    {
        var count = slice.Length % Vector<byte>.Count;
        if (count > 0)
        {
            vectorSpan = slice.Slice(0, count).NonPortableCast<T, Vector<T>>();
            return true;
        }
    
        vectorSpan = default;
        return false;
    }

    May also have predetermined fixed sized Vector processing that has no extra elements

    public static Span<Vector2> AsVector2Span(this Span<byte> slice) 
    {
        return slice.NonPortableCast<byte, Vector2>();
    }
    public static Span<Vector3> AsVector3Span(this Span<byte> slice) 
    {
        return slice.NonPortableCast<byte, Vector3>();
    }
    public static Span<Vector4> AsVector4Span(this Span<byte> slice) 
    {
        return slice.NonPortableCast<byte, Vector4>();
    }

    Probably the extensions should live in namespace System.Numerics?

  8. benaadams commented on Sep 30, 2017

    @benaadams
    Member

    .AsVector forces it to local var; which has the advantage of more likely dealing with vector registers; however would require the (Vector size) bounds check to be hoisted across slicing which may be more tricky? Also updating the Vector in the span would be harder for writes.

    .AsVectorSpan the bounds check (at Vector size) is manually hoisted via the api; and write backs are easier; so if you wanted to apply a in-place masking on a stream you could do

    for (var i = 0; i < vSpan.Length; i++)
    {
       var vec = vSpan[i];
       vec ^= mask;
       vSpan[i] = vec;
    }

    Though it may lead to more direct operations on the Vector elements, so shunting from memory and registers more?

  9. ahsonkhan commented on Oct 28, 2017

    @ahsonkhan
    Contributor

    @KrzysztofCwalina, is the proposal ready for review?

  10. terrajobst commented on Dec 5, 2017

    @terrajobst
    Contributor

    Video

    We should align it with the existing APIs, so I'd rather make this a ctor on Vector<T> itself:

    namespace System.Numerics
    {
        public struct Vector<T>
        {
            // Existing API
            public Vector(T[] values);
            // Proposed API
            public Vector(Span<T> values);
        }
    }
  11. karelz commented on Dec 5, 2017

    @karelz
    Member

    FYI: The API review discussion was recorded - see https://youtu.be/BI3iXFT8H7E?t=5251 (11 min duration)

  12. WinCPP commented on Jan 21, 2018

    @WinCPP
    Contributor

    To make sure I understand the ask and, the conclusion in the recording... this is a constructor that "copies" the contents of the span into the vector; no re-interpretation of span's is expected here...

    
    EDIT: I had code ready, based on the understanding, I have raised the PR. Kindly review and let me know.
    
  13. added a commit that references this issue on Jan 21, 2018
  14. 13 remaining items

  15. transferred this issue fromdotnet/corefxon Jan 31, 2020
  16. added this to the 2.1.0 milestone on Jan 31, 2020
  17. ghost locked as resolved and limited conversation to collaborators on Dec 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Memoryhelp wanted

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions