Species-agnostic shuffle

Vladimir Ivanov vladimir.x.ivanov at oracle.com
Wed Jul 10 22:03:46 UTC 2024


I had the following in mind:

jshell>     static VectorShuffle<Long> test(VectorSpecies<Long> vs) {
    ...>         return LongVector.fromArray(vs, new long[] { 1, 0, 3, 
2, 5, 4, 7, 6 }, 0)
    ...>                         .toShuffle();
    ...>     }
    ...>

jshell> test(LongVector.SPECIES_512)
$24 ==> Shuffle[1, 0, 3, 2, 5, 4, 7, 6]

jshell> test(LongVector.SPECIES_256)
$25 ==> Shuffle[1, 0, 3, 2]

jshell> test(LongVector.SPECIES_512)
$26 ==> Shuffle[1, 0, 3, 2, 5, 4, 7, 6]

It workarounds the problem you expressed as:
   "... VectorShuffle.fromArray() expects a number of indices that 
matches *exactly* the length of the vector for the given species, ..."

Simplified the problem a bit by representing indices as longs. 
Otherwise, int-to-long vector conversion is needed.


But, I fully agree that VectorShuffle.fromOp() John suggested is a 
superior option.

Best regards,
Vladimir Ivanov

On 7/9/24 15:58, Martin Traverso wrote:
> Hi Vladimir,
> 
> Not sure I understand how that helps. Can you elaborate?
> 
> What I'm looking for is something like this:
> 
> private static <E> VectorShuffle<E> makeShuffle(VectorSpecies<E> 
> species, int[] template)
> {
>      if (species.length() % template.length != 0) {
>          throw new IllegalArgumentException();
>      }
> 
>      int[] indexes = new int[species.length()];
> 
>      for (int i = 0; i < indexes.length; i++) {
>          int index = template[i % template.length] + i - i % 
> template.length;
>          if (index < 0 || index >= species.length()) {
>              throw new IllegalArgumentException();
>          }
>          indexes[i] = index;
>      }
> 
>      return VectorShuffle.fromArray(species, indexes, 0);
> }
> 
> Essentially, you give it a "template", such as {1, 0} to indicate you 
> want those two lanes rearranged, and the function creates a shuffle 
> array that replicates that pattern up to the length of the vector for 
> the given species.
> 
> Maybe it's too niche, but without something of that sort it seems 
> impossible to write code that uses rearrange() and works with 
> SPECIES_PREFERRED.
> 
> - Martin
> 
> 
> On Tue, Jul 9, 2024 at 10:33 AM Vladimir Ivanov 
> <vladimir.x.ivanov at oracle.com <mailto:vladimir.x.ivanov at oracle.com>> wrote:
> 
>     Hi Martin,
> 
>     What about loading indices as a Vector first (using
>     IntVector.fromArray()) and then converting it to VectorShuffle using
>     Vector.toShuffle()?
> 
>     Best regards,
>     Vladimir Ivanov
> 
>     On 7/9/24 09:59, Martin Traverso wrote:
>      > Hi,
>      >
>      > I've been experimenting with the Vector API in an attempt to port
>     the
>      > XXH3 hashing algorithm to Java. One of the steps requires
>     rearranging
>      > the elements of a vector by swapping adjacent values. I.e., (v0,
>     v1, v2,
>      > v3, ...) -> (v1, v0, v3, v2, ...).
>      >
>      > One usability issue I'm running into is creating the
>     VectorShuffle in a
>      > species-agnostic manner. The VectorShuffle.fromArray() expects a
>     number
>      > of indices that matches *exactly* the length of the vector for
>     the given
>      > species, so it's harder to create a shuffle that uses
>     SPECIES_PREFERRED
>      > without additional logic to construct that array of indices
>     dynamically.
>      >
>      > To illustrate, I need to do the following for each species:
>      >
>      >      VectorShuffle.fromArray(LongVector.SPECIES_128, new int[] {
>     1, 0 }, 0)
>      >      VectorShuffle.fromArray(LongVector.SPECIES_256, new int[] {
>     1, 0,
>      > 3, 2 }, 0)
>      >      VectorShuffle.fromArray(LongVector.SPECIES_512, new int[] {
>     1, 0,
>      > 3, 2, 5, 4, 7, 6 }, 0)
>      >
>      > Of course, I could make something like this
>      >
>      >      VectorShuffle.fromArray(LongVector.SPECIES_PREFERRED,
>      > makeIndexesFromTemplate(new int[] { 1, 0 }), 0)
>      >
>      > with my own custom "makeIndexesFromTemplate" function to create the
>      > array of indices of the appropriate length.
>      >
>      > Is there anything like that in the Vector APIs that I haven't
>     been able
>      > to find? If not, would it make sense to add something like it, or
>     allow
>      > VectorShuffle.fromXXX to derive such mapping?
>      >
>      > - Martin
>      >
> 


More information about the panama-dev mailing list