[vector] Prototype of apply() method on Vector
Brian Goetz
brian.goetz at oracle.com
Tue Feb 19 22:32:56 UTC 2019
>> The latter is where I see the major value; vectorized APIs won't want to
>> expose vectors, they'll want to let users pass in arrays of values and do
>> vectorized operations over them. Because the array size may not be an
>> integral multiple of the lane count, there may be a "tail" of values that have
>> to be computed "the old fashioned way". And that's not the really bad part;
>> the really bad part is that the main vectorized loop and the "tail" loop will
>> spell "add" differently from each other. And having two ways to say the
>> same thing is a bug waiting to happen.
>>
> Tail loop written with the current implementation would look something like this,
>
> c[i] = UOp.NEG.apply(BOp.ADD.apply(BOp.MUL.apply(a[i], a[i]), BOp.MUL.apply(b[i], b[i])));
>
> I am afraid this is not very similar to vector operations below, but it is consistent in using the same Operators defined in Vector package (and not Java operations).
What would be great, though I think it might be hard to get there, is to
have the Op APi support composition of ops into new ops, so that we
could describe "multiply then add then negate" as a new Op, and then
just apply (and reuse) that Op. However, I worry this would eventually
get complicated enough to lose the thread of constancy that the compiler
requires in order to cleanly vectorize. So I'm not sure how much gas
this idea has, though it sounds pretty.
> We could add new method - 'apply(Vector, Vector)', to vector operators, which would allow vector operations to written as above.
Yes, or even separate overloads for types, such as: apply(IntVector,
IntVector).
> ADD (VectorIntrinsics.VECTOR_OP_ADD) {
> /* new method*/
> <E> Vector<E> apply(Vector<E> opd1, Vector<E> opd2) {
> return opd1.apply(this, opd2);
> }
> ...
> }
>
> Now, vector operations can be written as,
> UOp.NEG.apply(BOp.ADD.apply(BOp.MUL.apply(av, av), BOp.MUL.apply(bv, bv))).intoArray(c, i);
Statically importing the ops helps a tiny bit:
NEG.apply(ADD.apply(MUL.apply(av, av), MUL.apply(bv, bv)))
.intoArray(c, i);
More information about the panama-dev
mailing list