Skip to content

Commit 3471efb

Browse files
committed
some benchmarks
1 parent 5102412 commit 3471efb

3 files changed

Lines changed: 94 additions & 30 deletions

File tree

benchmark_vs_breeze/src/breezey.scala

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,11 @@ import breeze.numerics.step
1111
val dataA = DenseMatrix.rand[Double](dim, dim)
1212
val dataB = DenseMatrix.rand[Double](dim, dim)
1313
val vectorData = DenseVector.rand[Double](dim)
14-
15-
println("Breeze Data:")
16-
println(dataA)
17-
println(dataB)
18-
println(vectorData)
19-
20-
// Representative linear algebra workload
14+
// Random linear algebra workload
2115
val step1 = dataA + dataB // Element-wise addition
2216

2317
val step2 = step1 *:* dataA // Hadamard product
24-
println("Step 2 (Hadamard product):")
25-
println(step2)
18+
2619
val step3 = step2 * vectorData // Matrix-vector multiply
2720
val step4 = step3.map(_ * 2.0 + 1.0) // Element-wise transform
2821
val step5 = breeze.linalg.norm(step4) // L2 norm
@@ -40,16 +33,11 @@ import breeze.numerics.step
4033
val matB = vecxt.matrix.Matrix(dataB.toArray, (dim, dim))
4134
val vec = vectorData.toArray
4235

43-
println(matA.printMat)
44-
println(matB.printMat)
45-
println(vec.mkString("Array(", ", ", ")"))
46-
4736
// Same representative linear algebra workload
4837
val step1Vecxt = matA + matB // Element-wise addition
4938

5039
val step2Vecxt = step1Vecxt.hadamard(matA) // Hadamard product
51-
println("Step 2 (Hadamard product):")
52-
println(step2Vecxt.printMat)
40+
5341
val step3Vecxt = step2Vecxt * vec // Matrix-vector multiply
5442
val step4Vecxt = step3Vecxt.fma(2.0, 1.0) // Element-wise transform
5543
val step5Vecxt = vecxt.all.norm(step4Vecxt) // L2 norm

benchmark_vs_breeze/src/doSomeStuff.scala

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,37 @@ class LinearAlgebraWorkloadBenchmark extends BLASBenchmark:
2222
var dataB: Array[Double] = uninitialized
2323
var vectorData: Array[Double] = uninitialized
2424

25-
@Setup(Level.Invocation)
25+
// Pre-created matrices for pure computation benchmarking
26+
var breezeMatA: DenseMatrix[Double] = uninitialized
27+
var breezeMatB: DenseMatrix[Double] = uninitialized
28+
var breezeVec: DenseVector[Double] = uninitialized
29+
30+
var vecxtMatA: vecxt.matrix.Matrix[Double] = uninitialized
31+
var vecxtMatB: vecxt.matrix.Matrix[Double] = uninitialized
32+
33+
@Setup(Level.Trial) // Only once per iteration
2634
def setup(): Unit =
2735
val dim = matDim.toInt
2836
dataA = randomDoubleArray(dim * dim)
2937
dataB = randomDoubleArray(dim * dim)
3038
vectorData = randomDoubleArray(dim)
39+
40+
// Pre-create matrices to exclude construction overhead
41+
breezeMatA = new DenseMatrix(dim, dim, dataA.clone())
42+
breezeMatB = new DenseMatrix(dim, dim, dataB.clone())
43+
breezeVec = new DenseVector(vectorData.clone())
44+
45+
vecxtMatA = vecxt.matrix.Matrix(dataA.clone(), (dim, dim))
46+
vecxtMatB = vecxt.matrix.Matrix(dataB.clone(), (dim, dim))
3147
end setup
3248

3349
@Benchmark
3450
def breezeWorkload(bh: Blackhole): Unit =
35-
val dim = matDim.toInt
36-
// Create matrices and vector from the same data
37-
val matA = new DenseMatrix(dim, dim, dataA)
38-
val matB = new DenseMatrix(dim, dim, dataB)
39-
val vec = new DenseVector(vectorData)
4051

