Index generation

Paul Sandoz paul.sandoz at oracle.com
Mon Sep 30 19:33:38 UTC 2024


Hi Simon,

When you compute a species using VectorSpecies.of you need to store the result it in a static final field and then access the species from that field. (There are similar restrictions for VarHandles and MethodHandles.)

This informs the compiler that the species is a compile time constant and therefore it can optimize the expressions that use it and generate optimal hardware instructions for the species's shape.

Paul.

On Sep 30, 2024, at 8:44 AM, Simon Parten <quafadas at gmail.com> wrote:

Hello Project Panama,

Apologies, if this is the wrong place - first time. I’ve been benchmarking index generation (I want to do linear algebra) with Panama, and obtained the following (surprising to me) results.

Benchmark                     (len)   Mode  Cnt       Score        Error  Units
IndexBenchmark.index_loop     10000  thrpt    3  476661.430 ± 333415.679  ops/s
IndexBenchmark.index_vec      10000  thrpt    3  821343.888 ±  22086.257  ops/s
IndexBenchmark.index_vec_bad  10000  thrpt    3   31524.934 ±  59643.768  ops/s

The first two implementations are the “obvious” ones. The third one attempts to instantiate the species differently. My ultimate goal, is to get as DoubleVector SPECIES_PREFFERED, and then calculate indexes for each, then put it in an array (i.e. have half the number of the preferred integer species for the platform).


@Benchmark
def index_loop(bh: Blackhole) =
val arr = Array.ofDim[Int](lenInt)
var i = 0
while (i < lenInt) do
arr(i) += 1
i += 1
end while

bh.consume(arr);
end index_loop

@Benchmark
def index_vec(bh: Blackhole) =
val arr = Array.ofDim[Int](lenInt)
val sp = IntVector.SPECIES_PREFERRED
val l = sp.length()
var i = 0

while i < sp.loopBound(lenInt) do
val v : IntVector = IntVector.broadcast(sp, i).addIndex(1)
v.intoArray(arr, i)
i += l
end while

bh.consume(arr);
end index_vec


@Benchmark
def index_vec_bad(bh: Blackhole) =
val arr = Array.ofDim[Int](lenInt)
val spi = IntVector.SPECIES_PREFERRED
val sp = VectorSpecies.of(java.lang.Integer.TYPE, spi.vectorShape())
val l = sp.length()
var i = 0

while i < sp.loopBound(lenInt) do
val v : IntVector = IntVector.broadcast(sp, i).addIndex(1)
v.intoArray(arr, i)
i += l
end while

bh.consume(arr);
end index_vec_bad



However, constructing the VectorSpecies through the VectorSpecies.of method, appears to have a catastrophic performance impact. Would that be expected? Am I doing something obviously wrong here?

Kind Regards,
Simon


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20240930/79a21287/attachment-0001.htm>


More information about the panama-dev mailing list