runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Add Gather methods for Vector<T>

Open CarolEidt opened this issue 11 years ago • 3 comments

Vector Gathers are a commonly used paradigm in vector computation - where a gather involves constructing a vector from non-contiguous elements of a source array. Gather is supported on AVX2, but even on targets that do not directly support it, such a method would be useful, in order to avoid the overhead of having to use an array to hold the elements prior to contructing the Vector.

VectorGather should perhaps be declared on the static Vector class, something like: public static Vector<T> VectorGather(T[] source, Vector<U> indexVector) where T,U : struct

Initially, I think it would be reasonable to support only same/size convert and gather – but eventually we may want to consider mixed size (e.g. a gather to a Vector using the lower values of a Vector. AVX2 has a pretty rich mixture of these.

@mellinoe what do you think?

CarolEidt avatar May 01 '15 21:05 CarolEidt

Sorry for the late response. Like I was saying in-person, I think we should definitely add something to address this scenario. Creating/storing Vector<T>s is one of the more confusing parts of the library, I think, as the only real way to create them right now is through the constructor taking an existing array.

Regarding the idea proposed here, how exactly would the method function? I'm assuming the second argument (Vector indexVector) is some sort of Vector<T> (probably something with the same number of elements as the returned Vector<T>?). I'm concerned about that because, for example, if you wanted to gather a Vector<Byte>, you would be limited to addressing only the first 256 elements of an array. Does that make sense? I may be mis-understanding that part. It's unclear how we would get around that, i.e. give a "Vector<int>" where that vector has the length of the gathered vector. Other than using another array parameter.

mellinoe avatar May 05 '15 04:05 mellinoe

Yes, you understand the function correctly. AVX2 currently only supports gathers of single, double, int and long values. It allows you to provide either int or long vectors for the elements, and will gather the max of the number of elements in the target vector and the number of elements in the index vector. We wouldn't have hardware acceleration for Vector<small-int> gathers in any case. API alternatives to consider would be 1) only support 32-bit or 64-bit integers in the indexVector, and then for smaller target elements, gather only the first N, and zero the upper elements, or 2) only support target vectors of 32-bit or 64-bit elements, and only accept same-size index vectors. I concluded that the same-size vectors are likely to present the best usage model, but I'm open to other thoughts (and I'm not an API designer).

CarolEidt avatar May 06 '15 14:05 CarolEidt

Going to close this for now. This API needs some more thought about how gathering would work with when exposed as a general-purpose API.

If someone feels strongly about this, please feel free to open a new issue. Our API review process is detailed here with a "good example" being listed under step 1. Ideally the new issue would provide many of the sections given, but would at a minimum provide the base rationale and proposed API surface sections.

For this API in particular, the exposure of the System.Runtime.Intrinsic types and the methods for converting to/from Vector<T> should unblock users who immediately need this functionality.

tannergooding avatar Mar 02 '20 18:03 tannergooding