Код: Выделить всё
inline def sum2 =
var sum: Double = 0.0
var i: Int = 0
inline val species = DoubleVector.SPECIES_256
while i < species.loopBound(vec.length) do
val vec2: DoubleVector = DoubleVector.fromArray(species, vec, i)
sum = sum + vec2.reduceLanes(VectorOperators.ADD)
i += species.length()
end while
while i < vec.length do
sum += vec(i)
i += 1
end while
sum
end sum2
inline def sum: Double =
var sum = 0.0
var i = 0;
while i < vec.length do
sum = sum + vec(i)
i = i + 1
end while
sum
inline def sum3 =
var i: Int = 0
val species = DoubleVector.SPECIES_256
var acc = DoubleVector.zero(species)
while i < species.loopBound(vec.length) do
val vec2: DoubleVector = DoubleVector.fromArray(species, vec, i)
acc =
acc.add(vec2) // This line, appears to break JMH. It's not clear why as it passes a unit test and compiles.
i += species.length()
end while
var temp = acc.reduceLanes(VectorOperators.ADD)
// var temp = 0.0
while i < vec.length do
temp += vec(i)
i += 1
end while
temp
end sum3
Код: Выделить всё
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Thread)
@Fork(value = 1)
@Warmup(iterations = 3)
@Measurement(iterations = 3)
abstract class BLASBenchmark:
private final val rand: Random = new Random(0);
protected def randomDouble(): Double =
return rand.nextDouble();
protected def randomDoubleArray(n: Int): Array[Double] =
val res = new Array[Double](n);
for i
Подробнее здесь: [url]https://stackoverflow.com/questions/78997148/problems-with-java-vector-api-to-sum-a-list-of-doubles[/url]