Vector API factories

Paul Sandoz paul.sandoz at oracle.com
Sat Jul 15 19:15:05 UTC 2017


Hi Ian,

There is definitely a tension with a Vector instance being used as the factory, with lots of methods that are not relevant and may throw USO or have conversion constraints.

Is it reasonable to assume that it’s preferable to gentrify a vector algorithm over shape rather than element.

If that is likely one could work with a semi-concrete species (which should be passed in to the generic-shape algorithm):

  interface Species<E, S extends Vector.Shape<Vector<?, ?>> {
    Vector<E, S> zero();

    public Mask<E, S> constantMask(boolean... bits) { … }

    public Shuffle<E, S> constantShuffle(int... ixs)  { … }
  }

  public abstract class SpeciesFloat<S extends Vector.Shape<Vector<?, ?>> implements Species<Float, S> {

    // Scalar-based factories

    public Vector<Float, S> single(float v) { … }

    public Vector<Float, S> broadcast(float v) { … }

    public Vector<Float, S> wither(Vector<Float, S> v, int i, float v) { … }


    // Array-based factories

    public Vector<Float, S> fromArray(...) { … }

    public Vector<Float, S> fromByteBuffer(...) { … }

    public Vector<Float, S> fromFloatBuffer(...) { … }


    // Scalar-based accessor

    public float get(Vector<Float, S>, int i) { … }


    // Array based sinks

    // + mask variants
    public void intoArray(Vector<Float, S> v, …) { … }

    public void intoByteBuffer(Vector<Float, S> v, …) { … }

    public void intoFloatBuffer(Vector<Float, S> v, …) { … }
  }

Factories are then mostly contained according to the primitive element type, but still easily accessible. Converting between vectors of different elements would require a cast.

Also i am wondering about the horizontal reductions now returning a Vector. I can see why you did this, we really don’t want the primitive boxes being exposed and you can easily reuse the result in further calculations.

This works well for the fixed sized small vectors but what if we have different kinds of shape that signify a larger size?

An alternative is to move those reductions into the semi-concrete species. If there is a method to construct a new vector with an element at the first position then i bet the JIT can optimize to avoid register shuffling.

We could of course do the same with a semi-concrete FloatVector, IntVector etc etc. but my intuition at the moment is to use species if we go this route.

Paul.


More information about the panama-dev mailing list