|
1 | | -/* |
2 | | - * Copyright 2020, 2021, Ludovic Henry |
3 | | - * |
4 | | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | - * of this software and associated documentation files (the "Software"), to deal |
6 | | - * in the Software without restriction, including without limitation the rights |
7 | | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
8 | | - * copies of the Software, and to permit persons to whom the Software is |
9 | | - * furnished to do so, subject to the following conditions: |
10 | | - * |
11 | | - * The above copyright notice and this permission notice shall be included in |
12 | | - * all copies or substantial portions of the Software. |
13 | | - * |
14 | | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
19 | | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
20 | | - * SOFTWARE. |
21 | | - * |
22 | | - * Please contact git@ludovic.dev or visit ludovic.dev if you need additional |
23 | | - * information or have any questions. |
24 | | - */ |
25 | | - |
26 | 1 | package vecxt.benchmark |
27 | 2 |
|
28 | 3 | import org.openjdk.jmh.annotations.* |
29 | 4 | import org.openjdk.jmh.infra.Blackhole |
30 | | -// import vecxt.Matrix.* |
31 | 5 | import vecxt.BoundsCheck |
32 | 6 | import scala.compiletime.uninitialized |
33 | 7 | import vecxt.all.* |
34 | 8 | import jdk.incubator.vector.VectorSpecies |
35 | 9 | import jdk.incubator.vector.VectorOperators |
36 | 10 | import jdk.incubator.vector.DoubleVector |
| 11 | +import breeze.linalg.* |
| 12 | +import vecxt.BoundsCheck.DoBoundsCheck.no |
37 | 13 |
|
| 14 | +//% mill benchmark_vs_breeze.runJmh -jvmArgs --add-modules=jdk.incubator.vector |
38 | 15 | @State(Scope.Thread) |
39 | | -class SumBenchmark extends BLASBenchmark: |
40 | | - |
41 | | - @Param(Array("10000")) |
42 | | - var len: String = uninitialized; |
| 16 | +class LinearAlgebraWorkloadBenchmark extends BLASBenchmark: |
43 | 17 |
|
44 | | - var arr: Array[Double] = uninitialized |
| 18 | + @Param(Array("500")) |
| 19 | + var matDim: String = uninitialized |
45 | 20 |
|
46 | | - // format: off |
47 | | - @Setup(Level.Trial) |
48 | | - def setup: Unit = |
49 | | - |
50 | | - arr = randomDoubleArray(len.toInt); |
51 | | - () |
| 21 | + var dataA: Array[Double] = uninitialized |
| 22 | + var dataB: Array[Double] = uninitialized |
| 23 | + var vectorData: Array[Double] = uninitialized |
52 | 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) |
53 | 31 | end setup |
54 | 32 |
|
55 | | - extension (vec: Array[Double]) |
56 | | - |
57 | | - inline def sum2 = |
58 | | - var sum: Double = 0.0 |
59 | | - var i: Int = 0 |
60 | | - val l = spd.length() |
61 | | - |
62 | | - while i < spd.loopBound(vec.length) do |
63 | | - sum = sum + DoubleVector.fromArray(spd, vec, i).reduceLanes(VectorOperators.ADD) |
64 | | - i += l |
65 | | - end while |
66 | | - while i < vec.length do |
67 | | - sum += vec(i) |
68 | | - i += 1 |
69 | | - end while |
70 | | - sum |
71 | | - end sum2 |
72 | | - |
73 | | - inline def sum3 = |
74 | | - var sum: Double = 0.0 |
75 | | - var i: Int = 0 |
76 | | - while i < vec.length do |
77 | | - sum = sum + vec(i) |
78 | | - i = i + 1 |
79 | | - end while |
80 | | - sum |
81 | | - end sum3 |
82 | | - |
83 | | - end extension |
84 | | - |
85 | | - |
86 | | - |
87 | 33 | @Benchmark |
88 | | - def sum_loop(bh: Blackhole) = |
89 | | - val r = arr.sum3 |
90 | | - bh.consume(r); |
91 | | - end sum_loop |
| 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 step6 = step2.t // Transpose |
| 48 | + val step7 = breeze.linalg.sum(step2) // Sum reduction |
| 49 | + val step8 = (step7 > 0.5) // Comparison |
| 50 | + |
| 51 | + // Combine results to prevent dead code elimination |
| 52 | + val result = step5 + (if step8 then 1.0 else 0.0) |
| 53 | + bh.consume(result) |
| 54 | + end breezeWorkload |
92 | 55 |
|
93 | 56 | @Benchmark |
94 | | - def sum_vec(bh: Blackhole) = |
95 | | - val r = arr.sum2 |
96 | | - bh.consume(r); |
97 | | - end sum_vec |
98 | | - |
99 | | - @Benchmark |
100 | | - def sum_vec_alt(bh: Blackhole) = |
101 | | - val r = arr.sum |
102 | | - bh.consume(r); |
103 | | - end sum_vec_alt |
104 | | - |
105 | | - |
106 | | -end SumBenchmark |
| 57 | + 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)) |
| 62 | + |
| 63 | + // Same representative linear algebra workload |
| 64 | + val step1 = matA + matB // Element-wise addition |
| 65 | + val step2 = step1.hadamard(matA) // Hadamard product |
| 66 | + val step3 = step2 * vectorData // Matrix-vector multiply |
| 67 | + val step4 = step3.fma(2.0, 1.0) // Element-wise transform |
| 68 | + val step5 = step4.norm // L2 norm |
| 69 | + // val step6 = step2.transpose // Transpose |
| 70 | + val step7 = step2.sum // Sum reduction |
| 71 | + val step8 = (step7 > 0.5) // Comparison |
| 72 | + |
| 73 | + // Combine results to prevent dead code elimination |
| 74 | + val result = step5 + (if step8 then 1.0 else 0.0) |
| 75 | + bh.consume(result) |
| 76 | + end vecxtWorkload |
| 77 | + |
| 78 | +end LinearAlgebraWorkloadBenchmark |
0 commit comments