Skip to content

Commit

Permalink
benchmark logicals
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Sep 26, 2024
1 parent 2b65c00 commit c6474a1
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 27 deletions.
81 changes: 81 additions & 0 deletions benchmark/src/logical.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2020, 2021, Ludovic Henry
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Please contact git@ludovic.dev or visit ludovic.dev if you need additional
* information or have any questions.
*/

package vecxt.benchmark

import org.openjdk.jmh.annotations.*
import org.openjdk.jmh.infra.Blackhole
// import vecxt.Matrix.*
import vecxt.BoundsCheck
import scala.compiletime.uninitialized
import vecxt.*
import jdk.incubator.vector.VectorSpecies
import jdk.incubator.vector.VectorOperators
import jdk.incubator.vector.DoubleVector

@State(Scope.Thread)
class LogicalBenchmark extends BLASBenchmark:

@Param(Array("3", "128", "100000"))
var len: String = uninitialized;

var arr: Array[Double] = uninitialized


// format: off
@Setup(Level.Trial)
def setup: Unit =
arr = randomDoubleArray(len.toInt);
()
end setup

extension (vec: Array[Double])
inline def lte2(num: Double) =
val idx: Array[Boolean] = new Array[Boolean](vec.length)
var i = 0

while i < vec.length do
idx(i) = vec(i) <= num
i += 1
end while
idx
end extension

@Benchmark
def lte_vec(bh: Blackhole) =
val r = arr <= 4.0
bh.consume(r);
end lte_vec



@Benchmark
def lte_loop(bh: Blackhole) =
val r = arr.lte2(4.0)
bh.consume(r);
end lte_loop

end LogicalBenchmark

2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ benchmark:
mill benchmark.runJmh -jvmArgs --add-modules=jdk.incubator.vector -rf json

benchmarkOnly:
mill benchmark.runJmh -jvmArgs --add-modules=jdk.incubator.vector -rf json vecxt.benchmark.OrBooleanBenchmark
mill benchmark.runJmh -jvmArgs --add-modules=jdk.incubator.vector -rf json vecxt.benchmark.LogicalBenchmark

setJvm:
eval "$(cs java --jvm 21 --env)"
49 changes: 26 additions & 23 deletions vecxt/jvm/src/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,9 @@ object extensions:
newVec
end apply


/**
* Apparently, left packing is hard problem in SIMD land.
* https://stackoverflow.com/questions/79025873/selecting-values-from-java-simd-doublevector
*
*/
/** Apparently, left packing is hard problem in SIMD land.
* https://stackoverflow.com/questions/79025873/selecting-values-from-java-simd-doublevector
*/

// inline def apply(index: Array[Boolean])(using inline boundsCheck: BoundsCheck): Array[Double] =
// dimCheck(vec, index)
Expand Down Expand Up @@ -382,11 +379,17 @@ object extensions:
vec.clone.tap(_ /= d)
end /

inline def =:=(num: Double): Array[Boolean] =
logicalIdx(VectorOperators.EQ, num)

inline def !:=(num: Double): Array[Boolean] =
logicalIdx(VectorOperators.NE, num)

inline def <(num: Double): Array[Boolean] =
logicalIdx( VectorOperators.LT, num)
logicalIdx(VectorOperators.LT, num)

inline def <=(num: Double): Array[Boolean] =
logicalIdx( VectorOperators.LE, num)
logicalIdx(VectorOperators.LE, num)

inline def >(num: Double): Array[Boolean] =
logicalIdx(VectorOperators.GT, num)
Expand All @@ -395,30 +398,30 @@ object extensions:
logicalIdx(VectorOperators.GE, num)

inline def logicalIdx(
inline op: VectorOperators.Comparison,
inline op: VectorOperators.Comparison,
num: Double
): Array[Boolean] =
val species = Matrix.doubleSpecies
val l = species.length()
val idx = new Array[Boolean](vec.length)
val idx = new Array[Boolean](vec.length)
var i = 0

while i < species.loopBound(vec.length) do
DoubleVector.fromArray(species, vec, i).compare(op, num).intoArray(idx, i)
DoubleVector.fromArray(species, vec, i).compare(op, num).intoArray(idx, i)
i += l
end while

inline op match
// case VectorOperators.EQ =>
// while i < vec.length do
// idx(i) = vec(i) == num
// i += 1
// end while
// case VectorOperators.NE =>
// while i < vec.length do
// idx(i) = vec(i) != num
// i += 1
// end while
case VectorOperators.EQ =>
while i < vec.length do
idx(i) = vec(i) == num
i += 1
end while
case VectorOperators.NE =>
while i < vec.length do
idx(i) = vec(i) != num
i += 1
end while
case VectorOperators.LT =>
while i < vec.length do
idx(i) = vec(i) < num
Expand All @@ -430,8 +433,8 @@ object extensions:
idx(i) = vec(i) <= num
i += 1
end while
case VectorOperators.GT =>

case VectorOperators.GT =>
while i < vec.length do
idx(i) = vec(i) > num
i += 1
Expand Down
6 changes: 3 additions & 3 deletions vecxt/test/src/arrayExtensions.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ArrayExtensionSuite extends munit.FunSuite:

val v2 = NArray[Double](1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)
val vIdx2 = NArray[Boolean](true, false, true, true, false, true, false, true, false)
val afterIndex2 = v2(vIdx2)
val afterIndex2 = v2(vIdx2)
assertEqualsDouble(afterIndex2(4), 8.0, 0.0001)

}
Expand Down Expand Up @@ -160,8 +160,8 @@ class ArrayExtensionSuite extends munit.FunSuite:
test("<= big") {
val n = 50000
val rand = scala.util.Random
val vec = NArray.tabulate(n)(_ => rand.nextDouble())
assertEqualsDouble((vec <= 0.2).countTrue / n.toDouble, 0.2, 0.01 )
val vec = NArray.tabulate(n)(_ => rand.nextDouble())
assertEqualsDouble((vec <= 0.2).countTrue / n.toDouble, 0.2, 0.01)
}

test("<=") {
Expand Down

0 comments on commit c6474a1

Please sign in to comment.