4152
// Representative linear algebra workload
42-
val step1 = matA + matB // Element-wise addition
43-
val step2 = step1 *:* matA // Hadamard product
44-
val step3 = step2 * vec // Matrix-vector multiply
53+
val step1 = breezeMatA + breezeMatB // Element-wise addition
54+
val step2 = step1 *:* breezeMatA // Hadamard product
55+
val step3 = step2 * breezeVec // Matrix-vector multiply
4556
val step4 = step3.map(_ * 2.0 + 1.0) // Element-wise transform
4657
val step5 = breeze.linalg.norm(step4) // L2 norm
4758
// val step6 = step2.t // Transpose
@@ -55,14 +66,10 @@ class LinearAlgebraWorkloadBenchmark extends BLASBenchmark:
5566

5667
@Benchmark
5768
def vecxtWorkload(bh: Blackhole): Unit =
58-
val dim = matDim.toInt
59-
// Create matrices and vector from the same data
60-
val matA = vecxt.matrix.Matrix(dataA, (dim, dim))
61-
val matB = vecxt.matrix.Matrix(dataB, (dim, dim))
6269

6370
// Same representative linear algebra workload
64-
val step1 = matA + matB // Element-wise addition
65-
val step2 = step1.hadamard(matA) // Hadamard product
71+
val step1 = vecxtMatA + vecxtMatB // Element-wise addition
72+
val step2 = step1.hadamard(vecxtMatA) // Hadamard product
6673
val step3 = step2 * vectorData // Matrix-vector multiply
6774
val step4 = step3.fma(2.0, 1.0) // Element-wise transform
6875
val step5 = step4.norm // L2 norm
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package vecxt.benchmark
2+
3+
import org.openjdk.jmh.annotations.*
4+
import org.openjdk.jmh.infra.Blackhole
5+
import vecxt.BoundsCheck
6+
import scala.compiletime.uninitialized
7+
import vecxt.all.*
8+
import jdk.incubator.vector.VectorSpecies
9+
import jdk.incubator.vector.VectorOperators
10+
import jdk.incubator.vector.DoubleVector
11+
import breeze.linalg.*
12+
import vecxt.BoundsCheck.DoBoundsCheck.no
13+
14+
//% mill benchmark_vs_breeze.runJmh -jvmArgs --add-modules=jdk.incubator.vector
15+
@State(Scope.Thread)
16+
class LazerBenchmark extends BLASBenchmark:
17+
18+
@Param(Array("500"))
19+
var matDim: String = uninitialized
20+
21+
var dataA: Array[Double] = uninitialized
22+
var dataB: Array[Double] = uninitialized
23+
var vectorData: Array[Double] = uninitialized
24+
25+
@Setup(Level.Invocation)
26+
def setup(): Unit =
27+
val dim = matDim.toInt
28+
dataA = randomDoubleArray(dim * dim)
29+
dataB = randomDoubleArray(dim * dim)
30+
vectorData = randomDoubleArray(dim)
31+
end setup
32+
33+
@Benchmark
34+
def breezeWorkload(bh: Blackhole): Unit =
35+
val dim = matDim.toInt
36+
// Create matrices and vector from the same data
37+
val matA = new DenseMatrix(dim, dim, dataA)
38+
val matB = new DenseMatrix(dim, dim, dataB)
39+
val vec = new DenseVector(vectorData)
40+
41+
// Representative linear algebra workload
42+
val step1 = matA + matB // Element-wise addition
43+
val step2 = step1 *:* matA // Hadamard product
44+
val step3 = step2 * vec // Matrix-vector multiply
45+
val step4 = step3.map(_ * 2.0 + 1.0) // Element-wise transform
46+
val step5 = breeze.linalg.norm(step4) // L2 norm
47+
val step7 = breeze.linalg.sum(step2) + step5 // Sum reduction
48+
bh.consume(step7)
49+
end breezeWorkload
50+
51+
@Benchmark
52+
def vecxtWorkload(bh: Blackhole): Unit =
53+
val dim = matDim.toInt
54+
// Create matrices and vector from the same data
55+
val matA = vecxt.matrix.Matrix(dataA, (dim, dim))
56+
val matB = vecxt.matrix.Matrix(dataB, (dim, dim))
57+
58+
// Same representative linear algebra workload
59+
val step1 = matA + matB // Element-wise addition
60+
val step2 = step1.hadamard(matA) // Hadamard product
61+
val step3 = step2 * vectorData // Matrix-vector multiply
62+
val step4 = step3.fma(2.0, 1.0) // Element-wise transform
63+
val step5 = step4.norm // L2 norm
64+
val step7 = step2.sum + step5 // Sum reduction
65+
66+
bh.consume(step7)
67+
end vecxtWorkload
68+
69+
end LazerBenchmark

0 commit comments

Comments
 (0)